vile / scheme / control.v
149 lines · 128 sloc · 5.65 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5// These instruction-only programs never contain a VM, environment or mutable
6// Scheme value. Static storage avoids publishing a caller arena's allocation
7// when a bridge is first used by an older VM or captured continuation.
8const return_bridge_function_code = [
9 Instruction{ op: .ret, origin: reader.no_node }
10]!
11
12const return_bridge_function = Function{
13 code: unsafe { nil }
14 instructions: unsafe { static_bridge_instructions(&return_bridge_function_code[0], return_bridge_function_code.len) }
15}
16
17const process_exit_bridge_function_code = [
18 Instruction{ op: .process_exit_finish, origin: reader.no_node }
19]!
20
21const process_exit_bridge_function = Function{
22 code: unsafe { nil }
23 instructions: unsafe { static_bridge_instructions(&process_exit_bridge_function_code[0], process_exit_bridge_function_code.len) }
24}
25
26const wind_transfer_bridge_function_code = [
27 Instruction{ op: .wind_step, origin: reader.no_node }
28 Instruction{ op: .wind_resume, origin: reader.no_node }
29 Instruction{ op: .jump, origin: reader.no_node }
30]!
31
32const wind_transfer_bridge_function = Function{
33 code: unsafe { nil }
34 slot_count: 3
35 instructions: unsafe { static_bridge_instructions(&wind_transfer_bridge_function_code[0], wind_transfer_bridge_function_code.len) }
36}
37
38const member_comparison_bridge_code = [
39 Instruction{ op: .search_init, origin: reader.no_node }
40 Instruction{ op: .search_step, origin: reader.no_node }
41 Instruction{ op: .search_collect, origin: reader.no_node }
42 Instruction{ op: .jump, a: 1, origin: reader.no_node }
43]!
44
45const member_comparison_bridge = Function{
46 code: unsafe { nil }
47 params: 3
48 slot_count: 6
49 instructions: unsafe { static_bridge_instructions(&member_comparison_bridge_code[0], member_comparison_bridge_code.len) }
50}
51
52const assoc_comparison_bridge_code = [
53 Instruction{ op: .search_init, a: 1, origin: reader.no_node }
54 Instruction{ op: .search_step, a: 1, origin: reader.no_node }
55 Instruction{ op: .search_collect, a: 1, origin: reader.no_node }
56 Instruction{ op: .jump, a: 1, origin: reader.no_node }
57]!
58
59const assoc_comparison_bridge = Function{
60 code: unsafe { nil }
61 params: 3
62 slot_count: 6
63 instructions: unsafe { static_bridge_instructions(&assoc_comparison_bridge_code[0], assoc_comparison_bridge_code.len) }
64}
65
66// Instruction-only protocol shared by all VMs. It contains no owner or values.
67const call_values_bridge_function_code = [
68 Instruction{op: .local, a: 0, b: 1, origin: reader.no_node}
69 Instruction{op: .local, a: 0, b: 0, origin: reader.no_node}
70 Instruction{op: .call, a: 0, b: 0, origin: reader.no_node}
71 Instruction{op: .call_values_tail, a: 0, b: 0, origin: reader.no_node}
72 Instruction{op: .ret, a: 0, b: 0, origin: reader.no_node}
73]!
74
75const call_values_bridge_function = Function{
76 code: unsafe { nil }
77 params: 2
78 slot_count: 2
79 instructions: unsafe { static_bridge_instructions(&call_values_bridge_function_code[0], call_values_bridge_function_code.len) }
80}
81
82struct SourceSite {
83 function &Function = unsafe { nil }
84 origin reader.NodeId = reader.no_node
85}
86
87struct BridgeSite {
88 site SourceSite
89 depth int
90}
91
92fn (mut vm VM) install_control_primitives() {
93 vm.install_list_utility_primitives()
94 vm.install_sort_primitives()
95 vm.install_iteration_primitives()
96 vm.install_indexed_iteration_primitives()
97 vm.install_promise_primitive()
98 vm.install_wind_primitive()
99 vm.install_parameter_primitive()
100 call_cc := procedure_value(&Procedure{ mode: .call_cc, name: 'call-with-current-continuation', owner: vm.owner })
101 vm.bind_global('call-with-current-continuation', call_cc)
102 vm.bind_global('call/cc', call_cc)
103 vm.bind_global('values', procedure_value(&Procedure{ mode: .values, name: 'values', owner: vm.owner }))
104 // Both Scheme callbacks run on this same machine. The producer returns to
105 // this private bridge; its consumer application is a proper tail call.
106 vm.bind_global('call-with-values', procedure_value(&Procedure{ mode: .closure, name: 'call-with-values', owner: vm.owner, function: &call_values_bridge_function }))
107}
108
109fn bridge_instruction(op Op, a int, b int) Instruction {
110 return Instruction{op, i32(a), i32(b), reader.no_node}
111}
112
113fn (m &Machine) error_site() SourceSite {
114 if !isnil(m.function) && !isnil(m.function.code) && m.origin != reader.no_node {
115 return SourceSite{m.function, m.origin}
116 }
117 if m.bridge_sites.len > 0 { return m.bridge_sites.last().site }
118 return SourceSite{}
119}
120
121fn (mut m Machine) enter_bridge(vm &VM, tail bool) ! {
122 if isnil(m.function) || isnil(m.function.code) || m.origin == reader.no_node { return }
123 depth := m.frames.len + if tail { 0 } else { 1 }
124 m.reserve_bridge_site(vm, depth)!
125 // Tail replacement must replace provenance at this depth too, keeping a
126 // loop through control procedures bounded in both frame and metadata space.
127 m.release_bridge_sites(depth - 1)
128 m.bridge_sites << BridgeSite{SourceSite{m.function, m.origin}, depth}
129}
130
131@[inline]
132fn (mut m Machine) reserve_bridge_site(vm &VM, depth int) ! {
133 if m.bridge_sites.len < m.bridge_sites.cap { return }
134 // Replacing this depth discards at least one existing site. Its capacity
135 // already holds the replacement, including a full one-slot tail buffer.
136 if m.bridge_sites.len > 0 && m.bridge_sites.last().depth >= depth { return }
137 m.reserve_buffer(vm, .bridge_sites, m.bridge_sites.len + 1)!
138}
139
140@[inline]
141fn (mut m Machine) release_bridge_sites(depth int) {
142 for m.bridge_sites.len > 0 && m.bridge_sites.last().depth > depth {
143 index := m.bridge_sites.len - 1
144 container_store_validated(mut m.bridge_sites, index, BridgeSite{})
145 // No view of this private container escapes. Clear discarded pointers
146 // before changing its length so spare capacity cannot pin source code.
147 unsafe { m.bridge_sites.len = index }
148 }
149}
150