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
}
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))
}

View File

@ -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()) {

View File

@ -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() { }
}