[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[MiNT] GCC returning structs



Hello.

I have made some investigation about how GCC returns structs.
It can use two incompatible schemes, which can be changed from the command line. Of course there is a default one.

1) -fpcc-struct-return (Portable C Compiler)
In this mode, the caller allocates space for the returned struct, and passes its address to the function as an additional parameter in the register a1. Then the function returns the data to the area pointed to by a1, and also returns a1 into d0.

2) -freg-struct-return (registers)
In this mode, structs of size 8 bytes or less can be returned directly in the registers d0/d1. This only occurs when some obscure alignment constraints are met. If this mode can't be used (big structs, unaligned...), the PCC mode is used instead.

It is considered that -freg-struct-return is a bit faster than -fpcc-struct-return. -freg-struct-return has always been the default for MiNT, with GCC 2.x or 4.x. It is considered to be the best choice when GCC has not to be compatible to anything. It is also used by the generic m68k-ELF and m68k/Linux.

But GCC 2.x and 4.x decide differently when returning structs to registers or not. With GCC 4.x, I have been able to return structs of size 1, 2, 4 or 8 bytes to d0/d1. But with GCC 2.x, I only managed to return structs of size 1 byte to d0.

Returning structs is a feature not so much used in C programs, due to its inefficient nature. But this is an area where GCC 2.x and 4.x are binary incompatible, and this will probably not change. This feature is probably much more used in C++. Of course this problem disappears if we use a single compiler and recompiled libraries.

--
Vincent Rivière