vile / scheme / native_loops_d_vile_native.c.v
97 lines · 91 sloc · 4.01 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3struct NativeBindingLoop {
4 function &Function
5 base int
6 entry int
7 barrier int
8}
9
10// Recognize the compiler's complete named-let wrapper, including the clearing
11// of unnamed initializer parameters. Other recursive closure stores remain
12// outside the nonescaping-helper proof.
13fn native_named_wrapper(f &Function) bool {
14 n := f.params
15 if n < 0 || n > 8 || f.rest || !isnil(f.star) || f.goops_next_slot >= 0
16 || f.slot_count != n + 1 || f.children.len != 1 || f.instructions.len != 6 + 4 * n {
17 return false
18 }
19 x := f.instructions
20 if x[0].op != .closure || x[0].a != 0 || x[1].op != .define_local || x[1].a != 0 || x[1].b != n
21 || x[2].op != .drop || x[3].op != .local || x[3].a != 0 || x[3].b != n { return false }
22 for i in 0 .. n {
23 if x[4 + i].op != .local || x[4 + i].a != 0 || x[4 + i].b != i { return false }
24 pc := 4 + n + 3 * i
25 if x[pc].op != .constant || x[pc].a < 0 || x[pc].a >= f.constants.len
26 || f.constants[int(x[pc].a)].tag != .unspecified || x[pc + 1].op != .define_local
27 || x[pc + 1].a != 0 || x[pc + 1].b != i || x[pc + 2].op != .drop { return false }
28 }
29 return x[4 + 4 * n].op == .tail_call && x[4 + 4 * n].a == n && x[5 + 4 * n].op == .ret
30}
31
32fn (mut b NativeBindingLowering) prepare(f &Function) !&Function {
33 if prepared := b.prepared[u64(f)] { return prepared }
34 if !b.allow_named_loops || !native_named_wrapper(f) {
35 b.prepared[u64(f)] = f
36 return f
37 }
38 child := f.children[0]
39 if child.params != f.params || child.rest || !isnil(child.star) || child.goops_next_slot >= 0
40 || child.slot_count > 64 || voidptr(child.code) != voidptr(f.code) { return error('unsupported named loop') }
41 mut loop := &Function{code: child.code, params: child.params, slot_count: child.slot_count}
42 b.loop_roots[u64(loop)] = true
43 b.prepared[u64(loop)] = loop
44 b.prepared[u64(f)] = loop
45 b.transform_loop(child, mut loop, loop, f.params, 0)!
46 return loop
47}
48
49// Remove one private wrapper environment. Its only visible cell is the loop
50// procedure itself; represent reads as private constructor tokens. Descendant
51// helper depths change at that same lexical boundary, not at their own frame.
52fn (mut b NativeBindingLowering) transform_loop(source &Function, mut out Function, target &Function, self_slot int, level int) ! {
53 if level > 64 || voidptr(source.code) != voidptr(target.code) { return error('named loop nesting') }
54 b.transformed += source.instructions.len
55 if b.transformed > 4096 { return error('named loop tree too large') }
56 out.constants = source.constants
57 out.names = source.names
58 out.global_slots = source.global_slots
59 out.star = source.star
60 out.goops_next_slot = source.goops_next_slot
61 for child in source.children {
62 mut lowered := &Function{code: child.code, params: child.params, rest: child.rest, slot_count: child.slot_count}
63 b.transform_loop(child, mut lowered, target, self_slot, level + 1)!
64 out.children << lowered
65 }
66 for original in source.instructions {
67 mut ins := original
68 if ins.op in [.local, .assign_local, .define_local] {
69 if ins.a == level + 1 {
70 if ins.op != .local || ins.b != self_slot { return error('named loop binding escapes or changes') }
71 index := out.children.len
72 out.children << target
73 ins = Instruction{...ins, op: .scope_function, a: i32(index), b: 0}
74 } else if ins.a > level + 1 {
75 ins = Instruction{...ins, a: ins.a - 1}
76 }
77 }
78 out.instructions << ins
79 }
80}
81
82fn (mut b NativeBindingLowering) move_arguments(ins Instruction, count int, base int) ! {
83 for i := count - 1; i >= 0; i-- {
84 _ := b.append(Instruction{...ins, op: .define_local, a: 0, b: i32(base + i)})!
85 _ := b.append(Instruction{...ins, op: .drop, a: 0, b: 0})!
86 }
87 _ := b.append(Instruction{...ins, op: .drop, a: 0, b: 0})!
88}
89
90fn (mut b NativeBindingLowering) reset_locals(f &Function, base int, uninit int) ! {
91 origin := f.instructions[0].origin
92 for i in f.params .. f.slot_count {
93 _ := b.append(Instruction{op: .constant, a: i32(uninit), origin: origin})!
94 _ := b.append(Instruction{op: .define_local, b: i32(base + i), origin: origin})!
95 _ := b.append(Instruction{op: .drop, origin: origin})!
96 }
97}
98