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