rv2fox improvements

This commit is contained in:
leap123 2023-02-06 17:29:21 +07:00
parent 01b649dda1
commit 135b20a309

View File

@ -122,6 +122,11 @@ class Emitter:
source = self._adjust_label(source) source = self._adjust_label(source)
self.insn(f'rta', dest, source) self.insn(f'rta', dest, source)
def halt(self):
"""
Emit an halt instruction
"""
self.insn('halt')
class Converter: class Converter:
""" """
@ -144,7 +149,7 @@ class Converter:
self.xregidx = { r: i for i, r in enumerate(self.xregs) } self.xregidx = { r: i for i, r in enumerate(self.xregs) }
self.tmp = 'r0' # temporary register self.tmp = 'r0' # temporary register
self.branches = ['bge', 'blt', 'ble', 'bne'] self.branches = ['bge', 'bgeu', 'blt', 'bltu', 'ble', 'bleu', 'bne', 'bneu']
self.label_gen = iter(range(1000000)) self.label_gen = iter(range(1000000))
@ -180,6 +185,8 @@ class Converter:
self.e.data(*m.groups()) self.e.data(*m.groups())
elif m := re.fullmatch(r'string\s+\"(.*)\"', line): elif m := re.fullmatch(r'string\s+\"(.*)\"', line):
self.e.strz(*m.groups()) self.e.strz(*m.groups())
elif m := re.fullmatch(r'asciz\s+\"(.*)\"', line):
self.e.strz(*m.groups())
elif m := re.match(r'set\s([^,]*),', line): elif m := re.match(r'set\s([^,]*),', line):
self.e.label(*m.groups()) self.e.label(*m.groups())
@ -199,15 +206,26 @@ class Converter:
@staticmethod @staticmethod
def convert_arithmetic_op(op): def convert_arithmetic_op(op):
if op in ['add', 'addi']: return 'add' if op in ['add', 'addi']: return 'add'
if op in ['sub', 'subi']: return 'sub'
if op in ['mul']: return 'mul'
if op in ['div']: return 'idiv'
if op in ['divu']: return 'div'
if op in ['rem']: return 'irem'
if op in ['remu']: return 'rem'
if op in ['and', 'andi']: return 'and'
if op in ['or', 'ori']: return 'or'
if op in ['xor', 'xori']: return 'xor'
if op in ['sll', 'slli']: return 'sla' if op in ['sll', 'slli']: return 'sla'
if op in ['srl', 'srli']: return 'srl'
else: else:
raise Exception(f'Unknown op {op}') raise Exception(f'Unknown op {op}')
@staticmethod @staticmethod
def convert_condition(op): def convert_condition(op):
if op in ['blt']: return 'iflt', False if op in ['blt', 'bltu']: return 'iflt', False
if op in ['bne', 'bnez']: return 'ifnz', False if op in ['bne', 'bnez']: return 'ifnz', False
if op in ['ble']: return 'ifgt', True if op in ['ble', 'bleu']: return 'ifgt', True
if op in ['bge', 'bgeu']: return 'ifgteq', False
def convert_branch(self, insn, rs1, rs2, label): def convert_branch(self, insn, rs1, rs2, label):
cond, reverse = self.convert_condition(insn) cond, reverse = self.convert_condition(insn)
@ -237,7 +255,7 @@ class Converter:
if insn == 'nop': if insn == 'nop':
self.e.nop() self.e.nop()
elif insn in ['addi', 'slli']: elif insn in ['addi', 'subi' 'div', 'rem', 'andi', 'ori', 'xori', 'slli', 'srli']:
rd, rs, imm = operands rd, rs, imm = operands
op = self.convert_arithmetic_op(insn) op = self.convert_arithmetic_op(insn)
if rd == rs: if rd == rs:
@ -246,7 +264,7 @@ class Converter:
self.e.mov(tmp, self.reg(rs)) self.e.mov(tmp, self.reg(rs))
self.e.insn(op, tmp, imm) self.e.insn(op, tmp, imm)
self.e.mov(self.reg(rd), tmp) self.e.mov(self.reg(rd), tmp)
elif insn in ['add']: elif insn in ['add', 'sub', 'mul', 'divu', 'remu', 'and', 'nor', 'xor', 'sll', 'srl']:
rd, rs1, rs2 = operands rd, rs1, rs2 = operands
if rd == rs1: if rd == rs1:
op = self.convert_arithmetic_op(insn) op = self.convert_arithmetic_op(insn)
@ -287,6 +305,8 @@ class Converter:
elif insn in ['lui']: elif insn in ['lui']:
rd, imm = operands rd, imm = operands
self.e.mov(self.reg(rd), str(int(imm, 0) << 12)) self.e.mov(self.reg(rd), str(int(imm, 0) << 12))
elif insn == 'wfi':
self.e.halt()
else: else:
raise Exception(f'Unknown instruction {insn} {operands}') raise Exception(f'Unknown instruction {insn} {operands}')