From 4d64bbafac866bdaf647d02255b2850d679d2bf6 Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Thu, 27 Dec 2018 14:16:34 +0100 Subject: [PATCH] 6502: always fold constants --- .../compiler/mos/MosExpressionCompiler.scala | 8 +++++++ .../scala/millfork/test/ConstantSuite.scala | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/test/scala/millfork/test/ConstantSuite.scala diff --git a/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala b/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala index 66682f55..3db1d849 100644 --- a/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala +++ b/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala @@ -344,6 +344,14 @@ 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 w = env.get[Type]("word") expr match { diff --git a/src/test/scala/millfork/test/ConstantSuite.scala b/src/test/scala/millfork/test/ConstantSuite.scala new file mode 100644 index 00000000..5d19fa6a --- /dev/null +++ b/src/test/scala/millfork/test/ConstantSuite.scala @@ -0,0 +1,23 @@ +package millfork.test + +import millfork.Cpu +import millfork.test.emu.EmuUnoptimizedCrossPlatformRun +import org.scalatest.{FunSuite, Matchers} + +/** + * @author Karol Stasiak + */ +class ConstantSuite extends FunSuite with Matchers { + + test("Constants should fold") { + EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Z80)( + """ + | array Sieve[4] + | array __screen[4] + | byte vic_mem + | void main() { + | vic_mem = lo( ((Sieve.addr >> 10) & 8) | ((__screen.addr >> 6) & $f0) ) + | } + """.stripMargin){m => } + } +} \ No newline at end of file