Fix bugs section.
[llvm-complete.git] / lib / ExecutionEngine / ExecutionEngineBindings.cpp
blob26e0d4141662394ab47216a8afee504f9377d690
1 //===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
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 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"
19 using namespace llvm;
21 /*===-- Operations on generic values --------------------------------------===*/
23 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
24 unsigned long long N,
25 int IsSigned) {
26 GenericValue *GenVal = new GenericValue();
27 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
28 return wrap(GenVal);
31 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
32 GenericValue *GenVal = new GenericValue();
33 GenVal->PointerVal = P;
34 return wrap(GenVal);
37 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
38 GenericValue *GenVal = new GenericValue();
39 switch (unwrap(TyRef)->getTypeID()) {
40 case Type::FloatTyID:
41 GenVal->FloatVal = N;
42 break;
43 case Type::DoubleTyID:
44 GenVal->DoubleVal = N;
45 break;
46 default:
47 assert(0 && "LLVMGenericValueToFloat supports only float and double.");
48 break;
50 return wrap(GenVal);
53 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
54 return unwrap(GenValRef)->IntVal.getBitWidth();
57 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
58 int IsSigned) {
59 GenericValue *GenVal = unwrap(GenValRef);
60 if (IsSigned)
61 return GenVal->IntVal.getSExtValue();
62 else
63 return GenVal->IntVal.getZExtValue();
66 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
67 return unwrap(GenVal)->PointerVal;
70 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
71 switch (unwrap(TyRef)->getTypeID()) {
72 case Type::FloatTyID:
73 return unwrap(GenVal)->FloatVal;
74 case Type::DoubleTyID:
75 return unwrap(GenVal)->DoubleVal;
76 default:
77 assert(0 && "LLVMGenericValueToFloat supports only float and double.");
78 break;
82 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
83 delete unwrap(GenVal);
86 /*===-- Operations on execution engines -----------------------------------===*/
88 int LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
89 LLVMModuleProviderRef MP,
90 char **OutError) {
91 std::string Error;
92 if (ExecutionEngine *EE = ExecutionEngine::create(unwrap(MP), false, &Error)){
93 *OutEE = wrap(EE);
94 return 0;
96 *OutError = strdup(Error.c_str());
97 return 1;
100 int LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
101 LLVMModuleProviderRef MP,
102 char **OutError) {
103 std::string Error;
104 if (ExecutionEngine *Interp =
105 ExecutionEngine::create(unwrap(MP), false, &Error)) {
106 *OutInterp = wrap(Interp);
107 return 0;
109 *OutError = strdup(Error.c_str());
110 return 1;
113 int LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
114 LLVMModuleProviderRef MP,
115 char **OutError) {
116 std::string Error;
117 if (ExecutionEngine *JIT = ExecutionEngine::createJIT(unwrap(MP), &Error)) {
118 *OutJIT = wrap(JIT);
119 return 0;
121 *OutError = strdup(Error.c_str());
122 return 1;
125 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
126 delete unwrap(EE);
129 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
130 unwrap(EE)->runStaticConstructorsDestructors(false);
133 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
134 unwrap(EE)->runStaticConstructorsDestructors(true);
137 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
138 unsigned ArgC, const char * const *ArgV,
139 const char * const *EnvP) {
140 std::vector<std::string> ArgVec;
141 for (unsigned I = 0; I != ArgC; ++I)
142 ArgVec.push_back(ArgV[I]);
144 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
147 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
148 unsigned NumArgs,
149 LLVMGenericValueRef *Args) {
150 std::vector<GenericValue> ArgVec;
151 ArgVec.reserve(NumArgs);
152 for (unsigned I = 0; I != NumArgs; ++I)
153 ArgVec.push_back(*unwrap(Args[I]));
155 GenericValue *Result = new GenericValue();
156 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
157 return wrap(Result);
160 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
161 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
164 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
165 unwrap(EE)->addModuleProvider(unwrap(MP));
168 int LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
169 LLVMModuleProviderRef MP,
170 LLVMModuleRef *OutMod, char **OutError) {
171 std::string Error;
172 if (Module *Gone = unwrap(EE)->removeModuleProvider(unwrap(MP), &Error)) {
173 *OutMod = wrap(Gone);
174 return 0;
176 if (OutError)
177 *OutError = strdup(Error.c_str());
178 return 1;
181 int LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
182 LLVMValueRef *OutFn) {
183 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
184 *OutFn = wrap(F);
185 return 0;
187 return 1;