How can I see VMT in pascal?

149 views Asked by At

Can I see somehow an VMT table in FREE Pascal? I am interested if VMT table has the same number of items in two objects that are connected by heredity?

For example in this model, what will be in the VMT table ?

And will there be ONE table for all functions or more (table between [Ob1 AND Ob2] and table between [Ob2 AND Ob3] )?

What will be in the table(s)?

Ob1 = object
  constructor Init;
  function f1..; virtual;
  function f2..; virtual;
end;


Ob2 = object(Ob1)
  constructor Init;
  function f1...; virtual;
  function f2...; virtual;
  function f3...; virtual;
end;

Ob3 = object(Ob2)
  constructor Init;
  function f1...; virtual;
  function f2...; virtual;
  function f3...; virtual;
end;
1

There are 1 answers

0
Doj On

Can I see somehow an VMT table in FREE Pascal?

In runtime you can get a pointer to VMT of an object instance with TypeOf intrinsic (like this: TypeOf(Obj)). Internal structure of the returned VMT is documented in 8.2.12 Object types of the Free Pascal Programmer’s Guide.

You can also dump VMTs while compilation. To do that compile your program with -al option ("List sourcecode lines in assembler file") and read lines in the generated .s file related to the VMTs. For your example I got this on my PC (Win32 for i386 target):

.section .data.n_VMT_$P$PROGRAM_$$_OB1,"d"
    .balign 4
.globl  VMT_$P$PROGRAM_$$_OB1
VMT_$P$PROGRAM_$$_OB1:
    .long   4,-4,0
    .long   P$PROGRAM$_$OB1_$__$$_F1$$LONGINT
    .long   P$PROGRAM$_$OB1_$__$$_F2$$LONGINT
    .long   0

.section .data.n_VMT_$P$PROGRAM_$$_OB2,"d"
    .balign 4
.globl  VMT_$P$PROGRAM_$$_OB2
VMT_$P$PROGRAM_$$_OB2:
    .long   4,-4
    .long   VMT_$P$PROGRAM_$$_OB1
    .long   P$PROGRAM$_$OB2_$__$$_F1$$LONGINT
    .long   P$PROGRAM$_$OB2_$__$$_F2$$LONGINT
    .long   P$PROGRAM$_$OB2_$__$$_F3$$LONGINT
    .long   0

.section .data.n_VMT_$P$PROGRAM_$$_OB3,"d"
    .balign 4
.globl  VMT_$P$PROGRAM_$$_OB3
VMT_$P$PROGRAM_$$_OB3:
    .long   4,-4
    .long   VMT_$P$PROGRAM_$$_OB2
    .long   P$PROGRAM$_$OB3_$__$$_F1$$LONGINT
    .long   P$PROGRAM$_$OB3_$__$$_F2$$LONGINT
    .long   P$PROGRAM$_$OB3_$__$$_F3$$LONGINT
    .long   0

Here you can clearly see that virtual methods are started from fourth cells in the VMTs. First cell of an VMT is size of object, third is the pointer to parent's VMT. The -4s are negative sizes of objects and used for validating pointers to VMT.

And will there be ONE table for all functions or more (table between [Ob1 AND Ob2] and table between [Ob2 AND Ob3] )?

One VMT for each object type. There is no tables between objects, VMTs are attached to objects itself.