diff --git a/compiler/test/TestOptimization.kt b/compiler/test/TestOptimization.kt index 9a884afd3..f68a9d947 100644 --- a/compiler/test/TestOptimization.kt +++ b/compiler/test/TestOptimization.kt @@ -776,4 +776,20 @@ main { """ compileText(VMTarget(), true, text, writeAssembly = false) shouldNotBe null } + + test("sub only called by asm should not be optimized away") { + val src=""" +main { + sub start() { + %asm{{ + jsr p8_test + }} + } + + sub test() { + cx16.r0++ + } +}""" + compileText(Cx16Target(), true, src, writeAssembly = true) shouldNotBe null + } }) diff --git a/compilerAst/src/prog8/compiler/CallGraph.kt b/compilerAst/src/prog8/compiler/CallGraph.kt index 6cfc2d7aa..fe8446792 100644 --- a/compilerAst/src/prog8/compiler/CallGraph.kt +++ b/compilerAst/src/prog8/compiler/CallGraph.kt @@ -163,7 +163,7 @@ class CallGraph(private val program: Program) : IAstVisitor { return allIdentifiersAndTargets.filter { decl===it.value }.map{ it.key } } - private fun nameInAssemblyCode(name: String) = allAssemblyNodes.any { name in it.names } + private fun nameInAssemblyCode(name: String) = allAssemblyNodes.any { "p8_$name" in it.names || name in it.names } inline fun unused(label: Label) = false // just always output labels diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 7bb54e775..4eda356b6 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -4,8 +4,6 @@ TODO - IRQ callback should return boolean to tell Prog8 to call the kernal ISR or not. -- Is there a way to tell prog8 not to optimize out a function that is only called from within %asm{{}}? like a @shared but for subs? - - [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 .... ... diff --git a/examples/test.p8 b/examples/test.p8 index ca875d7bc..133cfcf67 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,14 +1,14 @@ %import textio -%import floats %zeropage basicsafe main { sub start() { - float fl1 = 999.8877 - float fl2 = sqrt(fl1) * fl1 + fl1*33.44 - floats.print_f(fl2) - txt.nl() - fl1=0 - floats.print_f(fl1) + %asm{{ + jsr p8_test + }} + } + + sub test() { + cx16.r0++ } }