为什么编译器在函数调用之前放置了如此多的命令(查看下面的链接)?据我所知,它应该只在调用之前传递函数参数。C++编译器函数调用
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
}
为什么当我们调用类的成员,花了额外的4个命令准备打电话?我们也加载对象地址来注册?
,并用5个OPS最后一种情况,就是超越我...
附:在我年轻的时候,通常我们会把函数参数放在堆栈中(推),而不是读取它们(弹出)。现在,我们通过寄存器传递参数?
64位调用约定访问不使用推。 –
@RaymondChen这是什么?我只与旧的x80 / x86 asm :) – tower120
@Gluttton注册与q(没有)/(无法推)到堆栈?它有没有'pushq'? – tower120