0

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;
Doj
  • 1,244
  • 6
  • 13
  • 19
  • 1
    As a matter of interest, why are you asking about objects, which are years out of date, and not the more modern classes? – MartynA May 18 '20 at 18:03
  • @MartynA I just need to understend how VMT works based on Pascal language. – Aleksandre Khachaturov May 18 '20 at 18:59
  • Although it is about Delphi (on which FreePascal is based), you might find [this article](https://stackoverflow.com/questions/760513/where-can-i-find-information-on-the-structure-of-the-delphi-vmt) helpful, in particular the answer by Allen Bauer. Until quite recently he was the Chief Scientist at Borland/Embarcadero in charge of Delphi's development. – MartynA May 18 '20 at 19:11

1 Answers1

0

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.

Doj
  • 1,244
  • 6
  • 13
  • 19