| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | const exit_state_slots = 6 |
| 6 | |
| 7 | fn (b &Builder) return_scope() !&Scope { |
| 8 | mut cursor := ScopeRef{b.scope} |
| 9 | for !isnil(cursor.value) { |
| 10 | if cursor.value.return_boundary == .deferred { |
| 11 | return failure('return cannot cross a delayed expression') |
| 12 | } |
| 13 | if cursor.value.return_boundary == .function { return cursor.value } |
| 14 | cursor = ScopeRef{cursor.value.parent} |
| 15 | } |
| 16 | return failure('return is only valid inside a procedure body') |
| 17 | } |
| 18 | |
| 19 | fn (mut b Builder) return_form(arguments []reader.NodeId, tail bool, origin reader.NodeId) ! { |
| 20 | check_arity('return', arguments.len, 0, 1)! |
| 21 | mut target := b.return_scope()! |
| 22 | if b.return_tail { |
| 23 | if arguments.len == 0 { |
| 24 | b.constant(unspecified(), origin) |
| 25 | } else { |
| 26 | b.expression(arguments[0], true) |
| 27 | } |
| 28 | // Following forms are unreachable, including when this return occurs |
| 29 | // while evaluating another call's arguments or a conditional's test. |
| 30 | if !tail { b.emit(.ret, 0, 0, origin) } |
| 31 | return |
| 32 | } |
| 33 | if target.exit_slot < 0 { |
| 34 | if target.function.slot_count > 2147483647 - exit_state_slots { |
| 35 | return failure('return state exceeds the local slot range') |
| 36 | } |
| 37 | target.exit_slot = i32(target.function.slot_count) |
| 38 | target.function.slot_count += exit_state_slots |
| 39 | } |
| 40 | start := b.function.instructions.len |
| 41 | if arguments.len == 0 { |
| 42 | b.constant(unspecified(), origin) |
| 43 | } else { |
| 44 | b.expression(arguments[0], false) |
| 45 | } |
| 46 | if b.function.instructions.len >= 2147483647 { |
| 47 | return failure('return expression exceeds the instruction range') |
| 48 | } |
| 49 | finish := b.emit(.early_return, b.binding_depth(target.function)!, target.exit_slot, origin) |
| 50 | // A return's operand is semantically tail-position even when statements |
| 51 | // follow the return. Earlier calls evaluating the callee/arguments stay calls. |
| 52 | for i in start .. finish { |
| 53 | instruction := b.function.instructions[i] |
| 54 | if instruction.op !in [.call, .scope_call, .syntax_scope_call] { continue } |
| 55 | mut next := i + 1 |
| 56 | for next < finish && b.function.instructions[next].op == .jump { |
| 57 | jump := int(b.function.instructions[next].a) |
| 58 | if jump <= next { break } |
| 59 | next = jump |
| 60 | } |
| 61 | if next == finish { |
| 62 | if instruction.op == .scope_call { |
| 63 | // A return-only bridge outlives the current physical caller. |
| 64 | // Materialize this rare callee's lexical parent before truncating |
| 65 | // that caller; ordinary immediate scopes keep their allocation-free path. |
| 66 | if i == 0 || b.function.instructions[i - 1].op != .scope_function { |
| 67 | return failure('invalid scope call feeding return') |
| 68 | } |
| 69 | prefix := b.function.instructions[i - 1] |
| 70 | container_store_validated(mut b.function.instructions, i - 1, Instruction{ ...prefix, op: .closure }) |
| 71 | } |
| 72 | container_store_validated(mut b.function.instructions, i, Instruction{ |
| 73 | ...instruction |
| 74 | op: if instruction.op == .syntax_scope_call { |
| 75 | Op.syntax_return_call |
| 76 | } else { |
| 77 | Op.return_call |
| 78 | } |
| 79 | b: i32(finish) |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // No marker or traversal for procedures containing only ordinary/tail returns. |
| 86 | // Insert lazily after compilation so the existing path needs no placeholder op. |
| 87 | fn (mut b Builder) prepend_exit_mark(scope &Scope) { |
| 88 | if scope.exit_slot < 0 { return } |
| 89 | count := b.function.instructions.len |
| 90 | if count >= 2147483647 { |
| 91 | b.fault('return marker exceeds the instruction range', reader.no_node) |
| 92 | return |
| 93 | } |
| 94 | origin := b.function.instructions[0].origin |
| 95 | b.function.instructions << Instruction{} |
| 96 | for i := count; i > 0; i-- { |
| 97 | previous := b.function.instructions[i - 1] |
| 98 | instruction := if previous.op in [.jump, .jump_false, .and_jump, .or_jump, .case_match, |
| 99 | .star_bound_skip] { |
| 100 | Instruction{ ...previous, a: previous.a + 1 } |
| 101 | } else if previous.op in [.return_call, .syntax_return_call] { |
| 102 | Instruction{ ...previous, b: previous.b + 1 } |
| 103 | } else if previous.op == .ret && previous.b > 0 { |
| 104 | Instruction{ ...previous, a: previous.a + 1, b: previous.b + 1 } |
| 105 | } else { |
| 106 | previous |
| 107 | } |
| 108 | container_store_validated(mut b.function.instructions, i, instruction) |
| 109 | } |
| 110 | container_store_validated(mut b.function.instructions, 0, Instruction{.exit_mark, i32(scope.exit_slot), 0, origin}) |
| 111 | } |
| 112 | |
| 113 | // Hidden slots belong to this activation, so captured continuations retain the |
| 114 | // marker automatically. There is no separate token, snapshot or registration. |
| 115 | @[direct_array_access] |
| 116 | fn (mut m Machine) mark_exit(slot int) { |
| 117 | container_store_validated(mut m.env.slots, slot, integer(m.frames.len)) |
| 118 | container_store_validated(mut m.env.slots, slot + 1, integer(m.base)) |
| 119 | container_store_validated(mut m.env.slots, slot + 2, payload_value(.wind_frame, m.wind)) |
| 120 | container_store_validated(mut m.env.slots, slot + 3, payload_value(.parameter_context, m.parameters)) |
| 121 | container_store_validated(mut m.env.slots, slot + 4, payload_value(.parameter_context, m.parameter_base)) |
| 122 | container_store_validated(mut m.env.slots, slot + 5, integer(i64(m.logical_context))) |
| 123 | } |
| 124 | |
| 125 | @[direct_array_access] |
| 126 | fn (mut m Machine) return_early(mut vm VM, depth int, slot int) ! { |
| 127 | result := m.pop_value() |
| 128 | target := m.env.ancestor(depth) |
| 129 | count := int(target.slots[slot].whole()) |
| 130 | base := int(target.slots[slot + 1].whole()) |
| 131 | if u64(target.slots[slot + 5].whole()) != m.logical_context || count > m.frames.len { |
| 132 | return failure('return target is outside the active invocation') |
| 133 | } |
| 134 | wind := wind_pointer(target.slots[slot + 2]) |
| 135 | parameters := parameter_binding_pointer(target.slots[slot + 3]) |
| 136 | parameter_base := parameter_binding_pointer(target.slots[slot + 4]) |
| 137 | if same_wind(m.wind, wind) || m.parameter_only_exit(wind) { |
| 138 | for i in count .. m.frames.len { container_store_validated(mut m.frames, i, CallFrame{}) } |
| 139 | unsafe { m.frames.len = count } |
| 140 | m.env = target |
| 141 | m.base = base |
| 142 | m.parameters = parameters |
| 143 | m.parameter_base = parameter_base |
| 144 | m.wind = wind |
| 145 | m.finish_call(result) |
| 146 | return |
| 147 | } |
| 148 | // Only a winding exit needs a persistent destination: a Scheme after |
| 149 | // callback can capture and later replay its pending return, independently. |
| 150 | frame := if count > 0 { m.frames[count - 1] } else { CallFrame{} } |
| 151 | retained := if count > 0 { count - 1 } else { 0 } |
| 152 | escape_environment(frame.env) |
| 153 | for i in 0 .. retained { escape_environment(m.frames[i].env) } |
| 154 | mut sites := m.bridge_sites.len |
| 155 | for sites > 0 && m.bridge_sites[sites - 1].depth > retained { sites-- } |
| 156 | destination := &Continuation{ |
| 157 | owner: vm.owner |
| 158 | context: m.logical_context |
| 159 | wind: wind |
| 160 | function: frame.function |
| 161 | env: frame.env |
| 162 | ip: frame.ip |
| 163 | base: frame.base |
| 164 | origin: m.origin |
| 165 | stack: copy_values(m.stack, 0, base) |
| 166 | frames: copy_container_range(m.frames, 0, retained) |
| 167 | bridge_sites: copy_container_range(m.bridge_sites, 0, sites) |
| 168 | parameters: parameters |
| 169 | parameter_base: parameter_base |
| 170 | } |
| 171 | if !m.transfer(mut vm, destination, result)! { |
| 172 | return ContinuationTransfer{ continuation: destination, result: result } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | fn (m &Machine) parameter_only_exit(target &WindFrame) bool { |
| 177 | mut cursor := WindRef{m.wind} |
| 178 | for !same_wind(cursor.value, target) { |
| 179 | if isnil(cursor.value) || cursor.value.before.tag != .parameter_context |
| 180 | || cursor.value.after.tag != .parameter_context { |
| 181 | return false |
| 182 | } |
| 183 | cursor = WindRef{cursor.value.parent} |
| 184 | } |
| 185 | return true |
| 186 | } |
| 187 | |
| 188 | // This opcode is emitted only for a call feeding an explicit return. Ordinary |
| 189 | // calls keep their existing handler and incur no lookahead or extra branch. |
| 190 | @[direct_array_access] |
| 191 | fn (mut m Machine) call_return(mut vm VM, argc int, exit_index int) ! { |
| 192 | control := m.function.instructions[exit_index] |
| 193 | target := m.env.ancestor(int(control.a)) |
| 194 | slot := int(control.b) |
| 195 | wind := wind_pointer(target.slots[slot + 2]) |
| 196 | if !same_wind(m.wind, wind) { |
| 197 | // The callee must still observe the current dynamic bindings. Once its |
| 198 | // result returns, the original control flow reaches early_return. |
| 199 | m.apply(mut vm, argc, false)! |
| 200 | return |
| 201 | } |
| 202 | count := int(target.slots[slot].whole()) |
| 203 | base := int(target.slots[slot + 1].whole()) |
| 204 | if u64(target.slots[slot + 5].whole()) != m.logical_context || count > m.frames.len { |
| 205 | return failure('return target is outside the active invocation') |
| 206 | } |
| 207 | site := m.error_site() |
| 208 | if !isnil(site.function) { m.reserve_bridge_site(vm, count)! } |
| 209 | mut state := vm.system_state() |
| 210 | if isnil(state.return_function) { |
| 211 | state.return_function = &return_bridge_function |
| 212 | } |
| 213 | start := m.stack.len - argc - 1 |
| 214 | // Move the already evaluated procedure and arguments in this stack's own |
| 215 | // buffer. The destination is at or below the source; no temporary packet. |
| 216 | for i in 0 .. argc + 1 { container_store_validated(mut m.stack, base + i, m.stack[start + i]) } |
| 217 | m.truncate(base + argc + 1) |
| 218 | for i in count .. m.frames.len { container_store_validated(mut m.frames, i, CallFrame{}) } |
| 219 | unsafe { m.frames.len = count } |
| 220 | m.release_bridge_sites(count - 1) |
| 221 | if !isnil(site.function) { m.bridge_sites << BridgeSite{site, count} } |
| 222 | // The return abandons these helpers. Captured scopes stay escaped; the |
| 223 | // existing release routine caches only their unescaped private siblings. |
| 224 | m.release_loop_scopes(target) |
| 225 | m.env = target |
| 226 | m.base = base |
| 227 | m.function = state.return_function |
| 228 | m.ip = 0 |
| 229 | // call/cc will capture the ret-only bridge, never the unreachable forms |
| 230 | // after return. The existing apply loop handles native calls and transfers. |
| 231 | m.apply(mut vm, argc, true)! |
| 232 | } |
| 233 | |