Re: Two ways of receiving arrays on the C ABI

2020-10-25 Thread Jacob Carlborg via Digitalmars-d-learn

On 2020-10-20 02:16, Ali Çehreli wrote:

Everything works at least on Linux. Is this kosher, or am I using some 
internal knowledge?


Yes, you're using some internal knowledge. You cannot assume it works on 
any other platform or architecture. In theory, the D compiler could 
choose to change the ABI for passing D arrays and this would break. If 
fact, the ABI documentation [1] doesn't mention how a D array is passed. 
A different compiler could choose to pass it differently.


[1] https://dlang.org/spec/abi.html#arrays

--
/Jacob Carlborg


Re: Two ways of receiving arrays on the C ABI

2020-10-20 Thread Ali Çehreli via Digitalmars-d-learn
On 10/19/20 6:28 PM, Nicholas Wilson wrote:> On Tuesday, 20 October 2020 
at 00:16:48 UTC, Ali Çehreli wrote:

>> On the D side, both of the following extern(C) functions take the same
>> arguments.
>
> https://github.com/dlang/dmd/pull/8120
>
> there are issues with structs. Not sure about length/ptr.

Thank you, Nic.

I see that doing the following is undefined behavior:

extern(C)
void dFunc(int[] arr) {// <-- Takes slice
  assert(arr.equal(3.iota));
}

// A C function
void dFunc(size_t length, int * ptr);  // <-- Passes length+pointer

  int arr[] = { 0, 1, 2 };
  dFunc(3, arr);

C does not define an ABI. So, length+pointer can be passed in any way 
from there. The good thing is that D is aware of the "C Application 
Binary Interface of the target system"[1] but that means D would support 
taking length+pointer as well. Slice arguments are still strictly in 
length+pointer order for D.


So, it works on my Linux system by chance.

Ali

[1] https://dlang.org/spec/abi.html




Re: Two ways of receiving arrays on the C ABI

2020-10-19 Thread Nicholas Wilson via Digitalmars-d-learn

On Tuesday, 20 October 2020 at 00:16:48 UTC, Ali Çehreli wrote:
On the D side, both of the following extern(C) functions take 
the same arguments.


https://github.com/dlang/dmd/pull/8120

there are issues with structs. Not sure about length/ptr.




Two ways of receiving arrays on the C ABI

2020-10-19 Thread Ali Çehreli via Digitalmars-d-learn
On the D side, both of the following extern(C) functions take the same 
arguments.


1) func1 takes .length and .ptr implicitly as parts of a D array:

extern(C)
void func1(int[] arr) {
  assert(arr.equal(3.iota));
}

2) func2 takes .length and .ptr separately and then makes a slice 
explicitly:


extern(C)
void func2(size_t length, int * ptr) {
  auto arr = ptr[0..length];
  assert(arr.equal(3.iota));
}

C side declares and calls both of them the same way:

void func1(size_t length, int * ptr);
void func2(size_t length, int * ptr);

  int arr[] = { 0, 1, 2 };
  func1(3, arr);
  func2(3, arr);

Everything works at least on Linux. Is this kosher, or am I using some 
internal knowledge?


Here is the ABI spec:

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

One of the DConf Online 2020 slides thanks you! ;)

Ali