- 论坛徽章:
- 1
|
原帖由 scutan 于 2008-6-5 17:57 发表
嗯。我也是看的汇编,是这个结果。
那本<编程卓越之道> 第二卷 16.7节也是这样讲的。
确实是16.7节
原帖由 lipingtababa 于 2008-6-5 17:56 发表
感谢yecheng_110 (我是噩梦) 的引用,不过你贴的文章是讨论代码段指针存储的,而不是返回值存储的.
scutan (冬日夜雨) 的说法,有文献可以佐证吗?
凭记忆抄的 没细看了
16.7 Function Return Values
Most HLLs return function results in one or more CPU registers. Exactly
which register the compiler uses depends on the data type, CPU, and
compiler. For the most part, however, functions return their results in
registers.
On the 80x86, most functions that return ordinal (integer) values
return their function results in the AL, AX, or EAX register. Functions
that return 64-bit values (long long int) generally return the function result
in the EDX:EAX register pair (with EDX containing the HO double word
of the 64-bit value). On 64-bit variants of the 80x86 family, 64-bit compilers
return 64-bit results in the RAX register. On the PowerPC, most compilers
follow the IBM ABI and return 8-, 16-, and 32-bit values in the R3 register.
Compilers for the 32-bit versions of the PowerPC return 64-bit ordinal
values in the R4:R3 register pair (with R4 containing the HO word of the
function result). Presumably, compilers running on 64-bit variants of the
PowerPC can return 64-bit ordinal results directly in R3.
Generally, compilers return floating-point results in one of the CPU’s
(or FPU’s) floating-point registers. On 32-bit variants of the 80x86 CPU family,
most compilers return a floating-point result in the 80-bit ST0 floating-point
register. Although the 64-bit versions of the 80x86 family also provide the same
FPU registers as the 32-bit members, some OSes such as Windows64 typically
use one of the SSE registers (XMM0) to return floating-point values. PowerPC
systems generally return floating-point function results in the F1 floating-point
register. Other CPUs return floating-point results in comparable locations.
Some languages allow a function to return a nonscalar (aggregate)
value. The exact mechanism that compilers use to return large function
return results varies from compiler to compiler. However, a typical solution is
to pass a function the address of some storage where the function can place
the return result. As an example, consider the following short C++ program
whose func function returns a structure object:
The one thing that you should note from these 80x86 and PowerPC
examples is that functions returning large objects often copy the function
result data just prior to returning. This extra copying can take considerable
time, especially if the return result is large. Instead of returning a large
Functions and Procedures 575
structure as a function result, as I’ve done above, it is usually a better
solution to explicitly pass a pointer to some destination storage to a
function that returns a large result and let the function do whatever
copying is necessary. This often saves some time and code. Consider
the following C code that implements this policy:
As you can see, this approach is more efficient because the code doesn’t
have to copy the data twice, once to a local copy of the data and once to the
final destination variable. |
|