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 ; read the filename
repeat { repeat {
ubyte char = c64.CHRIN() ubyte char = c64.CHRIN()
if char==0 if_z
break break
; if_z
; break ; TODO fix generated code for this jump
if char=='\"' if char=='\"'
break break
@(nameptr) = char @(nameptr) = char

View File

@ -1076,21 +1076,43 @@ $counterVar .byte 0""")
out(" $instruction ${getJumpTarget(jump)}") out(" $instruction ${getJumpTarget(jump)}")
translate(stmt.elsepart) translate(stmt.elsepart)
} else { } else {
val truePartIsJustBreak = stmt.truepart.statements.firstOrNull() is Break
val elsePartIsJustBreak = stmt.elsepart.statements.firstOrNull() is Break
if(stmt.elsepart.containsNoCodeNorVars()) { 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 instruction = branchInstruction(stmt.condition, true)
val elseLabel = makeLabel("branch_else") val elseLabel = makeLabel("branch_else")
val endLabel = makeLabel("branch_end")
out(" $instruction $elseLabel") out(" $instruction $elseLabel")
translate(stmt.truepart) 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(" jmp $endLabel")
out(trueLabel) out(elseLabel)
translate(stmt.truepart) translate(stmt.elsepart)
out(endLabel) out(endLabel)
} }
} }

View File

@ -10,10 +10,15 @@ errors {
repeat { repeat {
ubyte char3 = c64.CHRIN() ubyte char3 = c64.CHRIN()
if_cc
break
if_z if_z
goto labeltje char3 ++
if_z else
break ; TODO wrong jump asm generated, works fine if you use a label instead to jump to break
char3--
} }
labeltje: labeltje: