1/*===-- llvm-c/ExecutionEngine.h - ExecutionEngine Lib C Iface --*- C++ -*-===*\
2|* *|
3|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
4|* Exceptions. *|
5|* See https://llvm.org/LICENSE.txt for license information. *|
6|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
7|* *|
8|*===----------------------------------------------------------------------===*|
9|* *|
10|* This header declares the C interface to libLLVMExecutionEngine.o, which *|
11|* implements various analyses of the 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\*===----------------------------------------------------------------------===*/
18
19#ifndef LLVM_C_EXECUTIONENGINE_H
20#define LLVM_C_EXECUTIONENGINE_H
21
22#include "llvm-c/ExternC.h"
23#include "llvm-c/Target.h"
24#include "llvm-c/TargetMachine.h"
25#include "llvm-c/Types.h"
26#include "llvm-c/Visibility.h"
27
28LLVM_C_EXTERN_C_BEGIN
29
30/**
31 * @defgroup LLVMCExecutionEngine Execution Engine
32 * @ingroup LLVMC
33 *
34 * @{
35 */
36
37/**
38 * Empty function used to force the linker to link MCJIT.
39 * Has no effect when called on a pre-built library (dylib interface).
40 */
41LLVM_C_ABI void LLVMLinkInMCJIT(void);
42/**
43 * Empty function used to force the linker to link the LLVM interpreter.
44 * Has no effect when called on a pre-built library (dylib interface).
45 */
46LLVM_C_ABI void LLVMLinkInInterpreter(void);
47
48typedef struct LLVMOpaqueGenericValue *LLVMGenericValueRef;
49typedef struct LLVMOpaqueExecutionEngine *LLVMExecutionEngineRef;
50typedef struct LLVMOpaqueMCJITMemoryManager *LLVMMCJITMemoryManagerRef;
51
52struct LLVMMCJITCompilerOptions {
53 unsigned OptLevel;
54 LLVMCodeModel CodeModel;
55 LLVMBool NoFramePointerElim;
56 LLVMBool EnableFastISel;
57 LLVMMCJITMemoryManagerRef MCJMM;
58};
59
60/*===-- Operations on generic values --------------------------------------===*/
61
62LLVM_C_ABI LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
63 unsigned long long N,
64 LLVMBool IsSigned);
65
66LLVM_C_ABI LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P);
67
68LLVM_C_ABI LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef Ty,
69 double N);
70
71LLVM_C_ABI unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef);
72
73LLVM_C_ABI unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenVal,
74 LLVMBool IsSigned);
75
76LLVM_C_ABI void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal);
77
78LLVM_C_ABI double LLVMGenericValueToFloat(LLVMTypeRef TyRef,
79 LLVMGenericValueRef GenVal);
80
81LLVM_C_ABI void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal);
82
83/*===-- Operations on execution engines -----------------------------------===*/
84
85LLVM_C_ABI LLVMBool LLVMCreateExecutionEngineForModule(
86 LLVMExecutionEngineRef *OutEE, LLVMModuleRef M, char **OutError);
87
88LLVM_C_ABI LLVMBool LLVMCreateInterpreterForModule(
89 LLVMExecutionEngineRef *OutInterp, LLVMModuleRef M, char **OutError);
90
91LLVM_C_ABI LLVMBool
92LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
93 unsigned OptLevel, char **OutError);
94
95LLVM_C_ABI void
96LLVMInitializeMCJITCompilerOptions(struct LLVMMCJITCompilerOptions *Options,
97 size_t SizeOfOptions);
98
99/**
100 * Create an MCJIT execution engine for a module, with the given options. It is
101 * the responsibility of the caller to ensure that all fields in Options up to
102 * the given SizeOfOptions are initialized. It is correct to pass a smaller
103 * value of SizeOfOptions that omits some fields. The canonical way of using
104 * this is:
105 *
106 * LLVMMCJITCompilerOptions options;
107 * LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
108 * ... fill in those options you care about
109 * LLVMCreateMCJITCompilerForModule(&jit, mod, &options, sizeof(options),
110 * &error);
111 *
112 * Note that this is also correct, though possibly suboptimal:
113 *
114 * LLVMCreateMCJITCompilerForModule(&jit, mod, 0, 0, &error);
115 */
116LLVM_C_ABI LLVMBool LLVMCreateMCJITCompilerForModule(
117 LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
118 struct LLVMMCJITCompilerOptions *Options, size_t SizeOfOptions,
119 char **OutError);
120
121LLVM_C_ABI void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE);
122
123LLVM_C_ABI void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE);
124
125LLVM_C_ABI void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE);
126
127LLVM_C_ABI int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
128 unsigned ArgC, const char *const *ArgV,
129 const char *const *EnvP);
130
131LLVM_C_ABI LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE,
132 LLVMValueRef F, unsigned NumArgs,
133 LLVMGenericValueRef *Args);
134
135LLVM_C_ABI void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE,
136 LLVMValueRef F);
137
138LLVM_C_ABI void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M);
139
140LLVM_C_ABI LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
141 LLVMModuleRef *OutMod, char **OutError);
142
143LLVM_C_ABI LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE,
144 const char *Name, LLVMValueRef *OutFn);
145
146LLVM_C_ABI void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
147 LLVMValueRef Fn);
148
149LLVM_C_ABI LLVMTargetDataRef
150LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE);
151LLVM_C_ABI LLVMTargetMachineRef
152LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE);
153
154LLVM_C_ABI void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE,
155 LLVMValueRef Global, void *Addr);
156
157LLVM_C_ABI void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE,
158 LLVMValueRef Global);
159
160LLVM_C_ABI uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE,
161 const char *Name);
162
163LLVM_C_ABI uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE,
164 const char *Name);
165
166/// Returns true on error, false on success. If true is returned then the error
167/// message is copied to OutStr and cleared in the ExecutionEngine instance.
168LLVM_C_ABI LLVMBool LLVMExecutionEngineGetErrMsg(LLVMExecutionEngineRef EE,
169 char **OutError);
170
171/*===-- Operations on memory managers -------------------------------------===*/
172
173typedef uint8_t *(*LLVMMemoryManagerAllocateCodeSectionCallback)(
174 void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID,
175 const char *SectionName);
176typedef uint8_t *(*LLVMMemoryManagerAllocateDataSectionCallback)(
177 void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID,
178 const char *SectionName, LLVMBool IsReadOnly);
179typedef LLVMBool (*LLVMMemoryManagerFinalizeMemoryCallback)(
180 void *Opaque, char **ErrMsg);
181typedef void (*LLVMMemoryManagerDestroyCallback)(void *Opaque);
182
183/**
184 * Create a simple custom MCJIT memory manager. This memory manager can
185 * intercept allocations in a module-oblivious way. This will return NULL
186 * if any of the passed functions are NULL.
187 *
188 * @param Opaque An opaque client object to pass back to the callbacks.
189 * @param AllocateCodeSection Allocate a block of memory for executable code.
190 * @param AllocateDataSection Allocate a block of memory for data.
191 * @param FinalizeMemory Set page permissions and flush cache. Return 0 on
192 * success, 1 on error.
193 */
194LLVM_C_ABI LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
195 void *Opaque,
196 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,
197 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,
198 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,
199 LLVMMemoryManagerDestroyCallback Destroy);
200
201LLVM_C_ABI void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM);
202
203/*===-- JIT Event Listener functions -------------------------------------===*/
204
205LLVM_C_ABI LLVMJITEventListenerRef LLVMCreateGDBRegistrationListener(void);
206LLVM_C_ABI LLVMJITEventListenerRef LLVMCreateIntelJITEventListener(void);
207LLVM_C_ABI LLVMJITEventListenerRef LLVMCreateOProfileJITEventListener(void);
208LLVM_C_ABI LLVMJITEventListenerRef LLVMCreatePerfJITEventListener(void);
209
210/**
211 * @}
212 */
213
214LLVM_C_EXTERN_C_END
215
216#endif
217