diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index a659669a16140feccece4992bdb8d4ffe26d2de4..80555aae2f13d1695661504847a905692d16d827 100644 --- a/ExprPrimaryNodes.py +++ b/ExprPrimaryNodes.py @@ -375,7 +375,7 @@ class ClassCreateNode(ASTNode): if self.args and hasattr(self.args, "codeGen"): self.args.codeGen() self.code += self.args.code - label = "M_" + self.cons.typeName + "_" + self.cons.paramTypes + label = "M_" + self.cons.typeName + "_" + self.cons.name + "_" + self.cons.paramTypes self.code += importHelper(classDef.name, self.typeName, label) self.code += p(instruction="call", arg1=label, comment="Calling constructor") diff --git a/MemberNodes.py b/MemberNodes.py index 1d1a299ec5966cb2039795c10e9e63021743b89d..82658d652dcd21a91640103a95d90f27bc566782 100644 --- a/MemberNodes.py +++ b/MemberNodes.py @@ -238,6 +238,7 @@ class MethodNode(ASTNode): self.code += genProcedure(bodyCode, "method definition for " + self.name) else: + self.code += ("mov", "eax", "0") self.code += p("ret", "") # This method is called instead of codeGen if this is a constructor @@ -247,8 +248,8 @@ class MethodNode(ASTNode): myClass = self.env.getNode(self.typeName, 'type') - self.label = "M_" + self.typeName + "_" + self.paramTypes - self.code = pLabel(self.typeName + "_" + self.paramTypes, "method") # label + self.label = "M_" + self.typeName + "_" + self.name + "_" + self.paramTypes + self.code = pLabel(self.typeName + "_" + self.name + "_" + self.paramTypes, "method") # label thisLoc = len(self.params) * 4 + 8 bodyCode = "" @@ -286,6 +287,8 @@ class MethodNode(ASTNode): # pop off all the local var for i in range(len(vars)): bodyCode += p("pop", "edx") + else: + bodyCode += ("mov", "eax", "0") self.code += genProcedure(bodyCode, "Constructor definition for " + self.name + " " + self.paramTypes) diff --git a/TypeNodes.py b/TypeNodes.py index 241d4a8162353a7b092a892fa39830764f230f27..eb4b588350b0281530c62e88a5988c92863b60d9 100644 --- a/TypeNodes.py +++ b/TypeNodes.py @@ -289,12 +289,13 @@ class ClassNode(ClassInterNode): # Adding in fields that are not inherited from parent class to offset table # Note: excluding static fields - for field in self.fields: + fields = sorted(self.fields, key=lambda x: x.order) + for field in fields: if not 'static' in field.mods: lastFieldOffset += 4 self.fieldOffset[(self.name,field.name)] = lastFieldOffset field.offset = lastFieldOffset - + # Calculating the size of objects of this class self.size += (len(self.fieldOffset))*4 @@ -302,7 +303,7 @@ class ClassNode(ClassInterNode): def codeGen(self): if hasattr(self, "code"): return - + self.code = "" # print("This is the super class: {}".format(self.superClass)) @@ -352,8 +353,8 @@ class ClassNode(ClassInterNode): self.methodOffset[(method.name, method.paramTypes)] = lastMethodOffset self.code += pLabel(name=self.name + "_" + method.name + "_" + method.paramTypes, type="vtable") self.code += p(instruction="dd", arg1=64) # just declaring a memory segment with a random number - - # Adding inherited method to the methodDict + + # Adding inherited method to the methodDict for i in self.inherits: if isinstance(i, MethodNode): key = (i.name, i.paramTypes) @@ -392,11 +393,13 @@ class ClassNode(ClassInterNode): - for c in self.children: - if c and hasattr(c, "codeGen"): - if not hasattr(c, "code"): # children hasn't generated code yet - c.codeGen() - self.code += c.code + for c in self.fields + self.methods: + c.codeGen() + self.code += c.code + + for c in self.constructors: + c.codeGenConstructor() + self.code += c.code #####################################################################