[PicForth] Re: More <0 strangeness

Alex Holden alex at linuxhacker.org
Wed Nov 24 21:28:55 CET 2004


Samuel Tardieu wrote:
>>>>>>"Alex" == Alex Holden <alex at linuxhacker.org> writes:
> Alex> This time I appear to have found a weird optimization bug with
> Alex> the 0< word.
> Could you try the following patch? (or get it from my repository)

Thanks; this fixes the first issue I reported but not the second one. 
Here is some example code which demonstrates it:

: (test0<)
         dup . space
         dup 0< if [char] - emit else [char] + emit then space
         dup 0< . cr
;

: test0<
         0 (test0<)
         &100 (test0<)
         -&100 (test0<)
;

I would expect to see the following output:
0 + 0
100 + 0
-100 - -1

What I actually get is:
0 + -1
100 + -1
-100 - -1

In other words if I use a word other than 'if' after the '0<' the number 
on the TOS is always -1 even if the number that was on the stack before 
calling '0<' was negative. It seems to be some kind of optimization 
problem because if I disable the optimizer the code works as expected.

I just extended the test to check 0= and 0<> and it appears that 0<> has 
the problem but 0= doesn't. Here's the output from my second test 
program with optimization enabled:

0 0< = false (-1)
100 0< = false (-1)
-100 0< = true (-1)
0 0<> = false (-1)
100 0<> = true (-1)
-100 0<> = true (-1)
0 0= = true (-1)
100 0= = false (0)
-100 0= = false (0)

Here's the relevant source:

: (test0<)
         dup . c"  0< = " type
         dup 0< if c" true (" type else c" false (" type then
         dup 0< . [char] ) emit cr
;
: test0<
         0 (test0<)
         &100 (test0<)
         -&100 (test0<)
;
: (test0<>)
         dup . c"  0<> = " type
         dup 0<> if c" true (" type else c" false (" type then
         dup 0<> . [char] ) emit cr
;
: test0<>
         0 (test0<>)
         &100 (test0<>)
         -&100 (test0<>)
;
: (test0=)
         dup . c"  0= = " type
         dup 0= if c" true (" type else c" false (" type then
         dup 0= . [char] ) emit cr
;
: test0=
         0 (test0=)
         &100 (test0=)
         -&100 (test0=)
;
: test
         test0<
         test0<>
         test0=
;

-- 
------------ Alex Holden - http://www.linuxhacker.org ------------
If it doesn't work, you're not hitting it with a big enough hammer


More information about the PicForth mailing list