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"
21 /*===-- Operations on generic values --------------------------------------===*/
23 LLVMGenericValueRef
LLVMCreateGenericValueOfInt(LLVMTypeRef Ty
,
26 GenericValue
*GenVal
= new GenericValue();
27 GenVal
->IntVal
= APInt(unwrap
<IntegerType
>(Ty
)->getBitWidth(), N
, IsSigned
);
31 LLVMGenericValueRef
LLVMCreateGenericValueOfPointer(void *P
) {
32 GenericValue
*GenVal
= new GenericValue();
33 GenVal
->PointerVal
= P
;
37 LLVMGenericValueRef
LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef
, double N
) {
38 GenericValue
*GenVal
= new GenericValue();
39 switch (unwrap(TyRef
)->getTypeID()) {
43 case Type::DoubleTyID
:
44 GenVal
->DoubleVal
= N
;
47 assert(0 && "LLVMGenericValueToFloat supports only float and double.");
53 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef
) {
54 return unwrap(GenValRef
)->IntVal
.getBitWidth();
57 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef
,
59 GenericValue
*GenVal
= unwrap(GenValRef
);
61 return GenVal
->IntVal
.getSExtValue();
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()) {
73 return unwrap(GenVal
)->FloatVal
;
74 case Type::DoubleTyID
:
75 return unwrap(GenVal
)->DoubleVal
;
77 assert(0 && "LLVMGenericValueToFloat supports only float and double.");
82 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal
) {
83 delete unwrap(GenVal
);
86 /*===-- Operations on execution engines -----------------------------------===*/
88 int LLVMCreateExecutionEngine(LLVMExecutionEngineRef
*OutEE
,
89 LLVMModuleProviderRef MP
,
92 if (ExecutionEngine
*EE
= ExecutionEngine::create(unwrap(MP
), false, &Error
)){
96 *OutError
= strdup(Error
.c_str());
100 int LLVMCreateInterpreter(LLVMExecutionEngineRef
*OutInterp
,
101 LLVMModuleProviderRef MP
,
104 if (ExecutionEngine
*Interp
=
105 ExecutionEngine::create(unwrap(MP
), false, &Error
)) {
106 *OutInterp
= wrap(Interp
);
109 *OutError
= strdup(Error
.c_str());
113 int LLVMCreateJITCompiler(LLVMExecutionEngineRef
*OutJIT
,
114 LLVMModuleProviderRef MP
,
117 if (ExecutionEngine
*JIT
= ExecutionEngine::createJIT(unwrap(MP
), &Error
)) {
121 *OutError
= strdup(Error
.c_str());
125 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef 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
,
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
);
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
) {
172 if (Module
*Gone
= unwrap(EE
)->removeModuleProvider(unwrap(MP
), &Error
)) {
173 *OutMod
= wrap(Gone
);
177 *OutError
= strdup(Error
.c_str());
181 int LLVMFindFunction(LLVMExecutionEngineRef EE
, const char *Name
,
182 LLVMValueRef
*OutFn
) {
183 if (Function
*F
= unwrap(EE
)->FindFunctionNamed(Name
)) {