]> git.saurik.com Git - apple/javascriptcore.git/blob - llvm/library/LLVMExports.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / llvm / library / LLVMExports.cpp
1 /*
2 * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config_llvm.h"
27
28 #if HAVE(LLVM)
29
30 #include "LLVMAPI.h"
31 #include "LLVMTrapCallback.h"
32
33 // Include some extra LLVM C++ headers. This is the only place where including LLVM C++
34 // headers is OK, since this module lives inside of the LLVM dylib we make and so can see
35 // otherwise private things. But to include those headers, we need to do some weird things
36 // first. Anyway, we should keep LLVM C++ includes to a minimum if possible.
37
38 #define __STDC_LIMIT_MACROS
39 #define __STDC_CONSTANT_MACROS
40
41 #if COMPILER(CLANG)
42 #pragma clang diagnostic push
43 #pragma clang diagnostic ignored "-Wmissing-noreturn"
44 #pragma clang diagnostic ignored "-Wunused-parameter"
45 #endif // COMPILER(CLANG)
46
47 #include <llvm/Support/CommandLine.h>
48
49 #if COMPILER(CLANG)
50 #pragma clang diagnostic pop
51 #endif // COMPILER(CLANG)
52
53 #undef __STDC_LIMIT_MACROS
54 #undef __STDC_CONSTANT_MACROS
55
56 static void llvmCrash(const char*) NO_RETURN;
57 extern "C" WTF_EXPORT_PRIVATE JSC::LLVMAPI* initializeAndGetJSCLLVMAPI(
58 void (*)(const char*, ...) NO_RETURN, bool* enableFastISel);
59
60 static void llvmCrash(const char* reason)
61 {
62 g_llvmTrapCallback("LLVM fatal error: %s", reason);
63 }
64
65 template<typename... Args>
66 void initCommandLine(Args... args)
67 {
68 const char* theArgs[] = { args... };
69 llvm::cl::ParseCommandLineOptions(sizeof(theArgs) / sizeof(const char*), theArgs);
70 }
71
72 extern "C" JSC::LLVMAPI* initializeAndGetJSCLLVMAPI(
73 void (*callback)(const char*, ...) NO_RETURN,
74 bool* enableFastISel)
75 {
76 g_llvmTrapCallback = callback;
77
78 LLVMInstallFatalErrorHandler(llvmCrash);
79
80 if (!LLVMStartMultithreaded())
81 callback("Could not start LLVM multithreading");
82
83 LLVMLinkInMCJIT();
84
85 // You think you want to call LLVMInitializeNativeTarget()? Think again. This presumes that
86 // LLVM was ./configured correctly, which won't be the case in cross-compilation situations.
87
88 #if CPU(X86_64)
89 LLVMInitializeX86TargetInfo();
90 LLVMInitializeX86Target();
91 LLVMInitializeX86TargetMC();
92 LLVMInitializeX86AsmPrinter();
93 LLVMInitializeX86Disassembler();
94 #elif CPU(ARM64)
95 #if (__IPHONE_OS_VERSION_MIN_REQUIRED > 80200) || OS(LINUX)
96 LLVMInitializeAArch64TargetInfo();
97 LLVMInitializeAArch64Target();
98 LLVMInitializeAArch64TargetMC();
99 LLVMInitializeAArch64AsmPrinter();
100 LLVMInitializeAArch64Disassembler();
101 #else
102 LLVMInitializeARM64TargetInfo();
103 LLVMInitializeARM64Target();
104 LLVMInitializeARM64TargetMC();
105 LLVMInitializeARM64AsmPrinter();
106 LLVMInitializeARM64Disassembler();
107 #endif
108 #else
109 UNREACHABLE_FOR_PLATFORM();
110 #endif
111
112 #if LLVM_VERSION_MAJOR >= 4 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6)
113 // It's OK to have fast ISel, if it was requested.
114 #else
115 // We don't have enough support for fast ISel. Disable it.
116 *enableFastISel = false;
117 #endif
118
119 if (*enableFastISel)
120 initCommandLine("llvmForJSC.dylib", "-enable-misched=false", "-regalloc=basic");
121 else
122 initCommandLine("llvmForJSC.dylib", "-enable-patchpoint-liveness=true");
123
124 JSC::LLVMAPI* result = new JSC::LLVMAPI;
125
126 // Initialize the whole thing to null.
127 memset(result, 0, sizeof(*result));
128
129 #define LLVM_API_FUNCTION_ASSIGNMENT(returnType, name, signature) \
130 result->name = LLVM##name;
131 FOR_EACH_LLVM_API_FUNCTION(LLVM_API_FUNCTION_ASSIGNMENT);
132 #undef LLVM_API_FUNCTION_ASSIGNMENT
133
134 // Handle conditionally available functions.
135 #if LLVM_VERSION_MAJOR >= 4 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6)
136 result->AddLowerSwitchPass = LLVMAddLowerSwitchPass;
137 #endif
138
139 return result;
140 }
141
142 #else
143
144 // Create a dummy initializeAndGetJSCLLVMAPI to placate the build system.
145 extern "C" WTF_EXPORT_PRIVATE void initializeAndGetJSCLLVMAPI();
146 extern "C" void initializeAndGetJSCLLVMAPI() { }
147
148 #endif // HAVE(LLVM)