added offset code in symbol table for records
This commit is contained in:
@ -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
|
||||
|
@ -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(
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user