- 论坛徽章:
- 0
|
偶尔翻到前面的旧贴,讨论函数返回的问题,看起来那么多回复吧,好多都乱乱的不知道在说些什么,我就纳闷了,为什么有些人不太明白也不去求证一下,就我猜我猜我猜猜呢,结果简单的问题变复杂了,跑题的也不鲜见...
好,牢骚结束
问题是关于函数返回值,也不一定是返回结构了,按照gcc internal来分,是分成scalar return和aggregate return,见gcc internals:http://gcc.gnu.org/onlinedocs/gc ... l#Stack-and-Calling
scalar return:通常情况,不用多说了
http://gcc.gnu.org/onlinedocs/gc ... .html#Scalar-Return
17.10.8 How Scalar Function Values Are Returned
This section discusses the macros that control returning scalars as values—values that can fit in registers. ......
aggregate return:返回复合数据结构,caller要传递返回数据存放的地址给callee
http://gcc.gnu.org/onlinedocs/gccint/Aggregate-Return.html
17.10.9 How Large Values Are Returned
When a function value's mode is BLKmode (and in some other cases), the value is not returned according to TARGET_FUNCTION_VALUE (see Scalar Return). Instead, the caller passes the address of a block of memory in which the value should be stored. This address is called the structure value address.
就是这样子,而且按照我的理解和实验来看,也没有什么memcpy拷贝来拷贝去的情况
稍微再复杂化一点,gcc对于后者也就是aggregate return又做了处理,提供了两个编译选项,默认的-fpcc-struct-return就是按照前面的说法做的,-freg-struct-return则是尽量用寄存器来存放返回值,效率可能高一点点,但是可能存在与其他编译器不兼容的情况。选项的解释如下:
-fpcc-struct-return
Return “short” struct and union values in memory like longer ones, rather than in registers. This convention is less efficient, but it has the advantage of allowing intercallability between GCC-compiled files and files compiled with other compilers, particularly the Portable C Compiler (pcc).
The precise convention for returning structures in memory depends on the target configuration macros.
Short structures and unions are those whose size and alignment match that of some integer type.
Warning: code compiled with the -fpcc-struct-return switch is not binary compatible with code compiled with the -freg-struct-return switch. Use it to conform to a non-default application binary interface.
-freg-struct-return
Return struct and union values in registers when possible. This is more efficient for small structures than -fpcc-struct-return.
If you specify neither -fpcc-struct-return nor -freg-struct-return, GCC defaults to whichever convention is standard for the target. If there is no standard convention, GCC defaults to -fpcc-struct-return, except on targets where GCC is the principal compiler. In those cases, we can choose the standard, and we chose the more efficient register return alternative.
Warning: code compiled with the -freg-struct-return switch is not binary compatible with code compiled with the -fpcc-struct-return switch. Use it to conform to a non-default application binary interface. |
|