added offset code in symbol table for records

This commit is contained in:
Partho
2025-04-09 15:15:17 -04:00
parent d17e99758f
commit 859ff3fd03
3 changed files with 166 additions and 3 deletions

View File

@ -680,7 +680,7 @@ assignable:
ID ID
{ {
$$ = look_up(cur,$1); $$ = look_up(cur,$1);
printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", getName((TableNode*)$$), $1); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", getTypeEntry((TableNode*)$$), $1);
} }
| assignable | assignable

View File

@ -135,6 +135,169 @@ AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) {
// This gets the number of elements that make up a record. // This gets the number of elements that make up a record.
// Perhaps this may not be needed since we need to iterate over all elements // Perhaps this may not be needed since we need to iterate over all elements
// anyways. // anyways.
int getRecTotal(TableNode* node){
if(node == NULL){
printdebug(
"passed a NULL node to getRecTotal. Invalid.");
return -1;
}
if(getAdInfoType(node) != TYPE_RECORD_TYPE){
printdebug(
"passed an invalid node to getRecTotal. Invalid.");
return -1;
}
if(node->additionalinfo == NULL){
printdebug(
"node has NULL additionalinfo. Invalid.");
return -1;
}
return node->additionalinfo->RecAdInfo->total_size;
}
TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) {
if (node == NULL) {
printdebug(
"passed a NULL node to setRecOffsetInfo. Invalid.");
return undefined;
}
if (scope == NULL) {
printdebug(
"passed an NULL scope to setRecOffsetInfo. Invalid.");
return undefined;
}
if(getFirstEntry(scope) == NULL){
printdebug(
"passed an empty scope to setRecOffsetInfo. Invalid.");
return undefined;
}
TableNode* this = getFirstEntry(scope);
int largest = 0;
int k = getRecLength(node);
int total_size = 0;
int counter = 0;
int *offsets = (int *)calloc(2 * k, sizeof(int));
if(getAdInfoType(this) == TYPE_FUNCTION_TYPE){
offsets[counter] = 8;
total_size = total_size + offsets[counter];
largest = 8;
counter++;
}
else if(getAdInfoType(this) == TYPE_RECORD_TYPE){
offsets[counter] = getRecTotal(this);
total_size = total_size + offsets[counter];
largest = offsets[counter];
counter++;
}
else if(getAdInfoType(this)==TYPE_PRIMITIVE){
offsets[counter] = getPrimSize(this);
total_size = total_size + offsets[counter];
largest = offsets[counter];
counter++;
}
else if(getAdInfoType(this)==TYPE_ARRAY_TYPE){
offsets[counter] = 8;
total_size = total_size + offsets[counter];
largest = offsets[counter];
counter++;
}
else {
printdebug(
"[TYPE CHECK] passed an invalid (first) parameter to a function definition.");
return undefined;
}
this = getNextEntry(this);
while(this != NULL){
if(getAdInfoType(this) == TYPE_FUNCTION_TYPE){
int s = 8;
if (s > largest) {
largest = s;
}
//make sure current location is aligned properly
offsets[counter] = (total_size % s);
total_size = total_size + offsets[counter];
counter++;
//add in the size of the entry and increment
offsets[counter] = s;
total_size = total_size + offsets[counter];
counter++;
this = getNextEntry(this);
}
else if(getAdInfoType(this) == TYPE_ARRAY_TYPE){
int s = 8;
if (s > largest) {
largest = s;
}
//make sure current location is aligned properly
offsets[counter] = (total_size % s);
total_size = total_size + offsets[counter];
counter++;
//add in the size of the entry and increment
offsets[counter] = s;
total_size = total_size + offsets[counter];
counter++;
this = getNextEntry(this);
}
else if(getAdInfoType(this) == TYPE_RECORD_TYPE){
int s = getRecTotal(this);
if (s > largest) {
largest = s;
}
//make sure current location is aligned properly
offsets[counter] = (total_size % s);
total_size = total_size + offsets[counter];
counter++;
//add in the size of the entry and increment
offsets[counter] = s;
total_size = total_size + offsets[counter];
counter++;
this = getNextEntry(this);
}
else if(getAdInfoType(this) == TYPE_PRIMITIVE){
int s = getPrimSize(this);
if (s > largest) {
largest = s;
}
//make sure current location is aligned properly
offsets[counter] = (total_size % s);
total_size = total_size + offsets[counter];
counter++;
//add in the size of the entry and increment
offsets[counter] = s;
total_size = total_size + offsets[counter];
counter++;
this = getNextEntry(this);
}else{
printdebug(
"[TYPE CHECK] passed an invalid parameter at position %d in record.",((counter+1)/2));
return undefined;
}
}
//make sure that size of whole structure is aligned with largest element in struct:
offsets[counter] = (total_size % largest);
total_size = total_size + offsets[counter];
node->additionalinfo->RecAdInfo->offsets = offsets;
node->additionalinfo->RecAdInfo->total_size = total_size;
return node;
}
int* getRecOffsets(TableNode* node){
if(node == NULL){
printdebug(
"passed a NULL node to getRecTotal. Invalid.");
return NULL;
}
if(getAdInfoType(node) != TYPE_RECORD_TYPE){
printdebug(
"passed an invalid node to getRecTotal. Invalid.");
return NULL;
}
if(node->additionalinfo == NULL){
printdebug(
"node has NULL additionalinfo. Invalid.");
return NULL;
}
return node->additionalinfo->RecAdInfo->offsets;
}
int getRecLength(TableNode *definition) { int getRecLength(TableNode *definition) {
if (definition == NULL) { if (definition == NULL) {
printdebug( printdebug(

View File

@ -24,8 +24,8 @@ typedef struct {
typedef struct { typedef struct {
int numofelements; int numofelements;
struct SymbolTable *recordScope; struct SymbolTable *recordScope;
int totalsize; int total_size;
int offsets[]; int* offsets;
} record_info; } record_info;
typedef struct { typedef struct {