Lang Hames | 633ea07 | 2020-03-14 14:16:42 -0700 | [diff] [blame] | 1 | //===--------------- OrcV2CBindings.cpp - C bindings OrcV2 APIs -----------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm-c/Orc.h" |
| 10 | #include "llvm-c/TargetMachine.h" |
| 11 | |
| 12 | #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" |
| 13 | #include "llvm/ExecutionEngine/Orc/LLJIT.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | using namespace llvm::orc; |
| 17 | |
| 18 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeContext, |
| 19 | LLVMOrcThreadSafeContextRef) |
| 20 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeModule, LLVMOrcThreadSafeModuleRef) |
| 21 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJIT, LLVMOrcLLJITRef) |
| 22 | |
| 23 | LLVMOrcThreadSafeContextRef LLVMOrcCreateNewThreadSafeContext() { |
| 24 | return wrap(new ThreadSafeContext(std::make_unique<LLVMContext>())); |
| 25 | } |
| 26 | |
| 27 | LLVMContextRef |
| 28 | LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx) { |
| 29 | return wrap(unwrap(TSCtx)->getContext()); |
| 30 | } |
| 31 | |
| 32 | void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx) { |
| 33 | delete unwrap(TSCtx); |
| 34 | } |
| 35 | |
| 36 | LLVMOrcThreadSafeModuleRef |
| 37 | LLVMOrcCreateNewThreadSafeModule(LLVMModuleRef M, |
| 38 | LLVMOrcThreadSafeContextRef TSCtx) { |
| 39 | return wrap( |
| 40 | new ThreadSafeModule(std::unique_ptr<Module>(unwrap(M)), *unwrap(TSCtx))); |
| 41 | } |
| 42 | |
| 43 | void LLVMOrcDisposeThreadSafeModule(LLVMOrcThreadSafeModuleRef TSM) { |
| 44 | delete unwrap(TSM); |
| 45 | } |
| 46 | |
| 47 | LLVMErrorRef LLVMOrcCreateDefaultLLJIT(LLVMOrcLLJITRef *Result) { |
| 48 | auto J = LLJITBuilder().create(); |
| 49 | |
| 50 | if (!J) { |
| 51 | Result = 0; |
| 52 | return wrap(J.takeError()); |
| 53 | } |
| 54 | |
| 55 | *Result = wrap(J->release()); |
| 56 | return LLVMErrorSuccess; |
| 57 | } |
| 58 | |
| 59 | LLVMErrorRef LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J) { |
| 60 | delete unwrap(J); |
| 61 | return LLVMErrorSuccess; |
| 62 | } |
| 63 | |
| 64 | LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J, |
| 65 | LLVMOrcThreadSafeModuleRef TSM) { |
| 66 | return wrap(unwrap(J)->addIRModule(std::move(*unwrap(TSM)))); |
| 67 | } |
| 68 | |
| 69 | LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J, |
| 70 | LLVMOrcJITTargetAddress *Result, |
| 71 | const char *Name) { |
| 72 | auto Sym = unwrap(J)->lookup(Name); |
| 73 | if (!Sym) { |
| 74 | *Result = 0; |
| 75 | return wrap(Sym.takeError()); |
| 76 | } |
| 77 | |
| 78 | *Result = Sym->getAddress(); |
| 79 | return LLVMErrorSuccess; |
| 80 | } |