Virtual machine emulator vs Virtual machine translator

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

Virtual machine emulator vs Virtual machine translator

a.reyes2511
I know what a virtual machine is, but whats the point of writing a virtual machine translator when we can make the compiler compile directly into hack machine code. Whats the point of the intermediate step.
Reply | Threaded
Open this post in threaded view
|

Re: Virtual machine emulator vs Virtual machine translator

ivant
When you compile one language to another, you'll often* need some intermediate representation. It could be as simple as parse trees or a more potent one like a stack machine or infinite register machine. You can modify this intermediate form for example to achieve various optimizations, and then translate it to the output language.

Another reason, specifically for this course is, that it provides a meaningful way to break up this rather large task (writing a compiler) to 4 smaller tasks.

For additional reasons, check out this thread: http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/What-does-quot-virtual-machine-quot-and-quot-real-platform-quot-mean-td4028959.html

----

* Unless the two languages are really close to each other.
Reply | Threaded
Open this post in threaded view
|

Re: Virtual machine emulator vs Virtual machine translator

a.reyes2511
I guess i was confused because in the book he mentioned we are going to implement a virtual machine. And thats not really what we are doing. Arent we really just  building a two stage compiler, where the vm translator is the back end of the compiler? I looked online and it said a virtual machine and a compiler arent the same thing.
Reply | Threaded
Open this post in threaded view
|

Re: Virtual machine emulator vs Virtual machine translator

WBahn
Administrator
I think you are thinking of a different meaning of "virtual machine".

I had the same misconception when I was introduced to Nand2Tetris because my only notion of what a virtual machine was came from things like VMWare and the like where we have a program running that behaves like a particular machine and then we run a program on that virtual hardware.

The notion of a virtual machine in a compiler is somewhat related, but also quite different. Here we compile a high level language to an intermediate level language as though we had a machine that used the VM language as its instruction set. Then we translate the resulting program into the actual instruction set of the target processor. So in that respect it is a two-stage compiler. But in another, very valid perspective, the program IS running on a virtual machine, it's simply a virtual machine that has been strongly bound to the program that is running on it.

We do this for a couple of reasons. First, making the jump from a high-level language all the way down to a two-register CPU is not easy and will be extremely difficult for most people to grasp and implement. But compiling it to a stack-oriented machine's instruction set is quite straightforward and translating the commands in that stack-oriented language to assembly is even more straightforward.

Second, the VM language is hardware-agnostic. So if you wanted to run your Jack programs on a different processor, all you have to do is make a VM translator to that processor's instruction set, which would be a pretty easy thing to do in most cases. If you don't do this intermediate step, you get to rewrite your entire compiler, which is not only going to be a lot harder to do, but a lot easier to make mistakes that will be hard to find and fix. Similarly, if you want to run C or Java programs on the Hack hardware, you only have to compile them down to the VM language since you already have a translator to take it the rest of the way.

Reply | Threaded
Open this post in threaded view
|

Re: Virtual machine emulator vs Virtual machine translator

a.reyes2511
I apologize, im confused by this statement. "But in another, very valid perspective, the program IS running on a virtual machine, it's simply a virtual machine that has been strongly bound to the program that is running on it. "

are u saying the vm translator is a virtual machine.

Can u elaborate.
Reply | Threaded
Open this post in threaded view
|

Re: Virtual machine emulator vs Virtual machine translator

WBahn
Administrator
You could probably think of the VM translator as being a virtual machine, but I don't think of it that way. In my view, the VM is an abstract construct. What the translator does is bind the virtual machine abstraction to the VM code that is translated.

Consider how a traditional virtual machine would work - you have a program (the virtual machine) that is running and then you tell it to access and run a program. It opens the file for that program, reads the first instruction that is to be executed, determines a sequence of instructions that will effectively execute that instruction on the host hardware, and then executes that sequence of instructions.

When you run the VM translator on a VM language program (the output of the compiler in most cases), it performs most of these steps. It reads a VM command from the input file, determines a sequence of assembly language instructions that will effectively execute that instruction on the Hack hardware, and writes those instructions out to an assembly language file for later execution of that sequence of instructions.