litmus5.js (2914B)
1 // Mutually recursive functions implement a multi-entry loop using indirect 2 // cross-module tail calls. The functions do not have the same signatures, so 3 // if all arguments are stack arguments then these use different amounts of 4 // stack space. 5 // 6 // Cross-module mutual recursion must be set up by JS, which is a bit of hair. 7 // But this should not destroy the ability to tail-call. 8 // 9 // The variable ballast is intended to test that we handle various combinations 10 // of stack and register arguments properly. 11 // 12 // The mutable globals accessed after the call are there to attempt to ensure 13 // that the correct instance is restored after the chain of tail calls. 14 15 for ( let ballast=1; ballast < TailCallBallast; ballast++ ) { 16 let vals = iota(ballast,1); 17 let ps = vals.map(_ => 'i32').join(' ') 18 let es = vals.map(i => `(local.get ${i})`).join(' ') 19 let sum = vals.reduceRight((p,c) => `(i32.add (local.get ${c}) ${p})`, `(i32.const 0)`) 20 let sumv = vals.reduce((p,c) => p+c); 21 22 let odd_cookie = 24680246; 23 let oddtext = ` 24 (module 25 (table (import "" "table") 2 2 funcref) 26 (type $even_t (func (param i32 ${ps}) (result i32))) 27 (global $glob (export "g") (mut i32) (i32.const ${odd_cookie})) 28 29 (func $odd_entry (export "odd_entry") (param $n i32) (param ${ps}) (result i32) 30 (call $odd (local.get $n) ${es} (i32.const 86)) 31 (global.get $glob) 32 i32.add) 33 34 (func $odd (export "odd") (param $n i32) (param ${ps}) (param $dummy i32) (result i32) 35 (if (result i32) (i32.eqz (local.get $n)) 36 (then (return (i32.or (i32.shl ${sum} (i32.const 1)) (i32.const 0)))) 37 (else (return_call_indirect (type $even_t) (i32.sub (local.get $n) (i32.const 1)) ${es} (i32.const 0))))))` 38 39 let even_cookie = 12345678; 40 let eventext = ` 41 (module 42 (table (import "" "table") 2 2 funcref) 43 (type $odd_t (func (param i32 ${ps} i32) (result i32))) 44 (global $glob (export "g") (mut i32) (i32.const ${even_cookie})) 45 46 (func $even_entry (export "even_entry") (param $n i32) (param ${ps}) (result i32) 47 (call $even (local.get $n) ${es}) 48 (global.get $glob) 49 i32.add) 50 51 (func $even (export "even") (param $n i32) (param ${ps}) (result i32) 52 (if (result i32) (i32.eqz (local.get $n)) 53 (then (return (i32.or (i32.shl ${sum} (i32.const 1)) (i32.const 1)))) 54 (else (return_call_indirect (type $odd_t) (i32.sub (local.get $n) (i32.const 1)) ${es} (i32.const 33) (i32.const 1))))))` 55 56 let table = new WebAssembly.Table({initial:2, maximum:2, element:"funcref"}) 57 58 let oddins = wasmEvalText(oddtext, {"":{table}}); 59 let evenins = wasmEvalText(eventext, {"":{table}}); 60 61 table.set(0, evenins.exports.even); 62 table.set(1, oddins.exports.odd); 63 64 assertEq(evenins.exports.even_entry(TailCallIterations, ...vals), ((sumv*2) + 1) + even_cookie); 65 assertEq(oddins.exports.odd_entry(TailCallIterations, ...vals, 33), ((sumv*2) + 0) + odd_cookie); 66 }