next up previous contents
Next: Simple Control Flow Statements Up: High Level Control Flow Previous: DoWhileStatement   Contents


ForStatement

A ``for'' statement in Standard SUIF corresponds to a ``do'' statement in Fortran.

    concrete ForStatement : Statement
      {
        VariableSymbol * reference index;
        Expression * owner lower_bound in source_ops;
        Expression * owner upper_bound in source_ops;
        Expression * owner step in source_ops;
        LString comparison_opcode;
        Statement * owner body in child_statements;
        Statement * owner pre_pad in child_statements build {0};
        CodeLabelSymbol * reference break_label in defined_labels build {0};
        CodeLabelSymbol * reference continue_label in defined_labels build {0};
      };

index An index variable that counts the iterations.
lower_bound lower bound of the loop index
upper_bound upper bound of the loop index
step step size
comparison_opcode The particular comparison used must be one of : is_less_than, is_less_than_or_equal_to, is_greater_than, is_greater_than_or_equal_to.
body the regular loop body
pre_pad Code executed just before the first iteration of the loop
break_label Label marking the target of a break statement.
continue_label Label marking the target of a continue statement.

The basic execution model is as follows. First the upper_bound , lower_bound, and step operands are all evaluated. The values of these operands are kept and re-used throughout the execution of the for_statement. After the evaluation of the three operands, the index variable is assigned to the value of the lower bound. Next the loop test is tried. The test compares the current value of the index to the upper_bound. The comparison is interpreted as comparing the current value of the index on the left with the upper_bound value. If the test fails, the execution of the loop is done. If the test succeeds, the execution of the loop continues. After the test, the body is executed. Then the value of the step is added to the value of the index variable and this incremented value becomes the new value of the index. Then the test is tried again, and the looping continues until the test fails.

If the break_label and continue_label are defined by this statement, code within the body is allowed to jump or branch to either of these labels. A branch to the break label causes the execution of the ForStatement to finish. A branch to the continue label causes execution to jump to just before the incrementing of the index. In this case, the next thing to happen after the branch is the increment updating the index, and then the loop test.

If the pre_pad is non-empty, it is executed just before the first iteration of the loop, just after the first test of the index. That is, the expressions, lower bound, upper bound and step, are evaluated first, then the index variable is set to the lower bound, then the first test happens. If the test fails, neither the pre_pad nor the body nor anything else is executed and the ForStatement execution is finished. If this first test succeeds, though, then the pre_pad is executed before the first execution of the body. The pre_pad is executed just this once, not on subsequent iterations. It is useful internally for a compiler doing loop-level optimizations. Loop-invariant and repeated code can be put in the pre_pad , guaranteeing it will run exactly once if the body will run at least once but also making sure it will not be executed in cases where the loop body would never be executed.


next up previous contents
Next: Simple Control Flow Statements Up: High Level Control Flow Previous: DoWhileStatement   Contents
SUIF Nightly Build
2000-08-14