From cfb3d9d08e46ee1e9e4e5f2adacad0c86313aab8 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Tue, 6 May 2025 23:49:01 -0400 Subject: [PATCH] final submission --- src/codegen.c | 38 ++++++++++++++++++++++------ tests/programs/sp3_if_else_sse.alpha | 10 ++++---- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index 67a625f..9baafd4 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -588,7 +588,7 @@ int generateCondGoto(Instruction *inst) { int generateIfTrue(Instruction *inst) { TNodeOrConst *op1 = getOperand1(inst); - + if (op1 == NULL) { printdebug("%sgenerateIfTrue failed, NULL operand", COLOR_RED); return -1; @@ -606,7 +606,7 @@ int generateIfTrue(Instruction *inst) { int generateIfFalse(Instruction *inst) { TNodeOrConst *op1 = getOperand1(inst); - + if (op1 == NULL) { printdebug("%sgenerateIfFalse failed, NULL operand", COLOR_RED); return -1; @@ -618,7 +618,7 @@ int generateIfFalse(Instruction *inst) { return -1; } - fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\t#if false start\n", getAddress(cg)); + fprintf(cg_flag, "\tcmpb\t$0, %d(%%rbp)\t#if false start\n", getAddress(cg)); fprintf(cg_flag, "\tje\t.L%d\t\t#if false end\n", getLabel(inst)); } @@ -690,9 +690,31 @@ int generateEqualTo(Instruction *inst) { printdebug("generateLessThan failed, %s is not initialized/in CG", getName(getTN(op2))); return -1; } + char *movtype; + char *reg; + char *cmptype; + TableNode *typetn = getTypeEntry(getTN(op1)); + if (typetn == integ) { + movtype = "movl"; - fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#equal to start\n", getAddress(op1CG)); - fprintf(cg_flag, "\tcmpl\t%d(%%rbp), %%eax\n", getAddress(op2CG)); + cmptype = "cmpl"; + reg = "%eax"; + } else if (typetn == boo) { + cmptype = "cmpb"; + movtype = "movb"; + reg = "%al"; + } else if (typetn == chara) { + cmptype = "cmpb"; + movtype = "movb"; + reg = "%al"; + } else { + cmptype = "cmpq"; + movtype = "movq"; + reg = "%rax"; + } + + fprintf(cg_flag, "\t%s\t%d(%%rbp), %s\t#equal to start\n", movtype, getAddress(op1CG), reg); + fprintf(cg_flag, "\t%s\t%d(%%rbp), %s\n", cmptype, getAddress(op2CG), reg); fprintf(cg_flag, "\tsete\t%%al\n"); fprintf(cg_flag, "\tmovb\t$0, %d(%%rbp)\n", getAddress(cg)); fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\t#equal to end\n", getAddress(cg)); @@ -734,7 +756,7 @@ int generateCall(Instruction *inst) { // } //now for the return CGNode *cg = findCG(getResult(inst)); - + if (cg == NULL) { cg = addCG(getResult(inst), offset); } @@ -757,7 +779,7 @@ int generateReturn(Instruction *inst) { printdebug("generateReturn failed, trying to return %s not in CGList", getName(getTN(op1))); return -1; } - if (table_lookup(getAncestor(cur), getName(getTN(op1))) != undefined) { + if (table_lookup(getAncestor(cur), getName(getTN(op1))) != undefined) { fprintf(cg_flag, "\tmovl\t$%s,%%eax\t#return a function\n", getName(getTN(op1))); } else { fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#return %s\n", getAddress(cg), getName(getTN(op1))); @@ -812,7 +834,7 @@ int generateParam(Instruction *inst) { 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%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; diff --git a/tests/programs/sp3_if_else_sse.alpha b/tests/programs/sp3_if_else_sse.alpha index c78ce35..b55f061 100644 --- a/tests/programs/sp3_if_else_sse.alpha +++ b/tests/programs/sp3_if_else_sse.alpha @@ -15,9 +15,9 @@ bar (r,s) := { *) entry (arg) := { [ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t; integer: c] - x := true; - y := true; - if ( x | y ) then { + x := false; + y := false; + if ( (x | y) | !( y = x ) ) then { (* if ( x