From eb5d7d8785e01c3053b5792d7731e31031f9b60c Mon Sep 17 00:00:00 2001 From: Xun Yang <x299yang@uwaterloo.ca> Date: Thu, 9 Apr 2020 18:33:33 -0400 Subject: [PATCH] fieldAcc addr --- ExprPrimaryNodes.py | 74 +++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index 37aedd4..602fd37 100644 --- a/ExprPrimaryNodes.py +++ b/ExprPrimaryNodes.py @@ -353,7 +353,7 @@ class ClassCreateNode(ASTNode): def codeGen(self): if hasattr(self, "code"): - return + return self.code = "" # 1. Allocating space for the object in heap @@ -385,7 +385,7 @@ class ClassCreateNode(ASTNode): p(instruction="pop", arg1="eax", comment="eax now contains pointer to newly created object") self.code += ";End of object creation\n" - + ################################################################################# # condOrExpr class ExprNode(ASTNode): @@ -492,7 +492,7 @@ class ExprNode(ASTNode): # ebx = left result # eax = right result # so, basically do "eax = ebx op eax" - + # Binary operations: # Add, Subtract, Multiply if self.op in ['+', '-', '*']: @@ -681,45 +681,41 @@ class FieldAccessNode(ASTNode): # result stored in eax (address) # NOTE: this is always called by codeGen, so self.code is already intialized to "" def addr(self): - fieldNode = self.ID.prefixLink + result = "" + fieldNode = self.ID.prefixLink + + if "static" in fieldNode.mods: + label = fieldNode.typeName + "_" + fieldNode.name + result += "; Start of calculating address for static field: " + label + "\n" + \ + importHelper(fieldNode.typeName, self.typeName, "S_"+label) + \ + p(instruction="mov", arg1="eax", arg2="dword S_"+label, comment="getting address to static field") + \ + "; End of field access\n" + + else: + result += "; Start of calculating address for non-static field\n" + self.primary.codeGen() + result += self.primary.code + # Null check + # At this point, eax stores the address of the object + result += p(instruction="call", arg1="H__Null_Check", comment="calling null check function") + # Make eax store the address to the field we're accessing + result += p(instruction="add", arg1="eax", arg2=self.ID.prefixLink.offset, comment="calculating pointer to the field") + result += "; End of calculating address for non-static field\n" + + return result - 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") + \ - "; End of field access\n" - else: - self.code += "; Start of calculating address for non-static field\n" - self.primary.codeGen() - 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") - # 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 the field") - self.code += "; End of calculating address for non-static field\n" - - def codeGen(self): - if hasattr(self, "code"): - return - - fieldNode = self.ID.prefixLink - label = fieldNode.typeName + "_" + fieldNode.name - self.code = "; Accessing a field :" + label + "\n" - # Evaluating the address of the field we're trying to access - self.addr() - self.code += p(instruction="mov", arg1="eax", arg2="[eax]") + \ - "; End of field access\n" - - - - - - - + if hasattr(self, "code"): + return + + fieldNode = self.ID.prefixLink + label = fieldNode.typeName + "_" + fieldNode.name + self.code = "; Accessing a field :" + label + "\n" + # Evaluating the address of the field we're trying to access + self.code += self.addr() + self.code += p(instruction="mov", arg1="eax", arg2="[eax]") + \ + "; End of field access\n" ################################################################################### -- GitLab