optimized code for branches containing just a goto or break statement

This commit is contained in:
Irmen de Jong 2020-12-08 21:56:40 +01:00
parent 63332c0530
commit b7694686c2
3 changed files with 40 additions and 15 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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: