Appearance
| 1 | namespace IR is | |
| 2 | use Semantic.Types.Type | |
| 3 | ||
| 4 | use Values | |
| 5 | ||
| 6 | // Builds the target-neutral IR a composite value expands to: its | |
| 7 | // operands and steps in order, temporaries for anything that has to | |
| 8 | // outlive a branch, and structured nodes for its control flow, so no | |
| 9 | // value is left on the stack across a branch. Nothing built here | |
| 10 | // marks a source position: the composite marks its own. | |
| 11 | class EXPANSION is | |
| 12 | result: BLOCK | |
| 13 | ||
| 14 | _void_type: Type | |
| 15 | _open: Collections.STACK[BLOCK] | |
| 16 | ||
| 17 | init(type: Type) is | |
| 18 | result = BLOCK(type) | |
| 19 | ||
| 20 | _void_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_void_type() | |
| 21 | _open = Collections.STACK[BLOCK]() | |
| 22 | _open.push(result) | |
| 23 | si | |
| 24 | ||
| 25 | add(value: Value) is | |
| 26 | _open.peek().add(value) | |
| 27 | si | |
| 28 | ||
| 29 | // Declares a temporary of `type`, named after `prefix`. | |
| 30 | temp(prefix: string, type: Type) -> LOCAL => | |
| 31 | declare(LOCAL(".{prefix}.{TEMP.get_next_id()}", type)) | |
| 32 | ||
| 33 | // Declares a local made by whoever built the values that read it. | |
| 34 | declare(local: LOCAL) -> LOCAL is | |
| 35 | add(DECLARE_LOCAL(local)) | |
| 36 | ||
| 37 | return local | |
| 38 | si | |
| 39 | ||
| 40 | store(local: LOCAL, value: Value) is | |
| 41 | add(value) | |
| 42 | add(STORE_TEMP(local)) | |
| 43 | si | |
| 44 | ||
| 45 | // Opens a region: values added from here until `close` land in | |
| 46 | // it, and an exit from it continues after it. | |
| 47 | region() -> REGION is | |
| 48 | let result = REGION(IR.LABEL(), _void_type) | |
| 49 | ||
| 50 | _open_node(result) | |
| 51 | ||
| 52 | return result | |
| 53 | si | |
| 54 | ||
| 55 | // Opens a loop: values added from here until `close` are | |
| 56 | // repeated until an exit leaves it. | |
| 57 | loop() -> LOOP is | |
| 58 | let result = LOOP(IR.LABEL(), IR.LABEL(), _void_type) | |
| 59 | ||
| 60 | _open_node(result) | |
| 61 | ||
| 62 | return result | |
| 63 | si | |
| 64 | ||
| 65 | _open_node(node: Breakable) is | |
| 66 | node.clear_position() | |
| 67 | ||
| 68 | add(node) | |
| 69 | _open.push(node.body) | |
| 70 | si | |
| 71 | ||
| 72 | close(node: Breakable) is | |
| 73 | assert _open.peek() == node.body else "expansion nodes closed out of order" | |
| 74 | ||
| 75 | _open.pop() | |
| 76 | ||
| 77 | node.close() | |
| 78 | si | |
| 79 | ||
| 80 | exit(target: Breakable) is | |
| 81 | _add_exit(BREAK(target)) | |
| 82 | si | |
| 83 | ||
| 84 | exit_if(target: Breakable, condition: BRANCH, operand: Value) is | |
| 85 | _add_exit(BREAK(target, condition, operand)) | |
| 86 | si | |
| 87 | ||
| 88 | exit_if(target: Breakable, condition: BRANCH, left: Value, right: Value) is | |
| 89 | _add_exit(BREAK(target, condition, left, right)) | |
| 90 | si | |
| 91 | ||
| 92 | continue_if(loop: LOOP, condition: BRANCH, operand: Value) is | |
| 93 | _add_exit(CONTINUE(loop, condition, operand)) | |
| 94 | si | |
| 95 | ||
| 96 | continue_if(loop: LOOP, condition: BRANCH, left: Value, right: Value) is | |
| 97 | _add_exit(CONTINUE(loop, condition, left, right)) | |
| 98 | si | |
| 99 | ||
| 100 | _add_exit(exit: Exit) is | |
| 101 | exit.clear_position() | |
| 102 | ||
| 103 | add(exit) | |
| 104 | si | |
| 105 | si | |
| 106 | si |