Chapter 8: Intermediate Code Generation

Translation

code to evaluate E into t

code to evaluate E into t

if t <> V1 goto L1

goto test

code for S1
L1: code for S1
goto next

goto next

L1 if t <> V2 goto L2 L2: code for S2
code for S2

goto next

goto next
..
L2: .. Ln: code for Sn
Ln-2 if t <> Vn-l goto Ln-l

goto next

code for Sn-l
test: if t = V1 goto L1
goto next

if t = V2 goto L2

Ln-1: code for Sn ..
next:

if t = Vn-1 goto Ln-1

 

goto Ln

  next:
   

Efficient for n-way branch

There are two ways of implementing switch-case statements, both given above. The above two implementations are equivalent except that in the first case all the jumps are short jumps while in the second case they are long jumps. However, many machines provide the n-way branch which is a hardware instruction. Exploiting this instruction is much easier in the second implementation while it is almost impossible in the first one. So, if hardware has this instruction the second method is much more efficient.