Skip to content

Commit 7d8b2d8

Browse files
agattidpgeorge
authored andcommitted
py/asmrv32: Use REG_TEMP2 whenever possible.
The RV32 emitter used an additional temporary register, as certain code sequences required extra storage. This commit removes its usage in all but one case, using REG_TEMP2 instead. Signed-off-by: Alessandro Gatti <[email protected]>
1 parent da0e027 commit 7d8b2d8

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

py/asmrv32.c

+18-28
Original file line numberDiff line numberDiff line change
@@ -312,25 +312,23 @@ void asm_rv32_emit_call_ind(asm_rv32_t *state, mp_uint_t index) {
312312
if (FIT_UNSIGNED(offset, 11)) {
313313
// lw temporary, offset(fun_table)
314314
// c.jalr temporary
315-
asm_rv32_opcode_lw(state, INTERNAL_TEMPORARY, REG_FUN_TABLE, offset);
316-
asm_rv32_opcode_cjalr(state, INTERNAL_TEMPORARY);
315+
asm_rv32_opcode_lw(state, REG_TEMP2, REG_FUN_TABLE, offset);
316+
asm_rv32_opcode_cjalr(state, REG_TEMP2);
317317
return;
318318
}
319319

320320
mp_uint_t upper = 0;
321321
mp_uint_t lower = 0;
322322
split_immediate(offset, &upper, &lower);
323323

324-
// TODO: Can this clobber REG_TEMP[0:2]?
325-
326324
// lui temporary, HI(index) ; Or c.lui if possible
327325
// c.add temporary, fun_table
328326
// lw temporary, LO(index)(temporary)
329327
// c.jalr temporary
330-
load_upper_immediate(state, INTERNAL_TEMPORARY, upper);
331-
asm_rv32_opcode_cadd(state, INTERNAL_TEMPORARY, REG_FUN_TABLE);
332-
asm_rv32_opcode_lw(state, INTERNAL_TEMPORARY, INTERNAL_TEMPORARY, lower);
333-
asm_rv32_opcode_cjalr(state, INTERNAL_TEMPORARY);
328+
load_upper_immediate(state, REG_TEMP2, upper);
329+
asm_rv32_opcode_cadd(state, REG_TEMP2, REG_FUN_TABLE);
330+
asm_rv32_opcode_lw(state, REG_TEMP2, REG_TEMP2, lower);
331+
asm_rv32_opcode_cjalr(state, REG_TEMP2);
334332
}
335333

336334
void asm_rv32_emit_jump_if_reg_eq(asm_rv32_t *state, mp_uint_t rs1, mp_uint_t rs2, mp_uint_t label) {
@@ -350,15 +348,13 @@ void asm_rv32_emit_jump_if_reg_eq(asm_rv32_t *state, mp_uint_t rs1, mp_uint_t rs
350348
mp_uint_t lower = 0;
351349
split_immediate(displacement, &upper, &lower);
352350

353-
// TODO: Can this clobber REG_TEMP[0:2]?
354-
355351
// bne rs1, rs2, 12 ; PC + 0
356352
// auipc temporary, HI(displacement) ; PC + 4
357353
// jalr zero, temporary, LO(displacement) ; PC + 8
358354
// ... ; PC + 12
359355
asm_rv32_opcode_bne(state, rs1, rs2, 12);
360-
asm_rv32_opcode_auipc(state, INTERNAL_TEMPORARY, upper);
361-
asm_rv32_opcode_jalr(state, ASM_RV32_REG_ZERO, INTERNAL_TEMPORARY, lower);
356+
asm_rv32_opcode_auipc(state, REG_TEMP2, upper);
357+
asm_rv32_opcode_jalr(state, ASM_RV32_REG_ZERO, REG_TEMP2, lower);
362358
}
363359

364360
void asm_rv32_emit_jump_if_reg_nonzero(asm_rv32_t *state, mp_uint_t rs, mp_uint_t label) {
@@ -377,8 +373,6 @@ void asm_rv32_emit_jump_if_reg_nonzero(asm_rv32_t *state, mp_uint_t rs, mp_uint_
377373
return;
378374
}
379375

380-
// TODO: Can this clobber REG_TEMP[0:2]?
381-
382376
// if rs1 in C window and displacement is negative:
383377
// c.beqz rs', 10 ; PC + 0
384378
// auipc temporary, HI(displacement) ; PC + 2
@@ -403,8 +397,8 @@ void asm_rv32_emit_jump_if_reg_nonzero(asm_rv32_t *state, mp_uint_t rs, mp_uint_
403397
mp_uint_t upper = 0;
404398
mp_uint_t lower = 0;
405399
split_immediate(displacement, &upper, &lower);
406-
asm_rv32_opcode_auipc(state, INTERNAL_TEMPORARY, upper);
407-
asm_rv32_opcode_jalr(state, ASM_RV32_REG_ZERO, INTERNAL_TEMPORARY, lower);
400+
asm_rv32_opcode_auipc(state, REG_TEMP2, upper);
401+
asm_rv32_opcode_jalr(state, ASM_RV32_REG_ZERO, REG_TEMP2, lower);
408402
}
409403

410404
void asm_rv32_emit_mov_local_reg(asm_rv32_t *state, mp_uint_t local, mp_uint_t rs) {
@@ -426,14 +420,12 @@ void asm_rv32_emit_mov_local_reg(asm_rv32_t *state, mp_uint_t local, mp_uint_t r
426420
mp_uint_t lower = 0;
427421
split_immediate(offset, &upper, &lower);
428422

429-
// TODO: Can this clobber REG_TEMP[0:2]?
430-
431423
// lui temporary, HI(offset) ; Or c.lui if possible
432424
// c.add temporary, sp
433425
// sw rs, LO(offset)(temporary)
434-
load_upper_immediate(state, INTERNAL_TEMPORARY, upper);
435-
asm_rv32_opcode_cadd(state, INTERNAL_TEMPORARY, ASM_RV32_REG_SP);
436-
asm_rv32_opcode_sw(state, rs, INTERNAL_TEMPORARY, lower);
426+
load_upper_immediate(state, REG_TEMP2, upper);
427+
asm_rv32_opcode_cadd(state, REG_TEMP2, ASM_RV32_REG_SP);
428+
asm_rv32_opcode_sw(state, rs, REG_TEMP2, lower);
437429
}
438430

439431
void asm_rv32_emit_mov_reg_local(asm_rv32_t *state, mp_uint_t rd, mp_uint_t local) {
@@ -525,12 +517,10 @@ void asm_rv32_emit_jump(asm_rv32_t *state, mp_uint_t label) {
525517
mp_uint_t lower = 0;
526518
split_immediate(displacement, &upper, &lower);
527519

528-
// TODO: Can this clobber REG_TEMP[0:2]?
529-
530520
// auipc temporary, HI(displacement)
531521
// jalr zero, temporary, LO(displacement)
532-
asm_rv32_opcode_auipc(state, INTERNAL_TEMPORARY, upper);
533-
asm_rv32_opcode_jalr(state, ASM_RV32_REG_ZERO, INTERNAL_TEMPORARY, lower);
522+
asm_rv32_opcode_auipc(state, REG_TEMP2, upper);
523+
asm_rv32_opcode_jalr(state, ASM_RV32_REG_ZERO, REG_TEMP2, lower);
534524
}
535525

536526
void asm_rv32_emit_store_reg_reg_offset(asm_rv32_t *state, mp_uint_t rd, mp_uint_t rs, mp_int_t offset) {
@@ -549,9 +539,9 @@ void asm_rv32_emit_store_reg_reg_offset(asm_rv32_t *state, mp_uint_t rd, mp_uint
549539
// lui temporary, HI(offset) ; Or c.lui if possible
550540
// c.add temporary, rs
551541
// sw rd, LO(offset)(temporary)
552-
load_upper_immediate(state, INTERNAL_TEMPORARY, upper);
553-
asm_rv32_opcode_cadd(state, INTERNAL_TEMPORARY, rs);
554-
asm_rv32_opcode_sw(state, rd, INTERNAL_TEMPORARY, lower);
542+
load_upper_immediate(state, REG_TEMP2, upper);
543+
asm_rv32_opcode_cadd(state, REG_TEMP2, rs);
544+
asm_rv32_opcode_sw(state, rd, REG_TEMP2, lower);
555545
}
556546

557547
void asm_rv32_emit_mov_reg_pcrel(asm_rv32_t *state, mp_uint_t rd, mp_uint_t label) {

0 commit comments

Comments
 (0)