Skip to content
Snippets Groups Projects
Commit b0882e33 authored by pycsham's avatar pycsham
Browse files

added field offset table at class node

parent 9070a875
No related branches found
Tags 3.0.0
No related merge requests found
......@@ -324,6 +324,18 @@ class ClassCreateNode(ASTNode):
if curClass.packageName != classDef.packageName:
raise Exception("ERROR: In class {0}, using a protected constructor, but class {1} is not in class {0}'s package ({2}).".format(curClass.name, classDef.name, curClass.packageName))
def codeGen(self):
if hasattr(self, "code"):
return
self.code = ""
for c in self.children:
if c and hasattr(c, "codeGen"):
# children hasn't generated code yet
# Note: this check is redundant if we're certain that every override of this method has the initial check
if not hasattr(c, "code"):
c.codeGen()
self.code += c.code
#################################################################################
......
......@@ -163,7 +163,8 @@ class ClassNode(ClassInterNode):
self.superClass = '' # these fields initially stores a string that represent the super
#### Code generation relevant fields ####
self.label = "" # label in assembly
self.methodOffset = {} # a dictionary that maps method signatures (method.name, method.paramTypes) to offsets in the memory layout
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
for node in parseTree.children:
......@@ -280,36 +281,44 @@ class ClassNode(ClassInterNode):
# TODO: SIT and subtype testing tables
####### ADDING METHODS TO CLASS MEMORY LAYOUT #########
# 1. Copying over the offsets of methods from superclass and DECLARING memory segment for the methods
lastOffset = -4 # stores the largest offset in the superCalss
####### 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
# 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
# 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 lastOffset 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
if self.superClass:
if not hasattr(self.superClass, "code"):
self.superClass.codeGen()
# Iterating through offset table sorted by offset
# Iterating through method offset table sorted by offset
for key,value in sorted(self.superClass.methodOffset.items(), key=lambda item: item[1]):
self.methodOffset[key] = value
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
lastOffset = max(value, lastOffset)
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
for method in self.constructors:
lastOffset += 4
key = (method.name, method.paramTypes)
self.methodOffset[(method.name, method.paramTypes)] = lastOffset
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
for method in self.methods:
if not (method.name, method.paramTypes) in self.methodOffset:
lastOffset += 4
self.methodOffset[(method.name, method.paramTypes)] = lastOffset
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
......@@ -322,11 +331,21 @@ class ClassNode(ClassInterNode):
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:
if not 'static' in field.mods:
lastFieldOffset += 4
self.fieldOffset[field.name] = lastFieldOffset
# print(self.name)
# print(self.fieldOffset)
# print(self.code)
###########################################################
for c in self.children:
if c and hasattr(c, "codeGen"):
if not hasattr(c, "code"): # children hasn't generated code yet
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment