Logical statements give unexpected results when compiling with -O optimization
Closed this issue · 3 comments
Orvid, logical statement evaluation seems to be broken in Caprica v0.2.0 when using optimization.
I put the following debug code in a script and compiled it with Caprica using the -O optimization option.
Debug.Trace("!TRUE && TRUE = " + (!TRUE && TRUE), 1)
Debug.Trace("!TRUE && FALSE = " + (!TRUE && FALSE), 1)
Debug.Trace("!FALSE && TRUE = " + (!FALSE && TRUE), 1)
Debug.Trace("!FALSE && FALSE = " + (!FALSE && FALSE), 1)
Debug.Trace("!TRUE || TRUE = " + (!TRUE || TRUE), 1)
Debug.Trace("!TRUE || FALSE = " + (!TRUE || FALSE), 1)
Debug.Trace("!FALSE || TRUE = " + (!FALSE || TRUE), 1)
Debug.Trace("!FALSE || FALSE = " + (!FALSE || FALSE), 1)
RESULT:
[06/21/2016 - 10:52:53PM] !TRUE && TRUE = True (this should be FALSE)
[06/21/2016 - 10:52:53PM] !TRUE && FALSE = True (this should be FALSE)
[06/21/2016 - 10:52:53PM] !FALSE && TRUE = True (correct result)
[06/21/2016 - 10:52:53PM] !FALSE && FALSE = False (correct result)
[06/21/2016 - 10:52:53PM] !TRUE || TRUE = True (correct result)
[06/21/2016 - 10:52:53PM] !TRUE || FALSE = False (correct result)
[06/21/2016 - 10:52:53PM] !FALSE || TRUE = False (this should be TRUE)
[06/21/2016 - 10:52:53PM] !FALSE || FALSE = False (this should be TRUE)
As you can see, some of the results are incorrect compared with what I would expect from the CK compiler.
Edit: HOWEVER, it does work correctly if the -O option is not used, so it is an issue with the optimization. Cheers
:D Well, I knew the branching optimization wasn't fully safe, so time to disable them. (enabling optimizations has no significant impact right now, as Caprica generates better code than the CK without needing to use any extra pass)
OK. I was mainly using that feature because it generates pex files that Champollion cannot decompile ;P
Is there any other way that I could compile my scripts in a way that they won't decompile? Sometimes I want to keep certain scripts private for various reasons.
Btw, I've just started using the new language extensions and am enjoying those additions as well as the very fast compiler speed. Cheers.
In order to do the optimization I was doing safely, I'd need to implement SSA form for the Pex API. The Pex API is currently a 1-1 with the way it's stored on disk, so I'd have to adjust that to a resolved system instead.
As far as breaking Champollion, it doesn't understand any of the language extensions, and, although it will decompile them, it does a few of them (Do .. LoopWhile
for instance) completely wrong.
If I remember correctly from the very little that I looked into it a few months ago, the reason Champollion breaks when -O is enabled is because I fold a not ::temp1 ::temp1; assign ::temp1 ::temp1; jmpT ::temp1 label2
to jmpF ::temp1 label2
, and Champollion specifically expects the useless assign
instruction to be there, as the Creation Kit's compiler doesn't have the ability to use the same temporary for both the source and destination of an instruction. The not
being removed also probably didn't help things. (the not
being folded into the branch is why you're hitting this issue in the first place :D)