diff --git a/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt b/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt index 66f52ef16..81418df0b 100644 --- a/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt +++ b/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt @@ -139,13 +139,20 @@ class UnusedCodeRemover(private val program: Program, ) } else if(assignment.value is IFunctionCall) { // replace the unused variable's initializer function call by a void - errors.warn("replaced unused variable '${decl.name}' with void call, maybe this can be removed altogether", decl.position) - val fcall = assignment.value as IFunctionCall - val voidCall = FunctionCallStatement(fcall.target, fcall.args, true, fcall.position) - return listOf( - IAstModification.ReplaceNode(decl, voidCall, parent), - IAstModification.Remove(assignment, assignment.parent as IStatementContainer) - ) + // but only if the vardecl immediately precedes it! + if(singleUse.parent.parent === parent) { + val declIndex = (parent as IStatementContainer).statements.indexOf(decl) + val singleUseIndex = (parent as IStatementContainer).statements.indexOf(singleUse.parent) + if(declIndex==singleUseIndex-1) { + errors.warn("replaced unused variable '${decl.name}' with void call, maybe this can be removed altogether", decl.position) + val fcall = assignment.value as IFunctionCall + val voidCall = FunctionCallStatement(fcall.target, fcall.args, true, fcall.position) + return listOf( + IAstModification.ReplaceNode(decl, voidCall, parent), + IAstModification.Remove(assignment, assignment.parent as IStatementContainer) + ) + } + } } else { errors.warn("variable '${decl.name}' is unused but has non-trivial initialization assignment. Leaving this in but maybe it can be removed altogether", decl.position) } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 95abbeda6..2f8247868 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,16 +2,6 @@ TODO ==== - -- fix problem with conversion to void call messing up the return '!': - if cx16.r0L==9 { - ; process TAB - uword cmd = grab_cmdline() - return '!' - } - - - - [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 881129f80..705953b6e 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,9 +1,44 @@ -%option enable_floats +%import textio +%import string +%zeropage basicsafe + main { sub start() { - uword w1 = 000_1234_5__ - uword w2 = $ff_ee - uword w3 = %11_0000_111111__0000 - float fl = 3_000_001.141_592_654 + cx16.set_chrin_keyhandler(0, &keystroke_handler) + cbm.CHRIN() + } + + sub keystroke_handler() -> ubyte { + %asm {{ + sta cx16.r0L + }} + uword cmdxx = grab_cmdline() + if_cs { + ; first entry, decide if we want to override + if cx16.r0L==9 { + ; intercept TAB + sys.clear_carry() + return 0 + } + sys.set_carry() + return 0 + } else { + if cx16.r0L==9 { + %asm {{ + brk ; BOOM + }} + uword cmd = grab_cmdline() + if cmd and cmd[0] { + ;cx16.r5++ + } + return '!' + } + return 0 ; eat all other characters + } + + sub grab_cmdline() -> uword { + cx16.r9++ + return $5000 + } } }