From 5d520645fd5bfd1a1a8abad1969a7b400ef1a6aa Mon Sep 17 00:00:00 2001 From: Annie Date: Sat, 3 May 2025 20:38:12 -0400 Subject: [PATCH] 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; }