From 85956b582870cbfc0ab0df20df80e3d1412e10fa Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 20 Oct 2021 22:36:13 +0200 Subject: [PATCH] code generator: add a return (RTS) to empty subroutines. Fixes #67 --- .../prog8/compiler/BeforeAsmGenerationAstChanger.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt b/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt index 5761b5591..f0816cfc5 100644 --- a/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt +++ b/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt @@ -120,13 +120,13 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, val errors: I // and if an assembly block doesn't contain a rts/rti, and some other situations. val mods = mutableListOf() val returnStmt = Return(null, subroutine.position) - if (subroutine.asmAddress == null - && !subroutine.inline - && subroutine.statements.isNotEmpty() - && subroutine.amountOfRtsInAsm() == 0 + if (subroutine.asmAddress == null && !subroutine.inline) { + if(subroutine.statements.isEmpty() || + (subroutine.amountOfRtsInAsm() == 0 && subroutine.statements.lastOrNull { it !is VarDecl } !is Return - && subroutine.statements.last() !is Subroutine) { - mods += IAstModification.InsertLast(returnStmt, subroutine) + && subroutine.statements.last() !is Subroutine)) { + mods += IAstModification.InsertLast(returnStmt, subroutine) + } } // precede a subroutine with a return to avoid falling through into the subroutine from code above it