diff --git a/AstBuilding.py b/AstBuilding.py index d29f96a9f10911ede3526e6e17288d16cf0dcf85..734eee03494ca491ba8f68f60721718a08346a30 100644 --- a/AstBuilding.py +++ b/AstBuilding.py @@ -112,7 +112,7 @@ def arrayClassMemory(types): p(instruction="mov", arg1="[eax]", arg2="dword "+m, comment="points to method implementation") if tt.__class__.__name__ == "ClassNode": - code += p("extern", "A_subT" + tt.name) + code += p("extern", "A_subT_" + tt.name) code += p("mov", "eax", "I_subT_spot_" + t) code += p("mov", "[eax]", "dword A_subT_" + tt.name) @@ -135,21 +135,23 @@ def codeGenPrep(ASTs): interM = [] types = ['boolean', 'byte', 'char', 'int', 'short'] # All possible types classNodes = [] + j = 0 for t in ASTs: classInterNode = t[1].typeDcl if classInterNode.__class__.__name__ == "ClassNode": - types.append(classInterNode) classNodes.append(classInterNode) else: # interfaceNode, get their methods to prep for SIT interM += classInterNode.methods + classInterNode.subTypeOffset = j * 4 # assign each type a position in the subtype testing table + j += 1 + # store SIT and subtype table size - for i in range(len(classNodes)): - classNodes[i].SITsize = len(interM) - classNodes[i].subTypeSize = len(types) - 5 # no primitive types in subtype table - classNodes[i].subTypeOffset = i * 4 # assign each type a position in the subtype testing table + for c in classNodes: + c.SITsize = len(interM) + c.subTypeSize = j # no primitive types in subtype table # prep SIT for i in range(len(interM)): diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index 90c413b4ee31c586e84a98807dab21b9d7d16fc7..f20a562b0aaa3d87d21c03a1e561a4f583c274f3 100644 --- a/ExprPrimaryNodes.py +++ b/ExprPrimaryNodes.py @@ -327,6 +327,21 @@ class AssignNode(ASTNode): self.code = self.right.code self.code += p("push", "eax") + # array assign check + if self.left.__class__.__name__ == "ArrayAccessNode" and (not self.left.myType.isPrimitive): + self.code += "; array assign subtype check\n" + self.left.array.codeGen() + self.code += self.left.array.code + + offset = self.right.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 +=("; Evaluate left of assignment\n") self.code += self.left.addr()