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

[ColorForth] drop/dup Optimization


Kristopher Johnson wrote:
> 
> OK, maybe I just misinterpreted what it said on Chuck's page.  
> I assumed it meant that every occurrence of "DROP DUP" would 
> be eliminated.  

That could still be true.  Chuck simply would not write
1 2 DROP DUP at any time.  There would be no occurences
in his code where LITERAL DROP or DROP DUP would get
written except perhaps for a couple of macros.

> If it only
> happens in certain cases (like for 0, -1, and other literals), 
> where the value being DUP-ed doesn't matter, then I'm not 
> worried.

1 2 DROP DUP might likely produce 1 2 instead of 1 1 
if Chuck wrote that in a system where he setup
DUP as a macro that removed DROP rather than DROP 
as a macro that removed a literal, a DUP, an OVER or an A.

Chuck wouldn't write LITERAL DROP or OVER DROP or 
DUP DROP so he doesn't need the peephole optimization
for DROP (unless DROP were used in some other macros).

It is a good thing to notice, but not something to
worry about unless you add other macros that break
the rules or compile 1 2 DROP DUP and need that to
produce 1 1 instead of 1 2.  

He didn't use macros for DUP or DROP in the Machine
Forths for his chips.   Improved 0 and -1 macros for
the 21 chips could use a macro for the leading DUP
that removes DROP.  DUP and DROP are simply inlined
opcodes with the programmer doing optimizaitons.

If he has a DUP macro that removes a DROP in the
MachineForth under Pentium ColorForth it would still
match the code he would write for his own chips.
His optimization is mostly done by the programmer
in the editor.  The design is very simple, inline
his opcodes, compile calls, use tail recursion in ;
( or -; ) and have a few macros.   The macros are
subtily different on Pentium and other chips.

If 1 2 DROP DUP ( not just DUP in a -1 or 0 macro)
produced 1 2 instead of 1 1 it wouldn't worry Chuck.
He, as the main optimizing compiler, would not
write, or think that and need or expect it to produce
1 1.

It is an interesting observation.  I haven't checked
to see what : X 1 2 DROP DUP ; X would do in Chuck's
ColorForth.  Someone else will have to check it.

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