|
| 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. |
| 4 | +// |
| 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. |
| 10 | + |
| 11 | +#include "rustllvm.h" |
| 12 | + |
| 13 | +#include "llvm/ExecutionEngine/SectionMemoryManager.h" |
| 14 | + |
| 15 | +using namespace llvm; |
| 16 | +using namespace llvm::sys; |
| 17 | +using namespace llvm::object; |
| 18 | + |
| 19 | +class RustJITMemoryManager : public SectionMemoryManager |
| 20 | +{ |
| 21 | + typedef SectionMemoryManager Base; |
| 22 | + |
| 23 | + const void *morestack; |
| 24 | + |
| 25 | + public: |
| 26 | + |
| 27 | + RustJITMemoryManager(const void *morestack_ptr) |
| 28 | + : morestack(morestack_ptr) |
| 29 | + {} |
| 30 | + |
| 31 | + uint64_t getSymbolAddress(const std::string &Name) override |
| 32 | + { |
| 33 | + if (Name == "__morestack" || Name == "___morestack") |
| 34 | + return reinterpret_cast<uint64_t>(morestack); |
| 35 | + |
| 36 | + return Base::getSymbolAddress(Name); |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RustJITMemoryManager, LLVMRustJITMemoryManagerRef) |
| 41 | + |
| 42 | +extern "C" LLVMRustJITMemoryManagerRef LLVMRustCreateJITMemoryManager(void *morestack) |
| 43 | +{ |
| 44 | + return wrap(new RustJITMemoryManager(morestack)); |
| 45 | +} |
| 46 | + |
| 47 | +extern "C" LLVMBool LLVMRustLoadDynamicLibrary(const char *path) |
| 48 | +{ |
| 49 | + std::string err; |
| 50 | + DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(path, &err); |
| 51 | + |
| 52 | + if (!lib.isValid()) |
| 53 | + LLVMRustSetLastError(err.c_str()); |
| 54 | + |
| 55 | + return lib.isValid(); |
| 56 | +} |
| 57 | + |
| 58 | +// Calls LLVMAddModule; |
| 59 | +// exists for consistency with LLVMExecutionEngineRemoveModule |
| 60 | +extern "C" void LLVMExecutionEngineAddModule( |
| 61 | + LLVMExecutionEngineRef eeref, LLVMModuleRef mref) |
| 62 | +{ |
| 63 | + LLVMAddModule(eeref, mref); |
| 64 | +} |
| 65 | + |
| 66 | +// LLVMRemoveModule exists in LLVM's C bindings, |
| 67 | +// but it requires pointless parameters |
| 68 | +extern "C" LLVMBool LLVMExecutionEngineRemoveModule( |
| 69 | + LLVMExecutionEngineRef eeref, LLVMModuleRef mref) |
| 70 | +{ |
| 71 | + ExecutionEngine *ee = unwrap(eeref); |
| 72 | + Module *m = unwrap(mref); |
| 73 | + |
| 74 | + return ee->removeModule(m); |
| 75 | +} |
| 76 | + |
| 77 | +extern "C" LLVMExecutionEngineRef LLVMBuildExecutionEngine( |
| 78 | + LLVMModuleRef mod, LLVMRustJITMemoryManagerRef mref) |
| 79 | +{ |
| 80 | + // These are necessary for code generation to work properly. |
| 81 | + InitializeNativeTarget(); |
| 82 | + InitializeNativeTargetAsmPrinter(); |
| 83 | + InitializeNativeTargetAsmParser(); |
| 84 | + |
| 85 | + std::unique_ptr<Module> m(unwrap(mod)); |
| 86 | + RustJITMemoryManager *mm = unwrap(mref); |
| 87 | + |
| 88 | + std::string error_str; |
| 89 | + TargetOptions options; |
| 90 | + |
| 91 | + options.JITEmitDebugInfo = true; |
| 92 | + options.NoFramePointerElim = true; |
| 93 | + |
| 94 | + ExecutionEngine *ee = EngineBuilder(std::move(m)) |
| 95 | + .setEngineKind(EngineKind::JIT) |
| 96 | + .setErrorStr(&error_str) |
| 97 | + .setMCJITMemoryManager(mm) |
| 98 | + .setTargetOptions(options) |
| 99 | + .create(); |
| 100 | + |
| 101 | + if (!ee) |
| 102 | + LLVMRustSetLastError(error_str.c_str()); |
| 103 | + |
| 104 | + return wrap(ee); |
| 105 | +} |
| 106 | + |
| 107 | +extern "C" void LLVMExecutionEngineFinalizeObject(LLVMExecutionEngineRef eeref) |
| 108 | +{ |
| 109 | + ExecutionEngine *ee = unwrap(eeref); |
| 110 | + |
| 111 | + ee->finalizeObject(); |
| 112 | +} |
0 commit comments