Next: Putting It Together with Up: First Approach Direct Compilation Previous: Using Jasmin Assembler   Contents

Data Type Support

With full access to the perl front-end, the B modules to manipulate the IR, and a code emitter object (as described in the last section) for JVM bytecode, most of the components for a Perl to JVM compiler are in place. However, recall that the IR generated by perl assumes that both a PVM and implementations of Perl's native data types are available. To successfully port Perl to the JVM, the data types that Perl considers native must be available on the JVM.

One approach would be to ``map'' all of Perl's data types onto equivalent data types already available for the JVM. Unfortunately, in most cases, this approach is not possible, since Perl's native data types are so unique. For example, at first glance, it might seem feasible to map Perl's hash onto an object of type java.util.Hashtable. However, Java's hash tables do not understand the concept of tie. Similarly, scalars cannot be mapped onto java.lang.String, since scalars act like numbers when they are supposed to, and Java strings do not. The uniqueness and flexibility of Perl's data types become the headache of the compiler writer who wants to port Perl to an architecture where the native data types are not so unique and flexible.

Thus, for each data type that the PVM considers ``native'', an equivalent class for it must exist on the JVM. Since the Java language easily compiles to the JVM in an idiomatic way, these classes are implemented in Java. Each class provides the interface that Perl expects for the data type, and since the implementation is in Java, the new data type can run on the JVM.

As an example, consider the portion of the class SvBase seen in Figure 4.2. SvBase is analogous to the perl's SvNULL object. SvBase provides a simple interface to a scalar variable. For example, it provides a function to test if a scalar is defined, and a function to undefine a scalar. (More complex scalar operations are implemented by subclasses of SvBase.)

Figure: Portions of the SvBase Class
class SvBase implements Cloneable
    boolean defined;
    SvBase() { undef(); }
    boolean isDefined() { return defined; }
    void undef() { defined = false; }

Perl's data types are thus handled by providing a library of Java classes that are analogous to those expected by the PVM. These Java classes allow PVM operations, expected by the perl front-end, to be more easily mapped onto the JVM.

Next: Putting It Together with Up: First Approach Direct Compilation Previous: Using Jasmin Assembler   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.