Skip to content

Commit

Permalink
tweak return's use of intermediate variable
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed Jan 24, 2022
1 parent adb979d commit 586ce1f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
12 changes: 8 additions & 4 deletions codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,14 @@ class StatementOptimizer(private val program: Program,
return null
}

if(returnStmt.value is BinaryExpression) {
val mod = returnViaIntermediaryVar(returnStmt.value!!)
if(mod!=null)
return mod
// TODO decision when to use intermediary variable to calculate returnvalue seems a bit arbitrary...
val returnvalue = returnStmt.value
if (returnvalue!=null) {
if (returnvalue is BinaryExpression || (returnvalue is TypecastExpression && !returnvalue.expression.isSimple)) {
val mod = returnViaIntermediaryVar(returnvalue)
if(mod!=null)
return mod
}
}

return noModifications
Expand Down
5 changes: 1 addition & 4 deletions docs/source/todo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ TODO

For next release
^^^^^^^^^^^^^^^^
- why does this use stack eval on return:
sub sprite_y_for_row(ubyte row) -> word {
return (8-row as byte)
}
...


Need help with
Expand Down
20 changes: 9 additions & 11 deletions examples/test.p8
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

main {
sub start() {
ubyte ccc
ubyte @shared qq = string.find("irmendejong", ccc)!=0
word ww
ww = calculate(6)
word ww = calculate(6)
txt.print_w(ww)
txt.nl()
ww = calculate(8)
txt.print_w(ww)
txt.nl()
ww = calculate(10)
txt.print_w(ww)
ubyte bb = calculate2(6)
txt.print_ub(bb)
txt.nl()
qq = string.find("irmendejong", ccc)!=0
}

sub calculate2(ubyte row) -> ubyte {
return 8+row
}


sub calculate(ubyte row) -> word {
return 8-(row as byte)
return 8+row
}
}

0 comments on commit 586ce1f

Please sign in to comment.