| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | fn (b &Builder) loop_scope(name string) !&Scope { |
| 6 | mut cursor := ScopeRef{b.scope} |
| 7 | for !isnil(cursor.value) { |
| 8 | match cursor.value.return_boundary { |
| 9 | .loop { return cursor.value } |
| 10 | .function { return failure('${name} cannot cross a procedure boundary') } |
| 11 | .deferred { return failure('${name} cannot cross a delayed expression') } |
| 12 | .none {} |
| 13 | } |
| 14 | cursor = ScopeRef{cursor.value.parent} |
| 15 | } |
| 16 | return failure('${name} is only valid inside a loop') |
| 17 | } |
| 18 | |
| 19 | fn (mut b Builder) break_form(arguments []reader.NodeId, origin reader.NodeId) ! { |
| 20 | check_arity('break', arguments.len, 0, 0)! |
| 21 | mut target := b.loop_scope('break')! |
| 22 | reserve_loop_exit(mut target)! |
| 23 | // The loop wrapper owns this state for the whole loop, rather than adding |
| 24 | // marker slots or instructions to each iteration's activation. |
| 25 | b.constant(unspecified(), origin) |
| 26 | b.emit(.early_return, b.binding_depth(target.function)!, target.exit_slot, origin) |
| 27 | } |
| 28 | |
| 29 | fn reserve_loop_exit(mut target Scope) ! { |
| 30 | if target.exit_slot < 0 { |
| 31 | if target.function.slot_count > 2147483647 - exit_state_slots { |
| 32 | return failure('loop exit state exceeds the local slot range') |
| 33 | } |
| 34 | target.exit_slot = i32(target.function.slot_count) |
| 35 | target.function.slot_count += exit_state_slots |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | fn (mut b Builder) continue_form(arguments []reader.NodeId, origin reader.NodeId) ! { |
| 40 | check_arity('continue', arguments.len, 0, 0)! |
| 41 | mut target := b.loop_scope('continue')! |
| 42 | reserve_loop_exit(mut target)! |
| 43 | if target.exit_slot != target.function.params + 1 { |
| 44 | return failure('invalid private loop exit layout') |
| 45 | } |
| 46 | target.continue_used = true |
| 47 | depth := b.binding_depth(target.function)! |
| 48 | if depth < 1 { return failure('continue is outside the iteration body') } |
| 49 | // Capture the wrapper too, preserving every helper and the current |
| 50 | // iteration environment on the path to it, including zero-slot scopes. |
| 51 | b.emit(.loop_continue, depth, target.exit_slot, origin) |
| 52 | } |
| 53 | |
| 54 | @[direct_array_access] |
| 55 | fn (mut m Machine) continue_loop(mut vm VM, depth int, slot int) ! { |
| 56 | iteration := m.env.ancestor(depth - 1) |
| 57 | target := iteration.parent |
| 58 | count := int(target.slots[slot].whole()) |
| 59 | base := int(target.slots[slot + 1].whole()) |
| 60 | if u64(target.slots[slot + 5].whole()) != m.logical_context || count > m.frames.len { |
| 61 | return failure('continue target is outside the active invocation') |
| 62 | } |
| 63 | function := target.slots[slot - 1].procedure_pointer().function |
| 64 | resume := function.instructions.last() |
| 65 | wind := wind_pointer(target.slots[slot + 2]) |
| 66 | parameters := parameter_binding_pointer(target.slots[slot + 3]) |
| 67 | parameter_base := parameter_binding_pointer(target.slots[slot + 4]) |
| 68 | if same_wind(m.wind, wind) || m.parameter_only_exit(wind) { |
| 69 | m.release_loop_scopes(iteration) |
| 70 | for i in count .. m.frames.len { container_store_validated(mut m.frames, i, CallFrame{}) } |
| 71 | unsafe { m.frames.len = count } |
| 72 | m.release_bridge_sites(count) |
| 73 | m.truncate(base) |
| 74 | m.env = iteration |
| 75 | m.function = function |
| 76 | m.ip = int(resume.a) |
| 77 | m.base = base |
| 78 | m.parameters = parameters |
| 79 | m.parameter_base = parameter_base |
| 80 | m.wind = wind |
| 81 | return |
| 82 | } |
| 83 | // A winding callback may retain and replay this transfer. Its immutable |
| 84 | // destination keeps the current iteration and discards partial step args. |
| 85 | escape_environment(iteration) |
| 86 | for i in 0 .. count { escape_environment(m.frames[i].env) } |
| 87 | mut sites := m.bridge_sites.len |
| 88 | for sites > 0 && m.bridge_sites[sites - 1].depth > count { sites-- } |
| 89 | destination := &Continuation{ |
| 90 | owner: vm.owner |
| 91 | context: m.logical_context |
| 92 | wind: wind |
| 93 | function: function |
| 94 | env: iteration |
| 95 | ip: int(resume.b) |
| 96 | base: base |
| 97 | origin: m.origin |
| 98 | stack: copy_values(m.stack, 0, base) |
| 99 | frames: copy_container_range(m.frames, 0, count) |
| 100 | bridge_sites: copy_container_range(m.bridge_sites, 0, sites) |
| 101 | parameters: parameters |
| 102 | parameter_base: parameter_base |
| 103 | } |
| 104 | if !m.transfer(mut vm, destination, unspecified())! { |
| 105 | return ContinuationTransfer{ continuation: destination, result: unspecified() } |
| 106 | } |
| 107 | } |
| 108 | |