diff --git a/CodeGenUtils.py b/CodeGenUtils.py index a17a024ada6d12819a20a54a0c35e453a4af56b5..c953da3cd38254b328dbf339a5ac04b05e86f372 100644 --- a/CodeGenUtils.py +++ b/CodeGenUtils.py @@ -113,7 +113,6 @@ def iffalse(cond, label): # else val == Int shouldn't happen here, since we already done typeCheck else: result = cond.getIfFalse(label) - result += p("jz", label) # jz: jump if 0 return result @@ -138,7 +137,7 @@ def genericHelperFunctions(): p(instruction="cmp", arg1="eax", arg2="[G__Zero]") + \ p(instruction="jle", arg1="H__Throw_Exception") + \ p(instruction="ret", arg1="") - + # Helper function to perform bounds check on array # eax = pointer to array # ecx = index i diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index 875443891797d24426f33b1e1bfaed15477aae94..1ff131bb618bc0b7b9937891039e3d8c16e0aa00 100644 --- a/ExprPrimaryNodes.py +++ b/ExprPrimaryNodes.py @@ -173,7 +173,7 @@ class ArrayAccessNode(ASTNode): result += "; Evaluating pointer to array\n" + \ self.array.code + \ p(instruction="push", arg1="eax", comment="push arrray pointer") - + # 2. Evaluating i if not hasattr(self.index, "code"): self.index.codeGen() @@ -184,8 +184,8 @@ class ArrayAccessNode(ASTNode): # 3. Runtime checks result += p(instruction="pop", arg1="eax", comment="eax is pointer to array") + \ p(instruction="call", arg1="H__Null_Check") + \ - p(instruction="call", arg1="H__Bounds_Check") - + p(instruction="call", arg1="H__Bounds_Check") + # 4. Make eax store the address to the item we're accessing result += p(instruction="add", arg1="eax", arg2=8, comment="eax points at a[0]") + \ p(instruction="imul", arg1="ecx", arg2=4, comment="ecx contains the number of bytes to shift according to i") + \ @@ -195,7 +195,7 @@ class ArrayAccessNode(ASTNode): result += "; End of calculating address for array access\n" return result - + def codeGen(self): if hasattr(self, "code"): return @@ -266,14 +266,14 @@ class ArrayCreateNode(ASTNode): aLabel += self.arrayType.myType.name else: aLabel += self.arrayType.myType.typePointer.name - + self.code += p(instruction="extern", arg1=aLabel) + \ p(instruction="mov", arg1="[eax]", arg2="dword "+aLabel, comment="first item is vtable pointer") # 3. Storing length in the second slot self.code += p(instruction="mov", arg1="[eax+4]", arg2="ecx", comment="storing array length in second slot") - # 4. Restoring ecx + # 4. Restoring ecx self.code += p(instruction="pop", arg1="ecx", comment="restoring register ecx") self.code += ";End of array creation\n" @@ -605,7 +605,14 @@ class ExprNode(ASTNode): return if self.op == '!': - self.code += p('not', 'eax') + n = getCFlowLabel() + endLabel = "_end" + n + + self.code += p("cmp", "eax", "[G__Zero]") + self.code += p("mov", "eax", "1") + self.code += p("je", endLabel) + self.code += p("mov", "eax", "0") + self.code += p(endLabel + ":", "") return # Comparisons that short-circuit: @@ -698,13 +705,9 @@ class ExprNode(ASTNode): # generate shorter code if self.op = comparison def getIfFalse(self, label): - if self.op == '&&': - result = self.left.getIfFalse(label) - result += self.right.getIfFalse(label) - return result - elif self.op not in ['==', '!=', '<=', '>=', '>', '<']: + if self.op not in ['==', '!=', '<=', '>=', '>', '<']: self.codeGen() - return self.code + return self.code + p("cmp", "eax", "[G__Zero]") + p("je", label) else: result = self.codeGenLeftRight() result += p("cmp", "ebx", "eax", "left " + self.op + " right") diff --git a/MemberNodes.py b/MemberNodes.py index 0758e0c2332bf373554a54d4a07d3535e5d71391..4a3d14c93f93a3fcf03d82ef26b549f5dc8fb01c 100644 --- a/MemberNodes.py +++ b/MemberNodes.py @@ -224,8 +224,13 @@ class MethodNode(ASTNode): self.code = pLabel(self.typeName + "_" + self.name + "_" + self.paramTypes, "method") # params - for i, param in enumerate(self.params): - param.offset = i * 4 + 12 # 12 since the stack is now of the order: ebp, eip, o, params + stackoffset = 12 + if 'static' in self.mods: + stackoffset = 8 # no object as argument for static methods + rparams = self.params.copy() + rparams.reverse() + for i, param in enumerate(rparams): + param.offset = i * 4 + stackoffset # 12 since the stack is now of the order: ebp, eip, o, params if self.body: