1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-05-29 04:41:30 +00:00

For-to loop fixes

This commit is contained in:
Karol Stasiak 2018-01-20 22:32:57 +01:00
parent c8c6ec83fc
commit 4e80236a65
4 changed files with 75 additions and 3 deletions

View File

@ -44,6 +44,8 @@ where `11111` is a sequential number and `xx` is the type:
* `th` beginning of the "then" block in an `if` statement
* `to` end of a `for-to` loop
* `wh` beginning of a `while` statement

View File

@ -1745,12 +1745,24 @@ object MfCompiler {
FunctionCallExpression("<", List(vex, f.end)),
f.body :+ increment),
))
// case (ForDirection.To | ForDirection.ParallelTo, _, Some(NumericConstant(n, _))) if n > 0 && n < 255 =>
// compile(ctx, List(
// Assignment(vex, f.start),
// WhileStatement(
// FunctionCallExpression("<=", List(vex, f.end)),
// f.body :+ increment),
// ))
case (ForDirection.To | ForDirection.ParallelTo, _, _) =>
val label = nextLabel("to")
compile(ctx, List(
Assignment(vex, f.start),
WhileStatement(
FunctionCallExpression("<=", List(vex, f.end)),
f.body :+ increment),
VariableExpression("true"),
f.body :+ IfStatement(
FunctionCallExpression("==", List(vex, f.end)),
List(AssemblyStatement(JMP, AddrMode.Absolute, VariableExpression(label), elidable = true)),
List(increment))),
AssemblyStatement(LABEL, AddrMode.DoesNotExist, VariableExpression(label), elidable=true)
))
case (ForDirection.DownTo, _, _) =>
compile(ctx, List(

View File

@ -1,6 +1,6 @@
package millfork.test
import millfork.test.emu.EmuBenchmarkRun
import millfork.test.emu.{EmuBenchmarkRun, EmuUnoptimizedRun}
import org.scalatest.{FunSuite, Matchers}
/**
@ -21,6 +21,26 @@ class ForLoopSuite extends FunSuite with Matchers {
| }
""".stripMargin)(_.readByte(0xc000) should equal(15))
}
test("For-to 2") {
EmuBenchmarkRun(
"""
| word output @$c000
| byte five
| void main () {
| init()
| byte i
| output = 0
| for i,0,to,five {
| output += i
| }
| }
| void init() {
| five = 5
| }
""".stripMargin)(_.readByte(0xc000) should equal(15))
}
test("For-downto") {
EmuBenchmarkRun(
"""
@ -73,4 +93,41 @@ class ForLoopSuite extends FunSuite with Matchers {
| }
""".stripMargin)(_.readByte(0xc000) should equal(15))
}
test("Various loops") {
EmuUnoptimizedRun(
"""
| void init() {
| zero = 0
| ff = $ff
| }
| byte zero
| byte ff
| byte flag @$c000
| void main () {
| init()
| byte i
| flag = 0
| for i, zero, until, ff { }
| flag = 1
| for i, zero, to, ff { }
| flag = 2
| for i, ff, downto, zero { }
| flag = 3
| for i, zero, paralleluntil, ff { }
| flag = 4
| for i, zero, parallelto, ff { }
| flag = 5
| for i, ff, until, zero { _panic() }
| flag = 6
| for i, ff, paralleluntil, zero { _panic() }
| flag = 7
| for i, ff, paralleluntil, zero { _panic() }
| flag = 8
| for i, zero, until, ff { }
| flag = 9
| }
| void _panic(){while(true){}}
""".stripMargin)
}
}

View File

@ -222,6 +222,7 @@ class EmuRun(cpu: millfork.Cpu.Value, nodeOptimizations: List[NodeOptimization],
}
}
}
ErrorReporting.trace(f"[$$c000] = ${memoryBank.readByte(0xc000)}%02X")
countCmos should be < TooManyCycles
println(countNmos + " NMOS cycles")
println(countCmos + " CMOS cycles")