1

Forth has a stack and a return-stack.

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.

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fadedbee
  • 42,671
  • 44
  • 178
  • 308
  • 1
    I'm no expert, but I guess in C it helps that the size of the parameters (which is the data you need to get past in order to "get" to the return address) is known at compile-time, and static. So is the size of the return value. In Forth, both of these are not known. – unwind May 17 '17 at 12:18
  • 1
    In C, usually, the return stack and the argument stack are the same. The arguments and the return address are (usually, this is not a necessity) on the machine stack. But C can just as well pass arguments in registers, and usually returns them in registers too. That is why C is not a stack based language like Forth. In Forth, the stack(s) are crucial to the language. – Rudy Velthuis May 17 '17 at 17:52
  • 1
    In Forth, the return stack is also often used as temporary storage, e.g. to get values off the data stack, so values "below" them are more easily accessible. And it generally plays a role in exception handling. It is not just used to remember the return addresses. Actually, it is not even mandatory that such return values are stored there. – Rudy Velthuis May 17 '17 at 17:55
  • 1
    So, yes, it would probably be possible, but it would be extremely hard to use, if arguments and local variables are usually not accessed in a random way (like in C, Pascal or most other languages). – Rudy Velthuis May 20 '17 at 15:58
  • The short version of the question (in the title) does not agree with the question that is actually asked. I've voted this question down, but I will reverse that if you fix this. – Albert van der Horst Jul 18 '21 at 19:42

3 Answers3

5

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. 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

Lars Brinkhoff
  • 13,542
  • 2
  • 28
  • 48
5

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.

The C stack consists of a mixture of return addresses, local variables and function parameters. 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. 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. But at what cost!

Of course, C programs have this overhead, too. 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.

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.

Philippos
  • 284
  • 2
  • 14
0

"Forth has a stack and a return-stack." 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.

So you have answered your own question:A resounding no. The two stacks are central to the Forth programming model. 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. In fact C has no stack in its programming model. Using one stack is an implemention detail that your are not supposed to worry about.