1.8. Functions. Altium TriCore
Reklama
Reklama
TASKING VX-toolset for TriCore User Guide
Especially for large switch statements, the jump table approach executes faster than the lookup table approach. Also the jump table has a predictable behavior in execution speed: independent of the switch argument, every case is reached in the same execution time. However, when the case labels are distributed far apart, the jump table becomes sparse, wasting code memory. The compiler will not use the jump table method when the waste becomes excessive.
With a small number of cases, the jump chain method can be faster in execution and shorter in size.
How to overrule the default switch method
You can overrule the compiler chosen switch method by using a pragma:
#pragma switch linear
#pragma switch jumptab
#pragma switch lookup
#pragma switch auto
#pragma switch restore force jump chain code force jump table code force lookup table code let the compiler decide the switch method used (this is the default) restore previous switch method
The switch pragmas must be placed before the switch
statement. Nested switch
statements use the same switch method, unless the nested switch
is implemented in a separate function which is preceded by a different switch pragma.
Example:
/* place pragma before function body */
#pragma switch jumptab void test(unsigned char val)
{ /* function containing the switch */
switch (val)
{
/* use jump table */
}
}
On the command line you can use C compiler option --switch .
1.8. Functions
1.8.1. Calling Convention
Parameter Passing
A lot of execution time of an application is spent transferring parameters between functions. The fastest parameter transport is via registers. Therefore, function parameters are first passed via registers. If no more registers are available for a parameter, the compiler pushes parameters on the stack.
24
C Language
Registers available for parameter passing are D4, D5, E4, D6, D7, E6, A4, A5, A6, A7. Up to 4 arithmetic types and 4 pointers can be passed this way. A 64-bit argument is passed in an even/odd data register pair. Parameter registers skipped because of alignment for a 64-bit argument are used by subsequent
32-bit arguments. Any remaining function arguments are passed on the stack. Stack arguments are pushed in reversed order, so that the first one is at the lowest address. On function entry, the first stack parameter is at the address (SP+0).
Structures up to eight bytes are passed via a data register or data register pair. Larger structures are passed via the stack.
All function arguments passed on the stack are aligned on a multiple of 4 bytes. As a result, the stack offsets for all types except float are compatible with the stack offsets used by a function declared without a prototype.
Examples: void func1( int i, char *p, char c ); /* D4 A4 D5 */ void func2( int i1, double d, int i2 ); /* D4 E6 D5 */ void func3( char c1, char c2, char c3[] ); /* D4 D5 A4 */ void func4( double d1, int i1, double d2, int i2 );
/* E4 D6 stack D7 */
Function Return Values
The C compiler uses registers to store C function return values, depending on the function return types.
Return Type
Arithmetic 32 bit
Arithmetic 64 bit
Pointer
Circular pointer
Register
D2
D2/D3 (E2)
A2
A2/A3
When the function return type is a structure, it is copied to a "return area" that is allocated by the caller.
The address of this area is passed as an implicit first argument in A4.
Stack model: __stackparm
The function qualifier
__stackparm
changes the standard calling convention of a function into a convention where all function arguments are passed via the stack, conforming a so-called stack model. This qualifier is only needed for situations where you need to use an indirect call to a function for which you do not have a valid prototype.
The compiler sets the least significant bit of the function pointer when you take the address of a function declared with the
__stackparm
qualifier, so that these function pointers can be identified at run-time.
The least significant bit of a function pointer address is ignored by the hardware.
Example: void plain_func ( int ); void __stackparm stack_func ( int );
25
Atsisiųsti
Reklama