Skip to content

Commit 586ce1f

Browse files
committed
tweak return's use of intermediate variable
1 parent adb979d commit 586ce1f

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,14 @@ class StatementOptimizer(private val program: Program,
459459
return null
460460
}
461461

462-
if(returnStmt.value is BinaryExpression) {
463-
val mod = returnViaIntermediaryVar(returnStmt.value!!)
464-
if(mod!=null)
465-
return mod
462+
// TODO decision when to use intermediary variable to calculate returnvalue seems a bit arbitrary...
463+
val returnvalue = returnStmt.value
464+
if (returnvalue!=null) {
465+
if (returnvalue is BinaryExpression || (returnvalue is TypecastExpression && !returnvalue.expression.isSimple)) {
466+
val mod = returnViaIntermediaryVar(returnvalue)
467+
if(mod!=null)
468+
return mod
469+
}
466470
}
467471

468472
return noModifications

docs/source/todo.rst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ TODO
33

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

118

129
Need help with

examples/test.p8

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44

55
main {
66
sub start() {
7-
ubyte ccc
8-
ubyte @shared qq = string.find("irmendejong", ccc)!=0
9-
word ww
10-
ww = calculate(6)
7+
word ww = calculate(6)
118
txt.print_w(ww)
129
txt.nl()
13-
ww = calculate(8)
14-
txt.print_w(ww)
15-
txt.nl()
16-
ww = calculate(10)
17-
txt.print_w(ww)
10+
ubyte bb = calculate2(6)
11+
txt.print_ub(bb)
1812
txt.nl()
19-
qq = string.find("irmendejong", ccc)!=0
2013
}
2114

15+
sub calculate2(ubyte row) -> ubyte {
16+
return 8+row
17+
}
18+
19+
2220
sub calculate(ubyte row) -> word {
23-
return 8-(row as byte)
21+
return 8+row
2422
}
2523
}

0 commit comments

Comments
 (0)