compiler-constructionintermediate-languagetac

Three-Address-Code (TAC) and Conjunction/Disjunction


I want to translate a conjunction (same for disjunction) to TAC.

int a;
int b;

bool c = a > 1 && b < 5;

The post how-to-represent-binary-logical-in-three-address-code answers the first half of my question. But it doesn't answer, how the result shall be handled. My solution would be as following:

    if a <= 1 goto L1
    _t1 = b < 5
    goto L2
L1: _t1 = 0
L2: c = _t1

The problem is, that I read (because of later optimization issues), temporaries shall be restricted to basic blocks. My solution would violate this rule.

Is this a problem? How can I create TAC without this violation?

Note: I want to handle the AST-branches independent of one another. So that the result-assignment is handled after the logical operation and therefor just assigning the results directly to c is not viable.

Thanks a lot.


Solution

  • Are you allowed to use logical operators outside of conditions? If yes, then you can do:

    _t0 = b < 5
    _t1 = a > 1
    c = _t0 & _t1
    

    Otherwise, if you are not allowed to use logical operators outside conditions:

        if a <= 1 goto False
        if b > 5 goto False
        c = 1
        goto True
    False:
        c = 0
    True: