From 1546ea178534745e54df5ed81e5fa55f5e36ed13 Mon Sep 17 00:00:00 2001 From: Annie Date: Sat, 3 May 2025 13:13:22 -0400 Subject: [PATCH 1/4] just comments --- a.out | Bin 464 -> 0 bytes src/codegen.c | 6 ++++-- 2 files changed, 4 insertions(+), 2 deletions(-) delete mode 100644 a.out diff --git a/a.out b/a.out deleted file mode 100644 index 8540d49290617f7498e8df1dc190587a92561a76..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 464 zcmb<-^>JfjWMqH=Mg}_u1P><4z%T*9WN-kp9T->{SQw7G{$U1kkGuY00#jc&fK<2Z z5B~M8Upiesbi4lPcKyJ3q0{wEx9giu*B1lX*gfr5Zs{n+dVSqHOU L2E;)E==K8uL2Dw) diff --git a/src/codegen.c b/src/codegen.c index cbeadc0..806972e 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -680,10 +680,12 @@ int generateFunctionStart(Instruction *inst) { printdebug("generateFunctionStart failed, NULL tablenode"); return -1; } - + + //this is independent of parameters: fprintf(cg_flag, "%s:\n", getName(getTN(op1))); 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 return 0; } From 5d520645fd5bfd1a1a8abad1969a7b400ef1a6aa Mon Sep 17 00:00:00 2001 From: Annie Date: Sat, 3 May 2025 20:38:12 -0400 Subject: [PATCH 2/4] may have function calls, need to merge other stuff to check --- src/codegen.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index 806972e..79a489c 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -663,19 +663,20 @@ int generateParam(Instruction *inst){ } 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(tn))); return 0; } int generateFunctionStart(Instruction *inst) { - TNodeOrConst *op1 = getOperand1(inst); + TNodeOrConst *op1 = getResult(inst); if (op1 == NULL) { printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED); return -1; } - + + TableNode *funDecTN = getTN(op1); if (getTN(op1) == NULL) { printdebug("generateFunctionStart failed, NULL tablenode"); return -1; @@ -686,6 +687,30 @@ int generateFunctionStart(Instruction *inst) { 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 + //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(funDecTN); + SymbolTable *st = getFunScope(funDecTN); + int paramOffset = -16; + if (getAdInfoType(paramTN) != TYPE_RECORD) { + CGNode *paramCG = addCG(tn, currentsp); + 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))); + } else { + int numParams = getRecLength(paramTN); + TableNode *tnToAdd = getFirstEntry(st); + for (int i = 0; i < numParams; i++) { + CGNode *paramCG = addCG(tnTooAdd, currentsp); + 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))); + paramoffset -= getPrimSize(getTypeEntry(tnToAdd)); + tnToAdd = getNextEntry(tnToAdd); + } + } + return 0; } From 3e1c510c68c068ca3137cd3b1240dba245ff390e Mon Sep 17 00:00:00 2001 From: Annie Date: Sat, 3 May 2025 21:14:24 -0400 Subject: [PATCH 3/4] seems to work for records, but the offsets are wrong (1) --- src/codegen.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index 79a489c..a3c4dff 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -663,27 +663,21 @@ int generateParam(Instruction *inst){ } 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", getPrimSize(getTypeEntry(tn))); + 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 = getResult(inst); + TableNode *funDecTN = getResult(inst); - if (op1 == NULL) { - printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED); - return -1; - } - - TableNode *funDecTN = getTN(op1); - if (getTN(op1) == NULL) { + if (funDecTN == NULL) { printdebug("generateFunctionStart failed, NULL tablenode"); return -1; } //this is independent of parameters: - fprintf(cg_flag, "%s:\n", getName(getTN(op1))); + fprintf(cg_flag, "%s:\n", getName(funDecTN)); fprintf(cg_flag, "\tpushq\t%%rbp\n"); fprintf(cg_flag, "\tmovq\t%%rsp, %%rbp\n"); @@ -693,21 +687,23 @@ int generateFunctionStart(Instruction *inst) { // 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(funDecTN); + TableNode *paramTN = getParameter(getTypeEntry(funDecTN)); SymbolTable *st = getFunScope(funDecTN); - int paramOffset = -16; - if (getAdInfoType(paramTN) != TYPE_RECORD) { - CGNode *paramCG = addCG(tn, currentsp); + int paramOffset = 16; + if (getAdInfoType(paramTN) != TYPE_RECORD_TYPE) { + fprintf(stderr, "%d\n", getAdInfoType(funDecTN)); + CGNode *paramCG = addCG(paramTN, currentsp); 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))); } else { + fprintf(stderr, "record type\n"); int numParams = getRecLength(paramTN); TableNode *tnToAdd = getFirstEntry(st); for (int i = 0; i < numParams; i++) { - CGNode *paramCG = addCG(tnTooAdd, currentsp); + CGNode *paramCG = addCG(tnToAdd, currentsp); 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))); - paramoffset -= getPrimSize(getTypeEntry(tnToAdd)); + paramOffset += getPrimSize(getTypeEntry(tnToAdd)); tnToAdd = getNextEntry(tnToAdd); } } From 85629fbf14114b1423737180465781530a7f13b2 Mon Sep 17 00:00:00 2001 From: Annie Date: Sun, 4 May 2025 16:44:55 -0400 Subject: [PATCH 4/4] function calls are starting to work i think --- src/codegen.c | 70 +++++++++++++++++-------- src/grammar.y | 2 +- tests/carl/NoErrors/functionValue.alpha | 6 +-- 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index a3c4dff..49748ce 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -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,7 +684,7 @@ 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; } @@ -668,7 +694,7 @@ int generateParam(Instruction *inst){ } int generateFunctionStart(Instruction *inst) { - + currentsp = offset; TableNode *funDecTN = getResult(inst); if (funDecTN == NULL) { @@ -690,20 +716,18 @@ int generateFunctionStart(Instruction *inst) { TableNode *paramTN = getParameter(getTypeEntry(funDecTN)); SymbolTable *st = getFunScope(funDecTN); int paramOffset = 16; + TableNode *tnToAdd = getFirstEntry(st); if (getAdInfoType(paramTN) != TYPE_RECORD_TYPE) { - fprintf(stderr, "%d\n", getAdInfoType(funDecTN)); - CGNode *paramCG = addCG(paramTN, currentsp); + 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", getPrimSize(getTypeEntry(paramTN))); + fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG)); } else { - fprintf(stderr, "record type\n"); int numParams = getRecLength(paramTN); - TableNode *tnToAdd = getFirstEntry(st); 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%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getPrimSize(getTypeEntry(paramTN))); - paramOffset += getPrimSize(getTypeEntry(tnToAdd)); + fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG)); + paramOffset = getPrimSize(getTypeEntry(tnToAdd)); tnToAdd = getNextEntry(tnToAdd); } } diff --git a/src/grammar.y b/src/grammar.y index 9d5d5a3..fb33253 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -1244,7 +1244,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; } diff --git a/tests/carl/NoErrors/functionValue.alpha b/tests/carl/NoErrors/functionValue.alpha index c1134ce..cac92bb 100644 --- a/tests/carl/NoErrors/functionValue.alpha +++ b/tests/carl/NoErrors/functionValue.alpha @@ -28,8 +28,8 @@ function c: string2int_2_integer function d: iic2b d(x,y,z) := { - [string: s;integer: s] - return (x < y & z < 'm'); + [string: s] + return 0; } function entry: string2int @@ -61,7 +61,7 @@ entry(arg) := { temp := a("Hello"); f := b(temp); result := c(f); - if (d(1,2,char)) + if (d(1,2,'a')) then { result := 0; }