summaryrefslogtreecommitdiff
path: root/Variables.py
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2011-12-03 13:55:31 +0000
committerPeter Wu <lekensteyn@gmail.com>2011-12-03 13:55:31 +0000
commitcbb589740133d53224fcd66c4c2decf43a7eca6e (patch)
tree3675a6cf2165af6d5c14b9bc6c5982facb7598cb /Variables.py
parent658bfe6c92b6dccea353da38a7651c61cd4ef1ce (diff)
downloadpp2cc-cbb589740133d53224fcd66c4c2decf43a7eca6e.tar.gz
Support multiple declarations in the global scope
Diffstat (limited to 'Variables.py')
-rw-r--r--Variables.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/Variables.py b/Variables.py
index 617113f..89c8501 100644
--- a/Variables.py
+++ b/Variables.py
@@ -84,7 +84,10 @@ class GlobalVariables(Variables):
defined in assembly
"""
self.defined_names = defined_names
+ # key: variable name, value: name as it appears in assembly
self.global_vars = {}
+ # a list of variable names which are declared and defined
+ self.global_vars_defined = []
def _uniqName(self, name):
"""Returns an unique global variable name for assembly"""
uniq_name = name
@@ -93,6 +96,12 @@ class GlobalVariables(Variables):
uniq_name = name + "_" + str(i)
i += 1
return uniq_name
+ def isDeclared(self, name):
+ """Returns True if the global variable is declared, False otherwise"""
+ return name in self.global_vars
+ def isDefined(self, name):
+ """Returns True if the global variable is defined, False otherwise"""
+ return name in self.global_vars_defined
def getAddress(self, name):
"""Gets the address for a variable as a tuple containing a register and
displacement
@@ -100,7 +109,7 @@ class GlobalVariables(Variables):
To get the address of the variable, add the register value and
displacement
"""
- if name in self.global_vars:
+ if self.isDeclared(name):
return ("GB", self.global_vars[name])
raise RuntimeError("Use of undefined variable '{}'".format(name))
def declName(self, name, size=1, is_param=False, is_static=False):
@@ -138,3 +147,13 @@ class GlobalVariables(Variables):
else:
# insert size items, initialized with 0
self.defined_names[var_name] = ["0"] * size
+ def defName(self, name):
+ """Marks a variable name as defined
+
+ Variables may be declared multiple times in the globa scope, but not
+ defined multiple times
+ """
+ assert self.isDeclared(name), "Definition of undeclared '{}'".format(name)
+ if self.isDefined(name):
+ raise RuntimeError("Redefinition of '{}'".format(name))
+ self.global_vars_defined.append(name)