next up previous contents
Next: Declarations Up: The Empty Program Previous: main

Fit it in the Compiler

To put the initial entries in the constant_pool[] I made an initialization function that creates the list of cp_entry structures and returns a pointer to the first entry so it can be assigned to the global variable cp.

The function has one parameter, the name of the Pascal program. It creates the following entries, plus the entries they need, like CONSTANT_Utf8_info entries containing the names of the classes, methods, descriptors, etc.:

These entries will always be on fixed positions, which is easy when it is needed to refer to them by their index. The three attributes ConstantValue, Code and Exceptions are the only attributes that will be used by the compiler, the attributes LineNumberTable. LocalVariableTable and SourceFile are optional attributes and therefore will be forgotten for the moment.

To put the <init> method in the methods[] table, I also made a initialization function that creates the list of m_f structures and returns a pointer to the first entry so it can be assigned to the global variable m. Only the <init> method is put in the methods[] table by this function, the entry for the main method will follow later while the parser runs.

Because there are no variables or constants in an empty class file, the fields[] table stays empty for now. Just to initialize the global variable f, the pointer to the first entry of the fields[] list, I made an initialization function that creates an empty m_f structure as the first entry of the list and returns its address.

These three functions are called right after the program identifier has been parsed. The main method is inserted in the methods[] table after the semicolon following the program heading has been read. This is done by a function that returns a pointer to the m_f structure containing the main method. This way, when there is code that has to be added to the method, you do not have to search for the right entry in the methods[] table m. This means that you do have to pass the pointer around in the parser to every procedure that might need it.

The return instruction in the main method is inserted in the code[] list of the m_f structure of the main method when the final keyword end is read, and it is certain there can not be any code left in the Pascal source program.

When the parser returns and no errors have been found, the class file is written and the memory holding the structures is released.

Now we have compiled an empty Pascal program to a Java class file. This provides us with a solid basis to which we can add declarations and statements. The next step will be to compile a Pascal program containing variable and constant declarations.



next up previous contents
Next: Declarations Up: The Empty Program Previous: main



mark@bottom.xs4all.nl