fix peek/poke error with const offset

This commit is contained in:
Irmen de Jong
2026-01-10 20:38:58 +01:00
parent 2b26952044
commit 34b228cfca
5 changed files with 36 additions and 47 deletions
@@ -58,16 +58,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
"pokew" -> funcPokeW(fcall)
"pokel" -> funcPokeL(fcall)
"pokef" -> funcPokeF(fcall)
"pokemon" -> {
val memread = PtMemoryByte(fcall.position)
memread.add(fcall.args[0])
memread.parent = fcall
asmgen.assignExpressionToRegister(memread, RegisterOrPair.A)
asmgen.out(" pha")
val memtarget = AsmAssignTarget(TargetStorageKind.MEMORY, asmgen, DataType.UBYTE, fcall.definingISub(), fcall.position, memory=memread)
asmgen.assignExpressionTo(fcall.args[1], memtarget)
asmgen.out(" pla")
}
"pokemon" -> funcPokemon(fcall)
"poke" -> throw AssemblyError("poke() should have been replaced by @()")
"pokebool" -> funcPokeBool(fcall)
"rsave" -> funcRsave()
@@ -915,6 +906,23 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
}
}
private fun funcPokemon(fcall: PtBuiltinFunctionCall) {
val memread = PtMemoryByte(fcall.position)
memread.add(fcall.args[0])
memread.parent = fcall
asmgen.assignExpressionToRegister(memread, RegisterOrPair.A)
if(fcall.args[1] is PtNumber || fcall.args[1] is PtIdentifier)
asmgen.out(" tay")
else
asmgen.out(" pha")
val memtarget = AsmAssignTarget(TargetStorageKind.MEMORY, asmgen, DataType.UBYTE, fcall.definingISub(), fcall.position, memory=memread)
asmgen.assignExpressionTo(fcall.args[1], memtarget)
if(fcall.args[1] is PtNumber || fcall.args[1] is PtIdentifier)
asmgen.out(" tya")
else
asmgen.out(" pla")
}
private fun funcPokeF(fcall: PtBuiltinFunctionCall) {
when(val number = fcall.args[1]) {
is PtIdentifier -> {
@@ -1352,7 +1360,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
val varname = asmgen.asmVariableName(addressOfIdentifier)
if(result.second is PtNumber) {
val offset = (result.second as PtNumber).number.toInt()
asmgen.out(" lda $varname+$offset,x")
asmgen.out(" lda $varname+$offset")
} else if(result.second is PtIdentifier) {
val offsetname = asmgen.asmVariableName(result.second as PtIdentifier)
asmgen.out(" ldx $offsetname | lda $varname,x")
@@ -379,7 +379,7 @@ internal class AssignmentAsmGen(
val varname = asmgen.asmVariableName(addressOfIdentifier)
if(result.second is PtNumber) {
val offset = (result.second as PtNumber).number.toInt()
asmgen.out(" lda $varname+$offset,x")
asmgen.out(" lda $varname+$offset")
assignRegisterByte(target, CpuRegister.A, false, true)
return
} else if (result.second is PtIdentifier) {
@@ -664,38 +664,6 @@ jump p8_label_gen_2
chunk.instructions[idx] = loadi
changed = true
}
// add.w R2,#offset loadi.b R1,R2 -> loadfield.b R1,R2,#offset
// add.w R2,#offset storei.b R1,R2 -> storefield.b R1,R2,#offset
if (ins.reg1!=ins.reg2 && idx>1) {
val previous = indexedInstructions[idx-1].value
if (ins.opcode == Opcode.LOADI) {
if(previous.opcode==Opcode.ADD && previous.type == IRDataType.WORD && previous.reg1==ins.reg2) {
val loadfield = IRInstruction(Opcode.LOADFIELD, ins.type, ins.reg1!!, ins.reg2!!, immediate = previous.immediate!!)
chunk.instructions[idx-1] = loadfield
chunk.instructions.removeAt(idx)
changed = true
} else if(previous.opcode==Opcode.INC && previous.type == IRDataType.WORD && previous.reg1==ins.reg2) {
val loadfield = IRInstruction(Opcode.LOADFIELD, ins.type, ins.reg1!!, ins.reg2!!, immediate = 1)
chunk.instructions[idx-1] = loadfield
chunk.instructions.removeAt(idx)
changed = true
}
}
else if(ins.opcode == Opcode.STOREI) {
if(previous.opcode==Opcode.ADD && previous.type == IRDataType.WORD && previous.reg1==ins.reg2) {
val storefield = IRInstruction(Opcode.STOREFIELD, ins.type, ins.reg1!!, ins.reg2!!, immediate = previous.immediate!!)
chunk.instructions[idx-1] = storefield
chunk.instructions.removeAt(idx)
changed = true
} else if(previous.opcode==Opcode.INC && previous.type == IRDataType.WORD && previous.reg1==ins.reg2) {
val storefield = IRInstruction(Opcode.STOREFIELD, ins.type, ins.reg1!!, ins.reg2!!, immediate = 1)
chunk.instructions[idx-1] = storefield
chunk.instructions.removeAt(idx)
changed = true
}
}
}
}
return changed
+15 -2
View File
@@ -6,15 +6,28 @@ main {
sub start() {
float @shared fv,fv2,fv3,fv4 = 1.11111
^^float fptr = &fv
uword @shared @nozp uw,uw2,uw3,uw4 = 1111
^^uword uptr = &uw
ubyte @shared @nozp ub,ub2,ub3,ub4 = 111
txt.print_ub(peek(&ub2 + 1))
txt.spc()
poke(&ub2+1, 222)
txt.print_ub(peek(&ub2 + 1))
txt.spc()
txt.print_ub(pokemon(&ub2 + 1, 99))
txt.spc()
txt.print_ub(peek(&ub2 + 1))
txt.nl()
txt.nl()
txt.print_uw(peekw(&uw2 + 4))
txt.spc()
pokew(&uw2+ 4, 9999)
txt.print_uw(peekw(&uw2 + 4))
txt.nl()
txt.nl()
txt.print_f(peekf(&fv + sizeof(float)))
txt.spc()
+1 -1
View File
@@ -4,4 +4,4 @@ org.gradle.parallel=true
org.gradle.daemon=true
org.gradle.configuration-cache=false
kotlin.code.style=official
version=12.1
version=12.1-SNAPSHOT