module scheme import arena fn test_mutual_tail_calls_with_different_local_widths_keep_bounded_storage() { for variant in 0 .. 4 { alternate := variant == 1 || variant == 3 mut region := arena.new() region.push() mut vm := new_with_options(Options{ max_steps: 0, max_frames: 2 }) or { panic(err) } if variant == 2 { vm.eval('(define (width-a n) (if (= n 0) 42 (width-b (- n 1) 0 0 0 0 0 0 0 0 0 0 0))) (define (width-b n a b c d e f g h i j k) (width-a n))') or { panic(err) } } else if variant == 3 { vm.eval_alt('width-a(n):={if n==0 {42} else { a:=n\nb:=a\nc:=b\nd:=c\ne:=d\nf:=e\ng:=f\nh:=g\ni:=h\nj:=i width-b(j - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)}} width-b(n, a, b, c, d, e, f, g, h, i, j, k):={width-a(n)}') or { panic(err) } } else if alternate { vm.eval_alt('width-a(n):={if n==0 {42} else {width-b(n - 1, 0)}} width-b(n, a):={width-c(n, a, 0)} width-c(n, a, b):={width-a(n)}') or { panic(err) } } else { vm.eval('(define (width-a n) (if (= n 0) 42 (width-b (- n 1) 0))) (define (width-b n a) (width-c n a 0)) (define (width-c n a b) (width-a n))') or { panic(err) } } proc := vm.lookup('width-a') or { panic(err) } vm.call(proc, [integer(100)]) or { panic(err) } before := region.used() value := vm.call(proc, [integer(50000)]) or { panic(err) } delta := region.used() - before region.pop() region.free() if value.whole() != 42 || delta > 65536 { panic('mutual tail calls allocated ${delta} bytes, variant=${variant}') } } } fn test_tail_activation_resizes_past_small_scope_cache_and_clears_discarded_values() { mut slots := value_storage(16) for i in 0 .. slots.len { container_store_validated(mut slots, i, integer(i + 1)) } original := &Environment{ slots: slots } mut machine := Machine{ env: original } small := machine.activation_for(&Function{ code: &Code{}, params: 2, slot_count: 3 }, unsafe { nil }, true) if voidptr(small) != voidptr(original) || small.slots.len != 3 { panic('tail activation did not reuse different-width storage') } for i in 3 .. 16 { if unsafe { (&Value(small.slots.data))[i] }.tag != .unspecified { panic('tail activation retained a discarded slot') } } large := machine.activation_for(&Function{ code: &Code{}, params: 2, slot_count: 12 }, unsafe { nil }, true) if voidptr(large) != voidptr(original) || large.slots.len != 12 { panic('tail activation failed beyond the small scope cache') } for i in 2 .. 12 { if large.slots[i].tag != .uninitialized { panic('tail activation kept stale internal slots') } } } fn test_different_width_tail_calls_preserve_escaped_bindings_and_continuation_replay() { mut vm := new_with_options(Options{ max_steps: 0 }) or { panic(err) } first := vm.eval('(define saved #f) (define resume #f) (define (wide n unused) (if (= n 0) (saved) (begin (if (= n 2) (begin (set! saved (lambda () n)) (call/cc (lambda (k) (set! resume k) 0)))) (narrow (- n 1))))) (define (narrow n) (wide n 0)) (wide 100 0)') or { panic(err) } if first.whole() != 2 { panic('lost escaped tail binding') } resume := vm.lookup('resume') or { panic(err) } for _ in 0 .. 3 { value := vm.call(resume, [integer(0)]) or { panic(err) } if value.whole() != 2 { panic('tail activation corrupted continuation replay') } } }