[ColorForth] Tail-recursion optimisation idea
- Subject: [ColorForth] Tail-recursion optimisation idea
- From: Eric Laforest <ecl@xxxxxxxxxxx>
- Date: Mon, 3 Dec 2001 03:15:26 -0500
Here is an idea I came up with while puttering on a stack computer design.
(The '?;' notation was thought up by someone else, whom I'll name if he
wishes so (I'm not sure if he's on this list...I should ask, but I'm
impatient :) )
Normally, a while-false loop goes like this:
(I can't do colors here, but the notation should be equivalent)
"While TASK leaves a false, execute TASK"
: FOO TASK IF ; THEN FOO ;
Once TASK is done, this takes one cycle for the IF (a JMP0)
and then another for a JMP back to the call to TASK.
: FOO TASK FOO ?;
This would generate a JMP0 to the call to TASK, saving a cycle.
(and implicitely add a RET afterwards)
Better yet:
: TASK ..... TASK ?;
Which collapses this even further by eliminating the call/return overhead.
This variant of tail-recursion optimization is rather limited, but could
be of use where timing is tight or for loops repeated a zillion times.
This could be taken even further by having a 'conditional jump or return'
instruction, such that a flag would cause either a JMP or a RET...but this
is a 'fat' instruction and would probably not map well to hardware.
It's probably also not a good factorization.
Comments?
Eric LaForest
------------------------
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