diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index 6fc1f9220b2dc2b868143289e7ec7c092edf8d5c..015d1196df10a6fcde3a959f28280c464ea594ca 100644 --- a/ExprPrimaryNodes.py +++ b/ExprPrimaryNodes.py @@ -293,7 +293,6 @@ class CastNode(ASTNode): def checkType(self): self.left.checkType() - from pprint import pprint self.right.disambigName() self.right.checkType() if (self.left.myType.isNum() and self.right.myType.isNum()) \ @@ -308,22 +307,22 @@ class CastNode(ASTNode): return self.code = "" - print(self.left.myType.name, self.typeName) - offset = self.left.myType.typePointer.subTypeOffset - # get object at right self.code += "; casting\n" self.right.codeGen() self.code += self.right.code - # subtype test - 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 - self.code += p("mov", "ebx", "[ebx + " + str(offset) + "]") # ebx has isSubtype - - # exception if not subtype, else do nothing (object is already in eax) - self.code += p("cmp", "[G__Zero]", "ebx") - self.code += p("je", "H__Throw_Exception") + # subtype test: + if not self.left.myType.isPrimitive: + # only test if right is not primitive (primitive types would be correctly static tested already) + 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 + self.code += p("mov", "ebx", "[ebx + " + str(offset) + "]") # ebx has isSubtype + + # exception if not subtype, else do nothing (object is already in eax) + self.code += p("cmp", "[G__Zero]", "ebx") + self.code += p("je", "H__Throw_Exception") self.code += "; end of casting\n" ################################################################################### @@ -500,14 +499,22 @@ class ExprNode(ASTNode): # instanceOf if self.op == "instanceof": - offset = self.right.myType.typePointer.subTypeOffset - self.code += ("; evaluate instanceof\n") + self.code += ("; evaluate instanceof\n") self.left.codeGen() self.code += self.left.code - self.code += p("mov", "eax", "[eax]") # access class tag of left object - self.code += p("mov", "eax", "[eax + 4]") # access subtype testing column - self.code += p("mov", "eax", "[eax + " + str(offset) + "]") # subType column stores 0 or 1 already + + # only test if non-primitive + if not self.right.myType.isPrimitive: + 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 + self.code += p("mov", "eax", "[eax + " + str(offset) + "]") # subType column stores 0 or 1 already + else: # primitive type can be staticly evaluated + if self.right.myType.assignable(self.left.myType): + self.code += p("mov", "eax", "1") + else: + self.code += p("mov", "eax", "0") self.code += ("; end of instanceof\n") return @@ -614,8 +621,6 @@ class ExprNode(ASTNode): self.code += p('pop', 'edx') # restore edx return - # if self.op == 'instanceof': TODO - # return # generate shorter code if self.op = comparison def getIfFalse(self, label):