vile / scheme / tail_width_recycling_prod_test.v
84 lines · 80 sloc · 3.21 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn test_mutual_tail_calls_with_different_local_widths_keep_bounded_storage() {
6 for variant in 0 .. 4 {
7 alternate := variant == 1 || variant == 3
8 mut region := arena.new()
9 region.push()
10 mut vm := new_with_options(Options{ max_steps: 0, max_frames: 2 }) or { panic(err) }
11 if variant == 2 {
12 vm.eval('(define (width-a n)
13 (if (= n 0) 42 (width-b (- n 1) 0 0 0 0 0 0 0 0 0 0 0)))
14 (define (width-b n a b c d e f g h i j k) (width-a n))') or { panic(err) }
15 } else if variant == 3 {
16 vm.eval_alt('width-a(n):={if n==0 {42} else {
17 a:=n\nb:=a\nc:=b\nd:=c\ne:=d\nf:=e\ng:=f\nh:=g\ni:=h\nj:=i
18 width-b(j - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)}}
19 width-b(n, a, b, c, d, e, f, g, h, i, j, k):={width-a(n)}') or { panic(err) }
20 } else if alternate {
21 vm.eval_alt('width-a(n):={if n==0 {42} else {width-b(n - 1, 0)}}
22 width-b(n, a):={width-c(n, a, 0)}
23 width-c(n, a, b):={width-a(n)}') or { panic(err) }
24 } else {
25 vm.eval('(define (width-a n) (if (= n 0) 42 (width-b (- n 1) 0)))
26 (define (width-b n a) (width-c n a 0))
27 (define (width-c n a b) (width-a n))') or { panic(err) }
28 }
29 proc := vm.lookup('width-a') or { panic(err) }
30 vm.call(proc, [integer(100)]) or { panic(err) }
31 before := region.used()
32 value := vm.call(proc, [integer(50000)]) or { panic(err) }
33 delta := region.used() - before
34 region.pop()
35 region.free()
36 if value.whole() != 42 || delta > 65536 {
37 panic('mutual tail calls allocated ${delta} bytes, variant=${variant}')
38 }
39 }
40}
41
42fn test_tail_activation_resizes_past_small_scope_cache_and_clears_discarded_values() {
43 mut slots := value_storage(16)
44 for i in 0 .. slots.len { container_store_validated(mut slots, i, integer(i + 1)) }
45 original := &Environment{ slots: slots }
46 mut machine := Machine{ env: original }
47 small := machine.activation_for(&Function{ code: &Code{}, params: 2, slot_count: 3 }, unsafe { nil }, true)
48 if voidptr(small) != voidptr(original) || small.slots.len != 3 {
49 panic('tail activation did not reuse different-width storage')
50 }
51 for i in 3 .. 16 {
52 if unsafe { (&Value(small.slots.data))[i] }.tag != .unspecified {
53 panic('tail activation retained a discarded slot')
54 }
55 }
56 large := machine.activation_for(&Function{ code: &Code{}, params: 2, slot_count: 12 }, unsafe { nil }, true)
57 if voidptr(large) != voidptr(original) || large.slots.len != 12 {
58 panic('tail activation failed beyond the small scope cache')
59 }
60 for i in 2 .. 12 {
61 if large.slots[i].tag != .uninitialized {
62 panic('tail activation kept stale internal slots')
63 }
64 }
65}
66
67fn test_different_width_tail_calls_preserve_escaped_bindings_and_continuation_replay() {
68 mut vm := new_with_options(Options{ max_steps: 0 }) or { panic(err) }
69 first := vm.eval('(define saved #f) (define resume #f)
70 (define (wide n unused)
71 (if (= n 0) (saved)
72 (begin (if (= n 2)
73 (begin (set! saved (lambda () n))
74 (call/cc (lambda (k) (set! resume k) 0))))
75 (narrow (- n 1)))))
76 (define (narrow n) (wide n 0))
77 (wide 100 0)') or { panic(err) }
78 if first.whole() != 2 { panic('lost escaped tail binding') }
79 resume := vm.lookup('resume') or { panic(err) }
80 for _ in 0 .. 3 {
81 value := vm.call(resume, [integer(0)]) or { panic(err) }
82 if value.whole() != 2 { panic('tail activation corrupted continuation replay') }
83 }
84}
85