pass machinemoduleinfo down into getSymbolForDwarfGlobalReference,
[llvm/avr.git] / lib / ExecutionEngine / ExecutionEngineBindings.cpp
blob5901cd757dc1f191b48458f6bd12d2ee60f54638
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"
18 #include "llvm/Support/ErrorHandling.h"
19 #include <cstring>
21 using namespace llvm;
23 /*===-- Operations on generic values --------------------------------------===*/
25 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
26 unsigned long long N,
27 int IsSigned) {
28 GenericValue *GenVal = new GenericValue();
29 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
30 return wrap(GenVal);
33 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
34 GenericValue *GenVal = new GenericValue();
35 GenVal->PointerVal = P;
36 return wrap(GenVal);
39 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
40 GenericValue *GenVal = new GenericValue();
41 switch (unwrap(TyRef)->getTypeID()) {
42 case Type::FloatTyID:
43 GenVal->FloatVal = N;
44 break;
45 case Type::DoubleTyID:
46 GenVal->DoubleVal = N;
47 break;
48 default:
49 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
51 return wrap(GenVal);
54 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
55 return unwrap(GenValRef)->IntVal.getBitWidth();
58 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
59 int IsSigned) {
60 GenericValue *GenVal = unwrap(GenValRef);
61 if (IsSigned)
62 return GenVal->IntVal.getSExtValue();
63 else
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()) {
73 case Type::FloatTyID:
74 return unwrap(GenVal)->FloatVal;
75 case Type::DoubleTyID:
76 return unwrap(GenVal)->DoubleVal;
77 default:
78 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
79 break;
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,
92 char **OutError) {
93 std::string Error;
94 EngineBuilder builder(unwrap(MP));
95 builder.setEngineKind(EngineKind::Either)
96 .setErrorStr(&Error);
97 if (ExecutionEngine *EE = builder.create()){
98 *OutEE = wrap(EE);
99 return 0;
101 *OutError = strdup(Error.c_str());
102 return 1;
105 int LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
106 LLVMModuleProviderRef MP,
107 char **OutError) {
108 std::string Error;
109 EngineBuilder builder(unwrap(MP));
110 builder.setEngineKind(EngineKind::Interpreter)
111 .setErrorStr(&Error);
112 if (ExecutionEngine *Interp = builder.create()) {
113 *OutInterp = wrap(Interp);
114 return 0;
116 *OutError = strdup(Error.c_str());
117 return 1;
120 int LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
121 LLVMModuleProviderRef MP,
122 unsigned OptLevel,
123 char **OutError) {
124 std::string Error;
125 EngineBuilder builder(unwrap(MP));
126 builder.setEngineKind(EngineKind::JIT)
127 .setErrorStr(&Error)
128 .setOptLevel((CodeGenOpt::Level)OptLevel);
129 if (ExecutionEngine *JIT = builder.create()) {
130 *OutJIT = wrap(JIT);
131 return 0;
133 *OutError = strdup(Error.c_str());
134 return 1;
137 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
138 delete unwrap(EE);
141 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
142 unwrap(EE)->runStaticConstructorsDestructors(false);
145 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
146 unwrap(EE)->runStaticConstructorsDestructors(true);
149 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
150 unsigned ArgC, const char * const *ArgV,
151 const char * const *EnvP) {
152 std::vector<std::string> ArgVec;
153 for (unsigned I = 0; I != ArgC; ++I)
154 ArgVec.push_back(ArgV[I]);
156 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
159 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
160 unsigned NumArgs,
161 LLVMGenericValueRef *Args) {
162 std::vector<GenericValue> ArgVec;
163 ArgVec.reserve(NumArgs);
164 for (unsigned I = 0; I != NumArgs; ++I)
165 ArgVec.push_back(*unwrap(Args[I]));
167 GenericValue *Result = new GenericValue();
168 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
169 return wrap(Result);
172 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
173 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
176 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
177 unwrap(EE)->addModuleProvider(unwrap(MP));
180 int LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
181 LLVMModuleProviderRef MP,
182 LLVMModuleRef *OutMod, char **OutError) {
183 std::string Error;
184 if (Module *Gone = unwrap(EE)->removeModuleProvider(unwrap(MP), &Error)) {
185 *OutMod = wrap(Gone);
186 return 0;
188 if (OutError)
189 *OutError = strdup(Error.c_str());
190 return 1;
193 int LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
194 LLVMValueRef *OutFn) {
195 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
196 *OutFn = wrap(F);
197 return 0;
199 return 1;
202 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
203 return wrap(unwrap(EE)->getTargetData());
206 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
207 void* Addr) {
208 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
211 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
212 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));