Skip to content

Commit

Permalink
i#3544: RV64: Mangle pc-relative addressing destining to tp (#7068)
Browse files Browse the repository at this point in the history
Reuse mangle_stolen_reg_and_tp_reg() to simplify register spilling.

This handles code sequence like

```
_start:
        lla             tp,             slot
	lw              a0,             0(tp)
	li              a7,             93
	ecall
slot:   .long           0x5a
```

Issue: #3544
  • Loading branch information
ziyao233 authored Nov 11, 2024
1 parent 0ab4d54 commit 72cf877
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions core/arch/riscv64/mangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,40 +608,6 @@ mangle_indirect_jump(dcontext_t *dcontext, instrlist_t *ilist, instr_t *instr,
return next_instr;
}

instr_t *
mangle_rel_addr(dcontext_t *dcontext, instrlist_t *ilist, instr_t *instr,
instr_t *next_instr)
{
opnd_t dst = instr_get_dst(instr, 0);
app_pc tgt;
ASSERT(instr_get_opcode(instr) == OP_auipc);
ASSERT(instr_has_rel_addr_reference(instr));
instr_get_rel_data_or_instr_target(instr, &tgt);
ASSERT(opnd_is_reg(dst));
ASSERT(opnd_is_rel_addr(instr_get_src(instr, 0)));

ASSERT_NOT_IMPLEMENTED(!instr_uses_reg(instr, DR_REG_TP));

if (instr_uses_reg(instr, dr_reg_stolen)) {
dst = opnd_create_reg(DR_REG_A0);
PRE(ilist, next_instr,
instr_create_save_to_tls(dcontext, DR_REG_A0, TLS_REG0_SLOT));
}

insert_mov_immed_ptrsz(dcontext, (ptr_int_t)tgt, dst, ilist, next_instr, NULL, NULL);

if (instr_uses_reg(instr, dr_reg_stolen)) {
PRE(ilist, next_instr,
instr_create_save_to_tls(dcontext, DR_REG_A0, TLS_REG_STOLEN_SLOT));
PRE(ilist, next_instr,
instr_create_restore_from_tls(dcontext, DR_REG_A0, TLS_REG0_SLOT));
}

instrlist_remove(ilist, instr);
instr_destroy(dcontext, instr);
return NULL;
}

static reg_id_t
pick_scratch_reg(dcontext_t *dcontext, instr_t *instr, reg_id_t do_not_pick,
reg_id_t do_not_pick_2, ushort *scratch_slot DR_PARAM_OUT)
Expand Down Expand Up @@ -866,6 +832,29 @@ mangle_special_registers(dcontext_t *dcontext, instrlist_t *ilist, instr_t *inst
return next_instr;
}

instr_t *
mangle_rel_addr(dcontext_t *dcontext, instrlist_t *ilist, instr_t *instr,
instr_t *next_instr)
{
app_pc tgt;
ASSERT(instr_get_opcode(instr) == OP_auipc);
ASSERT(instr_has_rel_addr_reference(instr));
instr_get_rel_data_or_instr_target(instr, &tgt);

if (instr_uses_reg(instr, dr_reg_stolen) || instr_uses_reg(instr, DR_REG_TP))
mangle_stolen_reg_and_tp_reg(dcontext, ilist, instr, next_instr);

opnd_t dst = instr_get_dst(instr, 0);
ASSERT(opnd_is_reg(dst));
ASSERT(opnd_is_rel_addr(instr_get_src(instr, 0)));

insert_mov_immed_ptrsz(dcontext, (ptr_int_t)tgt, dst, ilist, instr, NULL, NULL);

instrlist_remove(ilist, instr);
instr_destroy(dcontext, instr);
return NULL;
}

/***************************************************************************
* LR/SC sequence mangling.
*/
Expand Down

0 comments on commit 72cf877

Please sign in to comment.