summaryrefslogtreecommitdiff
path: root/pp2cc.py
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2011-11-27 14:39:42 +0000
committerPeter Wu <lekensteyn@gmail.com>2011-11-27 14:39:42 +0000
commit14d0835e355eb6044f8fb090ec7a70b267bda48b (patch)
treeee6af628714c78255b2c1b3e42ea79a020c9c6a4 /pp2cc.py
parent2224c3d82acc392bd380de352305a6da6cebf274 (diff)
downloadpp2cc-14d0835e355eb6044f8fb090ec7a70b267bda48b.tar.gz
Support declaration of ariables with an initialization value
Diffstat (limited to 'pp2cc.py')
-rwxr-xr-xpp2cc.py28
1 files 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