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 | // Loop and exception IL: try, catch, do and for walks, and the fused for loop. | |
| 17 | partial GENERATE_IL is | |
| 18 | pre(`try: Statements.TRY) -> bool is | |
| 19 | super.pre(`try) | |
| 20 | ||
| 21 | return true | |
| 22 | si | |
| 23 | ||
| 24 | get_exception_handler_temps() -> (outer_try: LOOP_LABELS?, return_needed: TEMP, return_value: TEMP?) is | |
| 25 | // A return inside a generator's try answers MoveNext, whose | |
| 26 | // result is a bool, rather than the generator function's own | |
| 27 | // return type. One inside an asynchronous function's try | |
| 28 | // carries the task's result, which the frame holds until the | |
| 29 | // task completes. | |
| 30 | let return_type = | |
| 31 | if _current_generator_resumable? then | |
| 32 | _innate_symbol_lookup.get_bool_type() | |
| 33 | elif let machine = _current_async_state_machine /\ current_function == machine.function then | |
| 34 | machine.frame?.result_type | |
| 35 | else | |
| 36 | current_function!.return_type | |
| 37 | fi | |
| 38 | ||
| 39 | let return_needed: TEMP mut | |
| 40 | let return_value: TEMP? mut = _ | |
| 41 | ||
| 42 | let outer_try = _loops.get_current_try() | |
| 43 | ||
| 44 | if outer_try? then | |
| 45 | // get_current_try returns only try-form labels, whose init sets return_needed | |
| 46 | return_needed = outer_try.return_needed! | |
| 47 | return_value = outer_try.return_value | |
| 48 | else | |
| 49 | return_needed = TEMP(current_block, "need_return", 1, _innate_symbol_lookup.get_bool_type()) | |
| 50 | ||
| 51 | if return_type? /\ !return_type.matches(_innate_symbol_lookup.get_void_type()) then | |
| 52 | return_value = TEMP(current_block, "return", return_type) | |
| 53 | fi | |
| 54 | fi | |
| 55 | ||
| 56 | return (outer_try, return_needed, return_value) | |
| 57 | si | |
| 58 | ||
| 59 | // After the structured try for `label` has closed: when a | |
| 60 | // return inside it is pending, carry it on out through any | |
| 61 | // enclosing try, or return. | |
| 62 | gen_exception_handler_exit(outer_try: LOOP_LABELS?, label: LOOP_LABELS, return_value: TEMP?) is | |
| 63 | let no_return = _open_region(label.end) | |
| 64 | ||
| 65 | add(Values.BREAK(no_return, BRANCH.Z, label.return_needed!.load())) | |
| 66 | ||
| 67 | if outer_try? then | |
| 68 | if return_value? then | |
| 69 | assert outer_try.return_value? else "outer try has no return value temporary" | |
| 70 | outer_try.return_value.store(return_value.load()) | |
| 71 | fi | |
| 72 | ||
| 73 | if outer_try.is_in_finally then | |
| 74 | outer_try.return_needed!.store(Literal.NUMBER(1, _innate_symbol_lookup.get_bool_type())) | |
| 75 | add(Values.BREAK(outer_try.finally_region!)) | |
| 76 | else | |
| 77 | add(Values.BREAK(outer_try.structured_try!)) | |
| 78 | fi | |
| 79 | elif | |
| 80 | _current_async_state_machine? /\ | |
| 81 | _current_async_success? /\ | |
| 82 | current_function == _current_async_state_machine!.function | |
| 83 | then | |
| 84 | // State-machine async: a return inside a try block | |
| 85 | // accumulates the value in `return_value` and the | |
| 86 | // "need return" flag. The post-try emit reaches here | |
| 87 | // — for state-machine async we stash the value to | |
| 88 | // `_result` and leave for the success path (NOT `ret`), | |
| 89 | // so the trailer's `builder.SetResult` fires. | |
| 90 | let frame = _current_async_state_machine!.frame | |
| 91 | ||
| 92 | assert frame? else "async state machine has no frame at exception handler exit emission" | |
| 93 | ||
| 94 | let result_field = frame.result_field | |
| 95 | ||
| 96 | if return_value? /\ result_field? then | |
| 97 | add(_build_frame_field_store(frame, result_field, return_value.load())) | |
| 98 | fi | |
| 99 | ||
| 100 | add(Values.BREAK(_current_async_success!)) | |
| 101 | else | |
| 102 | if return_value? then | |
| 103 | add(return_value.load()) | |
| 104 | fi | |
| 105 | ||
| 106 | add(Values.RET()) | |
| 107 | fi | |
| 108 | ||
| 109 | _close_structured(no_return) | |
| 110 | si | |
| 111 | ||
| 112 | visit(`try: Statements.TRY) is | |
| 113 | if | |
| 114 | `try.catches.count == 0 /\ | |
| 115 | !`try.`finally? | |
| 116 | then | |
| 117 | `try.body.walk(self) | |
| 118 | end_try_body(`try) | |
| 119 | ||
| 120 | super.visit(`try) | |
| 121 | ||
| 122 | return | |
| 123 | fi | |
| 124 | ||
| 125 | let (outer_try, return_needed, return_value) = get_exception_handler_temps() | |
| 126 | ||
| 127 | let label = _loops.enter_try(return_needed, return_value) | |
| 128 | ||
| 129 | let need_double_try = `try.catches.count > 0 /\ `try.`finally? | |
| 130 | ||
| 131 | // With both catches and a finally the finally's try encloses | |
| 132 | // the catches' try, as CIL nests the two `.try` blocks; with | |
| 133 | // only one kind of handler there is a single try. Leaving | |
| 134 | // the outermost one leaves the whole statement. | |
| 135 | let void_type = _innate_symbol_lookup.get_void_type() | |
| 136 | ||
| 137 | let outer: Values.TRY? = if need_double_try then Values.TRY(label.start, void_type) else null fi | |
| 138 | let outer_dispatch_holder: ASYNC_DISPATCH_HOLDER? mut = null | |
| 139 | ||
| 140 | if outer? then | |
| 141 | _open_structured(outer) | |
| 142 | outer_dispatch_holder = _maybe_push_dispatch_holder() | |
| 143 | fi | |
| 144 | ||
| 145 | let inner = Values.TRY(if outer? then LABEL() else label.start fi, void_type) | |
| 146 | let whole = outer ?? inner | |
| 147 | ||
| 148 | label.structured_try = whole | |
| 149 | ||
| 150 | _open_structured(inner) | |
| 151 | ||
| 152 | let body_dispatch_holder = _maybe_push_dispatch_holder() | |
| 153 | ||
| 154 | `try.body.walk(self) | |
| 155 | end_try_body(`try) | |
| 156 | ||
| 157 | _maybe_pop_dispatch_holder(body_dispatch_holder) | |
| 158 | ||
| 159 | add(Values.BREAK(whole)) | |
| 160 | ||
| 161 | _close_try_body(inner) | |
| 162 | ||
| 163 | _catching_trys.push(inner) | |
| 164 | ||
| 165 | for c in `try.catches do | |
| 166 | c.walk(self) | |
| 167 | od | |
| 168 | ||
| 169 | _catching_trys.pop() | |
| 170 | ||
| 171 | if outer? then | |
| 172 | _maybe_pop_dispatch_holder(outer_dispatch_holder) | |
| 173 | ||
| 174 | inner.close() | |
| 175 | ||
| 176 | _close_try_body(outer) | |
| 177 | fi | |
| 178 | ||
| 179 | let `finally = `try.`finally | |
| 180 | ||
| 181 | if `finally? then | |
| 182 | label.is_in_finally = true | |
| 183 | ||
| 184 | let finally_region = _open_finally_region(whole, label) | |
| 185 | ||
| 186 | let skip_finally = _open_finally_state_guard() | |
| 187 | ||
| 188 | `finally.walk(self) | |
| 189 | ||
| 190 | _close_finally_state_guard(skip_finally) | |
| 191 | ||
| 192 | _close_finally_region(finally_region) | |
| 193 | fi | |
| 194 | ||
| 195 | _loops.leave_loop() | |
| 196 | ||
| 197 | whole.close() | |
| 198 | ||
| 199 | gen_exception_handler_exit(outer_try, label, return_value) | |
| 200 | ||
| 201 | super.visit(`try) | |
| 202 | si | |
| 203 | ||
| 204 | pre(`catch: Statements.CATCH) -> bool is | |
| 205 | super.pre(`catch) | |
| 206 | ||
| 207 | return true | |
| 208 | si | |
| 209 | ||
| 210 | visit(`catch: Statements.CATCH) is | |
| 211 | let variable = `catch.variable! | |
| 212 | ||
| 213 | let handler = Values.CATCH(variable.type_expression.type!, _innate_symbol_lookup.get_void_type()) | |
| 214 | ||
| 215 | _catching_trys.peek().catches.add(handler) | |
| 216 | ||
| 217 | _open_part(handler.body) | |
| 218 | ||
| 219 | // Catch handler enters with the exception on the stack | |
| 220 | // and immediately stores it into the catch variable. The | |
| 221 | // loop-scoped default-store added for issue #483 must be | |
| 222 | // suppressed here — it would clobber the caught value. | |
| 223 | _in_catch_variable = true | |
| 224 | variable.walk(self) | |
| 225 | _in_catch_variable = false | |
| 226 | ||
| 227 | let symbol = find(variable.name!) | |
| 228 | ||
| 229 | // State-machine-resident catch variable (async function): | |
| 230 | // the exception's on the stack but the variable has no | |
| 231 | // local, since pre(VARIABLE) declared none. Route through | |
| 232 | // the frame field via a temp instead. | |
| 233 | if let v: Semantic.Symbols.Variable = symbol, sm_field = v.state_machine_field then | |
| 234 | let caught = IR.LOCAL(".catch_exc", variable.type_expression.type!) | |
| 235 | ||
| 236 | add(Values.DECLARE_LOCAL(caught)) | |
| 237 | add(Values.STORE_TEMP(caught)) | |
| 238 | ||
| 239 | add( | |
| 240 | _build_frame_field_store( | |
| 241 | cast Semantic.Symbols.STATE_MACHINE_FRAME_BASE?(sm_field.owner)!, | |
| 242 | sm_field, | |
| 243 | Values.Load.TEMP(caught))) | |
| 244 | else | |
| 245 | add(Values.STORE_TEMP((cast Semantic.Symbols.Variable?(symbol))!.ir_local)) | |
| 246 | fi | |
| 247 | ||
| 248 | `catch.body.walk(self) | |
| 249 | ||
| 250 | add(Values.BREAK(_loops.get_current_try()!.structured_try!)) | |
| 251 | ||
| 252 | _close_part(handler.body) | |
| 253 | ||
| 254 | super.visit(`catch) | |
| 255 | si | |
| 256 | ||
| 257 | pre(`do: Statements.DO) -> bool is | |
| 258 | super.pre(`do) | |
| 259 | ||
| 260 | return true | |
| 261 | si | |
| 262 | ||
| 263 | visit(`do: Statements.DO) is | |
| 264 | let loop = _loops.enter_loop(`do) | |
| 265 | ||
| 266 | // Loop-as-expression: compile-expressions decided capture- | |
| 267 | // vs-spill (a suspend point in the body routes the result | |
| 268 | // through a frame field). Every exit converges at one | |
| 269 | // label carrying the result — see _gen_loop_result. | |
| 270 | let value_block: Values.BLOCK? mut = null | |
| 271 | ||
| 272 | if `do.want_value /\ `do.value? then | |
| 273 | if isa Values.BLOCK(`do.value) then | |
| 274 | value_block = `do.value | |
| 275 | loop.wants_value = true | |
| 276 | loop.result_type = `do.value!.type | |
| 277 | fi | |
| 278 | fi | |
| 279 | ||
| 280 | let spiller = COMPOSITE_VALUE_SPILLER(self, `do, `do.value, _current_state_machine_frame()) | |
| 281 | ||
| 282 | spiller.enter() | |
| 283 | ||
| 284 | // Breaks emit during the body walk, so every label and | |
| 285 | // slot they reach is allocated before it - mirroring how | |
| 286 | // VAL_BLOCK pre-allocates its cross-try TEMP at block head. | |
| 287 | if let vb = value_block, rt = loop.result_type then | |
| 288 | loop.result_region = _open_region(LABEL()) | |
| 289 | ||
| 290 | if spiller.is_spilling then | |
| 291 | loop.spill_field = spiller.spill_field | |
| 292 | else | |
| 293 | loop.cross_try_temp = TEMP(vb, "loop_cross_try", rt) | |
| 294 | loop.cross_try_join_region = _open_region(LABEL()) | |
| 295 | fi | |
| 296 | fi | |
| 297 | ||
| 298 | let structured = _open_structured_loop(loop) | |
| 299 | ||
| 300 | if let `do.binding? then | |
| 301 | // `while let` clauses: emit the same per-clause | |
| 302 | // test / bind sequence as `if let`, but a failure | |
| 303 | // leaves the loop instead of the arm. Each iteration re-evaluates | |
| 304 | // every clause's scrutinee and re-binds — the | |
| 305 | // bound names are freshly stored on each pass. | |
| 306 | let first = _emit_if_let_clause_test(binding.clauses[0], structured) | |
| 307 | ||
| 308 | self.pre(binding) | |
| 309 | ||
| 310 | if first? then | |
| 311 | _emit_if_let_clause_bind(binding.clauses[0], first, structured) | |
| 312 | fi | |
| 313 | ||
| 314 | if binding.clauses[0].guard? then | |
| 315 | binding.clauses[0].guard!.walk(self) | |
| 316 | add(Values.BREAK(structured, BRANCH.Z, binding.clauses[0].guard!.value!)) | |
| 317 | fi | |
| 318 | ||
| 319 | let i mut = 1 | |
| 320 | while i < binding.clauses.count do | |
| 321 | let clause = binding.clauses[i] | |
| 322 | ||
| 323 | let temp = _emit_if_let_clause_test(clause, structured) | |
| 324 | ||
| 325 | if temp? then | |
| 326 | _emit_if_let_clause_bind(clause, temp, structured) | |
| 327 | fi | |
| 328 | ||
| 329 | if clause.guard? then | |
| 330 | clause.guard.walk(self) | |
| 331 | add(Values.BREAK(structured, BRANCH.Z, clause.guard!.value!)) | |
| 332 | fi | |
| 333 | ||
| 334 | i = i + 1 | |
| 335 | od | |
| 336 | elif `do.condition? then | |
| 337 | `do.condition.walk(self) | |
| 338 | ||
| 339 | add(Values.BREAK(structured, BRANCH.Z, `do.condition!.value!)) | |
| 340 | fi | |
| 341 | ||
| 342 | `do.body.walk(self) | |
| 343 | ||
| 344 | _close_structured_loop(structured) | |
| 345 | ||
| 346 | if let vb = value_block, rt = loop.result_type then | |
| 347 | _gen_loop_result(loop, rt, spiller) | |
| 348 | fi | |
| 349 | ||
| 350 | super.visit(`do) | |
| 351 | ||
| 352 | spiller.leave() | |
| 353 | ||
| 354 | _loops.leave_loop() | |
| 355 | si | |
| 356 | ||
| 357 | pre(`for: Statements.FOR) -> bool is | |
| 358 | super.pre(`for) | |
| 359 | ||
| 360 | return true | |
| 361 | si | |
| 362 | ||
| 363 | visit(`for: Statements.FOR) is | |
| 364 | // A recognised, fusible Pipe[T] chain is lowered to one | |
| 365 | // inline loop over the pinned base, applying the map/filter | |
| 366 | // stages per element, instead of iterating the built pipe | |
| 367 | // objects. Not in a state machine, whose iterator must live | |
| 368 | // on the frame to survive yield/await re-entry - those fall | |
| 369 | // through to the normal loop below. A loop in expression | |
| 370 | // position also takes the normal path: its result-value | |
| 371 | // convergence hangs off this method's shape. | |
| 372 | if | |
| 373 | !`for.want_value /\ | |
| 374 | `for.fusion? /\ | |
| 375 | !Semantic.Symbols.state_machine_for(current_function)? /\ | |
| 376 | !Semantic.Symbols.async_state_machine_for(current_function)? | |
| 377 | then | |
| 378 | _visit_fused_for(`for) | |
| 379 | ||
| 380 | return | |
| 381 | fi | |
| 382 | ||
| 383 | let loop = _loops.enter_loop(`for) | |
| 384 | ||
| 385 | let value_block: Values.BLOCK? mut = null | |
| 386 | ||
| 387 | if `for.want_value /\ `for.value? then | |
| 388 | if isa Values.BLOCK(`for.value) then | |
| 389 | value_block = `for.value | |
| 390 | loop.wants_value = true | |
| 391 | loop.result_type = `for.value!.type | |
| 392 | fi | |
| 393 | fi | |
| 394 | ||
| 395 | let spiller = COMPOSITE_VALUE_SPILLER(self, `for, `for.value, _current_state_machine_frame()) | |
| 396 | ||
| 397 | spiller.enter() | |
| 398 | ||
| 399 | // Breaks emit during the body walk, so every label and | |
| 400 | // slot they reach is allocated before it. | |
| 401 | if let vb = value_block, rt = loop.result_type then | |
| 402 | loop.result_region = _open_region(LABEL()) | |
| 403 | ||
| 404 | if spiller.is_spilling then | |
| 405 | loop.spill_field = spiller.spill_field | |
| 406 | else | |
| 407 | loop.cross_try_temp = TEMP(vb, "loop_cross_try", rt) | |
| 408 | loop.cross_try_join_region = _open_region(LABEL()) | |
| 409 | fi | |
| 410 | fi | |
| 411 | ||
| 412 | let expression = `for.expression! | |
| 413 | let variable = `for.variable! | |
| 414 | let body = `for.body! | |
| 415 | ||
| 416 | let iterator: Value mut | |
| 417 | ||
| 418 | if `for.read_iterator? then | |
| 419 | iterator = `for.read_iterator.call(expression.location, expression.value!, Collections.LIST[Value](0), null, _function_caller) | |
| 420 | else | |
| 421 | iterator = expression.value! | |
| 422 | fi | |
| 423 | ||
| 424 | variable.walk(self) | |
| 425 | ||
| 426 | // Generator / async FOR: the iterator value has to survive | |
| 427 | // every yield / await in the body, so it is read and written | |
| 428 | // through the frame field compile-expressions allocated for | |
| 429 | // it. The regular CLR-local TEMP path would reset on every | |
| 430 | // MoveNext re-entry. | |
| 431 | let iterator_field = `for.iterator_field | |
| 432 | let iterator_frame = _current_state_machine_frame() | |
| 433 | ||
| 434 | if iterator_field? /\ iterator_frame? then | |
| 435 | add(_build_frame_field_store(iterator_frame, iterator_field, iterator)) | |
| 436 | fi | |
| 437 | ||
| 438 | let temp: TEMP? mut = null | |
| 439 | let iteration: IR.FOR_ITERATION? mut = null | |
| 440 | ||
| 441 | if !iterator_field? then | |
| 442 | temp = TEMP(current_block, "iterator", iterator) | |
| 443 | ||
| 444 | if `for.read_iterator? then | |
| 445 | iteration = IR.FOR_ITERATION(expression.value!, iterator) | |
| 446 | temp!.local.iteration = iteration | |
| 447 | fi | |
| 448 | fi | |
| 449 | ||
| 450 | let structured = _open_structured_loop(loop) | |
| 451 | ||
| 452 | let load_iter_a = | |
| 453 | if iterator_field? then | |
| 454 | cast Value(IR.Values.Load.INSTANCE_FIELD( | |
| 455 | IR.Values.Load.REFERENCE_SELF(iterator_frame!, iterator_frame.type), | |
| 456 | iterator_field | |
| 457 | )) | |
| 458 | else | |
| 459 | cast Value(temp!.load()) | |
| 460 | fi | |
| 461 | ||
| 462 | let has_next = `for.move_next!.call(expression.location, load_iter_a, Collections.LIST[Value](0), null, _function_caller) | |
| 463 | ||
| 464 | add(Values.BREAK(structured, BRANCH.Z, has_next)) | |
| 465 | ||
| 466 | if let i = iteration then | |
| 467 | i.move_next = has_next | |
| 468 | fi | |
| 469 | ||
| 470 | let load_iter_b = | |
| 471 | if iterator_field? then | |
| 472 | cast Value(IR.Values.Load.INSTANCE_FIELD( | |
| 473 | IR.Values.Load.REFERENCE_SELF(iterator_frame!, iterator_frame.type), | |
| 474 | iterator_field | |
| 475 | )) | |
| 476 | else | |
| 477 | cast Value(temp!.load()) | |
| 478 | fi | |
| 479 | ||
| 480 | let current = `for.read_current!.call(expression.location, load_iter_b, Collections.LIST[Value](0), null, _function_caller) | |
| 481 | ||
| 482 | if let i = iteration then | |
| 483 | i.current = current | |
| 484 | fi | |
| 485 | ||
| 486 | gen_destructuring_initialize(variable.left, current) | |
| 487 | ||
| 488 | // not to generate code for the expression itself, but to capture IL for any anonymous function bodies: | |
| 489 | // TODO check that if expressions within the expression are emitted correctly | |
| 490 | expression.walk(self) | |
| 491 | ||
| 492 | body.walk(self) | |
| 493 | ||
| 494 | _close_structured_loop(structured) | |
| 495 | ||
| 496 | if let vb = value_block, rt = loop.result_type then | |
| 497 | _gen_loop_result(loop, rt, spiller) | |
| 498 | fi | |
| 499 | ||
| 500 | super.visit(`for) | |
| 501 | ||
| 502 | spiller.leave() | |
| 503 | ||
| 504 | _loops.leave_loop() | |
| 505 | si | |
| 506 | ||
| 507 | // Declare the per-stage locals a fused chain needs before its loop, | |
| 508 | // filling `stage_delegates` / `stage_counters` parallel to the | |
| 509 | // stages (null where a slot does not apply): an inlined stage's | |
| 510 | // parameter local, a delegate stage's hoisted function value, an | |
| 511 | // index stage's running counter initialised to its start. | |
| 512 | _setup_fused_stages( | |
| 513 | fusion: Syntax.Process.PIPE_FUSION, | |
| 514 | stage_delegates: Collections.LIST[TEMP?], | |
| 515 | stage_counters: Collections.LIST[TEMP?] | |
| 516 | ) is | |
| 517 | for stage in fusion.stages_outermost_first do | |
| 518 | if stage.is_countdown then | |
| 519 | // take/skip: evaluate the count once into a running | |
| 520 | // counter the loop decrements per pulled element. | |
| 521 | let counter = TEMP(current_block, "fused_countdown", _innate_symbol_lookup.get_int_type()) | |
| 522 | counter.store(stage.argument!.value!) | |
| 523 | stage_delegates.add(null) | |
| 524 | stage_counters.add(counter) | |
| 525 | elif stage.is_inlined then | |
| 526 | add(Values.DECLARE_LOCAL(stage.param_local!.ir_local)) | |
| 527 | stage_delegates.add(null) | |
| 528 | stage_counters.add(null) | |
| 529 | elif stage.is_index then | |
| 530 | // The counter starts at the caller's start where one | |
| 531 | // was given, and at zero otherwise. | |
| 532 | let int_type = _innate_symbol_lookup.get_int_type() | |
| 533 | let counter = TEMP(current_block, "fused_index", int_type) | |
| 534 | ||
| 535 | if let start = stage.argument then | |
| 536 | counter.store(start.value!) | |
| 537 | else | |
| 538 | counter.store(Literal.NUMBER(0, int_type)) | |
| 539 | fi | |
| 540 | ||
| 541 | stage_delegates.add(null) | |
| 542 | stage_counters.add(counter) | |
| 543 | else | |
| 544 | stage_delegates.add(TEMP(current_block, "fused_stage", stage.argument!.value!)) | |
| 545 | stage_counters.add(null) | |
| 546 | fi | |
| 547 | od | |
| 548 | si | |
| 549 | ||
| 550 | // Before the loop pulls an element, leave it when any | |
| 551 | // take stage has spent its count: a fused take then pulls exactly | |
| 552 | // the elements it yields, as the TAKE_PIPE it replaces does, and | |
| 553 | // never advances the source past them. | |
| 554 | _gen_fused_take_guards( | |
| 555 | fusion: Syntax.Process.PIPE_FUSION, | |
| 556 | stage_counters: Collections.List[TEMP?], | |
| 557 | loop: Values.LOOP | |
| 558 | ) is | |
| 559 | let int_type = _innate_symbol_lookup.get_int_type() | |
| 560 | ||
| 561 | for index in 0..fusion.stages_outermost_first.count do | |
| 562 | if fusion.stages_outermost_first[index].is_take then | |
| 563 | add(Values.BREAK(loop, BRANCH.LE, stage_counters[index]!.load(), Literal.NUMBER(0, int_type))) | |
| 564 | fi | |
| 565 | od | |
| 566 | si | |
| 567 | ||
| 568 | // Apply a fused chain's stages to `element` innermost-first, | |
| 569 | // returning the fully-transformed value. A map result and an index | |
| 570 | // result each land in a fresh temp so a downstream stage can reload | |
| 571 | // them; a filter reject continues `loop` (the next pull); a | |
| 572 | // take spends one of its count, checked before the next pull; an | |
| 573 | // index stage builds INDEXED_VALUE(counter++, current) directly. | |
| 574 | _apply_fused_stages( | |
| 575 | fusion: Syntax.Process.PIPE_FUSION, | |
| 576 | element: Value, | |
| 577 | stage_delegates: Collections.List[TEMP?], | |
| 578 | stage_counters: Collections.List[TEMP?], | |
| 579 | loop: Values.LOOP | |
| 580 | ) -> Value is | |
| 581 | let int_type = _innate_symbol_lookup.get_int_type() | |
| 582 | ||
| 583 | let current_value: Value mut = element | |
| 584 | ||
| 585 | let index mut = fusion.stages_outermost_first.count - 1 | |
| 586 | ||
| 587 | while index >= 0 do | |
| 588 | let stage = fusion.stages_outermost_first[index] | |
| 589 | ||
| 590 | if stage.is_take then | |
| 591 | // This element spends one of the take's count; the loop | |
| 592 | // checks the count before pulling the next element (see | |
| 593 | // _gen_fused_take_guards). The element passes through | |
| 594 | // unchanged. | |
| 595 | add(IR.Values.DECREMENT(stage_counters[index]!.local, int_type)) | |
| 596 | elif stage.is_skip then | |
| 597 | // Decrement the counter; while it stays non-negative this | |
| 598 | // is a leading element to drop, so pull the next one. | |
| 599 | add(Values.CONTINUE(loop, BRANCH.GE, IR.Values.PRE_DECREMENT(stage_counters[index]!.local, int_type), Literal.NUMBER(0, int_type))) | |
| 600 | else | |
| 601 | let stage_result: Value mut | |
| 602 | ||
| 603 | if stage.is_inlined then | |
| 604 | // Assign the incoming element to the parameter local; | |
| 605 | // the pre-harvested body IR reads it (and any captured | |
| 606 | // outer locals) as ordinary locals - no delegate, no | |
| 607 | // frame. | |
| 608 | add(IR.Values.Store.LOCAL_VARIABLE(stage.param_local!, current_value)) | |
| 609 | ||
| 610 | fill_inlined_body(stage.inlined_closure!) | |
| 611 | stage_result = stage.inline_body! | |
| 612 | elif stage.is_index then | |
| 613 | // INDEXED_VALUE(counter, current) with the counter | |
| 614 | // post-incremented in place - no INDEX_PIPE object. | |
| 615 | let arguments = Collections.LIST[Value]() | |
| 616 | arguments.add(cast Value(IR.Values.POST_INCREMENT(stage_counters[index]!.local, int_type))) | |
| 617 | arguments.add(current_value) | |
| 618 | ||
| 619 | stage_result = IR.Values.NEW(stage.indexed_value_type!, stage.indexed_value_constructor!, arguments) | |
| 620 | else | |
| 621 | let delegate = stage_delegates[index]!.load() | |
| 622 | let func_type = stage.argument!.value!.type! | |
| 623 | ||
| 624 | let result_type = | |
| 625 | if func_type.is_action then | |
| 626 | _innate_symbol_lookup.get_void_type() | |
| 627 | else | |
| 628 | func_type.arguments[func_type.arguments.count - 1] | |
| 629 | fi | |
| 630 | ||
| 631 | let call_arguments = Collections.LIST[Value]() | |
| 632 | call_arguments.add(current_value) | |
| 633 | ||
| 634 | stage_result = IR.Values.Call.CLOSURE(delegate, result_type, func_type.is_action, func_type, call_arguments) | |
| 635 | fi | |
| 636 | ||
| 637 | if stage.is_filter then | |
| 638 | // Rejected elements skip straight to the next pull. | |
| 639 | add(Values.CONTINUE(loop, BRANCH.Z, stage_result)) | |
| 640 | else | |
| 641 | current_value = TEMP(current_block, "fused_mapped", stage_result).load() | |
| 642 | fi | |
| 643 | fi | |
| 644 | ||
| 645 | index = index - 1 | |
| 646 | od | |
| 647 | ||
| 648 | return current_value | |
| 649 | si | |
| 650 | ||
| 651 | // Lower a recognised fusible Pipe[T] chain to a single loop: | |
| 652 | // drive the pinned base's own iterator, apply each map/filter/index | |
| 653 | // stage inline per element (map/index assign a temp, filter skips to | |
| 654 | // the next element), then bind the loop variable and run the | |
| 655 | // body. The built MAP_PIPE / FilterPipe / INDEX_PIPE objects and | |
| 656 | // their per-element virtual dispatch are never emitted. | |
| 657 | // Convergence for a loop used as an expression (see LOOP_ | |
| 658 | // LABELS): the natural exit yields absence; valued breaks | |
| 659 | // arrive having pushed or stored their value; a cross-try | |
| 660 | // join reloads the stashed temp. One result-typed value is | |
| 661 | // available to the consumer afterwards. The result region was | |
| 662 | // opened at loop entry — breaks leave it from inside the body | |
| 663 | // walk. | |
| 664 | _gen_loop_result(loop: LOOP_LABELS, result_type: Semantic.Types.Type, spiller: COMPOSITE_VALUE_SPILLER) is | |
| 665 | let result = loop.result_region! | |
| 666 | ||
| 667 | spiller.emit_value(IR.Values.DEFAULT(result_type)) | |
| 668 | add(Values.BREAK(result)) | |
| 669 | ||
| 670 | if let join = loop.cross_try_join_region then | |
| 671 | _close_structured(join) | |
| 672 | ||
| 673 | if loop.cross_try_used then | |
| 674 | add(loop.cross_try_temp!.load()) | |
| 675 | fi | |
| 676 | fi | |
| 677 | ||
| 678 | _close_structured(result) | |
| 679 | si | |
| 680 | ||
| 681 | // Opens the structured loop `loop`'s body is generated into. | |
| 682 | _open_structured_loop(loop: LOOP_LABELS) -> Values.LOOP is | |
| 683 | let result = Values.LOOP(loop.start, loop.end, _innate_symbol_lookup.get_void_type()) | |
| 684 | ||
| 685 | loop.structured = result | |
| 686 | ||
| 687 | _open_structured(result) | |
| 688 | ||
| 689 | return result | |
| 690 | si | |
| 691 | ||
| 692 | _close_structured_loop(loop: Values.LOOP) is | |
| 693 | _close_structured(loop) | |
| 694 | si | |
| 695 | ||
| 696 | // Opens a region whose end is `end`. | |
| 697 | _open_region(end: LABEL) -> Values.REGION is | |
| 698 | let result = Values.REGION(end, _innate_symbol_lookup.get_void_type()) | |
| 699 | ||
| 700 | _open_structured(result) | |
| 701 | ||
| 702 | return result | |
| 703 | si | |
| 704 | ||
| 705 | // Adds `node` and opens its body: values added from here until | |
| 706 | // _close_structured land in it. | |
| 707 | _open_structured(node: Values.Breakable) is | |
| 708 | add(node) | |
| 709 | _open_part(node.body) | |
| 710 | si | |
| 711 | ||
| 712 | _close_structured(node: Values.Breakable) is | |
| 713 | _close_part(node.body) | |
| 714 | ||
| 715 | node.close() | |
| 716 | si | |
| 717 | ||
| 718 | // Ends the protected body of `try`, so its handlers can follow. | |
| 719 | _close_try_body(`try: Values.TRY) is | |
| 720 | _close_part(`try.body) | |
| 721 | si | |
| 722 | ||
| 723 | // Opens the region the finally body of `try`, whose loop stack | |
| 724 | // entry is `label`, is generated into. | |
| 725 | _open_finally_region(`try: Values.TRY, label: LOOP_LABELS) -> Values.REGION is | |
| 726 | let result = Values.REGION(label.middle, _innate_symbol_lookup.get_void_type()) | |
| 727 | ||
| 728 | `try.finally_region = result | |
| 729 | label.finally_region = result | |
| 730 | ||
| 731 | _open_part(result.body) | |
| 732 | ||
| 733 | return result | |
| 734 | si | |
| 735 | ||
| 736 | _close_finally_region(region: Values.REGION) is | |
| 737 | _close_part(region.body) | |
| 738 | ||
| 739 | region.close() | |
| 740 | si | |
| 741 | ||
| 742 | // Values added from here until _close_part land in `body`, a | |
| 743 | // part of a structured node in the current block. | |
| 744 | _open_part(body: Values.BLOCK) is | |
| 745 | current_block.open_body(body) | |
| 746 | enter_block(body) | |
| 747 | si | |
| 748 | ||
| 749 | _close_part(body: Values.BLOCK) is | |
| 750 | leave_block() | |
| 751 | current_block.close_body(body) | |
| 752 | si | |
| 753 | ||
| 754 | _visit_fused_for(`for: Statements.FOR) is | |
| 755 | let fusion = `for.fusion! | |
| 756 | let source = fusion.source | |
| 757 | let variable = `for.variable! | |
| 758 | let body = `for.body! | |
| 759 | ||
| 760 | let loop = _loops.enter_loop(`for) | |
| 761 | ||
| 762 | // Iterate the source directly - the wrapping pipe objects are | |
| 763 | // never built. read_iterator is null when the source is its | |
| 764 | // own iterator (e.g. a range). | |
| 765 | let iterator: Value mut | |
| 766 | ||
| 767 | if fusion.source_read_iterator? then | |
| 768 | iterator = fusion.source_read_iterator.call(source.location, source.value!, Collections.LIST[Value](0), null, _function_caller) | |
| 769 | else | |
| 770 | iterator = source.value! | |
| 771 | fi | |
| 772 | ||
| 773 | variable.walk(self) | |
| 774 | ||
| 775 | let temp = TEMP(current_block, "fused_iterator", iterator) | |
| 776 | ||
| 777 | // Per-stage setup: an inlined stage needs a slot for its | |
| 778 | // parameter local; a delegate stage hoists its function value | |
| 779 | // into a local so the delegate is built once, not per element; | |
| 780 | // an index stage needs a running counter, initialised to its | |
| 781 | // start. | |
| 782 | let stage_delegates = Collections.LIST[TEMP?]() | |
| 783 | let stage_counters = Collections.LIST[TEMP?]() | |
| 784 | ||
| 785 | _setup_fused_stages(fusion, stage_delegates, stage_counters) | |
| 786 | ||
| 787 | let structured = _open_structured_loop(loop) | |
| 788 | ||
| 789 | _gen_fused_take_guards(fusion, stage_counters, structured) | |
| 790 | ||
| 791 | let has_next = fusion.source_move_next!.call(source.location, temp.load(), Collections.LIST[Value](0), null, _function_caller) | |
| 792 | ||
| 793 | add(Values.BREAK(structured, BRANCH.Z, has_next)) | |
| 794 | ||
| 795 | let element = fusion.source_read_current!.call(source.location, temp.load(), Collections.LIST[Value](0), null, _function_caller) | |
| 796 | ||
| 797 | let current_value = _apply_fused_stages(fusion, element, stage_delegates, stage_counters, structured) | |
| 798 | ||
| 799 | gen_destructuring_initialize(variable.left, current_value) | |
| 800 | ||
| 801 | // Emit any anonymous-function bodies in the chain (the | |
| 802 | // map/filter lambdas, plus anything inside the source). | |
| 803 | // Walking these nodes emits the nested closure method bodies | |
| 804 | // without emitting the pipe-construction IL itself. | |
| 805 | source.walk(self) | |
| 806 | ||
| 807 | for stage in fusion.stages_outermost_first do | |
| 808 | if stage.argument? then | |
| 809 | stage.argument.walk(self) | |
| 810 | fi | |
| 811 | od | |
| 812 | ||
| 813 | body.walk(self) | |
| 814 | ||
| 815 | _close_structured_loop(structured) | |
| 816 | ||
| 817 | super.visit(`for) | |
| 818 | ||
| 819 | _loops.leave_loop() | |
| 820 | si | |
| 821 | ||
| 822 | si | |
| 823 | si |