class B {
public:
int b1;
int b2;
virtual void f1 () { ... }
virtual void f2 () { ... }
};
class D : public B {
public:
int d1;
int d2;
virtual void f2 () { ... }
virtual void f3 () { ... }
};
D *d = new D ();
d->b1 = 2948;
d->f1 ();
d->f2 ();
d->f3 ();
B *b = d;
b->f1 ();
b->f2 ();
|
|
; d->b1 = 2948;
movq -24(%rbp), %rax
movl $2948, 8(%rax)
; d->f1();
movq -24(%rbp), %rdx ; load d into rdx
movq (%rdx), %rdx ; get vtable, at 0 bytes off of rdx
movq (%rdx), %rdx ; get method f1, at 0 bytes off of vtable
movq -24(%rbp), %rax ; load d into rax
movq %rax, %rdi ; store "this"
call *%rdx ; invoke f1
; d->f2()
movq -24(%rbp), %rax ; load d into rdx
movq (%rax), %rax ; get vtable
addq $8, %rax ; f2 offset 8 bytes off of vtable
movq (%rax), %rdx ; get method f2
movq -24(%rbp), %rax
movq %rax, %rdi ; store "this"
call *%rdx
movq -24(%rbp), %rax ; load d into rdx
movq (%rax), %rax ; get vtable
addq $16, %rax ; f3 offset
movq (%rax), %rdx ; get method f3
movq -24(%rbp), %rax
movq %rax, %rdi ; store "this"
call *%rdx ; invoke f3
; B *b = d;
movq -24(%rbp), %rax
movq %rax, -32(%rbp)
; b->f1();
movq -32(%rbp), %rax ; load b into rax
movq (%rax), %rax ; get vtable
movq (%rax), %rdx ; get method f1
movq -32(%rbp), %rax
movq %rax, %rdi ; store "this"
call *%rdx ; invoke f1
; b->f2();
movq -32(%rbp), %rax ; load b into rax
movq (%rax), %rax ; get vtable
addq $8, %rax ; f2 offset
movq (%rax), %rdx ; get method f2
movq -32(%rbp), %rax
movq %rax, %rdi ; store "this"
call *%rdx ; invoke f2
|
|
|
Layout of Stack
|
|
Offset
|
Contents
|
|
-24(%rbp)
|
local variable D *d
|
|
-32(%rbp)
|
local variable B *b
|
|
Layout of Instance of D
|
|
Offset
|
Name
|
Contents
|
| 20 | d2 | 3293 |
| 16 | d1 | 9032 |
| 12 | b2 | 8432 |
| 8 | b1 | 2948 |
| 0 | vtable | address of vtable of class D |
|
Layout of Vtable of Class D
|
|
Offset
|
Name
|
Contents
|
| 16 | f3 | address of D.f3 code |
| 8 | f2 | address of D.f2 code |
| 0 | f1 | address of B.f1 code |
|