blob: 95bdef815937ec61bb8c3553002a2f4f3b48e6b1 [file] [log] [blame]
Lang Hames130a7c42015-10-28 02:40:04 +00001/*===----------- llvm-c/OrcBindings.h - Orc Lib C Iface ---------*- C++ -*-===*\
2|* *|
3|* The LLVM Compiler Infrastructure *|
4|* *|
5|* This file is distributed under the University of Illinois Open Source *|
6|* License. See LICENSE.TXT for details. *|
7|* *|
8|*===----------------------------------------------------------------------===*|
9|* *|
10|* This header declares the C interface to libLLVMOrcJIT.a, which implements *|
11|* JIT compilation of LLVM IR. *|
12|* *|
13|* Many exotic languages can interoperate with C code but have a harder time *|
14|* with C++ due to name mangling. So in addition to C, this interface enables *|
15|* tools written in such languages. *|
16|* *|
17|* Note: This interface is experimental. It is *NOT* stable, and may be *|
18|* changed without warning. *|
19|* *|
20\*===----------------------------------------------------------------------===*/
21
22#ifndef LLVM_C_ORCBINDINGS_H
23#define LLVM_C_ORCBINDINGS_H
24
25#include "llvm-c/Object.h"
Lang Hames130a7c42015-10-28 02:40:04 +000026#include "llvm-c/TargetMachine.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
Lang Hamescd9d49b2017-06-23 23:25:28 +000032typedef struct LLVMOpaqueSharedModule *LLVMSharedModuleRef;
Lang Hames130a7c42015-10-28 02:40:04 +000033typedef struct LLVMOrcOpaqueJITStack *LLVMOrcJITStackRef;
Lang Hames0976cee2018-02-09 02:30:40 +000034typedef uint64_t LLVMOrcModuleHandle;
Lang Hames130a7c42015-10-28 02:40:04 +000035typedef uint64_t LLVMOrcTargetAddress;
Lang Hames1fa0e0e2016-04-25 21:21:20 +000036typedef uint64_t (*LLVMOrcSymbolResolverFn)(const char *Name, void *LookupCtx);
Lang Hamesfd6e8dc2015-10-30 03:20:21 +000037typedef uint64_t (*LLVMOrcLazyCompileCallbackFn)(LLVMOrcJITStackRef JITStack,
38 void *CallbackCtx);
Lang Hames130a7c42015-10-28 02:40:04 +000039
Lang Hames1fa0e0e2016-04-25 21:21:20 +000040typedef enum { LLVMOrcErrSuccess = 0, LLVMOrcErrGeneric } LLVMOrcErrorCode;
Lang Hamesef5a0ee2016-04-25 19:56:45 +000041
Lang Hames130a7c42015-10-28 02:40:04 +000042/**
Lang Hamescd9d49b2017-06-23 23:25:28 +000043 * Turn an LLVMModuleRef into an LLVMSharedModuleRef.
44 *
45 * The JIT uses shared ownership for LLVM modules, since it is generally
46 * difficult to know when the JIT will be finished with a module (and the JIT
47 * has no way of knowing when a user may be finished with one).
48 *
49 * Calling this method with an LLVMModuleRef creates a shared-pointer to the
50 * module, and returns a reference to this shared pointer.
51 *
52 * The shared module should be disposed when finished with by calling
53 * LLVMOrcDisposeSharedModule (not LLVMDisposeModule). The Module will be
54 * deleted when the last shared pointer owner relinquishes it.
55 */
56
57LLVMSharedModuleRef LLVMOrcMakeSharedModule(LLVMModuleRef Mod);
58
59/**
60 * Dispose of a shared module.
61 *
62 * The module should not be accessed after this call. The module will be
63 * deleted once all clients (including the JIT itself) have released their
64 * shared pointers.
65 */
66
67void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod);
68
69/**
Lang Hames130a7c42015-10-28 02:40:04 +000070 * Create an ORC JIT stack.
71 *
72 * The client owns the resulting stack, and must call OrcDisposeInstance(...)
73 * to destroy it and free its memory. The JIT stack will take ownership of the
74 * TargetMachine, which will be destroyed when the stack is destroyed. The
75 * client should not attempt to dispose of the Target Machine, or it will result
76 * in a double-free.
77 */
Rafael Espindolae63e0182015-11-03 16:40:37 +000078LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM);
Lang Hames130a7c42015-10-28 02:40:04 +000079
80/**
Lang Hamesef5a0ee2016-04-25 19:56:45 +000081 * Get the error message for the most recent error (if any).
82 *
83 * This message is owned by the ORC JIT Stack and will be freed when the stack
84 * is disposed of by LLVMOrcDisposeInstance.
85 */
86const char *LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack);
87
88/**
Lang Hames130a7c42015-10-28 02:40:04 +000089 * Mangle the given symbol.
90 * Memory will be allocated for MangledSymbol to hold the result. The client
91 */
92void LLVMOrcGetMangledSymbol(LLVMOrcJITStackRef JITStack, char **MangledSymbol,
93 const char *Symbol);
94
95/**
96 * Dispose of a mangled symbol.
97 */
Lang Hames130a7c42015-10-28 02:40:04 +000098void LLVMOrcDisposeMangledSymbol(char *MangledSymbol);
99
100/**
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000101 * Create a lazy compile callback.
102 */
Lang Hames4ce98662017-07-07 02:59:13 +0000103LLVMOrcErrorCode
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000104LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,
Lang Hames4ce98662017-07-07 02:59:13 +0000105 LLVMOrcTargetAddress *RetAddr,
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000106 LLVMOrcLazyCompileCallbackFn Callback,
107 void *CallbackCtx);
108
109/**
110 * Create a named indirect call stub.
111 */
Lang Hamesef5a0ee2016-04-25 19:56:45 +0000112LLVMOrcErrorCode LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,
113 const char *StubName,
114 LLVMOrcTargetAddress InitAddr);
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000115
116/**
117 * Set the pointer for the given indirect stub.
118 */
Lang Hamesef5a0ee2016-04-25 19:56:45 +0000119LLVMOrcErrorCode LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,
120 const char *StubName,
121 LLVMOrcTargetAddress NewAddr);
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000122
123/**
Lang Hames130a7c42015-10-28 02:40:04 +0000124 * Add module to be eagerly compiled.
125 */
Lang Hames4ce98662017-07-07 02:59:13 +0000126LLVMOrcErrorCode
Lang Hamescd9d49b2017-06-23 23:25:28 +0000127LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
Lang Hames4ce98662017-07-07 02:59:13 +0000128 LLVMOrcModuleHandle *RetHandle,
Lang Hamescd9d49b2017-06-23 23:25:28 +0000129 LLVMSharedModuleRef Mod,
Lang Hames130a7c42015-10-28 02:40:04 +0000130 LLVMOrcSymbolResolverFn SymbolResolver,
131 void *SymbolResolverCtx);
132
133/**
134 * Add module to be lazily compiled one function at a time.
135 */
Lang Hames4ce98662017-07-07 02:59:13 +0000136LLVMOrcErrorCode
Lang Hamescd9d49b2017-06-23 23:25:28 +0000137LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
Lang Hames4ce98662017-07-07 02:59:13 +0000138 LLVMOrcModuleHandle *RetHandle,
Lang Hamescd9d49b2017-06-23 23:25:28 +0000139 LLVMSharedModuleRef Mod,
Lang Hames130a7c42015-10-28 02:40:04 +0000140 LLVMOrcSymbolResolverFn SymbolResolver,
141 void *SymbolResolverCtx);
142
143/**
144 * Add an object file.
Lang Hamesec300632017-09-17 03:25:03 +0000145 *
146 * This method takes ownership of the given memory buffer and attempts to add
147 * it to the JIT as an object file.
148 * Clients should *not* dispose of the 'Obj' argument: the JIT will manage it
149 * from this call onwards.
Lang Hames130a7c42015-10-28 02:40:04 +0000150 */
Lang Hames4ce98662017-07-07 02:59:13 +0000151LLVMOrcErrorCode LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,
152 LLVMOrcModuleHandle *RetHandle,
Lang Hamesec300632017-09-17 03:25:03 +0000153 LLVMMemoryBufferRef Obj,
Lang Hames4ce98662017-07-07 02:59:13 +0000154 LLVMOrcSymbolResolverFn SymbolResolver,
155 void *SymbolResolverCtx);
Lang Hames130a7c42015-10-28 02:40:04 +0000156
157/**
158 * Remove a module set from the JIT.
159 *
160 * This works for all modules that can be added via OrcAdd*, including object
161 * files.
162 */
Lang Hames4ce98662017-07-07 02:59:13 +0000163LLVMOrcErrorCode LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack,
164 LLVMOrcModuleHandle H);
Lang Hames130a7c42015-10-28 02:40:04 +0000165
166/**
167 * Get symbol address from JIT instance.
168 */
Lang Hames4ce98662017-07-07 02:59:13 +0000169LLVMOrcErrorCode LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
170 LLVMOrcTargetAddress *RetAddr,
171 const char *SymbolName);
Lang Hames130a7c42015-10-28 02:40:04 +0000172
173/**
174 * Dispose of an ORC JIT stack.
175 */
Lang Hames4ce98662017-07-07 02:59:13 +0000176LLVMOrcErrorCode LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack);
Lang Hames130a7c42015-10-28 02:40:04 +0000177
178#ifdef __cplusplus
179}
180#endif /* extern "C" */
181
182#endif /* LLVM_C_ORCBINDINGS_H */