Generate IR for a loop. Args: index: the loop index Lvalue expr: the expression to iterate over body_insts: a function that generates the body of the loop else_insts: a function that generates the else block instructions
(
builder: IRBuilder,
index: Lvalue,
expr: Expression,
body_insts: GenFunc,
else_insts: GenFunc | None,
is_async: bool,
line: int,
)
| 92 | |
| 93 | |
| 94 | def for_loop_helper( |
| 95 | builder: IRBuilder, |
| 96 | index: Lvalue, |
| 97 | expr: Expression, |
| 98 | body_insts: GenFunc, |
| 99 | else_insts: GenFunc | None, |
| 100 | is_async: bool, |
| 101 | line: int, |
| 102 | ) -> None: |
| 103 | """Generate IR for a loop. |
| 104 | |
| 105 | Args: |
| 106 | index: the loop index Lvalue |
| 107 | expr: the expression to iterate over |
| 108 | body_insts: a function that generates the body of the loop |
| 109 | else_insts: a function that generates the else block instructions |
| 110 | """ |
| 111 | # Body of the loop |
| 112 | body_block = BasicBlock() |
| 113 | # Block that steps to the next item |
| 114 | step_block = BasicBlock() |
| 115 | # Block for the else clause, if we need it |
| 116 | else_block = BasicBlock() |
| 117 | # Block executed after the loop |
| 118 | exit_block = BasicBlock() |
| 119 | |
| 120 | # Determine where we want to exit, if our condition check fails. |
| 121 | normal_loop_exit = else_block if else_insts is not None else exit_block |
| 122 | |
| 123 | for_gen = make_for_loop_generator( |
| 124 | builder, index, expr, body_block, normal_loop_exit, line, is_async=is_async |
| 125 | ) |
| 126 | |
| 127 | builder.push_loop_stack(step_block, exit_block) |
| 128 | condition_block = BasicBlock() |
| 129 | builder.goto_and_activate(condition_block) |
| 130 | |
| 131 | # Add loop condition check. |
| 132 | for_gen.gen_condition() |
| 133 | |
| 134 | # Generate loop body. |
| 135 | builder.activate_block(body_block) |
| 136 | for_gen.begin_body() |
| 137 | body_insts() |
| 138 | |
| 139 | # We generate a separate step block (which might be empty). |
| 140 | builder.goto_and_activate(step_block) |
| 141 | for_gen.gen_step() |
| 142 | # Go back to loop condition. |
| 143 | builder.goto(condition_block) |
| 144 | |
| 145 | for_gen.add_cleanup(normal_loop_exit) |
| 146 | builder.pop_loop_stack() |
| 147 | |
| 148 | if else_insts is not None: |
| 149 | builder.activate_block(else_block) |
| 150 | else_insts() |
| 151 | builder.goto(exit_block) |
no test coverage detected
searching dependent graphs…