vile / scheme / scope_call.v
115 lines · 109 sloc · 3.94 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// Compiler-owned binding scopes are immediate calls with fixed arguments.
4// Keeping a Function handle on the value stack avoids a temporary Procedure,
5// and does not mark its parent escaped. Actual closures/promises and call/cc
6// mark their entire lexical paths before anything can escape or be retained.
7@[direct_array_access]
8fn (mut m Machine) call_scope(mut vm VM, argc int, tail bool) ! {
9 m.charge(vm.options.max_steps)!
10 if !tail && vm.options.max_frames > 0 && m.frames.len >= vm.options.max_frames {
11 return failure('continuation frame limit exceeded')
12 }
13 base := m.stack.len - argc - 1
14 function := unsafe { &Function(m.stack[base].pointer()) }
15 // Preserve zero-argument failure cleanup; arguments remain rooted until
16 // their destination activation has been reserved and initialized.
17 if argc == 0 { m.pop_value() }
18 parent := if function.capture_depth > 0 { m.env } else { unsafe { &Environment(nil) } }
19 // A tail helper still needs its caller as a lexical parent. It must never
20 // overwrite that same object while installing its own activation.
21 if !tail { m.reserve_frame_slot(vm)! }
22 mut scope := m.checked_activation_for(vm, function, parent, tail && isnil(parent))!
23 for i in 0 .. argc { scope.slots[i] = m.stack[base + 1 + i] }
24 if !isnil(scope) { scope.tail_parent = tail && !isnil(parent) }
25 if !tail {
26 m.frames << CallFrame{ function: m.function, env: m.env, ip: m.ip, base: m.base }
27 m.base = base
28 }
29 m.truncate(if tail { m.base } else { base })
30 m.function = function
31 m.env = scope
32 m.ip = 0
33}
34
35@[direct_array_access]
36fn (mut m Machine) release_loop_scopes(iteration &Environment) {
37 mut cursor := EnvironmentRef{m.env}
38 for voidptr(cursor.value) != voidptr(iteration) {
39 mut scope := cursor.value
40 next := scope.parent
41 m.cache_environment(mut scope)
42 cursor = EnvironmentRef{next}
43 }
44}
45
46// A tail helper temporarily needs its abandoned caller as a lexical parent.
47// Once a real tail call/return leaves the helper chain, those unescaped parents
48// are dead too. A false edge marks a live non-tail caller and stops traversal.
49@[direct_array_access; noinline]
50fn (mut m Machine) release_tail_parents(keep &Environment) {
51 if isnil(m.env) || !m.env.tail_parent { return }
52 mut cursor := EnvironmentRef{m.env.parent}
53 m.env.tail_parent = false
54 for !isnil(cursor.value) && voidptr(cursor.value) != voidptr(keep) {
55 mut scope := cursor.value
56 if scope.escaped { break }
57 next := scope.parent
58 more := scope.tail_parent
59 m.cache_environment(mut scope)
60 if !more { break }
61 cursor = EnvironmentRef{next}
62 }
63}
64
65@[direct_array_access; inline]
66fn (mut m Machine) cache_environment(mut available Environment) {
67 if available.escaped || m.cached >= 32 { return }
68 count := available.slots.len
69 for i in 0 .. count { container_store_validated(mut available.slots, i, Value{}) }
70 available.tail_parent = false
71 if count < m.spares.len {
72 available.parent = m.spares[count]
73 m.spares[count] = &available
74 } else {
75 available.parent = m.wide_spares
76 m.wide_spares = &available
77 }
78 m.cached++
79}
80
81fn (mut m Machine) take_wide_environment(count int, excluded &Environment) &Environment {
82 mut previous := unsafe { &Environment(nil) }
83 mut cursor := m.wide_spares
84 for !isnil(cursor) {
85 if (count < 0 || cursor.slots.len == count) && voidptr(cursor) != voidptr(excluded) {
86 if isnil(previous) {
87 m.wide_spares = cursor.parent
88 } else {
89 previous.parent = cursor.parent
90 }
91 m.cached--
92 return cursor
93 }
94 previous = cursor
95 cursor = cursor.parent
96 }
97 return unsafe { nil }
98}
99
100fn (mut m Machine) take_other_environment(excluded &Environment) &Environment {
101 for count in 0 .. m.spares.len {
102 mut available := m.spares[count]
103 if isnil(available) { continue }
104 if voidptr(available) == voidptr(excluded) {
105 if isnil(available.parent) { continue }
106 available = available.parent
107 m.spares[count].parent = available.parent
108 } else {
109 m.spares[count] = available.parent
110 }
111 m.cached--
112 return available
113 }
114 return m.take_wide_environment(-1, excluded)
115}
116