Skip to content

Commit 2725c4a

Browse files
committed
slight tweaks to zp and allocator
1 parent c8cd6e9 commit 2725c4a

File tree

6 files changed

+24
-26
lines changed

6 files changed

+24
-26
lines changed

codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,9 @@ internal class ProgramAndVarsGen(
297297
}
298298
}
299299

300-
val zp = zeropage
301300
// string and array variables in zeropage that have initializer value, should be initialized
302-
val stringVarsWithInitInZp = zp.variables.filter { it.value.dt==DataType.STR && it.value.initialStringValue!=null }
303-
val arrayVarsWithInitInZp = zp.variables.filter { it.value.dt in ArrayDatatypes && it.value.initialArrayValue!=null }
301+
val stringVarsWithInitInZp = allocator.zeropageVars.filter { it.value.dt==DataType.STR && it.value.initialStringValue!=null }
302+
val arrayVarsWithInitInZp = allocator.zeropageVars.filter { it.value.dt in ArrayDatatypes && it.value.initialArrayValue!=null }
304303
if(stringVarsWithInitInZp.isNotEmpty() || arrayVarsWithInitInZp.isNotEmpty()) {
305304
asmgen.out("; zp str and array initializations")
306305
stringVarsWithInitInZp.forEach {
@@ -352,7 +351,7 @@ internal class ProgramAndVarsGen(
352351
}
353352

354353
private fun zeropagevars2asm(scope: INameScope) {
355-
val zpVariables = zeropage.variables.filter { it.value.originalScope==scope }
354+
val zpVariables = allocator.zeropageVars.filter { it.value.originalScope==scope }
356355
for ((scopedName, zpvar) in zpVariables) {
357356
if (scopedName.size == 2 && scopedName[0] == "cx16" && scopedName[1][0] == 'r' && scopedName[1][1].isDigit())
358357
continue // The 16 virtual registers of the cx16 are not actual variables in zp, they're memory mapped

codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import prog8.ast.base.IntegerDatatypes
88
import prog8.ast.expressions.StringLiteral
99
import prog8.ast.statements.Subroutine
1010
import prog8.ast.statements.ZeropageWish
11-
import prog8.compilerinterface.CompilationOptions
12-
import prog8.compilerinterface.IErrorReporter
13-
import prog8.compilerinterface.IVariablesAndConsts
14-
import prog8.compilerinterface.ZeropageType
11+
import prog8.compilerinterface.*
1512

1613

1714
internal class VariableAllocator(private val vars: IVariablesAndConsts,
@@ -23,6 +20,7 @@ internal class VariableAllocator(private val vars: IVariablesAndConsts,
2320
private val memorySlabsInternal = mutableMapOf<String, Pair<UInt, UInt>>()
2421
internal val memorySlabs: Map<String, Pair<UInt, UInt>> = memorySlabsInternal
2522
internal val globalFloatConsts = mutableMapOf<Double, String>() // all float values in the entire program (value -> varname)
23+
internal val zeropageVars: Map<List<String>, Zeropage.ZpAllocation> = zeropage.allocatedVariables
2624

2725
internal fun getMemorySlab(name: String) = memorySlabsInternal[name]
2826
internal fun allocateMemorySlab(name: String, size: UInt, align: UInt) {
@@ -120,7 +118,7 @@ internal class VariableAllocator(private val vars: IVariablesAndConsts,
120118
println(" zeropage free space: ${zeropage.free.size} bytes")
121119
}
122120

123-
internal fun isZpVar(scopedName: List<String>) = scopedName in zeropage.variables
121+
internal fun isZpVar(scopedName: List<String>) = scopedName in zeropage.allocatedVariables
124122

125123
private fun numArrayElements(variable: IVariablesAndConsts.StaticVariable) =
126124
when(variable.type) {
@@ -129,7 +127,7 @@ internal class VariableAllocator(private val vars: IVariablesAndConsts,
129127
else -> null
130128
}
131129

132-
fun subroutineExtra(sub: Subroutine): SubroutineExtraAsmInfo {
130+
internal fun subroutineExtra(sub: Subroutine): SubroutineExtraAsmInfo {
133131
var extra = subroutineExtras[sub]
134132
return if(extra==null) {
135133
extra = SubroutineExtraAsmInfo()
@@ -140,7 +138,7 @@ internal class VariableAllocator(private val vars: IVariablesAndConsts,
140138
extra
141139
}
142140

143-
fun getFloatAsmConst(number: Double): String {
141+
internal fun getFloatAsmConst(number: Double): String {
144142
val asmName = globalFloatConsts[number]
145143
if(asmName!=null)
146144
return asmName

compiler/test/ZeropageTests.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,12 @@ class TestCx16Zeropage: FunSpec({
264264

265265
test("preallocated zp vars") {
266266
val zp1 = CX16Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.FULL, emptyList(), false, false, cx16target))
267-
zp1.variables[listOf("test")] shouldBe null
268-
zp1.variables[listOf("cx16", "r0")] shouldNotBe null
269-
zp1.variables[listOf("cx16", "r15")] shouldNotBe null
270-
zp1.variables[listOf("cx16", "r0L")] shouldNotBe null
271-
zp1.variables[listOf("cx16", "r15L")] shouldNotBe null
272-
zp1.variables[listOf("cx16", "r0sH")] shouldNotBe null
273-
zp1.variables[listOf("cx16", "r15sH")] shouldNotBe null
267+
zp1.allocatedVariables[listOf("test")] shouldBe null
268+
zp1.allocatedVariables[listOf("cx16", "r0")] shouldNotBe null
269+
zp1.allocatedVariables[listOf("cx16", "r15")] shouldNotBe null
270+
zp1.allocatedVariables[listOf("cx16", "r0L")] shouldNotBe null
271+
zp1.allocatedVariables[listOf("cx16", "r15L")] shouldNotBe null
272+
zp1.allocatedVariables[listOf("cx16", "r0sH")] shouldNotBe null
273+
zp1.allocatedVariables[listOf("cx16", "r15sH")] shouldNotBe null
274274
}
275275
})

compilerInterfaces/src/prog8/compilerinterface/Zeropage.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ abstract class Zeropage(protected val options: CompilationOptions) {
2929

3030
// the variables allocated into Zeropage.
3131
// name (scoped) ==> pair of address to (Datatype + bytesize)
32-
protected val allocatedVariables = mutableMapOf<List<String>, ZpAllocation>()
33-
private val allocations = mutableMapOf<UInt, Pair<List<String>, DataType>>()
34-
val variables: Map<List<String>, ZpAllocation> = allocatedVariables
32+
val allocatedVariables = mutableMapOf<List<String>, ZpAllocation>()
3533

3634
val free = mutableListOf<UInt>() // subclasses must set this to the appropriate free locations.
3735

@@ -61,7 +59,7 @@ abstract class Zeropage(protected val options: CompilationOptions) {
6159
position: Position?,
6260
errors: IErrorReporter): Result<Pair<UInt, Int>, ZeropageAllocationError> {
6361

64-
require(name.isEmpty() || !allocations.values.any { it.first==name } ) {"name can't be allocated twice"}
62+
require(name.isEmpty() || name !in allocatedVariables) {"name can't be allocated twice"}
6563

6664
if(options.zeropage== ZeropageType.DONTUSE)
6765
return Err(ZeropageAllocationError("zero page usage has been disabled"))
@@ -114,7 +112,6 @@ abstract class Zeropage(protected val options: CompilationOptions) {
114112
private fun makeAllocation(address: UInt, size: Int, datatype: DataType, name: List<String>, initValue: Expression?, originalScope: INameScope): UInt {
115113
require(size>=0)
116114
free.removeAll(address until address+size.toUInt())
117-
allocations[address] = name to datatype
118115
if(name.isNotEmpty()) {
119116
allocatedVariables[name] = when(datatype) {
120117
in NumericDatatypes -> ZpAllocation(address, datatype, size, originalScope, null, null) // numerical variables in zeropage never have an initial value here because they are set in separate initializer assignments

docs/source/todo.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Compiler:
2727
- allow "xxx" * constexpr (where constexpr is not a number literal), now gives expression error not same type
2828
- unify FunctioncallExpression + FunctioncallStatement and PipeExpression + Pipe statement classes, may require moving Expression/Statement into interfaces instead of abstract base classes
2929
- for the pipe operator: recognise a placeholder (``?`` or ``%`` or ``_``) in a non-unary function call to allow non-unary functions in the chain; ``4 |> mkword(?, $44) |> print_uw``
30+
OR: change pipe syntax and require function call, but always have implicit first argument added.
3031
- for the pipe operator: make it 100% syntactic sugar so there's no need for asm codegen like translatePipeExpression
3132
- make it possible to inline non-asmsub routines that just contain a single statement (return, functioncall, assignment)
3233
but this requires all identifiers in the inlined expression to be changed to fully scoped names.
@@ -69,5 +70,6 @@ Optimizations:
6970
- translateFunctioncall() in BuiltinFunctionsAsmGen: should be able to assign parameters to a builtin function directly from register(s), this will make the use of a builtin function in a pipe expression more efficient without using a temporary variable
7071
- translateNormalAssignment() -> better code gen for assigning boolean comparison expressions
7172
- when a for loop's loopvariable isn't referenced in the body, and the iterations are known, replace the loop by a repeatloop
73+
but we have no efficient way right now to see if the body references a variable.
7274
- automatically convert if statements that test for multiple values (if X==1 or X==2..) to if X in [1,2,..] statements, instead of just a warning.
7375
- introduce byte-index operator to avoid index multiplications in loops over arrays? see github issue #4

examples/test.p8

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
main {
44
sub start() {
5-
uword zzz = memory("sdfasdf", 100, 0)
6-
str @shared foobar = "zsdfzsdf"
7-
str @shared foobar2 = sc:"zsdfzsdf"
5+
ubyte @shared xx
6+
7+
if xx==1 or xx==2 or xx==3 {
8+
xx++
9+
}
810
}
911
}

0 commit comments

Comments
 (0)