~ubuntu-branches/ubuntu/quantal/llvm-3.1/quantal

« back to all changes in this revision

Viewing changes to lib/ExecutionEngine/ExecutionEngineBindings.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2012-03-29 19:09:51 UTC
  • Revision ID: package-import@ubuntu.com-20120329190951-aq83ivog4cg8bxun
Tags: upstream-3.1~svn153643
ImportĀ upstreamĀ versionĀ 3.1~svn153643

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
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
#include "llvm/Support/ErrorHandling.h"
 
19
#include <cstring>
 
20
 
 
21
using namespace llvm;
 
22
 
 
23
/*===-- Operations on generic values --------------------------------------===*/
 
24
 
 
25
LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
 
26
                                                unsigned long long N,
 
27
                                                LLVMBool IsSigned) {
 
28
  GenericValue *GenVal = new GenericValue();
 
29
  GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
 
30
  return wrap(GenVal);
 
31
}
 
32
 
 
33
LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
 
34
  GenericValue *GenVal = new GenericValue();
 
35
  GenVal->PointerVal = P;
 
36
  return wrap(GenVal);
 
37
}
 
38
 
 
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.");
 
50
  }
 
51
  return wrap(GenVal);
 
52
}
 
53
 
 
54
unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
 
55
  return unwrap(GenValRef)->IntVal.getBitWidth();
 
56
}
 
57
 
 
58
unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
 
59
                                         LLVMBool IsSigned) {
 
60
  GenericValue *GenVal = unwrap(GenValRef);
 
61
  if (IsSigned)
 
62
    return GenVal->IntVal.getSExtValue();
 
63
  else
 
64
    return GenVal->IntVal.getZExtValue();
 
65
}
 
66
 
 
67
void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
 
68
  return unwrap(GenVal)->PointerVal;
 
69
}
 
70
 
 
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
  }
 
80
}
 
81
 
 
82
void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
 
83
  delete unwrap(GenVal);
 
84
}
 
85
 
 
86
/*===-- Operations on execution engines -----------------------------------===*/
 
87
 
 
88
LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
 
89
                                            LLVMModuleRef M,
 
90
                                            char **OutError) {
 
91
  std::string Error;
 
92
  EngineBuilder builder(unwrap(M));
 
93
  builder.setEngineKind(EngineKind::Either)
 
94
         .setErrorStr(&Error);
 
95
  if (ExecutionEngine *EE = builder.create()){
 
96
    *OutEE = wrap(EE);
 
97
    return 0;
 
98
  }
 
99
  *OutError = strdup(Error.c_str());
 
100
  return 1;
 
101
}
 
102
 
 
103
LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
 
104
                                        LLVMModuleRef M,
 
105
                                        char **OutError) {
 
106
  std::string Error;
 
107
  EngineBuilder builder(unwrap(M));
 
108
  builder.setEngineKind(EngineKind::Interpreter)
 
109
         .setErrorStr(&Error);
 
110
  if (ExecutionEngine *Interp = builder.create()) {
 
111
    *OutInterp = wrap(Interp);
 
112
    return 0;
 
113
  }
 
114
  *OutError = strdup(Error.c_str());
 
115
  return 1;
 
116
}
 
117
 
 
118
LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
 
119
                                        LLVMModuleRef M,
 
120
                                        unsigned OptLevel,
 
121
                                        char **OutError) {
 
122
  std::string Error;
 
123
  EngineBuilder builder(unwrap(M));
 
124
  builder.setEngineKind(EngineKind::JIT)
 
125
         .setErrorStr(&Error)
 
126
         .setOptLevel((CodeGenOpt::Level)OptLevel);
 
127
  if (ExecutionEngine *JIT = builder.create()) {
 
128
    *OutJIT = wrap(JIT);
 
129
    return 0;
 
130
  }
 
131
  *OutError = strdup(Error.c_str());
 
132
  return 1;
 
133
}
 
134
 
 
135
LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
 
136
                                   LLVMModuleProviderRef MP,
 
137
                                   char **OutError) {
 
138
  /* The module provider is now actually a module. */
 
139
  return LLVMCreateExecutionEngineForModule(OutEE,
 
140
                                            reinterpret_cast<LLVMModuleRef>(MP),
 
141
                                            OutError);
 
142
}
 
143
 
 
144
LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
 
145
                               LLVMModuleProviderRef MP,
 
146
                               char **OutError) {
 
147
  /* The module provider is now actually a module. */
 
148
  return LLVMCreateInterpreterForModule(OutInterp,
 
149
                                        reinterpret_cast<LLVMModuleRef>(MP),
 
150
                                        OutError);
 
151
}
 
152
 
 
153
LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
 
154
                               LLVMModuleProviderRef MP,
 
155
                               unsigned OptLevel,
 
156
                               char **OutError) {
 
157
  /* The module provider is now actually a module. */
 
158
  return LLVMCreateJITCompilerForModule(OutJIT,
 
159
                                        reinterpret_cast<LLVMModuleRef>(MP),
 
160
                                        OptLevel, OutError);
 
161
}
 
162
 
 
163
 
 
164
void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
 
165
  delete unwrap(EE);
 
166
}
 
167
 
 
168
void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
 
169
  unwrap(EE)->runStaticConstructorsDestructors(false);
 
170
}
 
171
 
 
172
void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
 
173
  unwrap(EE)->runStaticConstructorsDestructors(true);
 
174
}
 
175
 
 
176
int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
 
177
                          unsigned ArgC, const char * const *ArgV,
 
178
                          const char * const *EnvP) {
 
179
  std::vector<std::string> ArgVec;
 
180
  for (unsigned I = 0; I != ArgC; ++I)
 
181
    ArgVec.push_back(ArgV[I]);
 
182
  
 
183
  return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
 
184
}
 
185
 
 
186
LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
 
187
                                    unsigned NumArgs,
 
188
                                    LLVMGenericValueRef *Args) {
 
189
  std::vector<GenericValue> ArgVec;
 
190
  ArgVec.reserve(NumArgs);
 
191
  for (unsigned I = 0; I != NumArgs; ++I)
 
192
    ArgVec.push_back(*unwrap(Args[I]));
 
193
  
 
194
  GenericValue *Result = new GenericValue();
 
195
  *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
 
196
  return wrap(Result);
 
197
}
 
198
 
 
199
void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
 
200
  unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
 
201
}
 
202
 
 
203
void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
 
204
  unwrap(EE)->addModule(unwrap(M));
 
205
}
 
206
 
 
207
void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
 
208
  /* The module provider is now actually a module. */
 
209
  LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
 
210
}
 
211
 
 
212
LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
 
213
                          LLVMModuleRef *OutMod, char **OutError) {
 
214
  Module *Mod = unwrap(M);
 
215
  unwrap(EE)->removeModule(Mod);
 
216
  *OutMod = wrap(Mod);
 
217
  return 0;
 
218
}
 
219
 
 
220
LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
 
221
                                  LLVMModuleProviderRef MP,
 
222
                                  LLVMModuleRef *OutMod, char **OutError) {
 
223
  /* The module provider is now actually a module. */
 
224
  return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
 
225
                          OutError);
 
226
}
 
227
 
 
228
LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
 
229
                          LLVMValueRef *OutFn) {
 
230
  if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
 
231
    *OutFn = wrap(F);
 
232
    return 0;
 
233
  }
 
234
  return 1;
 
235
}
 
236
 
 
237
void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn) {
 
238
  return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
 
239
}
 
240
 
 
241
LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
 
242
  return wrap(unwrap(EE)->getTargetData());
 
243
}
 
244
 
 
245
void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
 
246
                          void* Addr) {
 
247
  unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
 
248
}
 
249
 
 
250
void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
 
251
  return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
 
252
}