[PowerPC] Materialize more constants with CR-field set in late peephole
[llvm-core.git] / lib / Support / Windows / WindowsSupport.h
blobc2fd6bb982d42ae0a9f4899874c96678b470ae80
1 //===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines things specific to Windows implementations. In addition to
11 // providing some helpers for working with win32 APIs, this header wraps
12 // <windows.h> with some portability macros. Always include WindowsSupport.h
13 // instead of including <windows.h> directly.
15 //===----------------------------------------------------------------------===//
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic Win32 code that
19 //=== is guaranteed to work on *all* Win32 variants.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
23 #define LLVM_SUPPORT_WINDOWSSUPPORT_H
25 // mingw-w64 tends to define it as 0x0502 in its headers.
26 #undef _WIN32_WINNT
27 #undef _WIN32_IE
29 // Require at least Windows 7 API.
30 #define _WIN32_WINNT 0x0601
31 #define _WIN32_IE 0x0800 // MinGW at it again. FIXME: verify if still needed.
32 #define WIN32_LEAN_AND_MEAN
33 #ifndef NOMINMAX
34 #define NOMINMAX
35 #endif
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/StringExtras.h"
39 #include "llvm/ADT/StringRef.h"
40 #include "llvm/ADT/Twine.h"
41 #include "llvm/Config/config.h" // Get build system configuration settings
42 #include "llvm/Support/Chrono.h"
43 #include "llvm/Support/Compiler.h"
44 #include <cassert>
45 #include <string>
46 #include <system_error>
47 #include <windows.h>
49 // Must be included after windows.h
50 #include <wincrypt.h>
52 /// Determines if the program is running on Windows 8 or newer. This
53 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
54 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
55 /// yet have VersionHelpers.h, so we have our own helper.
56 inline bool RunningWindows8OrGreater() {
57 // Windows 8 is version 6.2, service pack 0.
58 OSVERSIONINFOEXW osvi = {};
59 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
60 osvi.dwMajorVersion = 6;
61 osvi.dwMinorVersion = 2;
62 osvi.wServicePackMajor = 0;
64 DWORDLONG Mask = 0;
65 Mask = VerSetConditionMask(Mask, VER_MAJORVERSION, VER_GREATER_EQUAL);
66 Mask = VerSetConditionMask(Mask, VER_MINORVERSION, VER_GREATER_EQUAL);
67 Mask = VerSetConditionMask(Mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
69 return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION |
70 VER_SERVICEPACKMAJOR,
71 Mask) != FALSE;
74 inline bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix) {
75 if (!ErrMsg)
76 return true;
77 char *buffer = NULL;
78 DWORD LastError = GetLastError();
79 DWORD R = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
80 FORMAT_MESSAGE_FROM_SYSTEM |
81 FORMAT_MESSAGE_MAX_WIDTH_MASK,
82 NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
83 if (R)
84 *ErrMsg = prefix + ": " + buffer;
85 else
86 *ErrMsg = prefix + ": Unknown error";
87 *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
89 LocalFree(buffer);
90 return R != 0;
93 template <typename HandleTraits>
94 class ScopedHandle {
95 typedef typename HandleTraits::handle_type handle_type;
96 handle_type Handle;
98 ScopedHandle(const ScopedHandle &other); // = delete;
99 void operator=(const ScopedHandle &other); // = delete;
100 public:
101 ScopedHandle()
102 : Handle(HandleTraits::GetInvalid()) {}
104 explicit ScopedHandle(handle_type h)
105 : Handle(h) {}
107 ~ScopedHandle() {
108 if (HandleTraits::IsValid(Handle))
109 HandleTraits::Close(Handle);
112 handle_type take() {
113 handle_type t = Handle;
114 Handle = HandleTraits::GetInvalid();
115 return t;
118 ScopedHandle &operator=(handle_type h) {
119 if (HandleTraits::IsValid(Handle))
120 HandleTraits::Close(Handle);
121 Handle = h;
122 return *this;
125 // True if Handle is valid.
126 explicit operator bool() const {
127 return HandleTraits::IsValid(Handle) ? true : false;
130 operator handle_type() const {
131 return Handle;
135 struct CommonHandleTraits {
136 typedef HANDLE handle_type;
138 static handle_type GetInvalid() {
139 return INVALID_HANDLE_VALUE;
142 static void Close(handle_type h) {
143 ::CloseHandle(h);
146 static bool IsValid(handle_type h) {
147 return h != GetInvalid();
151 struct JobHandleTraits : CommonHandleTraits {
152 static handle_type GetInvalid() {
153 return NULL;
157 struct CryptContextTraits : CommonHandleTraits {
158 typedef HCRYPTPROV handle_type;
160 static handle_type GetInvalid() {
161 return 0;
164 static void Close(handle_type h) {
165 ::CryptReleaseContext(h, 0);
168 static bool IsValid(handle_type h) {
169 return h != GetInvalid();
173 struct RegTraits : CommonHandleTraits {
174 typedef HKEY handle_type;
176 static handle_type GetInvalid() {
177 return NULL;
180 static void Close(handle_type h) {
181 ::RegCloseKey(h);
184 static bool IsValid(handle_type h) {
185 return h != GetInvalid();
189 struct FindHandleTraits : CommonHandleTraits {
190 static void Close(handle_type h) {
191 ::FindClose(h);
195 struct FileHandleTraits : CommonHandleTraits {};
197 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
198 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
199 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
200 typedef ScopedHandle<RegTraits> ScopedRegHandle;
201 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
202 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
204 namespace llvm {
205 template <class T>
206 class SmallVectorImpl;
208 template <class T>
209 typename SmallVectorImpl<T>::const_pointer
210 c_str(SmallVectorImpl<T> &str) {
211 str.push_back(0);
212 str.pop_back();
213 return str.data();
216 namespace sys {
218 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
219 ULARGE_INTEGER TimeInteger;
220 TimeInteger.LowPart = Time.dwLowDateTime;
221 TimeInteger.HighPart = Time.dwHighDateTime;
223 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
224 return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
227 inline TimePoint<> toTimePoint(FILETIME Time) {
228 ULARGE_INTEGER TimeInteger;
229 TimeInteger.LowPart = Time.dwLowDateTime;
230 TimeInteger.HighPart = Time.dwHighDateTime;
232 // Adjust for different epoch
233 TimeInteger.QuadPart -= 11644473600ll * 10000000;
235 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
236 return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
239 inline FILETIME toFILETIME(TimePoint<> TP) {
240 ULARGE_INTEGER TimeInteger;
241 TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
242 TimeInteger.QuadPart += 11644473600ll * 10000000;
244 FILETIME Time;
245 Time.dwLowDateTime = TimeInteger.LowPart;
246 Time.dwHighDateTime = TimeInteger.HighPart;
247 return Time;
250 namespace windows {
251 // Returns command line arguments. Unlike arguments given to main(),
252 // this function guarantees that the returned arguments are encoded in
253 // UTF-8 regardless of the current code page setting.
254 std::error_code GetCommandLineArguments(SmallVectorImpl<const char *> &Args,
255 BumpPtrAllocator &Alloc);
256 } // end namespace windows
257 } // end namespace sys
258 } // end namespace llvm.
260 #endif