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.");
81
return 0; // Not reached
84
void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
85
delete unwrap(GenVal);
88
/*===-- Operations on execution engines -----------------------------------===*/
90
LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
94
EngineBuilder builder(unwrap(M));
95
builder.setEngineKind(EngineKind::Either)
97
if (ExecutionEngine *EE = builder.create()){
101
*OutError = strdup(Error.c_str());
105
LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
109
EngineBuilder builder(unwrap(M));
110
builder.setEngineKind(EngineKind::Interpreter)
111
.setErrorStr(&Error);
112
if (ExecutionEngine *Interp = builder.create()) {
113
*OutInterp = wrap(Interp);
116
*OutError = strdup(Error.c_str());
120
LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
125
EngineBuilder builder(unwrap(M));
126
builder.setEngineKind(EngineKind::JIT)
128
.setOptLevel((CodeGenOpt::Level)OptLevel);
129
if (ExecutionEngine *JIT = builder.create()) {
133
*OutError = strdup(Error.c_str());
137
LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
138
LLVMModuleProviderRef MP,
140
/* The module provider is now actually a module. */
141
return LLVMCreateExecutionEngineForModule(OutEE,
142
reinterpret_cast<LLVMModuleRef>(MP),
146
LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
147
LLVMModuleProviderRef MP,
149
/* The module provider is now actually a module. */
150
return LLVMCreateInterpreterForModule(OutInterp,
151
reinterpret_cast<LLVMModuleRef>(MP),
155
LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
156
LLVMModuleProviderRef MP,
159
/* The module provider is now actually a module. */
160
return LLVMCreateJITCompilerForModule(OutJIT,
161
reinterpret_cast<LLVMModuleRef>(MP),
166
void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
170
void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
171
unwrap(EE)->runStaticConstructorsDestructors(false);
174
void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
175
unwrap(EE)->runStaticConstructorsDestructors(true);
178
int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
179
unsigned ArgC, const char * const *ArgV,
180
const char * const *EnvP) {
181
std::vector<std::string> ArgVec;
182
for (unsigned I = 0; I != ArgC; ++I)
183
ArgVec.push_back(ArgV[I]);
185
return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
188
LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
190
LLVMGenericValueRef *Args) {
191
std::vector<GenericValue> ArgVec;
192
ArgVec.reserve(NumArgs);
193
for (unsigned I = 0; I != NumArgs; ++I)
194
ArgVec.push_back(*unwrap(Args[I]));
196
GenericValue *Result = new GenericValue();
197
*Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
201
void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
202
unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
205
void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
206
unwrap(EE)->addModule(unwrap(M));
209
void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
210
/* The module provider is now actually a module. */
211
LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
214
LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
215
LLVMModuleRef *OutMod, char **OutError) {
216
Module *Mod = unwrap(M);
217
unwrap(EE)->removeModule(Mod);
222
LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
223
LLVMModuleProviderRef MP,
224
LLVMModuleRef *OutMod, char **OutError) {
225
/* The module provider is now actually a module. */
226
return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
230
LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
231
LLVMValueRef *OutFn) {
232
if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
239
void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn) {
240
return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
243
LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
244
return wrap(unwrap(EE)->getTargetData());
247
void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
249
unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
252
void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
253
return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));