blob: bcb72ea944913c152dceb082dc1362e0a7d75b12 [file] [log] [blame]
Gordon Henriksen2e855e62007-12-23 16:59:28 +00001//===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Gordon Henriksen2e855e62007-12-23 16:59:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the C bindings for the ExecutionEngine library.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "jit"
15#include "llvm-c/ExecutionEngine.h"
16#include "llvm/ExecutionEngine/GenericValue.h"
17#include "llvm/ExecutionEngine/ExecutionEngine.h"
18
19using namespace llvm;
20
21/*===-- Operations on generic values --------------------------------------===*/
22
23LLVMGenericValueRef 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);
29}
30
31LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
32 GenericValue *GenVal = new GenericValue();
33 GenVal->PointerVal = P;
34 return wrap(GenVal);
35}
36
37LLVMGenericValueRef 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;
49 }
50 return wrap(GenVal);
51}
52
53unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
54 return unwrap(GenValRef)->IntVal.getBitWidth();
55}
56
57unsigned 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();
64}
65
66void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
67 return unwrap(GenVal)->PointerVal;
68}
69
70double 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;
79 }
80}
81
82void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
83 delete unwrap(GenVal);
84}
85
86/*===-- Operations on execution engines -----------------------------------===*/
87
88int 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;
95 }
96 *OutError = strdup(Error.c_str());
97 return 1;
98}
99
100int LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
101 LLVMModuleProviderRef MP,
102 char **OutError) {
103 std::string Error;
104 if (ExecutionEngine *Interp = ExecutionEngine::create(unwrap(MP), &Error)) {
105 *OutInterp = wrap(Interp);
106 return 0;
107 }
108 *OutError = strdup(Error.c_str());
109 return 1;
110}
111
112int LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
113 LLVMModuleProviderRef MP,
114 char **OutError) {
115 std::string Error;
116 if (ExecutionEngine *JIT = ExecutionEngine::createJIT(unwrap(MP), &Error)) {
117 *OutJIT = wrap(JIT);
118 return 0;
119 }
120 *OutError = strdup(Error.c_str());
121 return 1;
122}
123
124void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
125 delete unwrap(EE);
126}
127
128void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
129 unwrap(EE)->runStaticConstructorsDestructors(false);
130}
131
132void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
133 unwrap(EE)->runStaticConstructorsDestructors(true);
134}
135
136int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
137 unsigned ArgC, const char * const *ArgV,
138 const char * const *EnvP) {
139 std::vector<std::string> ArgVec;
140 for (unsigned I = 0; I != ArgC; ++I)
141 ArgVec.push_back(ArgV[I]);
142
143 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
144}
145
146LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
147 unsigned NumArgs,
148 LLVMGenericValueRef *Args) {
149 std::vector<GenericValue> ArgVec;
150 ArgVec.reserve(NumArgs);
151 for (unsigned I = 0; I != NumArgs; ++I)
152 ArgVec.push_back(*unwrap(Args[I]));
153
154 GenericValue *Result = new GenericValue();
155 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
156 return wrap(Result);
157}
158
159void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
160 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
161}
162
163void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
164 unwrap(EE)->addModuleProvider(unwrap(MP));
165}
166
167int LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
168 LLVMModuleProviderRef MP,
169 LLVMModuleRef *OutMod, char **OutError) {
170 std::string Error;
171 if (Module *Gone = unwrap(EE)->removeModuleProvider(unwrap(MP), &Error)) {
172 *OutMod = wrap(Gone);
173 return 0;
174 }
175 if (OutError)
176 *OutError = strdup(Error.c_str());
177 return 1;
178}
179
180int LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
181 LLVMValueRef *OutFn) {
182 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
183 *OutFn = wrap(F);
184 return 0;
185 }
186 return 1;
187}