[Issue 21476] New: [codegen] 64 bit C ABI not followed when returning struct with 3 floats

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Dec 12 14:32:13 UTC 2020


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

          Issue ID: 21476
           Summary: [codegen] 64 bit C ABI not followed when returning
                    struct with 3 floats
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: mrsmith33 at yandex.ru

struct vec3 { float x; float y; float z; }
extern(C) vec3 pass_func(vec3 v) {
    // dmd returns `v` on stack, while C ABI requires returning in xmm0 and
xmm1
    return v;
}
void caller() {
    // same here. Value is loaded from stack instead of from xmm0 + xmm1
    vec3 r = pass_func(vec3(1, 2, 3));
}

Code for pass_func
dmd
```
push   rbp
mov    rbp,rsp
sub    rsp,0x18
mov    QWORD PTR [rbp-0x18],rdi
movsd  QWORD PTR [rbp-0x10],xmm0
movss  DWORD PTR [rbp-0x8],xmm1
lea    rsi,[rbp-0x10]
mov    rdi,QWORD PTR [rbp-0x18]
movs   QWORD PTR es:[rdi],QWORD PTR ds:[rsi]
movs   BYTE PTR es:[rdi],BYTE PTR ds:[rsi]
movs   BYTE PTR es:[rdi],BYTE PTR ds:[rsi]
movs   BYTE PTR es:[rdi],BYTE PTR ds:[rsi]
movs   BYTE PTR es:[rdi],BYTE PTR ds:[rsi]
mov    rax,QWORD PTR [rbp-0x18]
leave
ret
```

ldc
```
push    rbp
mov     rbp, rsp
movsd   qword ptr [rbp - 16], xmm0
movss   dword ptr [rbp - 8], xmm1
mov     eax, dword ptr [rbp - 8]
mov     dword ptr [rbp - 24], eax
mov     rcx, qword ptr [rbp - 16]
mov     qword ptr [rbp - 32], rcx
movsd   xmm0, qword ptr [rbp - 32]
movss   xmm1, dword ptr [rbp - 24]
pop     rbp
ret
```

Code for caller:
dmd
```
movsd  xmm1,QWORD PTR [rbp-0x70]
fstp   QWORD PTR [rbp-0x70]
movsd  xmm0,QWORD PTR [rbp-0x70]
lea    rdi,[rbp-0x48]
call   rax
lea    rdx,[rip+0x9558c]
mov    edi,0x2
mov    rsi,rdx
fld    QWORD PTR [rbp-0x48]
fld    QWORD PTR [rbp-0x40]
```

ldc
```
mov     rax, qword ptr [rbp - 8]
mov     dword ptr [rbp - 40], 1065353216
mov     dword ptr [rbp - 36], 1073741824
mov     dword ptr [rbp - 32], 1077936128
mov     ecx, dword ptr [rbp - 32]
mov     dword ptr [rbp - 48], ecx
mov     rdx, qword ptr [rbp - 40]
mov     qword ptr [rbp - 56], rdx
movss   xmm1, dword ptr [rbp - 48]
movsd   xmm0, qword ptr [rbp - 56]
call    rax
movss   dword ptr [rbp - 64], xmm1
movsd   qword ptr [rbp - 72], xmm0
```

--


More information about the Digitalmars-d-bugs mailing list