1
0
mirror of https://github.com/KarolS/millfork.git synced 2025-01-27 11:30:19 +00:00

6502: Correctly inlie variables that happen to be returned (fixes #32)

This commit is contained in:
Karol Stasiak 2020-01-11 22:20:02 +01:00
parent 298e94e3bc
commit 49bf0d2777
2 changed files with 24 additions and 1 deletions

View File

@ -858,6 +858,7 @@ object VariableToRegisterOptimization extends AssemblyOptimization[AssemblyLine]
aCandidate: Option[String],
features: FeaturesForIndexRegisters,
lines: List[(AssemblyLine, CpuImportance)]): TailRec[List[AssemblyLine]] = {
// only one of these will be defined:
val vx = xCandidate.getOrElse("-")
val vy = yCandidate.getOrElse("-")
val vz = zCandidate.getOrElse("-")
@ -911,7 +912,7 @@ object VariableToRegisterOptimization extends AssemblyOptimization[AssemblyLine]
if opcodesIdentityTable(op) && th.name == vy =>
tailcall(inlineVars(xCandidate, yCandidate, zCandidate, aCandidate, features, xs)).map(l.copy(addrMode = AbsoluteY, parameter = features.identityArray) :: _)
case (l@AssemblyLine0(LDA | TYA | TXA | TZA | CLA, _, _), _) :: xs if va != "" && isReturn(xs) =>
case (l@AssemblyLine0(LDA | TYA | TXA | TZA | CLA, _, _), _) :: xs if aCandidate.isDefined && isReturn(xs) =>
tailcall(inlineVars(xCandidate, yCandidate, zCandidate, aCandidate, features, xs)).map(l :: _)
case (l@AssemblyLine0(LDA, _, _), _) :: (AssemblyLine0(op, Absolute | ZeroPage, MemoryAddressConstant(th)), _) :: xs

View File

@ -768,4 +768,26 @@ class AssemblyOptimizationSuite extends FunSuite with Matchers {
m.readByte(0xc000) should equal(2)
}
}
test("Inline variables correctly") {
EmuBenchmarkRun("""
|array arr = [2,2,2,1,1,2,2]
|noinline byte f() {
| byte i
| i = 0
| while true {
| if arr[i] == 1 { return i }
| i += 1
| }
| return $ff
|}
|volatile byte output @$c000
|void main() {
| output = f()
|}
|""".stripMargin
) { m =>
m.readByte(0xc000) should equal(3)
}
}
}