This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
g++ and virtual table
- To: "gcc_help" <gcc-help at gcc dot gnu dot org>
- Subject: g++ and virtual table
- From: "Lada 'Ray' Lostak" <ray at dspaudio dot com>
- Date: Tue, 15 May 2001 19:26:58 +0200
Hi !
Please, can any1 help me with following problem please ? I need to call
C++ object based on pure virtual classes (defined by structures) from
'assembler' basically. It is pretty easy (all parameter conversions, stack,
and similar - it is done and pretty well working)... But:
Let's say I have following project:
#include <new.h>
#include <stdio.h>
// this should be SHARED interface - acros BINARY
// files compiled in MORE compilers
typedef struct base {
virtual void uch()=0;
} base;
// some 'end' object supporting 'base'
class object:public base {
public:
object();
~object();
virtual void uch();
protected:
int x,y; // just something
};
object::object() {
printf("object\n");
}
object::~object() {
printf("~object\n");
}
void object::uch() {
printf("uch\n");
}
void main() {
object* o;
base* b;
o=new object();
b=o;
// let's call it - this is my interest
b->uch();
delete o;
}
Nothing special. Now let's look on generated code of line 'b=0' and
'b->uch()'
latest g++ release, without optimalizations (disasm, I do not like listing):
b=0
000009EC:i8B45FC mov eax,[ebp-04]
000009EF:i8945F8 mov [ebp-08],eax
Yes, normal...
b->uch()
000009F7:i8B55F8 mov edx,[ebp-08]
000009FA:i8B02 mov eax,[edx]
000009FC:i83C008 add (d) eax,+08
000009FF:i8B55F8 mov edx,[ebp-08]
00000A02:i52 push edx
00000A03:i8B30 mov esi,[eax]
00000A05:iFFD6 call (d) esi
00000A07:i83C404 add (d) esp,+04
[ebp-08] - instance of b...
eax,[edx] - loads virtual table base...
esi,[eax] - loads adress of method 'uch'...
With optimalizations it looks like:
[ebx is instance from previous code]
0000095A:i8B03 mov eax,[ebx]
0000095C:i53 push ebx
0000095D:i8B4008 mov eax,[eax+08]
00000960:iFFD0 call (d) eax
But I do not understand why compiler "add (d) eax,+08" ? What this mean ?
Why compiler skips 2 methods/pointers ? It is problem for me when compiler
adds to the start own things. Adding '2' VT items makes hard to MIX more
compiler binaries together.
Can anyone help how g++ handles 'Virtual Table' internally ? What are 2
initial items ? Can I tell compiler 'do not put these items here - use
something more clever for own things' :) ?
We are assuming 'our' virtual methods starts at offset 0. Works for all
major compilers we're supporting.
Thanx in advance for any coment, hint, or where to get these infos.
Best regards
Lada 'Ray' Lostak
DSPaudio inc.
http://www.dspaudio.com