Next: Data Type Support Up: First Approach Direct Compilation Previous: First Approach Direct Compilation   Contents

Using Jasmin Assembler

The JVM class file format is somewhat complex. Directly generating such a file from a B module would be tricky. Unfortunately, there is no standard assembler syntax for the JVM, so there are no tools in the standard Java environment to easily generate JVM class files directly. Attempting to generate a JVM class from B was a focus of much attention early in this project. Generating class files from B proved very difficult, given the strict format of a JVM class file.

However, Brian Jepson proposed that instead of generating the JVM class file directly, output instead could be generated using the Jasmin assembler [15]. Jasmin assembler [19, pages 399-409] is a syntax for writing JVM class files that is similar to assembler formats used for physical architectures. This solution greatly reduced the problem scope, since there was now an easy way to generate JVM class files from B.

However, there was the concern that the Jasmin assembler format is not standardized; it is simply one possible format for JVM bytecode assembler. Indeed, other formats do exist and are in use. Therefore, it was imperative that a JVM port not rely on one particular assembler syntax.

To alleviate this problem, the concept of JVM ``bytecode emitters'' was introduced. First, a virtual base class called B::JVM::Emit was created. All code that must emit Java bytecode uses the interface provided by B::JVM::Emit, and all subclasses of B::JVM::Emit must provide implementations of B::JVM::Emit's interface specific to that given assembler syntax.

As an example, consider the code in Figure 4.1. It creates a JVM class called Foo, with one static public method, main, whose body has a single JVM dup instruction.

Figure: Example of B::JVM::Emit Interface
my $emit = new B::JVM::Jasmin::Emit("Foo");
$emit->methodStart("main([Ljava/lang/String;)V", "static public");

If a standard assembler format for the JVM is ever created, one needs only implement B::JVM::StandardAssembler::Emit as a subclass of B::JVM::Emit, and change the first line in Figure 4.1 to:

my $emit = new B::JVM::StandardAssembler::Emit("Foo");

Assuming that B::JVM::StandardAssembler::Emit is implemented properly, the rest of the code will function properly, generating the Foo class as described.

Thus, creation of this abstract base class mitigated the concern that Jasmin assembler syntax is not standard. Jasmin could be used, via the abstract interface, without worry that it might be outdated and replaced eventually4.1.

Next: Data Type Support Up: First Approach Direct Compilation Previous: First Approach Direct Compilation   Contents

Copyright © 2000, 2001 Bradley M. Kuhn.

Verbatim copying and distribution of this entire thesis is permitted in any medium, provided this notice is preserved.