From 2b8b7207d8368ef20994f77eda5e6ce58015f9be Mon Sep 17 00:00:00 2001 From: Xun Yang <x299yang@uwaterloo.ca> Date: Mon, 13 Apr 2020 01:09:35 -0400 Subject: [PATCH] array cast instance of --- ExprPrimaryNodes.py | 9 +++++---- TypeNodes.py | 14 +++----------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index 1ff131b..90c413b 100644 --- a/ExprPrimaryNodes.py +++ b/ExprPrimaryNodes.py @@ -387,8 +387,9 @@ class CastNode(ASTNode): self.code += self.right.code # subtype test: - if not self.left.myType.isPrimitive: - # only test if right is not primitive (primitive types would be correctly static tested already) + if (not self.left.myType.isPrimitive) and self.right.myType.isArray == self.left.myType.isArray: + # only test if not primitive, both are array or both non array + # cast would've exception if array is casting to non-array and not Object offset = self.left.myType.typePointer.subTypeOffset self.code += p("mov", "ebx", "[eax]", "start subtype test") # access class tag of left object self.code += p("mov", "ebx", "[ebx + 4]") # access subtype testing column @@ -578,8 +579,8 @@ class ExprNode(ASTNode): self.left.codeGen() self.code += self.left.code - # only test if non-primitive - if not self.right.myType.isPrimitive: + # only test if non-primitive, both arrays or both non arrays + if (not self.right.myType.isPrimitive) and self.right.myType.isArray == self.left.myType.isArray: offset = self.right.myType.typePointer.subTypeOffset self.code += p("mov", "eax", "[eax]") # access class tag of left object self.code += p("mov", "eax", "[eax + 4]") # access subtype testing column diff --git a/TypeNodes.py b/TypeNodes.py index e3bca58..b621d53 100644 --- a/TypeNodes.py +++ b/TypeNodes.py @@ -19,8 +19,7 @@ class ClassInterNode(ASTNode): self.SITsize = 0 self.subTypeSize = 0 self.subTypeOffset = 0 - self.arrayTypeOffset = 0 # only array assignable classes will have this field set - + # sets self.inherits = [] self.super = [] @@ -308,13 +307,6 @@ class ClassNode(ClassInterNode): # Calculating the size of objects of this class self.size += (len(self.fieldOffset))*4 - assignable = dict({"java.lang.Object" : 4, - "java.lang.Cloneable" : 8, - "java.io.Serializable" : 12 - }) # fix offsets for array assignable classes, 0 row is reserve for all the non assignable ones - if self.canonName in assignable.keys(): - self.arrayTypeOffset = assignable[self.canonName] - # Populating method offset and creating class memory layout def populateMethodOffset(self): if hasattr(self, "data"): @@ -392,7 +384,7 @@ class ClassNode(ClassInterNode): self.data += p("dd", "42") # Layout subtype testing column - self.data += pLabel("subT_" + self.name, "inter") + self.data += pLabel("subT_" + self.name, "array") for i in range(self.subTypeSize): self.data += pLabel("subT_" + str(i), "inter") self.data += p("dd", "0") # 0 for False, 1 for True @@ -451,7 +443,7 @@ class ClassNode(ClassInterNode): # fill in subtype testing column self.code += "\n; Filling in subtype testing\n" self.code += p("mov", "eax", "I_subT_spot_" + self.name) - self.code += p("mov", "[eax]", "dword I_subT_" + self.name) + self.code += p("mov", "[eax]", "dword A_subT_" + self.name) for t in getSupers(self): # getSupers includes self dlabel = "I_subT_" + str(t.subTypeOffset // 4) -- GitLab