From 135b20a30964daaf205647105218e95d81ae6422 Mon Sep 17 00:00:00 2001 From: leap123 Date: Mon, 6 Feb 2023 17:29:21 +0700 Subject: [PATCH 1/2] rv2fox improvements --- gcc/rv2fox | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/gcc/rv2fox b/gcc/rv2fox index 45e1f4d..ceca99d 100755 --- a/gcc/rv2fox +++ b/gcc/rv2fox @@ -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}') From 71324b8898aa2c80725dd195feed79eb8be893c9 Mon Sep 17 00:00:00 2001 From: leap123 Date: Mon, 6 Feb 2023 17:31:56 +0700 Subject: [PATCH 2/2] fix --- gcc/rv2fox | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gcc/rv2fox b/gcc/rv2fox index ceca99d..e0a9d29 100755 --- a/gcc/rv2fox +++ b/gcc/rv2fox @@ -255,7 +255,7 @@ class Converter: if insn == 'nop': self.e.nop() - elif insn in ['addi', 'subi' 'div', 'rem', 'andi', 'ori', 'xori', 'slli', 'srli']: + 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: