JVM Extensions



next up previous
Next: References Up: Parameterized Types for Java Previous: Methods

JVM Extensions

 

This appendix describes the extensions to the Java Virtual Machine that support parametric polymorphism. In the following specification, we include section numbers from the original JVM specification [Sun95b] to indicate where changes are being made.

(1.4) Objects whose type is statically understood to be a parameter type are always stored as 32-bit quantities on the stack and in local variables. The "aload" and "astore" bytecodes are used to access these variables, effectively treating them as references. The types "double" and "long" cannot be used as type parameters at the VM level. The Java compiler will either automatically wrap these data into "Double" and "Long" objects respectively, or automatically instantiate such classes, rewriting the code appropriately. The only exception to this rule is arrays, where longs and doubles are stored in packed form.

(2.1) The class file format is extended to contain the following additional field:

parameter_info parameters;

where "parameter_info" is defined as follows:

parameter_info u2 parameters_count; u2 parameter_names[parameters_count];

u2 where_count; u2 where_clauses[where_count];

The array "parameter_names" contains constant pool indices for the names of the formal parameters. These name are provided for debugging and disassembly purposes, as in Figure 6.

The field "where_clauses" contains constant pool indices for entries of type "CONSTANT_WhereRef", which describe one where clause of a indicated parameter as specified below. Its signature may mention the parameter types.

(2.2) Signature specifications include the following additional options:

M<fullclassname>[<fieldtype>...]

This signature denotes the instantiation of the parameterized class or interface using the types inside the brackets. The number of parameters much match the number of parameters in the class, and the parameter types must have the required methods.

#<parameterindex>;

This signature denotes one of the parameter types of the current class or method. The parameter index is written as an integer in ASCII form.

Note that these new grammar productions inductively preserve the property that the end of a typespec can be determined as you read the characters from left to right.

(2.3.1) For example, the string ``MMutex[[I]'' represents the type "Mutex[int[]]", and inside a class "Set[Object]", the string ``[#0;'' represents an array of "Object" and ``MHashMap[#0;I]'' represents a "HashMap[Object, int]".

(2.3.2) The new constant pool entries "CONSTANT_LargeMethodref" and "CONSTANT_LargeFieldRef" are similar to "CONSTANT_Methodref" and "CONSTANT_FieldRef" except that they take up two constant pool entries. Their format in the class file is exactly the same as "CONSTANT_Methodref". They are used to refer to static methods and fields of parameterized classes.

A "LargeMethodref" must be used if a method is used by a static or non-virtual method call, and the actual implementation of the method is defined in a parameterized class. This condition can be checked by the compiler. For example, if the class signature described by "constant_pool[class_index]" is an entry of type "CONSTANT_Class" that describes an instantiation, and the method is static, then a "LargeMethodref" must be used. However, a "LargeMethodref" is required even when "constant_pool[class_index]" is not an instantiation, but it inherits its implementation of the method from a parameterized class. The extra constant-pool entry is used to find the correct ipool for the static method code.

A "LargeFieldref" must be used for all accesses to a static field of a parameterized class. The extra constant-pool entry contains an ipool index; at that index, the ipool contains a pointer to the static variable storage. This mechanism allows each distinct instantiation of a parameterized class to have its own copy of the static variable.

(2.3.8) "CONSTANT_WhereRef" is a new kind of constant pool entry, used to denote an operation of a parameter type. It corresponds to a where clause.

CONSTANT_WhereRef u1 tag; u2 param_index; u2 name_and_type_index; u2 access_flags;

The tag will have the value "CONSTANT_WhereRef". The "param_index" is the index of the parameter type in the class or the method that calls this parameter operation. The "name_and_type_index" describes the signature and name of the operation, as described in the where clause. It must match the signature in "parameters[param_index].where_clauses[k].methods", above.

The "access_flags" field may only have "ACC_STATIC" set of the various possible flags.

(2.5) The structure "method_info", which describes one method of the current class, is extended to contain the following field:

     parameter_info parameters;

which describes any new parameters that apply within the scope of the method and where clauses on both these parameters and on any existing parameters. The parameters and where clauses of the class also apply to the method. New parameters are indexed sequentially following the indices of the class parameters, so different methods that have their own additional type parameters may use the same indices. However, there is no ambiguity in any given scope about which parameter corresponds to an index. The current prototype does not implement additional method parameters.

(3.15) Two new bytecodes are added to the virtual machine for invoking where-clause operations. New bytecodes are not strictly necessary, since the attributes of the constant pool entry serve to disambiguate the various invocation bytecodes. We added new bytecodes by analogy with existing bytecodes, and our interpreter does have distinct quick bytecodes.

invokewhere

Invoke an operation of a parameter type.

Syntax:

Stack: ..., objectref, [arg1, [arg2 ...]] ...

The index bytes are used to form an index into the constant pool, which must be an entry of type "CONSTANT_WhereRef". This entry is used to determine which code to run for the method, using information in the current object to determine what the parameter type is.

invokestaticwhere

Invoke a static operation of a parameter type.

Syntax:

Stack: ..., objectref, [arg1, [arg2 ...]] ...

The index bytes are used to form an index into the constant pool, which must be an entry of type "CONSTANT_WhereRef". This entry is used to determine which code to run for the method, using information in the current object to determine what the parameter type is.



next up previous
Next: References Up: Parameterized Types for Java Previous: Methods



Andrew C. Myers, Joseph A. Bank, Barbara Liskov
Copyright © 1996 Association for Computing Machinery