Skip to content
Snippets Groups Projects
Commit 98330d70 authored by Xun Yang's avatar Xun Yang
Browse files

genConstructors at classNode

parent 20169afd
No related branches found
No related tags found
1 merge request!14Insta field
...@@ -375,7 +375,7 @@ class ClassCreateNode(ASTNode): ...@@ -375,7 +375,7 @@ class ClassCreateNode(ASTNode):
if self.args and hasattr(self.args, "codeGen"): if self.args and hasattr(self.args, "codeGen"):
self.args.codeGen() self.args.codeGen()
self.code += self.args.code 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 += importHelper(classDef.name, self.typeName, label)
self.code += p(instruction="call", arg1=label, comment="Calling constructor") self.code += p(instruction="call", arg1=label, comment="Calling constructor")
......
...@@ -238,6 +238,7 @@ class MethodNode(ASTNode): ...@@ -238,6 +238,7 @@ class MethodNode(ASTNode):
self.code += genProcedure(bodyCode, "method definition for " + self.name) self.code += genProcedure(bodyCode, "method definition for " + self.name)
else: else:
self.code += ("mov", "eax", "0")
self.code += p("ret", "") self.code += p("ret", "")
# This method is called instead of codeGen if this is a constructor # This method is called instead of codeGen if this is a constructor
...@@ -247,8 +248,8 @@ class MethodNode(ASTNode): ...@@ -247,8 +248,8 @@ class MethodNode(ASTNode):
myClass = self.env.getNode(self.typeName, 'type') myClass = self.env.getNode(self.typeName, 'type')
self.label = "M_" + self.typeName + "_" + self.paramTypes self.label = "M_" + self.typeName + "_" + self.name + "_" + self.paramTypes
self.code = pLabel(self.typeName + "_" + self.paramTypes, "method") # label self.code = pLabel(self.typeName + "_" + self.name + "_" + self.paramTypes, "method") # label
thisLoc = len(self.params) * 4 + 8 thisLoc = len(self.params) * 4 + 8
bodyCode = "" bodyCode = ""
...@@ -286,6 +287,8 @@ class MethodNode(ASTNode): ...@@ -286,6 +287,8 @@ class MethodNode(ASTNode):
# pop off all the local var # pop off all the local var
for i in range(len(vars)): for i in range(len(vars)):
bodyCode += p("pop", "edx") bodyCode += p("pop", "edx")
else:
bodyCode += ("mov", "eax", "0")
self.code += genProcedure(bodyCode, "Constructor definition for " + self.name + " " + self.paramTypes) self.code += genProcedure(bodyCode, "Constructor definition for " + self.name + " " + self.paramTypes)
......
...@@ -289,12 +289,13 @@ class ClassNode(ClassInterNode): ...@@ -289,12 +289,13 @@ class ClassNode(ClassInterNode):
# Adding in fields that are not inherited from parent class to offset table # Adding in fields that are not inherited from parent class to offset table
# Note: excluding static fields # 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: if not 'static' in field.mods:
lastFieldOffset += 4 lastFieldOffset += 4
self.fieldOffset[(self.name,field.name)] = lastFieldOffset self.fieldOffset[(self.name,field.name)] = lastFieldOffset
field.offset = lastFieldOffset field.offset = lastFieldOffset
# Calculating the size of objects of this class # Calculating the size of objects of this class
self.size += (len(self.fieldOffset))*4 self.size += (len(self.fieldOffset))*4
...@@ -302,7 +303,7 @@ class ClassNode(ClassInterNode): ...@@ -302,7 +303,7 @@ class ClassNode(ClassInterNode):
def codeGen(self): def codeGen(self):
if hasattr(self, "code"): if hasattr(self, "code"):
return return
self.code = "" self.code = ""
# print("This is the super class: {}".format(self.superClass)) # print("This is the super class: {}".format(self.superClass))
...@@ -352,8 +353,8 @@ class ClassNode(ClassInterNode): ...@@ -352,8 +353,8 @@ class ClassNode(ClassInterNode):
self.methodOffset[(method.name, method.paramTypes)] = lastMethodOffset self.methodOffset[(method.name, method.paramTypes)] = lastMethodOffset
self.code += pLabel(name=self.name + "_" + method.name + "_" + method.paramTypes, type="vtable") 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 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: for i in self.inherits:
if isinstance(i, MethodNode): if isinstance(i, MethodNode):
key = (i.name, i.paramTypes) key = (i.name, i.paramTypes)
...@@ -392,11 +393,13 @@ class ClassNode(ClassInterNode): ...@@ -392,11 +393,13 @@ class ClassNode(ClassInterNode):
for c in self.children: for c in self.fields + self.methods:
if c and hasattr(c, "codeGen"): c.codeGen()
if not hasattr(c, "code"): # children hasn't generated code yet self.code += c.code
c.codeGen()
self.code += c.code for c in self.constructors:
c.codeGenConstructor()
self.code += c.code
##################################################################### #####################################################################
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment