fix boolean pointer condition in if expression

This commit is contained in:
Irmen de Jong
2025-08-08 22:15:58 +02:00
parent 5ada80779d
commit f7fde070ca
4 changed files with 36 additions and 13 deletions

View File

@@ -259,7 +259,7 @@ class AsmGen6502Internal (
private val pointerGen = PointerAssignmentsGen(this, allocator)
private val assignmentAsmGen = AssignmentAsmGen(program, this, pointerGen, anyExprGen, allocator)
private val builtinFunctionsAsmGen = BuiltinFunctionsAsmGen(program, this, assignmentAsmGen)
private val ifElseAsmgen = IfElseAsmGen(program, symbolTable, this, assignmentAsmGen, errors)
private val ifElseAsmgen = IfElseAsmGen(program, symbolTable, this, pointerGen, assignmentAsmGen, errors)
private val ifExpressionAsmgen = IfExpressionAsmGen(this, assignmentAsmGen, errors)
fun compileToAssembly(): IAssemblyProgram? {

View File

@@ -6,11 +6,13 @@ import prog8.code.ast.*
import prog8.code.core.*
import prog8.codegen.cpu6502.assignment.AsmAssignTarget
import prog8.codegen.cpu6502.assignment.AssignmentAsmGen
import prog8.codegen.cpu6502.assignment.PointerAssignmentsGen
import prog8.codegen.cpu6502.assignment.TargetStorageKind
internal class IfElseAsmGen(private val program: PtProgram,
private val st: SymbolTable,
private val asmgen: AsmGen6502Internal,
private val pointergen: PointerAssignmentsGen,
private val assignmentAsmGen: AssignmentAsmGen,
private val errors: IErrorReporter) {
@@ -63,9 +65,14 @@ internal class IfElseAsmGen(private val program: PtProgram,
}
}
val deref = stmt.condition as? PtPointerDeref
if(deref!=null) {
TODO("ptr deref resulting in bool ${deref.position}")
val dereference = stmt.condition as? PtPointerDeref
if(dereference!=null) {
val zpPtrVar = pointergen.deref(dereference)
pointergen.loadIndirectByte(zpPtrVar)
return if (jumpAfterIf != null)
translateJumpElseBodies("bne", "beq", jumpAfterIf, stmt.elseScope)
else
translateIfElseBodies("beq", stmt)
}
throw AssemblyError("weird non-boolean condition node type ${stmt.condition} at ${stmt.condition.position}")

View File

@@ -101,7 +101,7 @@ internal class PointerAssignmentsGen(private val asmgen: AsmGen6502Internal, pri
private fun deref(pointer: PtPointerDeref): String {
internal fun deref(pointer: PtPointerDeref): String {
// walk the pointer deref chain and leaves the final pointer value in a ZP var
// this will often be the temp var P8ZP_SCRATCH_W1 but can also be the original pointer variable if it is already in zeropage
if(pointer.chain.isEmpty()) {
@@ -197,7 +197,7 @@ internal class PointerAssignmentsGen(private val asmgen: AsmGen6502Internal, pri
""")
}
private fun loadIndirectByte(zpPtrVar: String) {
internal fun loadIndirectByte(zpPtrVar: String) {
// loads byte pointed to by the ptrvar into A
if(asmgen.isTargetCpu(CpuType.CPU65C02))
asmgen.out(" lda ($zpPtrVar)")

View File

@@ -1,14 +1,30 @@
%import textio
%import floats
%option no_sysinit
%zeropage basicsafe
main {
uword[5] a
struct Enemy {
ubyte xpos, ypos
uword health
bool elite
}
sub start() {
pokebool(cx16.r0, false)
pokebool(a[2], false)
pokebool(cx16.r0+cx16.r1, false)
^^Enemy e1 = 30000
e1.elite=false
if e1.elite
txt.print("elite")
else
txt.print("pleb")
cx16.r0bL = peekbool(cx16.r0)
cx16.r0bL = peekbool(a[2])
cx16.r0bL = peekbool(cx16.r0+cx16.r1)
e1.elite = true
if e1.elite
txt.print("elite")
else
txt.print("pleb")
}
}