fix void optimization issue

This commit is contained in:
Irmen de Jong 2023-12-09 21:48:22 +01:00
parent dd4c073e18
commit 2668bf8519
3 changed files with 54 additions and 22 deletions

View File

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

View File

@ -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 ....
...

View File

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