Skip to content
Snippets Groups Projects
Commit df155422 authored by Pui Yan Chloe Sham's avatar Pui Yan Chloe Sham
Browse files

Merge branch 'field-init' into 'CodeGenField'

change fieldAccessNode.addr(), change classCreate code Gen, will init field...

See merge request !11
parents baee5e15 e92214f7
No related branches found
No related tags found
2 merge requests!14Insta field,!11change fieldAccessNode.addr(), change classCreate code Gen, will init field...
......@@ -368,16 +368,6 @@ class ClassCreateNode(ASTNode):
self.code += importHelper(classDef.name, self.typeName, "C_"+classDef.name)
self.code += p(instruction="mov", arg1="[eax]", arg2="[C_"+classDef.name+"]", comment="first item is vtable pointer")
# 3. Filling up the object memory layout with pointers to fields
# Note: the key of the offset table is of the form (className, fieldName)
self.code += "; Filling in object memory layout with pointers to fields\n"
for key,value in sorted(fieldOffset.items(), key=lambda item: item[1]):
label = "F_"+key[0]+"_"+key[1]
self.code += importHelper(key[0], self.typeName, label)
self.code += p(instruction="call", arg1=label, comment="calling function to allocate space on heap") + \
p(instruction="mov", arg1="[eax"+str(value)+"]", arg2="ecx", comment="filling in field on object memory for "+label)
self.code += "; End of filling in object memory layout\n"
# 4. Calling constructor
self.code += "; Calling constructor for object\n"
self.code += p(instruction="push", arg1="eax", comment="pushing object as first argument")
......@@ -692,13 +682,12 @@ class FieldAccessNode(ASTNode):
# NOTE: this is always called by codeGen, so self.code is already intialized to ""
def addr(self):
fieldNode = self.ID.prefixLink
label = fieldNode.typeName + "_" + fieldNode.name
if "static" in fieldNode.mods:
label = fieldNode.typeName + "_" + fieldNode.name
self.code += "; Start of calculating address for static field: " + label + "\n" + \
importHelper(fieldNode.typeName, self.typeName, "S_"+label) + \
p(instruction="mov", arg1="eax", arg2="[S_"+label+"]", comment="getting address to static field") + \
p(instruction="mov", arg1="eax", arg2="S_"+label, comment="getting address to static field") + \
"; End of field access\n"
else:
......@@ -707,10 +696,9 @@ class FieldAccessNode(ASTNode):
self.code += self.primary.code
# Null check
# At this point, eax stores the address of the object
self.code += p(instruction="call", arg1="H__Null_Check", comment="calling null check function")
self.code += p(instruction="call", arg1="H__Null_Check", comment="calling null check function")
# Make eax store the address to the field we're accessing
self.code += p(instruction="add", arg1="eax", arg2=self.ID.prefixLink.offset, comment="calculating pointer to a pointer to the field") + \
p(instruction="mov", arg1="eax", arg2="[eax]", comment="storing pointer to the field")
self.code += p(instruction="add", arg1="eax", arg2=self.ID.prefixLink.offset, comment="calculating pointer to the field")
self.code += "; End of calculating address for non-static field\n"
......
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