This commit is contained in:
Annie
2025-05-06 20:32:57 -04:00
parent 8c409561b7
commit 666d924608
2 changed files with 46 additions and 21 deletions

View File

@ -92,6 +92,18 @@ int generate() {
return -1;
}
bool isAnActualFunction(TableNode *tn) {
FunDecList *fdl = funList;
while (fdl != NULL) {
printf("%s %s, %s %s\n", getName(fdl->tn), getType(fdl->tn), getName(tn), getType(tn));
if (tn == fdl->tn) {
return true;
}
fdl = fdl ->next;
}
return false;
}
void align(TableNode *tn) {
int size = getPrimSize(getTypeEntry(tn));
offset += offset % size;
@ -142,6 +154,9 @@ CGNode *addCG(TableNode *tn, int sp) {
int generateFunctionDec(Instruction *i) {
FunDecList * fd = calloc(1, sizeof(FunDecList));
CGNode *cg = addCG(getResult(i),offset);
// printf("%d\n", getAddress(cg)+currentsp);
offset += 8;
fd->tn = getResult(i);
fd->next = funList;
funList = fd;
@ -519,9 +534,13 @@ int generateAssign(Instruction *inst) {
printdebug("generateAssign failed, %s is not constant but not in CGlist", getName(getTN(op1)));
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#assign start\n", getAddress(op1CG));
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#assign end\n", getAddress(cg));
if (getAdInfoType(getTN(op1)) != TYPE_FUNCTION_DECLARATION || !isAnActualFunction(getTN(op1))){
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#assign start\n", getAddress(op1CG));
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#assign end\n", getAddress(cg));
} else {
fprintf(cg_flag, "\tmovl\t$%s,%%eax\t#assign function\n", getName(getTN(op1)));
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#assign function end\n", getAddress(cg));
}
return 0;
}
@ -665,18 +684,21 @@ int generateCall(Instruction *inst) {
return -1;
}
//if (findCG(getTN(op1)) == NULL) {
// printdebug("generateFunctionCall failed, function not in stack");
// return -1;
//}
// if (getTN(op2) == NULL) {
// printdebug("generateFunctionCall failed, NULL tablenode2");
// return -1;
//}
fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1)));
//fprintf(cg_flag, "\tmovq\t%d(%%rbp), %%rax\t#call %s!\n", getAddress(findCG(getTN(op1))), getName(getTN(op1)));
//fprintf(cg_flag, "\tcall *%%rax\n");
if (table_lookup(getAncestor(cur),getName(getTN(op1)))!=undefined) {
fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1)));
} else {
// printf("%s\n", getName(getTN(op1)));
if (findCG(getTN(op1)) == NULL) {
//printf("generateFunctionCall failed, function not in stack");
return -1;
}
fprintf(cg_flag, "\tmovq\t%d(%%rbp), %%rax\t#call %s!\n", getAddress(findCG(getTN(op1))), getName(getTN(op1)));
fprintf(cg_flag, "\tcall *%%rax\n");
}
//if (getTN(op2) == NULL) {
//printdebug("generateFunctionCall failed, NULL tablenode2");
// return -1;
// }
//now for the return
CGNode *cg = findCG(getResult(inst));
@ -702,8 +724,11 @@ int generateReturn(Instruction *inst) {
printdebug("generateReturn failed, trying to return %s not in CGList", getName(getTN(op1)));
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#return %s\n", getAddress(cg), getName(getTN(op1)));
if (isAnActualFunction(getTN(op1))) {
fprintf(cg_flag, "\tmovl\t$%s,%%eax\t#return a function\n", getName(getTN(op1)));
} else {
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#return %s\n", getAddress(cg), getName(getTN(op1)));
}
fprintf(cg_flag, "\tleave\n");
fprintf(cg_flag, "\tret\n");
return 0;
@ -830,14 +855,13 @@ int generateFunctionStart(Instruction *inst) {
if (funList == NULL) {
return -1;
}
fprintf(stderr, "here\n");
TableNode *funDec;
while (funList != NULL) {
funDec = funList->tn;
CGNode *cg = addCG(getResult(inst), offset);
CGNode *cg = findCG(getResult(inst));
fprintf(cg_flag, "\tmovq\t$%s, %d(%%rbp)\t#storing function declaration\n", getName(funDec), getAddress(cg));
funList = funList->next;
}
}
}
return 0;
}

View File

@ -33,6 +33,7 @@ typedef struct FunDecList {
struct FunDecList *next;
} FunDecList;
bool isAnActualFunction(TableNode *tn);
int generate();
CGNode *getNextCG(CGNode *cg);
int getAddress(CGNode *cg);