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

fix array cast check

parent a9969f2d
No related branches found
No related tags found
2 merge requests!30New new string,!20Master
...@@ -414,17 +414,24 @@ class CastNode(ASTNode): ...@@ -414,17 +414,24 @@ class CastNode(ASTNode):
self.code += self.right.code self.code += self.right.code
# subtype test: # subtype test:
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
self.code += p("mov", "ebx", "[ebx + " + str(offset) + "]") # ebx has isSubtype
# exception if not subtype, else do nothing (object is already in eax) # only test if not primitive
self.code += p("cmp", "[G__Zero]", "ebx") if self.right.myType.isArray == self.left.myType.isArray:
self.code += p("je", "H__Throw_Exception") if (not self.left.myType.isPrimitive):
# 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
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")
else: # one is array, one is Object/Serializable/Cloneable
# make sure cast only one way
if self.left.myType.isArray:
self.code += p("jmp", "H__Throw_Exception")
self.code += "; end of casting\n" self.code += "; end of casting\n"
################################################################################### ###################################################################################
...@@ -667,16 +674,27 @@ class ExprNode(ASTNode): ...@@ -667,16 +674,27 @@ class ExprNode(ASTNode):
self.code += self.left.code self.code += self.left.code
# only test if non-primitive, both arrays or both non arrays # 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: if self.right.myType.isArray == self.left.myType.isArray:
offset = self.right.myType.typePointer.subTypeOffset if not self.right.myType.isPrimitive:
self.code += p("mov", "eax", "[eax]") # access class tag of left object offset = self.right.myType.typePointer.subTypeOffset
self.code += p("mov", "eax", "[eax + 4]") # access subtype testing column self.code += p("mov", "eax", "[eax]") # access class tag of left object
self.code += p("mov", "eax", "[eax + " + str(offset) + "]") # subType column stores 0 or 1 already self.code += p("mov", "eax", "[eax + 4]") # access subtype testing column
else: # primitive type can be staticly evaluated self.code += p("mov", "eax", "[eax + " + str(offset) + "]") # subType column stores 0 or 1 already
if self.right.myType.assignable(self.left.myType):
self.code += p("mov", "eax", "1") else: # primitive type can be staticly evaluated
else: if self.right.myType.assignable(self.left.myType):
self.code += p("mov", "eax", "1")
else:
self.code += p("mov", "eax", "0")
else: # non array is never an instance of array, array could be non array
if self.right.myType.isArray:
self.code += p("mov", "eax", "0") self.code += p("mov", "eax", "0")
else:
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
......
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