| 1 | module scheme |
| 2 | |
| 3 | import strings |
| 4 | |
| 5 | fn test_native_entry_argument_boundary_keeps_nine_argument_fallback() { |
| 6 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 7 | vm.eval('(define (eight a b c d e f g h) (+ a b c d e f g h)) |
| 8 | (define (nine a b c d e f g h i) (+ a b c d e f g h i))')! |
| 9 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 10 | assert vm.call(vm.lookup('eight')!, [integer(1), integer(2), integer(3), integer(4), |
| 11 | integer(5), integer(6), integer(7), integer(8)])!.whole() == 36 |
| 12 | assert vm.call(vm.lookup('nine')!, [integer(1), integer(2), integer(3), integer(4), |
| 13 | integer(5), integer(6), integer(7), integer(8), integer(9)])!.whole() == 45 |
| 14 | hits, deopts := vm.native_execution_stats() |
| 15 | assert hits == 1 && deopts == 0 |
| 16 | return unspecified() |
| 17 | })! |
| 18 | } |
| 19 | |
| 20 | fn test_native_entry_graph_boundary_keeps_oversized_root_on_vm() { |
| 21 | for count in [32, 33] { |
| 22 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 23 | mut source := strings.new_builder(2048) |
| 24 | for i in 0 .. count { |
| 25 | body := if i + 1 == count { '(+ x 1)' } else { '(node${i + 1} x)' } |
| 26 | source.writeln('(define (node${i} x) ${body})') |
| 27 | } |
| 28 | vm.eval(source.str())! |
| 29 | root := vm.lookup('node0')! |
| 30 | if graph := native_graph(mut vm, root, [integer(41)]) { |
| 31 | assert count == 32 && graph.nodes.len == 32 |
| 32 | } else { |
| 33 | assert count == 33 && err.msg().contains('graph too large') |
| 34 | } |
| 35 | vm.with_native_execution(fn [root, count] (mut vm VM) !Value { |
| 36 | assert vm.call(root, [integer(41)])!.whole() == 42 |
| 37 | scope := unsafe { &NativeScope(vm.native_execution_scope) } |
| 38 | assert scope.hits == 1 && scope.deopts == 0 |
| 39 | assert scope.plans.len == 1 && scope.plans[0].nodes.len == 32 |
| 40 | assert scope.rejected.len == count - 32 |
| 41 | if count == 33 { |
| 42 | assert voidptr(scope.rejected[0]) == voidptr(root.procedure_pointer().function) |
| 43 | } |
| 44 | return unspecified() |
| 45 | })! |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn test_native_entry_capture_boundary_clears_full_live_prefix() { |
| 50 | for count in [1024, 1025] { |
| 51 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 52 | mut source := strings.new_builder(4096) |
| 53 | source.write_string('(define work (let ((x 17)) (lambda () (begin ') |
| 54 | for _ in 0 .. count { source.write_string('x ') } |
| 55 | source.write_string('))))') |
| 56 | vm.eval(source.str())! |
| 57 | root := vm.lookup('work')! |
| 58 | if graph := native_graph(mut vm, root, []Value{}) { |
| 59 | assert count == 1024 && graph.captures == 1024 |
| 60 | assert native_capture_layout(mut vm, graph)!.len == 1024 |
| 61 | } else { |
| 62 | assert count == 1025 && err.msg().contains('too many native captures') |
| 63 | } |
| 64 | vm.with_native_execution(fn [root, count] (mut vm VM) !Value { |
| 65 | assert vm.call(root, []Value{})!.whole() == 17 |
| 66 | scope := unsafe { &NativeScope(vm.native_execution_scope) } |
| 67 | assert scope.hits == u64(if count == 1024 { 1 } else { 0 }) |
| 68 | assert scope.deopts == 0 |
| 69 | for value in scope.capture_values { assert value.k == 0 } |
| 70 | for cell in scope.capture_cells { assert isnil(cell.env) } |
| 71 | return unspecified() |
| 72 | })! |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fn test_native_entry_highest_written_capture_commits_and_releases_cell() { |
| 77 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 78 | mut source := strings.new_builder(32768) |
| 79 | source.write_string('(define (identity x) x) (define accessors (let (') |
| 80 | for i in 0 .. 1024 { source.write_string('(x${i} ${i})') } |
| 81 | source.write_string(') (list (lambda () (begin ') |
| 82 | for i in 0 .. 1023 { source.write_string('x${i} ') } |
| 83 | source.write_string('(set! x1023 42) (identity 42))) (lambda () x1023)))) |
| 84 | (define work (car accessors)) (define last (cadr accessors))') |
| 85 | vm.eval(source.str())! |
| 86 | root := vm.lookup('work')! |
| 87 | graph := native_graph(mut vm, root, []Value{})! |
| 88 | layout := native_capture_layout(mut vm, graph)! |
| 89 | assert graph.captures == 1024 && layout.len == 1024 |
| 90 | assert layout[1023].written && layout[1023].alias == 1023 |
| 91 | vm.with_native_execution(fn [root] (mut vm VM) !Value { |
| 92 | assert vm.call(root, []Value{})!.whole() == 42 |
| 93 | scope := unsafe { &NativeScope(vm.native_execution_scope) } |
| 94 | assert scope.hits == 1 && scope.deopts == 0 |
| 95 | assert scope.plans[0].capture_writes == [1023] |
| 96 | for value in scope.capture_values { assert value.k == 0 } |
| 97 | for cell in scope.capture_cells { assert isnil(cell.env) } |
| 98 | return unspecified() |
| 99 | })! |
| 100 | gc_collect() |
| 101 | assert vm.call(vm.lookup('last')!, []Value{})!.whole() == 42 |
| 102 | } |
| 103 | |