module scheme import reader const exit_state_slots = 6 fn (b &Builder) return_scope() !&Scope { mut cursor := ScopeRef{b.scope} for !isnil(cursor.value) { if cursor.value.return_boundary == .deferred { return failure('return cannot cross a delayed expression') } if cursor.value.return_boundary == .function { return cursor.value } cursor = ScopeRef{cursor.value.parent} } return failure('return is only valid inside a procedure body') } fn (mut b Builder) return_form(arguments []reader.NodeId, tail bool, origin reader.NodeId) ! { check_arity('return', arguments.len, 0, 1)! mut target := b.return_scope()! if b.return_tail { if arguments.len == 0 { b.constant(unspecified(), origin) } else { b.expression(arguments[0], true) } // Following forms are unreachable, including when this return occurs // while evaluating another call's arguments or a conditional's test. if !tail { b.emit(.ret, 0, 0, origin) } return } if target.exit_slot < 0 { if target.function.slot_count > 2147483647 - exit_state_slots { return failure('return state exceeds the local slot range') } target.exit_slot = i32(target.function.slot_count) target.function.slot_count += exit_state_slots } start := b.function.instructions.len if arguments.len == 0 { b.constant(unspecified(), origin) } else { b.expression(arguments[0], false) } if b.function.instructions.len >= 2147483647 { return failure('return expression exceeds the instruction range') } finish := b.emit(.early_return, b.binding_depth(target.function)!, target.exit_slot, origin) // A return's operand is semantically tail-position even when statements // follow the return. Earlier calls evaluating the callee/arguments stay calls. for i in start .. finish { instruction := b.function.instructions[i] if instruction.op !in [.call, .scope_call, .syntax_scope_call] { continue } mut next := i + 1 for next < finish && b.function.instructions[next].op == .jump { jump := int(b.function.instructions[next].a) if jump <= next { break } next = jump } if next == finish { if instruction.op == .scope_call { // A return-only bridge outlives the current physical caller. // Materialize this rare callee's lexical parent before truncating // that caller; ordinary immediate scopes keep their allocation-free path. if i == 0 || b.function.instructions[i - 1].op != .scope_function { return failure('invalid scope call feeding return') } prefix := b.function.instructions[i - 1] container_store_validated(mut b.function.instructions, i - 1, Instruction{ ...prefix, op: .closure }) } container_store_validated(mut b.function.instructions, i, Instruction{ ...instruction op: if instruction.op == .syntax_scope_call { Op.syntax_return_call } else { Op.return_call } b: i32(finish) }) } } } // No marker or traversal for procedures containing only ordinary/tail returns. // Insert lazily after compilation so the existing path needs no placeholder op. fn (mut b Builder) prepend_exit_mark(scope &Scope) { if scope.exit_slot < 0 { return } count := b.function.instructions.len if count >= 2147483647 { b.fault('return marker exceeds the instruction range', reader.no_node) return } origin := b.function.instructions[0].origin b.function.instructions << Instruction{} for i := count; i > 0; i-- { previous := b.function.instructions[i - 1] instruction := if previous.op in [.jump, .jump_false, .and_jump, .or_jump, .case_match, .star_bound_skip] { Instruction{ ...previous, a: previous.a + 1 } } else if previous.op in [.return_call, .syntax_return_call] { Instruction{ ...previous, b: previous.b + 1 } } else if previous.op == .ret && previous.b > 0 { Instruction{ ...previous, a: previous.a + 1, b: previous.b + 1 } } else { previous } container_store_validated(mut b.function.instructions, i, instruction) } container_store_validated(mut b.function.instructions, 0, Instruction{.exit_mark, i32(scope.exit_slot), 0, origin}) } // Hidden slots belong to this activation, so captured continuations retain the // marker automatically. There is no separate token, snapshot or registration. @[direct_array_access] fn (mut m Machine) mark_exit(slot int) { container_store_validated(mut m.env.slots, slot, integer(m.frames.len)) container_store_validated(mut m.env.slots, slot + 1, integer(m.base)) container_store_validated(mut m.env.slots, slot + 2, payload_value(.wind_frame, m.wind)) container_store_validated(mut m.env.slots, slot + 3, payload_value(.parameter_context, m.parameters)) container_store_validated(mut m.env.slots, slot + 4, payload_value(.parameter_context, m.parameter_base)) container_store_validated(mut m.env.slots, slot + 5, integer(i64(m.logical_context))) } @[direct_array_access] fn (mut m Machine) return_early(mut vm VM, depth int, slot int) ! { result := m.pop_value() target := m.env.ancestor(depth) count := int(target.slots[slot].whole()) base := int(target.slots[slot + 1].whole()) if u64(target.slots[slot + 5].whole()) != m.logical_context || count > m.frames.len { return failure('return target is outside the active invocation') } wind := wind_pointer(target.slots[slot + 2]) parameters := parameter_binding_pointer(target.slots[slot + 3]) parameter_base := parameter_binding_pointer(target.slots[slot + 4]) if same_wind(m.wind, wind) || m.parameter_only_exit(wind) { for i in count .. m.frames.len { container_store_validated(mut m.frames, i, CallFrame{}) } unsafe { m.frames.len = count } m.env = target m.base = base m.parameters = parameters m.parameter_base = parameter_base m.wind = wind m.finish_call(result) return } // Only a winding exit needs a persistent destination: a Scheme after // callback can capture and later replay its pending return, independently. frame := if count > 0 { m.frames[count - 1] } else { CallFrame{} } retained := if count > 0 { count - 1 } else { 0 } escape_environment(frame.env) for i in 0 .. retained { escape_environment(m.frames[i].env) } mut sites := m.bridge_sites.len for sites > 0 && m.bridge_sites[sites - 1].depth > retained { sites-- } destination := &Continuation{ owner: vm.owner context: m.logical_context wind: wind function: frame.function env: frame.env ip: frame.ip base: frame.base origin: m.origin stack: copy_values(m.stack, 0, base) frames: copy_container_range(m.frames, 0, retained) bridge_sites: copy_container_range(m.bridge_sites, 0, sites) parameters: parameters parameter_base: parameter_base } if !m.transfer(mut vm, destination, result)! { return ContinuationTransfer{ continuation: destination, result: result } } } fn (m &Machine) parameter_only_exit(target &WindFrame) bool { mut cursor := WindRef{m.wind} for !same_wind(cursor.value, target) { if isnil(cursor.value) || cursor.value.before.tag != .parameter_context || cursor.value.after.tag != .parameter_context { return false } cursor = WindRef{cursor.value.parent} } return true } // This opcode is emitted only for a call feeding an explicit return. Ordinary // calls keep their existing handler and incur no lookahead or extra branch. @[direct_array_access] fn (mut m Machine) call_return(mut vm VM, argc int, exit_index int) ! { control := m.function.instructions[exit_index] target := m.env.ancestor(int(control.a)) slot := int(control.b) wind := wind_pointer(target.slots[slot + 2]) if !same_wind(m.wind, wind) { // The callee must still observe the current dynamic bindings. Once its // result returns, the original control flow reaches early_return. m.apply(mut vm, argc, false)! return } count := int(target.slots[slot].whole()) base := int(target.slots[slot + 1].whole()) if u64(target.slots[slot + 5].whole()) != m.logical_context || count > m.frames.len { return failure('return target is outside the active invocation') } site := m.error_site() if !isnil(site.function) { m.reserve_bridge_site(vm, count)! } mut state := vm.system_state() if isnil(state.return_function) { state.return_function = &return_bridge_function } start := m.stack.len - argc - 1 // Move the already evaluated procedure and arguments in this stack's own // buffer. The destination is at or below the source; no temporary packet. for i in 0 .. argc + 1 { container_store_validated(mut m.stack, base + i, m.stack[start + i]) } m.truncate(base + argc + 1) for i in count .. m.frames.len { container_store_validated(mut m.frames, i, CallFrame{}) } unsafe { m.frames.len = count } m.release_bridge_sites(count - 1) if !isnil(site.function) { m.bridge_sites << BridgeSite{site, count} } // The return abandons these helpers. Captured scopes stay escaped; the // existing release routine caches only their unescaped private siblings. m.release_loop_scopes(target) m.env = target m.base = base m.function = state.return_function m.ip = 0 // call/cc will capture the ret-only bridge, never the unreachable forms // after return. The existing apply loop handles native calls and transfers. m.apply(mut vm, argc, true)! }