Merge pull request #67 from UB-CSE443/FunctionCalls

Function calls
This commit is contained in:
Annie Slenker
2025-05-04 19:32:16 -04:00
committed by GitHub
3 changed files with 75 additions and 27 deletions

View File

@ -493,6 +493,7 @@ int generateAssign(Instruction *inst) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
//add option for constant assignment (should be easy)
if (isConst(op1) == true) {
@ -500,9 +501,8 @@ int generateAssign(Instruction *inst) {
return 0;
}
CGNode *op1CG = findCG(getTN(op1));
if (op1CG == NULL) {
printdebug("generateAssign failed, op1 is not constant but not in CGlist");
else if (op1CG == NULL) {
printdebug("generateAssign failed, %s is not constant but not in CGlist", getName(getTN(op1)));
return -1;
}
@ -608,35 +608,61 @@ int generateCall(Instruction *inst){
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
if (op1 == NULL) {
printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED);
printdebug("%sgenerateFunctionCall failed, NULL operand", COLOR_RED);
return -1;
}
if (op1 == NULL) {
printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED);
printdebug("%sgenerateFunctionCall failed, NULL operand", COLOR_RED);
return -1;
}
if (getTN(op1) == NULL) {
printdebug("generateFunctionCall failed, NULL tablenode");
printdebug("generateFunctionCall failed, NULL tablenode1");
return -1;
}
if (getTN(op2) == NULL) {
printdebug("generateFunctionCall failed, NULL tablenode");
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, "\taddq\t$%d, %%rsp\n", 8 + getConst(op1));
//now for the return
CGNode *cg = findCG(getResult(inst));
if (cg == NULL) {
cg = addCG(getResult(inst), offset);
}
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#store return from call\n", getAddress(cg));
return 0;
}
int generateReturn(Instruction *inst){
return -1;
//will movl the result into the appropriate register and move the stack pointer/offset stuff back to correct value
TNodeOrConst *op1 = getOperand1(inst);
CGNode *cg;
if (op1 == NULL) {
printdebug("generateReturn failed, NULL operand");
return -1;
}
cg = findCG(getTN(op1));
if (cg == NULL) {
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)));
fprintf(cg_flag, "\tpopq\t%%rbp\n");
fprintf(cg_flag, "\tret\n");
return 0;
}
int generateCopyRight(Instruction *inst){
return -1;
@ -658,32 +684,53 @@ int generateParam(Instruction *inst){
CGNode *op1CG = findCG(getTN(op1));
if (op1CG == NULL) {
printdebug("generateParam failed, op1 is not in CGlist");
printdebug("generateParam failed, %s is not in CGlist", getName(getTN(op1)));
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#adding param start\n", getAddress(op1CG));
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#adding param end\n", offset += 8);
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#adding param end\n", getPrimSize(getTypeEntry(getTN(op1))));
return 0;
}
int generateFunctionStart(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
currentsp = offset;
TableNode *funDecTN = getResult(inst);
if (op1 == NULL) {
printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED);
return -1;
}
if (getTN(op1) == NULL) {
if (funDecTN == NULL) {
printdebug("generateFunctionStart failed, NULL tablenode");
return -1;
}
fprintf(cg_flag, "%s:\n", getName(getTN(op1)));
//this is independent of parameters:
fprintf(cg_flag, "%s:\n", getName(funDecTN));
fprintf(cg_flag, "\tpushq\t%%rbp\n");
fprintf(cg_flag, "\tmovq\t%%rsp, %%rbp\n");
//now we need to add the CGs of nodes to the CG list by doing assign from the
// have function declararation node
//declaration ->getType: if record, go through each element and load param from stack and store to correct tn cg
// if not, go get one element of type of param
//declaration ->getType->getDefinitionScope?: go through first n entries to get table nodes for params
TableNode *paramTN = getParameter(getTypeEntry(funDecTN));
SymbolTable *st = getFunScope(funDecTN);
int paramOffset = 16;
TableNode *tnToAdd = getFirstEntry(st);
if (getAdInfoType(paramTN) != TYPE_RECORD_TYPE) {
CGNode *paramCG = addCG(tnToAdd, offset);
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset);
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG));
} else {
int numParams = getRecLength(paramTN);
for (int i = 0; i < numParams; i++) {
CGNode *paramCG = addCG(tnToAdd, offset);
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset);
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG));
paramOffset = getPrimSize(getTypeEntry(tnToAdd));
tnToAdd = getNextEntry(tnToAdd);
}
}
return 0;
}

View File

@ -1403,7 +1403,7 @@ constant:
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, chara, temp, NULL);
emit_assignment(node, tn_or_const(CHARACTER,&$1));
printdebug("string of C_CHARACTER in constant is %s",$1);
//printdebug("string of C_CHARACTER in constant is %s",$1);
$$ = node;
}

View File

@ -29,7 +29,8 @@ function c: string2int_2_integer
function d: iic2b
d(x,y,z) := {
return (x < y & z < 'm');
[string: s]
return 0;
}
function entry: string2int