why do we need the Stack

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

why do we need the Stack

JoeyWoo

We need a intermediate language which is used as the VM language. I think that any language such as Java C++ can do this(maybe I don't get the point this book talk about), I don't understand why we need the Stack.

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: why do we need the Stack

WBahn
Administrator
You don't NEED the stack -- but your life would be miserable without it.

You are talking about two different things -- the virtual machine and the stack. These are very distinct concepts, it just happens that THIS virtual machine happens to be stack-oriented.

So let's first ask why we want to use a stack.

Think about some of the key things that have to happen for a program written in nearly any high-level language to work. You need to be able to evaluate arbitrarily complicated expressions, you need to be able to write functions, you need to be able to pass arguments to the functions you call, you need functions to have local variables, you need to be able to get values back from the functions you call, you need functions to be able to call other functions, you need functions to be able to call themselves.  

It turns out that all of these things can be performed efficiently and very elegantly using a stack. We could implement them all without using a stack, but it would be a nightmare. In fact, the word 'stack' doesn't appear anywhere in the C language standard, so implementers are free to not use one, but I'm not aware of any implementation that is not stack oriented.

We could implement a compiler that directly translates the high-level code into code that manipulates the stack (and I suspect some implementations do). This will likely result in tighter, higher-performing code, but it significantly complicates the design, implementation, and testing of the compiler, not to mention the challenge that maintaining it would become.

So this is where the virtual machine comes in -- and this is NOT a virtual machine like VMWare are the Java Virtual Machine (it is nothing like the first, but has ties to the second). Consider this -- why not write programs by entering 0's and 1's into a file? Because humans aren't good at it. So we developed assemblers that move us one step away from 0's and 1's. Implementing an assembler is relative straight forward since we are translating between two languages that are pretty close to each other. This allows humans to grasp the necessary translations and do so efficiently and effectively. Going from a high-level language directly to assembly is too much of a gulf for most people and they could be neither efficient nor effective. What we need is an intermediate language that is not as big a step away from assembly. The virtual machine language fits the bill. It has commands whose implementations are straightforward in assembly language. But those commands also lend themselves to being used to implement the constructs in high-level languages. So now we humans can better grasp the logic of translating from a high-level language to the VM language.

Reply | Threaded
Open this post in threaded view
|

Re: why do we need the Stack

JoeyWoo
Thanks for your reply.

In Chapter 7 and 8, the book says two things:
1 We need an intermediate language.  VM language
2 How to implement this language.   Using Stack

Why The VM language is designed basing on Stack is that it can simplify everything. In fact, we can implement a language without Stack, but it would be very difficult.

Am I right?
Reply | Threaded
Open this post in threaded view
|

Re: why do we need the Stack

WBahn
Administrator
JoeyWoo wrote
Thanks for your reply.

In Chapter 7 and 8, the book says two things:
1 We need an intermediate language.  VM language
2 How to implement this language.   Using Stack

Why The VM language is designed basing on Stack is that it can simplify everything. In fact, we can implement a language without Stack, but it would be very difficult.

Am I right?
Yes. Keep in mind that the scope of this text is so broad -- it basically spans the material taught in four to six courses -- that the authors go to some lengths to keep things very minimal. At times, that results in them saying things as though they are universal and absolute or as though it is the only way when, strictly speaking, they are merely common or typical.
Reply | Threaded
Open this post in threaded view
|

Re: why do we need the Stack

JoeyWoo
Thank you very much!