Merge pull request #2 from leap0x7b/rv2fox

rv2fox improvements
This commit is contained in:
Ry 2023-02-06 16:26:47 -08:00 committed by GitHub
commit 04aeba8eb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -122,6 +122,11 @@ class Emitter:
source = self._adjust_label(source)
self.insn(f'rta', dest, source)
def halt(self):
"""
Emit an halt instruction
"""
self.insn('halt')
class Converter:
"""
@ -144,7 +149,7 @@ class Converter:
self.xregidx = { r: i for i, r in enumerate(self.xregs) }
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))
@ -180,6 +185,8 @@ class Converter:
self.e.data(*m.groups())
elif m := re.fullmatch(r'string\s+\"(.*)\"', line):
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):
self.e.label(*m.groups())
@ -199,15 +206,26 @@ class Converter:
@staticmethod
def convert_arithmetic_op(op):
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 ['srl', 'srli']: return 'srl'
else:
raise Exception(f'Unknown op {op}')
@staticmethod
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 ['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):
cond, reverse = self.convert_condition(insn)
@ -237,7 +255,7 @@ class Converter:
if insn == '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
op = self.convert_arithmetic_op(insn)
if rd == rs:
@ -246,7 +264,7 @@ class Converter:
self.e.mov(tmp, self.reg(rs))
self.e.insn(op, tmp, imm)
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
if rd == rs1:
op = self.convert_arithmetic_op(insn)
@ -287,6 +305,8 @@ class Converter:
elif insn in ['lui']:
rd, imm = operands
self.e.mov(self.reg(rd), str(int(imm, 0) << 12))
elif insn == 'wfi':
self.e.halt()
else:
raise Exception(f'Unknown instruction {insn} {operands}')