简体   繁体   English

Forth 实现使用系统堆栈作为返回堆栈是否可行?

[英]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.像 C 这样的语言将系统堆栈用于局部变量和返回地址。 Forth has the data stack and the return stack. Forth 有数据栈和返回栈。 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?是否有使用系统堆栈作为返回堆栈并因此使用返回指令来结束一个字的执行的 Forth 实现? Is this a feasible approach?这是一种可行的方法吗?

The native call and ret instructions are used in the subroutine-threaded code .本机callret指令用于子例程线程代码 And in this case the system stack plays role of the return stack in Forth.在这种情况下,系统堆栈在 Forth 中扮演返回堆栈的角色。

SP-Forth/4 is an example of a Forth system that uses this approach (see forthproc ) along with peephole optimization . SP-Forth/4 是一个 Forth 系统的例子,它使用了这种方法(参见第四进程)和窥视孔优化

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 语言,但是如果您想要一个与标准 Forth 程序兼容的 Forth,您将需要单独的数据堆栈和返回堆栈。 Forth passes arguments from function to function by just leaving them on the data stack; Forth 将参数从一个函数传递到另一个函数,只是将它们留在数据堆栈中; 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.如果您将一个堆栈用于数据和流量控制,您需要小心地在函数结束之前清除堆栈中的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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