[toc title=”Table of Contents” hint=”Click on links to Navigate the Guide” class=toc-right]
The Java Compiler Kit is a straightforward implementation of a [[Java compiler]], designed with extensibility in mind. Jkit differs from most Java compilers, in that it provides a simple [[intermediate language]] suitable for [[static program analysis]]. In building JKit, the aims where:
- To help with teaching compilers by considering one for a fully fledged language (Java), rather than a stripped-down imitation language.
- To aid research in programming languages, compilers and verification.
JKit was started in 2008, and has since been used in a number of research projects.
As is common in compiler design, JKit uses a (configurable) staged pipeline for processing source files. The pipeline begins by reading and parsing the source file into an [[abstract syntax tree]]; then, various stages are executed in a careful manner to process the AST in various ways, before it is converted into an [[intermediate language]] called the Java Intermediate Language (JIL); the JIL code is finally converted into [[Java Bytecode]] and written out as a [[Java Class File]]. The following gives a pictorial overview of the pipeline which highlights the most important stages:
- Java File Reader. This stage is responsible for reading a Java source file, parsing it and constructing an [[Abstract Syntax Tree]] (AST). The ANTLR parser generator is used to construct an initial parse tree, and this is then refined to form the completed AST.
- Skeleton Discovery. This stage is responsible for identifying the names of all classes being compiled. This includes the names of any inner classes which are declared. This information is used in the following stage to determine what type a given name actually refers to.
- Type Resolution. This stages determines the full type of any variable declarations with incomplete types. For example, a variable declaration
String xhas an incomplete type. Most likely, the full type would be
java.lang.String. However, this depends on what classes are declared in the given source file, and on what import statements are present.
- Skeleton Building. At this point, more complete information regarding the classes being compiled is known. In particular, the full type of each method return and parameter is available. This stage collates all of this information and puts it into a skeleton. The skeleton for a class records the full type name of the class, its superclass and any interfaces being implemented. It also records the full type for all method parameters and return values. This information is critical for type propagation, which occurs later.
- Scope Resolution. This stage determines the scope for all variables used in a method. For example, suppose an expression
x+ymay occurs in a method for some class being compiled. The question is, what are
y? They could be local variables, fields of this class, fields of a superclass, or fields of an enclosing class (for non-static inner classes). To determine this, we need to traverse the list of enclosing scopes and possibly the hierarchy of class skeletons we have constructed. When a skeleton is needed that is not one of the classes being compiled, the
ClassLoaderwill load it on demand for us using the current
- Type Propagation. This stage determines the type of all expressions which occur in a method being compiled. For example, consider an expression
x+y. The type of this expression depends on the declared types of variables
y. The Java Language Specification incorporates the notion of binary numeric promotion to determine the proper type.
- Type Checking. This stage is responsible for checking that types are used appropriately in all methods being compiled. Incorrect usages result in syntax errors being produced. In some cases, automatic conversions may be applied where appropriate. For example, an int variable may be assigned to a float variable, but this requires the compiler to insert an automatic conversion.
- Anon Class Rewrite. This stage is responsible for breaking down anonymous [[inner class|inner classes]]. The stage simply rewrites the source code to something which is equivalent, but doesn’t contain an anonymous inner class. Instead, it will create a normal class with a special name and replace the anonymous inner class with that.
- Inner Class RewriteThis stage is responsible for breaking down [[inner class|inner classes]]. The stage simply rewrites the source code to something which is equivalent, but doesn’t contain an inner class. Instead, it will create a normal class with a special name made up from the parent class name and the inner class name.
- Enum RewriteThis stage is responsible for breaking down [[enumerated type|enumerations]]. The stage simply rewrites the source code to something which is equivalent, but doesn’t contain an inner class. Instead, it will create a normal class with a special name made up from the parent class name and the inner class name.
- JIL Generation
- Dead-Code Elimination
- Definite Assignment
- Bypass Methods
- Bytecode Generation
- Peephole Optimisation
- ClassFile Generation
- The Java Language Specification, Third Edition is a fantastic online resource which provides a very detailed description of what should be considered correct Java programs, and how such a program should execute.
- The Java Virtual Machine Specification, Second Edition is another excellent online resource which covers many aspects of the [[Java Class File]] format. In particular, it provides a detailed description of every [[Java Bytecode]].
Java Intermediate Language