Re: Phix : loop question for Pete
- Posted by petelomax Feb 26, 2018
- 1066 views
Yes, unfortunately I had to.
25/10/2017 An "illegal/unsupported construct" error now occurs if an end for statement is immediately preceded by an unconditional exit. Where possible, use a simple if construct instead. This proved necessary because the opEndFor was being skipped, but that sets the zero iterations jump. Example: for i=1 to length(s) do ?s[i] exit end for (s[1] errored on length(s)=0, because the <1 jump was wrong.) However this sort of thing is (still) fine: for i=1 to length(s) do ?s[i] if "abc"="def" then ?"what??" else exit end if end for (assuming the compiler does not optimise that test away too) Update: Problem resurfaced. There are now fixes for this in both pmain.e/DoFor() and pilx86.e/jskip().
Feel free to try and improve this. I've tried and failed on at least three occasions, perhaps a fresh perspective would help.
What happens is that the code in pilx86.e line 10787
-- finally, batchpatch zero-iteration jmp(s) [if any] at loop start:
is not executed, if an unconditional exit causes the opEndFor to be skipped. Note that error can also happen for some always-true cases such as if DEBUG then exit end if.
Perhaps opEndFor could/should be split into opEndFor and opPatchForStart, with the latter not being skipped by an unconditional exit, i.e. that new opcode emitted after the final "backpatch(exitBP,..)" in pmain.e/DoFor().
[I don't think that I actually tried, or previously thought of doing that...]
Pete