Thread overview
[Issue 12308] New: Request pragma for very thin struct wrappers
Mar 07, 2014
Adam D. Ruppe
Mar 07, 2014
Adam D. Ruppe
March 07, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12308

           Summary: Request pragma for very thin struct wrappers
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: destructionator@gmail.com


--- Comment #0 from Adam D. Ruppe <destructionator@gmail.com> 2014-03-06 20:04:13 PST ---
In short, I want codegen for

pragma(thin_struct) struct S { int; }

to be identical in every way as for int; Especially, struct member functions just get the value in the register instead of a pointer, and returning structs is done in the register instead of via hidden pointer.

http://forum.dlang.org/thread/lfbbcn$2th7$1@digitalmars.com?page=2#post-jklqyrulonhxwrucvbil:40forum.dlang.org

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 07, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12308


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #1 from bearophile_hugs@eml.cc 2014-03-07 02:21:06 PST ---
Walter seems clearly against this idea:

---------------------

http://forum.dlang.org/post/lfbhc4$dc$1@digitalmars.com 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/lfbg06$30kh$1@digitalmars.com

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.

---------------------

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 07, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12308



--- Comment #2 from Adam D. Ruppe <destructionator@gmail.com> 2014-03-07 07:18:14 PST ---
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.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------