How is the VM really made?

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

How is the VM really made?

Andreas
Hi,

In the book we write the VM translator in any high level language of our choice. However, this assumes that the high level language of our choice compiles on the Hack computer.

In the real world, how would you write a VM? Surely it would have to be written in assembly code, which therefore suggests that it would have to be tailored to run on each architecture. However, this then takes us back to a problem which we were resolving by implementing the VM -- we don't need to have a custom compiler for each architecture (and thus it becomes very portable). However, we have a high level language compiling into an intermediate language, which is then converted (using the machine's language) into the machine language.

Also, with the Hack machine it shouldn't be possible to have the VM written in Jack, because in order to compile the Jack code to form the VM program you would need to translate it using the VM, so you would end up in an infinite loop.

Thanks for any responses.
Reply | Threaded
Open this post in threaded view
|

Re: How is the VM really made?

cadet1620
Administrator
Andreas wrote
Hi,

In the book we write the VM translator in any high level language of our choice. However, this assumes that the high level language of our choice compiles on the Hack computer.

The VM Translator runs on your computer and translates .vm files to .asm files. I wrote my VM Translator in Python. Writing a native VM is a much harder task and requires a much more sophisticated computer than Hack to run on.
In the real world, how would you write a VM? Surely it would have to be written in assembly code, which therefore suggests that it would have to be tailored to run on each architecture. However, this then takes us back to a problem which we were resolving by implementing the VM -- we don't need to have a custom compiler for each architecture (and thus it becomes very portable). However, we have a high level language compiling into an intermediate language, which is then converted (using the machine's language) into the machine language.
The Java VM is written in C/C++. The platform specific compilers allow it to run on the various different target hardware. Libraries have been written, also in C/C++, that interface with the target operating systems. When you download Java for a 32-bit Windows system you get a binary that is targeted to 32-bit Intel processors linked with the appropriate Windows support library. In the real world we write as little assembly code as possible.

For the embedded systems programming I do, the tools (compilers, etc.) run on Windows and only the binaries get loaded onto the actual hardware. All the code is written in C with a few small assembly language routines. 512K ROM and 64K RAM is a big configuration.

Also, with the Hack machine it shouldn't be possible to have the VM written in Jack, because in order to compile the Jack code to form the VM program you would need to translate it using the VM, so you would end up in an infinite loop.
Modern C/C++ compilers are written using earlier versions of themselves. The first C compiler was probably written in B, a precursor to the C language.

In college, we had to write an assembler in assembly language. It was written using the system supplied assembler, but the final for the course was to run our assembler through itself and show that it generated the correct output!

--Mark

Reply | Threaded
Open this post in threaded view
|

Re: How is the VM really made?

Andreas
So to build a VM for a new machine architecture you need another machine which has a programming language and compiler already in existance? But as you mentioned, the native approach is far more difficult. What does this process involve? Why does the machine need to be more sophisticated? Surely it can be done using assembly language and no hardware adjustments.

I suppose the same problem arises with the assembler. To write the assembler you need to have a programming language, so I suppose the very first assembler would have needed to be written in binary?
Reply | Threaded
Open this post in threaded view
|

Re: How is the VM really made?

cadet1620
Administrator
Andreas wrote
So to build a VM for a new machine architecture you need another machine which has a programming language and compiler already in existance? But as you mentioned, the native approach is far more difficult. What does this process involve? Why does the machine need to be more sophisticated? Surely it can be done using assembly language and no hardware adjustments.

I suppose the same problem arises with the assembler. To write the assembler you need to have a programming language, so I suppose the very first assembler would have needed to be written in binary?
Yes, in theory one could write a VM emulator that ran on the Hack platform.  I'm guessing that there would need to be more ROM to store the code for the VM emulator and there would need to be some way to load the VM code to be emulated into RAM. Some sort of storage peripherals?

Early computer programming was very different than what we do these days.  The first paper on programming a stored program computer that I am aware of is Planning and Coding of Problems for an Electronic Computing Instrument, John von Neumann, et.al., 1947. You can find it with Google if you're interested.

Not only did the first assembler need to be written in binary, think about this.  There was no boot ROM in these early computers.  How did the program get into them to read the OS tape?  How was the first OS tape written?  Ther's been an incredible amount of bootstrapping to get to where we are now.  We rest on the shoulders of giants!

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: How is the VM really made?

Andreas
Brilliant, thanks!