From d48fb25678e10c79bf8971892b8573fb7cb9e247 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 2 Dec 2011 20:28:29 +0000 Subject: Use DW instead of DS, add ASM source, fix some coding errors --- pp2cc.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'pp2cc.py') diff --git a/pp2cc.py b/pp2cc.py index ffe9ee4..d3aac94 100755 --- a/pp2cc.py +++ b/pp2cc.py @@ -80,7 +80,8 @@ class Parse(object): self.binary_ops.update(self.comparison_ops) self.binary_ops.update(self.shift_ops) - # local and global variable names to be defined in @DATA + # global variable names to be defined in @DATA. key: name, value: list + # of initializers self.varNames = {} self.functions = {} # holds instructions for initializing the global variables @@ -158,16 +159,29 @@ 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(): + if name in self.varNames: + old_count = len(self.varNames[name]) + new_count = len(init_vals) + if old_count != new_count: + self.logger.error("Global variable '{}' was defined before" + ", but the length of the initializers" + " are not equal. Old: {}, new: {}" + .format(name, old_count, new_count)) + elif self.varNames[name] != init_vals: + self.logger.warning("The initialization of global variable" + " '{}' contains different values" + .format(name)) + self.varNames[name] = init_vals self.codeSegment += self.asm_node.getCodeLines() def getSource(self): """Retrieves the ASM source. You need to compile it first""" output = [] output.append("@DATA") - for varName, size in self.varNames.iteritems(): + for varName, initializers in self.varNames.iteritems(): padding = " " * (16 - len(varName) - 1) - assert size > 0, "Size of '{}' must be at least 1".format(varName) - initializers = "0,".repeat(size)[0:-1] - output.append(varName + padding + " DW " + initializers) + assert len(initializers) > 0, "Size of '{}' must be at least 1".format(varName) + output.append(varName + padding + " DW " + ",".join(initializers)) output.append("") output.append("@CODE") # initialization of global variables -- cgit v1.2.1