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

cast instanceof for primitive types

parent 5cf8a149
No related branches found
No related tags found
2 merge requests!22Subtype test,!20Master
...@@ -293,7 +293,6 @@ class CastNode(ASTNode): ...@@ -293,7 +293,6 @@ class CastNode(ASTNode):
def checkType(self): def checkType(self):
self.left.checkType() self.left.checkType()
from pprint import pprint
self.right.disambigName() self.right.disambigName()
self.right.checkType() self.right.checkType()
if (self.left.myType.isNum() and self.right.myType.isNum()) \ if (self.left.myType.isNum() and self.right.myType.isNum()) \
...@@ -308,22 +307,22 @@ class CastNode(ASTNode): ...@@ -308,22 +307,22 @@ class CastNode(ASTNode):
return return
self.code = "" self.code = ""
print(self.left.myType.name, self.typeName)
offset = self.left.myType.typePointer.subTypeOffset
# get object at right # get object at right
self.code += "; casting\n" self.code += "; casting\n"
self.right.codeGen() self.right.codeGen()
self.code += self.right.code self.code += self.right.code
# subtype test # subtype test:
self.code += p("mov", "ebx", "[eax]", "start subtype test") # access class tag of left object if not self.left.myType.isPrimitive:
self.code += p("mov", "ebx", "[ebx + 4]") # access subtype testing column # only test if right is not primitive (primitive types would be correctly static tested already)
self.code += p("mov", "ebx", "[ebx + " + str(offset) + "]") # ebx has isSubtype offset = self.left.myType.typePointer.subTypeOffset
self.code += p("mov", "ebx", "[eax]", "start subtype test") # access class tag of left object
# exception if not subtype, else do nothing (object is already in eax) self.code += p("mov", "ebx", "[ebx + 4]") # access subtype testing column
self.code += p("cmp", "[G__Zero]", "ebx") self.code += p("mov", "ebx", "[ebx + " + str(offset) + "]") # ebx has isSubtype
self.code += p("je", "H__Throw_Exception")
# 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" self.code += "; end of casting\n"
################################################################################### ###################################################################################
...@@ -500,14 +499,22 @@ class ExprNode(ASTNode): ...@@ -500,14 +499,22 @@ class ExprNode(ASTNode):
# instanceOf # instanceOf
if self.op == "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.left.codeGen()
self.code += self.left.code 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 # only test if non-primitive
self.code += p("mov", "eax", "[eax + " + str(offset) + "]") # subType column stores 0 or 1 already 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") self.code += ("; end of instanceof\n")
return return
...@@ -614,8 +621,6 @@ class ExprNode(ASTNode): ...@@ -614,8 +621,6 @@ class ExprNode(ASTNode):
self.code += p('pop', 'edx') # restore edx self.code += p('pop', 'edx') # restore edx
return return
# if self.op == 'instanceof': TODO
# return
# generate shorter code if self.op = comparison # generate shorter code if self.op = comparison
def getIfFalse(self, label): def getIfFalse(self, label):
......
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