1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-06-09 16:29:34 +00:00

Improvements to constant folding

This commit is contained in:
Karol Stasiak 2021-02-18 00:36:13 +01:00
parent fb71f88343
commit 2beabb7bed
2 changed files with 15 additions and 1 deletions

View File

@ -522,7 +522,10 @@ case class CompoundConstant(operator: MathOperator.Value, lhs: Constant, rhs: Co
case MathOperator.Plus9 => c
case MathOperator.DecimalPlus => c
case MathOperator.DecimalPlus9 => c
case MathOperator.Minus => CompoundConstant(operator, l, r)
case MathOperator.Minus => c match {
case NumericConstant(rv, rs) => NumericConstant(-rv, rs)
case _ => CompoundConstant(operator, l, r)
}
case MathOperator.Times => Constant.Zero
case MathOperator.Shl => Constant.Zero
case MathOperator.Shr => Constant.Zero

View File

@ -178,4 +178,15 @@ class ConstantSuite extends FunSuite with Matchers {
m.readMedium(0xc003) should equal(200/8)
}
}
test("Negative constant evaluation") {
EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086, Cpu.Motorola6809)("""
| word output @$c000
| void main () {
| output = -1 * -1
| }
""".stripMargin){m =>
m.readWord(0xc000) should equal(1)
}
}
}