From f56dc6276875e805ad110db65705ba4b16e10975 Mon Sep 17 00:00:00 2001 From: Raed Rizqie Date: Thu, 11 Jul 2024 15:32:51 +0800 Subject: [PATCH] [headers]: [MinGW][1/3] Add support for MinGW-w64 --- include/dxc/Support/Global.h | 6 +- include/dxc/Support/WinIncludes.h | 2 + include/dxc/WinAdapter.h | 111 ++++++++++++++++++++++++++- include/dxc/dxcapi.h | 4 +- include/dxc/dxcdxrfallbackcompiler.h | 5 +- include/dxc/dxcisense.h | 4 +- include/dxc/dxctools.h | 2 +- include/llvm/ADT/StringRef.h | 1 + include/llvm/Object/COFF.h | 33 ++++---- include/llvm/Object/RelocVisitor.h | 10 ++- include/llvm/Support/COFF.h | 12 +++ 11 files changed, 158 insertions(+), 32 deletions(-) diff --git a/include/dxc/Support/Global.h b/include/dxc/Support/Global.h index 9d97467df9..2262b46439 100644 --- a/include/dxc/Support/Global.h +++ b/include/dxc/Support/Global.h @@ -281,7 +281,7 @@ inline void OutputDebugFormatA(const char *pszFormat, ...) { #ifndef NDEBUG -#ifdef _WIN32 +#ifdef _MSC_VER // DXASSERT is used to debug break when 'exp' evaluates to false and is only // intended for internal developer use. It is compiled out in free @@ -314,7 +314,7 @@ inline void OutputDebugFormatA(const char *pszFormat, ...) { #define DXVERIFY_NOMSG(exp) DXASSERT(exp, "") -#else // _WIN32 +#else // _MSC_VER #include #define DXASSERT_NOMSG assert @@ -341,7 +341,7 @@ inline void OutputDebugFormatA(const char *pszFormat, ...) { } \ } while (0) -#endif // _WIN32 +#endif // _MSC_VER #else // NDEBUG diff --git a/include/dxc/Support/WinIncludes.h b/include/dxc/Support/WinIncludes.h index b1c25ec392..854e3615dc 100644 --- a/include/dxc/Support/WinIncludes.h +++ b/include/dxc/Support/WinIncludes.h @@ -65,6 +65,7 @@ template void swap(CComHeapPtr &a, CComHeapPtr &b) { #include "dxc/WinAdapter.h" +#ifndef __MINGW32__ #ifdef __cplusplus #if !defined(DEFINE_ENUM_FLAG_OPERATORS) // Define operator overloads to enable bit operations on enum values that are @@ -118,6 +119,7 @@ template struct _ENUM_FLAG_SIZED_INTEGER { #else #define DEFINE_ENUM_FLAG_OPERATORS(ENUMTYPE) // NOP, C allows these operators. #endif +#endif // __MINGW32__ #endif // _MSC_VER diff --git a/include/dxc/WinAdapter.h b/include/dxc/WinAdapter.h index b8c6646871..b43b550902 100644 --- a/include/dxc/WinAdapter.h +++ b/include/dxc/WinAdapter.h @@ -15,7 +15,7 @@ #ifndef LLVM_SUPPORT_WIN_ADAPTER_H #define LLVM_SUPPORT_WIN_ADAPTER_H -#ifndef _WIN32 +#ifndef _MSC_VER #ifdef __cplusplus #include @@ -33,6 +33,105 @@ #include #endif // __cplusplus +#ifdef __MINGW32__ + +#undef _WIN32_WINNT +#undef _WIN32_IE + +// Require at least Windows 7 (Updated from XP) +#define _WIN32_WINNT 0x0601 +#define _WIN32_IE 0x0800 + +#define WIN32_LEAN_AND_MEAN +#define STRSAFE_NO_DEPRECATE + +#include +#include +#include +#include +#include +#include + +#undef EN +#undef IN +#undef OUT +#undef MemoryFence +#undef ReplaceText + +#define EventRegisterMicrosoft_Windows_DXCompiler_API() +#define EventUnregisterMicrosoft_Windows_DXCompiler_API() + +#define DxcEtw_DXCompilerCompile_Start() +#define DxcEtw_DXCompilerCompile_Stop(hr) +#define DxcEtw_DXCompilerCreateInstance_Start() +#define DxcEtw_DXCompilerCreateInstance_Stop(hr) +#define DxcEtw_DXCompilerDisassemble_Start() +#define DxcEtw_DXCompilerDisassemble_Stop(hr) +#define DxcEtw_DXCompilerInitialization_Start() +#define DxcEtw_DXCompilerInitialization_Stop(hr) +#define DxcEtw_DXCompilerPreprocess_Start() +#define DxcEtw_DXCompilerPreprocess_Stop(hr) +#define DxcEtw_DXCompilerShutdown_Start() +#define DxcEtw_DXCompilerShutdown_Stop(hr) +#define DxcEtw_DxcValidation_Start() +#define DxcEtw_DxcValidation_Stop(hr) + +#define ATLASSERT assert + +#ifdef __cplusplus + +constexpr uint8_t nybble_from_hex(char c) { + return ((c >= '0' && c <= '9') + ? (c - '0') + : ((c >= 'a' && c <= 'f') + ? (c - 'a' + 10) + : ((c >= 'A' && c <= 'F') ? (c - 'A' + 10) + : /* Should be an error */ -1))); +} + +constexpr uint8_t byte_from_hex(char c1, char c2) { + return nybble_from_hex(c1) << 4 | nybble_from_hex(c2); +} + +constexpr uint8_t byte_from_hexstr(const char str[2]) { + return nybble_from_hex(str[0]) << 4 | nybble_from_hex(str[1]); +} + +constexpr GUID guid_from_string(const char str[37]) { + return GUID{static_cast(byte_from_hexstr(str)) << 24 | + static_cast(byte_from_hexstr(str + 2)) << 16 | + static_cast(byte_from_hexstr(str + 4)) << 8 | + byte_from_hexstr(str + 6), + static_cast( + static_cast(byte_from_hexstr(str + 9)) << 8 | + byte_from_hexstr(str + 11)), + static_cast( + static_cast(byte_from_hexstr(str + 14)) << 8 | + byte_from_hexstr(str + 16)), + {byte_from_hexstr(str + 19), byte_from_hexstr(str + 21), + byte_from_hexstr(str + 24), byte_from_hexstr(str + 26), + byte_from_hexstr(str + 28), byte_from_hexstr(str + 30), + byte_from_hexstr(str + 32), byte_from_hexstr(str + 34)}}; +} + +#define CROSS_PLATFORM_UUIDOF(name, spec) \ + struct __declspec(uuid(spec)) name; \ + extern "C++" { \ + template <> struct __mingw_uuidof_s { \ + static constexpr IID __uuid_inst = guid_from_string(spec); \ + }; \ + template <> constexpr const GUID &__mingw_uuidof() { \ + return __mingw_uuidof_s::__uuid_inst; \ + } \ + template <> constexpr const GUID &__mingw_uuidof() { \ + return __mingw_uuidof_s::__uuid_inst; \ + } \ + } + +#endif // __cplusplus + +#else // __MINGW32__ + #define COM_NO_WINDOWS_H // needed to inform d3d headers that this isn't windows //===----------------------------------------------------------------------===// @@ -630,6 +729,12 @@ CROSS_PLATFORM_UUIDOF(ID3D12LibraryReflection, CROSS_PLATFORM_UUIDOF(ID3D12ShaderReflection, "5A58797D-A72C-478D-8BA2-EFC6B0EFE88E") +#endif // __cplusplus + +#endif // __MINGW32__ + +#ifdef __cplusplus + //===--------------------- COM Pointer Types ------------------------------===// class CAllocator { @@ -897,6 +1002,7 @@ class CHeapPtr : public CHeapPtrBase { #define CComHeapPtr CHeapPtr +#ifndef __MINGW32__ //===--------------------------- BSTR Allocation --------------------------===// void SysFreeString(BSTR bstrString); @@ -905,6 +1011,7 @@ BSTR SysAllocStringLen(const OLECHAR *strIn, UINT ui); //===--------------------------- BSTR Length ------------------------------===// unsigned int SysStringLen(const BSTR bstrString); +#endif // __MINGW32__ //===--------------------- UTF-8 Related Types ----------------------------===// @@ -1034,6 +1141,6 @@ class WArgV { #endif // __cplusplus -#endif // _WIN32 +#endif // _MSC_VER #endif // LLVM_SUPPORT_WIN_ADAPTER_H diff --git a/include/dxc/dxcapi.h b/include/dxc/dxcapi.h index 752041b7c7..330ba873a0 100644 --- a/include/dxc/dxcapi.h +++ b/include/dxc/dxcapi.h @@ -13,7 +13,7 @@ #ifndef __DXC_API__ #define __DXC_API__ -#ifdef _WIN32 +#ifdef _MSC_VER #ifndef DXC_API_IMPORT #define DXC_API_IMPORT __declspec(dllimport) #endif @@ -1222,7 +1222,7 @@ struct IDxcPdbUtils2 : public IUnknown { // Note: __declspec(selectany) requires 'extern' // On Linux __declspec(selectany) is removed and using 'extern' results in link // error. -#ifdef _MSC_VER +#ifdef _WIN32 #define CLSID_SCOPE __declspec(selectany) extern #else #define CLSID_SCOPE diff --git a/include/dxc/dxcdxrfallbackcompiler.h b/include/dxc/dxcdxrfallbackcompiler.h index c15906cabf..d41872aceb 100644 --- a/include/dxc/dxcdxrfallbackcompiler.h +++ b/include/dxc/dxcdxrfallbackcompiler.h @@ -12,7 +12,8 @@ #ifndef __DXC_DXR_FALLBACK_COMPILER_API__ #define __DXC_DXR_FALLBACK_COMPILER_API__ -#include "dxcapi.h" + +#include "dxc/dxcapi.h" enum class ShaderType : unsigned int { Raygen, @@ -94,7 +95,7 @@ struct __declspec(uuid("76bb3c85-006d-4b72-9e10-63cd97df57f0")) // Note: __declspec(selectany) requires 'extern' // On Linux __declspec(selectany) is removed and using 'extern' results in link // error. -#ifdef _MSC_VER +#ifdef _WIN32 #define CLSID_SCOPE __declspec(selectany) extern #else #define CLSID_SCOPE diff --git a/include/dxc/dxcisense.h b/include/dxc/dxcisense.h index 661de1680a..6b6dbd336e 100644 --- a/include/dxc/dxcisense.h +++ b/include/dxc/dxcisense.h @@ -12,7 +12,7 @@ #ifndef __DXC_ISENSE__ #define __DXC_ISENSE__ -#include "dxcapi.h" +#include "dxc/dxcapi.h" #ifndef _WIN32 #include "WinAdapter.h" #endif @@ -943,7 +943,7 @@ struct IDxcCompletionString : public IUnknown { // CLSID_DxcIntelliSense is not visible externally (this is OK in C, since const // is not by default static in C) -#ifdef _MSC_VER +#ifdef _WIN32 #define CLSID_SCOPE __declspec(selectany) extern #else #define CLSID_SCOPE diff --git a/include/dxc/dxctools.h b/include/dxc/dxctools.h index bfdc0d86f7..bacc20c156 100644 --- a/include/dxc/dxctools.h +++ b/include/dxc/dxctools.h @@ -42,7 +42,7 @@ struct IDxcRewriter : public IUnknown { IDxcOperationResult **ppResult) = 0; }; -#ifdef _MSC_VER +#ifdef _WIN32 #define CLSID_SCOPE __declspec(selectany) extern #else #define CLSID_SCOPE diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index 957707f427..f5a49a4cf6 100644 --- a/include/llvm/ADT/StringRef.h +++ b/include/llvm/ADT/StringRef.h @@ -12,6 +12,7 @@ #include #include +#include #include #include #include diff --git a/include/llvm/Object/COFF.h b/include/llvm/Object/COFF.h index 025a9dbc6b..18e01296b0 100644 --- a/include/llvm/Object/COFF.h +++ b/include/llvm/Object/COFF.h @@ -258,6 +258,8 @@ struct coff_symbol_generic { support::ulittle32_t Value; }; +using namespace COFF; + class COFFSymbolRef { public: COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS), CS32(nullptr) {} @@ -326,7 +328,7 @@ class COFFSymbolRef { uint8_t getBaseType() const { return getType() & 0x0F; } uint8_t getComplexType() const { - return (getType() & 0xF0) >> COFF::SCT_COMPLEX_TYPE_SHIFT; + return (getType() & 0xF0) >> 4; // SCT_COMPLEX_TYPE_SHIFT } bool isAbsolute() const { @@ -334,31 +336,31 @@ class COFFSymbolRef { } bool isExternal() const { - return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL; + return getStorageClass() == IMAGE_SYM_CLASS_EXTERNAL; } bool isCommon() const { - return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED && + return isExternal() && getSectionNumber() == IMAGE_SYM_UNDEFINED && getValue() != 0; } bool isUndefined() const { - return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED && + return isExternal() && getSectionNumber() == IMAGE_SYM_UNDEFINED && getValue() == 0; } bool isWeakExternal() const { - return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL; + return getStorageClass() == IMAGE_SYM_CLASS_WEAK_EXTERNAL; } bool isFunctionDefinition() const { - return isExternal() && getBaseType() == COFF::IMAGE_SYM_TYPE_NULL && - getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION && + return isExternal() && getBaseType() == IMAGE_SYM_TYPE_NULL && + getComplexType() == IMAGE_SYM_DTYPE_FUNCTION && !COFF::isReservedSectionNumber(getSectionNumber()); } bool isFunctionLineInfo() const { - return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION; + return getStorageClass() == IMAGE_SYM_CLASS_FUNCTION; } bool isAnyUndefined() const { @@ -366,27 +368,26 @@ class COFFSymbolRef { } bool isFileRecord() const { - return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE; + return getStorageClass() == IMAGE_SYM_CLASS_FILE; } bool isSection() const { - return getStorageClass() == COFF::IMAGE_SYM_CLASS_SECTION; + return getStorageClass() == IMAGE_SYM_CLASS_SECTION; } bool isSectionDefinition() const { // C++/CLI creates external ABS symbols for non-const appdomain globals. // These are also followed by an auxiliary section definition. - bool isAppdomainGlobal = - getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL && - getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE; - bool isOrdinarySection = getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC; + bool isAppdomainGlobal = getStorageClass() == IMAGE_SYM_CLASS_EXTERNAL && + getSectionNumber() == IMAGE_SYM_ABSOLUTE; + bool isOrdinarySection = getStorageClass() == IMAGE_SYM_CLASS_STATIC; if (!getNumberOfAuxSymbols()) return false; return isAppdomainGlobal || isOrdinarySection; } bool isCLRToken() const { - return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN; + return getStorageClass() == IMAGE_SYM_CLASS_CLR_TOKEN; } private: @@ -411,7 +412,7 @@ struct coff_section { // Returns true if the actual number of relocations is stored in // VirtualAddress field of the first relocation table entry. bool hasExtendedRelocations() const { - return (Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) && + return (Characteristics & IMAGE_SCN_LNK_NRELOC_OVFL) && NumberOfRelocations == UINT16_MAX; } }; diff --git a/include/llvm/Object/RelocVisitor.h b/include/llvm/Object/RelocVisitor.h index d5e4258cb0..519f73a494 100644 --- a/include/llvm/Object/RelocVisitor.h +++ b/include/llvm/Object/RelocVisitor.h @@ -29,6 +29,8 @@ namespace llvm { namespace object { +using namespace COFF; + struct RelocToApply { // The computed value after applying the relevant relocations. int64_t Value; @@ -206,17 +208,17 @@ class RelocVisitor { switch (ObjToVisit.getArch()) { case Triple::x86: switch (RelocType) { - case COFF::IMAGE_REL_I386_SECREL: + case IMAGE_REL_I386_SECREL: return visitCOFF_I386_SECREL(R, Value); - case COFF::IMAGE_REL_I386_DIR32: + case IMAGE_REL_I386_DIR32: return visitCOFF_I386_DIR32(R, Value); } break; case Triple::x86_64: switch (RelocType) { - case COFF::IMAGE_REL_AMD64_SECREL: + case IMAGE_REL_AMD64_SECREL: return visitCOFF_AMD64_SECREL(R, Value); - case COFF::IMAGE_REL_AMD64_ADDR64: + case IMAGE_REL_AMD64_ADDR64: return visitCOFF_AMD64_ADDR64(R, Value); } break; diff --git a/include/llvm/Support/COFF.h b/include/llvm/Support/COFF.h index 3c5ee06969..32e745fc48 100644 --- a/include/llvm/Support/COFF.h +++ b/include/llvm/Support/COFF.h @@ -80,6 +80,7 @@ namespace COFF { uint32_t NumberOfSymbols; }; +#ifndef __MINGW32__ enum MachineTypes { MT_Invalid = 0xffff, @@ -145,6 +146,7 @@ namespace COFF { /// and should be 0. IMAGE_FILE_BYTES_REVERSED_HI = 0x8000 }; +#endif // __MINGW32__ struct symbol { char Name[NameSize]; @@ -155,6 +157,7 @@ namespace COFF { uint8_t NumberOfAuxSymbols; }; +#ifndef __MINGW32__ enum SymbolSectionNumber : int32_t { IMAGE_SYM_DEBUG = -2, IMAGE_SYM_ABSOLUTE = -1, @@ -226,6 +229,7 @@ namespace COFF { /// Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT)) SCT_COMPLEX_TYPE_SHIFT = 4 }; +#endif // __MINGW32__ enum AuxSymbolType { IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF = 1 @@ -244,6 +248,7 @@ namespace COFF { uint32_t Characteristics; }; +#ifndef __MINGW32__ enum SectionCharacteristics : uint32_t { SC_Invalid = 0xffffffff, @@ -283,6 +288,7 @@ namespace COFF { IMAGE_SCN_MEM_READ = 0x40000000, IMAGE_SCN_MEM_WRITE = 0x80000000 }; +#endif // __MINGW32__ struct relocation { uint32_t VirtualAddress; @@ -290,6 +296,7 @@ namespace COFF { uint16_t Type; }; +#ifndef __MINGW32__ enum RelocationTypeI386 { IMAGE_REL_I386_ABSOLUTE = 0x0000, IMAGE_REL_I386_DIR16 = 0x0001, @@ -351,6 +358,7 @@ namespace COFF { IMAGE_COMDAT_SELECT_LARGEST, IMAGE_COMDAT_SELECT_NEWEST }; +#endif // __MINGW32__ // Auxiliary Symbol Formats struct AuxiliaryFunctionDefinition { @@ -375,12 +383,14 @@ namespace COFF { uint8_t unused[10]; }; +#ifndef __MINGW32__ /// These are not documented in the spec, but are located in WinNT.h. enum WeakExternalCharacteristics { IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY = 1, IMAGE_WEAK_EXTERN_SEARCH_LIBRARY = 2, IMAGE_WEAK_EXTERN_SEARCH_ALIAS = 3 }; +#endif // __MINGW32__ struct AuxiliarySectionDefinition { uint32_t Length; @@ -541,6 +551,7 @@ namespace COFF { NUM_DATA_DIRECTORIES }; +#ifndef __MINGW32__ enum WindowsSubsystem { IMAGE_SUBSYSTEM_UNKNOWN = 0, ///< An unknown subsystem. IMAGE_SUBSYSTEM_NATIVE = 1, ///< Device drivers and native Windows processes @@ -612,6 +623,7 @@ namespace COFF { IMAGE_REL_BASED_MIPS_JMPADDR16 = 9, IMAGE_REL_BASED_DIR64 = 10 }; +#endif // __MINGW32__ enum ImportType { IMPORT_CODE = 0,