 | C166 User's Guide |  |
|
|
| Inline FunctionsThe C166 Compiler supports inline functions using the __inline keyword. For example:
__inline <[>type<]> funcname (<[>args<]>)
Where | __inline | specifies that the function is an inline function. | | type | is the type of the value returned from the function. If no type is specified, int is assumed. | | funcname | is the name of the function. | | args | is the argument list for the function. Functions with variable argument lists cannot be expanded as inline code. |
Code generated for inline functions is expanded in-line inside the calling function. This has the benefit of reduced call overhead at the expense of increased code size. There are several rules that apply to inline functions. These rules become especially important to consider when inline functions call other inline functions. - Function in-lining is performed on an expression statement boundary where the right side operator is a function call. For example:
z = iFunc (z1, z2); // in-lined:
// first operation is a call
z = iFunc (z1, z2) * 11; // not in-lined:
// first operation is a multiply
z = iFunc (z1, z2) + 2; // not in-lined:
// first operation is an add
- Expressions with for statements are never in-lined. For example:
for (z = iFunc (z1, z2) ; z != 0 ; ) // iFunc is not in-lined
{ ... }
- Inline functions that call no other functions and inline functions that call only inline functions are in-lined as long as the above rules are fulfilled.
static __inline int yFunc (int n) {
return (n + 10);
}
static int zFunc (int n) {
return (n + 10);
}
static __inline int iFunc (int i, int y) {
i += y;
i += yFunc (y);
i *= zFunc (i);
// Note: i += y + yFunc * zFunc (i): is not in-lined
return (i);
}
static int z, z1, z2;
void main (void) {
z = iFunc (z1, z2); // in-lined
z = z2 + iFunc (z1, z2); // not in-lined
z = iFunc (z1, z2) * 11; // not in-lined
}
Note - When you create an inline function, the compiler generates callable function code unless you declare the function with file scope using the static keyword. For example:
static __inline int func (void) ...
For static __inline functions, the compiler generates a callable function if the inline function is called in a way that violates the above rules. Functions with global scope may be invoked by other external functions. Since the compiler only compiles one file at a time, an inline function with global scope is assumed to be called from a function in another source module and object code is generated. - When using the SRC directive to generate an assembler source file, the C166 Compiler does not perform function inlining.
- Function inlining depends on the complexity of a function. The C166 Compiler does not issue any warning messages in case that a function with the __inline attribute is not expaneded as inline code.
|
|