vile / scheme / machine_closure.v
45 lines · 44 sloc · 1.95 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// Enter an ordinary fixed-arity closure through its existing activation API.
4// Frame growth, control protocols and rejected calls retain the complete
5// dispatcher. The activation API owns reuse, allocation and lifetime rules.
6// Specialize by caller so sharing the source does not make the C optimizer
7// outline the hot dispatch entry when another bridge starts using it.
8@[direct_array_access; inline]
9fn (mut m Machine) enter_cached_closure[T](vm &VM, argc int, tail bool) bool {
10 $if vile_native ? {
11 // The optional backend must see this call before any VM shortcut.
12 if !isnil(vm.native_execution_scope) { return false }
13 }
14 base := m.stack.len - argc - 1
15 callable := m.stack[base]
16 if callable.tag != .procedure { return false }
17 p := callable.procedure_pointer()
18 if p.mode != .closure || voidptr(p.owner) != voidptr(vm.owner) { return false }
19 function := p.function
20 if isnil(function.code) || function.rest || !isnil(function.star)
21 || function.goops_next_slot >= 0 || function.goops_needs_next || argc != function.params {
22 return false
23 }
24 if vm.options.max_steps > 0 && m.steps >= vm.options.max_steps { return false }
25 if m.goops_pending_next.tag != .unspecified { return false }
26 if !tail && ((vm.options.max_frames > 0 && m.frames.len >= vm.options.max_frames)
27 || m.frames.len == m.frames.cap) { return false }
28 m.steps++
29 mut scope := m.activation_for(function, p.env, tail)
30 for i in 0 .. function.params { scope.slots[i] = m.stack[base + 1 + i] }
31 if !tail {
32 m.append_reserved_frame(CallFrame{function: m.function, env: m.env, ip: m.ip, base: m.base})
33 m.base = base
34 }
35 m.truncate(if tail { m.base } else { base })
36 m.function = function
37 m.env = scope
38 m.ip = 0
39 $if T is PlainMachineLoop {
40 // The ordinary closure transition already changes the machine registers.
41 // Keep complete numeric-loop execution here, outside global-load dispatch.
42 if argc == 2 && !isnil(function.integer_tail) { _ = m.fuse_integer_tail_loop(vm) }
43 }
44 return true
45}
46