From bab71df5172626182581f3cc77edbd1c0641e87c Mon Sep 17 00:00:00 2001 From: Annie Date: Tue, 6 May 2025 15:53:24 -0400 Subject: [PATCH] hopefully fixed merge --- library/std.alpha | 2 +- src/codegen.c | 188 +++++++++++++++----------- src/codegen.h | 9 +- src/grammar.h | 2 +- src/intermediate_code.c | 4 + tests/given/test/functionValue.alpha | 6 +- tests/sprint4/test/sp4_cg_add.alpha | 4 +- tests/sprint4/test/sp4_cg_calls.alpha | 6 +- 8 files changed, 135 insertions(+), 86 deletions(-) diff --git a/library/std.alpha b/library/std.alpha index 2d0f1a9..19b278a 100644 --- a/library/std.alpha +++ b/library/std.alpha @@ -32,4 +32,4 @@ external function printB: Boolean2integer external function inS: integer2string external function inI: integer2integer -external function inC: integer2character \ No newline at end of file +external function inC: integer2character diff --git a/src/codegen.c b/src/codegen.c index 492d3df..9de14e7 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -3,92 +3,99 @@ #include "codegen.h" int paramCount = 0; +int funDec = 0; int generate() { offset = 0; currentsp = 0; Instruction *i = begin; - + funList = NULL; // temporary fprintf(cg_flag, ".globl entry\n"); while (i != NULL) { switch (getOp(i)) { - case E_LABEL: - generateLabel(i); - break; - case E_ADD: - generateAdd(i); - break; - case E_SUB: - generateSub(i); - break; - case E_MUL: - generateMult(i); - break; - case E_DIV: - generateDiv(i); - break; - case E_MOD: - generateMod(i); - break; - case E_OR: - generateOr(i); - break; - case E_AND: - generateAnd(i); - break; - case E_NEG: - generateNeg(i); - break; - case E_NOT: - generateNot(i); - break; - case E_ASSIGN: - generateAssign(i); - break; - case E_GOTO: - generateGoto(i); - break; - case E_IF_X_TRUE: - generateIfTrue(i); - break; - case E_IF_X_FALSE: - generateIfFalse(i); - break; - case E_LESS_THAN: - generateLessThan(i); - break; - case E_EQUAL_TO: - generateEqualTo(i); - break; - case E_CALL: - generateCall(i); - break; - case E_PARAM: - generateParam(i); - break; - case E_RETURN: - generateReturn(i); - break; - case E_INDEX_COPY_RIGHT: - generateCopyRight(i); - break; - case E_INDEX_COPY_LEFT: - generateCopyLeft(i); - break; - case E_ADDRESS_OF: - generateAddressOf(i); - break; - case E_FUNC_START: - generateFunctionStart(i); - break; - default:; + case E_LABEL: + generateLabel(i); + break; + case E_ADD: + generateAdd(i); + break; + case E_SUB: + generateSub(i); + break; + case E_MUL: + generateMult(i); + break; + case E_DIV: + generateDiv(i); + break; + case E_MOD: + generateMod(i); + break; + case E_OR: + generateOr(i); + break; + case E_AND: + generateAnd(i); + break; + case E_NEG: + generateNeg(i); + break; + case E_NOT: + generateNot(i); + break; + case E_ASSIGN: + generateAssign(i); + break; + case E_GOTO: + generateGoto(i); + break; + case E_IF_X_TRUE: + generateIfTrue(i); + break; + case E_IF_X_FALSE: + generateIfFalse(i); + break; + case E_LESS_THAN: + generateLessThan(i); + break; + case E_EQUAL_TO: + generateEqualTo(i); + break; + case E_CALL: + generateCall(i); + break; + case E_PARAM: + generateParam(i); + break; + case E_RETURN: + generateReturn(i); + break; + case E_INDEX_COPY_RIGHT: + generateCopyRight(i); + break; + case E_INDEX_COPY_LEFT: + generateCopyLeft(i); + break; + case E_ADDRESS_OF: + generateAddressOf(i); + break; + case E_FUNC_START: + generateFunctionStart(i); + break; + case E_FUNC_DEC: + generateFunctionDec(i); + default:; } i = i->next; } return -1; } +void align(TableNode *tn) { + int size = getPrimSize(getTypeEntry(tn)); + offset += offset % size; +} CGNode *getNextCG(CGNode *cg) { if (cg == NULL) { return NULL; @@ -125,6 +132,7 @@ CGNode *findCG(TableNode *tn) { CGNode *addCG(TableNode *tn, int sp) { CGNode *cg = calloc(1, sizeof(CGNode)); cg->tn = tn; + align(tn); offset += getPrimSize(getTypeEntry(tn)); cg->address = offset; cg->next = cgList; @@ -132,6 +140,13 @@ CGNode *addCG(TableNode *tn, int sp) { return cg; } +int generateFunctionDec(Instruction *i) { + FunDecList * fd = calloc(1, sizeof(FunDecList)); + fd->tn = getResult(i); + fd->next = funList; + funList = fd; + return 0; +} int generateLabel(Instruction *inst) { if (inst == NULL) { return -1; @@ -511,7 +526,8 @@ int generateAssign(Instruction *inst) { } int generateGoto(Instruction *inst) { - return -1; + fprintf(cg_flag, "\tjmp\t.L%d\n", getLabel(inst)); + return 0; } int generateCondGoto(Instruction *inst) { @@ -520,7 +536,6 @@ int generateCondGoto(Instruction *inst) { 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 *inst) { @@ -621,13 +636,18 @@ int generateCall(Instruction *inst) { return -1; } + //if (findCG(getTN(op1)) == NULL) { + // printdebug("generateFunctionCall failed, function not in stack"); + // 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, "\tmovq\t%d(%%rbp), %%rax\t#call %s!\n", getAddress(findCG(getTN(op1))), getName(getTN(op1))); + //fprintf(cg_flag, "\tcall *%%rax\n"); //now for the return CGNode *cg = findCG(getResult(inst)); @@ -703,6 +723,7 @@ int generateParam(Instruction *inst) { fprintf(cg_flag, "\tmovl\t%d(%%rbp), %s\t#adding param start\n", getAddress(op1CG), REG6); break; default: + align(getTN(op1)); 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)))); @@ -737,7 +758,8 @@ int generateFunctionStart(Instruction *inst) { TableNode *tnToAdd = getFirstEntry(st); if (getAdInfoType(paramTN) != TYPE_RECORD_TYPE) { CGNode *paramCG = addCG(tnToAdd, offset); - paramOffset += getPrimSize(tnToAdd); + int size = getPrimSize(getTypeEntry(tnToAdd)); + paramOffset += getPrimSize(getTypeEntry(tnToAdd)); fprintf(cg_flag, "\tmovl\t%%edi, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG)); } else { int numParams = getRecLength(paramTN); @@ -763,7 +785,8 @@ int generateFunctionStart(Instruction *inst) { fprintf(cg_flag, "\tmovl\t%s, %d(%%rbp)\t#FunctionStart1param end\n", REG6, getAddress(paramCG)); break; default: - paramOffset += getPrimSize(getTypeEntry(tnToAdd)); + int size = getPrimSize(getTypeEntry(tnToAdd)); + paramOffset += size; 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)); @@ -774,5 +797,18 @@ int generateFunctionStart(Instruction *inst) { } } + if (strcmp(getName(funDecTN), "entry") == 0) { + if (funList == NULL) { + return -1; + } + fprintf(stderr, "here\n"); + TableNode *funDec; + while (funList != NULL) { + funDec = funList->tn; + CGNode *cg = addCG(getResult(inst), offset); + fprintf(cg_flag, "\tmovq\t$%s, %d(%%rbp)\t#storing function declaration\n", getName(funDec), getAddress(cg)); + funList = funList->next; + } + } return 0; } diff --git a/src/codegen.h b/src/codegen.h index bf1d7cd..0667445 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -25,15 +25,21 @@ extern FILE *cg_flag; typedef struct CGNode { TableNode *tn; int address; - CGNode *next; + struct CGNode *next; } CGNode; +typedef struct FunDecList { + TableNode *tn; + struct FunDecList *next; +} FunDecList; + int generate(); CGNode *getNextCG(CGNode *cg); int getAddress(CGNode *cg); TableNode *getTNofCG(CGNode *cg); CGNode *findCG(TableNode *tn); CGNode *addCG(TableNode *tn, int sp); +int generateFunctionDec(Instruction *inst); int generateLabel(Instruction *inst); int generateAdd(Instruction *inst); int generateSub(Instruction *instruction); @@ -66,3 +72,4 @@ extern Instruction *current; extern int offset; extern int currentsp; extern CGNode *cgList; +extern FunDecList *funList; diff --git a/src/grammar.h b/src/grammar.h index ec850a2..94384d8 100644 --- a/src/grammar.h +++ b/src/grammar.h @@ -31,7 +31,7 @@ Instruction *current; int offset; int currentsp; CGNode *cgList; - +FunDecList *funList; Stack *stack; Stack *TrueList; Stack *FalseList; diff --git a/src/intermediate_code.c b/src/intermediate_code.c index beccafd..f6d4e9b 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -615,6 +615,10 @@ TableNode *getTN(TNodeOrConst *tnc) { int getConst(TNodeOrConst *tnc) { if (tnc->d == INTEGER) { return tnc->tnc_union->integer; + } else if (tnc->d == CHARACTER) { + return tnc->tnc_union->character; + } else if (tnc->d == BOOLEAN) { + return tnc->tnc_union->Boolean; } return -1; } diff --git a/tests/given/test/functionValue.alpha b/tests/given/test/functionValue.alpha index 8216d9e..aa31fde 100644 --- a/tests/given/test/functionValue.alpha +++ b/tests/given/test/functionValue.alpha @@ -30,7 +30,7 @@ function d: iic2b d(x,y,z) := { [string: s] - return 0; + return true; } function entry: string2int @@ -38,6 +38,7 @@ function entry: string2int a(x) := { [string : s] s := x; + return 0; } @@ -50,7 +51,7 @@ b(x) := { c(x) := { [string: s] s := "Hi!"; - return x(s); + return a(s); } @@ -61,6 +62,7 @@ entry(arg) := { [integer: result; string2int: f; integer: temp] temp := a("Hello"); f := b(temp); + result := f("ahhh"); result := c(f); if (d(1,2,'c')) then { diff --git a/tests/sprint4/test/sp4_cg_add.alpha b/tests/sprint4/test/sp4_cg_add.alpha index 96e2842..1b63c8d 100644 --- a/tests/sprint4/test/sp4_cg_add.alpha +++ b/tests/sprint4/test/sp4_cg_add.alpha @@ -1,5 +1,5 @@ (* TEST: [-asc -tc -cg -ir] *) - + type string: 1 -> character type BooleanXBoolean: [Boolean: x; Boolean: y] type characterXcharacter: [character: x; character: y] @@ -35,7 +35,7 @@ external function inI: integer2integer external function inC: integer2character function entry: string2integer - + entry (arg) := { [integer:x; integer:y; integer: result] diff --git a/tests/sprint4/test/sp4_cg_calls.alpha b/tests/sprint4/test/sp4_cg_calls.alpha index c7beea1..ab70dbc 100644 --- a/tests/sprint4/test/sp4_cg_calls.alpha +++ b/tests/sprint4/test/sp4_cg_calls.alpha @@ -36,7 +36,6 @@ external function inC: integer2character 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 @@ -54,8 +53,9 @@ bar (a, b, c,d,e,f,g) := { } entry (arg) := { - [integer:x; integer:y; integer: result] - + [integer:x; integer:y; integer: result; character: a] + a := 'a'; + x := printCharacter(a); result := bar(1,2,3,4,5,6,7); return 1; }