Skip to content

Commit

Permalink
fix void optimization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed Dec 9, 2023
1 parent dd4c073 commit 2668bf8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
21 changes: 14 additions & 7 deletions codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 0 additions & 10 deletions docs/source/todo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 ....

...
Expand Down
45 changes: 40 additions & 5 deletions examples/test.p8
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 2668bf8

Please sign in to comment.