vile / scheme / source.v
195 lines · 185 sloc · 7.34 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5@[inline]
6fn (vm &VM) new_source_program(doc reader.Document, context u64, space &Namespace, path string, discard bool) !&Function {
7 if vm.uses_bounded_operations() {
8 return vm.owner.bounded_files.build_program(vm, doc, context, space, path, discard)
9 }
10 return source_program(vm.new_code(doc, context, space, path), doc.forms, discard)
11}
12
13// The instruction pointer is the resume cursor. A continuation taken in one
14// form therefore resumes the correct remaining forms, including on reentry.
15fn source_program(code &Code, forms []reader.NodeId, discard bool) &Function {
16 // Empty programs need no staged driver or retained expansion scratch.
17 if forms.len == 0 { return compile_program(code, []) }
18 function := &Function{ code: code }
19 mut b := Builder{ code: code, function: function }
20 for i, id in forms {
21 tail := i == forms.len - 1 && !discard
22 b.emit(.source_form, int(id), int(tail), id)
23 if !tail { b.emit(.drop, 0, 0, id) }
24 }
25 if discard { b.constant(unspecified(), reader.no_node) }
26 b.emit(.ret, 0, 0, reader.no_node)
27 // Only compilation borrows the VM name table. A staged driver (including
28 // one retained by a continuation) must not keep the VM through this borrow.
29 b.code.release_compile_borrows()
30 return function
31}
32
33struct SourceImportJob {
34 source reader.NodeId
35 target reader.NodeId
36}
37
38struct SourceImporter {
39mut:
40 parent &Code
41 code &Code
42 indices map[reader.NodeId]reader.NodeId
43 pending []SourceImportJob
44 use_contexts &SyntaxUseImportState = unsafe { nil }
45 share_use_contexts bool
46}
47
48fn (mut importer SourceImporter) reference(source reader.NodeId) !reader.NodeId {
49 // Reader and included-document nodes are immutable and contain no generated links.
50 if source >= reader.no_node { return source }
51 if target := importer.indices[source] { return target }
52 node := importer.parent.generated.nodes[-int(source) - 2]
53 mut target := reader.no_node
54 if node.kind == .symbol {
55 symbol_info := importer.parent.generated.symbols[node.car]
56 target = importer.code.syntax_symbol(symbol_info.name, symbol_info.raw_name, node.origin)!
57 if alias := importer.parent.generated.aliases[symbol_info.name] {
58 importer.code.generated.set_alias(symbol_info.name, alias)
59 importer.import_use_context(symbol_info.name, alias)
60 }
61 } else if node.kind in [.pair, .vector] {
62 target = importer.code.syntax_node(SyntaxNode{ kind: node.kind, renamed: node.renamed, origin: node.origin })!
63 importer.pending << SourceImportJob{source, target}
64 } else {
65 value := if node.kind == .empty_list {
66 nil_value()
67 } else {
68 importer.parent.atom_datum(source)!
69 }
70 target = importer.code.syntax_atom(value, node.origin)!
71 }
72 if !isnil(importer.code.graphs) {
73 importer.code.inherit_graph_identity(importer.parent, source, target)!
74 }
75 if node.kind == .pair {
76 token := importer.parent.record_source_uid(source)!
77 if token.tag == .record_uid {
78 importer.code.keep_record_source_uid(target, token)
79 }
80 }
81 importer.indices[source] = target
82 return target
83}
84
85// Import just the referenced generated subtree, never its driver's entire
86// arena. This keeps a flat datum begin or macro-generated begin linear in size.
87// Binding aliases travel with their identifiers so hygiene survives staging.
88fn (mut importer SourceImporter) import_form(source reader.NodeId) !reader.NodeId {
89 unsafe { importer.pending.flags.set(.noslices) }
90 root := importer.reference(source)!
91 for importer.pending.len > 0 {
92 job := importer.pending.pop()
93 node := importer.parent.generated.nodes[-int(job.source) - 2]
94 mut result := SyntaxNode{ kind: node.kind, renamed: node.renamed, origin: node.origin }
95 if node.kind == .pair {
96 car := importer.reference(node.car)!
97 cdr := importer.reference(node.cdr)!
98 result = SyntaxNode{ ...result, car: car, cdr: cdr }
99 } else {
100 start := importer.code.generated.elements.len
101 count := int(node.cdr)
102 if count > 2147483647 - start {
103 return failure('source vector exceeds supported range')
104 }
105 unsafe { importer.code.generated.elements.grow_len(count) }
106 for i in 0 .. count {
107 child := importer.reference(importer.parent.generated.elements[int(node.car) + i])!
108 importer.code.generated.elements[start + i] = child
109 }
110 result = SyntaxNode{ ...result, car: reader.NodeId(start), cdr: reader.NodeId(count) }
111 }
112 importer.code.generated.nodes[-int(job.target) - 2] = result
113 }
114 return root
115}
116
117@[inline]
118fn (mut m Machine) compile_source_form(mut vm VM, source reader.NodeId) !&Function {
119 // Bootstrap uses its constructor's outer allocation boundary. Later forms
120 // build private compiler state before their Function enters this machine.
121 if vm.uses_bounded_operations() {
122 return vm.owner.bounded_files.compile_form(mut vm, mut m, source)
123 }
124 return m.compile_source_form_inner(mut vm, source)
125}
126
127fn (mut m Machine) compile_source_form_inner(mut vm VM, source reader.NodeId) !&Function {
128 parent := m.function.code
129 mut code := vm.new_code(parent.doc, vm.next_context(), parent.namespace, parent.source_path)
130 code.sources = parent.sources
131 if !isnil(parent.graphs) {
132 code.enable_datum_graphs()
133 code.graphs.staged = parent.graphs.staged
134 code.graphs.literals = parent.source_form_graph_literals(source)
135 }
136 code.record_seed = if parent.record_seed != 0 { parent.record_seed } else { parent.syntax_seed }
137 code.has_static = parent.has_static
138 code.expansions = m.expansions
139 scratch := code.enter_list_scratch()
140 defer { code.leave_list_scratch(scratch) }
141 imported := if source >= reader.no_node {
142 source
143 } else {
144 import_source_form(parent, code, source, true)!
145 }
146 mut probe := Builder{ code: code, function: &Function{ code: code }, definition_context: true }
147 before_expansion := code.expansions
148 id := probe.expand_syntax_head(imported)!
149 // A new transformer expansion has new occurrences. Plain begin staging and
150 // code generation preserve the original reader-backed occurrence identities.
151 if code.expansions != before_expansion { code.record_seed = code.syntax_seed }
152 node := code.node(id)!
153 if node.kind == .pair && code.node(node.car)!.kind == .symbol {
154 name := probe.keyword_name(node.car)!
155 if name in ['include', 'include-ci', 'cond-expand'] {
156 forms := probe.static_forms(name, code.nodes(id)!, id)!
157 code.enter_source_group(id)!
158 m.expansions = code.expansions
159 return source_program(code, forms, false)
160 }
161 }
162 if node.kind == .pair && code.node(node.car)!.kind == .symbol && probe.keyword_name(node.car)! == 'begin' {
163 forms := code.nodes(id)!
164 code.enter_source_group(id)!
165 m.expansions = code.expansions
166 return source_program(code, forms[1..], false)
167 }
168 probe.sequence([id], true)
169 probe.emit(.ret, 0, 0, id)
170 function := probe.finish_program()
171 if !isnil(code.procedural) && !isnil(code.procedural.aborted) {
172 return code.procedural.aborted.error
173 }
174 m.expansions = code.expansions
175 return function
176}
177
178@[noinline]
179fn (mut m Machine) enter_source_form(mut vm VM, source reader.NodeId, tail bool) ! {
180 if !tail && vm.options.max_frames > 0 && m.frames.len >= vm.options.max_frames {
181 return failure('continuation frame limit exceeded')
182 }
183 function := m.compile_source_form(mut vm, source)!
184 if !tail { m.reserve_frame_slot(vm)! }
185 scope := m.checked_activation_for(vm, function, unsafe { nil }, tail)!
186 if !tail {
187 m.frames << CallFrame{ function: m.function, env: m.env, ip: m.ip, base: m.base }
188 m.base = m.stack.len
189 } else {
190 m.truncate(m.base)
191 }
192 m.function = function
193 m.env = scope
194 m.ip = 0
195}
196