1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
16 using namespace llvm::sys;
17 using namespace llvm::object;
19 class RustJITMemoryManager : public SectionMemoryManager
21 typedef SectionMemoryManager Base;
25 RustJITMemoryManager() {}
27 uint64_t getSymbolAddress(const std::string &Name) override
29 return Base::getSymbolAddress(Name);
33 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RustJITMemoryManager, LLVMRustJITMemoryManagerRef)
35 extern "C" LLVMBool LLVMRustLoadDynamicLibrary(const char *path)
38 DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(path, &err);
41 LLVMRustSetLastError(err.c_str());
46 // Calls LLVMAddModule;
47 // exists for consistency with LLVMExecutionEngineRemoveModule
48 extern "C" void LLVMExecutionEngineAddModule(
49 LLVMExecutionEngineRef eeref, LLVMModuleRef mref)
52 // On Windows, MCJIT must generate ELF objects
53 std::string target = getProcessTriple();
55 target = Triple::normalize(target);
56 unwrap(mref)->setTargetTriple(target);
58 LLVMAddModule(eeref, mref);
61 // LLVMRemoveModule exists in LLVM's C bindings,
62 // but it requires pointless parameters
63 extern "C" LLVMBool LLVMExecutionEngineRemoveModule(
64 LLVMExecutionEngineRef eeref, LLVMModuleRef mref)
66 ExecutionEngine *ee = unwrap(eeref);
67 Module *m = unwrap(mref);
69 return ee->removeModule(m);
72 extern "C" LLVMExecutionEngineRef LLVMBuildExecutionEngine(LLVMModuleRef mod)
74 // These are necessary for code generation to work properly.
75 InitializeNativeTarget();
76 InitializeNativeTargetAsmPrinter();
77 InitializeNativeTargetAsmParser();
80 // On Windows, MCJIT must generate ELF objects
81 std::string target = getProcessTriple();
83 target = Triple::normalize(target);
84 unwrap(mod)->setTargetTriple(target);
87 std::string error_str;
88 TargetOptions options;
90 RustJITMemoryManager *mm = new RustJITMemoryManager;
93 #if LLVM_VERSION_MINOR >= 6
94 EngineBuilder(std::unique_ptr<Module>(unwrap(mod)))
95 .setMCJITMemoryManager(std::unique_ptr<RustJITMemoryManager>(mm))
97 EngineBuilder(unwrap(mod))
98 .setMCJITMemoryManager(mm)
100 .setEngineKind(EngineKind::JIT)
101 .setErrorStr(&error_str)
102 .setTargetOptions(options)
106 LLVMRustSetLastError(error_str.c_str());
111 extern "C" void LLVMExecutionEngineFinalizeObject(LLVMExecutionEngineRef eeref)
113 ExecutionEngine *ee = unwrap(eeref);
115 ee->finalizeObject();