From bb3c12c5c31f637642936e3177ad211fa3053e3e Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 28 Nov 2011 22:41:00 +0000 Subject: Properly handle $F (=$FFFFF) and %1 (=-1) assembly constants --- pp2cc.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'pp2cc.py') diff --git a/pp2cc.py b/pp2cc.py index b130356..14de70a 100755 --- a/pp2cc.py +++ b/pp2cc.py @@ -690,6 +690,10 @@ class Parse(object): lines.append(self.asm.noop(lbl_end, register=result_reg)) return lines def convertInteger(self, value=None, node=None): + """Returns a string of the value + + For integers, the string returned is a base-10 number modulo WORDSIZE + """ if value is None: if isinstance(node, c_ast.Constant) and node.type == "int": value = node.value @@ -704,23 +708,34 @@ class Parse(object): if "U" in value: # unsigned integer, ignore for now value = value.replace("U", "", 1) if value.startswith("0X"): - value = value[2:] - if len(value) == 5: - # highest possible value is 3FFFF, so adding a zero would not make sense - value = "$" + value - else: - value = "$0" + value + value = int(value[2:], 16) elif value.startswith("0"): # octal numbers are not understood by PP2 assembly, therefore convert it - value = str(int(value, 8)) - return value + value = int(value, 8) + else: + value = int(value) + # values are modulo WORDSIZE. If negative, the MSB is 1 + value %= pow(2, self.WORDSIZE) + return str(value) def convertStrToInteger(self, str): """Converts a PP2 assembly integer string to an integer""" str = str.upper() if str.startswith("$"): - return int(str[1:], 16) + if str.startswith("$0"): + return int(str[2:], 16) + else: + # anything with a leading 8 or higher is padded... assembler + # bug? + val = str[1:] + if int(val[1]) >= 8: + hexsize = len(hex(pow(2, self.WORDSIZE) - 1)[2:]) + val = val.rjust(hexsize, "F") + return int(val, 16) elif str.startswith("%"): - return int(str[1:], 8) + if str.startswith("%0"): + return int(str[2:], 8) + else: + return int(str[1:].rjust(self.WORDSIZE, "1"), 8) return int(str, 10) def parseConstant(self, linked_node, register="R0"): node = linked_node.node -- cgit v1.2.1