You copied the Doc URL to your clipboard.

Returning structures from functions through registers

The compiler allows functions to return structures containing multiple values through the registers, rather than the stack.

In C and C++, one way of returning multiple values from a function is to use a structure. Normally, structures are returned on the stack, with all the associated expense this entails.

To reduce memory traffic and reduce code size, the compiler enables functions to return multiple values through the registers. A function can return up to four words in a struct by qualifying the function with __value_in_regs. For example:

typedef struct s_coord { int x; int y; } coord;
coord reflect(int x1, int y1) __value_in_regs;

You can use __value_in_regs anywhere where multiple values have to be returned from a function. Examples include:

  • Returning multiple values from C and C++ functions.

  • Returning multiple values from embedded assembly language functions.

  • Making supervisor calls.

  • Re-implementing __user_initial_stackheap.