Everything
9.1.3 Rules Concerning Setting and Referencing Parameters

General rules concerning parameters and the method for allocating parameters are described.

Refer to section 8.2.5, Examples of Parameter Allocation, for details on how to actually allocate parameters.

(1)

Passing Parameters

A function is called after parameters have been copied to a parameter area in registers or on the stack. Since the caller does not reference the parameter area after control returns to it, the caller is not affected even if the callee modifies the parameters.

(2)

Rules on Type Conversion

(a)

Parameters whose types are declared by a prototype declaration are converted to the declared types.

(b)

Parameters whose types are not declared by a prototype declaration are converted according to the following rules.

-

int type of 2 bytes or less is converted to a 4-byte int type.

-

float type parameters are converted to double type parameters.

-

Types other than the above are not converted.

Example

(3)

Parameter Area Allocation

Parameters are allocated to registers or to a parameter area on the stack. Figure 3.3 shows the parameter-allocated areas.

Following the order of their declaration in the source program, parameters are normally allocated to the registers starting with the smallest numbered register. After parameters have been allocated to all registers, parameters are allocated to the stack. However, in some cases, such as a function with variable-number parameters, parameters are allocated to the stack even though there are empty registers left. The this pointer to a nonstatic function member in a C++ program is always assigned to R1.

Table 3.28 lists general rules on parameter area allocation.

Figure 9.2

Parameter Area Allocation

Table 9.2

General Rules on Parameter Area Allocation

Parameters Allocated to Registers

Parameters Allocated to Stack

Target Type

Parameter Storage Registers

Allocation Method

signed char, (unsigned) char, bool, _Bool, (signed) short, unsigned short, (signed) int, unsigned int, (signed) long, unsigned long, float, double*1, long double*1, pointer, pointer to a data member, and reference

One register among R1 to R4

Sign extension is performed for signed char or (signed) short type, and zero extension is performed for (unsigned) char type, and the results are allocated.

All other types are allocated without any extension performed.

(1) Parameters whose types are other than target types for register passing

(2) Parameters of a function which has been declared by a prototype declaration to have variable-number parameters*3

(3) When the number of registers not yet allocated with parameters among R1 to R4 is smaller than the number of registers needed to allocate parameters

(signed) long long, unsigned long long, double*2, and long double*2

Two registers among R1 to R4

The lower four bytes are allocated to the smaller numbered register and the upper four bytes are allocated to the larger numbered register.

Structure, union, or class whose size is a multiple of 4 not greater than 16 bytes

Among R1 to R4, a number of registers obtained by dividing the size by 4

From the beginning of the memory image, parameters are allocated in 4-byte units to the registers starting with the smallest numbered register.

 

 

 

 

Notes 1.

When dbl_size=8 is not specified.

Notes 2.

When dbl_size=8 is specified.

Notes 3.

If a function has been declared to have variable parameters by a prototype declaration, parameters which do not have a corresponding type in the declaration and the immediately preceding parameter are allocated to the stack. For parameters which do not have a corresponding type, an integer of 2 bytes or less is converted to long type and float type is converted to double type so that all parameters will be handled with a boundary alignment number of 4.

Example

int f2(int,int,int,int,...);

:

f2(a,b,c,x,y,z); ? x, y, and z are allocated to the stack.

(4)

Allocation Method for Parameters Allocated to the Stack

The address and allocation method to the stack for the parameters that are shown in table 3.28 as parameters allocated to the stack are as follows:

-

Each parameter is placed at an address matching its boundary alignment number.

-

Parameters are stored in the parameter area on the stack in a manner so that the leftmost parameter in the parameter sequence will be located at the deep end of the stack. To be more specific, when parameter A and its right-hand parameter B are both allocated to the stack, the address of parameter B is calculated by adding the occupation size of parameter A to the address of parameter A and then aligning the address to the boundary alignment number of parameter B.