Compiler-generated data and data structures
The compiler generates several data when compiling a program, this article describes its layout.
One of the most important data in an object pascal program are classes. A class is a pointer to the actucal data of a given instance. The first entry in the data of an object is a pointer to its virtual method table (VMT).
This name is historic because this table contains nowadays more information than only the pointers to the virtual methods. The layout of the VMT generated by FPC (Aug. 2006) is shown in the following table.
|sizeof(sizeint)||This field contains the negative instance size so by adding this field to the field before and checking the result allows to validate a vmt.|
|vmtParent||sizeof(sizeint)*2||Pointer to the vmt of the parent. nil in case of TObject.|
|vmtClassName||vmtParent+sizeof(pointer)||Pointer to a zero terminated string containing the name of the class.|
|vmtMethodStart||vmtParent+sizeof(pointer)*10||At this offset, the real vmt starts, this is the offset of the first virtual method.|
|last field||The last field has the value zero so the vmt size can be determined.|
Instance data layout
After the pointer to the vmt, the fields of the class follow. Furthermore, the vmts of the implemented interfaces are stored in the instance data. If an interface to a class instance is queried, the pointer to the vmt of this interface is returned. Later, when a method of the interface is invoked, the class instance can be determined from the vmt because it's part of the instance data simply by subtracting a fixed offset from the interface's vmt address.