www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Linux 64bit Calling Convention

reply "Maxime Chevalier" <maximechevalierb gmail.com> writes:
I'm implementing a JIT compiler and having to call into D 
functions from machine code I generated. Unfortunately, I seem to 
be experiencing a problem where the arguments are passed in the 
reverse order of what I would expect.

The functions I'm calling are global functions with 2 class 
pointer arguments. E.g.:

void foo(ClassA ptrA, ClassB ptrB) { ... }

The ABI page on dlang.org seems to imply that D uses the C 
calling convention on Linux. Now, using the C convention on 
64-bit linux, this should mean that the first class pointer 
(ptrA) get passed in register RDI, and the second one (ptrB) in 
RSI. This is what I would expect but when I actually call a D 
function, the two arguments are reversed.

I understand that this isn't necessarily super clear without 
looking at code, but I just wanted to know if there was something 
obvious I might be overlooking with regards to the D calling 
convention on Linux/AMD64.

Is it because ptrA automatically gets treated as a "this" 
pointer, as if the function were a method of ClassA?
Feb 27 2013
next sibling parent reply "Maxime Chevalier" <maximechevalierb gmail.com> writes:
I did some further testing:

void foo(int a, int b)
{
     writefln("a: %s", a);
     writefln("b: %s", b);
}

unittest
{
     foo(1, 2);

     asm
     {
         mov RDI, 1;
         mov RSI, 2;
         call foo;
     }
}

Produces:

a: 1
b: 2
a: 2
b: 1

Unless I'm mistaken, DMD does not respect the C calling 
convention on Linux/AMD64.
Feb 27 2013
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 2/27/2013 5:03 PM, Maxime Chevalier wrote:
 Unless I'm mistaken, DMD does not respect the C calling convention on
Linux/AMD64.

To use the C calling convention, specify "extern (C)" for the function. BTW, if this did not work, very little of D would work, as there's a lot of reliance on the C standard library and the Linux API functions.
Feb 27 2013
parent Walter Bright <newshound2 digitalmars.com> writes:
On 2/27/2013 6:36 PM, Maxime Chevalier wrote:
 The ABI page (http://dlang.org/abi.html) doesn't make it very clear what
calling
 convention DMD uses by default.

Should file a bugzilla report about that!
Feb 27 2013
prev sibling next sibling parent "Maxime Chevalier" <maximechevalierb gmail.com> writes:
 BTW, if this did not work, very little of D would work, as 
 there's a lot of reliance on the C standard library and the 
 Linux API functions.

The ABI page (http://dlang.org/abi.html) doesn't make it very clear what calling convention DMD uses by default.
Feb 27 2013
prev sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Thursday, 28 February 2013 at 02:36:07 UTC, Maxime Chevalier 
wrote:
 BTW, if this did not work, very little of D would work, as 
 there's a lot of reliance on the C standard library and the 
 Linux API functions.

The ABI page (http://dlang.org/abi.html) doesn't make it very clear what calling convention DMD uses by default.

Be careful with that page. Other compiler may use other ABI.
Feb 27 2013