vile / scheme / integer_tail_loop_admission_test.v
97 lines · 91 sloc · 4.5 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn integer_tail_test_machine(mut vm VM, source string, a i64, b i64) !&Machine {
6 vm.eval(source)!
7 callable := vm.lookup('f')!
8 vm.call(callable, [integer(a),integer(b)])! // Resolve original global loads.
9 p := callable.procedure_pointer()
10 assert !isnil(p.function.integer_tail), 'params=${p.function.params} slots=${p.function.slot_count} rest=${p.function.rest} star=${!isnil(p.function.star)} next=${p.function.goops_next_slot}/${p.function.goops_needs_next} owner=${p.function.code.allocation_owner == v_arena_current()} instructions=${p.function.instructions}'
11 return &Machine{function:p.function, env:&Environment{parent:p.env,slots:[integer(a),integer(b)]},
12 ip:0, steps:0, origin:p.function.instructions[0].origin}
13}
14
15fn test_integer_tail_plan_admits_both_polarities_and_aliases() {
16 for source in [
17 '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))',
18 '(define(f n a)(if (> n 0)(f (- n 1)(+ a n)) a))',
19 '(define test =)(define next -)(define combine +)(define(f n a)(if(test n 0) a (f(next n 1)(combine a n))))',
20 ] {
21 mut vm := new_with_options(Options{max_steps:0,max_frames:0})!
22 mut machine := integer_tail_test_machine(mut vm, source, 10, 0)!
23 assert machine.fuse_integer_tail_loop(vm)
24 assert machine.ip == 0 && machine.steps == 190
25 assert machine.env.slots[0].whole() == 0 && machine.env.slots[1].whole() == 55
26 assert machine.stack.len == 0 && machine.frames.len == 0
27 assert machine.origin == machine.function.instructions[machine.function.integer_tail.recurrence + 9].origin
28 assert machine.run(mut vm)!.whole() == 55
29 }
30}
31
32fn test_integer_tail_plan_captured_bound_and_borrowed_call_storage() {
33 mut vm := new_with_options(Options{max_steps:0,max_frames:0})!
34 mut machine := integer_tail_test_machine(mut vm,
35 '(define(make bound)(letrec((loop(lambda(n a)(if (>= n bound) a (loop (+ n 1)(+ a n)))))) loop))(define f(make 10))',0,0)!
36 owner := machine.function.code.allocation_owner
37 plan := machine.function.integer_tail
38 mut borrowed := arena.new()
39 borrowed.push()
40 before := borrowed.used()
41 assert machine.fuse_integer_tail_loop(vm)
42 assert borrowed.used() == before
43 assert voidptr(machine.function.integer_tail) == voidptr(plan)
44 assert machine.function.code.allocation_owner == owner
45 borrowed.pop()
46 borrowed.free()
47 gc_collect()
48 assert machine.run(mut vm)!.whole() == 45
49 assert vm.call(vm.lookup('f')!,[integer(0),integer(0)])!.whole() == 45
50}
51
52fn test_integer_tail_plan_declines_escaped_scopes_and_different_self_environments() {
53 mut vm := new_with_options(Options{max_steps:0,max_frames:0})!
54 mut machine := integer_tail_test_machine(mut vm,
55 '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))', 3,0)!
56 for flag in 0 .. 4 {
57 machine.env.escaped = flag == 0
58 machine.env.tail_parent = flag == 1
59 if flag == 2 { machine.push(integer(42)) }
60 if flag == 3 {
61 original := vm.lookup('f')!.procedure_pointer()
62 vm.define('f',procedure_value(&Procedure{mode:.closure, owner:vm.owner,
63 function:original.function,env:&Environment{}}))!
64 }
65 assert !machine.fuse_integer_tail_loop(vm)
66 assert machine.steps == 0 && machine.ip == 0
67 assert machine.env.slots[0].whole() == 3 && machine.env.slots[1].whole() == 0
68 if flag == 2 { assert machine.pop_value().whole() == 42 }
69 }
70}
71
72fn test_integer_tail_plan_partial_budget_and_second_operation_overflow() {
73 for limit in [18,19,37,38,39] {
74 mut vm := new_with_options(Options{max_steps:0,max_frames:0})!
75 mut machine := integer_tail_test_machine(mut vm,
76 '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))', 3,0)!
77 vm.options = Options{max_steps:limit,max_frames:0}
78 advanced := machine.fuse_integer_tail_loop(vm)
79 completed := if limit < 19 { 0 } else if limit < 38 { 1 } else { 2 }
80 assert advanced == (completed > 0)
81 assert machine.steps == completed * 19
82 assert machine.ip == 0
83 assert machine.env.slots[0].whole() == 3 - completed
84 assert machine.env.slots[1].whole() == if completed == 0 { 0 } else if completed == 1 { 3 } else { 5 }
85 }
86 mut vm := new_with_options(Options{max_steps:0,max_frames:0})!
87 mut machine := integer_tail_test_machine(mut vm,
88 '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))', 2,0)!
89 machine.env.slots[1] = integer(exact_max)
90 assert !machine.fuse_integer_tail_loop(vm)
91 assert machine.steps == 0 && machine.ip == 0
92 assert machine.env.slots[0].whole() == 2 && machine.env.slots[1].whole() == exact_max
93 machine.env.slots[1] = integer(exact_max - 2)
94 assert machine.fuse_integer_tail_loop(vm)
95 assert machine.steps == 19 && machine.ip == 0
96 assert machine.env.slots[0].whole() == 1 && machine.env.slots[1].whole() == exact_max
97}
98