www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16527] New: extern( C++ ) Win64 build - return struct by

https://issues.dlang.org/show_bug.cgi?id=16527

          Issue ID: 16527
           Summary: extern( C++ ) Win64 build - return struct by value is
                    broken
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: blocker
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: gooberman gmail.com

DMD 2.071.2, Windows, 64-bit output.

The this pointer should be always stored in RCX, but the functions I'm using
where structs are being returned by value switch the return address from RDX
with RCX.

While I'm actually using raw C++ functions through Binderoo, this is actually
quite reproducible with D code that extern( C++ )'s its functions.

Marking as blocker as there's a ridiculous number of functions we bind to that
return structs by value that are just unusable.

Code as follows:

module thismodule;

import std.stdio;
import std.conv;

align( 16 ) struct SomeCommonStruct
{
    float valA = 0;
    float valB = 0;
    float valC = 0;
    float valD = 0;
}

struct SomeObject
{
    void doAThing()
    {
        SomeWrapper aWrapper;

        SomeCommonStruct structThing = aWrapper.callme( &aWrapper );

        writeln( to!string( structThing.valD ) );
    }

}

extern( C++ ) struct SomeWrapper
{
    SomeCommonStruct giveMeACommonStruct( SomeObject* pObject )
    {
        return structeroo;
    }

    SomeCommonStruct structeroo = SomeCommonStruct( 1, 0, 3, 4 );

    alias FnType = extern( C++ ) SomeCommonStruct function( SomeWrapper* );

    __gshared extern( C++ ) SomeCommonStruct function( SomeWrapper* ) callme;
}

int main(string[] argv)
{
    SomeWrapper.callme = cast( SomeWrapper.FnType
)cast(void*)&SomeWrapper.giveMeACommonStruct;

    SomeObject someObject;
    someObject.doAThing();

    return 0;
}

Expected: function SomeObject.doAThing writes the value "4" to stdout
Actual: function SomeObject.doAThing writes rubbish to stdout

--
Sep 23 2016