Difference between revisions of "Compiler-generated data and data structures"

From Free Pascal wiki
Jump to navigationJump to search
Line 15: Line 15:
 
| vmtInstanceSize
 
| vmtInstanceSize
 
| 0
 
| 0
|
+
|  
 +
|-
 +
|
 +
| sizeof(ptrint)
 +
| 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
 
| vmtParent
 
| sizeof(ptrint)*2
 
| sizeof(ptrint)*2
|
+
| Pointer to the vmt of the parent. nil in case of TObject.
 
|-
 
|-
 
| vmtClassName
 
| vmtClassName
 
| vmtParent+sizeof(pointer)
 
| vmtParent+sizeof(pointer)
|
+
| Pointer to a zero terminated string containing the name of the class.
 
|-
 
|-
 
| vmtDynamicTable
 
| vmtDynamicTable
Line 49: Line 53:
 
|  
 
|  
 
|-
 
|-
|}
+
| vmtIntfTable
 
+
| vmtParent+sizeof(pointer)*8
      vmtIntfTable            = vmtParent+sizeof(pointer)*8
+
|
 
|-
 
|-
      vmtMsgStrPtr           = vmtParent+sizeof(pointer)*9
+
| vmtMsgStrPtr
 +
| vmtParent+sizeof(pointer)*9
 +
|
 
|-
 
|-
      vmtMethodStart         = vmtParent+sizeof(pointer)*10
+
| vmtMethodStart
 +
| vmtParent+sizeof(pointer)*10
 +
| At this offset, the real vmt starts, this is the offset of the first virtual method.
 
|-
 
|-
      vmtDestroy              = vmtMethodStart;
+
|
 +
| last field
 +
| The last field has the value zero so the vmt size can be determined.
 
|-
 
|-
      vmtNewInstance          = vmtMethodStart+sizeof(pointer);
+
|}
|-
 
      vmtFreeInstance        = vmtMethodStart+sizeof(pointer)*2;
 
|-
 
      vmtSafeCallException    = vmtMethodStart+sizeof(pointer)*3;
 
|-
 
      vmtDefaultHandler      = vmtMethodStart+sizeof(pointer)*4;
 
|-
 
      vmtAfterConstruction    = vmtMethodStart+sizeof(pointer)*5;
 
|-
 
      vmtBeforeDestruction    = vmtMethodStart+sizeof(pointer)*6;
 
|-
 
      vmtDefaultHandlerStr    = vmtMethodStart+sizeof(pointer)*7;
 

Revision as of 22:19, 5 August 2006

Introduction

The compiler generates several data when compiling a program, this article describes its layout.

Classes 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. It contains information about the

VMT layout

The layout of the VMT generated by FPC (Aug. 2006) is shown in the following table.

entry offset explanation
vmtInstanceSize 0
sizeof(ptrint) 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(ptrint)*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.
vmtDynamicTable vmtParent+sizeof(pointer)*2
vmtMethodTable vmtParent+sizeof(pointer)*3
vmtFieldTable vmtParent+sizeof(pointer)*4
vmtTypeInfo vmtParent+sizeof(pointer)*5
vmtInitTable vmtParent+sizeof(pointer)*6
vmtAutoTable vmtParent+sizeof(pointer)*7
vmtIntfTable vmtParent+sizeof(pointer)*8
vmtMsgStrPtr vmtParent+sizeof(pointer)*9
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.