Comment #1 by bearophile_hugs — 2014-03-07T02:21:06Z
Walter seems clearly against this idea:
---------------------
http://forum.dlang.org/post/[email protected]
Walter Bright:
Posted in reply to Adam D. Ruppe
On 3/6/2014 8:01 PM, Adam D. Ruppe wrote:
> BTW you know what would help this? A pragma we can attach to a struct which
> makes it a very thin value type.
I'd rather fix the compiler's codegen than add a pragma.
---------------------
http://forum.dlang.org/post/[email protected]
Walter Bright:
On 3/6/2014 10:12 PM, H. S. Teoh wrote:
> From what I understand, structs are *supposed* to be thin value types. I
> would say that if a struct is under a certain size (determined by the
> compiler), and doesn't have complicated semantics like dtors and stuff
> like that, then it should be treated like a POD (passed in registers,
> etc).
Yes, that's right.
---------------------
Comment #2 by destructionator — 2014-03-07T07:18:14Z
I don't think it has been explained fully yet. A struct with just an int is not fully interchangeable for an int because they are returned differently by functions, including in C. So this isn't really a matter of fixing anything, since it isn't broken, it is a different spec.
indeed, since it changes the calling convention, how would this mangle? I'd say the same as the type it wraps. The struct just disappears as far as the backend is concerned.
A few other points in the thread: couldn't the hidden this be changed to pass by value? Maybe, though taking the address of this wouldn't work, I think.
A* ptr() { return &this; }
works now, just returning EAX (which is the hidden this pointer in the first place). Trying that with a regular int is (awesomely!) an error:
int* foo(int a) { return &a; }
test56.d(17): Error: escaping reference to local a
* * *
Could we just change the D calling convention to pass small structs by value* while keeping extern(C) the same for compatibility? Yes, I think we can, and I think it would be a win. But I'd still like it to be available in extern(C) too for cases like library typedef, where we want it to look different on the D side, but remain ABI identical to the naked type.
* Note: this is already what happens for most function arguments:
void test(A a);
calling that looks like:
mov EAX, [a];
call test;
Which is identical to test(int a).
The difference is returning a value and the hidden this pointer. Returning a value is done by hidden pointer too.
Comment #3 by robert.schadek — 2024-12-13T18:17:57Z