Java Reference
In-Depth Information
FIGURE 1.7 Re-use through decomposition.
Decomposition was certainly helpful to us, the authors, in writing the j-- compiler as it
allowed us better organize the program and to work concurrently on distinct parts of it.
1.3.5 Compiling to a Virtual Machine: New Boundaries
The Java compiler, the program invoked when one types, for example,
>javacMyProgram.java
produces a .class file called MyProgram.class , that is, a byte code 6 program suitable for
execution on a Java Virtual Machine (JVM). The source language is Java; the target machine
is the JVM. To execute this .class file, one types
>javaMyProgram
which effectively interprets the JVM program. The JVM is an interpreter, which is imple-
mented based on the observation that almost all programs spend most of their time in a
small part of their code. The JVM monitors itself to identify these \hotspots" in the program
it is interpreting, and it compiles these critical methods to native code; this compilation is
accompanied by a certain amount of in-lining: the replacement of method invocations by the
method bodies. The native code is then executed, or interpreted, on the native computer.
Thus, the JVM byte code might be considered an IR, with the Java \compiler" acting as
the front end and the JVM acting as the back end, targeted to the native machine on which
it is running.
The IR analogy to the byte code makes even more sense in Microsoft's Common Lan-
guage Runtime (CLR) architecture used in implementing its .Net tools. Microsoft has writ-
ten compilers (or front ends) for Visual Basic, C ++ , C # , and J ++ (a variant of Java), all of
which produce byte code targeted for a common architecture (the CLR). Using a technique
called just-in-time (JIT) compilation, the CLR compiles each method to native code and
caches that native code when that method is first invoked. Third parties have implemented
other front-end compilers for other programming languages, taking advantage of the existing
JIT compilers.
In this textbook, we compile a (non-trivial) subset of Java, which we call j--. In the first
instance, we target the Oracle JVM. So in a sense, this compiler is a front end. Nevertheless,
our compiler implements many of those phases that are traditional to compilers and so it
serves as a reasonable example for an introductory compilers course.
The experience in writing a compiler targeting the JVM is deficient in one respect: one
does not learn about register allocation because the JVM is a stack-based architecture and
has no registers.
6 \byte code" because the program is represented as a sequence of byte instructions and operands (and
operands occupy several bytes).
 
Search WWH ::




Custom Search