next up previous contents
Next: Variables and Constants Up: Variable and Constant Previous: Variable and Constant

Variables and Constants as ``Normal'' Java Fields

 

Each compiled global Java variable and constant has an entry in the fields[] table of the class file. This entry contains a field representing the access flags of the variable or constant, where one of the flags of a constant should be the ACC_FINAL flag. There is also a field containing the index in the constant_pool[] of the CONSTANT_Utf8_entry representing the name of the variable or constant and a field containing the index of the CONSTANT_Utf8_entry representing the descriptor of the variable or constant. Constants also have a ConstantValue attribute, containing the index in the constant_pool[] of an entry representing the value of the constant. Variables have no attribute.

Each global variable or constant also has a CONSTANT_Fieldref_info entry in the constant_pool[]. Besides the index of the CONSTANT_Class_info entry of the class it is declared in, this entry also contains the index of the CONSTANT_NameAndType_info entry that holds the indexes of the CONSTANT_Utf8_info entries containing the name and descriptor of the field.

Compiled local variables and constants only have an index into the local variable table. The only way to access them is by this index.

At first I thought that compiling all Pascal variables and constants into normal Java fields was the way to go. I compiled the variables as Java fields of the appropriate type and with access flags ACC_PUBLIC and ACC_STATIC as if they were declared as Java fields being public and static. The constants were also compiled as normal Java fields of the appropriate type with the same access flags as a variable plus the flag ACC_FINAL and with a ConstantValue attribute representing the value of the constant.

The variables and constants have to be declared static because the main method also has to be declared static, and you have to be able to access the fields and methods from the main method. The main method probably has to be declared static because there can be only one main, so it is a class method and the fields and methods it can access should also be class fields and methods.

I compiled local variables and constants the same way as the global ones, which also meant making them global. I did this because Pascal has nested procedures and functions and Java does not. This means that if you have a nested procedure or function you have to make this procedure or function global, it is simply the only way. This nested procedure or function still has to be able to access the local variables and constants of the procedure or function it is declared in. One way to do this is to make these local variables and constants also global. The same goes for parameters of procedures and functions, which in effect are local variables.

When you make a local variable or constant global, you have the problem that a global variable or constant with the same name can already exist. This means you have to make the name of the local variable or constant unique. I wanted to do this by prefixing the identifier with a unique number, simply a counter that is incremented each time an identifier is prefixed. This should create a unique identifier because identifiers in Pascal are not allowed to begin with a digit. Identifiers in Java are not allowed to start with a digit either. Surprisingly enough, this is also the case in a class file, where there is a very clear distinction between what is an identifier and what is not. To solve this, I prefixed the digit with the string `` $_''.

Now that I had solved this problem and it all seemed to work perfectly, I thought about procedure and function declarations. This was when I thought of a problem in my variable and constant declarations theory. The problem was the following: Procedures and functions in Pascal can have var parameters (call-by-reference parameters) and in Java there are only call-by-value parameters. The problem lies in the fact that when you declare the variables as regular Java fields, there is no reference to pass as actual parameter for a procedure call when the corresponding formal parameter of the procedure is call-by-reference, as Java fields do not have a reference.

The only way to solve this problem is to compile the variable declarations in such a way that you do have a reference. In Java the only references are objectreferences and arrayreference (besides the references to interfaces that are clearly not relevant here). This leaves us with two options: Compile the variables so that they are objects or so that they are arrays. These two options will be discussed in the next subsection.



next up previous contents
Next: Variables and Constants Up: Variable and Constant Previous: Variable and Constant



mark@bottom.xs4all.nl