next up previous contents
Next: Pointers Up: Things Left To Previous: Things Left To

Records

Records in Pascal can be compiled as if they are separate classes in Java. For each recordtype defined in a type declaration of a Pascal program, a class has to be created that contains the fields of the record as instance variables.

Making a class also includes writing it to a class file, so the same procedures and structures in the compiler that are used for the class file containing the program can be used for a class file containing a definition of a record, except that the fields in a class defining a record must not be static. The fields in the class containing the Pascal program are declared static because they can not be accessed by the main method if they are not. This is because the main method has to be declared static (see also subsection 9.1.1).

The fields in a class defining a record must not be declared static. This way, every time a variable is declared to be of the recordtype, an instance of the class can be created to represent the variable. This way variables of recordtypes become objects of the class defining the record.

Besides creating a class and writing the class file for every recordtype when the type is declared, entries for the class and its fields have to be created in the constant_pool[] of the class file that contains the program. The following entries have to be created for every record class:

With these entries it is possible to access the fields in an instance of the class. For example, the bytecode to store the value 5 in an integer field of a local variable of a recordtype is now:

 
 aload var_index			 load the  objectref on the stack;

bipush 5 load int value 5 on the stack;

putfield cp_index store the value in the field of the record.

The cp_index is the index in the constant_pool[] that describes the field of the record.

To load a value from a field of a variable of a recordtype is:

 
 aload var_index			 load the  objectref on the stack;

getfield cp_index load the value in the field of the record on the stack.

As before with the variables of primitive types that are actually references to arrays of one element that hold the value, variables of recordtypes are now references to objects that hold the fields of the record.



next up previous contents
Next: Pointers Up: Things Left To Previous: Things Left To



mark@bottom.xs4all.nl