diff --git a/src/codegen.c b/src/codegen.c index 3393217..1784a09 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -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,21 +684,24 @@ 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)); - + if (cg == NULL) { cg = addCG(getResult(inst), offset); } @@ -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; } diff --git a/src/codegen.h b/src/codegen.h index d790ea9..828f16a 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -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);