diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index 602fd37af7fc0c7628f3a3ec2fad78bbd56befcc..a659669a16140feccece4992bdb8d4ffe26d2de4 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 928a38b60ec71032e24409e43cea9f823862d15d..1d1a299ec5966cb2039795c10e9e63021743b89d 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