summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xpp2cc.py19
1 files changed, 19 insertions, 0 deletions
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