diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index 22b869db3..0bc3a1cd2 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -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? { diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/IfElseAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/IfElseAsmGen.kt index 6fdecf2df..b71e7b9a4 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/IfElseAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/IfElseAsmGen.kt @@ -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}") diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/PointerAssignmentsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/PointerAssignmentsGen.kt index d10f30338..53e635706 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/PointerAssignmentsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/PointerAssignmentsGen.kt @@ -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)") diff --git a/examples/test.p8 b/examples/test.p8 index 9bf06147e..2d6bb029c 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -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") } }