From dfe33c9b7ab0011b65a054fde258c2608f7c9f26 Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Thu, 26 Mar 2020 18:52:46 +0100 Subject: [PATCH] 6502: Pointers should have priority when allocating to the zeropage --- src/main/scala/millfork/env/Environment.scala | 11 ++++++++-- .../scala/millfork/test/PointerSuite.scala | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/scala/millfork/env/Environment.scala b/src/main/scala/millfork/env/Environment.scala index 0d9b7aa1..8a1f5d12 100644 --- a/src/main/scala/millfork/env/Environment.scala +++ b/src/main/scala/millfork/env/Environment.scala @@ -186,7 +186,13 @@ class Environment(val parent: Option[Environment], val prefix: String, val cpuFa } else Nil case VariableAllocationMethod.Auto | VariableAllocationMethod.Register | VariableAllocationMethod.Static => if (m.alloc == VariableAllocationMethod.Register && options.flag(CompilationFlag.FallbackValueUseWarning)) { - log.warn(s"Failed to inline variable `${m.name}` into a register", None) + options.platform.cpuFamily match { + case CpuFamily.M6502 if m.sizeInBytes == 1 => + log.warn(s"Failed to inline variable `${m.name}` into a register", None) + case CpuFamily.I80 | CpuFamily.I80 | CpuFamily.I86 | CpuFamily.M6809 if m.sizeInBytes <= 2 => + log.warn(s"Failed to inline variable `${m.name}` into a register", None) + case _ => + } } if (m.sizeInBytes == 0) Nil else { val graveName = m.name.stripPrefix(prefix) + "`" @@ -1471,7 +1477,8 @@ class Environment(val parent: Option[Environment], val prefix: String, val cpuFa stmt.assemblyParamPassingConvention match { case ByVariable(name) => val zp = pointies(name) // TODO - val v = UninitializedMemoryVariable(prefix + name, typ, if (zp) VariableAllocationMethod.Zeropage else VariableAllocationMethod.Auto, None, defaultVariableAlignment(options, 2), isVolatile = false) + val allocationMethod = if (pointies(name)) VariableAllocationMethod.Zeropage else if (typ.isPointy) VariableAllocationMethod.Register else VariableAllocationMethod.Auto + val v = UninitializedMemoryVariable(prefix + name, typ, allocationMethod, None, defaultVariableAlignment(options, 2), isVolatile = false) addThing(v, stmt.position) registerAddressConstant(v, stmt.position, options, Some(typ)) val addr = v.toAddress diff --git a/src/test/scala/millfork/test/PointerSuite.scala b/src/test/scala/millfork/test/PointerSuite.scala index b7163bf7..12a944ea 100644 --- a/src/test/scala/millfork/test/PointerSuite.scala +++ b/src/test/scala/millfork/test/PointerSuite.scala @@ -453,4 +453,26 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { m.readWord(0xc000) should be <(256) } + + test ("Pointers should have priority when allocating to the zeropage") { + val m = EmuUnoptimizedRun( + """ + | word output1 @$c000 + | word output2 @$c000 + | + | array arr [252] + | + | noinline word f(pointer p, pointer q) { + | output1 = p.addr + | output2 = q.addr + | return p + q + | } + | + | void main () { + | f(0,1) + | } + |""".stripMargin) + m.readWord(0xc000) should be <(256) + m.readWord(0xc002) should be <(256) + } }