This commit is contained in:
Scarlett
2025-05-06 16:42:22 -04:00
parent 5e0d2e3cdf
commit a7e10521d8
5 changed files with 41 additions and 15 deletions

View File

@ -744,7 +744,7 @@ int generateFunctionStart(Instruction *inst) {
fprintf(cg_flag, "%s:\n", getName(funDecTN)); fprintf(cg_flag, "%s:\n", getName(funDecTN));
fprintf(cg_flag, "\tpushq\t%%rbp\n"); fprintf(cg_flag, "\tpushq\t%%rbp\n");
fprintf(cg_flag, "\tmovq\t%%rsp, %%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 //now we need to add the CGs of nodes to the CG list by doing assign from the
// have function declararation node // have function declararation node

View File

@ -68,6 +68,7 @@ int generateFunctionStart(Instruction *instruction);
extern int label_count; extern int label_count;
extern Instruction *begin; extern Instruction *begin;
extern Instruction *current; extern Instruction *current;
extern SymbolTable *top;
extern int offset; extern int offset;
extern int currentsp; extern int currentsp;

View File

@ -462,7 +462,7 @@ int getRecSize(SymbolTable *tn) {
} }
return -1; return -1;
} }
int getRecPosition(TableNode* rec, char* id){ int getRecPosition(TableNode *rec, char *id) {
if (rec == NULL) { if (rec == NULL) {
printdebug( printdebug(
"passed a NULL entry to getRecPosition. Invalid."); "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"); "not checking the position of a record -- invalid op");
return -1; return -1;
} }
TableNode* cur = getFirstEntry(getRecList(rec)); TableNode *cur = getFirstEntry(getRecList(rec));
int i = 1; int i = 1;
while(cur != NULL){ while (cur != NULL) {
if(strcmp(getName(cur), id) == 0){ if (strcmp(getName(cur), id) == 0) {
return i; return i;
} }
cur = getNextEntry(cur); cur = getNextEntry(cur);
i++; i++;
} }
if (cur == NULL){ if (cur == NULL) {
printdebug( printdebug(
"passed an invalid entry to getRecPosition. Invalid."); "passed an invalid entry to getRecPosition. Invalid.");
return -1; return -1;
}else{ } else {
return i; return i;
} }
} }
int getElementOffset(TableNode *rec, char* id) { int getElementOffset(TableNode *rec, char *id) {
if (rec == NULL) { if (rec == NULL) {
printdebug( printdebug(
"passed a NULL entry to getElementOffset. Invalid."); "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"); "not checking the offset of a record -- invalid op");
return -1; return -1;
} }
int* offsets = getRecOffsets(rec); int *offsets = getRecOffsets(rec);
int position = getRecPosition(rec, id); int position = getRecPosition(rec, id);
if (position == -1) { if (position == -1) {
printdebug( printdebug(
@ -522,11 +522,11 @@ int getElementOffset(TableNode *rec, char* id) {
position = position - 1; position = position - 1;
int total_offset = 0; int total_offset = 0;
int current_position = 1; int current_position = 1;
while(current_position < position+1){ while (current_position < position + 1) {
//adding current element in struct //adding current element in struct
total_offset += offsets[2*current_position]; total_offset += offsets[2 * current_position];
//adding padding between elements //adding padding between elements
total_offset += offsets[2*current_position+1]; total_offset += offsets[2 * current_position + 1];
current_position++; current_position++;
} }
//returning the offset of the start of the element //returning the offset of the start of the element
@ -1739,3 +1739,28 @@ TableNode *printTableNode(TableNode *tn) {
return 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;
}

View File

@ -163,12 +163,14 @@ TableNode *getNextEntry(TableNode *tn);
TableNode *printTableNode(TableNode *tn); TableNode *printTableNode(TableNode *tn);
int getElementOffset(TableNode *rec, char* id); int getElementOffset(TableNode *rec, char* id);
int getRecPosition(TableNode* rec, char* id); int getRecPosition(TableNode* rec, char* id);
int getStackSize();
extern int yylex(void); extern int yylex(void);
extern char *yytext; extern char *yytext;
extern int yyleng; extern int yyleng;
extern int yychar; extern int yychar;
extern SymbolTable *cur; extern SymbolTable *cur;
extern SymbolTable *top;
extern int line_number; extern int line_number;
extern int column_number; extern int column_number;
extern FILE *yyin; extern FILE *yyin;

View File

@ -5,8 +5,6 @@ function entry: string2integer
entry (arg) := { entry (arg) := {
[integer: x; string: s; character: c; integer: result] [integer: x; string: s; character: c; integer: result]
result := printS("hello world");
s := inS(1); s := inS(1);
result := printS(s); result := printS(s);