简体   繁体   中英

Is it practical for a Forth implementation to use the system stack as the return stack?

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?

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM