Skip to content

Commit 28e4327

Browse files
committed
Refactor RISC-V sign extension to reduce code duplication
Extract common instruction sequence and parameterize differences to eliminate repeated emit calls in OP_sign_ext cases.
1 parent 2bf196d commit 28e4327

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/riscv-codegen.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,20 +443,20 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
443443
case OP_sign_ext:
444444
if (ph2_ir->src1 == 1) {
445445
/* Sign extend from byte to word */
446-
emit(__andi(rd, rs1, 0xFF));
447-
emit(__slli(rd, rd, 24));
448-
emit(__srai(rd, rd, 24));
446+
rs2 = 0xFF;
447+
ofs = 24;
449448
} else if (ph2_ir->src1 == 2) {
450449
/* Sign extend from short to word */
451-
emit(__andi(rd, rs1, 0xFFFF));
452-
emit(__slli(rd, rd, 16)); /* Shift left 16 bits */
453-
emit(__srai(rd, rd, 16)); /* Arithmetic shift right 16 bits */
450+
rs2 = 0xFFFF;
451+
ofs = 16;
454452
} else {
455453
/* Fallback to original behavior for unknown sizes */
456-
emit(__andi(rd, rs1, 0xFF));
457-
emit(__slli(rd, rd, 24));
458-
emit(__srai(rd, rd, 24));
454+
rs2 = 0xFF;
455+
ofs = 24;
459456
}
457+
emit(__andi(rd, rs1, rs2));
458+
emit(__slli(rd, rd, ofs));
459+
emit(__srai(rd, rd, ofs));
460460
return;
461461
case OP_cast:
462462
/* Generic cast operation - for now, just move the value */

0 commit comments

Comments
 (0)