diff --git a/src/codegen.c b/src/codegen.c index 9de14e7..88a2ab1 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -744,7 +744,7 @@ int generateFunctionStart(Instruction *inst) { fprintf(cg_flag, "%s:\n", getName(funDecTN)); fprintf(cg_flag, "\tpushq\t%%rbp\n"); fprintf(cg_flag, "\tmovq\t%%rsp, %%rbp\n"); - fprintf(cg_flag, "\tsubq\t$%d, %%rsp\n", 128); // [CHANGE ME] 128 is a placeholder for the stack size + fprintf(cg_flag, "\tsubq\t$%d, %%rsp\n", getStackSize()); //now we need to add the CGs of nodes to the CG list by doing assign from the // have function declararation node diff --git a/src/codegen.h b/src/codegen.h index 0667445..d790ea9 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -68,6 +68,7 @@ int generateFunctionStart(Instruction *instruction); extern int label_count; extern Instruction *begin; extern Instruction *current; +extern SymbolTable *top; extern int offset; extern int currentsp; diff --git a/src/symbol_table.c b/src/symbol_table.c index 50c2a39..9f81805 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -462,7 +462,7 @@ int getRecSize(SymbolTable *tn) { } return -1; } -int getRecPosition(TableNode* rec, char* id){ +int getRecPosition(TableNode *rec, char *id) { if (rec == NULL) { printdebug( "passed a NULL entry to getRecPosition. Invalid."); @@ -478,25 +478,25 @@ int getRecPosition(TableNode* rec, char* id){ "not checking the position of a record -- invalid op"); return -1; } - TableNode* cur = getFirstEntry(getRecList(rec)); + TableNode *cur = getFirstEntry(getRecList(rec)); int i = 1; - while(cur != NULL){ - if(strcmp(getName(cur), id) == 0){ + while (cur != NULL) { + if (strcmp(getName(cur), id) == 0) { return i; } cur = getNextEntry(cur); i++; } - if (cur == NULL){ + if (cur == NULL) { printdebug( "passed an invalid entry to getRecPosition. Invalid."); return -1; - }else{ - return i; + } else { + return i; } } -int getElementOffset(TableNode *rec, char* id) { +int getElementOffset(TableNode *rec, char *id) { if (rec == NULL) { printdebug( "passed a NULL entry to getElementOffset. Invalid."); @@ -512,7 +512,7 @@ int getElementOffset(TableNode *rec, char* id) { "not checking the offset of a record -- invalid op"); return -1; } - int* offsets = getRecOffsets(rec); + int *offsets = getRecOffsets(rec); int position = getRecPosition(rec, id); if (position == -1) { printdebug( @@ -522,11 +522,11 @@ int getElementOffset(TableNode *rec, char* id) { position = position - 1; int total_offset = 0; int current_position = 1; - while(current_position < position+1){ + while (current_position < position + 1) { //adding current element in struct - total_offset += offsets[2*current_position]; + total_offset += offsets[2 * current_position]; //adding padding between elements - total_offset += offsets[2*current_position+1]; + total_offset += offsets[2 * current_position + 1]; current_position++; } //returning the offset of the start of the element @@ -1739,3 +1739,28 @@ TableNode *printTableNode(TableNode *tn) { return tn; } + +int getStackSize() { + int i = 0; + TableNode *tn = getFirstEntry(top); + while (tn != NULL) { + i++; + tn = getNextEntry(tn); + } + + ListOfTable *lt = getChildren(top); + while (lt != NULL) { + TableNode *first = lt->table->entries; + while (first != NULL) { + i++; + first = getNextEntry(first); + } + + lt = lt->next; + } + + i= i * 8; + if (i % 16 != 0) i += 8; + + return i; +} \ No newline at end of file diff --git a/src/symbol_table.h b/src/symbol_table.h index 5feb46c..e6da226 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -163,12 +163,14 @@ TableNode *getNextEntry(TableNode *tn); TableNode *printTableNode(TableNode *tn); int getElementOffset(TableNode *rec, char* id); int getRecPosition(TableNode* rec, char* id); +int getStackSize(); extern int yylex(void); extern char *yytext; extern int yyleng; extern int yychar; extern SymbolTable *cur; +extern SymbolTable *top; extern int line_number; extern int column_number; extern FILE *yyin; diff --git a/tests/programs/io.alpha b/tests/programs/io.alpha index 45b387f..eac7b03 100644 --- a/tests/programs/io.alpha +++ b/tests/programs/io.alpha @@ -5,8 +5,6 @@ function entry: string2integer entry (arg) := { [integer: x; string: s; character: c; integer: result] - result := printS("hello world"); - s := inS(1); result := printS(s);