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"
22 /*===-- Operations on generic values --------------------------------------===*/
24 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
27 GenericValue *GenVal = new GenericValue();
28 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
32 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
33 GenericValue *GenVal = new GenericValue();
34 GenVal->PointerVal = P;
38 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
39 GenericValue *GenVal = new GenericValue();
40 switch (unwrap(TyRef)->getTypeID()) {
44 case Type::DoubleTyID:
45 GenVal->DoubleVal = N;
48 assert(0 && "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 assert(0 && "LLVMGenericValueToFloat supports only float and double.");
81 return 0; // Not reached
84 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
85 delete unwrap(GenVal);
88 /*===-- Operations on execution engines -----------------------------------===*/
90 int LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
91 LLVMModuleProviderRef MP,
94 if (ExecutionEngine *EE = ExecutionEngine::create(unwrap(MP), false, &Error)){
98 *OutError = strdup(Error.c_str());
102 int LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
103 LLVMModuleProviderRef MP,
106 if (ExecutionEngine *Interp =
107 ExecutionEngine::create(unwrap(MP), true, &Error)) {
108 *OutInterp = wrap(Interp);
111 *OutError = strdup(Error.c_str());
115 int LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
116 LLVMModuleProviderRef MP,
120 if (ExecutionEngine *JIT =
121 ExecutionEngine::create(unwrap(MP), false, &Error,
122 (CodeGenOpt::Level)OptLevel)) {
126 *OutError = strdup(Error.c_str());
130 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
134 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
135 unwrap(EE)->runStaticConstructorsDestructors(false);
138 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
139 unwrap(EE)->runStaticConstructorsDestructors(true);
142 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
143 unsigned ArgC, const char * const *ArgV,
144 const char * const *EnvP) {
145 std::vector<std::string> ArgVec;
146 for (unsigned I = 0; I != ArgC; ++I)
147 ArgVec.push_back(ArgV[I]);
149 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
152 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
154 LLVMGenericValueRef *Args) {
155 std::vector<GenericValue> ArgVec;
156 ArgVec.reserve(NumArgs);
157 for (unsigned I = 0; I != NumArgs; ++I)
158 ArgVec.push_back(*unwrap(Args[I]));
160 GenericValue *Result = new GenericValue();
161 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
165 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
166 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
169 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
170 unwrap(EE)->addModuleProvider(unwrap(MP));
173 int LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
174 LLVMModuleProviderRef MP,
175 LLVMModuleRef *OutMod, char **OutError) {
177 if (Module *Gone = unwrap(EE)->removeModuleProvider(unwrap(MP), &Error)) {
178 *OutMod = wrap(Gone);
182 *OutError = strdup(Error.c_str());
186 int LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
187 LLVMValueRef *OutFn) {
188 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
195 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
196 return wrap(unwrap(EE)->getTargetData());
199 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
201 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
204 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
205 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));