为什么clang 优化级别为-O0时,rsp会减0x10?
mac默认的clang 优化级别是-O0,默认情况下的编译结果中,当调用函数时rsp这个栈寄存器都会减0x10. 这会导致栈浪费了16个字节
这么傻瓜的编译结果让我非常纳闷,其背后是不是有什么个中奥秘?
源码:
#include <stdio.h>
int bar(int c, int d) {
int i = c + d ;
return i;
}
int main(void) {
return bar(2, 3);
}
编译结果为:
> clang -O0 -g c.c -o c.out ;
> otool -tV c.out
_bar:
0000000100000f50 pushq %rbp
0000000100000f51 movq %rsp, %rbp
0000000100000f54 movl %edi, -0x4(%rbp)
0000000100000f57 movl %esi, -0x8(%rbp)
0000000100000f5a movl -0x4(%rbp), %esi
0000000100000f5d addl -0x8(%rbp), %esi
0000000100000f60 movl %esi, -0xc(%rbp)
0000000100000f63 movl -0xc(%rbp), %eax
0000000100000f66 popq %rbp
0000000100000f67 ret
0000000100000f68 nopl (%rax,%rax)
_main:
0000000100000f70 pushq %rbp
0000000100000f71 movq %rsp, %rbp
0000000100000f74 subq $0x10, %rsp
0000000100000f78 movl $0x2, %edi
0000000100000f7d movl $0x3, %esi
0000000100000f82 movl $0x0, -0x4(%rbp)
0000000100000f89 callq _bar
0000000100000f8e addq $0x10, %rsp
0000000100000f92 popq %rbp
0000000100000f93 ret
葬颜·离字凄
10 years, 2 months ago