From 29f430dc3c911971e62819292ceb02fa79d514a7 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 22 Feb 2013 15:40:49 +0100 Subject: python3, pycparser 2.06 compatibility --- AsmParser.py | 4 ++-- Registers.py | 2 +- pp2cc.py | 46 ++++++++++++++++++++++++++-------------------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/AsmParser.py b/AsmParser.py index c3354dd..13b41b8 100644 --- a/AsmParser.py +++ b/AsmParser.py @@ -65,7 +65,7 @@ class AsmParser(object): break file.close() # substitute constants and remove the name - for name, value in self.constants.iteritems(): + for name, value in self.constants.items(): if name in self.defined_names: self.defined_names[name].rename(value) del self.defined_names[name] @@ -184,4 +184,4 @@ class AsmParser(object): return [str(elm) for elm in self.code] def getDataDefinitions(self): """Returns the data section as a dictionary""" - return self.data \ No newline at end of file + return self.data diff --git a/Registers.py b/Registers.py index 7fac4dd..13149fb 100644 --- a/Registers.py +++ b/Registers.py @@ -153,7 +153,7 @@ class Registers(object): elif len(matches) == 3: instruction, reg, operand = matches # remove whitespace from the operand. LF and CR do not occur - operand = operand.translate(None, "\t ") + operand = operand.replace("\t", "").replace(" ", "") if (operand.startswith("[--" + register) or operand.endswith(register + "++]")): return True diff --git a/pp2cc.py b/pp2cc.py index 6205e94..c2f5029 100755 --- a/pp2cc.py +++ b/pp2cc.py @@ -37,14 +37,14 @@ class Logger(object): def error(self, message, linked_node=None): self.log(message, linked_node=linked_node, type="error") while linked_node: - print " in", linked_node.node.coord, linked_node.type + print(" in {} {}".format(linked_node.node.coord, linked_node.type)) linked_node = linked_node.parent raise def log(self, message, linked_node=None, type="log"): source = "" if isinstance(linked_node, LinkedNode): source = str(linked_node.getLocation()) + ": " - print type + ":" + source, message + print("{}:{} {}".format(type, source, message)) class Parse(object): def __init__(self): @@ -151,8 +151,8 @@ class Parse(object): str(thing), linked_node=linked_node) self.codeSegment += self.parseStatement(thing, root_node) # hack: static functions are local to a file, so remove them - for funcname, function in self.functions.items(): - if function.isStatic(): + for funcname in list(self.functions.keys()): + if self.functions[funcname].isStatic(): del self.functions[funcname] def compileASM(self): """Processes lines of assembly and merge it into the output""" @@ -163,7 +163,7 @@ class Parse(object): new_label = self.uniqLbl(label) self.asm_node.renameId(label, new_label) self.labels.add(new_label) - for name, init_vals in self.asm_node.getDataDefinitions().iteritems(): + for name, init_vals in self.asm_node.getDataDefinitions().items(): if name in self.varNames: old_count = len(self.varNames[name]) new_count = len(init_vals) @@ -182,7 +182,7 @@ class Parse(object): """Retrieves the ASM source. You need to compile it first""" output = [] output.append("@DATA") - for varName, initializers in self.varNames.iteritems(): + for varName, initializers in self.varNames.items(): padding = " " * (16 - len(varName) - 1) assert len(initializers) > 0, "Size of '{}' must be at least 1".format(varName) output.append(varName + padding + " DW " + ",".join(initializers)) @@ -1151,21 +1151,28 @@ class Parse(object): linked_cn)) except RuntimeError: e = sys.exc_info()[1] - print "case label does not reduce to an integer constant" - print e + print("case label does not reduce to an integer constant") + print(e) raise lines_cases.append(self.asm.binary_op("CMP", switch_reg, case_val)) lines_cases.append(self.asm.branch_op("BEQ", lbl_case)) lines_stmts.append(self.asm.noop(lbl_case)) - linked_cn = LinkedNode(cn.stmt, linked_cn.parent) + if hasattr(cn, "stmt"): # pycparser-2.05 + linked_cn = LinkedNode(cn.stmt, linked_cn.parent) elif linked_cn.type == "Default": lbl_default = self.uniqLbl("default") lines_stmts.append(self.asm.noop(lbl_default)) - linked_cn = LinkedNode(cn.stmt, linked_cn.parent) + if hasattr(cn, "stmt"): # pycparser-2.05 + linked_cn = LinkedNode(cn.stmt, linked_cn.parent) if linked_cn.type in ("Case", "Default"): - # nested case/default labels like case 1: case 2: stmt; - pass + # pycparser-2.05: nested case/default labels like case 1: case 2: stmt; + if hasattr(cn, "stmts"): + # pycparser-2.06: may be empty if there is a following case + for node in cn.stmts: + # regular body + lines_stmts += self.parseStatement(node, linked_cn) + linked_cn = None else: # regular body lines_stmts += self.parseStatement(linked_cn.node, linked_cn.parent) @@ -1411,7 +1418,7 @@ if __name__ == "__main__": class ArgumentError(Exception): pass def usage(): - print """Usage: pp2cc.py [options] filename.. + print("""Usage: pp2cc.py [options] filename.. Multiple input files can be specified, options can be specified before and after filenames. Options: @@ -1425,8 +1432,7 @@ Options: adding #define name or #define name definition respectively -U name This option is passed to the cpp program and acts like adding #undef name - --no-cpp Disable the use of the C Preprocessor - """ + --no-cpp Disable the use of the C Preprocessor""") try: for arg in sys.argv[1:]: if not set_arg is None: @@ -1470,8 +1476,8 @@ Options: raise ArgumentError("No input files") except ArgumentError: e = sys.exc_info()[1] - print str(e) - print "For usage, run: python pp2cc.py --help" + print(e) + print("For usage, run: python pp2cc.py --help") sys.exit(255) # end of arguments processing @@ -1494,9 +1500,9 @@ Options: outf = open(settings["output_filename"], "w") outf.write(source) outf.close() - print "Compilation succesful" - print "The output is written to", settings["output_filename"] + print("Compilation succesful") + print("The output is written to " + settings["output_filename"]) except c_parser.ParseError: e = sys.exc_info()[1] - print "A fatal (syntax) error was found during parsing:", str(e) + print("A fatal (syntax) error was found during parsing:" + str(e)) sys.exit(1) -- cgit v1.2.1