2 つの単純な C ソース ファイルがあります。1 つ目は mainswap.c です。
void swap(int *x, int *y);
int main()
{
int a, b;
a = 5;
b = 44;
swap(&a, &b);
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
もう 1 つは mainfoobar.c です。
int bar(int x, int y)
{
int z = x + y;
return z;
}
int foo(int a, int b)
{
return bar(a, b);
}
int main(void)
{
foo(2, 3);
return 0;
}
両方のリロケータブル オブジェクト ファイルを取得しました。main
そして、gcc は関数inのスタック フレームの位置合わせについて何かを行うことがわかりましたが、gcc は関数inmainswap.c
に対して明示的には行いません。main
mainfoobar.c
mainswap.c のメイン:
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movl $5, 24(%esp)
movl $44, 28(%esp)
leal 28(%esp), %eax
movl %eax, 4(%esp)
leal 24(%esp), %eax
movl %eax, (%esp)
call swap
movl $0, %eax
leave
ret
mainfoobar.c のメイン:
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl $3, 4(%esp)
movl $2, (%esp)
call foo
movl $0, %eax
leave
ret
andl $-16, %esp
との意図はわかるsubl $32, %esp
。同じ gcc バージョン、同じオプション、同じコンピューター、唯一の違いは C ソース ファイルです。私の質問は、gcc が 2 つの主要な関数を異なる方法で扱う理由です。この現象の背後にあるものは何ですか?</p>
また、使用した gcc のバージョンは次のとおりです。
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright © 2011 Free Software Foundation, Inc.
ところで、main
mainswap.c の関数では、アラインメントと使用上の要求も満たすと思いますがsubl $16, %esp
、なぜ gcc は 16 バイトを浪費するのですか?</p>