Why compiler put so much commands before function call (look at the link below)? As I understand, it should pass only function parameters before call.
struct A{
int c = 5;
void test(unsigned int a){
a++;
c++;
}
};
struct C{
int k =2;
A a;
};
struct D{
int k =2;
C c;
};
struct B{
int k =2;
D d;
};
void test(unsigned int a){
a++;
}
B *b = new B();
A *ae = new A();
int main()
{
int a = 1;
A ai;
B bi;
C ci;
// 2 operations (why not pop/push ?)
// movl -36(%rbp), %eax
// movl %eax, %edi
// call test(unsigned int)
test(a);
// 4 operations (why 4? we pass something else?)
// movl -36(%rbp), %edx
// leaq -48(%rbp), %rax
// movl %edx, %esi
// movq %rax, %rdi
// call A::test(unsigned int)
ai.test(a);
ae->test(a);
// 5 operations before call (what a hell is going here?, why that "addq" ?)
// movl -36(%rbp), %eax
// leaq -32(%rbp), %rdx
// addq $4, %rdx
// movl %eax, %esi
// movq %rdx, %rdi
// call A::test(unsigned int)
ci.a.test(a);
bi.d.c.a.test(a);
b->d.c.a.test(a);
// no matter how long this chain will be - it will always took 5 operations
}
Why when we call class member, it took 4 additional commands to prepare to call? We load object address to register, as well?
And the last case with 5 ops, is just beyond me...
P.S. In the days of my youth, usually, we put function params to stack (push), than read them (pop). Now what, we pass parameters through registers?