From 98330d70d4697ddd74c82bda3d4fcf82ab083110 Mon Sep 17 00:00:00 2001 From: Xun Yang <x299yang@uwaterloo.ca> Date: Thu, 9 Apr 2020 21:59:51 -0400 Subject: [PATCH] genConstructors at classNode --- ExprPrimaryNodes.py | 2 +- MemberNodes.py | 7 +++++-- TypeNodes.py | 23 +++++++++++++---------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index a659669..80555aa 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 1d1a299..82658d6 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 241d4a8..eb4b588 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 ##################################################################### -- GitLab