|
| 1 | +import * as assert from '../assert.js' |
| 2 | +import { instantiate } from '../wabt-wrapper.js'; |
| 3 | + |
| 4 | +async function test() { |
| 5 | + // The same local reaching a call twice, so the parallel move has to fan one source out to two |
| 6 | + // parameter registers. |
| 7 | + { |
| 8 | + const instance = await instantiate(` |
| 9 | + (func $sub (param i32) (param i32) (result i32) |
| 10 | + (i32.sub (local.get 0) (local.get 1))) |
| 11 | +
|
| 12 | + (func (export "foo") (param i32) (result i32) |
| 13 | + (call $sub (local.get 0) (local.get 0))) |
| 14 | + `); |
| 15 | + assert.eq(instance.exports.foo(7), 0); |
| 16 | + } |
| 17 | + |
| 18 | + // Fan-out and a swap in the same move: the third argument shares a source with the first, while |
| 19 | + // the first two are passed in the opposite order to the one they arrive in. |
| 20 | + { |
| 21 | + const instance = await instantiate(` |
| 22 | + (func $mix (param i32) (param i32) (param i32) (result i32) |
| 23 | + (i32.add (i32.mul (local.get 0) (i32.const 100)) |
| 24 | + (i32.add (i32.mul (local.get 1) (i32.const 10)) (local.get 2)))) |
| 25 | +
|
| 26 | + (func (export "foo") (param i32) (param i32) (result i32) |
| 27 | + (call $mix (local.get 1) (local.get 0) (local.get 1))) |
| 28 | + `); |
| 29 | + assert.eq(instance.exports.foo(2, 3), 323); |
| 30 | + } |
| 31 | + |
| 32 | + // One local filling every parameter, to exercise a source that is already-moved by the time the |
| 33 | + // later copies are emitted. |
| 34 | + { |
| 35 | + const instance = await instantiate(` |
| 36 | + (func $sum4 (param i32) (param i32) (param i32) (param i32) (result i32) |
| 37 | + (i32.add (i32.add (local.get 0) (local.get 1)) |
| 38 | + (i32.add (local.get 2) (local.get 3)))) |
| 39 | +
|
| 40 | + (func (export "foo") (param i32) (result i32) |
| 41 | + (call $sum4 (local.get 0) (local.get 0) (local.get 0) (local.get 0))) |
| 42 | + `); |
| 43 | + assert.eq(instance.exports.foo(5), 20); |
| 44 | + } |
| 45 | + |
| 46 | + // The same local as two return values. |
| 47 | + { |
| 48 | + const instance = await instantiate(` |
| 49 | + (func (export "foo") (param i32) (result i32 i32) |
| 50 | + (local.get 0) (local.get 0)) |
| 51 | + `); |
| 52 | + assert.eq(instance.exports.foo(11), [11, 11]); |
| 53 | + } |
| 54 | + |
| 55 | + // Floats, so the fan-out runs through the FPR side of the move. |
| 56 | + { |
| 57 | + const instance = await instantiate(` |
| 58 | + (func $fsub (param f64) (param f64) (result f64) |
| 59 | + (f64.sub (local.get 0) (local.get 1))) |
| 60 | +
|
| 61 | + (func (export "foo") (param f64) (result f64 f64) |
| 62 | + (call $fsub (local.get 0) (local.get 0)) |
| 63 | + (local.get 0)) |
| 64 | + `); |
| 65 | + assert.eq(instance.exports.foo(1.5), [0, 1.5]); |
| 66 | + } |
| 67 | + |
| 68 | + // Several entries naming one local when a write to it lands: each needs the old value, and the |
| 69 | + // reads afterwards need the new one. |
| 70 | + { |
| 71 | + const instance = await instantiate(` |
| 72 | + (func (export "foo") (param i32) (result i32 i32 i32 i32) |
| 73 | + (local.get 0) |
| 74 | + (local.get 0) |
| 75 | + (local.get 0) |
| 76 | + (local.set 0 (i32.const 42)) |
| 77 | + (local.get 0)) |
| 78 | + `); |
| 79 | + assert.eq(instance.exports.foo(3), [3, 3, 3, 42]); |
| 80 | + } |
| 81 | + |
| 82 | + // A write inside a nested block, with the readers sitting on the enclosing stack. |
| 83 | + { |
| 84 | + const instance = await instantiate(` |
| 85 | + (func (export "foo") (param i32) (result i32 i32 i32) |
| 86 | + (local.get 0) |
| 87 | + (local.get 0) |
| 88 | + (block (local.set 0 (i32.const 0xbbadbeef))) |
| 89 | + (local.get 0)) |
| 90 | + `); |
| 91 | + assert.eq(instance.exports.foo(3), [3, 3, 0xbbadbeef | 0]); |
| 92 | + } |
| 93 | + |
| 94 | + // Duplicate namers crossing a block boundary as that block's results. |
| 95 | + { |
| 96 | + const instance = await instantiate(` |
| 97 | + (func (export "foo") (param i32) (result i32 i32) |
| 98 | + (block (result i32 i32) |
| 99 | + (local.get 0) |
| 100 | + (local.get 0))) |
| 101 | + `); |
| 102 | + assert.eq(instance.exports.foo(9), [9, 9]); |
| 103 | + } |
| 104 | + |
| 105 | + // Duplicate namers live across a loop back edge. |
| 106 | + { |
| 107 | + const instance = await instantiate(` |
| 108 | + (func (export "foo") (param i32) (result i32) |
| 109 | + (local i32) |
| 110 | + (local.set 1 (i32.const 0)) |
| 111 | + (loop $l |
| 112 | + (local.set 1 (i32.add (local.get 1) (i32.add (local.get 0) (local.get 0)))) |
| 113 | + (local.set 0 (i32.sub (local.get 0) (i32.const 1))) |
| 114 | + (br_if $l (local.get 0))) |
| 115 | + (local.get 1)) |
| 116 | + `); |
| 117 | + assert.eq(instance.exports.foo(4), 20); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +await assert.asyncTest(test()); |
0 commit comments