summaryrefslogtreecommitdiff
path: root/pp2cc.py
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2011-12-02 20:28:29 +0000
committerPeter Wu <lekensteyn@gmail.com>2011-12-02 20:28:29 +0000
commitd48fb25678e10c79bf8971892b8573fb7cb9e247 (patch)
tree61138630c541427793ff40bbc1c1e342539f7328 /pp2cc.py
parenta6f9617bd891074bba20cc1fc220e60a226e7fb4 (diff)
downloadpp2cc-d48fb25678e10c79bf8971892b8573fb7cb9e247.tar.gz
Use DW instead of DS, add ASM source, fix some coding errors
Diffstat (limited to 'pp2cc.py')
-rwxr-xr-xpp2cc.py24
1 files changed, 19 insertions, 5 deletions
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