为什么 GCC 使用 mov 而不是 push in 函数调用? [英] Why Is GCC Using Mov Instead Of Push In Function Calls?

查看:32
本文介绍了为什么 GCC 使用 mov 而不是 push in 函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个示例 C 程序.

int 崇拜(long john){返回 0 * 约翰;}主函数(){回敬拜(666);}

程序集看起来(基本上)是这样的:

崇拜(长):pushq %rbpmovq %rsp, %rbpmovq %rdi, -8(%rbp)移动 $0, %eax流行音乐%rbpret主要的:pushq %rbpmovq %rsp, %rbp移动 $666, %edi召唤崇拜(长)流行音乐%rbpret

我在阅读堆栈粉碎时遇到了这个问题.在汇编 worship(long): 部分中,它说 movq %rdi, -8(%rbp) 我希望它使用 pushq 基于我目前阅读的所有内容.这是 GCC 将参数推送到堆栈上的新方式吗?如果是,我可以使用编译器标志来切换它吗?

解决方案

GCC 手册 说,

-mpush-args

<块引用>

推送指令将用于在调用函数时传递传出参数.默认启用.

-mno-push-args

<块引用>

使用 PUSH 操作来存储传出参数.这种方法更短,通常与使用 SUB/MOV 操作的方法一样快,并且默认启用.在某些情况下,禁用它可能会因为改进调度而提高性能并减少依赖.

-maccumulate-outgoing-args

<块引用>

如果启用,传出参数所需的最大空间量将在函数序言中计算.这在大多数现代 CPU 上更快,因为当首选堆栈边界不等于 2 时,依赖减少、调度改进和堆栈使用减少.缺点是代码大小显着增加.此开关意味着 -mno-push-args.

即使 -mpush-args 默认启用,它也会被默认启用的 -maccumulate-outgoing-args 覆盖.显式编译传递选项 -mno-accumulate-outgoing-args 可以将指令更改为 push.

So I've got this example C program.

int worship(long john)
{
    return 0 * john;
}

int main()
{
    return worship(666);
}

The assembly looks (essentially) like this:

worship(long):
    pushq   %rbp
    movq    %rsp, %rbp
    movq    %rdi, -8(%rbp)
    movl    $0, %eax
    popq    %rbp
    ret
main:
    pushq   %rbp
    movq    %rsp, %rbp
    movl    $666, %edi
    call    worship(long)
    popq    %rbp
    ret

I ran into this while reading about stack smashing. In the assembly worship(long): section where it says movq %rdi, -8(%rbp) I would expect it to be using pushq based on everything I've read so far. Is this the new way that GCC is pushing arguments onto the stack and if so is there a compiler flag I could be using to toggle this?

解决方案

GCC manual says,

-mpush-args

Push instructions will be used to pass outgoing arguments when functions are called. Enabled by default.

-mno-push-args   

Use PUSH operations to store outgoing parameters. This method is shorter and usually equally fast as method using SUB/MOV operations and is enabled by default. In some cases disabling it may improve performance because of improved scheduling and reduced dependencies.

-maccumulate-outgoing-args

If enabled, the maximum amount of space required for outgoing arguments will be computed in the function prologue. This is faster on most modern CPUs because of reduced dependencies, improved scheduling and reduced stack usage when preferred stack boundary is not equal to 2. The drawback is a notable increase in code size. This switch implies -mno-push-args.

Even -mpush-args enabled by default it is override by -maccumulate-outgoing-args which is enabled by default. Compiling passing option -mno-accumulate-outgoing-args explicitly could change the instructions to push.

这篇关于为什么 GCC 使用 mov 而不是 push in 函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆