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/GenericValue.h"
17 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/Support/ErrorHandling.h"
23 /*===-- Operations on generic values --------------------------------------===*/
25 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
28 GenericValue *GenVal = new GenericValue();
29 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
33 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
34 GenericValue *GenVal = new GenericValue();
35 GenVal->PointerVal = P;
39 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
40 GenericValue *GenVal = new GenericValue();
41 switch (unwrap(TyRef)->getTypeID()) {
45 case Type::DoubleTyID:
46 GenVal->DoubleVal = N;
49 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
54 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
55 return unwrap(GenValRef)->IntVal.getBitWidth();
58 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
60 GenericValue *GenVal = unwrap(GenValRef);
62 return GenVal->IntVal.getSExtValue();
64 return GenVal->IntVal.getZExtValue();
67 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
68 return unwrap(GenVal)->PointerVal;
71 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
72 switch (unwrap(TyRef)->getTypeID()) {
74 return unwrap(GenVal)->FloatVal;
75 case Type::DoubleTyID:
76 return unwrap(GenVal)->DoubleVal;
78 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
82 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
83 delete unwrap(GenVal);
86 /*===-- Operations on execution engines -----------------------------------===*/
88 LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
92 EngineBuilder builder(unwrap(M));
93 builder.setEngineKind(EngineKind::Either)
95 if (ExecutionEngine *EE = builder.create()){
99 *OutError = strdup(Error.c_str());
103 LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
107 EngineBuilder builder(unwrap(M));
108 builder.setEngineKind(EngineKind::Interpreter)
109 .setErrorStr(&Error);
110 if (ExecutionEngine *Interp = builder.create()) {
111 *OutInterp = wrap(Interp);
114 *OutError = strdup(Error.c_str());
118 LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
123 EngineBuilder builder(unwrap(M));
124 builder.setEngineKind(EngineKind::JIT)
126 .setOptLevel((CodeGenOpt::Level)OptLevel);
127 if (ExecutionEngine *JIT = builder.create()) {
131 *OutError = strdup(Error.c_str());
135 LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
136 LLVMModuleProviderRef MP,
138 /* The module provider is now actually a module. */
139 return LLVMCreateExecutionEngineForModule(OutEE,
140 reinterpret_cast<LLVMModuleRef>(MP),
144 LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
145 LLVMModuleProviderRef MP,
147 /* The module provider is now actually a module. */
148 return LLVMCreateInterpreterForModule(OutInterp,
149 reinterpret_cast<LLVMModuleRef>(MP),
153 LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
154 LLVMModuleProviderRef MP,
157 /* The module provider is now actually a module. */
158 return LLVMCreateJITCompilerForModule(OutJIT,
159 reinterpret_cast<LLVMModuleRef>(MP),
164 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
168 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
169 unwrap(EE)->runStaticConstructorsDestructors(false);
172 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
173 unwrap(EE)->runStaticConstructorsDestructors(true);
176 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
177 unsigned ArgC, const char * const *ArgV,
178 const char * const *EnvP) {
179 std::vector<std::string> ArgVec;
180 for (unsigned I = 0; I != ArgC; ++I)
181 ArgVec.push_back(ArgV[I]);
183 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
186 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
188 LLVMGenericValueRef *Args) {
189 std::vector<GenericValue> ArgVec;
190 ArgVec.reserve(NumArgs);
191 for (unsigned I = 0; I != NumArgs; ++I)
192 ArgVec.push_back(*unwrap(Args[I]));
194 GenericValue *Result = new GenericValue();
195 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
199 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
200 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
203 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
204 unwrap(EE)->addModule(unwrap(M));
207 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
208 /* The module provider is now actually a module. */
209 LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
212 LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
213 LLVMModuleRef *OutMod, char **OutError) {
214 Module *Mod = unwrap(M);
215 unwrap(EE)->removeModule(Mod);
220 LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
221 LLVMModuleProviderRef MP,
222 LLVMModuleRef *OutMod, char **OutError) {
223 /* The module provider is now actually a module. */
224 return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
228 LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
229 LLVMValueRef *OutFn) {
230 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
237 void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn) {
238 return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
241 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
242 return wrap(unwrap(EE)->getTargetData());
245 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
247 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
250 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
251 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));