Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use IO.Std | |
| 3 | ||
| 4 | use System.Text.StringBuilder | |
| 5 | ||
| 6 | use Logging | |
| 7 | use Trees | |
| 8 | use Source | |
| 9 | ||
| 10 | use IR | |
| 11 | use IR.Values | |
| 12 | ||
| 13 | use Ghul.Pipes | |
| 14 | ||
| 15 | ||
| 16 | // Async machinery: the async outer method, dispatch holders, frame construction and the | |
| 17 | // async state-machine class emission. | |
| 18 | partial GENERATE_IL is | |
| 19 | // The outer method of an async function: everything | |
| 20 | // ASYNC_METHOD_LAUNCH emits, plus the return that hands back the | |
| 21 | // builder's task. | |
| 22 | _emit_async_outer_method( | |
| 23 | symbol: Semantic.Symbols.Function, | |
| 24 | launch: IR.Values.ASYNC_METHOD_LAUNCH | |
| 25 | ) is | |
| 26 | _backend.begin_body() | |
| 27 | ||
| 28 | try | |
| 29 | _backend.write(launch) | |
| 30 | _backend.write(IR.Values.RET()) | |
| 31 | ||
| 32 | _backend.finish_body(symbol) | |
| 33 | finally | |
| 34 | _backend.end_body() | |
| 35 | yrt | |
| 36 | si | |
| 37 | ||
| 38 | // The frame type as it must be named in IL: constructed over the | |
| 39 | // type arguments visible inside the owning function, so a frame | |
| 40 | // declared in a generic owner is never referred to by its open | |
| 41 | // form. Null when the frame is not generic. | |
| 42 | _specialized_frame_type( | |
| 43 | frame: Semantic.Symbols.Classy, | |
| 44 | construction_type_args: Collections.List[Semantic.Types.Type]?, | |
| 45 | location: Source.LOCATION | |
| 46 | ) -> Semantic.Types.Type? is | |
| 47 | if !construction_type_args? then | |
| 48 | return null | |
| 49 | fi | |
| 50 | ||
| 51 | return Semantic.Types.GENERIC(location, frame, construction_type_args) | |
| 52 | si | |
| 53 | ||
| 54 | // A frame field as it must be named from the outer method: on | |
| 55 | // the frame's own instantiation, so the reference and the | |
| 56 | // field's type are both in the outer method's terms rather than | |
| 57 | // the frame's own. | |
| 58 | _specialized_field( | |
| 59 | frame: Semantic.Symbols.Classy, | |
| 60 | `field: Semantic.Symbols.Field, | |
| 61 | construction_type_args: Collections.List[Semantic.Types.Type]?, | |
| 62 | location: Source.LOCATION | |
| 63 | ) -> Semantic.Symbols.Field is | |
| 64 | if !construction_type_args? \/ construction_type_args.count == 0 then | |
| 65 | return `field | |
| 66 | fi | |
| 67 | ||
| 68 | if let specialized = | |
| 69 | _specialized_frame_type(frame, construction_type_args, location) | |
| 70 | then | |
| 71 | if let found: Semantic.Symbols.Field = | |
| 72 | specialized.find_member(`field.name) | |
| 73 | then | |
| 74 | return found | |
| 75 | fi | |
| 76 | fi | |
| 77 | ||
| 78 | return `field | |
| 79 | si | |
| 80 | ||
| 81 | // Async-function IL emission. Parallels | |
| 82 | // `_pre_generator_function`: emits the outer method (newobj | |
| 83 | // SM, builder.Start, return Task) and stashes a MoveNext | |
| 84 | // BLOCK to be flushed when the enclosing class closes. | |
| 85 | // MoveNext wraps the body in a try/catch routing exceptions | |
| 86 | // through `builder.SetException` and success through | |
| 87 | // `builder.SetResult`. | |
| 88 | _pre_async_function(function: Definitions.FUNCTION, symbol: Semantic.Symbols.Function, async_state_machine: Semantic.Symbols.ASYNC_STATE_MACHINE) -> bool is | |
| 89 | let frame = async_state_machine.frame | |
| 90 | ||
| 91 | if !frame? then | |
| 92 | _logger.error(function.location, "async function must return a task-like type — one carrying AsyncMethodBuilderAttribute, such as Tasks.TASK or Tasks.ValueTask") | |
| 93 | return true | |
| 94 | fi | |
| 95 | ||
| 96 | frame.declare() | |
| 97 | ||
| 98 | let construction_type_args = async_state_machine.get_construction_type_arguments() | |
| 99 | ||
| 100 | let constructor = | |
| 101 | _specialized_constructor( | |
| 102 | frame, frame.constructor, construction_type_args, symbol.location) | |
| 103 | ||
| 104 | // The local holding the state machine, and the type argument | |
| 105 | // to builder.Start, both name the frame type. A frame in a | |
| 106 | // generic owner is generic too, so both need the constructed | |
| 107 | // form — the open form does not resolve at run time. | |
| 108 | let state_machine_type = | |
| 109 | _specialized_frame_type(frame, construction_type_args, symbol.location) ?? frame.type! | |
| 110 | ||
| 111 | // ctor args: an instance method's receiver for _outer_self, | |
| 112 | // then each declared argument. Build them as a sequence Value so | |
| 113 | // ASYNC_METHOD_LAUNCH composes them in the right order. | |
| 114 | let ctor_args_block = Values.BLOCK() | |
| 115 | ||
| 116 | // The enclosing method's own arguments, forwarded | |
| 117 | // positionally after its receiver, if it has one. | |
| 118 | if frame.outer_self_field? then | |
| 119 | ctor_args_block.add(Values.Load.RECEIVER(symbol)) | |
| 120 | fi | |
| 121 | ||
| 122 | for index in 0..symbol.argument_names.count do | |
| 123 | ctor_args_block.add(Values.Load.ARGUMENT(symbol, index)) | |
| 124 | od | |
| 125 | ctor_args_block.close() | |
| 126 | ||
| 127 | let task_type = symbol.return_type! | |
| 128 | ||
| 129 | let launch = IR.Values.ASYNC_METHOD_LAUNCH( | |
| 130 | task_type, | |
| 131 | state_machine_type, | |
| 132 | constructor, | |
| 133 | ctor_args_block, | |
| 134 | _specialized_field( | |
| 135 | frame, frame.state_field, construction_type_args, symbol.location), | |
| 136 | _specialized_field( | |
| 137 | frame, frame.builder_field!, construction_type_args, symbol.location) | |
| 138 | ) | |
| 139 | ||
| 140 | _emit_async_outer_method(symbol, launch) | |
| 141 | ||
| 142 | // --- Walk the body into MoveNext's block --- | |
| 143 | async_state_machine.install_body_emission_overrides() | |
| 144 | ||
| 145 | enter_scope(function) | |
| 146 | ||
| 147 | let move_next_block = Values.BLOCK() | |
| 148 | ||
| 149 | enter_block(move_next_block) | |
| 150 | ||
| 151 | add(Values.MAX_STACK(64)) | |
| 152 | ||
| 153 | // Cache the state field to a CLR local at MoveNext entry. | |
| 154 | // Every AWAIT_SUSPEND keeps this local in sync via | |
| 155 | // `dup; stloc; stfld` so the per-region dispatch (below) | |
| 156 | // and the finally-body state guards read a stable value. | |
| 157 | // | |
| 158 | // Finally bodies (let-use auto-dispose and user-written | |
| 159 | // `finally`) state-guard their work on the same local: | |
| 160 | // when V_state >= 0 the function is mid-suspend (the | |
| 161 | // `leave` from the AwaitUnsafeOnCompleted path fires the | |
| 162 | // finally before exiting), and side effects must be | |
| 163 | // skipped. | |
| 164 | // The entire MoveNext body lives inside a `.try` whose | |
| 165 | // handler routes exceptions through `builder.SetException(e)`. | |
| 166 | let move_next = _open_async_move_next() | |
| 167 | ||
| 168 | let outer_dispatch_holder = | |
| 169 | _open_state_machine_entry(frame, frame.state_field, ".async_state") | |
| 170 | ||
| 171 | // Stash the emission context so visit(LET-await), | |
| 172 | // visit(AWAIT), and visit(RETURN) inside the body know | |
| 173 | // they're inside this state machine and can reach the | |
| 174 | // frame / labels they need to emit. Cleared after the | |
| 175 | // body walk so anything else emitted at this nesting | |
| 176 | // level (other functions, nested classes' visits) | |
| 177 | // goes through the standard paths. | |
| 178 | let prev_async_sm = _current_async_state_machine | |
| 179 | let prev_success = _current_async_success | |
| 180 | let prev_resumable = _current_async_resumable | |
| 181 | _current_async_state_machine = async_state_machine | |
| 182 | _current_async_success = move_next.success | |
| 183 | _current_async_resumable = move_next.resumable | |
| 184 | ||
| 185 | // Unpack any destructured formal arguments into their leaf | |
| 186 | // frame fields before the body reads them. The plain path's | |
| 187 | // AST walk does this from visit(Variables.VARIABLE); this | |
| 188 | // path drives its own walk and never reaches those nodes. | |
| 189 | _gen_destructured_formal_prologue(function) | |
| 190 | ||
| 191 | // Walk the body. visit(AWAIT) appends AWAIT_SUSPEND IR | |
| 192 | // Values into move_next_block; other statements emit | |
| 193 | // normally. | |
| 194 | if function.body? then | |
| 195 | function.body.walk(self) | |
| 196 | fi | |
| 197 | ||
| 198 | // What the body produces goes into the frame's result before | |
| 199 | // leaving to success, exactly as `visit(RETURN)` puts it there. | |
| 200 | _gen_async_body_result(function.body, frame) | |
| 201 | ||
| 202 | // Body fell off the end: leave for the success path so the | |
| 203 | // trailer fires. | |
| 204 | add(Values.BREAK(move_next.success)) | |
| 205 | ||
| 206 | _current_async_state_machine = prev_async_sm | |
| 207 | _current_async_success = prev_success | |
| 208 | _current_async_resumable = prev_resumable | |
| 209 | ||
| 210 | // Populate the outer dispatch from the holder that | |
| 211 | // accumulated awaits not enclosed in any inner `.try`. | |
| 212 | _pop_async_dispatch_holder(outer_dispatch_holder) | |
| 213 | ||
| 214 | _close_async_move_next(move_next) | |
| 215 | ||
| 216 | leave_block() | |
| 217 | leave_scope(function) | |
| 218 | ||
| 219 | async_state_machine.uninstall_body_emission_overrides() | |
| 220 | ||
| 221 | _pending_async_state_machines.add( | |
| 222 | PENDING_ASYNC_STATE_MACHINE( | |
| 223 | async_state_machine, | |
| 224 | move_next_block, | |
| 225 | move_next | |
| 226 | ) | |
| 227 | ) | |
| 228 | ||
| 229 | return true | |
| 230 | si | |
| 231 | ||
| 232 | // The state local of the innermost open dispatch holder, or | |
| 233 | // null when no state machine is open. Used by `.try` emission, | |
| 234 | // the finally-body state guard and suspension, so the same code | |
| 235 | // paths use the right local without each caller having to know. | |
| 236 | _current_state_local() -> IR.LOCAL? is | |
| 237 | if _async_dispatch_stack.count == 0 then | |
| 238 | return null | |
| 239 | fi | |
| 240 | return _async_dispatch_stack[_async_dispatch_stack.count - 1].state_local | |
| 241 | si | |
| 242 | ||
| 243 | // Declare a CLR `int32` local in the MoveNext frame, copy | |
| 244 | // the state field into it, and push the outer dispatch | |
| 245 | // holder bound to that local. Returns the pushed holder so | |
| 246 | // the caller can pop it at the end of the body walk via | |
| 247 | // `_pop_async_dispatch_holder`. Used by every state-machine | |
| 248 | // entry point (named function, lambda closure, generator). | |
| 249 | // `state_local_name` names the local - | |
| 250 | // `'.async_state'` for async, `'.gen_state'` for | |
| 251 | // generators — and travels with the holder so inner | |
| 252 | // dispatch sites pick it up. | |
| 253 | _open_state_machine_entry( | |
| 254 | frame: Semantic.Symbols.STATE_MACHINE_FRAME_BASE, | |
| 255 | state_field: Semantic.Symbols.Field, | |
| 256 | state_local_name: string | |
| 257 | ) -> ASYNC_DISPATCH_HOLDER is | |
| 258 | let state_local = IR.LOCAL(state_local_name, _innate_symbol_lookup.get_int_type()) | |
| 259 | ||
| 260 | add(Values.DECLARE_LOCAL(state_local)) | |
| 261 | add(_build_frame_field_load(frame, state_field)) | |
| 262 | add(Values.STORE_TEMP(state_local)) | |
| 263 | ||
| 264 | let dispatch = Values.DISPATCH(state_local) | |
| 265 | add(dispatch) | |
| 266 | let holder = ASYNC_DISPATCH_HOLDER(dispatch, true) | |
| 267 | _async_dispatch_stack.add(holder) | |
| 268 | return holder | |
| 269 | si | |
| 270 | ||
| 271 | // Push an inner dispatch holder at the top of a `.try` body | |
| 272 | // if we're inside a state-machine method. Returns the | |
| 273 | // holder (to pop later) or null when no state machine is | |
| 274 | // open — the caller can pass that null back to | |
| 275 | // `_maybe_pop_dispatch_holder` for symmetric cleanup | |
| 276 | // without an `if` at every call site. | |
| 277 | _maybe_push_dispatch_holder() -> ASYNC_DISPATCH_HOLDER? is | |
| 278 | if !_current_state_local()? then | |
| 279 | return null | |
| 280 | fi | |
| 281 | return _push_async_dispatch_holder() | |
| 282 | si | |
| 283 | ||
| 284 | _maybe_pop_dispatch_holder(holder: ASYNC_DISPATCH_HOLDER?) is | |
| 285 | if holder? then | |
| 286 | _pop_async_dispatch_holder(holder) | |
| 287 | fi | |
| 288 | si | |
| 289 | ||
| 290 | // The state-machine frame for the current function — async | |
| 291 | // or generator, whichever applies. Used by the BLOCK-with- | |
| 292 | // suspend spill in `LIST.visit` / `visit(IF)` / `visit(CASE)` | |
| 293 | // (in expression position) to allocate spill fields that | |
| 294 | // survive across MoveNext re-entries. | |
| 295 | _current_state_machine_frame() -> Semantic.Symbols.STATE_MACHINE_FRAME_BASE? is | |
| 296 | let casm = _current_async_state_machine | |
| 297 | ||
| 298 | if casm? /\ casm.frame? then | |
| 299 | return casm.frame | |
| 300 | fi | |
| 301 | ||
| 302 | let cf = current_function | |
| 303 | if cf? then | |
| 304 | let sm = Semantic.Symbols.state_machine_for(cf) | |
| 305 | if sm? /\ sm.frame? then | |
| 306 | return sm.frame | |
| 307 | fi | |
| 308 | fi | |
| 309 | ||
| 310 | return null | |
| 311 | si | |
| 312 | ||
| 313 | // Build the IR Value that loads from `field` on the current | |
| 314 | // state-machine frame — used as the spilled BLOCK's stand-in | |
| 315 | // value. The consumer that does `add(block)` reads this | |
| 316 | // load when the BLOCK gens, in place of the original body | |
| 317 | // IL. | |
| 318 | _build_frame_field_load(frame: Semantic.Symbols.STATE_MACHINE_FRAME_BASE, spill_field: Semantic.Symbols.Field) -> IR.Values.Value => | |
| 319 | IR.Values.Load.INSTANCE_FIELD( | |
| 320 | IR.Values.Load.REFERENCE_SELF(frame, frame.type), | |
| 321 | spill_field | |
| 322 | ) | |
| 323 | ||
| 324 | // The counterpart store: `value` is emitted between the frame | |
| 325 | // reference and the `stfld`, so anything that leaves one item | |
| 326 | // on the stack can stand in for it. | |
| 327 | _build_frame_field_store( | |
| 328 | frame: Semantic.Symbols.STATE_MACHINE_FRAME_BASE, | |
| 329 | `field: Semantic.Symbols.Field, | |
| 330 | value: IR.Values.Value | |
| 331 | ) -> IR.Values.Value => | |
| 332 | IR.Values.Store.INSTANCE_FIELD( | |
| 333 | IR.Values.Load.REFERENCE_SELF(frame, frame.type), | |
| 334 | `field, | |
| 335 | value | |
| 336 | ) | |
| 337 | ||
| 338 | // BLOCK-with-suspend spill path for `Statements.LIST` in | |
| 339 | // expression position. The capture-mode LIST.visit wraps | |
| 340 | // its body in lots of state-machine plumbing (dispatch | |
| 341 | // holders, exception-handler temps, `.try` for `want | |
| 342 | // _dispose`) that the spill path doesn't need — the body | |
| 343 | // emits inline in outer current_block, the result lands | |
| 344 | // in a frame field, and the BLOCK ends up holding just a | |
| 345 | // load. Returns true if the spill path ran (caller skips | |
| 346 | // the ordinary capture path); false otherwise. | |
| 347 | _try_spill_block_with_suspend(list: Statements.LIST) -> bool is | |
| 348 | let spiller = COMPOSITE_VALUE_SPILLER( | |
| 349 | self, | |
| 350 | list, | |
| 351 | list.value, | |
| 352 | _current_state_machine_frame() | |
| 353 | ) | |
| 354 | ||
| 355 | if !spiller.is_spilling then | |
| 356 | return false | |
| 357 | fi | |
| 358 | ||
| 359 | // Walk body in outer current_block. Statement IL — | |
| 360 | // including visit(AWAIT) / visit(YIELD) suspend | |
| 361 | // emissions and nested composites' own spills — lands | |
| 362 | // in outer, in the same flat stream as the SM | |
| 363 | // dispatcher. | |
| 364 | for s in list.statements do | |
| 365 | enter_node(s) | |
| 366 | try | |
| 367 | s.walk(self) | |
| 368 | finally | |
| 369 | leave_node(s) | |
| 370 | yrt | |
| 371 | od | |
| 372 | ||
| 373 | // Tail-evaluate-and-spill. By now the tail's value is a | |
| 374 | // clean load (any tail-internal suspends already | |
| 375 | // hoisted by visit(AWAIT) / visit(YIELD), and any | |
| 376 | // tail-internal composites already spilled themselves), | |
| 377 | // so the ldarg.0 / value-IL / stfld sequence has no | |
| 378 | // suspend between receiver push and stfld. | |
| 379 | if let list.last?, last.value? then | |
| 380 | spiller.emit_value(value) | |
| 381 | fi | |
| 382 | ||
| 383 | spiller.leave() | |
| 384 | ||
| 385 | return true | |
| 386 | si | |
| 387 | ||
| 388 | ||
| 389 | // Record a (state, resume_label) pair with the innermost | |
| 390 | // open dispatch holder, so its dispatch block (at the top | |
| 391 | // of its `.try` body OR at MoveNext outer scope) routes | |
| 392 | // cold-resume entries to {resume_label}. No-op outside a | |
| 393 | // state machine. | |
| 394 | _register_resume_with_dispatch(state: int, resume_label: IR.LABEL) is | |
| 395 | if _async_dispatch_stack.count == 0 then | |
| 396 | return | |
| 397 | fi | |
| 398 | let holder = _async_dispatch_stack[_async_dispatch_stack.count - 1] | |
| 399 | holder.register(state, resume_label) | |
| 400 | si | |
| 401 | ||
| 402 | // Emit the `if V_state >= 1 → skip` guard at the top of a | |
| 403 | // finally body in a state-machine method. The `leave` that | |
| 404 | // ends a suspend (AwaitUnsafeOnCompleted or yield) fires the | |
| 405 | // surrounding finally with the suspend's state, which is | |
| 406 | // numbered from 1; the user's code must not run then. Real | |
| 407 | // exits and exceptional unwinds run the finally normally: | |
| 408 | // the state is -1 once a resume has been dispatched, and 0 | |
| 409 | // in a generator's first run, before anything has yielded. | |
| 410 | // | |
| 411 | // Returns the skip label so the caller can plant it at the | |
| 412 | // end of the guarded code via `_close_finally_state_guard`. | |
| 413 | // Returns null when not inside a state machine — caller | |
| 414 | // emits the finally body unguarded. | |
| 415 | _open_finally_state_guard() -> Values.REGION? is | |
| 416 | let state_local = _current_state_local() | |
| 417 | ||
| 418 | if !state_local? then | |
| 419 | return null | |
| 420 | fi | |
| 421 | ||
| 422 | let skip = _open_region(LABEL()) | |
| 423 | ||
| 424 | let int_type = _innate_symbol_lookup.get_int_type() | |
| 425 | ||
| 426 | add( | |
| 427 | Values.BREAK( | |
| 428 | skip, | |
| 429 | BRANCH.GE, | |
| 430 | Values.Load.TEMP(state_local), | |
| 431 | Values.Literal.NUMBER(1, int_type))) | |
| 432 | ||
| 433 | return skip | |
| 434 | si | |
| 435 | ||
| 436 | _close_finally_state_guard(skip: Values.REGION?) is | |
| 437 | if skip? then | |
| 438 | _close_structured(skip) | |
| 439 | fi | |
| 440 | si | |
| 441 | ||
| 442 | // Push a new dispatch holder onto the stack and emit its | |
| 443 | // placeholder block at the current emission point. Returns | |
| 444 | // the holder so the caller can pop it once the protected | |
| 445 | // region's body has been walked. Use when opening a `.try` | |
| 446 | // body inside an async function or generator so the | |
| 447 | // AWAIT_SUSPENDs / YIELDs that fire in the body register | |
| 448 | // here and the dispatch ends up inside the same region as | |
| 449 | // the targets it reaches. The new holder inherits its | |
| 450 | // state-local IL spelling from the top of the stack — the | |
| 451 | // outer holder (pushed at MoveNext entry) chose the name | |
| 452 | // (`'.async_state'` for async, `'.gen_state'` for | |
| 453 | // generators) so inner holders just match. | |
| 454 | _push_async_dispatch_holder() -> ASYNC_DISPATCH_HOLDER is | |
| 455 | assert _async_dispatch_stack.count > 0 else "no outer dispatch holder to inherit state-local name from" | |
| 456 | let outer = _async_dispatch_stack[_async_dispatch_stack.count - 1] | |
| 457 | let dispatch = Values.DISPATCH(outer.state_local) | |
| 458 | add(dispatch) | |
| 459 | let holder = ASYNC_DISPATCH_HOLDER(dispatch, false) | |
| 460 | _async_dispatch_stack.add(holder) | |
| 461 | return holder | |
| 462 | si | |
| 463 | ||
| 464 | // Populate the holder's dispatch block from registered | |
| 465 | // (state, cold_resume_label) entries and pop. No-op if no | |
| 466 | // entries — keeps emitted IL minimal when a `.try` body | |
| 467 | // contains no awaits. The popped region's states are handed | |
| 468 | // to the enclosing holder against `top_label` (this region's | |
| 469 | // first instruction), so a resuming MoveNext is routed from | |
| 470 | // the MoveNext-entry dispatch directly into the innermost | |
| 471 | // region holding the resume point, entering each region at | |
| 472 | // its first instruction and skipping the statements between | |
| 473 | // them — re-running those would re-execute hoisted-variable | |
| 474 | // initializers and loop iterators that the frame fields | |
| 475 | // already carry mid-suspend values for. | |
| 476 | _pop_async_dispatch_holder(holder: ASYNC_DISPATCH_HOLDER) is | |
| 477 | _populate_async_dispatch(holder) | |
| 478 | assert _async_dispatch_stack.count > 0 else "dispatch holder stack underflow" | |
| 479 | _async_dispatch_stack.remove_at(_async_dispatch_stack.count - 1) | |
| 480 | ||
| 481 | // A state-machine entry holder (one per MoveNext body) is | |
| 482 | // never propagated: the holder beneath it on the stack | |
| 483 | // belongs to an ENCLOSING state machine — a different | |
| 484 | // method body — and a branch there could not reach this | |
| 485 | // body's labels. Try-to-try propagation stays within one | |
| 486 | // body by construction. | |
| 487 | if !holder.is_state_machine_entry /\ _async_dispatch_stack.count > 0 then | |
| 488 | let parent = _async_dispatch_stack[_async_dispatch_stack.count - 1] | |
| 489 | for entry in holder.entries do | |
| 490 | parent.register(entry.state, holder.top_label) | |
| 491 | od | |
| 492 | fi | |
| 493 | si | |
| 494 | ||
| 495 | // Give the holder's dispatch one entry per registered await | |
| 496 | // or yield. The dispatch was added to the surrounding emission | |
| 497 | // stream when the holder was pushed, so it sits at the top of | |
| 498 | // the protected region's body, before any user code. | |
| 499 | _populate_async_dispatch(holder: ASYNC_DISPATCH_HOLDER) is | |
| 500 | for entry in holder.entries do | |
| 501 | holder.dispatch.add_entry(entry.state, entry.cold_resume_label) | |
| 502 | od | |
| 503 | si | |
| 504 | ||
| 505 | // Emit all pending state-machine classes accumulated during | |
| 506 | // the walk of an enclosing class or namespace. Called at the | |
| 507 | // tail of visit(`class) / visit(`namespace) so the classes | |
| 508 | // land at namespace scope, parallel to the user's class. | |
| 509 | emit_pending_state_machines() is | |
| 510 | if _pending_state_machines.count > 0 then | |
| 511 | let pending = _pending_state_machines | |
| 512 | _pending_state_machines = Collections.LIST[PENDING_STATE_MACHINE]() | |
| 513 | ||
| 514 | for psm in pending do | |
| 515 | _emit_state_machine_class(psm.state_machine, psm.move_next_block) | |
| 516 | od | |
| 517 | fi | |
| 518 | ||
| 519 | if _pending_async_state_machines.count > 0 then | |
| 520 | let pending = _pending_async_state_machines | |
| 521 | _pending_async_state_machines = Collections.LIST[PENDING_ASYNC_STATE_MACHINE]() | |
| 522 | ||
| 523 | for pasm in pending do | |
| 524 | _emit_async_state_machine_class( | |
| 525 | pasm.async_state_machine, | |
| 526 | pasm.move_next_block, | |
| 527 | pasm.move_next | |
| 528 | ) | |
| 529 | od | |
| 530 | fi | |
| 531 | si | |
| 532 | ||
| 533 | // Emits the synthesised IAsyncStateMachine class — parallels | |
| 534 | // `_emit_state_machine_class` for generators. Class members: | |
| 535 | // `$state`, `$builder`, `$result` (value-async), `$outer_self` | |
| 536 | // (instance), `$arg_*`, `$local_*`, `$awaiter_*` fields; ctor; | |
| 537 | // MoveNext (with entry-dispatch and try/catch routing through | |
| 538 | // the builder); trivial SetStateMachine. | |
| 539 | _emit_async_state_machine_class( | |
| 540 | async_state_machine: Semantic.Symbols.ASYNC_STATE_MACHINE, | |
| 541 | move_next_block: Values.BLOCK, | |
| 542 | move_next: ASYNC_MOVE_NEXT | |
| 543 | ) is | |
| 544 | let frame = async_state_machine.frame | |
| 545 | ||
| 546 | assert frame? else "async state machine has no frame at IL emission" | |
| 547 | ||
| 548 | // Same settled-type refresh the generator emitter performs. | |
| 549 | frame.refresh_local_field_types() | |
| 550 | ||
| 551 | async_state_machine.install_body_emission_overrides() | |
| 552 | ||
| 553 | try | |
| 554 | _without_position(() is _gen_async_state_machine_ctor(frame) si) | |
| 555 | ||
| 556 | _emit_async_frame_member_body( | |
| 557 | frame, | |
| 558 | IR.FrameMember.MOVE_NEXT, | |
| 559 | () is | |
| 560 | _gen_async_move_next( | |
| 561 | frame, move_next_block, move_next) | |
| 562 | si) | |
| 563 | ||
| 564 | _emit_async_frame_member_body( | |
| 565 | frame, | |
| 566 | IR.FrameMember.SET_STATE_MACHINE, | |
| 567 | () is si) | |
| 568 | finally | |
| 569 | async_state_machine.uninstall_body_emission_overrides() | |
| 570 | yrt | |
| 571 | si | |
| 572 | ||
| 573 | // A synthesised async frame member, finished against the frame | |
| 574 | // and which member it is. | |
| 575 | // `emit` writes the body; SetStateMachine's writes nothing, | |
| 576 | // because the whole of it is the `ret` added here. | |
| 577 | _emit_async_frame_member_body( | |
| 578 | frame: Semantic.Symbols.ASYNC_STATE_MACHINE_FRAME, | |
| 579 | member: IR.FrameMember, | |
| 580 | emit: () -> void | |
| 581 | ) is | |
| 582 | _backend.begin_body() | |
| 583 | ||
| 584 | try | |
| 585 | emit() | |
| 586 | ||
| 587 | _backend.write(IR.Values.RET()) | |
| 588 | ||
| 589 | _backend.finish_frame_member_body(frame, member) | |
| 590 | finally | |
| 591 | _backend.end_body() | |
| 592 | yrt | |
| 593 | si | |
| 594 | ||
| 595 | // MoveNext: the body, inside the try whose handler routes an | |
| 596 | // escaping exception through the builder. The success path | |
| 597 | // leaves the success region and completes the task, and both | |
| 598 | // paths meet at the end of the resumable body. | |
| 599 | _gen_async_move_next( | |
| 600 | frame: Semantic.Symbols.ASYNC_STATE_MACHINE_FRAME, | |
| 601 | move_next_block: Values.BLOCK, | |
| 602 | move_next: ASYNC_MOVE_NEXT | |
| 603 | ) is | |
| 604 | let void_type = _innate_symbol_lookup.get_void_type() | |
| 605 | let builder_field = frame.builder_field! | |
| 606 | ||
| 607 | let handler = Values.CATCH(_innate_symbol_lookup.get_exception_type(), void_type) | |
| 608 | ||
| 609 | handler.clear_position() | |
| 610 | handler.body.add( | |
| 611 | Values.ASYNC_COMPLETION.failure( | |
| 612 | frame.state_field, builder_field, _innate_symbol_lookup.get_exception_type())) | |
| 613 | ||
| 614 | let leave = Values.BREAK(move_next.resumable) | |
| 615 | ||
| 616 | leave.clear_position() | |
| 617 | handler.body.add(leave) | |
| 618 | ||
| 619 | move_next.`try.catches.add(handler) | |
| 620 | ||
| 621 | move_next.resumable.body.add( | |
| 622 | Values.ASYNC_COMPLETION.success(frame.state_field, builder_field, frame.result_field)) | |
| 623 | ||
| 624 | _backend.write(move_next_block) | |
| 625 | si | |
| 626 | ||
| 627 | // Opens the structure an async MoveNext body is generated into. | |
| 628 | // None of it is written in source, so none of it marks a | |
| 629 | // position. | |
| 630 | _open_async_move_next() -> ASYNC_MOVE_NEXT is | |
| 631 | let void_type = _innate_symbol_lookup.get_void_type() | |
| 632 | ||
| 633 | let resumable = Values.RESUMABLE(LABEL(), void_type) | |
| 634 | let success = Values.REGION(LABEL(), void_type) | |
| 635 | let `try = Values.TRY(LABEL(), void_type) | |
| 636 | ||
| 637 | resumable.clear_position() | |
| 638 | success.clear_position() | |
| 639 | `try.clear_position() | |
| 640 | ||
| 641 | _open_structured(resumable) | |
| 642 | _open_structured(success) | |
| 643 | _open_structured(`try) | |
| 644 | ||
| 645 | return ASYNC_MOVE_NEXT(resumable, success, `try) | |
| 646 | si | |
| 647 | ||
| 648 | // Closes the structure, leaving the catch handler and the success | |
| 649 | // path to be added when the state machine class is emitted. | |
| 650 | _close_async_move_next(move_next: ASYNC_MOVE_NEXT) is | |
| 651 | _close_try_body(move_next.`try) | |
| 652 | move_next.`try.close() | |
| 653 | ||
| 654 | _close_structured(move_next.success) | |
| 655 | _close_structured(move_next.resumable) | |
| 656 | si | |
| 657 | ||
| 658 | // The frame's constructor: chain to Object, then copy each | |
| 659 | // constructor argument into the field the lowering declared for | |
| 660 | // it. The builder is not among them — the outer method creates | |
| 661 | // it and stores it after the newobj. | |
| 662 | _gen_async_state_machine_ctor(frame: Semantic.Symbols.ASYNC_STATE_MACHINE_FRAME) is | |
| 663 | let body = Values.BLOCK(_innate_symbol_lookup.get_void_type()) | |
| 664 | ||
| 665 | body.add(Values.RUNTIME(IR.RuntimeOperation.INIT_OBJECT, _frame_self(frame), null)) | |
| 666 | ||
| 667 | let index mut = 0 | |
| 668 | ||
| 669 | if let outer_self_field = frame.outer_self_field then | |
| 670 | body.add(_store_frame_field(frame, outer_self_field, Values.Load.ARGUMENT(frame.constructor, index))) | |
| 671 | ||
| 672 | index = index + 1 | |
| 673 | fi | |
| 674 | ||
| 675 | for arg_field in frame.argument_fields do | |
| 676 | body.add(_store_frame_field(frame, arg_field, Values.Load.ARGUMENT(frame.constructor, index))) | |
| 677 | ||
| 678 | index = index + 1 | |
| 679 | od | |
| 680 | ||
| 681 | body.add(Values.RET()) | |
| 682 | ||
| 683 | _backend.begin_body() | |
| 684 | ||
| 685 | try | |
| 686 | _backend.write(body) | |
| 687 | _backend.finish_body(frame.constructor) | |
| 688 | finally | |
| 689 | _backend.end_body() | |
| 690 | yrt | |
| 691 | si | |
| 692 | ||
| 693 | si | |
| 694 | si |