www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20993] New: spec claims extern(C) and extern(D) function are

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

          Issue ID: 20993
           Summary: spec claims extern(C) and extern(D) function are
                    identical
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Keywords: spec
          Severity: normal
          Priority: P1
         Component: dlang.org
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

The spec on the ABI claims that "The extern (C) and extern (D) calling
convention matches the C calling convention used by the supported C compiler on
the host system."

https://dlang.org/spec/abi.html#function_calling_conventions

On MacOS, however, the calling convention of even 2 int parameters is
different:

--- foo.c
#include <stdio.h>

void foo(int a, int b)
{
    printf("a=%d b=%d\n", a, b);
}

--- main.d

pragma(mangle, "foo") extern (D) void foo_extern_d(int, int);
pragma(mangle, "foo") extern (C) void foo_extern_c(int, int);

void main()
{
    foo_extern_d(1, 2);
    foo_extern_c(1, 2);
} 

---

gcc -c foo.c
dmd main.d foo.o
./main
a=2 b=1
a=1 b=2

Clearly, the arguments are passed in a different order depending on whether the
function is extern(C) or extern(D). Either the spec should be updated, or the
compiler should be modified to match the spec.

--
Jun 29 2020