1 //===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the C bindings for the ExecutionEngine library.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "jit"
15 #include "llvm-c/ExecutionEngine.h"
16 #include "llvm/ExecutionEngine/ExecutionEngine.h"
17 #include "llvm/ExecutionEngine/GenericValue.h"
18 #include "llvm/Support/ErrorHandling.h"
23 // Wrapping the C bindings types.
24 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef )
26 inline DataLayout *unwrap(LLVMTargetDataRef P) {
27 return reinterpret_cast<DataLayout*>(P);
30 inline LLVMTargetDataRef wrap(const DataLayout *P) {
31 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
34 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
35 return reinterpret_cast<TargetLibraryInfo*>(P);
38 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
39 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
40 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
43 /*===-- Operations on generic values --------------------------------------===*/
45 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
48 GenericValue *GenVal = new GenericValue();
49 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
53 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
54 GenericValue *GenVal = new GenericValue();
55 GenVal->PointerVal = P;
59 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
60 GenericValue *GenVal = new GenericValue();
61 switch (unwrap(TyRef)->getTypeID()) {
65 case Type::DoubleTyID:
66 GenVal->DoubleVal = N;
69 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
74 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
75 return unwrap(GenValRef)->IntVal.getBitWidth();
78 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
80 GenericValue *GenVal = unwrap(GenValRef);
82 return GenVal->IntVal.getSExtValue();
84 return GenVal->IntVal.getZExtValue();
87 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
88 return unwrap(GenVal)->PointerVal;
91 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
92 switch (unwrap(TyRef)->getTypeID()) {
94 return unwrap(GenVal)->FloatVal;
95 case Type::DoubleTyID:
96 return unwrap(GenVal)->DoubleVal;
98 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
102 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
103 delete unwrap(GenVal);
106 /*===-- Operations on execution engines -----------------------------------===*/
108 LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
112 EngineBuilder builder(unwrap(M));
113 builder.setEngineKind(EngineKind::Either)
114 .setErrorStr(&Error);
115 if (ExecutionEngine *EE = builder.create()){
119 *OutError = strdup(Error.c_str());
123 LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
127 EngineBuilder builder(unwrap(M));
128 builder.setEngineKind(EngineKind::Interpreter)
129 .setErrorStr(&Error);
130 if (ExecutionEngine *Interp = builder.create()) {
131 *OutInterp = wrap(Interp);
134 *OutError = strdup(Error.c_str());
138 LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
143 EngineBuilder builder(unwrap(M));
144 builder.setEngineKind(EngineKind::JIT)
146 .setOptLevel((CodeGenOpt::Level)OptLevel);
147 if (ExecutionEngine *JIT = builder.create()) {
151 *OutError = strdup(Error.c_str());
155 LLVMBool LLVMCreateMCJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
157 LLVMMCJITCompilerOptions *PassedOptions,
158 size_t SizeOfPassedOptions,
160 LLVMMCJITCompilerOptions options;
161 // If the user passed a larger sized options struct, then they were compiled
162 // against a newer LLVM. Tell them that something is wrong.
163 if (SizeOfPassedOptions > sizeof(options)) {
165 "Refusing to use options struct that is larger than my own; assuming LLVM "
166 "library mismatch.");
170 // Defend against the user having an old version of the API by ensuring that
171 // any fields they didn't see are cleared. We must defend against fields being
172 // set to the bitwise equivalent of zero, and assume that this means "do the
173 // default" as if that option hadn't been available.
174 memset(&options, 0, sizeof(options));
175 memcpy(&options, PassedOptions, SizeOfPassedOptions);
177 TargetOptions targetOptions;
178 targetOptions.NoFramePointerElim = options.NoFramePointerElim;
181 EngineBuilder builder(unwrap(M));
182 builder.setEngineKind(EngineKind::JIT)
185 .setOptLevel((CodeGenOpt::Level)options.OptLevel)
186 .setTargetOptions(targetOptions);
187 if (ExecutionEngine *JIT = builder.create()) {
191 *OutError = strdup(Error.c_str());
195 LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
196 LLVMModuleProviderRef MP,
198 /* The module provider is now actually a module. */
199 return LLVMCreateExecutionEngineForModule(OutEE,
200 reinterpret_cast<LLVMModuleRef>(MP),
204 LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
205 LLVMModuleProviderRef MP,
207 /* The module provider is now actually a module. */
208 return LLVMCreateInterpreterForModule(OutInterp,
209 reinterpret_cast<LLVMModuleRef>(MP),
213 LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
214 LLVMModuleProviderRef MP,
217 /* The module provider is now actually a module. */
218 return LLVMCreateJITCompilerForModule(OutJIT,
219 reinterpret_cast<LLVMModuleRef>(MP),
224 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
228 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
229 unwrap(EE)->runStaticConstructorsDestructors(false);
232 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
233 unwrap(EE)->runStaticConstructorsDestructors(true);
236 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
237 unsigned ArgC, const char * const *ArgV,
238 const char * const *EnvP) {
239 unwrap(EE)->finalizeObject();
241 std::vector<std::string> ArgVec;
242 for (unsigned I = 0; I != ArgC; ++I)
243 ArgVec.push_back(ArgV[I]);
245 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
248 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
250 LLVMGenericValueRef *Args) {
251 unwrap(EE)->finalizeObject();
253 std::vector<GenericValue> ArgVec;
254 ArgVec.reserve(NumArgs);
255 for (unsigned I = 0; I != NumArgs; ++I)
256 ArgVec.push_back(*unwrap(Args[I]));
258 GenericValue *Result = new GenericValue();
259 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
263 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
264 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
267 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
268 unwrap(EE)->addModule(unwrap(M));
271 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
272 /* The module provider is now actually a module. */
273 LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
276 LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
277 LLVMModuleRef *OutMod, char **OutError) {
278 Module *Mod = unwrap(M);
279 unwrap(EE)->removeModule(Mod);
284 LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
285 LLVMModuleProviderRef MP,
286 LLVMModuleRef *OutMod, char **OutError) {
287 /* The module provider is now actually a module. */
288 return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
292 LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
293 LLVMValueRef *OutFn) {
294 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
301 void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn) {
302 return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
305 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
306 return wrap(unwrap(EE)->getDataLayout());
309 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
311 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
314 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
315 unwrap(EE)->finalizeObject();
317 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));