首页 > LinuxDev > ANSI C Argument-Passing Mechanism

ANSI C Argument-Passing Mechanism

2011年3月15日

Got from “C User’s Guide” – Appendix A ANSI/ISO C Data Representations

This section describes how arguments are passed in ANSI/ISO C.

All arguments to C functions are passed by value.

Actual arguments are passed in the reverse order from which they are declared in a
function declaration.

Actual arguments which are expressions are evaluated before the function reference.
The result of the expression is then placed in a register or pushed onto the stack.

(SPARC)
Functions return integer results in register %o0, float results in register %f0, and
double results in registers %f0 and %f1.
long long1 integers are passed in registers with the higher word order in %oN, and
the lower order word in %o(N+1). In-register results are returned in %i0 and %i1,
with similar ordering.

All arguments, except doubles and long doubles, are passed as four-byte values.
A double is passed as an eight-byte value. The first six four-byte values (double
counts as 8 ) are passed in registers %o0 through %o5. The rest are passed onto the
stack. Structures are passed by making a copy of the structure and passing a pointer
to the copy. A long double is passed in the same manner as a structure.
Upon return from a function, it is the responsibility of the caller to pop arguments
from the stack. Registers described are as seen by the caller.

(Intel)

Functions return integer results in register %eax.
long long results are returned in registers %edx and %eax. Functions return
float, double, and long double results in register %st(0).

All arguments, except structs, unions, long longs, doubles and long
doubles
, are passed as four-byte values; a long long is passed as an eight-byte
value, a double is passed as an eight-byte value, and a long double is passed as a
12-byte value.

structs and unions are copied onto the stack. The size is rounded up to a
multiple of four bytes. Functions returning structs and unions are passed a
hidden first argument, pointing to the location into which the returned struct or
union
is stored.

Upon return from a function, it is the responsibility of the caller to pop arguments
from the stack, except for the extra argument for struct and union returns that is
popped by the called function.

(PowerPC)

Functions return integer results in register %r3. Float and double results are
returned in %f1.

For float results, the value returned is rounded to float precision. Long long and
structure or union result whose size is 8 bytes or less are returned in registers
%r3 and %r4, with %r3 containing the low-addressed word of the structure as stored
in memory. For other structures or unions and long double results, the caller passes
a pointer to a storage area into which the result is copied in register %r3 as an implicit
first argument.

Arguments are passed in integer registers %r3 through %r10, floating-point registers
%f1 through %f8 and a parameter area on the stack. All arguments except doubles,
long doubles, and long long are passed as four-byte values.

In the description of the argument-passing algorith below, gr is the number of the next
integer register to be used for argument passing, fr is the number of the next floating
point register, and starg is a pointer to the next word in the parameter area.

INITIALIZE:
Set fr=1, gr=3, and starg to the start of the parameter area.

SCAN:
If there are no more arguments, terminate. Otherwise, select one of the following,
depending on the type of the next argument:

DOUBLE_OR_FLOAT:
If fr>8 (that is, there are no more available floating-point registers), go to OTHER.
Otherwise, load the argument value into floating register fr, increment fr, and go
to SCAN.

SIMPLE_ARG:
A SIMPLE_ARG is one of the following:
1. One of the simple integer types no more than 32 bits wide (char, short, int, long, enum), or
2. A pointer to an object of any type, or
3. A structure, union, or long double, is passed as a pointer to a copy of the object made by the caller.

If gr>10, go to OTHER. Otherwise, load the argument value into general register gr, set gr to gr+1,
and go to SCAN. Values shorter than 32 bits are sign extended or zero extended depending on
whether they are signed or unsigned.

LONG_LONG:
If gr>7, go to OTHER. If gr is even, set gr to gr+1. Load the lower-addressed word of the long long
into gr and the higher-addressed word into gr+1, increment gr by 2 and to SCAN.

OTHER:
Arguments not otherwise handled above are passed in the parameter words of the caller’s stack
frame. Integer values shorter than 32 bits are (conceptually) sign or zero extended to 32 bits and
considered to have 4-byte size and alignment; otherwise the size, sz, of the argument is as
determined by the sizeof operator. Round starg up to a multiple of the alignment requirement of
the argument and copy the argument byte-for-byte, beginning with its lowest addressed byte, into
starg, …, starg+sz-1. Set starg to starg+sz, then go to SCAN.

本文地址:
http://www.kgdb.info/c_argument_passing_mechanism/
版权所有 © 转载时必须以链接形式注明作者和原始出处!

分类: LinuxDev 标签:
本文的评论功能被关闭了.