From 14d0835e355eb6044f8fb090ec7a70b267bda48b Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sun, 27 Nov 2011 14:39:42 +0000 Subject: Support declaration of ariables with an initialization value --- pp2cc.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/pp2cc.py b/pp2cc.py index 35b208b..224a88f 100755 --- a/pp2cc.py +++ b/pp2cc.py @@ -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 -- cgit v1.2.1