Re: optimisation issue #2

Samuel Tardieu <[email protected]> 30 Nov 2004 12:17:49 +0100
Newsgroups gmane.comp.lang.forth.picforth
Organization Avian Carrier & Friends
Message-ID <[email protected]>
>>>>> "David" == David McNab <[email protected]> writes:

David> Hi, Thanks to Samuel for earlier enlightening reply.

You're welcome.

As a rule, when you submit questions like that (that I am happy to
answer as it illustrates real use cases), please try to provide a
compilable example, so that I and others can start to play with it
right away (in this case, it was lacking constants).

David> As I (mis)understand PicForth, the 'return-in-w' command flags
David> a word, such that when another word calls it, the calling word
David> knows to look in W instead of TOS for the return value.

Exactly -- however, it only tells the *caller*, as you correctly
write. It is the callee job to place the result in W using >w.
The reason for that is that some assembly routines may be called from
such a Forth word and let the result in W, and the compiler can't
figure that out by itself.

If you do that (add ">w" before every "return"), you will end with 3
instructions per test.

Also, I never encountered such a case, so I never implemented the
"movlw N+return->retlw N" and "clrw+return->retlw 0"
optimizations. This is now done in my development version. You get 2
instructions per test, as in your assembly code:

        ; name: 5key.getc-raw
        ; max return-stack depth: 0
0x0004  1888    btfsc   0x08,1
0x0005  3400    retlw   0x00
0x0006  1908    btfsc   0x08,2
0x0007  3401    retlw   0x01
0x0008  1988    btfsc   0x08,3
0x0009  3402    retlw   0x02
0x000A  1A08    btfsc   0x08,4
0x000B  3403    retlw   0x03
0x000C  1A88    btfsc   0x08,5
0x000D  3404    retlw   0x04
0x000E  3495    retlw   0x95

(with made-up constants and ports)

David> So my (hopefully small) feature request is: - expose
David> 'return-in-w' in 'target' wordspace - extend the PicForth
David> optimiser to take advantage of 'return-in-w' and generate code
David> which makes full use of this.

I'll expose "return-in-w" and explain it, but I don't think changing
code generation would be a good thing for reasons explained above.

Does that look reasonable?

  Sam
-- 
Samuel Tardieu -- [email protected] -- http://www.rfc1149.net/sam