next up previous contents
Next: 9. Basic Blocks and Up: 8. Intermediate Code Previous: 8.1 Translating Variables, Records,   Contents

8.2 Control Statements

Statements, such as for-loop and while-loop, are translated into IR statements that contain jumps and conditional jumps.

The while loop while c do s; is evaluated in the following way:

loop: if not(c) goto done
      s
      goto loop
done:
which corresponds to the following IR:
SEQ(SEQ(SEQ(LABEL(loop),
            CJUMP(EQ,c,0,NAME(done),NAME(cont))),
        SEQ(LABEL(cont),
            SEQ(s,JUMP(NAME(loop))))),
    LABEL(done))
The for statement for i:=lo to hi do body is evaluated in the following way:
      i := lo
      j := hi
      if i>j goto done
loop: s
      i := i+1
      if i<=j goto loop
done:
The break statement is translated into a JUMP. In order the compiler to keep track which label to JUMP to in a break, it maintains a stack of labels that hold the ``done:'' labels of the for- or while-loop. When it compiles a loop, it pushes the label in the stack, and when it exits a loop, it pops the stack. The break statement is then translated into a JUMP to the label at the top of the stack.

A function call f(a1,...,an) is translated into the IR CALL(NAME(l_f),[sl,e1,...,en]), where l_r is the label for f, sl is the static link, and ei is the IR for ai. For example, if the difference between the static levels of the caller and callee is one, then sl is MEM(+(TEMP(fp),CONST(static))), where static is the offset of the static link in a frame. That is, we follow the static link once.


next up previous contents
Next: 9. Basic Blocks and Up: 8. Intermediate Code Previous: 8.1 Translating Variables, Records,   Contents
Leonidas Fegaras
2000-12-27