From e598fad62aaf4d7f23a375c8dd85c250018d5081 Mon Sep 17 00:00:00 2001 From: Annie Date: Fri, 2 May 2025 16:12:35 -0400 Subject: [PATCH] idk what i did anymore --- a.out | Bin 0 -> 464 bytes integer | 0 src/codegen.c | 106 +++++++++++++++++++++----- src/codegen.h | 3 +- src/codegen.h~ | 2 + src/intermediate_code.h | 55 ++++++------- tests/sprint4/test/sp4_cg_demo.alpha | 8 ++ tests/sprint4/test/sp4_cg_demo.alpha~ | 8 ++ tests/sprint4/test/sp4_cg_demo.cg | 9 +++ 9 files changed, 146 insertions(+), 45 deletions(-) create mode 100644 a.out create mode 100644 integer create mode 100644 src/codegen.h~ create mode 100644 tests/sprint4/test/sp4_cg_demo.alpha create mode 100644 tests/sprint4/test/sp4_cg_demo.alpha~ create mode 100644 tests/sprint4/test/sp4_cg_demo.cg diff --git a/a.out b/a.out new file mode 100644 index 0000000000000000000000000000000000000000..8540d49290617f7498e8df1dc190587a92561a76 GIT binary patch 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) literal 0 HcmV?d00001 diff --git a/integer b/integer new file mode 100644 index 0000000..e69de29 diff --git a/src/codegen.c b/src/codegen.c index 250d2a4..a9d8759 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -5,6 +5,7 @@ int generate(){ offset = 0; + currentsp = 0; Instruction *i = begin; while (i != NULL) { switch(getOp(i)) { @@ -74,6 +75,9 @@ int generate(){ case E_ADDRESS_OF: generateAddressOf(i); break; + case E_FUNC_START: + generateFunctionStart(i); + break; default: ; } @@ -118,7 +122,7 @@ CGNode *findCG(TableNode *tn) { CGNode *addCG(TableNode *tn, int sp) { CGNode *cg = calloc(1, sizeof(CGNode)); cg->tn = tn; - offset += 4; // <- quick fix getPrimSize(getTypeEntry(tn)) + offset += getPrimSize(getTypeEntry(tn)); cg->address = offset; cg->next = cgList; cgList = cg; @@ -127,7 +131,12 @@ CGNode *addCG(TableNode *tn, int sp) { int generateLabel(Instruction *inst) { + if (inst == NULL) { + return -1; + } + fprintf(cg_flag, ".L%d:\n", getLabel(inst)); + return 0; } int generateAdd(Instruction *inst) { @@ -151,7 +160,7 @@ int generateAdd(Instruction *inst) { CGNode *op1CG = findCG(getTN(op1)); - CGNode *op2CG = findCG(getTN(op1)); + CGNode *op2CG = findCG(getTN(op2)); if (op1CG == NULL) { printdebug("generateAdd failed, %s is not initialized/in CG", getName(getTN(op1))); return -1; @@ -503,20 +512,20 @@ int generateAssign(Instruction *inst) { return 0; } -int generateGoto(Instruction *instruction){ +int generateGoto(Instruction *inst){ return -1; } -int generateCondGoto(Instruction *instruction) { +int generateCondGoto(Instruction *inst) { return -1; } -int generateIfTrue(Instruction *instruction){ +int generateIfTrue(Instruction *inst){ return -1; // might just be a goto for where to go if something is true, or returning if something is true, or checking if true and writing goto if thats the case } -int generateIfFalse(Instruction *instruction){ +int generateIfFalse(Instruction *inst){ return -1; } @@ -595,24 +604,87 @@ int generateEqualTo(Instruction *inst){ fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\t#equal to end\n", getAddress(cg)); return 0; } -int generateCall(Instruction *instruction){ - return -1; - //will want to store parameters and then update the offset by adding 8? for stack pointer stuff, can then print call subroutine name, followed by movl of the result into the result's cg +int generateCall(Instruction *inst){ + + TNodeOrConst *op1 = getOperand1(inst); + TNodeOrConst *op2 = getOperand2(inst); + if (op1 == NULL) { + printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED); + return -1; + } + + if (op1 == NULL) { + printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED); + return -1; + } + + if (getTN(op1) == NULL) { + printdebug("generateFunctionCall failed, NULL tablenode"); + return -1; + } + + if (getTN(op2) == NULL) { + printdebug("generateFunctionCall failed, NULL tablenode"); + return -1; + } + + + fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1))); + fprintf(cg_flag, "\taddq\t$%d, %%rsp\n", 8 + getConst(op1)); + + return 0; + } -int generateReturn(Instruction *instruction){ +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 } -int generateCopyRight(Instruction *instruction){ +int generateCopyRight(Instruction *inst){ return -1; } -int generateCopyLeft(Instruction *instruction){ +int generateCopyLeft(Instruction *inst){ return -1; } -int generateAddressOf(Instruction *instruction){ +int generateAddressOf(Instruction *inst){ return -1; } -int generateParam(Instruction *instruction){ - //need to check if op1 is null, then add it to the appropriate register/cg node. need a way to keep track of this, maybe just have global count of params generated - return -1; -} \ No newline at end of file +int generateParam(Instruction *inst){ + TNodeOrConst *op1 = getOperand1(inst); + + if (op1 == NULL) { + printdebug("generateParam failed, NULL operand"); + return -1; + } + + CGNode *op1CG = findCG(getTN(op1)); + if (op1CG == NULL) { + printdebug("generateParam failed, op1 is not in CGlist"); + 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); + return 0; +} + +int generateFunctionStart(Instruction *inst) { + + TNodeOrConst *op1 = getOperand1(inst); + + if (op1 == NULL) { + printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED); + return -1; + } + + if (getTN(op1) == NULL) { + printdebug("generateFunctionStart failed, NULL tablenode"); + return -1; + } + + fprintf(cg_flag, "%s:\n", getName(getTN(op1))); + fprintf(cg_flag, "\tpushq\t%%rbp\n"); + fprintf(cg_flag, "\tmovq\t%%rsp, %%rbp\n"); + + return 0; + +} diff --git a/src/codegen.h b/src/codegen.h index 3203a52..2d67a6e 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -50,6 +50,7 @@ int generateCopyRight(Instruction *instruction); int generateCopyLeft(Instruction *instruction); int generateAddressOf(Instruction *instruction); int generateParam(Instruction *instruction); +int generateFunctionStart(Instruction *instruction); extern int label_count; extern Instruction *begin; @@ -57,4 +58,4 @@ extern Instruction *current; extern int offset; extern int currentsp; -extern CGNode *cgList; \ No newline at end of file +extern CGNode *cgList; diff --git a/src/codegen.h~ b/src/codegen.h~ new file mode 100644 index 0000000..2d4c660 --- /dev/null +++ b/src/codegen.h~ @@ -0,0 +1,2 @@ +/* Code Generation File - Contains functions to generate assembly code */ + diff --git a/src/intermediate_code.h b/src/intermediate_code.h index c1efb76..bce2b17 100644 --- a/src/intermediate_code.h +++ b/src/intermediate_code.h @@ -13,32 +13,33 @@ #include "symbol_table.h" // these are from page 364 -typedef enum { - E_LABEL = 10000, // this is not in the book - E_ADD, // 1 from the list - E_SUB, // 1 - E_MUL, // 1 - E_DIV, // 1 - E_MOD, // 1 - E_OR, // 1 - E_AND, // 1 - E_NEG, // 2 - E_NOT, // 2 - E_ASSIGN, // 3 - E_GOTO, // 4 - E_COND_GOTO, // 5 I don't thik I need this because we could just follow the < or the = and just assume that it's a cond got - E_IF_X_TRUE, // 5 - E_IF_X_FALSE, // 5 - E_LESS_THAN, // 6 rule 1 + 5 - E_EQUAL_TO, // 6 rule 1 + 5 - E_CALL, // 7 - E_PARAM, // 7 - E_RETURN, // 7 - E_INDEX_COPY_RIGHT, // 8 this is x = y[i] - E_INDEX_COPY_LEFT, // 8 x[i] = y - E_ADDRESS_OF, // 9 x = &y - E_DEREF_RIGHT, // 9 x = *y - E_DEREF_LEFT // 9 x* = y +typedef enum { // these are from page 364 + E_LABEL = 10000, // this is not in the book + E_FUNC_START, + E_ADD, // 1 from the list + E_SUB, // 1 + E_MUL, // 1 + E_DIV, // 1 + E_MOD, // 1 TODO: Please change to REM + E_OR, // 1 + E_AND, // 1 + E_NEG, // 2 + E_NOT, // 2 + E_ASSIGN, // 3 + E_GOTO, // 4 + E_COND_GOTO, // 5 I don't thik I need this because we could just follow the < or the = and just assume that it's a cond got + E_IF_X_TRUE, // 5 + E_IF_X_FALSE, // 5 + E_LESS_THAN, // 6 rule 1 + 5 + E_EQUAL_TO, // 6 rule 1 + 5 + E_CALL, // 7 + E_PARAM, // 7 + E_RETURN, // 7 + E_INDEX_COPY_RIGHT, // 8 this is x = y[i] + E_INDEX_COPY_LEFT, // 8 x[i] = y + E_ADDRESS_OF, // 9 x = &y + E_DEREF_RIGHT, // 9 x = *y + E_DEREF_LEFT // 9 x* = y } Op; typedef enum { @@ -121,4 +122,4 @@ extern Instruction* current; extern int offset; extern int currentsp; -extern CGNode* cgList; \ No newline at end of file +extern CGNode* cgList; diff --git a/tests/sprint4/test/sp4_cg_demo.alpha b/tests/sprint4/test/sp4_cg_demo.alpha new file mode 100644 index 0000000..e74c5ad --- /dev/null +++ b/tests/sprint4/test/sp4_cg_demo.alpha @@ -0,0 +1,8 @@ +type main: string -> integer +function entry: main + +entry(arg) := { + [integer:x] + x := 3 + 2 * 8; + return x; + } \ No newline at end of file diff --git a/tests/sprint4/test/sp4_cg_demo.alpha~ b/tests/sprint4/test/sp4_cg_demo.alpha~ new file mode 100644 index 0000000..6f69389 --- /dev/null +++ b/tests/sprint4/test/sp4_cg_demo.alpha~ @@ -0,0 +1,8 @@ +type main: string -> integer +function entry: main + +entry(arg) := { + [integer:x] + x := 3 + 2 * 8; + return 0; + } \ No newline at end of file diff --git a/tests/sprint4/test/sp4_cg_demo.cg b/tests/sprint4/test/sp4_cg_demo.cg new file mode 100644 index 0000000..32d366c --- /dev/null +++ b/tests/sprint4/test/sp4_cg_demo.cg @@ -0,0 +1,9 @@ +type main: string -> integer +function entry: main + +entry(arg) := { + [integer:x; Boolean b] + x := 3 + 2 * 8; + b := x < 1; + return 0; + } \ No newline at end of file