diff --git a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt index 5dcdd0999..c6249b1e3 100644 --- a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt +++ b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt @@ -1340,9 +1340,7 @@ $repeatLabel lda $counterVar throw AssemblyError("only else part contains code, shoud have been switched already") val jump = stmt.truepart.statements.first() as? Jump - if(jump!=null) { - if(jump.isGosub) - throw FatalAstException("didn't expect GoSub here") + if(jump!=null && !jump.isGosub) { // branch with only a jump (goto) val instruction = branchInstruction(stmt.condition, false) out(" $instruction ${getJumpTarget(jump)}") diff --git a/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt b/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt index 088d4186c..fb770e3d7 100644 --- a/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt +++ b/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt @@ -279,7 +279,19 @@ class StatementOptimizer(private val program: Program, } override fun after(jump: Jump, parent: Node): Iterable { - if(!jump.isGosub) { + if(jump.isGosub) { + // if the next statement is return with no returnvalue, change into a regular jump if there are no parameters as well. + val subroutineParams = jump.identifier?.targetSubroutine(program)?.parameters + if(subroutineParams!=null && subroutineParams.isEmpty()) { + val returnstmt = jump.nextSibling() as? Return + if(returnstmt!=null && returnstmt.value==null) { + return listOf( + IAstModification.Remove(returnstmt, parent as IStatementContainer), + IAstModification.ReplaceNode(jump, Jump(jump.address, jump.identifier, jump.generatedLabel, jump.position), parent) + ) + } + } + } else { // if the jump is to the next statement, remove the jump val scope = jump.parent as IStatementContainer val label = jump.identifier?.targetStatement(program) diff --git a/examples/test.p8 b/examples/test.p8 index fbc2b462b..b444bbe3b 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,9 +3,18 @@ main { sub start() { - - uword foobar - - foobar() + ubyte @shared yy + yy = foobar() + } + sub foobar() -> ubyte { + if_mi { + foobar2() + foobar2() + return true + } + return 22 + } + sub foobar2() { + main.start.yy++ } }