Skip to content

Commit 198441b

Browse files
committed
stdlib/msvc: Runtime with MSVC library
This patch is for libswiftCore.lib, linking with the library set of Visual Studio 2015. Clang with the option -fms-extension is used to build.
1 parent 73202fc commit 198441b

File tree

10 files changed

+187
-31
lines changed

10 files changed

+187
-31
lines changed

include/swift/Runtime/HeapObject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ struct TwoWordPair {
106106
// in registers, so cram the result into an unsigned long long.
107107
// Use an enum class with implicit conversions so we don't dirty C callers
108108
// too much.
109-
#if __arm__ || __i386__ || defined(__CYGWIN__)
110-
#if defined(__CYGWIN__)
109+
#if __arm__ || __i386__ || defined(__CYGWIN__) || defined(_MSC_VER)
110+
#if defined(__CYGWIN__) || defined(_MSC_VER)
111111
enum class Return : unsigned __int128 {};
112112
#else
113113
enum class Return : unsigned long long {};

stdlib/public/runtime/Errors.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@
1818
#include <stdlib.h>
1919
#include <string.h>
2020
#include <stdint.h>
21+
#if defined(_MSC_VER)
22+
#include <io.h>
23+
#else
2124
#include <unistd.h>
25+
#endif
26+
#if !defined(_MSC_VER)
27+
#include <pthread.h>
28+
#endif
2229
#include <stdarg.h>
2330
#include "swift/Runtime/Debug.h"
2431
#include "swift/Runtime/Mutex.h"
2532
#include "swift/Basic/Demangle.h"
2633
#include "swift/Basic/LLVM.h"
2734
#include "llvm/ADT/StringRef.h"
28-
35+
#if !defined(_MSC_VER)
2936
#include <cxxabi.h>
30-
31-
#if !defined(__CYGWIN__) && !defined(__ANDROID__)
37+
#endif
38+
#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(_MSC_VER)
3239

3340
// execinfo.h is not available on Android. Checks in this file ensure that
3441
// fatalError behaves as expected, but without stack traces.
@@ -50,7 +57,7 @@ enum: uint32_t {
5057

5158
using namespace swift;
5259

53-
#if !defined(__CYGWIN__) && !defined(__ANDROID__)
60+
#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(_MSC_VER)
5461

5562
static bool getSymbolNameAddr(llvm::StringRef libraryName, Dl_info dlinfo,
5663
std::string &symbolName, uintptr_t &addrOut) {
@@ -187,11 +194,16 @@ reportOnCrash(uint32_t flags, const char *message)
187194
static void
188195
reportNow(uint32_t flags, const char *message)
189196
{
197+
#if defined(_MSC_VER)
198+
#define STDERR_FILENO 2
199+
_write(STDERR_FILENO, message, strlen(message));
200+
#else
190201
write(STDERR_FILENO, message, strlen(message));
202+
#endif
191203
#ifdef __APPLE__
192204
asl_log(NULL, NULL, ASL_LEVEL_ERR, "%s", message);
193205
#endif
194-
#if !defined(__CYGWIN__) && !defined(__ANDROID__)
206+
#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(_MSC_VER)
195207
if (flags & FatalErrorFlags::ReportBacktrace) {
196208
fputs("Current stack trace:\n", stderr);
197209
constexpr unsigned maxSupportedStackDepth = 128;
@@ -222,7 +234,13 @@ swift::fatalError(uint32_t flags, const char *format, ...)
222234
va_start(args, format);
223235

224236
char *log;
237+
#if defined(_MSC_VER)
238+
int len = _vscprintf(format, args) + 1;
239+
log = reinterpret_cast<char *>(malloc(len));
240+
vsprintf(log, format, args);
241+
#else
225242
vasprintf(&log, format, args);
243+
#endif
226244

227245
swift_reportError(flags, log);
228246
abort();

stdlib/public/runtime/Metadata.cpp

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@
2828
#include <condition_variable>
2929
#include <new>
3030
#include <cctype>
31+
#if defined(_MSC_VER)
32+
// Avoid defining macro max(), min() which conflict with std::max(), std::min()
33+
#define NOMINMAX
34+
#include <windows.h>
35+
#else
3136
#include <sys/mman.h>
3237
#include <unistd.h>
38+
#endif
3339
#include "llvm/ADT/DenseMap.h"
3440
#include "llvm/ADT/Hashing.h"
3541
#include "ErrorObject.h"
@@ -56,19 +62,45 @@
5662
using namespace swift;
5763
using namespace metadataimpl;
5864

65+
// allocate memory up to a nearby page boundary
66+
static void *swift_allocPage(size_t size) {
67+
#if defined(_MSC_VER)
68+
auto mem = VirtualAlloc(
69+
nullptr, size, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
70+
#else
71+
auto mem = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
72+
VM_TAG_FOR_SWIFT_METADATA, 0);
73+
if (mem == MAP_FAILED)
74+
mem = nullptr;
75+
#endif
76+
return mem;
77+
}
78+
79+
// free memory allocated by swift_allocPage()
80+
// On success, swift_freePage() returns 0, on failure -1
81+
static int swift_freePage(void *addr, size_t size) {
82+
#if defined(_MSC_VER)
83+
return VirtualFree(addr, 0, MEM_RELEASE) == 0 ? -1 : 0;
84+
#else
85+
return munmap(addr, size);
86+
#endif
87+
}
88+
5989
void *MetadataAllocator::alloc(size_t size) {
6090
#if defined(__APPLE__)
6191
const uintptr_t PageSizeMask = vm_page_mask;
92+
#elif defined(_MSC_VER)
93+
SYSTEM_INFO SystemInfo;
94+
GetSystemInfo(&SystemInfo);
95+
const uintptr_t PageSizeMask = SystemInfo.dwPageSize - 1;
6296
#else
6397
static const uintptr_t PageSizeMask = sysconf(_SC_PAGESIZE) - 1;
6498
#endif
6599
// If the requested size is a page or larger, map page(s) for it
66100
// specifically.
67101
if (LLVM_UNLIKELY(size > PageSizeMask)) {
68-
auto mem = mmap(nullptr, (size + PageSizeMask) & ~PageSizeMask,
69-
PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
70-
VM_TAG_FOR_SWIFT_METADATA, 0);
71-
if (mem == MAP_FAILED)
102+
void *mem = swift_allocPage((size + PageSizeMask) & ~PageSizeMask);
103+
if (!mem)
72104
crash("unable to allocate memory for metadata cache");
73105
return mem;
74106
}
@@ -83,13 +115,9 @@ void *MetadataAllocator::alloc(size_t size) {
83115
if (LLVM_UNLIKELY(((uintptr_t)next & ~PageSizeMask)
84116
!= (((uintptr_t)end & ~PageSizeMask)))) {
85117
// Allocate a new page if we haven't already.
86-
allocation = mmap(nullptr, PageSizeMask + 1,
87-
PROT_READ|PROT_WRITE,
88-
MAP_ANON|MAP_PRIVATE,
89-
VM_TAG_FOR_SWIFT_METADATA,
90-
/*offset*/ 0);
118+
allocation = swift_allocPage(PageSizeMask + 1);
91119

92-
if (allocation == MAP_FAILED)
120+
if (!allocation)
93121
crash("unable to allocate memory for metadata cache");
94122

95123
next = (char*) allocation;
@@ -107,7 +135,7 @@ void *MetadataAllocator::alloc(size_t size) {
107135
// This potentially causes us to perform multiple mmaps under contention,
108136
// but it keeps the fast path pristine.
109137
if (allocation) {
110-
munmap(allocation, PageSizeMask + 1);
138+
swift_freePage(allocation, PageSizeMask + 1);
111139
}
112140
}
113141
}

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
#include <link.h>
3535
#endif
3636

37+
#if defined(_MSC_VER)
38+
#include <windows.h>
39+
#else
40+
#include <dlfcn.h>
41+
#endif
42+
3743
using namespace swift;
3844
using namespace Demangle;
3945

@@ -47,7 +53,7 @@ using namespace Demangle;
4753
#define SWIFT_TYPE_METADATA_SECTION "__swift2_types"
4854
#elif defined(__ELF__)
4955
#define SWIFT_TYPE_METADATA_SECTION ".swift2_type_metadata_start"
50-
#elif defined(__CYGWIN__)
56+
#elif defined(__CYGWIN__) || defined(_MSC_VER)
5157
#define SWIFT_TYPE_METADATA_SECTION ".sw2tymd"
5258
#endif
5359

stdlib/public/runtime/Private.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ namespace swift {
172172
dest = newValue;
173173
}
174174

175-
#if defined(__CYGWIN__)
175+
#if defined(__CYGWIN__) || defined(_MSC_VER)
176176
struct dl_phdr_info {
177177
void *dlpi_addr;
178178
const char *dlpi_name;
@@ -183,6 +183,8 @@ namespace swift {
183183
void *data);
184184
uint8_t *_swift_getSectionDataPE(void *handle, const char *sectionName,
185185
unsigned long *sectionSize);
186+
#endif
187+
#if defined(__CYGWIN__)
186188
void _swift_once_f(uintptr_t *predicate, void *context,
187189
void (*function)(void *));
188190
#endif

stdlib/public/runtime/ProtocolConformance.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
#include <link.h>
3030
#endif
3131

32+
#if defined(_MSC_VER)
33+
#include <windows.h>
34+
#else
3235
#include <dlfcn.h>
36+
#endif
3337

3438
using namespace swift;
3539

@@ -144,7 +148,7 @@ const {
144148
#define SWIFT_PROTOCOL_CONFORMANCES_SECTION "__swift2_proto"
145149
#elif defined(__ELF__)
146150
#define SWIFT_PROTOCOL_CONFORMANCES_SECTION ".swift2_protocol_conformances_start"
147-
#elif defined(__CYGWIN__)
151+
#elif defined(__CYGWIN__) || defined(_MSC_VER)
148152
#define SWIFT_PROTOCOL_CONFORMANCES_SECTION ".sw2prtc"
149153
#endif
150154

@@ -396,17 +400,25 @@ void swift::_swift_initializeCallbacksToInspectDylib(
396400
// rdar://problem/19045112
397401
dl_iterate_phdr(_addImageProtocolConformances, &inspectArgs);
398402
}
399-
#elif defined(__CYGWIN__)
403+
#elif defined(__CYGWIN__) || defined(_MSC_VER)
400404
static int _addImageProtocolConformances(struct dl_phdr_info *info,
401405
size_t size, void *data) {
402406
InspectArgs *inspectArgs = (InspectArgs *)data;
403407
// inspectArgs contains addImage*Block function and the section name
408+
#if defined(_MSC_VER)
409+
HMODULE handle;
404410

411+
if (!info->dlpi_name || info->dlpi_name[0] == '\0')
412+
handle = GetModuleHandle(nullptr);
413+
else
414+
handle = GetModuleHandle(info->dlpi_name);
415+
#else
405416
void *handle;
406-
if (!info->dlpi_name || info->dlpi_name[0] == '\0') {
417+
if (!info->dlpi_name || info->dlpi_name[0] == '\0')
407418
handle = dlopen(nullptr, RTLD_LAZY);
408-
} else
419+
else
409420
handle = dlopen(info->dlpi_name, RTLD_LAZY | RTLD_NOLOAD);
421+
#endif
410422

411423
unsigned long conformancesSize;
412424
const uint8_t *conformances =
@@ -416,7 +428,11 @@ static int _addImageProtocolConformances(struct dl_phdr_info *info,
416428
if (conformances)
417429
inspectArgs->fnAddImageBlock(conformances, conformancesSize);
418430

431+
#if defined(_MSC_VER)
432+
FreeLibrary(handle);
433+
#else
419434
dlclose(handle);
435+
#endif
420436
return 0;
421437
}
422438

stdlib/public/runtime/Reflection.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <cstring>
2525
#include <new>
2626
#include <string>
27+
#if !defined(_MSC_VER)
28+
#include <dlfcn.h>
29+
#endif
2730

2831
#if SWIFT_OBJC_INTEROP
2932
#include "swift/Runtime/ObjCBridge.h"

stdlib/public/runtime/SwiftObject.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
#include "../SwiftShims/RuntimeShims.h"
3535
#include "Private.h"
3636
#include "swift/Runtime/Debug.h"
37+
#if !defined(_MSC_VER)
3738
#include <dlfcn.h>
39+
#endif
3840
#include <stdio.h>
3941
#include <stdlib.h>
4042
#include <unordered_map>

stdlib/public/stubs/LibcShims.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212

1313
#include <random>
1414
#include <type_traits>
15+
#if defined(_MSC_VER)
16+
#include <io.h>
17+
#else
1518
#include <unistd.h>
19+
#endif
1620
#include <stdlib.h>
1721
#include <stdio.h>
1822
#include <string.h>
1923
#include "../SwiftShims/LibcShims.h"
24+
#include "llvm/Support/DataTypes.h"
2025

2126
using namespace swift;
2227

@@ -28,7 +33,11 @@ void swift::_swift_stdlib_free(void *ptr) {
2833
}
2934

3035
int swift::_swift_stdlib_putchar_unlocked(int c) {
36+
#if defined(_MSC_VER)
37+
return _putc_nolock(c, stdout);
38+
#else
3139
return putchar_unlocked(c);
40+
#endif
3241
}
3342

3443
__swift_size_t swift::_swift_stdlib_fwrite_stdout(const void *ptr,
@@ -70,6 +79,11 @@ size_t swift::_swift_stdlib_malloc_size(const void *ptr) {
7079
size_t swift::_swift_stdlib_malloc_size(const void *ptr) {
7180
return malloc_usable_size(const_cast<void *>(ptr));
7281
}
82+
#elif defined(_MSC_VER)
83+
#include <malloc.h>
84+
size_t _swift_stdlib_malloc_size(const void *ptr) {
85+
return _msize(const_cast<void *>(ptr));
86+
}
7387
#elif defined(__FreeBSD__)
7488
#include <malloc_np.h>
7589
size_t swift::_swift_stdlib_malloc_size(const void *ptr) {

0 commit comments

Comments
 (0)