From c3e45f06ae03a108d6b666d666a0d84b37110230 Mon Sep 17 00:00:00 2001
From: Xun Yang <x299yang@uwaterloo.ca>
Date: Sun, 12 Apr 2020 20:51:25 -0400
Subject: [PATCH] fix codeGenprep

---
 AstBuilding.py | 29 ++++++++++++++---------------
 TypeNodes.py   |  9 ++++-----
 2 files changed, 18 insertions(+), 20 deletions(-)

diff --git a/AstBuilding.py b/AstBuilding.py
index dbf919b..d722999 100644
--- a/AstBuilding.py
+++ b/AstBuilding.py
@@ -109,33 +109,32 @@ def arrayClassMemory(types):
 def codeGenPrep(ASTs):
     interM = []
     types = ['boolean', 'byte', 'char', 'int', 'short'] # All possible types
-    j = 0
+    classNodes = []
+
     for t in ASTs:
         classInterNode = t[1].typeDcl
-        types.append(classInterNode)
-
-        # assign each type a position in the subtype testing table
-        classInterNode.subTypeOffset = j * 4
-        j += 1
-
         if classInterNode.__class__.__name__ == "ClassNode":
-            classInterNode.populateSizeAndFieldOffset()
-            classInterNode.populateMethodOffset()
+
             types.append(classInterNode.name)
+            classNodes.append(classInterNode)
         else: # interfaceNode, get their methods to prep for SIT
             interM += classInterNode.methods
 
+    # 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
+
     # prep SIT
     for i in range(len(interM)):
         interM[i].methodOffset = i * 4
         interM[i].isInterM = True
 
-    # store SIT and subtype table size
-    for t in ASTs:
-        classInterNode = t[1].typeDcl
-        if classInterNode.__class__.__name__ == "ClassNode":
-            classInterNode.SITsize = len(interM)
-            classInterNode.subTypeSize = len(types) - 5 # no primitive types in subtype table
+    # init data section
+    for c in classNodes:
+        c.populateSizeAndFieldOffset()
+        c.populateMethodOffset()
 
     return arrayClassMemory(types)
 
diff --git a/TypeNodes.py b/TypeNodes.py
index 71c91b7..99fab64 100644
--- a/TypeNodes.py
+++ b/TypeNodes.py
@@ -306,13 +306,12 @@ class ClassNode(ClassInterNode):
 
         # Calculating the size of objects of this class
         self.size += (len(self.fieldOffset))*4
-    
+
     # Populating method offset and creating class memory layout
     def populateMethodOffset(self):
         if hasattr(self, "data"):
             return
 
-        self.code = "" # For read-only section
         self.data = "" # For writeable data section
         if self.canonName == "java.lang.Object":
             self.data += p("global", "I_SIT_" + self.name)
@@ -391,15 +390,15 @@ class ClassNode(ClassInterNode):
             self.data += p("dd", "0") # 0 for False, 1 for True
 
         self.data += ";END OF CLASS MEMORY LAYOUT FOR CLASS " + self.name + "\n"
-        
+
 
     def codeGen(self):
         if hasattr(self, "code"):
             return
-        
+
 
         self.code = "" # For read-only section
-        
+
         # print("This is the super class: {}".format(self.superClass))
 
         # 4. Fill in the memory segment declared in step 1 and 2 with the addresses of the method implementations
-- 
GitLab