A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
winch(aarch64): Remove side effect from shift kind to ALUOp calculation (#12501)
Saúl Cabrera01580149a0ef
diff --git a/tests/disas/winch/aarch64/i32_rotl/8_const.wat b/tests/disas/winch/aarch64/i32_rotl/8_const.watindex 72d8a65d9e..63ba50a657 100644--- a/tests/disas/winch/aarch64/i32_rotl/8_const.wat+++ b/tests/disas/winch/aarch64/i32_rotl/8_const.wat@@ -19,19 +19,18 @@;; movk x17, #0x10;; add x16, x16, x17;; cmp sp, x16-;; b.lo #0x64+;; b.lo #0x60;; 2c: mov x9, x0;; sub x28, x28, #0x10;; mov sp, x28;; stur x0, [x28, #8];; stur x1, [x28];; mov x0, #1-;; neg w0, w0-;; ror w0, w0, #2+;; ror w0, w0, #0x1e;; add x28, x28, #0x10;; mov sp, x28;; mov sp, x28;; ldr x28, [sp], #0x10;; ldp x29, x30, [sp], #0x10;; ret-;; 64: .byte 0x1f, 0xc1, 0x00, 0x00+;; 60: .byte 0x1f, 0xc1, 0x00, 0x00diff --git a/tests/disas/winch/aarch64/i32_shl/imm.wat b/tests/disas/winch/aarch64/i32_shl/imm.watnew file mode 100644index 0000000000..9d56e4f91f--- /dev/null+++ b/tests/disas/winch/aarch64/i32_shl/imm.wat@@ -0,0 +1,48 @@+;;! target = "aarch64"+;;! test = "winch"++(module+ (func+ i32.const 1+ i32.const 1+ i32.rotl+ i32.const 2+ i32.ne+ if+ unreachable+ end+ )+ (export "a" (func 0))+)+;; wasm[0]::function[0]:+;; stp x29, x30, [sp, #-0x10]!+;; mov x29, sp+;; str x28, [sp, #-0x10]!+;; mov x28, sp+;; ldur x16, [x0, #8]+;; ldur x16, [x16, #0x10]+;; mov x17, #0+;; movk x17, #0x10+;; add x16, x16, x17+;; cmp sp, x16+;; b.lo #0x78+;; 2c: mov x9, x0+;; sub x28, x28, #0x10+;; mov sp, x28+;; stur x0, [x28, #8]+;; stur x1, [x28]+;; mov x0, #1+;; ror w0, w0, #0x1f+;; cmp w0, #2+;; cset x0, ne+;; tst w0, w0+;; b.eq #0x60+;; b #0x5c+;; 5c: .byte 0x1f, 0xc1, 0x00, 0x00+;; add x28, x28, #0x10+;; mov sp, x28+;; mov sp, x28+;; ldr x28, [sp], #0x10+;; ldp x29, x30, [sp], #0x10+;; ret+;; 78: .byte 0x1f, 0xc1, 0x00, 0x00diff --git a/tests/disas/winch/aarch64/i64_rotl/8_const.wat b/tests/disas/winch/aarch64/i64_rotl/8_const.watindex 660ee6a6f6..055d339e84 100644--- a/tests/disas/winch/aarch64/i64_rotl/8_const.wat+++ b/tests/disas/winch/aarch64/i64_rotl/8_const.wat@@ -19,19 +19,18 @@;; movk x17, #0x10;; add x16, x16, x17;; cmp sp, x16-;; b.lo #0x64+;; b.lo #0x60;; 2c: mov x9, x0;; sub x28, x28, #0x10;; mov sp, x28;; stur x0, [x28, #8];; stur x1, [x28];; mov x0, #1-;; neg x0, x0-;; ror x0, x0, #2+;; ror x0, x0, #0x3e;; add x28, x28, #0x10;; mov sp, x28;; mov sp, x28;; ldr x28, [sp], #0x10;; ldp x29, x30, [sp], #0x10;; ret-;; 64: .byte 0x1f, 0xc1, 0x00, 0x00+;; 60: .byte 0x1f, 0xc1, 0x00, 0x00diff --git a/tests/disas/winch/aarch64/i64_shl/imm.wat b/tests/disas/winch/aarch64/i64_shl/imm.watnew file mode 100644index 0000000000..cd967acd40--- /dev/null+++ b/tests/disas/winch/aarch64/i64_shl/imm.wat@@ -0,0 +1,48 @@+;;! target = "aarch64"+;;! test = "winch"++(module+ (func+ i64.const 1+ i64.const 1+ i64.rotl+ i64.const 2+ i64.ne+ if+ unreachable+ end+ )+ (export "a" (func 0))+)+;; wasm[0]::function[0]:+;; stp x29, x30, [sp, #-0x10]!+;; mov x29, sp+;; str x28, [sp, #-0x10]!+;; mov x28, sp+;; ldur x16, [x0, #8]+;; ldur x16, [x16, #0x10]+;; mov x17, #0+;; movk x17, #0x10+;; add x16, x16, x17+;; cmp sp, x16+;; b.lo #0x78+;; 2c: mov x9, x0+;; sub x28, x28, #0x10+;; mov sp, x28+;; stur x0, [x28, #8]+;; stur x1, [x28]+;; mov x0, #1+;; ror x0, x0, #0x3f+;; cmp x0, #2+;; cset x0, ne+;; tst w0, w0+;; b.eq #0x60+;; b #0x5c+;; 5c: .byte 0x1f, 0xc1, 0x00, 0x00+;; add x28, x28, #0x10+;; mov sp, x28+;; mov sp, x28+;; ldr x28, [sp], #0x10+;; ldp x29, x30, [sp], #0x10+;; ret+;; 78: .byte 0x1f, 0xc1, 0x00, 0x00diff --git a/tests/misc_testsuite/winch/issue-12423.wast b/tests/misc_testsuite/winch/issue-12423.wastnew file mode 100644index 0000000000..dea1071435--- /dev/null+++ b/tests/misc_testsuite/winch/issue-12423.wast@@ -0,0 +1,27 @@+(module+ (func+ i64.const 1+ i64.const 1+ i64.rotl+ i64.const 2+ i64.ne+ if+ unreachable+ end+ )+ (func+ i32.const 1+ i32.const 1+ i32.rotl+ i32.const 2+ i32.ne+ if+ unreachable+ end+ )+ (export "a" (func 0))+ (export "b" (func 1))+)++(assert_return (invoke "a"))+(assert_return (invoke "b"))diff --git a/winch/codegen/src/isa/aarch64/asm.rs b/winch/codegen/src/isa/aarch64/asm.rsindex d64b98e9a1..548e0b8247 100644--- a/winch/codegen/src/isa/aarch64/asm.rs+++ b/winch/codegen/src/isa/aarch64/asm.rs@@ -86,6 +86,18 @@ impl From<OperandSize> for ScalarSize {}}+impl From<ShiftKind> for ALUOp {+ fn from(kind: ShiftKind) -> Self {+ match kind {+ ShiftKind::Shl => ALUOp::Lsl,+ ShiftKind::ShrS => ALUOp::Asr,+ ShiftKind::ShrU => ALUOp::Lsr,+ ShiftKind::Rotr => ALUOp::Extr,+ ShiftKind::Rotl => ALUOp::Extr,+ }+ }+}+/// Low level assembler implementation for Aarch64.pub(crate) struct Assembler {/// The machine instruction buffer.@@ -575,8 +587,15 @@ impl Assembler {kind: ShiftKind,size: OperandSize,) {- let shift_op = self.shift_kind_to_alu_op(kind, rm, size);- self.alu_rrr(shift_op, rm, rn, rd, size);+ let shift_op: ALUOp = kind.into();+ // In the case of rotate left, we negate the register containing the+ // shift value.+ if kind == ShiftKind::Rotl {+ self.alu_rrr(ALUOp::Sub, rm, regs::zero(), writable!(rm), size);+ self.alu_rrr(shift_op, rm, rn, rd, size);+ } else {+ self.alu_rrr(shift_op, rm, rn, rd, size);+ }}/// Shift immediate and register.@@ -588,8 +607,19 @@ impl Assembler {kind: ShiftKind,size: OperandSize,) {- let shift_op = self.shift_kind_to_alu_op(kind, rn, size);- self.alu_rri_shift(shift_op, imm, rn, rd, size);+ let shift_op: ALUOp = kind.into();+ // In the case of rotate left, we emit rotate right with type_size -+ // value.+ if kind == ShiftKind::Rotl {+ let value_size = size.num_bits();+ let mut imm_val = value_size.wrapping_sub(imm.value());+ imm_val &= value_size - 1;+ let negated_imm = ImmShift::maybe_from_u64(imm_val as u64).unwrap();++ self.alu_rri_shift(shift_op, negated_imm, rn, rd, size);+ } else {+ self.alu_rri_shift(shift_op, imm, rn, rd, size);+ }}/// Count Leading Zeros.@@ -1049,23 +1079,6 @@ impl Assembler {});}- // Convert ShiftKind to ALUOp. If kind == Rotl, then emulate it by emitting- // the negation of the given reg r, and returns ALUOp::Extr (an alias for- // `ror` the rotate-right instruction)- fn shift_kind_to_alu_op(&mut self, kind: ShiftKind, r: Reg, size: OperandSize) -> ALUOp {- match kind {- ShiftKind::Shl => ALUOp::Lsl,- ShiftKind::ShrS => ALUOp::Asr,- ShiftKind::ShrU => ALUOp::Lsr,- ShiftKind::Rotr => ALUOp::Extr,- ShiftKind::Rotl => {- // neg(r) is sub(zero, r).- self.alu_rrr(ALUOp::Sub, r, regs::zero(), writable!(r), size);- ALUOp::Extr- }- }- }-/// Get a label from the underlying machine code buffer.pub fn get_label(&mut self) -> MachLabel {self.buffer.get_label()diff --git a/winch/codegen/src/isa/aarch64/masm.rs b/winch/codegen/src/isa/aarch64/masm.rsindex d5a3e77f11..ef72fa490c 100644--- a/winch/codegen/src/isa/aarch64/masm.rs+++ b/winch/codegen/src/isa/aarch64/masm.rs@@ -764,6 +764,9 @@ impl Masm for MacroAssembler {size: OperandSize,) -> Result<()> {match ImmShift::maybe_from_u64(imm.unwrap_as_u64()) {+ // Immediate Ranges:+ // 32-bit variant: 0-31+ // 64-bit variant: 0-63Some(imml) if imml.value() < size.num_bits() => {self.asm.shift_ir(imml, lhs, dst, kind, size)}