- 論壇徽章:
- 0
|
偶爾翻到前面的舊貼,討論函數(shù)返回的問題,看起來(lái)那么多回復(fù)吧,好多都亂亂的不知道在說(shuō)些什么,我就納悶了,為什么有些人不太明白也不去求證一下,就我猜我猜我猜猜呢,結(jié)果簡(jiǎn)單的問題變復(fù)雜了,跑題的也不鮮見...
好,牢騷結(jié)束
問題是關(guān)于函數(shù)返回值,也不一定是返回結(jié)構(gòu)了,按照gcc internal來(lái)分,是分成scalar return和aggregate return,見gcc internals:http://gcc.gnu.org/onlinedocs/gc ... l#Stack-and-Calling
scalar return:通常情況,不用多說(shuō)了
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:返回復(fù)合數(shù)據(jù)結(jié)構(gòu),caller要傳遞返回?cái)?shù)據(jù)存放的地址給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.
就是這樣子,而且按照我的理解和實(shí)驗(yàn)來(lái)看,也沒有什么memcpy拷貝來(lái)拷貝去的情況
稍微再?gòu)?fù)雜化一點(diǎn),gcc對(duì)于后者也就是aggregate return又做了處理,提供了兩個(gè)編譯選項(xiàng),默認(rèn)的-fpcc-struct-return就是按照前面的說(shuō)法做的,-freg-struct-return則是盡量用寄存器來(lái)存放返回值,效率可能高一點(diǎn)點(diǎn),但是可能存在與其他編譯器不兼容的情況。選項(xiàng)的解釋如下:
-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. |
|