module scheme struct NativeBindingLoop { function &Function base int entry int barrier int } // Recognize the compiler's complete named-let wrapper, including the clearing // of unnamed initializer parameters. Other recursive closure stores remain // outside the nonescaping-helper proof. fn native_named_wrapper(f &Function) bool { n := f.params if n < 0 || n > 8 || f.rest || !isnil(f.star) || f.goops_next_slot >= 0 || f.slot_count != n + 1 || f.children.len != 1 || f.instructions.len != 6 + 4 * n { return false } x := f.instructions if x[0].op != .closure || x[0].a != 0 || x[1].op != .define_local || x[1].a != 0 || x[1].b != n || x[2].op != .drop || x[3].op != .local || x[3].a != 0 || x[3].b != n { return false } for i in 0 .. n { if x[4 + i].op != .local || x[4 + i].a != 0 || x[4 + i].b != i { return false } pc := 4 + n + 3 * i if x[pc].op != .constant || x[pc].a < 0 || x[pc].a >= f.constants.len || f.constants[int(x[pc].a)].tag != .unspecified || x[pc + 1].op != .define_local || x[pc + 1].a != 0 || x[pc + 1].b != i || x[pc + 2].op != .drop { return false } } return x[4 + 4 * n].op == .tail_call && x[4 + 4 * n].a == n && x[5 + 4 * n].op == .ret } fn (mut b NativeBindingLowering) prepare(f &Function) !&Function { if prepared := b.prepared[u64(f)] { return prepared } if !b.allow_named_loops || !native_named_wrapper(f) { b.prepared[u64(f)] = f return f } child := f.children[0] if child.params != f.params || child.rest || !isnil(child.star) || child.goops_next_slot >= 0 || child.slot_count > 64 || voidptr(child.code) != voidptr(f.code) { return error('unsupported named loop') } mut loop := &Function{code: child.code, params: child.params, slot_count: child.slot_count} b.loop_roots[u64(loop)] = true b.prepared[u64(loop)] = loop b.prepared[u64(f)] = loop b.transform_loop(child, mut loop, loop, f.params, 0)! return loop } // Remove one private wrapper environment. Its only visible cell is the loop // procedure itself; represent reads as private constructor tokens. Descendant // helper depths change at that same lexical boundary, not at their own frame. fn (mut b NativeBindingLowering) transform_loop(source &Function, mut out Function, target &Function, self_slot int, level int) ! { if level > 64 || voidptr(source.code) != voidptr(target.code) { return error('named loop nesting') } b.transformed += source.instructions.len if b.transformed > 4096 { return error('named loop tree too large') } out.constants = source.constants out.names = source.names out.global_slots = source.global_slots out.star = source.star out.goops_next_slot = source.goops_next_slot for child in source.children { mut lowered := &Function{code: child.code, params: child.params, rest: child.rest, slot_count: child.slot_count} b.transform_loop(child, mut lowered, target, self_slot, level + 1)! out.children << lowered } for original in source.instructions { mut ins := original if ins.op in [.local, .assign_local, .define_local] { if ins.a == level + 1 { if ins.op != .local || ins.b != self_slot { return error('named loop binding escapes or changes') } index := out.children.len out.children << target ins = Instruction{...ins, op: .scope_function, a: i32(index), b: 0} } else if ins.a > level + 1 { ins = Instruction{...ins, a: ins.a - 1} } } out.instructions << ins } } fn (mut b NativeBindingLowering) move_arguments(ins Instruction, count int, base int) ! { for i := count - 1; i >= 0; i-- { _ := b.append(Instruction{...ins, op: .define_local, a: 0, b: i32(base + i)})! _ := b.append(Instruction{...ins, op: .drop, a: 0, b: 0})! } _ := b.append(Instruction{...ins, op: .drop, a: 0, b: 0})! } fn (mut b NativeBindingLowering) reset_locals(f &Function, base int, uninit int) ! { origin := f.instructions[0].origin for i in f.params .. f.slot_count { _ := b.append(Instruction{op: .constant, a: i32(uninit), origin: origin})! _ := b.append(Instruction{op: .define_local, b: i32(base + i), origin: origin})! _ := b.append(Instruction{op: .drop, origin: origin})! } }