home .. forth .. colorforth mail list archive ..

[ColorForth] drop/dup Optimization


Kristopher Johnson wrote:
> 
> On the http://www.colorforth.com/install.htm page, it says 
> "When one macro ends with 'drop' and the next starts with 
> 'dup' both may be eliminated"
> 
> I think this is backwards (maybe a typo).  DROP followed 
> by DUP can't generally be optimized away by eliminating both.  
> (Maybe "'dup' followed by 'drop'" is
> what was intended.)

The most common macro may be -1, DUP DUP XOR COM is
slightly faster and smaller than 0 # COM.  Likewise
the macro for 0, DUP DUP XOR is slightly faster and
slightly smaller than 0 # in MachineForth.  Generating
-1 or 0 this way takes less time and space than the
full word memory load required by a literal. (Although
DUP DUP does use an extra stack position which is
the tradeoff)

... DROP DUP DUP ...   can be converted to 
... DUP ...   as in the -1 and 0 macros.
Since you are going to destroy it anyway by
converting it into a 0 you can use the thing
that you were going to drop instead.

Chuck doesn't have a lot of general macro 
recombinations because the doesn't have a lot
of macros.  He has said that literals can
use peephole optimization and some operations
like + can check for literals and combine
things at compile time with peephole optimization.
But he doesn't.  He doesn't really need to
because he isn't using lots of macros that
combine this way.  His code is more manually
crafted to combine logically, he is the 
optimizer.

Chuck uses a color change equivalent to
[ 12345 23456 + ] literal to get the same thing
that peephone optimization would yield.

> For example,
> 
> : foo  1 2 drop dup ;  \ result: ( 1 1 )
> 
> is not the same as
> 
> : foo  1 2 ;           \ result ( 1 2 )

True.  But then again peephole optimization would compile
the DROP by removing the 2.

1 2 DROP  -->  removes 2 DROP   thus yields    1
1 DUP    thus yields   1 1 

> Or maybe I'm missing something related to the caching 
> of the top-of-stack in EAX.  Or maybe the optimization 
> is more sophisticated, being triggered only in
> situations where "DROP DUP" really is a no-op.

You may have just been expecting it to be more complicated than
it really is.  It really is quite simple.

Jeff Fox
------------------------

To Unsubscribe from this list, send mail to Mdaemon@xxxxxxxxxxxxxxxxxx with:
unsubscribe ColorForth
as the first and only line within the message body
Problems   -   List-Admin@xxxxxxxxxxxxxxxxxx
Main ColorForth site   -   http://www.colorforth.com