simplify ZeroPage reserved locations handling a bit

This commit is contained in:
Irmen de Jong 2021-10-29 17:34:42 +02:00
parent 1137da37c3
commit 4d5bd0fa32
5 changed files with 20 additions and 37 deletions

View File

@ -89,7 +89,6 @@ object C64MachineDefinition: IMachineDefinition {
if (options.zeropage == ZeropageType.FULL) { if (options.zeropage == ZeropageType.FULL) {
free.addAll(0x04..0xf9) free.addAll(0x04..0xf9)
free.add(0xff) free.add(0xff)
free.removeAll(listOf(SCRATCH_B1, SCRATCH_REG, SCRATCH_W1, SCRATCH_W1 + 1, SCRATCH_W2, SCRATCH_W2 + 1))
free.removeAll(listOf(0xa0, 0xa1, 0xa2, 0x91, 0xc0, 0xc5, 0xcb, 0xf5, 0xf6)) // these are updated by IRQ free.removeAll(listOf(0xa0, 0xa1, 0xa2, 0x91, 0xc0, 0xc5, 0xcb, 0xf5, 0xf6)) // these are updated by IRQ
} else { } else {
if (options.zeropage == ZeropageType.KERNALSAFE || options.zeropage == ZeropageType.FLOATSAFE) { if (options.zeropage == ZeropageType.KERNALSAFE || options.zeropage == ZeropageType.FLOATSAFE) {
@ -133,13 +132,8 @@ object C64MachineDefinition: IMachineDefinition {
free.clear() free.clear()
} }
} }
require(SCRATCH_B1 !in free)
require(SCRATCH_REG !in free)
require(SCRATCH_W1 !in free)
require(SCRATCH_W2 !in free)
for (reserved in options.zpReserved) removeReservedFromFreePool()
reserve(reserved)
} }
} }

View File

@ -104,16 +104,13 @@ object CX16MachineDefinition: IMachineDefinition {
when (options.zeropage) { when (options.zeropage) {
ZeropageType.FULL -> { ZeropageType.FULL -> {
free.addAll(0x22..0xff) free.addAll(0x22..0xff)
free.removeAll(listOf(SCRATCH_B1, SCRATCH_REG, SCRATCH_W1, SCRATCH_W1 + 1, SCRATCH_W2, SCRATCH_W2 + 1))
} }
ZeropageType.KERNALSAFE -> { ZeropageType.KERNALSAFE -> {
free.addAll(0x22..0x7f) free.addAll(0x22..0x7f)
free.addAll(0xa9..0xff) free.addAll(0xa9..0xff)
free.removeAll(listOf(SCRATCH_B1, SCRATCH_REG, SCRATCH_W1, SCRATCH_W1 + 1, SCRATCH_W2, SCRATCH_W2 + 1))
} }
ZeropageType.BASICSAFE -> { ZeropageType.BASICSAFE -> {
free.addAll(0x22..0x7f) free.addAll(0x22..0x7f)
free.removeAll(listOf(SCRATCH_B1, SCRATCH_REG, SCRATCH_W1, SCRATCH_W1 + 1, SCRATCH_W2, SCRATCH_W2 + 1))
} }
ZeropageType.DONTUSE -> { ZeropageType.DONTUSE -> {
free.clear() // don't use zeropage at all free.clear() // don't use zeropage at all
@ -121,13 +118,7 @@ object CX16MachineDefinition: IMachineDefinition {
else -> throw CompilerException("for this machine target, zero page type 'floatsafe' is not available. ${options.zeropage}") else -> throw CompilerException("for this machine target, zero page type 'floatsafe' is not available. ${options.zeropage}")
} }
require(SCRATCH_B1 !in free) removeReservedFromFreePool()
require(SCRATCH_REG !in free)
require(SCRATCH_W1 !in free)
require(SCRATCH_W2 !in free)
for (reserved in options.zpReserved)
reserve(reserved)
} }
} }
} }

View File

@ -19,6 +19,13 @@ abstract class Zeropage(protected val options: CompilationOptions) {
val allowedDatatypes = NumericDatatypes val allowedDatatypes = NumericDatatypes
fun removeReservedFromFreePool() {
for (reserved in options.zpReserved)
reserve(reserved)
free.removeAll(listOf(SCRATCH_B1, SCRATCH_REG, SCRATCH_W1, SCRATCH_W1 + 1, SCRATCH_W2, SCRATCH_W2 + 1))
}
fun availableBytes() = if(options.zeropage== ZeropageType.DONTUSE) 0 else free.size fun availableBytes() = if(options.zeropage== ZeropageType.DONTUSE) 0 else free.size
fun hasByteAvailable() = if(options.zeropage== ZeropageType.DONTUSE) false else free.isNotEmpty() fun hasByteAvailable() = if(options.zeropage== ZeropageType.DONTUSE) false else free.isNotEmpty()
fun availableWords(): Int { fun availableWords(): Int {

View File

@ -24,25 +24,24 @@ class TestZeropage {
compTarget compTarget
) )
) )
assertEquals(256-4-16, zp.free.size) assertEquals(256-6-16, zp.free.size)
} }
class DummyCompilationTarget: ICompilationTarget { class DummyCompilationTarget: ICompilationTarget {
override val name: String override val name: String = "dummy"
get() = TODO("Not yet implemented")
override val machine: IMachineDefinition override val machine: IMachineDefinition
get() = TODO("Not yet implemented") get() = throw NotImplementedError("dummy")
override fun encodeString(str: String, altEncoding: Boolean): List<Short> { override fun encodeString(str: String, altEncoding: Boolean): List<Short> {
TODO("Not yet implemented") throw NotImplementedError("dummy")
} }
override fun decodeString(bytes: List<Short>, altEncoding: Boolean): String { override fun decodeString(bytes: List<Short>, altEncoding: Boolean): String {
TODO("Not yet implemented") throw NotImplementedError("dummy")
} }
override fun memorySize(dt: DataType): Int { override fun memorySize(dt: DataType): Int {
TODO("Not yet implemented") throw NotImplementedError("dummy")
} }
} }
@ -50,21 +49,13 @@ class TestZeropage {
class DummyZeropage(options: CompilationOptions) : Zeropage(options) { class DummyZeropage(options: CompilationOptions) : Zeropage(options) {
override val SCRATCH_B1: Int = 0x10 override val SCRATCH_B1: Int = 0x10
override val SCRATCH_REG: Int = 0x11 override val SCRATCH_REG: Int = 0x11
override val SCRATCH_W1: Int= 0x12 override val SCRATCH_W1: Int= 0x20
override val SCRATCH_W2: Int = 0x13 override val SCRATCH_W2: Int = 0x30
init { init {
free.addAll(0..255) free.addAll(0..255)
// TODO should be in class: removeReservedFromFreePool()
free.remove(SCRATCH_B1)
free.remove(SCRATCH_REG)
free.remove(SCRATCH_W1)
free.remove(SCRATCH_W2)
// TODO should be in class:
for (reserved in options.zpReserved)
reserve(reserved)
} }
} }

View File

@ -4,6 +4,7 @@ TODO
For next compiler release (7.2) For next compiler release (7.2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- fix the asm-labels problem (github issue #62) - fix the asm-labels problem (github issue #62)
- find a way to optimize if-statement codegen so that "if var & %10000" doesn't use stack & subroutine call, but also that the simple case "if X {...}" remains fast
Blocked by Commander-x16 v39 release Blocked by Commander-x16 v39 release
@ -15,7 +16,7 @@ Blocked by Commander-x16 v39 release
Future Future
^^^^^^ ^^^^^^
- get rid of all TODO's in the code - get rid of all TODO's in the code
- improve testability further, add more tests, address more questions/issues from the testability discussions. - improve testability further, add more tests
- replace certain uses of inferredType.getOr(DataType.UNDEFINED) by i.getOrElse({ errorhandler }) - replace certain uses of inferredType.getOr(DataType.UNDEFINED) by i.getOrElse({ errorhandler })
- see if we can remove more "[InferredType].getOr(DataType.UNDEFINED)" - see if we can remove more "[InferredType].getOr(DataType.UNDEFINED)"
- use more of Result<> and Either<> to handle errors/ nulls better - use more of Result<> and Either<> to handle errors/ nulls better
@ -37,7 +38,6 @@ Future
More code optimization ideas More code optimization ideas
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- a way to optimize if-statement codegen so that "if var & %10000" doesn't use stack & subroutine call, but also that the simple case "if X {...}" remains fast
- detect variables that are written but never read - mark those as unused too and remove them, such as ``uword unused = memory("unused222", 20)`` - also remove this memory slab allocation - detect variables that are written but never read - mark those as unused too and remove them, such as ``uword unused = memory("unused222", 20)`` - also remove this memory slab allocation
- rewrite expression tree evaluation such that it doesn't use an eval stack but flatten the tree into linear code that uses a fixed number of predetermined value 'variables' - rewrite expression tree evaluation such that it doesn't use an eval stack but flatten the tree into linear code that uses a fixed number of predetermined value 'variables'
- this removes the need for the BinExprSplitter (which is problematic now) - this removes the need for the BinExprSplitter (which is problematic now)