A new look at rvalue references

via Digitalmars-d digitalmars-d at puremagic.com
Sat Jul 11 03:28:20 PDT 2015


bitwise and I had an interesting discussion about rvalue refs in 
the `auto ref` PR [1]. It revolved around whether the ABI [2] 
requires large structs to be passed as a pointer, as it happens 
to be the case with return values. The spec isn't clear on this, 
it merely says:

> Static arrays are passed as pointers to their first element.

I assumed the same would be true for large structs, because these 
and static arrays are treated alike as return values:

> For other sized structs and static arrays, the return value is 
> stored through a hidden pointer passed as an argument to the 
> function.

But bitwise's experiments showed that this isn't the case, and 
from what I can see, it doesn't even work according to spec for 
static arrays: The structs (and fixed-size arrays) are copied 
onto the stack by the caller, even for rvalues, and no pointer is 
passed to the callee.

I'm aware that the spec is only for x86 32-bit, and that on e.g. 
Posix x86_64, D uses the Linux ABI, AFAIK.

But assuming we could modify the ABI at will, we could have the 
following calling convention for large structs/arrays (say, 
anything that doesn't fit into two registers):

- These arguments are always passed as a pointer to the callee.
- If they are lvalues (and non-const), the caller has to make a 
copy and pass a pointer to this copy.
- If they are rvalues (or const), the caller doesn't need to make 
a copy, but can pass a pointer to the original location.

Wouldn't this make `ref` totally unnecessary as a performance 
trick, reducing it merely to its semantic meaning (i.e. mutation 
by the callee), and therefore also reduce the need for rvalue 
refs?

[1] https://github.com/D-Programming-Language/dmd/pull/4717
[2] http://dlang.org/abi.html (section Calling Conventions)


More information about the Digitalmars-d mailing list