From b4fb43bc806e027338439141b075f642f8b8a6f2 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 18 Nov 2024 22:33:47 +0100 Subject: [PATCH] fix the if not check in ir codegen --- .../prog8/codegen/intermediate/IRCodeGen.kt | 2 +- .../astprocessing/IntermediateAstMaker.kt | 2 +- .../test/codegeneration/TestVariousCodeGen.kt | 49 +++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index 57da5431a..5b50f6c20 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -1330,7 +1330,7 @@ class IRCodeGen( } private fun translateIfElse(ifElse: PtIfElse): IRCodeChunks { - if((ifElse.condition as? PtPrefix)?.operator=="not") + if((ifElse.condition as? PtPrefix)?.operator=="not" && ifElse.hasElse()) throw AssemblyError("not prefix in ifelse should have been replaced by swapped if-else blocks") val condition = ifElse.condition as? PtBinaryExpression diff --git a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt index 94bd1f7df..8a4879af2 100644 --- a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt +++ b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt @@ -401,7 +401,7 @@ class IntermediateAstMaker(private val program: Program, private val errors: IEr val prefixedFcall = prefix.expression as? FunctionCallExpression if (prefixedFcall != null) { val returnRegs = prefixedFcall.target.targetSubroutine(program)?.asmReturnvaluesRegisters - if(returnRegs!=null && returnRegs.size==1 && returnRegs[0].statusflag!=null) { + if (returnRegs != null && returnRegs.size == 1 && returnRegs[0].statusflag != null) { return codeForStatusflag(prefixedFcall, returnRegs[0].statusflag!!, true) } } diff --git a/compiler/test/codegeneration/TestVariousCodeGen.kt b/compiler/test/codegeneration/TestVariousCodeGen.kt index 5919f9aca..c94ba33c6 100644 --- a/compiler/test/codegeneration/TestVariousCodeGen.kt +++ b/compiler/test/codegeneration/TestVariousCodeGen.kt @@ -9,6 +9,9 @@ import io.kotest.matchers.string.shouldStartWith import io.kotest.matchers.types.instanceOf import prog8.code.ast.PtAssignment import prog8.code.ast.PtBinaryExpression +import prog8.code.ast.PtFunctionCall +import prog8.code.ast.PtIfElse +import prog8.code.ast.PtPrefix import prog8.code.ast.PtVariable import prog8.code.core.DataType import prog8.code.target.* @@ -514,4 +517,50 @@ main { compileText(C64Target(), false, src, writeAssembly = true) shouldNotBe null } + + test("if not without else is not swapped") { + val src=""" +main { + sub start() { + if not thing() + cx16.r0++ + } + + sub thing() -> bool { + cx16.r0++ + return false + } +}""" + compileText(C64Target(), false, src, writeAssembly = true) shouldNotBe null + val result = compileText(VMTarget(), false, src, writeAssembly = true)!! + val st = result.codegenAst!!.entrypoint()!!.children + st.size shouldBe 2 + val ifelse = st[0] as PtIfElse + ifelse.hasElse() shouldBe false + (ifelse.condition as PtPrefix).operator shouldBe "not" + } + + test("if not with else is swapped") { + val src=""" +main { + sub start() { + if not thing() + cx16.r0++ + else + cx16.r1++ + } + + sub thing() -> bool { + cx16.r0++ + return false + } +}""" + compileText(C64Target(), false, src, writeAssembly = true) shouldNotBe null + val result = compileText(VMTarget(), false, src, writeAssembly = true)!! + val st = result.codegenAst!!.entrypoint()!!.children + st.size shouldBe 2 + val ifelse = st[0] as PtIfElse + ifelse.hasElse() shouldBe true + ifelse.condition shouldBe instanceOf() + } }) \ No newline at end of file