Liste des Groupes | Revenir à c arch |
David Brown <david.brown@hesbynett.no> writes:Agreed.But the ABI only allows returning a single 32-bit value in R0, or aMost calling conventions on RISCs are oriented towards C (if you want
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?
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.
Struct returns were (and AFAIK still are, many decades afterI use struct returns sometimes in my C code, but they are (naturally enough) a far smaller proportion of return types than in C++ code.
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. OfI know about the -freg-struct-return option (and the requirements for using it), but it only has effect for 32-bit x86 as far as I know. It certainly makes no difference for 32-bit ARM/Thumb. (clang specifically says it does not support that option for 32-bit ARM/Thumb.) I think part of this is that the calling convention already returns structs in registers - just as long as the struct fits in the single 32-bit register.
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.
I first used ARM assembly in the late 1980's, but that was mixed BBC BASIC and assembly, all with almost no documentation, so I don't know what calling conventions there were at that time. (But the Acorn Archimedes was /really/ cool :-) )Newer ABIs like RISC-V 32-bit and x86_64ARM A32 is from 1985, and its calling convention is probably not much
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.
younger.
Passing all parameters on the stack and returning a single int in a register was a perfect fit for old-style C where functions were often used without declarations. It would certainly be a lot easier for variadic functions. But once you start passing some parameters in registers, it seems strange to use so few. Perhaps it was to make life easier for earlier compiler writers? Things like lifetime analysis and register allocation algorithms were not as sophisticated as they are now - it used to be that if a variable used a register (via the C "register" qualifier), the register was dedicated to the variable throughout the function. Too many registers for parameter passing might have left too few registers for function implementation, or at least made the compiler more complex.I also think code would be a bit more efficient if there more registersThere is a tendency towards passing more parameters in registers in
available for parameter passing and as scratch registers - perhaps 6
would make more sense.
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.
Yes.In more modern C++ programming, it's very practical to use types likeThe ARM calling convention is certainly much older than "modern C++
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.
programming".
I program in C and C++, and in the past did a lot of assembly (mostly on 8-bit or 16-bit microcontrollers). I am fully aware that C and C++ are different languages, and I write code in different styles for each.But the limited return registers adds significant overhead toC++ programmers think they know what C programming is about (and
small functions.
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.
Les messages affichés proviennent d'usenet.