Re: C aggregate passing (Rust kernel policy)

Jan Engelhardt
2025-02-20T15:17:09Z

On Thursday 2025-02-20 14:23, H. Peter Anvin wrote:
>
>People writing C seem to have a real aversion for using structures
>as values (arguments, return values or assignments) even though that
>has been valid since at least C90 and can genuinely produce better
>code in some cases.

The aversion stems from compilers producing "worse" ASM to this
date, as in this case for example:

```c
#include <sys/stat.h>
extern struct stat fff();
struct stat __attribute__((noinline)) fff()
{
struct stat sb = {};
stat(".", &sb);
return sb;
}
```

Build as C++ and C and compare.

$ g++-15 -std=c++23 -O2 -x c++ -c x.c && objdump -Mintel -d x.o
$ gcc-15 -std=c23 -O2 -c x.c && objdump -Mintel -d x.o

Returning aggregates in C++ is often implemented with a secret extra
pointer argument passed to the function. The C backend does not
perform that kind of transformation automatically. I surmise ABI reasons.