diff --git a/compiler/res/prog8lib/diskio.p8 b/compiler/res/prog8lib/diskio.p8 index 8f4153bec..017fb37d2 100644 --- a/compiler/res/prog8lib/diskio.p8 +++ b/compiler/res/prog8lib/diskio.p8 @@ -124,10 +124,8 @@ io_error: ; read the filename repeat { ubyte char = c64.CHRIN() - if char==0 + if_z break - ; if_z - ; break ; TODO fix generated code for this jump if char=='\"' break @(nameptr) = char diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 7e32abb0c..37913d266 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -1076,21 +1076,43 @@ $counterVar .byte 0""") out(" $instruction ${getJumpTarget(jump)}") translate(stmt.elsepart) } else { + val truePartIsJustBreak = stmt.truepart.statements.firstOrNull() is Break + val elsePartIsJustBreak = stmt.elsepart.statements.firstOrNull() is Break if(stmt.elsepart.containsNoCodeNorVars()) { + if(truePartIsJustBreak) { + // branch with just a break (jump out of loop) + val instruction = branchInstruction(stmt.condition, false) + val loopEndLabel = loopEndLabels.peek() + out(" $instruction $loopEndLabel") + } else { + val instruction = branchInstruction(stmt.condition, true) + val elseLabel = makeLabel("branch_else") + out(" $instruction $elseLabel") + translate(stmt.truepart) + out(elseLabel) + } + } + else if(truePartIsJustBreak) { + // branch with just a break (jump out of loop) + val instruction = branchInstruction(stmt.condition, false) + val loopEndLabel = loopEndLabels.peek() + out(" $instruction $loopEndLabel") + translate(stmt.elsepart) + } else if(elsePartIsJustBreak) { + // branch with just a break (jump out of loop) but true/false inverted + val instruction = branchInstruction(stmt.condition, true) + val loopEndLabel = loopEndLabels.peek() + out(" $instruction $loopEndLabel") + translate(stmt.truepart) + } else { val instruction = branchInstruction(stmt.condition, true) val elseLabel = makeLabel("branch_else") + val endLabel = makeLabel("branch_end") out(" $instruction $elseLabel") translate(stmt.truepart) - out(elseLabel) - } else { - val instruction = branchInstruction(stmt.condition, false) - val trueLabel = makeLabel("branch_true") - val endLabel = makeLabel("branch_end") - out(" $instruction $trueLabel") - translate(stmt.elsepart) out(" jmp $endLabel") - out(trueLabel) - translate(stmt.truepart) + out(elseLabel) + translate(stmt.elsepart) out(endLabel) } } diff --git a/examples/test.p8 b/examples/test.p8 index f79a34388..0b7e8340c 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -10,10 +10,15 @@ errors { repeat { ubyte char3 = c64.CHRIN() + if_cc + break + if_z - goto labeltje - if_z - break ; TODO wrong jump asm generated, works fine if you use a label instead to jump to + char3 ++ + else + break + + char3-- } labeltje: