David Brown <
david.brown@hesbynett.no> writes:
But the ABI only allows returning a single 32-bit value in R0, or a
scalar 64-bit value in R0:R1. If a function returns a non-scalar that
is larger than 32-bit, the caller has to allocate space on the stack for
the return type and pass a pointer to that space in R0.
>
To my mind, this is massively inefficient, especially when using structs
that are made up of two 32-bit parts.
>
Is there any good reason why the ABI is designed with such limited
register usage for returns?
Most calling conventions on RISCs are oriented towards C (if you want
calling conventions that try to be more cross-language (and slower),
look at VAX) and its properties and limitations at the time when the
calling convention was designed, in particular, the PCC
implementation, which was the de-facto standard Unix C compiler at the
time. C compilers in the 1980s did not allocate structs to registers,
so passing structs in registers was foreign to them, so the solution
is that the caller passes the target struct as an additional
parameter.
And passing the return value in registers might not have saved
anything on a compiler that does not deal with structs in registers.
E.g., if you have
mystruct = myfunc(arg1, arg2);
you would see stores to mystruct behind the call. With the PCC
calling convention, the same stores would happen in the caller
(possibly resulting in smaller code if there are several calls to
myfunc()).
I wonder, though, how things look for
mystruct = foo(&mystruct);
Does PCC perform the return stores to mystruct only after performing
all other memory accesses in foo? Probably yes, anything else would
complicate the compiler. In that case the caller could pass &mystruct
for the return value (a slight complication). But is that restriction
reflected in the calling convention?
Struct returns were (and AFAIK still are, many decades after
they were added to C) a relatively rarely used feature, so Johnson
(PCC's author) probably did not want to waste a lot of effort on
making it more efficient.
gcc has an option -freg-struct-return, which does what you want. Of
course, if you use this option on ARM A32/T32, you are not following
the calling convention, so you should only use it when all sides of a
struct return are compiled with that option.
Newer ABIs like RISC-V 32-bit and x86_64
can at least use two registers for return values. Modern compilers are
quite happy breaking structs into parts in individual registers - it's a
/long/ time since they insisted that structs occupied a contiguous block
of memory.
ARM A32 is from 1985, and its calling convention is probably not much
younger.
I also think code would be a bit more efficient if there more registers
available for parameter passing and as scratch registers - perhaps 6
would make more sense.
There is a tendency towards passing more parameters in registers in
more recent calling conventions. IA-32 (and IIRC VAX) passes none,
MIPS uses 4 integer registers (for either integer or FP parameters),
Alpha uses 6 integer and 6 FP registers, AMD64's System V ABI 6
integer and 8 FP registers, ARM A64 has 8 integer and 8 FP registers,
RISC-V has 8 integer and 8 FP registers. Not sure why they were so
reluctant to use more registers earlier.
In more modern C++ programming, it's very practical to use types like
std::optional<>, std::variant<>, std::expected<> and std::tuple<> as a
way of dealing safely with status and multiple return values rather than
using C-style error codes or passing manual pointers to return value
slots.
The ARM calling convention is certainly much older than "modern C++
programming".
But the limited return registers adds significant overhead to
small functions.
C++ programmers think they know what C programming is about (and
unfortunately they dominate not just C++ compiler writers, but they
also damage C compilers while they are at it), so my sympathy for your
problem is very limited.
- anton
-- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>