module scheme import reader // These instruction-only programs never contain a VM, environment or mutable // Scheme value. Static storage avoids publishing a caller arena's allocation // when a bridge is first used by an older VM or captured continuation. const return_bridge_function_code = [ Instruction{ op: .ret, origin: reader.no_node } ]! const return_bridge_function = Function{ code: unsafe { nil } instructions: unsafe { static_bridge_instructions(&return_bridge_function_code[0], return_bridge_function_code.len) } } const process_exit_bridge_function_code = [ Instruction{ op: .process_exit_finish, origin: reader.no_node } ]! const process_exit_bridge_function = Function{ code: unsafe { nil } instructions: unsafe { static_bridge_instructions(&process_exit_bridge_function_code[0], process_exit_bridge_function_code.len) } } const wind_transfer_bridge_function_code = [ Instruction{ op: .wind_step, origin: reader.no_node } Instruction{ op: .wind_resume, origin: reader.no_node } Instruction{ op: .jump, origin: reader.no_node } ]! const wind_transfer_bridge_function = Function{ code: unsafe { nil } slot_count: 3 instructions: unsafe { static_bridge_instructions(&wind_transfer_bridge_function_code[0], wind_transfer_bridge_function_code.len) } } const member_comparison_bridge_code = [ Instruction{ op: .search_init, origin: reader.no_node } Instruction{ op: .search_step, origin: reader.no_node } Instruction{ op: .search_collect, origin: reader.no_node } Instruction{ op: .jump, a: 1, origin: reader.no_node } ]! const member_comparison_bridge = Function{ code: unsafe { nil } params: 3 slot_count: 6 instructions: unsafe { static_bridge_instructions(&member_comparison_bridge_code[0], member_comparison_bridge_code.len) } } const assoc_comparison_bridge_code = [ Instruction{ op: .search_init, a: 1, origin: reader.no_node } Instruction{ op: .search_step, a: 1, origin: reader.no_node } Instruction{ op: .search_collect, a: 1, origin: reader.no_node } Instruction{ op: .jump, a: 1, origin: reader.no_node } ]! const assoc_comparison_bridge = Function{ code: unsafe { nil } params: 3 slot_count: 6 instructions: unsafe { static_bridge_instructions(&assoc_comparison_bridge_code[0], assoc_comparison_bridge_code.len) } } // Instruction-only protocol shared by all VMs. It contains no owner or values. const call_values_bridge_function_code = [ Instruction{op: .local, a: 0, b: 1, origin: reader.no_node} Instruction{op: .local, a: 0, b: 0, origin: reader.no_node} Instruction{op: .call, a: 0, b: 0, origin: reader.no_node} Instruction{op: .call_values_tail, a: 0, b: 0, origin: reader.no_node} Instruction{op: .ret, a: 0, b: 0, origin: reader.no_node} ]! const call_values_bridge_function = Function{ code: unsafe { nil } params: 2 slot_count: 2 instructions: unsafe { static_bridge_instructions(&call_values_bridge_function_code[0], call_values_bridge_function_code.len) } } struct SourceSite { function &Function = unsafe { nil } origin reader.NodeId = reader.no_node } struct BridgeSite { site SourceSite depth int } fn (mut vm VM) install_control_primitives() { vm.install_list_utility_primitives() vm.install_sort_primitives() vm.install_iteration_primitives() vm.install_indexed_iteration_primitives() vm.install_promise_primitive() vm.install_wind_primitive() vm.install_parameter_primitive() call_cc := procedure_value(&Procedure{ mode: .call_cc, name: 'call-with-current-continuation', owner: vm.owner }) vm.bind_global('call-with-current-continuation', call_cc) vm.bind_global('call/cc', call_cc) vm.bind_global('values', procedure_value(&Procedure{ mode: .values, name: 'values', owner: vm.owner })) // Both Scheme callbacks run on this same machine. The producer returns to // this private bridge; its consumer application is a proper tail call. vm.bind_global('call-with-values', procedure_value(&Procedure{ mode: .closure, name: 'call-with-values', owner: vm.owner, function: &call_values_bridge_function })) } fn bridge_instruction(op Op, a int, b int) Instruction { return Instruction{op, i32(a), i32(b), reader.no_node} } fn (m &Machine) error_site() SourceSite { if !isnil(m.function) && !isnil(m.function.code) && m.origin != reader.no_node { return SourceSite{m.function, m.origin} } if m.bridge_sites.len > 0 { return m.bridge_sites.last().site } return SourceSite{} } fn (mut m Machine) enter_bridge(vm &VM, tail bool) ! { if isnil(m.function) || isnil(m.function.code) || m.origin == reader.no_node { return } depth := m.frames.len + if tail { 0 } else { 1 } m.reserve_bridge_site(vm, depth)! // Tail replacement must replace provenance at this depth too, keeping a // loop through control procedures bounded in both frame and metadata space. m.release_bridge_sites(depth - 1) m.bridge_sites << BridgeSite{SourceSite{m.function, m.origin}, depth} } @[inline] fn (mut m Machine) reserve_bridge_site(vm &VM, depth int) ! { if m.bridge_sites.len < m.bridge_sites.cap { return } // Replacing this depth discards at least one existing site. Its capacity // already holds the replacement, including a full one-slot tail buffer. if m.bridge_sites.len > 0 && m.bridge_sites.last().depth >= depth { return } m.reserve_buffer(vm, .bridge_sites, m.bridge_sites.len + 1)! } @[inline] fn (mut m Machine) release_bridge_sites(depth int) { for m.bridge_sites.len > 0 && m.bridge_sites.last().depth > depth { index := m.bridge_sites.len - 1 container_store_validated(mut m.bridge_sites, index, BridgeSite{}) // No view of this private container escapes. Clear discarded pointers // before changing its length so spare capacity cannot pin source code. unsafe { m.bridge_sites.len = index } } }