From 9962a8344a1b215211b76c138502912ab581d80f Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Fri, 2 Aug 2019 00:17:24 +0200 Subject: [PATCH] 6502: Fix booleans --- .../compiler/mos/MosExpressionCompiler.scala | 19 ++++---- .../scala/millfork/test/BooleanSuite.scala | 45 ++++++++++++++++++- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala b/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala index 551db01c..4b27c17d 100644 --- a/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala +++ b/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala @@ -493,8 +493,7 @@ object MosExpressionCompiler extends AbstractExpressionCompiler[AssemblyLine] { AssemblyLine.immediate(LDA, 0), AssemblyLine.label(skip)) case _ => - println(sourceType) - ??? + ctx.log.fatal(s"Cannot assign `${sourceType.name}` to `bool`", expr.position) } } @@ -599,14 +598,6 @@ object MosExpressionCompiler extends AbstractExpressionCompiler[AssemblyLine] { def compile(ctx: CompilationContext, expr: Expression, exprTypeAndVariable: Option[(Type, Variable)], branches: BranchSpec): List[AssemblyLine] = { val env = ctx.env - env.eval(expr) match { - case Some(value) => - return exprTypeAndVariable.fold(noop) { case (exprType, target) => - assertCompatible(exprType, target.typ) - compileConstant(ctx, value, target) - } - case _ => - } val b = env.get[Type]("byte") val exprType = AbstractExpressionCompiler.getExpressionType(ctx, expr) if (branches != NoBranching) { @@ -622,6 +613,14 @@ object MosExpressionCompiler extends AbstractExpressionCompiler[AssemblyLine] { case _ => () } } + env.eval(expr) match { + case Some(value) => + return exprTypeAndVariable.fold(noop) { case (exprType, target) => + assertCompatible(exprType, target.typ) + compileConstant(ctx, value, target) + } + case _ => + } val w = env.get[Type]("word") expr match { case HalfWordExpression(expression, _) => ??? // TODO diff --git a/src/test/scala/millfork/test/BooleanSuite.scala b/src/test/scala/millfork/test/BooleanSuite.scala index ed257a72..23f14091 100644 --- a/src/test/scala/millfork/test/BooleanSuite.scala +++ b/src/test/scala/millfork/test/BooleanSuite.scala @@ -1,7 +1,7 @@ package millfork.test import millfork.Cpu -import millfork.test.emu.{EmuCrossPlatformBenchmarkRun, EmuUnoptimizedCrossPlatformRun} +import millfork.test.emu.{EmuCrossPlatformBenchmarkRun, EmuUnoptimizedCrossPlatformRun, EmuUnoptimizedRun} import org.scalatest.{FunSuite, Matchers} /** @@ -155,4 +155,47 @@ class BooleanSuite extends FunSuite with Matchers { """.stripMargin EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80)(code)(_.readByte(0xc000) should equal(7)) } + + test("Constant booleans") { + val code =""" + | word output @$c000 + | noinline bool f(byte x) = x & 1 != 0 + | noinline bool g(byte x) = x & 1 == 0 + | void main () { + | output = 5 + | if f(3) && true { output += 1 } + | if f(3) && false { output += 100 } + | if g(2) && true { output += 1 } + | if g(2) && false { output += 100 } + | if f(2) && true { output += 100 } + | if f(2) && false { output += 100 } + | if g(3) && true { output += 100 } + | if g(3) && false { output += 100 } + | + | if f(3) || true { output += 1 } + | if f(3) || false { output += 1 } + | if g(2) || true { output += 1 } + | if g(2) || false { output += 1 } + | if f(2) || true { output += 1 } + | if f(2) || false { output += 100 } + | if g(3) || true { output += 1 } + | if g(3) || false { output += 100 } + | } + | + """.stripMargin + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80)(code)(_.readWord(0xc000) should equal(13)) + } + + test("Constant booleans mini") { + val code =""" + | byte output @$c000 + | noinline byte f(byte x) = x + | void main () { + | output = 5 + | if f(3) != 0 && false { output += 100 } + | } + | + """.stripMargin + EmuUnoptimizedRun(code).readWord(0xc000) should equal(5) + } }