From 859ff3fd032db8774c5d4a1e26d9c080f8ac3113 Mon Sep 17 00:00:00 2001 From: Partho Date: Wed, 9 Apr 2025 15:15:17 -0400 Subject: [PATCH] added offset code in symbol table for records --- src/grammar.y | 2 +- src/symbol_table.c | 163 +++++++++++++++++++++++++++++++++++++++++++++ src/symbol_table.h | 4 +- 3 files changed, 166 insertions(+), 3 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index e12580d..4b4d14c 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -680,7 +680,7 @@ assignable: ID { $$ = 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 diff --git a/src/symbol_table.c b/src/symbol_table.c index c76043f..cd04f8a 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -135,6 +135,169 @@ AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) { // 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 // 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) { if (definition == NULL) { printdebug( diff --git a/src/symbol_table.h b/src/symbol_table.h index e2fac84..af2d827 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -24,8 +24,8 @@ typedef struct { typedef struct { int numofelements; struct SymbolTable *recordScope; - int totalsize; - int offsets[]; + int total_size; + int* offsets; } record_info; typedef struct {