4
votes

A language like C uses the system stack for local variables and return addresses. Forth has the data stack and the return stack. Is there an implementation of Forth that uses the system stack as the return stack and hence uses the return instruction to end execution of a word? Is this a feasible approach?

3
Welcome to SO! Please see how to ask. It is unclear what you are trying to do here.DCTID
@DCTID It does no harm to reread how to ask. I'm into Forth and I can assure you that the question makes senseAlbert van der Horst

3 Answers

2
votes

The native call and ret instructions are used in the subroutine-threaded code. And in this case the system stack plays role of the return stack in Forth.

SP-Forth/4 is an example of a Forth system that uses this approach (see forthproc) along with peephole optimization.

0
votes

Yep, it's perfectly feasible.

It is probably only useful if you compile words instead of interpreting them, and you'd need to write some kind of stack frame convention if you want to catch return stack errors instead of just crashing.

0
votes

It's possible to make a Forth-like language that uses only one stack, but if you want a Forth that's compatible with standard Forth programs you would need separate data stack and return stack. Forth passes arguments from function to function by just leaving them on the data stack; if you use one stack for both data and flow control you need to be careful to clean the data off the stack before a function ends.