text
stringlengths
1
93.6k
""" convert shifts to corresponding name in RISC-V
"""
opcodes = ['lsl', 'lsr', 'asr']
opmap = {
'lsl': 'sll',
'lsr': 'srl',
'asr': 'sra',
}
def emit_riscv(self):
dest, s1, s2 = self.get_args()
self.riscv_instructions = [
f'{self.opmap[self.opcode]}{self.iflag}{self.wflag} {dest}, {s1}, {s2}'
]
"""
RISC-V Load Types
LB rd,offset(rs1) Load Byte rd ← s8[rs1 + offset]
LH rd,offset(rs1) Load Half rd ← s16[rs1 + offset]
LW rd,offset(rs1) Load Word rd ← s32[rs1 + offset]
LBU rd,offset(rs1) Load Byte Unsigned rd ← u8[rs1 + offset]
LHU rd,offset(rs1) Load Half Unsigned rd ← u16[rs1 + offset]
LWU rd,offset(rs1) Load Word Unsigned rd ← u32[rs1 + offset]
LD rd,offset(rs1) Load Double rd ← u64[rs1 + offset]
RISC-V stores
SB rs2,offset(rs1) Store Byte u8[rs1 + offset] ← rs2
SH rs2,offset(rs1) Store Half u16[rs1 + offset] ← rs2
SW rs2,offset(rs1) Store Word u32[rs1 + offset] ← rs2
SD rs2,offset(rs1) Store Double u64[rs1 + offset] ← rs2
"""
class LoadStoreRegister(Arm64Instruction):
"""The behavior here is roughly equivalent for L/S.
Load and Store are mixed together because the SP behavior (the complicated part here) is basically identical.
"""
opcodes = ['ldr', 'ldrb', 'ldrsw', 'ldrsh', 'str', 'strh', 'strb']
opmap = {
'ldrb': 'lb',
'ldrsh': 'lh',
'ldrsw': 'lw',
'ldr': 'ld',
'str': 'sd',
'strh': 'sh',
'strb': 'sb'
}
def __init__(self, opcode, operands):
super().__init__(opcode, operands)
if self.opcode[0] == 's':
self.num_reg_writes = 0
r1, sp = operands[:2]
self.base_op = self.opmap[self.opcode]
if self.opcode not in ('ldr', 'str'):
pass
elif r1['type'] == 'fp':
self.base_op = 'f' + self.opcode[0] + get_fl_flag(r1)
self.float_st = True
elif self.wflag:
self.base_op = self.opcode[0] + 'w'
self.reg_offset = False
if len(operands) == 3:
post_index = True
self.final_offset = operands[2]['immediate']
elif sp['writeback']:
pre_index = True
self.final_offset = self.offset
else: # Signed Offset
self.final_offset = None
if sp.get('original_mode'):
if 'got' in sp['original_mode']: # GOT -- relocation
self.base_op = 'add' # this is weird but that's what the Arm docs say 🤷🏻‍♂️
self.offset = sp['offset']
def emit_riscv(self):
super().emit_riscv()
dest, sp, *reg_offset = self.specific_regs
if self.base_op == 'add':
self.riscv_instructions.append(
f'{self.base_op} {dest}, {sp}, {self.offset} # load from GOT -> ADD!'
)
return
load_src = sp
if self.set_offset_reg:
load_src = self.required_temp_regs[0]
self.riscv_instructions.append(
f'{self.base_op} {dest}, {self.offset}({load_src})'
)
if self.final_offset: