diff --git a/src/codegen.c b/src/codegen.c index 88a2ab1..acbf76d 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -535,11 +535,39 @@ int generateCondGoto(Instruction *inst) { } int generateIfTrue(Instruction *inst) { - return -1; + TNodeOrConst *op1 = getOperand1(inst); + + if (op1 == NULL) { + printdebug("%sgenerateIfTrue failed, NULL operand", COLOR_RED); + return -1; + } + + CGNode *cg = findCG(getTN(op1)); + if (cg == NULL) { + printdebug("generateIfTrue failed, operand not on stack"); + return -1; + } + + fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\t#if true start\n", getAddress(cg)); + fprintf(cg_flag, "\tjne\t.L%d\t\t#if true end\n", getLabel(inst)); } int generateIfFalse(Instruction *inst) { - return -1; + TNodeOrConst *op1 = getOperand1(inst); + + if (op1 == NULL) { + printdebug("%sgenerateIfFalse failed, NULL operand", COLOR_RED); + return -1; + } + + CGNode *cg = findCG(getTN(op1)); + if (cg == NULL) { + printdebug("generateIfFalse failed, operand not on stack"); + return -1; + } + + fprintf(cg_flag, "\tcmpl\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)); } int generateLessThan(Instruction *inst) { diff --git a/tests/programs/fib.alpha b/tests/programs/fib.alpha index 9ebef58..37a86c8 100644 --- a/tests/programs/fib.alpha +++ b/tests/programs/fib.alpha @@ -12,9 +12,10 @@ Fib(i) := { } else { i := i; } - b := b | (a & c); - b := 2 < 3; + (*b := b | (a & c); + b := 2 < 3;*) if(i = 1) then { + i := printCharacter('a'); return 1; } else { return i + Fib(i - 1);