简体   繁体   English

可以只用一个堆栈来实现类似 Forth 的语言吗?

[英]Can a Forth-like language be implemented with just one stack?

Forth has a stack and a return-stack. Forth 有一个栈和一个返回栈。

As far as I understand, the point of the return-stack is to store the previous values of the program counter.据我了解,返回堆栈的重点是存储程序计数器的先前值。

C programs put the previous value of program counter on the stack, and use no return stack. C 程序将程序计数器的先前值放在堆栈上,并且不使用返回堆栈。

Does Forth only need a return-stack because it returns result(s) on the stack, and thus the previous value of the program counter could be buried? Forth 是否只需要一个返回堆栈,因为它在堆栈上返回结果,因此程序计数器的先前值可能会被掩埋?

The "Portable Assembly Language" should be close. “便携式汇编语言”应该很接近。 It's a concept for a compiler for a language which is almost identical to standard/traditional Forth.这是一个几乎与标准/传统 Forth 相同的语言的编译器概念。 There are some restrictions for which kind of programs can be written.对于可以编写的程序类型有一些限制。 Mostly, you have to avoid situations where the depth of the stack can't be statically determined.大多数情况下,您必须避免无法静态确定堆栈深度的情况。

This language can be compiled in a way that only requires one stack.这种语言可以以只需要一个堆栈的方式进行编译。

http://www.complang.tuwien.ac.at/anton/euroforth/ef13/papers/ertl-paf.pdf http://www.complang.tuwien.ac.at/anton/euroforth/ef13/papers/ertl-paf.pdf

Preface: I've written extensions for hardware debuggers to trace some problems with messed call stacks, so I've seen some hex dumps of actual C stacks.前言:我已经为硬件调试器编写了扩展来跟踪混乱的调用堆栈的一些问题,所以我看到了一些实际C堆栈的十六进制转储。

The C stack consists of a mixture of return addresses, local variables and function parameters. C堆栈由返回地址、局部变量和函数参数的混合组成。 For each function you can find out where it expects which value, but this knowledge stops at the scope of the function.对于每个函数,您可以找出它期望哪个值的位置,但这种知识仅停留在函数的范围内。

You can do it this way with forth as well, but this means a giant overhead: You can place your parameters on the stack and call the function, placing the return address on top of the same stack.你可以这样来做forth为好,但这意味着一个巨大的开销:您可以将堆栈上的参数,调用函数,将返回地址在同一个堆栈的顶部。 No problem: Each command knows that the operands are second and following on the stack.没问题:每个命令都知道操作数在堆栈中是第二个和后面的。 But now the command wants to call another command with these values: This makes it neccessary to change the order of the stack to put the values on top again, as the called command can't know where they are buried in the stack, as you wrote.但是现在命令想要使用这些值调用另一个命令:这使得有必要更改堆栈的顺序以再次将值放在顶部,因为被调用的命令无法知道它们在堆栈中的位置,因为您写道。

A more complex forth compiler could keep track of how many values each command takes and sort the stack before each command call.更复杂的forth编译器可以跟踪每个命令采用多少个值,并在每个命令调用之前对堆栈进行排序。 But at what cost!但代价是什么!

Of course, C programs have this overhead, too.当然, C程序也有这种开销。 If you see non-inline function calls in inline assembly, there is always some register swapping overhead.如果您在内联汇编中看到非内联函数调用,那么总会有一些寄存器交换开销。 But good forth programs consist of tiny functions, so you have a lot more function calling and a lot more overhead.但是,良好的forth程序由微小的功能,让你有更多的函数调用和更大量的开销。

And without any benefit, as in modern computer architecture each register can be used as stack pointer, so you could have a handful of stacks without any problem.而且没有任何好处,就像在现代计算机体系结构中一样,每个寄存器都可以用作堆栈指针,因此您可以毫无问题地拥有少量堆栈。

So finally, the answer to your question is: Yes, you can implement a one-stack derivative of forth , but it's pain without gain.所以最后,回答你的问题是:是的,你可以实现的一组衍生forth ,但它的痛苦没有增益。

"Forth has a stack and a return-stack." “Forth 有一个栈和一个返回栈。” This is true for every extant and preceeding Forth standard, for pre-standard dialects and even for dialects like colorforth that some hesitate to call Forth.对于每一个现存的和之前的 Forth 标准,对于前标准方言,甚至对于像 colorforth 这样的方言,有些人不愿将其称为 Forth。

So you have answered your own question:A resounding no.所以你已经回答了你自己的问题:一个响亮的不。 The two stacks are central to the Forth programming model.这两个堆栈是 Forth 编程模型的核心。 The programmer is entitled to manipulate both.程序员有权操作两者。

The reason is of course that you handle the data yourself and a return address would be in the way.原因当然是您自己处理数据并且返回地址会碍事。 If you insist to compare it with C, C manipulates the data by name, and handles the returning.如果您坚持将其与 C 进行比较,C 将按名称操作数据,并处理返回。 In fact C has no stack in its programming model.事实上,C 在其编程模型中没有堆栈。 Using one stack is an implemention detail that your are not supposed to worry about.使用一个堆栈是一个您不应该担心的实现细节。

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

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