From 1c5b7de5fd3dab8d48fc3d120d49e5140842d0b0 Mon Sep 17 00:00:00 2001 From: Annie Date: Mon, 5 May 2025 15:26:27 -0400 Subject: [PATCH] FUNCTION CALLS WORK WITH 6+ ENTRIES YIPPEEgit add . --- src/codegen.c | 70 ++++++++++++++++++++++---- src/codegen.h | 7 +++ tests/sprint4/test/sp4_cg_calls.alpha | 34 +++++++++++++ tests/sprint4/test/sp4_cg_calls.alpha~ | 34 +++++++++++++ 4 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 tests/sprint4/test/sp4_cg_calls.alpha create mode 100644 tests/sprint4/test/sp4_cg_calls.alpha~ diff --git a/src/codegen.c b/src/codegen.c index 6da4ddf..492d3df 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -2,7 +2,7 @@ /* The Translators - Spring 2025 */ #include "codegen.h" - +int paramCount = 0; int generate() { offset = 0; currentsp = 0; @@ -603,6 +603,7 @@ int generateEqualTo(Instruction *inst) { return 0; } int generateCall(Instruction *inst) { + paramCount = 0; TNodeOrConst *op1 = getOperand1(inst); TNodeOrConst *op2 = getOperand2(inst); if (op1 == NULL) { @@ -670,7 +671,7 @@ int generateAddressOf(Instruction *inst) { int generateParam(Instruction *inst) { TNodeOrConst *op1 = getOperand1(inst); - + fprintf(stderr, "generate param reached\n"); if (op1 == NULL) { printdebug("generateParam failed, NULL operand"); return -1; @@ -681,8 +682,31 @@ int generateParam(Instruction *inst) { printdebug("generateParam failed, %s is not in CGlist", getName(getTN(op1))); return -1; } - - fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%edi\t#adding param start\n", getAddress(op1CG)); + paramCount += 1; + switch(paramCount) { + case 1: + fprintf(cg_flag, "\tmovl\t%d(%%rbp), %s\t#adding param start\n", getAddress(op1CG), REG1); + break; + case 2: + fprintf(cg_flag, "\tmovl\t%d(%%rbp), %s\t#adding param start\n", getAddress(op1CG), REG2); + break; + case 3: + fprintf(cg_flag, "\tmovl\t%d(%%rbp), %s\t#adding param start\n", getAddress(op1CG), REG3); + break; + case 4: + fprintf(cg_flag, "\tmovl\t%d(%%rbp), %s\t#adding param start\n", getAddress(op1CG), REG4); + break; + case 5: + fprintf(cg_flag, "\tmovl\t%d(%%rbp), %s\t#adding param start\n", getAddress(op1CG), REG5); + break; + case 6: + fprintf(cg_flag, "\tmovl\t%d(%%rbp), %s\t#adding param start\n", getAddress(op1CG), REG6); + break; + default: + offset += getPrimSize(getTypeEntry(getTN(op1))); + 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 - getPrimSize(getTypeEntry(getTN(op1)))); + } return 0; } @@ -712,16 +736,40 @@ int generateFunctionStart(Instruction *inst) { 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)); + CGNode *paramCG = addCG(tnToAdd, offset); + paramOffset += getPrimSize(tnToAdd); + fprintf(cg_flag, "\tmovl\t%%edi, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG)); } else { int numParams = getRecLength(paramTN); - for (int i = 0; i < numParams; i++) { + for (int i = numParams; i > 0; 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)); + switch(i) { + case 1: + fprintf(cg_flag, "\tmovl\t%s, %d(%%rbp)\t#FunctionStart1param end\n", REG1, getAddress(paramCG)); + break; + case 2: + fprintf(cg_flag, "\tmovl\t%s, %d(%%rbp)\t#FunctionStart1param end\n", REG2, getAddress(paramCG)); + break; + case 3: + fprintf(cg_flag, "\tmovl\t%s, %d(%%rbp)\t#FunctionStart1param end\n", REG3, getAddress(paramCG)); + break; + case 4: + fprintf(cg_flag, "\tmovl\t%s, %d(%%rbp)\t#FunctionStart1param end\n", REG4, getAddress(paramCG)); + break; + case 5: + fprintf(cg_flag, "\tmovl\t%s, %d(%%rbp)\t#FunctionStart1param end\n", REG5, getAddress(paramCG)); + break; + case 6: + fprintf(cg_flag, "\tmovl\t%s, %d(%%rbp)\t#FunctionStart1param end\n", REG6, getAddress(paramCG)); + break; + default: + paramOffset += getPrimSize(getTypeEntry(tnToAdd)); + offset += getPrimSize(getTypeEntry(tnToAdd)); + 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)); + + } + tnToAdd = getNextEntry(tnToAdd); } } diff --git a/src/codegen.h b/src/codegen.h index 2d67a6e..bf1d7cd 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -13,6 +13,13 @@ #include "intermediate_code.h" #include "symbol_table.h" +#define REG1 "%edi" +#define REG2 "%esi" +#define REG3 "%edx" +#define REG4 "%ecx" +#define REG5 "%r8d" +#define REG6 "%r9d" + extern FILE *cg_flag; typedef struct CGNode { diff --git a/tests/sprint4/test/sp4_cg_calls.alpha b/tests/sprint4/test/sp4_cg_calls.alpha new file mode 100644 index 0000000..45a1512 --- /dev/null +++ b/tests/sprint4/test/sp4_cg_calls.alpha @@ -0,0 +1,34 @@ +(* TEST: [-asc -tc -cg -ir] *) + +#include "std.alpha" +(* Standard Alpha Library - Provided by Carl *) + +type string: 1 -> character +function entry: string2integer + + +type rec: [integer: x; integer: y; integer: z; integer: a; integer: b; integer: c; integer: d] + +type T2: rec -> integer +type T: integer -> integer +type main: string -> integer +function entry: main +function bar: T2 +function ahh: T + +ahh (a) := { + a := printInteger(a); + return -1; +} + +bar (a, b, c,d,e,f,g) := { + a := printInteger(g); + return b; +} + +entry (arg) := { + [integer:x; integer:y; integer: result] + + result := bar(1,2,3,4,5,6,7); + return 1; +} diff --git a/tests/sprint4/test/sp4_cg_calls.alpha~ b/tests/sprint4/test/sp4_cg_calls.alpha~ new file mode 100644 index 0000000..5a69469 --- /dev/null +++ b/tests/sprint4/test/sp4_cg_calls.alpha~ @@ -0,0 +1,34 @@ +(* TEST: [-asc -tc -cg -ir] *) + +#include "std.alpha" +(* Standard Alpha Library - Provided by Carl *) + +type string: 1 -> character +function entry: string2integer + + +type rec: [integer: x; integer: y; integer: z; integer: a; integer: b; integer: c; integer: d] + +type T2: rec -> integer +type T: integer -> integer +type main: string -> integer +function entry: main +function bar: T2 +function ahh: T + +ahh (a) := { + a := printInteger(a); + return -1; +} + +bar (a, b, c,d,e,f,g) := { + a := printInteger(f); + return b; +} + +entry (arg) := { + [integer:x; integer:y; integer: result] + + result := bar(1,2,3,4,5,6,7); + return 1; +}