Re: What does "virtual machine" and "real platform" mean?
This post was updated on .
"Real platform" means a physical computer. The idea is, that instead of targeting a specific hardware platform like x68, MIPS or HACK, the compiler is going to target a "virtual machine", that is not a real one. Then we'll have to map this VM code to the real hardware on which it will operate.
The book compares this to Java's JVM or .Net's CLR virtual machines, but it's a bit misleading. The term "virtual machine" can mean at least 3 different things:
1. Emulators for real hardware. These can be both for contemporary machines (e.g. VMware and VirtualBox target modern-day x86 and x86 hardware) and historical ones for various old computers and consoles. The use-case here is to run existing software without any need to change it.
2. Runtime platforms, like JVM, CLR and P-system. Here we have a system, which doesn't correspond to a real hardware. Instead it is meant to hide the differences between various real platforms, so that the same code can run on different platforms without modification. To run it, you need to have the VM-runtime (e.g. the JVM) running on the target machine.
3. Intermediate compilation target. The goal is similar to the runtime platforms, but the difference is, that the intermediate representation isn't executable by itself. Instead, one has to compile it to the real platforms that they need to get executable programs. Real world examples include LLVM and GCC.
The main benefit of the 3rd type of VM is, that it vastly simplifies compiling to multiple targets (different real hardware) and it also simplifies creating compilers for multiple languages. Consider this scenario:
You have to create compilers for 3 languages (e.g. C, Pascal and Jack) and each of them has to produce code for 3 platforms (x86, 68K and HACK). With the direct approach you'll need 9 compilers. With VM, you'll need 6 compilers: 3 Lang -> VM compilers + 3 VM -> target platform compilers.
This approach has additional benefits: Adding a new language requires just 1 compiler (Lang -> VM) and it automatically works on all supported target platforms.
Adding a new target platform also requires 1 new compiler (VM -> Target) and it will work with all supported languages.
Yet another benefit is with optimizing the executable. With direct compilation (Lang -> Target), the compilers will have a lot of common optimizations, but it will be quite hard to port them from one compiler to the next, especially if this wasn't planned from the start. With VM, the optimizations are split in 3:
1. language-level optimizations (specific for each language, but benefit all targets)
2. VM-level optimizations (benefit all languages and all platforms)
3. Target-level optimizations (specific for the given hardware, but benefit all the languages)
Again, this makes adding new languages or new targets much easier. And optimizing existing languages, targets or intermediate code, with greater direct benefits.
The VM from N2T is of the 3rd type. In the course, we're only developing one target platform (HACK) and one high-level language (JACK), so its benefits aren't that obvious, but they are there.
 Although here, HACK is considered a real platform.