Issue 24310 - ImportC: varargs from Microsoft header incompatible with va_start
Summary: ImportC: varargs from Microsoft header incompatible with va_start
Status: REOPENED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All Windows
: P3 normal
Assignee: No Owner
URL:
Keywords: ImportC, pull
Depends on:
Blocks:
 
Reported: 2023-12-31 13:18 UTC by Dennis
Modified: 2024-01-04 15:26 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Dennis 2023-12-31 13:18:08 UTC
In Phobos' etc/c/zlib/gzwrite.c file there is a workaround:

---
// needed on Win64 - MS __va_start intrinsic not supported by importC yet
#if __IMPORTC__
    __builtin_va_start(va, format);
#else
    va_start(va, format);
#endif
---

See https://github.com/dlang/phobos/pull/8865#issuecomment-1862006408

---
Argh, the Microsoft headers use something like this (`vadefs.h`) for x86_64:

```c
void __cdecl __va_start(va_list* , ...);
#define __crt_va_start_a(ap, x) ((void)(__va_start(&ap, x)))
```

The signature of their intrinsic isn't compatible with druntime's `va_start` (which takes the first param as `out` ref, not as explicit pointer), so I sadly don't see a way to handle this in druntime's `__builtins.di` (or `importc.h`).
---
Comment 1 Dlang Bot 2024-01-01 20:27:53 UTC
@WalterBright updated dlang/dlang.org pull request #3752 "fix Issue 24312 - importC: Document workaround for using C symbols wh…" fixing this issue:

- fix Issue 24310 - importC: Document workaround for using C symbols which are also D keywords

https://github.com/dlang/dlang.org/pull/3752
Comment 2 Walter Bright 2024-01-01 21:14:22 UTC
(In reply to Dlang Bot from comment #1)
> https://github.com/dlang/dlang.org/pull/3752

Ignore that. It was meant for another issue.
Comment 3 Walter Bright 2024-01-02 02:13:33 UTC
Currently,

1. there is no va_start in importc.h

2. in __builtins.di there is:

    alias __builtin_va_start = imported!"core.stdc.stdarg".va_start;

3. in Microsoft's stdarg.h there is:

    #define va_start __crt_va_start

4. in core.stdc.stdarg there is:

    void va_start(T)(out va_list ap, ref T parmn);

va_start() is an intrinsic built in to dmd.

Is __va_start() a Microsoft intrinsic?
Comment 4 Walter Bright 2024-01-02 02:22:29 UTC
I suspect it might work if we added to stdarg.d:

    void __va_start(T)(va_list* ap, ref T parmn)
    {
        va_start(*ap, parmn);
    }

and to __builtins.di:

    alias __va_start = imported!"core.stdc.stdarg".__va_start;
Comment 5 Dlang Bot 2024-01-04 15:13:40 UTC
dlang/dlang.org pull request #3752 "fix Issue 24310 - importC: Document workaround for using C symbols wh…" was merged into master:

- 078629cbca561a697cb380481bcce6744c0a07ef by Walter Bright:
  fix Issue 24310 - importC: Document workaround for using C symbols which are also D keywords

https://github.com/dlang/dlang.org/pull/3752
Comment 6 Dennis 2024-01-04 15:25:24 UTC
Issue was linked to the wrong PR
Comment 7 kinke 2024-01-04 15:26:45 UTC
(In reply to Walter Bright from comment #4)
> I suspect it might work if we added to stdarg.d:
> 
>     void __va_start(T)(va_list* ap, ref T parmn)
>     {
>         va_start(*ap, parmn);
>     }
> 
> and to __builtins.di:
> 
>     alias __va_start = imported!"core.stdc.stdarg".__va_start;

I doubt it - this would probably need to be force-inlined at the AST level to work properly.