module scheme // Compiler-owned binding scopes are immediate calls with fixed arguments. // Keeping a Function handle on the value stack avoids a temporary Procedure, // and does not mark its parent escaped. Actual closures/promises and call/cc // mark their entire lexical paths before anything can escape or be retained. @[direct_array_access] fn (mut m Machine) call_scope(mut vm VM, argc int, tail bool) ! { m.charge(vm.options.max_steps)! if !tail && vm.options.max_frames > 0 && m.frames.len >= vm.options.max_frames { return failure('continuation frame limit exceeded') } base := m.stack.len - argc - 1 function := unsafe { &Function(m.stack[base].pointer()) } // Preserve zero-argument failure cleanup; arguments remain rooted until // their destination activation has been reserved and initialized. if argc == 0 { m.pop_value() } parent := if function.capture_depth > 0 { m.env } else { unsafe { &Environment(nil) } } // A tail helper still needs its caller as a lexical parent. It must never // overwrite that same object while installing its own activation. if !tail { m.reserve_frame_slot(vm)! } mut scope := m.checked_activation_for(vm, function, parent, tail && isnil(parent))! for i in 0 .. argc { scope.slots[i] = m.stack[base + 1 + i] } if !isnil(scope) { scope.tail_parent = tail && !isnil(parent) } if !tail { m.frames << CallFrame{ function: m.function, env: m.env, ip: m.ip, base: m.base } m.base = base } m.truncate(if tail { m.base } else { base }) m.function = function m.env = scope m.ip = 0 } @[direct_array_access] fn (mut m Machine) release_loop_scopes(iteration &Environment) { mut cursor := EnvironmentRef{m.env} for voidptr(cursor.value) != voidptr(iteration) { mut scope := cursor.value next := scope.parent m.cache_environment(mut scope) cursor = EnvironmentRef{next} } } // A tail helper temporarily needs its abandoned caller as a lexical parent. // Once a real tail call/return leaves the helper chain, those unescaped parents // are dead too. A false edge marks a live non-tail caller and stops traversal. @[direct_array_access; noinline] fn (mut m Machine) release_tail_parents(keep &Environment) { if isnil(m.env) || !m.env.tail_parent { return } mut cursor := EnvironmentRef{m.env.parent} m.env.tail_parent = false for !isnil(cursor.value) && voidptr(cursor.value) != voidptr(keep) { mut scope := cursor.value if scope.escaped { break } next := scope.parent more := scope.tail_parent m.cache_environment(mut scope) if !more { break } cursor = EnvironmentRef{next} } } @[direct_array_access; inline] fn (mut m Machine) cache_environment(mut available Environment) { if available.escaped || m.cached >= 32 { return } count := available.slots.len for i in 0 .. count { container_store_validated(mut available.slots, i, Value{}) } available.tail_parent = false if count < m.spares.len { available.parent = m.spares[count] m.spares[count] = &available } else { available.parent = m.wide_spares m.wide_spares = &available } m.cached++ } fn (mut m Machine) take_wide_environment(count int, excluded &Environment) &Environment { mut previous := unsafe { &Environment(nil) } mut cursor := m.wide_spares for !isnil(cursor) { if (count < 0 || cursor.slots.len == count) && voidptr(cursor) != voidptr(excluded) { if isnil(previous) { m.wide_spares = cursor.parent } else { previous.parent = cursor.parent } m.cached-- return cursor } previous = cursor cursor = cursor.parent } return unsafe { nil } } fn (mut m Machine) take_other_environment(excluded &Environment) &Environment { for count in 0 .. m.spares.len { mut available := m.spares[count] if isnil(available) { continue } if voidptr(available) == voidptr(excluded) { if isnil(available.parent) { continue } available = available.parent m.spares[count].parent = available.parent } else { m.spares[count] = available.parent } m.cached-- return available } return m.take_wide_environment(-1, excluded) }