From cbb589740133d53224fcd66c4c2decf43a7eca6e Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sat, 3 Dec 2011 13:55:31 +0000 Subject: Support multiple declarations in the global scope --- Variables.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'Variables.py') 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) -- cgit v1.2.1