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