1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-07-05 09:28:54 +00:00

6502: Fix booleans

This commit is contained in:
Karol Stasiak 2019-08-02 00:17:24 +02:00
parent bb63a73f15
commit 9962a8344a
2 changed files with 53 additions and 11 deletions

View File

@ -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

View File

@ -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)
}
}