module scheme import arena fn integer_tail_test_machine(mut vm VM, source string, a i64, b i64) !&Machine { vm.eval(source)! callable := vm.lookup('f')! vm.call(callable, [integer(a),integer(b)])! // Resolve original global loads. p := callable.procedure_pointer() 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}' return &Machine{function:p.function, env:&Environment{parent:p.env,slots:[integer(a),integer(b)]}, ip:0, steps:0, origin:p.function.instructions[0].origin} } fn test_integer_tail_plan_admits_both_polarities_and_aliases() { for source in [ '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))', '(define(f n a)(if (> n 0)(f (- n 1)(+ a n)) a))', '(define test =)(define next -)(define combine +)(define(f n a)(if(test n 0) a (f(next n 1)(combine a n))))', ] { mut vm := new_with_options(Options{max_steps:0,max_frames:0})! mut machine := integer_tail_test_machine(mut vm, source, 10, 0)! assert machine.fuse_integer_tail_loop(vm) assert machine.ip == 0 && machine.steps == 190 assert machine.env.slots[0].whole() == 0 && machine.env.slots[1].whole() == 55 assert machine.stack.len == 0 && machine.frames.len == 0 assert machine.origin == machine.function.instructions[machine.function.integer_tail.recurrence + 9].origin assert machine.run(mut vm)!.whole() == 55 } } fn test_integer_tail_plan_captured_bound_and_borrowed_call_storage() { mut vm := new_with_options(Options{max_steps:0,max_frames:0})! mut machine := integer_tail_test_machine(mut vm, '(define(make bound)(letrec((loop(lambda(n a)(if (>= n bound) a (loop (+ n 1)(+ a n)))))) loop))(define f(make 10))',0,0)! owner := machine.function.code.allocation_owner plan := machine.function.integer_tail mut borrowed := arena.new() borrowed.push() before := borrowed.used() assert machine.fuse_integer_tail_loop(vm) assert borrowed.used() == before assert voidptr(machine.function.integer_tail) == voidptr(plan) assert machine.function.code.allocation_owner == owner borrowed.pop() borrowed.free() gc_collect() assert machine.run(mut vm)!.whole() == 45 assert vm.call(vm.lookup('f')!,[integer(0),integer(0)])!.whole() == 45 } fn test_integer_tail_plan_declines_escaped_scopes_and_different_self_environments() { mut vm := new_with_options(Options{max_steps:0,max_frames:0})! mut machine := integer_tail_test_machine(mut vm, '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))', 3,0)! for flag in 0 .. 4 { machine.env.escaped = flag == 0 machine.env.tail_parent = flag == 1 if flag == 2 { machine.push(integer(42)) } if flag == 3 { original := vm.lookup('f')!.procedure_pointer() vm.define('f',procedure_value(&Procedure{mode:.closure, owner:vm.owner, function:original.function,env:&Environment{}}))! } assert !machine.fuse_integer_tail_loop(vm) assert machine.steps == 0 && machine.ip == 0 assert machine.env.slots[0].whole() == 3 && machine.env.slots[1].whole() == 0 if flag == 2 { assert machine.pop_value().whole() == 42 } } } fn test_integer_tail_plan_partial_budget_and_second_operation_overflow() { for limit in [18,19,37,38,39] { mut vm := new_with_options(Options{max_steps:0,max_frames:0})! mut machine := integer_tail_test_machine(mut vm, '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))', 3,0)! vm.options = Options{max_steps:limit,max_frames:0} advanced := machine.fuse_integer_tail_loop(vm) completed := if limit < 19 { 0 } else if limit < 38 { 1 } else { 2 } assert advanced == (completed > 0) assert machine.steps == completed * 19 assert machine.ip == 0 assert machine.env.slots[0].whole() == 3 - completed assert machine.env.slots[1].whole() == if completed == 0 { 0 } else if completed == 1 { 3 } else { 5 } } mut vm := new_with_options(Options{max_steps:0,max_frames:0})! mut machine := integer_tail_test_machine(mut vm, '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))', 2,0)! machine.env.slots[1] = integer(exact_max) assert !machine.fuse_integer_tail_loop(vm) assert machine.steps == 0 && machine.ip == 0 assert machine.env.slots[0].whole() == 2 && machine.env.slots[1].whole() == exact_max machine.env.slots[1] = integer(exact_max - 2) assert machine.fuse_integer_tail_loop(vm) assert machine.steps == 19 && machine.ip == 0 assert machine.env.slots[0].whole() == 1 && machine.env.slots[1].whole() == exact_max }