Chapter 8: Intermediate Code Generation

S if E then S1 else S2
E.true = newlabel
E.false = newlabel
S 1 .next = S.next
S2 .next = S.next
S.code = E.code ||

gen(E.true ':') ||

S 1 .code ||

gen(goto S.next) ||

gen(E.false ':') ||

S 2 .code

For if-then-else

S -> if E then S1 else S2

E.true = newlabel

E.false = newlabel

S1.next = S.next

S2.next = S.next

S.code = E.code || gen(E.true ':') || S1.code || gen('goto' S.next) || gen(E.false ':') || S2.code

In the above code, the labels E.true and E.false created are associated with the first three address code instructions for S1 and S2 respectively, so that if E is true, jump to S1 occurs and if E is false jump to S2 occurs. An explicit goto S.next is required after the code of S1 to ensure that after execution of code for S1 control moves to the statement after S instead of falling through the code of S2, in case E is true.