cardillan / mindcode

A high level language for Mindustry Logic and Mindustry Schematics.

Home Page:http://mindcode.herokuapp.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

If Expression optimization fails to preserve variable value

cardillan opened this issue · comments

The If Expression optimization may wrongly reuse a variable:

#set data-flow-optimization = off
i = rand(10)
i = i > 10 ? 1 : 2
print(i)

compiles to

op rand i 10 0
set i 2
jump 4 lessThanEq i 10
set i 1
print i
end

The optimizer moves the false branch in front of the if condition and replaces a temporary variable holding the result of the if expression with the eventual target -- i, overwriting the value of i before it is evaluated in the condition.