diff options
author | Peter Wu <lekensteyn@gmail.com> | 2011-11-27 14:39:42 +0000 |
---|---|---|
committer | Peter Wu <lekensteyn@gmail.com> | 2011-11-27 14:39:42 +0000 |
commit | 14d0835e355eb6044f8fb090ec7a70b267bda48b (patch) | |
tree | ee6af628714c78255b2c1b3e42ea79a020c9c6a4 | |
parent | 2224c3d82acc392bd380de352305a6da6cebf274 (diff) | |
download | pp2cc-14d0835e355eb6044f8fb090ec7a70b267bda48b.tar.gz |
Support declaration of ariables with an initialization value
-rwxr-xr-x | pp2cc.py | 28 |
1 files changed, 26 insertions, 2 deletions
@@ -240,8 +240,12 @@ class Parse(object): } self.binary_ops.update(self.comparison_ops) + # global variable names to be defined in @DATA self.globalVars = [] self.functions = {} + # holds instructions for initializing the globalVars + self.globalInit = [] + # holds the miscellaneous instructions for @CODE self.codeSegment = [] self.labels = set() # helper for asm code generation @@ -285,9 +289,16 @@ class Parse(object): self.addSource(self.nameToVar(varName) + padding + " DW 1") self.addSource() self.addSource("@CODE") - for line in self.codeSegment: + # initialization of global variables + for line in self.globalInit: self.addSource(line) + + if not "main" in self.functions: + self.logger.warning("No main function found with label 'fn_main'") + self.addSource(self.asm.branch_op("BRA", "fn_main")) self.addSource() + for line in self.codeSegment: + self.addSource(line) self.addSource() self.addSource("@END") def addSource(self, line=''): @@ -808,11 +819,24 @@ class Parse(object): raise RuntimeError("Unsupported assignment operator: " + node.op) return lines def parseDecl(self, linked_node): + lines = [] varname = linked_node.node.name if varname in self.globalVars: raise RuntimeError("Redefinition of variable '{}'".format(varname)) self.globalVars.append(varname) - return [] + + # if the declaration also has a definition + if linked_node.node.init: + var_name = self.nameToVar(varname) + init_val = self.parseStatement(linked_node.node.init, linked_node) + result_reg = self.registers.find_register(init_val) + init_val.append(self.asm.binary_op("STOR", result_reg, "[GB+" + var_name + "]")) + # if global context (i.e. not in a function) + if linked_node.getFunctionNode() is None: + self.globalInit += init_val + else: + lines += init_val + return lines def parseStatement(self, node, parent_linked_node, level_increment=False): linked_node = LinkedNode(node, parent_linked_node, level_increment=level_increment) self.asm.level = linked_node.level |