4.2.4.3 Inline expansion

The CC-RH allows inline expansion of each function. This section explains how to specify inline expansion.

(1)

Inline Expansion

Inline expansion is used to expand the main body of a function at a location where the function is called. This decreases the overhead of function call and increases the possibility of optimization. As a result, the execution speed can be increased.

If inline expansion is executed, however, the object size increases.

Specify the function to be expanded inline using the #pragma inline directive.

#pragma inline [(]function-name[, function-name, ...][)]

 

Describe functions that are described in the C language. In the case of a function, "void func1() {}", specify "func1". Two or more function names can be specified with each delimited by "," (comma).

#pragma inline  func1, func2
void    func1() {...}
void    func2() {...}
void func(void) {
        func1();    /*function subject to inline expansion*/
        func2();    /*function subject to inline expansion*/
}

(2)

Conditions of inline expansion

At least the following conditions must be satisfied for inline expansion of a function specified using the #pragma inline directive.

Inline expansion may not be executed even if the following conditions are satisfied, because of the internal processing of the CC-RH.

(a)

A function that expands inline and a function that is expanded inline are described in the same file

A function that expands inline and a function that is expanded inline, i.e., a function call and a function definition must be in the same file. This means that a function described in another C source cannot be expanded inline. If it is specified that a function described in another C source is expanded inline, the CC-RH does not output a warning message and ignores the specification.

(b)

The #pragma inline directive is described before function definition.

If the #pragma inline directive is described after function definition, the CC-RH outputs a warning message and ignores the specification. However, prototype declaration of the function may be described in any order. Here is an example.

Example

[Valid Inline Expansion Specification]
#pragma inline  func1, func2
void    func1();        /*prototype declaration*/
void    func2();        /*prototype declaration*/
void    func1() {...}   /*function definition*/
void    func2() {...}   /*function definition*/
[Invalid Inline Expansion Specification]
void    func1();        /*prototype declaration*/
void    func2();        /*prototype declaration*/
void    func1() {...}   /*function definition*/
void    func1() {...}   /*function definition*/
#pragma inline  func1, func2

(c)

The number of arguments is the same between "call" and "definition" of the function to be expanded inline.

If the number of arguments is different between "call" and "definition" of the function to be expanded inline, the CC-RH ignores the specification.

(d)

The types of return value and argument are the same between "call" and "definition" of the function to be expanded inline.

If the number of arguments is different between "call" and "definition" of the function to be expanded inline, the CC-RH ignores the specification. If the type of the argument is the integer type (including enum) or pointer-type, and in the same size, however, inline expansion is executed.

(e)

The number of arguments of the function to be expanded inline is not variable.

If inline expansion is specified for a function with a variable arguments, the CC-RH outputs neither an error nor warning message and ignores the specification.

(f)

Recursive function is not specified to be expanded inline.

If a recursive function that calls itself is specified for inline expansion, the CC-RH outputs neither an error nor warning message and ignores the specification. If two or more function calls are nested and if a code that calls itself exists, however, inline expansion may be executed.

(g)

does not make calls via the addresses of functions for inline expansion.

If you call a function for inline expansion via its address, then the inline expansion specification will be ignored, without outputting an error or warning message.

(h)

An interrupt handler is not specified to be expanded inline.

A function specified by the #pragma interrupt is recognized as an interrupt handler. If inline expansion is specified for this function, the CC-RH outputs a warning message and ignores the specification.

(i)

Interrupts are not disabled in a function by the #pragma block_interrupt directive.

#If inline expansion is specified for a function in which interrupts are declared by the #pragma block_interrupt directive to be disabled, the CC-RH outputs a warning message and ignores the specification.

(j)

If #pragma inline_asm is specified, then a #pragma inline specification will be ignored.

If you specify inline expansion for a function coded in assembly using #pragma inline_asm, then a warning message will be output, and the #pragma inline specification will be ignored.

(k)

If you specify -Xmerge_files, then functions may be inlined even if they are not coded within the file.

 

(3)

Functions ineligible for inline expansion

When using the -Oinline option, use #pragma noinline to prevent inline expansion of a specific function.

#pragma noinline  [(]function-name[, function-name, ...][)]

Specifying #pragma inline and #pragma noinline for the same function simultaneously within a single translation unit will cause an error.

(4)

Examples of differences in inline expansion operation depending on option specification

Here are differences in inline expansion operation depending on whether the #pragma inline directive or an option is specified.

-Oinline=0

Inline expansion specification will be ignored, without outputting an error or warning message.

-Oinline=1

Inline expansion will be performed on functions specified for it.

-Oinline=2

Inline expansion will be performed on functions automatically, even if it is not specified.

However, inline expansion will not be performed on functions specified as ineligible for inline expansion.

-Oinline=3

Inline expansion will be performed on functions automatically, even if it is not specified.

However, inline expansion will not be performed on functions specified as ineligible for inline expansion.

(5)

Sample inline expansion

Below is an example of inline expansion.

-

C source

#pragma inline  (func)
static int      func(int a, int b)
{
        return  (a+b)/2;
}
int     x;
main()
{
        x = func (10, 20);
}

-

Sample expansion

int     x;
main()
{
        int     func_result;
{
        int     a_1 = 10, b_1 = 20;
        func_result = (a_1+b_1)/2;
}
        x = func_result;
}