From 20169afdb7ab4f43f806bcff100f2b2d9dccd459 Mon Sep 17 00:00:00 2001 From: Xun Yang <x299yang@uwaterloo.ca> Date: Thu, 9 Apr 2020 21:48:02 -0400 Subject: [PATCH] constructor --- ExprPrimaryNodes.py | 5 ++--- MemberNodes.py | 37 +++++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index 602fd37..a659669 100644 --- a/ExprPrimaryNodes.py +++ b/ExprPrimaryNodes.py @@ -373,10 +373,9 @@ class ClassCreateNode(ASTNode): self.code += p(instruction="push", arg1="eax", comment="pushing object as first argument") # Evaluate arguments and pushing parameters if self.args and hasattr(self.args, "codeGen"): - if not hasattr(self.args, "code"): - self.args.codeGen() + self.args.codeGen() self.code += self.args.code - label = "M_"+self.cons.typeName+"_"+self.cons.name+"_"+self.cons.paramTypes + label = "M_" + self.cons.typeName + "_" + 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 928a38b..1d1a299 100644 --- a/MemberNodes.py +++ b/MemberNodes.py @@ -82,7 +82,7 @@ class FieldNode(ASTNode): p(instruction="mov", arg1="[ebx]", arg2="eax", comment="eax is a pointer to field value in heap") self.code += ";End of declaration of static field\n" - + ########################################################### @@ -245,36 +245,49 @@ class MethodNode(ASTNode): if hasattr(self, "code") and self.code != "": return - + myClass = self.env.getNode(self.typeName, 'type') self.label = "M_" + self.typeName + "_" + self.paramTypes self.code = pLabel(self.typeName + "_" + self.paramTypes, "method") # label + thisLoc = len(self.params) * 4 + 8 + bodyCode = "" - # init fields - - # body code if self.body: - bodyCode = "" # push all local var to stack vars = getVarDclNodes(self.body) for i in range(len(vars)): vars[i].offset = i * 4 + 16 bodyCode += p("push", 0) + # call parent constructor + if myClass.superClass: + suLabel = "M_" + myClass.superClass.name + "_" + bodyCode += importHelper(myClass.superClass.name, self.typeName, suLabel) + bodyCode += p("mov", "eax", "[ebp - " + thisLoc + "]") + bodyCode += p("push", "eax", None, "# Pass THIS as argument to superClass.") + bodyCode += p("call", suLabel) + + + # init fields + fields = sorted(myClass.fields, key=lambda x: x.order) + for f in fields: + if not 'static' in field.mods and f.variableDcl.variableInit: + f.variableDcl.variableInit.right.codeGen() + bodyCode += f.variableDcl.variableInit.right.code + bodyCode += p("mov", "ebx", "[ebp - " + thisLoc + "]") # THIS + bodyCode += p("mov", "[ebx + " + f.offset + " ]", "eax") + + # body code + if self.body: self.body.codeGen() bodyCode += self.body.code - bodyCode += self.label + "_end: ; end of method for " + self.name + "\n" # pop off all the local var for i in range(len(vars)): bodyCode += p("pop", "edx") - self.code += genProcedure(bodyCode, "method definition for " + self.name) - else: - self.code += p("ret", "") - - + self.code += genProcedure(bodyCode, "Constructor definition for " + self.name + " " + self.paramTypes) ############# helper for forward ref checking ######## # Input: AST Node -- GitLab