C编译器如何在ASM中处理函数的结构返回值 [英] how c compiler treats a struct return value from a function, in ASM

查看:161
本文介绍了C编译器如何在ASM中处理函数的结构返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当谈到C函数的返回值时,返回值存储在EAX寄存器中.假设我们正在谈论32位寄存器,欢迎使用整数,但是当我们返回这些类型时会发生什么: long longlong double,大于32位的struct/union.

解决方案

在常见的x86调用约定中,适合两个寄存器的对象在RDX:RAX中返回.这是同一对寄存器,是div和mul指令以及cdq/cqo(符号将e/rax扩展为e/rdx)的隐式输入/输出.

i386 Linux(SysV)调用约定仅以这种方式返回64位整数.结构(甚至是由单个int32_t组成的结构)都使用隐藏参数方法,而不是打包为eaxedx:eax. 64位Linux和Microsoft当前的标准__vectorcall都将结构打包到e/raxe/rdx:e/rax中.

许多调用约定通过添加隐藏的额外参数来处理较大的对象:指向用于存储返回值的空间的指针.有关所使用的特定ABI,请查阅ABI文档. ( Wiki的问题).

与注释中讨论的其他调用约定相比(例如,隐式使用堆栈上的空间来存储要返回的大对象),传递指针可以保存副本,因为指针可以指向最终目标而不是指针上的临时空间.堆栈.

When speaking about C function's return value, the return value is stored in the EAX register. Suppose we are speaking about 32 bit register, integers are welcomed, but what happens when we return these types: long long, long double, a struct/union that is larger than 32bit.

解决方案

In common x86 calling conventions, objects that fit in two registers are returned in RDX:RAX. This is the same register pair that is an implicit input/output for div and mul instructions, and for cdq / cqo (sign extend e/rax into e/rdx).

The i386 Linux (SysV) calling convention only returns 64bit integers that way. Structs (even a struct consisting of a single int32_t) use the hidden-parameter method instead of being packed eax or edx:eax. 64bit Linux, and Microsoft's current standard __vectorcall, both pack structs into e/rax, or e/rdx:e/rax.

Many calling conventions handle larger objects by adding a hidden extra parameter: a pointer to space for storing the return value. Consult the ABI documentation for the specific ABI you are using. (links at in the wiki).

Compared to other calling conventions discussed in comments (e.g. implicitly using space on the stack to store large objects being returned), passing a pointer can save a copy, because the pointer can point to the final destination instead of scratch space on the stack.

这篇关于C编译器如何在ASM中处理函数的结构返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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