text
stringlengths
1
93.6k
if self.required_temp_regs:
self.riscv_instructions.append(
f'mv {sp}, {load_src} # writeback'
)
else:
self.riscv_instructions.append(
f'addi {sp}, {sp}, {final_offset} # writeback'
)
class Compare(Arm64Instruction):
"""Compare: sets the comparison arithmetically."""
opcodes = ['cmp']
def __init__(self, opcode, operands):
super().__init__(opcode, operands)
self.specific_regs.append(COMPARE)
def emit_riscv(self):
lhs, rhs = self.get_args()
cmpreg = self.specific_regs[-1]
op = 'sub'
if self.is_safe_imm(self.operands[1]):
rhs = -rhs
op = 'addi'
self.riscv_instructions = [
f'{op} {cmpreg}, {lhs}, {rhs}'
]
class ConditionalBranch(Arm64Instruction):
"""Conditional branches are about the same, just check the last comparison to zero
"""
opcodes = ['ble', 'blt', 'bge', 'bgt', 'beq', 'bne', 'bpl', 'bhi']
opmap = dict(zip(opcodes, opcodes))
opmap['bpl'] = 'bge'
opmap['bhi'] = 'bgt'
def __init__(self, opcode, operands):
super().__init__(opcode, operands)
self.target = operands[0]['label']
self.specific_regs = [COMPARE]
self.op = self.opmap[self.opcode]
def emit_riscv(self):
cmpreg = self.specific_regs[0]
self.riscv_instructions = [
f'{self.op} {cmpreg}, x0, {self.target}'
]
class ConditionalBranchNonZero(Arm64Instruction):
''' CBNZ -- branch if R!=0
'''
opcodes = ['cbnz']
def emit_riscv(self):
r = self.specific_regs[0]
target = self.operands[1]['label']
self.riscv_instructions = [
f'bne {r}, x0, {target}'
]
class ConditionalSelect(Arm64Instruction):
"""Conditional Select uses a local branch to guard the condition
Note: pray that '999999' is not being used as a numeric label elsewhere.
We only go forward so multiple csels won't matter
TODO: add checking for the numeric local in cleanup
"""
opcodes = ['csel']
def __init__(self, opcode, operands):
super().__init__(opcode, operands)
self.cc = operands[3]['label'].lower()
self.specific_regs.append(COMPARE)
# this allows cutting out a branch and simplifying
self.required_temp_regs = ['temp']
def emit_riscv(self):
dest, s1, s2, cond = self.specific_regs
temp = self.required_temp_regs[0]
self.riscv_instructions = [
f'add{self.wflag} {temp}, {s1}, x0 # move option s1 to temp',
f'b{self.cc} {cond}, x0, 999999f # conditionally branch past moving option s2 to temp ', # f -- only forward.
f'add{self.wflag} {temp}, {s2}, x0 # move s2 to temp',
f'999999:',
f'add{self.wflag} {dest}, x0, {temp} # move temp to dest'
]
class ConditionalSet(Arm64Instruction):
"""Conditional Set uses a local branch to guard the condition
"""
opcodes = ['cset']