diff --git a/TypeNodes.py b/TypeNodes.py index 29dcb2bc721208526816af8a34af3584563c40a3..2aeb0dc8e8732844b6b0d0eb4e25edd38d31c746 100644 --- a/TypeNodes.py +++ b/TypeNodes.py @@ -165,7 +165,7 @@ class ClassNode(ClassInterNode): self.label = "" # label in assembly self.methodOffset = {} # a dictionary that maps method signatures (method.name, method.paramTypes) to offsets in the CLASS memory layout self.fieldOffset = {} # a dictionary that maps field names to offsets in OBJECT memory layout - self.staticFieldLabels = [] # a list of static field labels + self.staticFieldLabels = [] # a list of static field labels for node in parseTree.children: if node.name == 'classMod': @@ -257,7 +257,12 @@ class ClassNode(ClassInterNode): # overlapping class/interface logic super().checkHierarchy() - + # point superClass to java.lang.Object after hierarchy checking is done + if self.canonName != 'java.lang.Object' and not self.superClass: + self.superClass = self.env.getNode('java.lang.Object', 'type') + + + def checkType(self): super().checkType() @@ -278,19 +283,19 @@ class ClassNode(ClassInterNode): self.label = pLabel(name=self.name, type="class") self.code += ";START OF CLASS MEMORY LAYOUT FOR CLASS: " + self.canonName + "\n" self.code += self.label - + # TODO: SIT and subtype testing tables ####### ADDING METHODS TO CLASS MEMORY LAYOUT AND FIELDS TO FIELD OFFSET TABLE ######### - # 1. - # a) Copying over the offsets of methods from superclass and DECLARING memory segment for the methods + # 1. + # a) Copying over the offsets of methods from superclass and DECLARING memory segment for the methods # b) copying over the offsets of fields from superclass (TO BE USED LATER FOR OBJECT MEMORY LAYOUT) - lastMethodOffset = -4 # stores the largest method offset in the superCalss + lastMethodOffset = -4 # stores the largest method offset in the superCalss # TODO: set this to 4 after the implemntation of both the SIT and subtype testing table # Note: This is 4 less than the offset of where the next method would be located - # This is to accomodate for the addition of 4 to lastMethodOffset in EVERY (including the first) iteration in the + # This is to accomodate for the addition of 4 to lastMethodOffset in EVERY (including the first) iteration in the # loops that loops through self.constructors and self.methods, in the case where there is no superClass - lastFieldOffset = 0 + lastFieldOffset = 0 if self.superClass: if not hasattr(self.superClass, "code"): @@ -301,20 +306,20 @@ class ClassNode(ClassInterNode): self.code += pLabel(name=self.name + "_" + key[0] + "_" + key[1], type="vtable") self.code += p(instruction="dd", arg1=64) # just declaring a memory segment with a random number lastMethodOffset = max(value, lastMethodOffset) - + # Iterating through field offset table sorted by offset for key,value in self.superClass.fieldOffset.items(): self.fieldOffset[key] = value lastFieldOffset = max(value, lastFieldOffset) - - # 2. Assigning offsets to constructors and DECLARING memory segment for the methods + + # 2. Assigning offsets to constructors and DECLARING memory segment for the methods for method in self.constructors: lastMethodOffset += 4 self.methodOffset[(method.name, method.paramTypes)] = lastMethodOffset self.code += pLabel(name=self.name + "_" + method.name + "_" + method.paramTypes, type="vtable") self.code += p(instruction="dd", arg1=64) # just declaring a memory segment with a random number - - # 3. Assigning offsets to methods that aren't in the super class DECLARING memory segment for the methods + + # 3. Assigning offsets to methods that aren't in the super class DECLARING memory segment for the methods for method in self.methods: if not (method.name, method.paramTypes) in self.methodOffset: lastMethodOffset += 4 @@ -324,13 +329,13 @@ class ClassNode(ClassInterNode): # print(self.methodOffset) self.code += ";END OF CLASS MEMORY LAYOUT FOR CLASS " + self.name + "\n" - # 4. Fill in the memory segment declared in step 1 and 2 with the addresses of the method implementations + # 4. Fill in the memory segment declared in step 1 and 2 with the addresses of the method implementations for key,value in self.methodOffset.items(): vLabel = "V_" + self.name + "_" + key[0] + "_" + key[1]+"" # method at class's vtable mLabel = "M_" + self.name + "_" + key[0] + "_" + key[1] # method implementation self.code += p(instruction="mov", arg1="eax", arg2=vLabel, comment="Filling in class memory segment for method " + mLabel) self.code += p(instruction="mov", arg1="[eax]", arg2=mLabel) - + #5. Adding in fields that are not inherited from parent class to offset table # Note: excluding static fields for field in self.fields: @@ -339,7 +344,7 @@ class ClassNode(ClassInterNode): self.fieldOffset[field.name] = lastFieldOffset # print(self.name) # print(self.fieldOffset) - + # print(self.code) ########################################################### @@ -350,7 +355,7 @@ class ClassNode(ClassInterNode): if c and hasattr(c, "codeGen"): if not hasattr(c, "code"): # children hasn't generated code yet c.codeGen() - self.code += c.code + self.code += c.code #####################################################################