From 1f8ece007b10e635b4441c216b519ee2f75782da Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Mon, 2 Sep 2019 23:22:07 +0200 Subject: [PATCH] Add constants for segment bounds --- docs/doc_index.md | 2 ++ docs/lang/predefined_constants.md | 19 ++++++++++++++ src/main/scala/millfork/env/Environment.scala | 25 ++++++++++++++++--- .../millfork/output/AbstractAssembler.scala | 8 ++++++ .../millfork/output/VariableAllocator.scala | 3 +++ 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 docs/lang/predefined_constants.md diff --git a/docs/doc_index.md b/docs/doc_index.md index a1f854d8..811da15f 100644 --- a/docs/doc_index.md +++ b/docs/doc_index.md @@ -22,6 +22,8 @@ * [Literals](lang/literals.md) +* [Predefined constants](lang/predefined_constants.md) + * [List of text encodings and escape sequences](lang/text.md) * [Operators reference](lang/operators.md) diff --git a/docs/lang/predefined_constants.md b/docs/lang/predefined_constants.md new file mode 100644 index 00000000..dab73a57 --- /dev/null +++ b/docs/lang/predefined_constants.md @@ -0,0 +1,19 @@ +[< back to index](../doc_index.md) + +# Predefined constants + +* `byte nullchar` – the null terminator for strings in the default encoding, equivalent to `""z[0]` + +* `null$ nullptr` – the invalid pointer value; the value of the `NULLPTR` feature + +* `bool true`, `bool false` – boolean constants + +* `pointer segment.N.start` – the value of `segment_N_start` from the platform definition + +* `pointer segment.N.end` – the value of `segment_N_end` from the platform definition + +* `pointer segment.N.heapstart` – the address of the first byte in the `N` segment that was not automatically allocated + +* `word segment.N.length` – the number of byte locations between `segment_N_start` and `segment_N_end`, inclusive + +* `byte segment.N.bank` – the value of `segment_N_bank` from the platform definition diff --git a/src/main/scala/millfork/env/Environment.scala b/src/main/scala/millfork/env/Environment.scala index 322b31dc..90a13ffd 100644 --- a/src/main/scala/millfork/env/Environment.scala +++ b/src/main/scala/millfork/env/Environment.scala @@ -439,18 +439,35 @@ class Environment(val parent: Option[Environment], val prefix: String, val cpuFa val __zeropage_usage = UnexpandedConstant("__zeropage_usage", 1) addThing(ConstantThing("__zeropage_usage", __zeropage_usage, b), None) def addUnexpandedWordConstant(name: String): Unit = { + val c = UnexpandedConstant(name, 2) + addThing(ConstantThing(name, c, w), None) + addThing(ConstantThing(name + ".hi", c.hiByte, b), None) + addThing(ConstantThing(name + ".lo", c.loByte, b), None) + } + def addUnexpandedByteConstant(name: String): Unit = { + val c = UnexpandedConstant(name, 1) + addThing(ConstantThing(name, c, b), None) + } + def addUnexpandedPointerConstant(name: String): Unit = { val c = UnexpandedConstant(name, 2) addThing(ConstantThing(name, c, p), None) addThing(ConstantThing(name + ".hi", c.hiByte, b), None) addThing(ConstantThing(name + ".lo", c.loByte, b), None) } - addUnexpandedWordConstant("__rwdata_start") - addUnexpandedWordConstant("__rwdata_end") + addUnexpandedPointerConstant("__rwdata_start") + addUnexpandedPointerConstant("__rwdata_end") if (options.platform.ramInitialValuesBank.isDefined) { - addUnexpandedWordConstant("__rwdata_init_start") - addUnexpandedWordConstant("__rwdata_init_end") + addUnexpandedPointerConstant("__rwdata_init_start") + addUnexpandedPointerConstant("__rwdata_init_end") addUnexpandedWordConstant("__rwdata_size") } + for(segment <- options.platform.bankNumbers.keys) { + addUnexpandedPointerConstant(s"segment.$segment.start") + addUnexpandedPointerConstant(s"segment.$segment.heapstart") + addUnexpandedPointerConstant(s"segment.$segment.end") + addUnexpandedWordConstant(s"segment.$segment.length") + addUnexpandedByteConstant(s"segment.$segment.bank") + } addThing(ConstantThing("$0000", NumericConstant(0, 2), p), None) addThing(FlagBooleanType("set_carry", BranchingOpcodeMapping(Opcode.BCS, IfFlagSet(ZFlag.C), MOpcode.BCS), diff --git a/src/main/scala/millfork/output/AbstractAssembler.scala b/src/main/scala/millfork/output/AbstractAssembler.scala index adf5b975..b02ae1e3 100644 --- a/src/main/scala/millfork/output/AbstractAssembler.scala +++ b/src/main/scala/millfork/output/AbstractAssembler.scala @@ -541,6 +541,14 @@ abstract class AbstractAssembler[T <: AbstractCode](private val program: Program labelMap += "__rwdata_start" -> (defaultBank -> rwDataStart) labelMap += "__rwdata_end" -> (defaultBank -> rwDataEnd) labelMap += "__heap_start" -> (defaultBank -> variableAllocators("default").heapStart) + for (segment <- platform.bankNumbers.keys) { + val allocator = options.platform.variableAllocators(segment) + labelMap += s"segment.$segment.start" -> (defaultBank -> allocator.startAt) + labelMap += s"segment.$segment.end" -> (defaultBank -> (allocator.endBefore - 1)) + labelMap += s"segment.$segment.heapstart" -> (defaultBank -> allocator.heapStart) + labelMap += s"segment.$segment.length" -> (defaultBank -> (allocator.endBefore - allocator.startAt)) + labelMap += s"segment.$segment.bank" -> (defaultBank -> platform.bankNumbers(segment)) + } env = rootEnv.allThings diff --git a/src/main/scala/millfork/output/VariableAllocator.scala b/src/main/scala/millfork/output/VariableAllocator.scala index cc341f0b..a4ffff31 100644 --- a/src/main/scala/millfork/output/VariableAllocator.scala +++ b/src/main/scala/millfork/output/VariableAllocator.scala @@ -95,6 +95,9 @@ class AfterCodeByteAllocator(startIfNoCode: Int, val endBefore: Int) extends Byt class VariableAllocator(zpBytes: List[Int], private val bytes: ByteAllocator) { + def startAt: Int = bytes.startAt + def endBefore: Int = bytes.endBefore + def totalHimemSize: Int = bytes.endBefore - bytes.startAt val zeropage: ByteAllocator = new ZeropageAllocator(zpBytes)