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
56extern "C" WTF_EXPORT_PRIVATE JSC::LLVMAPI* initializeAndGetJSCLLVMAPI(void (*)(const char*, ...));
57
58static void llvmCrash(const char* reason)
59{
60    g_llvmTrapCallback("LLVM fatal error: %s", reason);
61}
62
63extern "C" JSC::LLVMAPI* initializeAndGetJSCLLVMAPI(void (*callback)(const char*, ...))
64{
65    g_llvmTrapCallback = callback;
66
67    LLVMInstallFatalErrorHandler(llvmCrash);
68
69    if (!LLVMStartMultithreaded())
70        callback("Could not start LLVM multithreading");
71
72    LLVMLinkInMCJIT();
73
74    // You think you want to call LLVMInitializeNativeTarget()? Think again. This presumes that
75    // LLVM was ./configured correctly, which won't be the case in cross-compilation situations.
76
77#if CPU(X86_64)
78    LLVMInitializeX86TargetInfo();
79    LLVMInitializeX86Target();
80    LLVMInitializeX86TargetMC();
81    LLVMInitializeX86AsmPrinter();
82    LLVMInitializeX86Disassembler();
83#elif CPU(ARM64)
84    LLVMInitializeARM64TargetInfo();
85    LLVMInitializeARM64Target();
86    LLVMInitializeARM64TargetMC();
87    LLVMInitializeARM64AsmPrinter();
88    LLVMInitializeARM64Disassembler();
89#else
90    UNREACHABLE_FOR_PLATFORM();
91#endif
92
93    const char* args[] = {
94        "llvmForJSC.dylib",
95        "-enable-stackmap-liveness=true",
96        "-enable-patchpoint-liveness=true"
97    };
98    llvm::cl::ParseCommandLineOptions(sizeof(args) / sizeof(const char*), args);
99
100    JSC::LLVMAPI* result = new JSC::LLVMAPI;
101
102#define LLVM_API_FUNCTION_ASSIGNMENT(returnType, name, signature) \
103    result->name = LLVM##name;
104    FOR_EACH_LLVM_API_FUNCTION(LLVM_API_FUNCTION_ASSIGNMENT);
105#undef LLVM_API_FUNCTION_ASSIGNMENT
106
107    return result;
108}
109
110#else
111
112// Create a dummy initializeAndGetJSCLLVMAPI to placate the build system.
113extern "C" WTF_EXPORT_PRIVATE void initializeAndGetJSCLLVMAPI();
114extern "C" void initializeAndGetJSCLLVMAPI() { }
115
116#endif // HAVE(LLVM)
117