a Cross Development Platform for Java

User Tools

Site Tools


Type Descriptor

Each class needs a type descriptor in the constant block of this class (see Linker32). This type descriptor will be needed to find instance methods or to do type checking. All arrays - even arrays with elements being of an interface type - need a type descriptor. Interfaces themselves need no type descriptor because there cannot be instances of interfaces.
However, there is one exeption to this. If an object, which is of type “array of interface type”, is checked with instructions like instanceof or checkcast, then the type descriptor of the array must have an entry componentTD (see below). And this entry must be a type descriptor of an interface. For such cases even an interface needs a type descriptor. This type descriptor can be very small and most entries may be ommitted. When parsing the classes, affected interfaces are inserted in a special list.


Type descriptor for arrays and classes

The fields size indicates the size (in bytes) of an object of this type (without tag, see Objects). For arrays the entry is arrayComponentSize, which is the size (in bytes) of one array element. The name of the class could be used for reflection.

The entry extensionLevel gives the number of superclasses. The method table is discussed below. The type table (baseClassTD[0] up to baseClassTD[extLevel]) and interface type table (interface ChkId1, …) is used for Type Checking. The interface method table together with the interfaceDelegateMethod and the entries with interface0Id, interface0MethodOffset serves to find the right interface method, see Interfaces.
The garbage collector has to know which instance fields are references. For this there is a field instPtrOffset. This field has a fixed offset within the type descriptor. From there you can reach a list with all reference fields (nofInstPtr, instPtrOffset[0], … ).

Type descriptors for arrays are solely used for type checking. They contain the references to all type descritors of the lower dimensional arrays of the same base type. If necessary, these lower dimensional types must be created by the class file reader. The field arrayDimension gives the number of dimension of this array. The bit P (primitive) indicates whether this is an array of base types, such as byte, short, … (P = 1) or if its an array of regular classes (P = 0). For arrays of base types the value of componentTD is set to 0. The P-bit is also set in the higher dimensional type descriptors, e.g. in [S, [[S and [[[S.

The reduced type descriptor for interfaces comprises the fields from size to intfTypeChkOffset and the interface table. The type table with the base classes can be omitted. Important: the table with the interface id's must include the own id!

Instance Methods

The method table within the type descriptor contains the addresses of all instance methods of this class starting with the methods of the superclass (if present). The compiler translates the instruction invokevirtual into:

  • The reference to the object is put in a register.
  • With this reference the tag of the object on the heap is read. This tag points to the type descritor of the dynamic type of this object.
  • The Tag points to the field size of the type descriptor. The compiler calculates the offset into the method table. This is possible, because all the methods of a certain type are known at compile time.
  • With this offset the address of the wanted method can be fetched from the table.

For this to work, it is important, that methods which are overridden, are placed exactly at the same offset in the table as their overriding method.

Examples of Type Descriptors

For all examples below test cases have been created for the compiler.

Example A

The structure of the type descriptor is shown. The order of methods of a class must be maintained in all its subclasses! Standard order is: first constructors then normal methods.

Example A of type descriptor

Please note:

  • Class CXexA implements just one interface with a single method. At the place of the interface delegation method the address of this method can be put directly, see Interfaces.
  • The class file reader numbers all interfaces with methods, which are called by the instruction invokeinterface (id). Interfaces without such methods have id = -1.
  • For efficient searching, the interfaces with id != -1 are listed for each class sorted with id. The last interface in the list must be id = 0. This ensures a simple termination of the search.
  • mo: means Method Offset
  • imo: means Interface Method Offset
  • bmo: means Base Method Offset

Example B

This example extends example A and shows what happens, if a method with identical signature is present in several interfaces.

Example B of type descriptor

Example C

This example shows an interface having several superinterfaces, which are themselves extensions.

Example C of type descriptor

Example D

A further test case.

Example D of type descriptor
dev/crosscompiler/type_descriptor.txt · Last modified: 2019/07/11 19:12 by ursgraf