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 module llvm.c.executionEngine;
20 
21 public import llvm.c.types;
22 
23 import llvm.c.target;
24 import llvm.c.targetMachine;
25 
26 extern(C) nothrow:
27 
28 /**
29  * @defgroup LLVMCExecutionEngine Execution Engine
30  * @ingroup LLVMC
31  *
32  * @{
33  */
34 
35 void LLVMLinkInMCJIT();
36 void LLVMLinkInInterpreter();
37 
38 struct __LLVMOpaqueGenericValue {};
39 alias LLVMGenericValueRef = __LLVMOpaqueGenericValue*;
40 struct __LLVMOpaqueExecutionEngine {};
41 alias LLVMExecutionEngineRef = __LLVMOpaqueExecutionEngine*;
42 struct __LLVMOpaqueMCJITMemoryManager {};
43 alias LLVMMCJITMemoryManagerRef = __LLVMOpaqueMCJITMemoryManager*;
44 
45 struct LLVMMCJITCompilerOptions {
46   uint OptLevel;
47   LLVMCodeModel CodeModel;
48   LLVMBool NoFramePointerElim;
49   LLVMBool EnableFastISel;
50   LLVMMCJITMemoryManagerRef MCJMM;
51 }
52 
53 /*===-- Operations on generic values --------------------------------------===*/
54 
55 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
56                                                 ulong N,
57                                                 LLVMBool IsSigned);
58 
59 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void* P);
60 
61 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef Ty, double N);
62 
63 uint LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef);
64 
65 ulong LLVMGenericValueToInt(LLVMGenericValueRef GenVal,
66                             LLVMBool IsSigned);
67 
68 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal);
69 
70 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal);
71 
72 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal);
73 
74 /*===-- Operations on execution engines -----------------------------------===*/
75 
76 LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
77                                             LLVMModuleRef M,
78                                             char** OutError);
79 
80 LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
81                                         LLVMModuleRef M,
82                                         char** OutError);
83 
84 LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
85                                         LLVMModuleRef M,
86                                         uint OptLevel,
87                                         char** OutError);
88 
89 void LLVMInitializeMCJITCompilerOptions(
90   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  */
109 LLVMBool LLVMCreateMCJITCompilerForModule(
110   LLVMExecutionEngineRef* OutJIT, LLVMModuleRef M,
111   LLVMMCJITCompilerOptions* Options, size_t SizeOfOptions,
112   char** OutError);
113 
114 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE);
115 
116 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE);
117 
118 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE);
119 
120 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
121                           uint ArgC, const(char*) *ArgV,
122                           const(char*) *EnvP);
123 
124 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
125                                     uint NumArgs,
126                                     LLVMGenericValueRef *Args);
127 
128 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F);
129 
130 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M);
131 
132 LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
133                           LLVMModuleRef *OutMod, char **OutError);
134 
135 LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const(char)* Name,
136                           LLVMValueRef *OutFn);
137 
138 void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
139                                      LLVMValueRef Fn);
140 
141 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE);
142 LLVMTargetMachineRef
143 LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE);
144 
145 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
146                           void* Addr);
147 
148 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global);
149 
150 ulong LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const(char)* Name);
151 
152 ulong LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const(char)* Name);
153 
154 /*===-- Operations on memory managers -------------------------------------===*/
155 
156 alias LLVMMemoryManagerAllocateCodeSectionCallback = ubyte* function(
157   void* Opaque, size_t Size, uint Alignment, uint SectionID,
158   const(char)* SectionName);
159 alias LLVMMemoryManagerAllocateDataSectionCallback = ubyte* function(
160   void* Opaque, size_t Size, uint Alignment, uint SectionID,
161   const(char)* SectionName, LLVMBool IsReadOnly);
162 alias LLVMMemoryManagerFinalizeMemoryCallback = LLVMBool function(
163   void* Opaque, char** ErrMsg);
164 alias LLVMMemoryManagerDestroyCallback = void function(void* Opaque);
165 
166 /**
167  * Create a simple custom MCJIT memory manager. This memory manager can
168  * intercept allocations in a module-oblivious way. This will return NULL
169  * if any of the passed functions are NULL.
170  *
171  * @param Opaque An opaque client object to pass back to the callbacks.
172  * @param AllocateCodeSection Allocate a block of memory for executable code.
173  * @param AllocateDataSection Allocate a block of memory for data.
174  * @param FinalizeMemory Set page permissions and flush cache. Return 0 on
175  *   success, 1 on error.
176  */
177 LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
178   void* Opaque,
179   LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,
180   LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,
181   LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,
182   LLVMMemoryManagerDestroyCallback Destroy);
183 
184 void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM);
185 
186 /**
187  * @}
188  */