Issue 24130 - ImportC: Windows headers use inline asm with different syntax
Summary: ImportC: Windows headers use inline asm with different syntax
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P1 critical
Assignee: No Owner
URL:
Keywords: iasm, ImportC, pull
Depends on:
Blocks:
 
Reported: 2023-09-01 18:51 UTC by Jeffrey H. Johnson
Modified: 2023-09-11 15:01 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 Jeffrey H. Johnson 2023-09-01 18:51:35 UTC
DMD v2.105.0, Windows 11, latest MSVC community edition:

>TYPE test.c

#include <windows.h>
int main(void) { return 0; }

>DMD -m32 -v test.c
predefs   DigitalMars LittleEndian D_Version2 all Windows Win32 CRuntime_Microsoft CppRuntime_Microsoft D_InlineAsm D_InlineAsm_X86 X86 assert D_PreConditions D_PostConditions D_Invariants D_ModuleInfo D_Exceptions D_TypeInfo D_HardFloat
binary    dmd
version   v2.105.0-dirty
config    C:\D\dmd2\windows\bin64\sc.ini
DFLAGS    -IC:\D\dmd2\windows\bin64\..\..\src\phobos -IC:\D\dmd2\windows\bin64\..\..\src\druntime\import
include   C:\D\dmd2\windows\bin64\..\..\src\druntime\import\importc.h
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\bin\HostX64\x86\cl.exe /P /Zc:preprocessor /PD /nologo test.c /FIC:\D\dmd2\windows\bin64\..\..\src\druntime\import\importc.h /Fitest.i
parse     test
C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um\winnt.h(1016): Error: `asm` statements must end in `;`
C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um\winnt.h(1032): Error: `asm` statements must end in `;`
C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um\winnt.h(1048): Error: `asm` statements must end in `;`
C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um\winnt.h(13826): Error: found `i64` when expecting `)`
C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um\winnt.h(13826): Error: found `i64` when expecting `)`
C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um\winnt.h(13826): Error: found `i64` when expecting `)`
C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um\winnt.h(13826): Error: found `i64` when expecting `)`
C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um\winnt.h(13826): Error: found `i64` when expecting `)`
C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um\winnt.h(13826): Error: found `)` when expecting `]`
C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um\winnt.h(13826): Error: `=`, `;` or `,` expected to end declaration instead of `)`
Comment 1 Walter Bright 2023-09-05 04:46:31 UTC
Can you please take a look at the generated test.i file and post which lines in it are failing?
Comment 2 Jeffrey H. Johnson 2023-09-05 21:25:41 UTC
(In reply to Walter Bright from comment #1)
> Can you please take a look at the generated test.i file and post which lines
> in it are failing?

Yes, I'll try to get this done today.
Comment 3 anonymous4 2023-09-06 07:26:50 UTC
I have this code around that line:

__inline ULONGLONG
NTAPI
Int64ShrlMod32 (
    _In_ ULONGLONG Value,
    _In_ DWORD ShiftCount
    )
{
    __asm    {
        mov     ecx, ShiftCount
        mov     eax, dword ptr [Value]
        mov     edx, dword ptr [Value+4]
        shrd    eax, edx, cl
        shr     edx, cl
    }
}

So D complains about absence of semicolon after the asm statement.
Comment 4 Jeffrey H. Johnson 2023-09-06 23:06:11 UTC
> So D complains about absence of semicolon after the asm statement.

Can confirm.
Comment 5 Walter Bright 2023-09-09 04:27:17 UTC
Looking at the file winnt.h, the inline assembler:

--------------
#if defined(MIDL_PASS) || defined(RC_INVOKED) || defined(_M_CEE_PURE) \
    || defined(_68K_) || defined(_MPPC_) \
    || defined(_M_IA64) || defined(_M_AMD64) || defined(_M_ARM) || defined(_M_ARM64) \
    || defined(_M_HYBRID_X86_ARM64)

//
// Midl does not understand inline assembler. Therefore, the Rtl functions
// are used for shifts by 0..31 and multiplies of 32-bits times 32-bits to
// form a 64-bit product.
//
//
// IA64 and AMD64 have native 64-bit operations that are just as fast as their
// 32-bit counter parts. Therefore, the int64 data type is used directly to form
// shifts of 0..31 and multiplies of 32-bits times 32-bits to form a 64-bit
// product.
//

#define Int32x32To64(a, b)  (((__int64)((long)(a))) * ((__int64)((long)(b))))
#define UInt32x32To64(a, b) (((unsigned __int64)((unsigned int)(a))) * ((unsigned __int64)((unsigned int)(b))))

#define Int64ShllMod32(a, b) (((unsigned __int64)(a)) << (b))
#define Int64ShraMod32(a, b) (((__int64)(a)) >> (b))
#define Int64ShrlMod32(a, b) (((unsigned __int64)(a)) >> (b))


#elif defined(_M_IX86)

... inline assembler versions go here ...

#endif
---------------------------------

And, well, the inline assembler format is different than ImportC's.

So, try #define'ing _M_CEE_PURE to be 1 and see if that helps.


As for the line 13826 errors, I cannot find "i64" in the version of winnt.h that I have. Perhaps you could quote the offending lines, please?
Comment 6 Dlang Bot 2023-09-09 04:35:42 UTC
@WalterBright created dlang/dmd pull request #15595 "fix Issue 24130 - ImportC: Windows headers use inline asm with differ…" fixing this issue:

- fix Issue 24130 - ImportC: Windows headers use inline asm with different syntax

https://github.com/dlang/dmd/pull/15595
Comment 7 Dlang Bot 2023-09-11 01:24:38 UTC
dlang/dmd pull request #15595 "fix Issue 24130 - ImportC: Windows headers use inline asm with differ…" was merged into master:

- 05403c7aaf342af2bbb92f5a8cec121dda5dce98 by Walter Bright:
  fix Issue 24130 - ImportC: Windows headers use inline asm with different syntax

https://github.com/dlang/dmd/pull/15595
Comment 8 anonymous4 2023-09-11 15:01:11 UTC
i64 is used in some kind of cpu feature constants (Extended processor state configuration):
#define XSTATE_MASK_LEGACY_FLOATING_POINT   (1ui64 << (XSTATE_LEGACY_FLOATING_POINT))
#define XSTATE_MASK_LEGACY_SSE              (1ui64 << (XSTATE_LEGACY_SSE))