Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use System.Exception | |
| 3 | ||
| 4 | use IO.Std | |
| 5 | ||
| 6 | use Logging | |
| 7 | use Source | |
| 8 | ||
| 9 | use IR.Values | |
| 10 | use IR.VALUE_CONVERTER | |
| 11 | use IR.VALUE_BOXER | |
| 12 | ||
| 13 | use Semantic.LEAST_UPPER_BOUND_MAP | |
| 14 | use Semantic.Types.Type | |
| 15 | ||
| 16 | use Syntax.Trees.Definitions.PRAGMA | |
| 17 | ||
| 18 | use Ghul.Pipes | |
| 19 | ||
| 20 | ||
| 21 | // State-machine frame declaration: iterator, spill, await-result and awaiter fields for generator and async bodies. | |
| 22 | partial COMPILE_EXPRESSIONS is | |
| 23 | // A `for` inside a generator or async function keeps its | |
| 24 | // iterator in a frame field, so that it survives the | |
| 25 | // suspensions in the loop body. Allocated here, with the rest | |
| 26 | // of the frame's state, rather than while the loop is emitted: | |
| 27 | // the emitted assembly's rows are numbered before any body is | |
| 28 | // compiled, so a field first declared during emission is | |
| 29 | // numbered by nothing and has no row to reference. | |
| 30 | // | |
| 31 | // No-op outside a state machine. | |
| 32 | _declare_state_machine_iterator_field(carrier: Trees.IteratorCarrier, iterator_type: Type) is | |
| 33 | let function = current_function | |
| 34 | ||
| 35 | if !function? then | |
| 36 | return | |
| 37 | fi | |
| 38 | ||
| 39 | let frame = _state_machine_frame_for(function) | |
| 40 | ||
| 41 | if !frame? then | |
| 42 | return | |
| 43 | fi | |
| 44 | ||
| 45 | carrier.iterator_field = | |
| 46 | frame.declare_or_retype_anonymous_field( | |
| 47 | carrier.iterator_field, "for_iterator", iterator_type) | |
| 48 | si | |
| 49 | ||
| 50 | // Frame fields for the two values an await has to carry across | |
| 51 | // a suspend: whatever was already on the stack to its left, and | |
| 52 | // its own result. Allocated here for the same reason the loop | |
| 53 | // iterator above is — a field first declared while the body is | |
| 54 | // emitted is numbered by nothing. | |
| 55 | // | |
| 56 | // Declared once. A body re-walk sees the field already there | |
| 57 | // and retypes it, rather than allocating a second and leaving | |
| 58 | // the first as a member nothing reads. | |
| 59 | // | |
| 60 | // No-op outside a state machine. | |
| 61 | _declare_spill_field(spill: Trees.Expressions.SPILL, type: Type) is | |
| 62 | if let frame = _enclosing_state_machine_frame() then | |
| 63 | spill.spill_field = | |
| 64 | frame.declare_or_retype_anonymous_field( | |
| 65 | spill.spill_field, "spill", type) | |
| 66 | fi | |
| 67 | si | |
| 68 | ||
| 69 | // The frame field a composite value expression routes its result | |
| 70 | // through when its body suspends. Recording one is what tells IL | |
| 71 | // generation to spill rather than capture, so the conditions here | |
| 72 | // are the whole decision. | |
| 73 | // | |
| 74 | // Allocated here for the same reason the fields above are: the | |
| 75 | // emitter runs after the rows the assembly contains have been | |
| 76 | // fixed, so a field first declared while a body is emitted is | |
| 77 | // numbered by nothing and the assembly loads and then dies | |
| 78 | // looking for it. | |
| 79 | _declare_composite_spill_field(node: Trees.Node, value: Value?) is | |
| 80 | let frame = _enclosing_state_machine_frame() | |
| 81 | ||
| 82 | // A field recorded by an earlier walk of the same body | |
| 83 | // serves again, retyped below with whatever this walk | |
| 84 | // knows. One belonging to a frame that has since been | |
| 85 | // dropped and rebuilt is a reference into nothing, and is | |
| 86 | // forgotten whether or not this walk records another. | |
| 87 | let existing mut = _composite_spill_state.get(node) | |
| 88 | ||
| 89 | if existing? /\ existing.owner != frame then | |
| 90 | _composite_spill_state.remove(node) | |
| 91 | ||
| 92 | existing = null | |
| 93 | fi | |
| 94 | ||
| 95 | if !value? \/ !isa IR.Values.BLOCK(value) then | |
| 96 | return | |
| 97 | fi | |
| 98 | ||
| 99 | // A void result carries no data across the join, and | |
| 100 | // declaring a void-typed field would itself produce invalid | |
| 101 | // IL. Capture mode handles it. | |
| 102 | let type = value.type | |
| 103 | ||
| 104 | if type.is_void then | |
| 105 | return | |
| 106 | fi | |
| 107 | ||
| 108 | if !_contains_suspend(node) /\ !_contains_protected_region(node) then | |
| 109 | return | |
| 110 | fi | |
| 111 | ||
| 112 | if frame? then | |
| 113 | _composite_spill_state.set( | |
| 114 | node, | |
| 115 | frame.declare_or_retype_anonymous_field(existing, "spill", type)) | |
| 116 | fi | |
| 117 | si | |
| 118 | ||
| 119 | // A protected region has to be entered with nothing on the stack, | |
| 120 | // and a consumer such as a frame-field store pushes its receiver | |
| 121 | // before the value, so the value is carried in a field instead. | |
| 122 | _contains_protected_region(node: Trees.Node) -> bool => ( | |
| 123 | let scanner = CONTAINS_PROTECTED_REGION_SCANNER() | |
| 124 | node.walk(scanner) | |
| 125 | scanner.found | |
| 126 | ) | |
| 127 | ||
| 128 | _contains_suspend(node: Trees.Node) -> bool => ( | |
| 129 | let scanner = CONTAINS_SUSPEND_SCANNER() | |
| 130 | node.walk(scanner) | |
| 131 | scanner.found | |
| 132 | ) | |
| 133 | ||
| 134 | _declare_await_result_field(`await: Trees.Expressions.AWAIT, type: Type) is | |
| 135 | if let frame = _enclosing_state_machine_frame() then | |
| 136 | `await.result_field = | |
| 137 | frame.declare_or_retype_anonymous_field( | |
| 138 | `await.result_field, "spill", type) | |
| 139 | fi | |
| 140 | si | |
| 141 | ||
| 142 | // The awaiter an `await` holds while suspended. Its type is | |
| 143 | // settled by resolving the operand's awaiter pattern, which | |
| 144 | // happens here, so the field is allocated here too. | |
| 145 | _declare_awaiter_field(`await: Trees.Expressions.AWAIT, awaiter_type: Type) is | |
| 146 | if let frame: Semantic.Symbols.ASYNC_STATE_MACHINE_FRAME = | |
| 147 | _enclosing_state_machine_frame() | |
| 148 | then | |
| 149 | `await.awaiter_field = | |
| 150 | frame.declare_or_retype_awaiter_field( | |
| 151 | `await.awaiter_field, awaiter_type) | |
| 152 | fi | |
| 153 | si | |
| 154 | ||
| 155 | _declare_lambda_state_machine_frame(function: Trees.Expressions.FUNCTION) is | |
| 156 | let closure = cast Semantic.Symbols.Function?(function.scope) | |
| 157 | ||
| 158 | if !closure? then | |
| 159 | return | |
| 160 | fi | |
| 161 | ||
| 162 | if let state_machine = Semantic.Symbols.state_machine_for(closure) then | |
| 163 | if let frame = state_machine.frame then | |
| 164 | frame.declare() | |
| 165 | _register_lambda_pattern_fields(function, closure, frame) | |
| 166 | fi | |
| 167 | elif let async_state_machine = Semantic.Symbols.async_state_machine_for(closure) then | |
| 168 | if let frame = async_state_machine.frame then | |
| 169 | frame.declare() | |
| 170 | _register_lambda_pattern_fields(function, closure, frame) | |
| 171 | fi | |
| 172 | fi | |
| 173 | si | |
| 174 | ||
| 175 | // A destructured lambda parameter's leaves are body locals, so | |
| 176 | // inside a generator or async closure each one needs a frame | |
| 177 | // field, exactly as `let`-bound names get one: MoveNext routes | |
| 178 | // their loads and stores through it. The aggregate stays an | |
| 179 | // argument - `frame.declare()` above wires its field from the | |
| 180 | // closure's argument names. No-op for simple parameters and | |
| 181 | // for closures with no state machine. | |
| 182 | _register_lambda_pattern_fields( | |
| 183 | function: Trees.Expressions.FUNCTION, | |
| 184 | closure: Semantic.Symbols.Function, | |
| 185 | frame: Semantic.Symbols.STATE_MACHINE_FRAME_BASE | |
| 186 | ) is | |
| 187 | for a in function.arguments.expressions do | |
| 188 | let argument = cast Trees.Expressions.VARIABLE?(a) | |
| 189 | ||
| 190 | if !argument? \/ !argument.left? then | |
| 191 | continue | |
| 192 | fi | |
| 193 | ||
| 194 | let names_into = Collections.LIST[Trees.Identifiers.Identifier]() | |
| 195 | argument.left.get_names_into(names_into) | |
| 196 | ||
| 197 | for name in names_into do | |
| 198 | let symbol = closure.find_direct(name.name) | |
| 199 | ||
| 200 | if let local: Semantic.Symbols.LOCAL_VARIABLE = symbol then | |
| 201 | frame.declare_local_field(local) | |
| 202 | fi | |
| 203 | od | |
| 204 | od | |
| 205 | si | |
| 206 | ||
| 207 | _enclosing_state_machine_frame() -> Semantic.Symbols.STATE_MACHINE_FRAME_BASE? is | |
| 208 | let function = current_function | |
| 209 | ||
| 210 | if !function? then | |
| 211 | return null | |
| 212 | fi | |
| 213 | ||
| 214 | return _state_machine_frame_for(function) | |
| 215 | si | |
| 216 | ||
| 217 | // The frame of whichever kind of state machine encloses | |
| 218 | // `function`, or null when none does. Generator and async are | |
| 219 | // mutually exclusive — declare-symbols rejects a function that | |
| 220 | // is both. | |
| 221 | _state_machine_frame_for( | |
| 222 | function: Semantic.Symbols.Function | |
| 223 | ) -> Semantic.Symbols.STATE_MACHINE_FRAME_BASE? is | |
| 224 | if let state_machine = Semantic.Symbols.state_machine_for(function) then | |
| 225 | return state_machine.frame | |
| 226 | fi | |
| 227 | ||
| 228 | if let async_state_machine = Semantic.Symbols.async_state_machine_for(function) then | |
| 229 | return async_state_machine.frame | |
| 230 | fi | |
| 231 | ||
| 232 | return null | |
| 233 | si | |
| 234 | ||
| 235 | // Iterate the names on a variable-left pattern and, when the | |
| 236 | // enclosing function is a generator, register each LOCAL_VARIABLE | |
| 237 | // with the state-machine frame. LOCAL_ARGUMENTs are filtered out | |
| 238 | // — those are already wired by `frame.declare()` at function | |
| 239 | // entry — and non-generator functions are a no-op. Public so | |
| 240 | // COMPILE_CONDITIONALS can register the names an `if let` / | |
| 241 | // `while let` / case-when pattern binds, which never pass | |
| 242 | // through the plain-`let` visitors below. | |
| 243 | declare_state_machine_local_fields(left: Trees.Variables.VariableLeft) is | |
| 244 | ||
| 245 | let function = current_function | |
| 246 | ||
| 247 | if !function? then | |
| 248 | return | |
| 249 | fi | |
| 250 | ||
| 251 | // Try generator first, then async — they're mutually | |
| 252 | // exclusive (declare-symbols enforces, see the | |
| 253 | // generator-and-async-not-allowed diagnostic). | |
| 254 | let state_machine = Semantic.Symbols.state_machine_for(function) | |
| 255 | let async_state_machine = Semantic.Symbols.async_state_machine_for(function) | |
| 256 | ||
| 257 | let names_into = Collections.LIST[Trees.Identifiers.Identifier]() | |
| 258 | left.get_names_into(names_into) | |
| 259 | ||
| 260 | if state_machine? /\ state_machine.frame? then | |
| 261 | if let frame = state_machine.frame then | |
| 262 | for name in names_into do | |
| 263 | let symbol = find(name) | |
| 264 | if let local: Semantic.Symbols.LOCAL_VARIABLE = symbol then | |
| 265 | frame.declare_local_field(local) | |
| 266 | fi | |
| 267 | od | |
| 268 | fi | |
| 269 | elif async_state_machine? /\ async_state_machine.frame? then | |
| 270 | if let frame = async_state_machine.frame then | |
| 271 | for name in names_into do | |
| 272 | let symbol = find(name) | |
| 273 | if let local: Semantic.Symbols.LOCAL_VARIABLE = symbol then | |
| 274 | frame.declare_local_field(local) | |
| 275 | fi | |
| 276 | od | |
| 277 | fi | |
| 278 | fi | |
| 279 | si | |
| 280 | ||
| 281 | si | |
| 282 | si |