On coding DO/LOOP and friends in a STC Forth
Posted: Wed Jan 28, 2015 10:21 pm
In a different thread, there was a discussion on how to code DO/LOOP and friends such as ?DO and LEAVE (and IF, THEN, ELSE while we are at it) in STC Forth. Though the basic mechanisms are clear, it might be helpful to have the discussion all in one place.
As a brute-force overview, these are the possible combinations we're talking about that will have to work, from simplest to most complicated (LOOP is merely shorthand for 1 +LOOP, so we'll use "LOOP" for both here): (We ignore UNLOOPing for the moment as part of LOOP and/or LEAVE) If you examine this word by word, you can see that
If we use the Data Stack as the control-flow stack, we need a way to tell LOOP what it has to call how many times and when. Making the whole thing more difficult is that it makes sense to use the Return Stack to hold the limit and start values of ?DO/DO with the "V-flag fudge" which makes ANS-loop behavior (the Laxen & Perry F83 method). Even worse, because a loop is the last place we want to be slow, it would be nice to do as much as possible directly in assembler (though it should be easier to use Forth and pseudocode for the examples). Oh, and the STC is going to fool around with the Return Stack a lot, too.
I'd like to use this thread to discuss how to do this, starting in the next entry where I'll take care of the easy cases. I'll include a bit more explanation of what they do for those people who are new to this part of Forth.
As a brute-force overview, these are the possible combinations we're talking about that will have to work, from simplest to most complicated (LOOP is merely shorthand for 1 +LOOP, so we'll use "LOOP" for both here):
Code: Select all
1. IF THEN
2. IF ELSE THEN
3. DO LOOP
4. ?DO LOOP
5. DO LEAVE LOOP
6. ?DO LEAVE LOOP
7. DO IF LEAVE THEN LOOP
8. ?DO IF LEAVE THEN LOOP
9. DO IF LEAVE ELSE THEN LOOP
10. DO IF ELSE LEAVE THEN LOOP
11. ?DO IF LEAVE ELSE THEN LOOP
12. ?DO IF ELSE LEAVE THEN LOOP- IF, THEN, DO, LEAVE always take exactly one action
- ELSE, ?DO always take exactly two actions
- LOOP can take between one and three actions (argh).
If we use the Data Stack as the control-flow stack, we need a way to tell LOOP what it has to call how many times and when. Making the whole thing more difficult is that it makes sense to use the Return Stack to hold the limit and start values of ?DO/DO with the "V-flag fudge" which makes ANS-loop behavior (the Laxen & Perry F83 method). Even worse, because a loop is the last place we want to be slow, it would be nice to do as much as possible directly in assembler (though it should be easier to use Forth and pseudocode for the examples). Oh, and the STC is going to fool around with the Return Stack a lot, too.
I'd like to use this thread to discuss how to do this, starting in the next entry where I'll take care of the easy cases. I'll include a bit more explanation of what they do for those people who are new to this part of Forth.