function inlining can no longer get into an infinite loop. Fixes #154

This commit is contained in:
Irmen de Jong 2024-10-07 19:58:04 +02:00
parent ae669af904
commit a088ee56b0
3 changed files with 16 additions and 13 deletions

View File

@ -224,8 +224,11 @@ class Inliner(private val program: Program, private val options: CompilationOpti
noModifications noModifications
} }
else -> { else -> {
sub.hasBeenInlined=true if(origNode !== toInline) {
sub.hasBeenInlined = true
listOf(IAstModification.ReplaceNode(origNode, toInline.copy(), parent)) 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 Return -> {
// is an expression, so we have to have a Return here in the inlined sub // 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. // 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 sub.hasBeenInlined=true
listOf(IAstModification.ReplaceNode(functionCallExpr, toInline.value!!.copy(), parent)) listOf(IAstModification.ReplaceNode(functionCallExpr, toInline.value!!.copy(), parent))
} }

View File

@ -434,7 +434,7 @@ private fun optimizeAst(program: Program, compilerOptions: CompilationOptions, e
} }
removeUnusedCode(program, errors,compilerOptions) removeUnusedCode(program, errors,compilerOptions)
while (true) { for(num_cycles in 0..10000) {
// keep optimizing expressions and statements until no more steps remain // keep optimizing expressions and statements until no more steps remain
val optsDone1 = program.simplifyExpressions(errors) val optsDone1 = program.simplifyExpressions(errors)
val optsDone2 = program.optimizeStatements(errors, functions, compilerOptions) val optsDone2 = program.optimizeStatements(errors, functions, compilerOptions)
@ -444,8 +444,13 @@ private fun optimizeAst(program: Program, compilerOptions: CompilationOptions, e
errors.report() errors.report()
break break
} }
if (optsDone1 + optsDone2 + optsDone3 == 0) val numOpts = optsDone1 + optsDone2 + optsDone3
if (numOpts == 0)
break break
if(num_cycles==10000) {
throw InternalCompilerException("optimizeAst() is looping endlessly, numOpts = $numOpts")
}
} }
removeUnusedCode(program, errors, compilerOptions) removeUnusedCode(program, errors, compilerOptions)
if(errors.noErrors()) { if(errors.noErrors()) {

View File

@ -2,12 +2,7 @@
%zeropage basicsafe %zeropage basicsafe
main { main {
sub start() { sub foo() { foo() }
txt.print_ub(sys.sizeof_byte) sub bar() -> ubyte { return bar() }
txt.spc() sub start() { }
txt.print_ub(sys.sizeof_word)
txt.spc()
txt.print_ub(sys.sizeof_float)
txt.nl()
}
} }