diff --git a/codeOptimizers/src/prog8/optimizer/Inliner.kt b/codeOptimizers/src/prog8/optimizer/Inliner.kt index 6ce822bcb..5b8a74256 100644 --- a/codeOptimizers/src/prog8/optimizer/Inliner.kt +++ b/codeOptimizers/src/prog8/optimizer/Inliner.kt @@ -224,8 +224,11 @@ class Inliner(private val program: Program, private val options: CompilationOpti noModifications } else -> { - sub.hasBeenInlined=true - listOf(IAstModification.ReplaceNode(origNode, toInline.copy(), parent)) + if(origNode !== toInline) { + sub.hasBeenInlined = true + listOf(IAstModification.ReplaceNode(origNode, toInline.copy(), parent)) + } else + noModifications } } } @@ -255,7 +258,7 @@ class Inliner(private val program: Program, private val options: CompilationOpti is Return -> { // is an expression, so we have to have a Return here in the inlined sub // note that we don't have to process any args, because we online inline parameterless subroutines. - if(toInline.value!=null) { + if(toInline.value!=null && functionCallExpr!==toInline.value) { sub.hasBeenInlined=true listOf(IAstModification.ReplaceNode(functionCallExpr, toInline.value!!.copy(), parent)) } diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index b15a90b11..403034c1c 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -434,7 +434,7 @@ private fun optimizeAst(program: Program, compilerOptions: CompilationOptions, e } removeUnusedCode(program, errors,compilerOptions) - while (true) { + for(num_cycles in 0..10000) { // keep optimizing expressions and statements until no more steps remain val optsDone1 = program.simplifyExpressions(errors) val optsDone2 = program.optimizeStatements(errors, functions, compilerOptions) @@ -444,8 +444,13 @@ private fun optimizeAst(program: Program, compilerOptions: CompilationOptions, e errors.report() break } - if (optsDone1 + optsDone2 + optsDone3 == 0) + val numOpts = optsDone1 + optsDone2 + optsDone3 + if (numOpts == 0) break + + if(num_cycles==10000) { + throw InternalCompilerException("optimizeAst() is looping endlessly, numOpts = $numOpts") + } } removeUnusedCode(program, errors, compilerOptions) if(errors.noErrors()) { diff --git a/examples/test.p8 b/examples/test.p8 index 23abc6cf6..3ec96ba43 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -2,12 +2,7 @@ %zeropage basicsafe main { - sub start() { - txt.print_ub(sys.sizeof_byte) - txt.spc() - txt.print_ub(sys.sizeof_word) - txt.spc() - txt.print_ub(sys.sizeof_float) - txt.nl() - } + sub foo() { foo() } + sub bar() -> ubyte { return bar() } + sub start() { } }