From e3df80b0d1b0a5e6db6b75eccc32d2849851c1f6 Mon Sep 17 00:00:00 2001
From: Xun Yang <x299yang@uwaterloo.ca>
Date: Sun, 12 Apr 2020 23:04:16 -0400
Subject: [PATCH] fix iffalse, not, static param offset"

---
 CodeGenUtils.py     |  3 +--
 ExprPrimaryNodes.py | 29 ++++++++++++++++-------------
 MemberNodes.py      |  9 +++++++--
 3 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/CodeGenUtils.py b/CodeGenUtils.py
index 2602d67..a56fb5e 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 8754438..1ff131b 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 0758e0c..4a3d14c 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:
 
-- 
GitLab