mirror of
https://github.com/irmen/prog8.git
synced 2025-01-27 10:31:40 +00:00
fix zp address output and adjust vars datastructure
This commit is contained in:
parent
fb4c1473c5
commit
a3b5c2ad71
@ -348,19 +348,19 @@ class AsmGen(private val program: Program,
|
||||
val result = zeropage.allocate(scopedName, variable.datatype, null, null, errors)
|
||||
errors.report()
|
||||
result.fold(
|
||||
success = { address -> out("${variable.name} = $address\t; zp ${variable.datatype}") },
|
||||
success = { (address, _) -> out("${variable.name} = $address\t; zp ${variable.datatype}") },
|
||||
failure = { /* leave it as it is, not on zeropage. */ }
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// Var has been placed in ZP, just output the address
|
||||
val lenspec = when(zpAlloc.second) {
|
||||
val lenspec = when(zpAlloc.second.first) {
|
||||
DataType.FLOAT,
|
||||
DataType.STR,
|
||||
in ArrayDatatypes -> " ${zpAlloc.first.second} bytes"
|
||||
in ArrayDatatypes -> " ${zpAlloc.second.second} bytes"
|
||||
else -> ""
|
||||
}
|
||||
out("${variable.name} = ${zpAlloc.first.first}\t; zp ${variable.datatype} $lenspec")
|
||||
out("${variable.name} = ${zpAlloc.first}\t; zp ${variable.datatype} $lenspec")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1507,7 +1507,7 @@ $repeatLabel lda $counterVar
|
||||
DataType.UBYTE, DataType.UWORD -> {
|
||||
val result = zeropage.allocate(listOf(counterVar), dt, null, stmt.position, errors)
|
||||
result.fold(
|
||||
success = { zpvar -> asmInfo.extraVars.add(Triple(dt, counterVar, zpvar.first)) },
|
||||
success = { (address, _) -> asmInfo.extraVars.add(Triple(dt, counterVar, address)) },
|
||||
failure = { asmInfo.extraVars.add(Triple(dt, counterVar, null)) } // allocate normally
|
||||
)
|
||||
return counterVar
|
||||
|
@ -40,12 +40,12 @@ class CX16Zeropage(options: CompilationOptions) : Zeropage(options) {
|
||||
removeReservedFromFreePool()
|
||||
|
||||
for(reg in 0..15) {
|
||||
allocatedVariables[listOf("cx16", "r${reg}")] = ((2+reg*2).toUInt() to 2) to DataType.UWORD // cx16.r0 .. cx16.r15
|
||||
allocatedVariables[listOf("cx16", "r${reg}s")] = ((2+reg*2).toUInt() to 2) to DataType.WORD // cx16.r0s .. cx16.r15s
|
||||
allocatedVariables[listOf("cx16", "r${reg}L")] = ((2+reg*2).toUInt() to 1) to DataType.UBYTE // cx16.r0L .. cx16.r15L
|
||||
allocatedVariables[listOf("cx16", "r${reg}H")] = ((3+reg*2).toUInt() to 1) to DataType.UBYTE // cx16.r0H .. cx16.r15H
|
||||
allocatedVariables[listOf("cx16", "r${reg}sL")] = ((2+reg*2).toUInt() to 1) to DataType.BYTE // cx16.r0sL .. cx16.r15sL
|
||||
allocatedVariables[listOf("cx16", "r${reg}sH")] = ((3+reg*2).toUInt() to 1) to DataType.BYTE // cx16.r0sH .. cx16.r15sH
|
||||
allocatedVariables[listOf("cx16", "r${reg}")] = (2+reg*2).toUInt() to (DataType.UWORD to 2) // cx16.r0 .. cx16.r15
|
||||
allocatedVariables[listOf("cx16", "r${reg}s")] = (2+reg*2).toUInt() to (DataType.WORD to 2) // cx16.r0s .. cx16.r15s
|
||||
allocatedVariables[listOf("cx16", "r${reg}L")] = (2+reg*2).toUInt() to (DataType.UBYTE to 1) // cx16.r0L .. cx16.r15L
|
||||
allocatedVariables[listOf("cx16", "r${reg}H")] = (3+reg*2).toUInt() to (DataType.UBYTE to 1) // cx16.r0H .. cx16.r15H
|
||||
allocatedVariables[listOf("cx16", "r${reg}sL")] = (2+reg*2).toUInt() to (DataType.BYTE to 1) // cx16.r0sL .. cx16.r15sL
|
||||
allocatedVariables[listOf("cx16", "r${reg}sH")] = (3+reg*2).toUInt() to (DataType.BYTE to 1) // cx16.r0sH .. cx16.r15sH
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,9 +18,8 @@ abstract class Zeropage(protected val options: CompilationOptions) {
|
||||
|
||||
|
||||
// the variables allocated into Zeropage.
|
||||
// name (scoped) ==> pair of (address and bytesize) and DataType
|
||||
// TODO switch the pair around
|
||||
protected val allocatedVariables = mutableMapOf<List<String>, Pair<Pair<UInt, Int>, DataType>>()
|
||||
// name (scoped) ==> pair of address to (Datatype + bytesize)
|
||||
protected val allocatedVariables = mutableMapOf<List<String>, Pair<UInt, Pair<DataType, Int>>>()
|
||||
private val allocations = mutableMapOf<UInt, Pair<List<String>, DataType>>()
|
||||
|
||||
val free = mutableListOf<UInt>() // subclasses must set this to the appropriate free locations.
|
||||
@ -97,7 +96,7 @@ abstract class Zeropage(protected val options: CompilationOptions) {
|
||||
free.removeAll(address until address+size.toUInt())
|
||||
allocations[address] = name to datatype
|
||||
if(name.isNotEmpty())
|
||||
allocatedVariables[name] = (address to size) to datatype
|
||||
allocatedVariables[name] = address to (datatype to size)
|
||||
return address
|
||||
}
|
||||
|
||||
@ -107,5 +106,5 @@ abstract class Zeropage(protected val options: CompilationOptions) {
|
||||
return free.containsAll((address until address+size.toUInt()).toList())
|
||||
}
|
||||
|
||||
fun allocatedZeropageVariable(name: List<String>): Pair<Pair<UInt, Int>, DataType>? = allocatedVariables[name]
|
||||
fun allocatedZeropageVariable(name: List<String>): Pair<UInt, Pair<DataType, Int>>? = allocatedVariables[name]
|
||||
}
|
||||
|
112
examples/test.p8
112
examples/test.p8
@ -1,120 +1,22 @@
|
||||
%import textio
|
||||
%import floats
|
||||
%import test_stack
|
||||
%zeropage floatsafe
|
||||
|
||||
main {
|
||||
%option force_output
|
||||
|
||||
sub start() {
|
||||
test_stack.test()
|
||||
|
||||
ubyte @requirezp foobar = 2
|
||||
uword @requirezp foobar2 = 2
|
||||
uword @requirezp foobar3 = 2
|
||||
uword @requirezp foobar4 = 2
|
||||
uword @requirezp foobar5 = 2
|
||||
uword @requirezp foobar6 = 2
|
||||
uword @requirezp foobar7 = 2
|
||||
uword @requirezp foobar8 = 2
|
||||
uword @requirezp foobar9 = 2
|
||||
uword @requirezp foobar10 = 2
|
||||
uword @requirezp foobar11 = 2
|
||||
uword @requirezp foobar12 = 2
|
||||
uword @requirezp foobar13 = 2
|
||||
uword @requirezp foobar14 = 2
|
||||
uword @requirezp foobar15 = 2
|
||||
float @shared @requirezp myfloat=1.23456789
|
||||
str @shared @requirezp name = "irmen"
|
||||
str @shared name2 = "hello"
|
||||
ubyte[] @shared @requirezp array = [1,2,3]
|
||||
|
||||
txt.print(name)
|
||||
txt.nl()
|
||||
txt.print_ub(array[0])
|
||||
txt.spc()
|
||||
txt.print_ub(array[1])
|
||||
txt.spc()
|
||||
txt.print_ub(array[2])
|
||||
txt.nl()
|
||||
txt.print_uwhex(&name, true)
|
||||
txt.nl()
|
||||
txt.print_uwhex(&array, true)
|
||||
txt.nl()
|
||||
txt.print_ub(foobar)
|
||||
txt.nl()
|
||||
floats.print_f(myfloat)
|
||||
|
||||
; float fl
|
||||
; test_stack.test()
|
||||
;
|
||||
; fl = addfloat3(addfloat2(addfloat1(1.234)))
|
||||
; floats.print_f(fl)
|
||||
; txt.nl()
|
||||
;
|
||||
; fl = 1.234 |> addfloat1 |> addfloat2 |> addfloat3
|
||||
; floats.print_f(fl)
|
||||
; txt.nl()
|
||||
; 1.234 |> addfloat1
|
||||
; |> addfloat2 |> addfloat3 |> floats.print_f
|
||||
; txt.nl()
|
||||
;
|
||||
; txt.print_uw(times_two(add_one(sin8u(add_one(assemblything(9+3))))))
|
||||
; txt.nl()
|
||||
;
|
||||
; 9+3 |> assemblything
|
||||
; |> add_one
|
||||
; |> sin8u
|
||||
; |> add_one
|
||||
; |> times_two
|
||||
; |> txt.print_uw
|
||||
; txt.nl()
|
||||
;
|
||||
; uword @shared uw= 9+3 |> assemblything
|
||||
; |> add_one
|
||||
; |> sin8u
|
||||
; |> add_one
|
||||
; |> times_two
|
||||
; txt.print_uw(uw)
|
||||
; txt.nl()
|
||||
|
||||
uword ww = 10
|
||||
repeat 10 {
|
||||
ww++
|
||||
txt.print_uw(ww)
|
||||
txt.nl()
|
||||
}
|
||||
test_stack.test()
|
||||
|
||||
repeat {
|
||||
}
|
||||
}
|
||||
|
||||
sub func() -> ubyte {
|
||||
txt.print("func!\n")
|
||||
return 99
|
||||
}
|
||||
|
||||
sub addfloat1(float fl) -> float {
|
||||
return fl+1.11
|
||||
}
|
||||
|
||||
sub addfloat2(float fl) -> float {
|
||||
return fl+2.22
|
||||
}
|
||||
|
||||
sub addfloat3(float fl) -> float {
|
||||
return fl+3.33
|
||||
}
|
||||
|
||||
sub add_one(ubyte input) -> ubyte {
|
||||
return input+1
|
||||
}
|
||||
|
||||
sub times_two(ubyte input) -> uword {
|
||||
return input*$0002
|
||||
}
|
||||
|
||||
asmsub assemblything(ubyte input @A) clobbers(X,Y) -> ubyte @A {
|
||||
%asm {{
|
||||
ldx #64
|
||||
asl a
|
||||
rts
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user