Fix interpreter semantics of 'irsub_imm'

Previously it used `arg - imm` but the functionality should be a wrapping `imm - arg` (see `cranelift/codegen/meta/src/shared/instructions.rs`).
This commit is contained in:
Andrew Brown
2020-05-27 20:23:56 -07:00
committed by Benjamin Bouvier
parent fbac2e53f9
commit b017844bef
2 changed files with 8 additions and 8 deletions

View File

@@ -10,12 +10,12 @@ block0(v0: i32):
block1(v4: i32, v5:i32): block1(v4: i32, v5:i32):
v6 = iconst.i32 1 v6 = iconst.i32 1
v7 = irsub_imm v4, 2 v7 = iadd_imm v4, -2
fallthrough block2(v7, v5, v6) fallthrough block2(v7, v5, v6)
block2(v10: i32, v11: i32, v12: i32): ; params: n, fib(n-1), fib(n-2) block2(v10: i32, v11: i32, v12: i32): ; params: n, fib(n-1), fib(n-2)
v13 = iadd v11, v12 v13 = iadd v11, v12
v14 = irsub_imm v10, 1 v14 = iadd_imm v10, -1
v15 = icmp_imm eq v14, 0 v15 = icmp_imm eq v14, 0
brnz v15, block3(v13) brnz v15, block3(v13)
jump block2(v14, v13, v11) jump block2(v14, v13, v11)
@@ -43,9 +43,9 @@ block0(v0: i32):
fallthrough block1(v0) fallthrough block1(v0)
block1(v10: i32): block1(v10: i32):
v11 = irsub_imm v10, 1 v11 = iadd_imm v10, -1
v12 = call fn0(v11) v12 = call fn0(v11)
v13 = irsub_imm v10, 2 v13 = iadd_imm v10, -2
v14 = call fn0(v13) v14 = call fn0(v13)
v15 = iadd v12, v14 v15 = iadd v12, v14
return v15 return v15

View File

@@ -163,7 +163,7 @@ impl Interpreter {
let arg = frame.get(&arg); let arg = frame.get(&arg);
let result = match opcode { let result = match opcode {
IaddImm => binary_op!(Add::add[arg, imm]; [I8, I16, I32, I64, F32, F64]; inst), IaddImm => binary_op!(Add::add[arg, imm]; [I8, I16, I32, I64, F32, F64]; inst),
IrsubImm => binary_op!(Sub::sub[arg, imm]; [I8, I16, I32, I64, F32, F64]; inst), IrsubImm => binary_op!(Sub::sub[imm, arg]; [I8, I16, I32, I64, F32, F64]; inst),
_ => unimplemented!("interpreter does not support opcode yet: {}", opcode), _ => unimplemented!("interpreter does not support opcode yet: {}", opcode),
}?; }?;
frame.set(first_result(frame.function, inst), result); frame.set(first_result(frame.function, inst), result);
@@ -349,9 +349,9 @@ mod tests {
fn sanity() { fn sanity() {
let code = "function %test() -> b1 { let code = "function %test() -> b1 {
block0: block0:
v0 = iconst.i32 40 v0 = iconst.i32 1
v1 = iadd_imm v0, 3 v1 = iadd_imm v0, 1
v2 = irsub_imm v1, 1 v2 = irsub_imm v1, 44 ; 44 - 2 == 42 (see irsub_imm's semantics)
v3 = icmp_imm eq v2, 42 v3 = icmp_imm eq v2, 42
return v3 return v3
}"; }";