In the previous article we chose to generate code for a virtual machine. But what is it exactly?
A virtual machine is an emulator of a computer system. There are two types of virtual machines:
- system virtual machines, those that emulate a complete computer system, they can be used as a substitute of a real computer. They can execute an entire operating system, and thus they are also called full virtualization VMs.
- process virtual machines, they are designed to execute computer programs in a platform-independent environment.
Based on this specification it seems obvious that we need to write one of the second kind. What are the pros/cons of writing a process VM?
On the con side it will be slower than compiling our code to native machine code, because every instruction must be simulated at runtime every time the program is executed.
Sounds like a good compromise to me. Others thought that too, so this kind of VMs are the basis of lots of popular programming languages:
- Java and the JVM
- .NET languages (C#, VB.NET, etc.) and the Common Language Runtime
- Perl and Parrot
among many others.
We have almost everything now to actually execute our compiled program. The only piece missing is the language runtime. I will talk about it next time.
Stay tuned. I’ll be back.