D struct with String type accessing from Python

Ali Çehreli acehreli at yahoo.com
Wed Dec 28 00:15:42 UTC 2022


On 12/27/22 11:02, Ali Çehreli wrote:

 > whether an array is the same as a struct with 2 members. (We
 > know it is equivalent but it doesn't say it is the same as a
 > struct definition.)

I've just checked. No, D arrays are not returned as a struct that has 
two members.

Source code and the output of -vasm compilation:

$ tail -n 17 deneme.d
struct S {
     size_t length;
     char * ptr;
}

S returns_S() {
     return S();
}

string returns_string() {
     return "hello world";
}

void main() {
     auto a = returns_S();
     auto b = returns_string();
}

$ dmd -vasm deneme.d
_D6deneme9returns_SFZSQu1S:
0000:   55                       push      RBP
0001:   48 8B EC                 mov       RBP,RSP
0004:   48 83 EC 10              sub       RSP,010h
0008:   48 C7 45 F0 00 00 00 00  mov       qword ptr -010h[RBP],0
0010:   48 C7 45 F8 00 00 00 00  mov       qword ptr -8[RBP],0
0018:   48 8B 55 F8              mov       RDX,-8[RBP]
001c:   48 8B 45 F0              mov       RAX,-010h[RBP]
0020:   C9                       leave
0021:   C3                       ret
_D6deneme14returns_stringFZAya:
0000:   48 8D 15 FC FF FF FF     lea       RDX,[0FFFFFFFCh][RIP]
0007:   B8 0B 00 00 00           mov       EAX,0Bh
000c:   C3                       ret
_Dmain:
0000:   55                       push      RBP
0001:   48 8B EC                 mov       RBP,RSP
0004:   E8 00 00 00 00           call      L0
0009:   E8 00 00 00 00           call      L0
000e:   31 C0                    xor       EAX,EAX
0010:   5D                       pop       RBP
0011:   C3                       ret
main:
0000:   55                       push      RBP
0001:   48 8B EC                 mov       RBP,RSP
0004:   48 8B 15 FC FF FF FF     mov       RDX,[0FFFFFFFCh][RIP]
000b:   E8 00 00 00 00           call      L0
0010:   5D                       pop       RBP
0011:   C3                       ret

A string is passed by registers while a struct object is copied to 
stack. For strings, EAX carries the length because 0Bh is 11, the length 
of "hello world".

As expected, compiling with -O changes how the struct object is 
returned; now by registers like a string is:

_D6deneme9returns_SFZSQu1S:
0000:   31 C0                    xor       EAX,EAX
0002:   31 D2                    xor       EDX,EDX
0004:   C3                       ret

Ali



More information about the Digitalmars-d mailing list