7.1.程序编译前瞻

\(7.1.\)Program Compilation Preview

1.Problem in compilation

  To start with an abstract program and end with something actually runs on the computer, we need

  • A computer that can run machine language code.
  • Translating the high level language into machine language.

  However, there are so many kinds of computers in the world, and many of them use different processes, which take different machine languages. It seems that we have to develop many compilers, one for each processor. This is very annoying.

  Luckily, this "write once fix everywhere" can be replaced by a "write once run everywhere" process.

2. 2-Tier compilation

  Let's take Java as example:

  • The first tier: also called the top tier

    • Java compiler translates the Java program into VM code.

      • The VM code is designed to run on an abstract artifact called virtual machine.
      • It's not a real computer, so if we want to execute the code, we have to translate it further into machine language and realize it on real hardware (computer, for example).

  • The second tier:

    • We equip the target device with JVM, the Java machine implementation.
    • The JVM is a program that takes VM code and translates it into the target code of the target platform.
    • We need such translator for every platform on which we want to execute VM program.

  The gap between high-level language (Java, for example) and low-level language is huge. But by introducing an intermediate level, we decouple this process into two separate standalone sub-processes: the compiler of VM compiler and VM implementation.