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

cast and instanceof

parent 2f31b4d1
No related branches found
No related tags found
2 merge requests!22Subtype test,!20Master
...@@ -303,6 +303,29 @@ class CastNode(ASTNode): ...@@ -303,6 +303,29 @@ class CastNode(ASTNode):
return return
raise Exception("ERROR: Cannot cast type {} to type {}.".format(self.right.myType.name, self.left.myType.name)) raise Exception("ERROR: Cannot cast type {} to type {}.".format(self.right.myType.name, self.left.myType.name))
def codeGen(self):
if hasattr(self, "code") and self.code != "":
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")
self.code += "; end of casting\n"
################################################################################### ###################################################################################
# unqualCreate NEW name LPAREN args RPAREN # unqualCreate NEW name LPAREN args RPAREN
class ClassCreateNode(ASTNode): class ClassCreateNode(ASTNode):
...@@ -475,6 +498,20 @@ class ExprNode(ASTNode): ...@@ -475,6 +498,20 @@ class ExprNode(ASTNode):
return return
self.code = "" self.code = ""
# instanceOf
if self.op == "instanceof":
offset = self.right.myType.typePointer.subTypeOffset
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
self.code += ("; end of instanceof\n")
return
# Unary operations: # Unary operations:
if not self.left: if not self.left:
# get right output # get right output
......
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