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