Skip to content

Commit 6973f99

Browse files
committed
Fix Arm outputs for right shift operations
Let shecc generate arithmetic shift right instructions for right shift operations instead of using logical shift right instructions. Currently, the behavior of right shift operations is consistent with GCC and Clang.
1 parent aab5755 commit 6973f99

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

src/arm-codegen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
408408
emit(__sll(__AL, rd, rn, rm));
409409
return;
410410
case OP_rshift:
411-
emit(__srl(__AL, rd, rn, rm));
411+
emit(__sra(__AL, rd, rn, rm));
412412
return;
413413
case OP_eq:
414414
case OP_neq:

src/arm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ int __sll_amt(arm_cond_t cond,
226226
rm + (0 << 4) + (shift << 5) + (amt << 7));
227227
}
228228

229+
int __sra(arm_cond_t cond, arm_reg rd, arm_reg rm, arm_reg rs)
230+
{
231+
return arm_encode(cond, 0 + (arm_mov << 1) + (0 << 5), 0, rd,
232+
rm + (5 << 4) + (rs << 8));
233+
}
234+
229235
int __add_i(arm_cond_t cond, arm_reg rd, arm_reg rs, int imm)
230236
{
231237
if (imm >= 0)

tests/driver.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ expr 1 "1 && 1"
120120

121121
expr 16 "2 << 3"
122122
expr 32 "256 >> 3"
123+
try_output 0 "128 59926 -6 -4 -500283" << EOF
124+
int main() {
125+
printf("%d %d %d %d %d", 32768 >> 8, 245458999 >> 12, -11 >> 1, -16 >> 2, -1000565 >> 1);
126+
return 0;
127+
}
128+
EOF
123129
expr 239 "237 | 106"
124130
expr 135 "237 ^ 106"
125131
expr 104 "237 & 106"

0 commit comments

Comments
 (0)