function calls are starting to work i think

This commit is contained in:
Annie
2025-05-04 16:44:55 -04:00
parent 3e1c510c68
commit 85629fbf14
3 changed files with 51 additions and 27 deletions

View File

@ -493,6 +493,7 @@ int generateAssign(Instruction *inst) {
cg = addCG(getResult(inst), offset); cg = addCG(getResult(inst), offset);
} }
CGNode *op1CG = findCG(getTN(op1));
//add option for constant assignment (should be easy) //add option for constant assignment (should be easy)
if (isConst(op1) == true) { if (isConst(op1) == true) {
@ -500,9 +501,8 @@ int generateAssign(Instruction *inst) {
return 0; return 0;
} }
CGNode *op1CG = findCG(getTN(op1)); else if (op1CG == NULL) {
if (op1CG == NULL) { printdebug("generateAssign failed, %s is not constant but not in CGlist", getName(getTN(op1)));
printdebug("generateAssign failed, op1 is not constant but not in CGlist");
return -1; return -1;
} }
@ -608,35 +608,61 @@ int generateCall(Instruction *inst){
TNodeOrConst *op1 = getOperand1(inst); TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst); TNodeOrConst *op2 = getOperand2(inst);
if (op1 == NULL) { if (op1 == NULL) {
printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED); printdebug("%sgenerateFunctionCall failed, NULL operand", COLOR_RED);
return -1; return -1;
} }
if (op1 == NULL) { if (op1 == NULL) {
printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED); printdebug("%sgenerateFunctionCall failed, NULL operand", COLOR_RED);
return -1; return -1;
} }
if (getTN(op1) == NULL) { if (getTN(op1) == NULL) {
printdebug("generateFunctionCall failed, NULL tablenode"); printdebug("generateFunctionCall failed, NULL tablenode1");
return -1; return -1;
} }
if (getTN(op2) == NULL) { // if (getTN(op2) == NULL) {
printdebug("generateFunctionCall failed, NULL tablenode"); // printdebug("generateFunctionCall failed, NULL tablenode2");
return -1; // return -1;
} //}
fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1))); fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1)));
fprintf(cg_flag, "\taddq\t$%d, %%rsp\n", 8 + getConst(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; return 0;
} }
int generateReturn(Instruction *inst){ int generateReturn(Instruction *inst){
return -1; TNodeOrConst *op1 = getOperand1(inst);
//will movl the result into the appropriate register and move the stack pointer/offset stuff back to correct value 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){ int generateCopyRight(Instruction *inst){
return -1; return -1;
@ -658,7 +684,7 @@ int generateParam(Instruction *inst){
CGNode *op1CG = findCG(getTN(op1)); CGNode *op1CG = findCG(getTN(op1));
if (op1CG == NULL) { if (op1CG == NULL) {
printdebug("generateParam failed, op1 is not in CGlist"); printdebug("generateParam failed, %s is not in CGlist", getName(getTN(op1)));
return -1; return -1;
} }
@ -668,7 +694,7 @@ int generateParam(Instruction *inst){
} }
int generateFunctionStart(Instruction *inst) { int generateFunctionStart(Instruction *inst) {
currentsp = offset;
TableNode *funDecTN = getResult(inst); TableNode *funDecTN = getResult(inst);
if (funDecTN == NULL) { if (funDecTN == NULL) {
@ -690,20 +716,18 @@ int generateFunctionStart(Instruction *inst) {
TableNode *paramTN = getParameter(getTypeEntry(funDecTN)); TableNode *paramTN = getParameter(getTypeEntry(funDecTN));
SymbolTable *st = getFunScope(funDecTN); SymbolTable *st = getFunScope(funDecTN);
int paramOffset = 16; int paramOffset = 16;
TableNode *tnToAdd = getFirstEntry(st);
if (getAdInfoType(paramTN) != TYPE_RECORD_TYPE) { if (getAdInfoType(paramTN) != TYPE_RECORD_TYPE) {
fprintf(stderr, "%d\n", getAdInfoType(funDecTN)); CGNode *paramCG = addCG(tnToAdd, offset);
CGNode *paramCG = addCG(paramTN, currentsp);
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset); 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", getPrimSize(getTypeEntry(paramTN))); fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG));
} else { } else {
fprintf(stderr, "record type\n");
int numParams = getRecLength(paramTN); int numParams = getRecLength(paramTN);
TableNode *tnToAdd = getFirstEntry(st);
for (int i = 0; i < numParams; i++) { for (int i = 0; i < numParams; i++) {
CGNode *paramCG = addCG(tnToAdd, currentsp); CGNode *paramCG = addCG(tnToAdd, offset);
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset); 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", getPrimSize(getTypeEntry(paramTN))); fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG));
paramOffset += getPrimSize(getTypeEntry(tnToAdd)); paramOffset = getPrimSize(getTypeEntry(tnToAdd));
tnToAdd = getNextEntry(tnToAdd); tnToAdd = getNextEntry(tnToAdd);
} }
} }

View File

@ -1244,7 +1244,7 @@ constant:
char* temp = temp_var_gen(); char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, chara, temp, NULL); TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, chara, temp, NULL);
emit_assignment(node, tn_or_const(CHARACTER,&$1)); 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; $$ = node;
} }

View File

@ -28,8 +28,8 @@ function c: string2int_2_integer
function d: iic2b function d: iic2b
d(x,y,z) := { d(x,y,z) := {
[string: s;integer: s] [string: s]
return (x < y & z < 'm'); return 0;
} }
function entry: string2int function entry: string2int
@ -61,7 +61,7 @@ entry(arg) := {
temp := a("Hello"); temp := a("Hello");
f := b(temp); f := b(temp);
result := c(f); result := c(f);
if (d(1,2,char)) if (d(1,2,'a'))
then { then {
result := 0; result := 0;
} }