next up previous contents
Next: The Empty Program Up: Starting Out Previous: How to Store

Datastructures to Hold the Class File

When you want to hold a class file in memory, the only fields you actually have to store are the constant_pool[], the fields[] and the methods[]. The rest of the fields are either always the same, for example the magic field, or can be derived from the three stored fields, for example the constant_pool_count field.

The fields[] and the methods[] have basically the same structure, so they can be stored using the same datastructure. This leaves only two datastructures to be designed: One for the constant_pool[] and one for the fields[] and methods[].

For the constant_pool[] I designed a linked list of entries. Each entry, called cp_entry, contains a pointer to the next. In the compiler there is a global variable cp which is a pointer to the first cp_entry in the list.

The datastructures to hold the fields[] and methods[] are also linked lists of entries. Each entry, called m_f can hold a complete description of a field or a method. Each m_f structure contains a pointer to the next. For the fields[] there is a global variable f in the compiler that is a pointer to the first m_f entry of the list of fields. For the methods[] there is a global variable m that is a pointer to the first m_f entry of the list of methods.

The instructions of a method are stored in a list of instruct structures. Each instruct structure contains a pointer to the first entry in the list of operands of that instruction. It also contains the fields idx, that contains the index of the instruction in the code[] of the method, and jmp_label, that is used by the labeling algorithm discussed in subsection 10.6.

When all the code is generated, no errors have been found and the structures in memory are filled, it is time to write the class file to disk. This is done by writing all the necessary fields to a file with the name of the program as the basename followed by the extension .class. So in case of a program called empty, the name of the class file will be empty.class.



next up previous contents
Next: The Empty Program Up: Starting Out Previous: How to Store



mark@bottom.xs4all.nl