From 2224c3d82acc392bd380de352305a6da6cebf274 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sun, 27 Nov 2011 14:20:41 +0000 Subject: Fix function calls yielding 'None' as register where 'R0' should be placed --- pp2cc.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pp2cc.py b/pp2cc.py index db1886e..35b208b 100755 --- a/pp2cc.py +++ b/pp2cc.py @@ -72,12 +72,31 @@ class Registers(object): return text # out of luck return None + def get_instruction(self, text): + """Retrieve the instruction from text skipping labels and comments""" + text = text.strip() + # ignore commented lines + if text.startswith(";"): + return None + # skip labels if any + text = text.upper().split(":", 1)[-1] + # the first element is assumed to be an instruction + text = text.lstrip(" ").split(" ", 1)[0] + # instructions have a length between 2 (OR) and 4 (LOAD) + if len(text) >= 2 and len(text) <= 4: + return text + return None def find_register(self, instructions_list, fatal=False): """Finds the last modified register in a list of instructions""" for line in reversed(instructions_list): reg = self.get_register(line) if reg: return reg + else: + instruction = self.get_instruction(line) + # convention: non-void functions store their result value in R0 + if instruction == "BRS": + return "R0" if fatal: raise RuntimeError("No register found in instructions list") return None -- cgit v1.2.1