Go to the previous, next section.

Loop Nodes

A tree_loop node represents a "do-while" loop. It contains two tree node lists: the body and the test. The body list comes first and holds the loop body. The test list contains code to evaluate the "while" expression and conditionally branch back to the beginning of the body.

There are three labels associated with a tree_loop node: toplab, contlab, and brklab. The toplab label marks the beginning of the loop body; the test list typically contains a conditional branch back to toplab. A "continue" statement in the loop body requires a jump over the rest of the body to the beginning of the test code. The contlab label is positioned at the beginning of the test list for this purpose. Similarly, a "break" statement in the loop is translated to a jump to the brklab label which is located immediately after the loop. These tree_loop labels must be defined in the scope of the loop node, but the label positions are implicit rather than being marked by label instructions. When the tree_loop is expanded to low-SUIF form, the label instructions are inserted into the tree node lists.

Because the loop nodes are only intended for use with structured control flow, certain restrictions on the contents of the tree_loop lists are required. The test part has exactly the same restrictions as the header of a tree_if except that it is the toplab label instead of the jumpto label to which jumps and branches are allowed and expected. See section If Nodes. Note in particular that use of the brklab and contlab labels is not allowed in the test list.

The body list has restrictions analogous to the restriction on then_part and else_part lists of a tree_if: arbitrary nesting of other tree_nodes is allowed, but control flow into or out of the body is not allowed. The body list is allowed slightly more leeway, though: jumps or branches are allowed from anywhere in the body to the brklab or the contlab labels. This is the only place where the brklab or contlab labels can be used--they cannot be used outside the tree_loop or in the test list.

Note that either or both of the test and body parts may be empty lists, but if the test part is empty, the loop is degenerate--it will always execute exactly once.

For example, the following C code could be translated into the SUIF code shown in a simplified form below. Note that because the test code is at the bottom of the loop, the "while" loop must be converted to a "do-while" loop guarded by an "if" node. See section If Nodes.

while (k > 0) {
    if (k = 1) break;
    k = k - 1;
    if (k < 10) continue;
    k = k - 10;
}

IF (Jumpto=L:__L1)
IF HEADER
    bfalse e1, L:__L1
      e1: sl e2, k
        e2: ldc 0
IF THEN
    LOOP (Top=L:__L2 Cont=L:__L3 Brk=L:__L4)
    LOOP BODY
        btrue e1, L:__L4
          e1: seq k, e2
            e2: ldc 1
        sub k = k, e1
          e1: ldc 1
        btrue e1, L:__L3
          e1: sl k, e2
            e2: ldc 10
        sub k = k, e1
          e1: ldc 10
    LOOP TEST
        btrue e1, L:__L2
          e1: sl e2, k
            e2: ldc 0
    LOOP END 
IF ELSE 
IF END

Go to the previous, next section.