Update comments.
[llvm/msp430.git] / lib / ExecutionEngine / ExecutionEngineBindings.cpp
blob39bf60121e08bbbb6a83a804e1bd3d20d394e80c
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 <cstring>
20 using namespace llvm;
22 /*===-- Operations on generic values --------------------------------------===*/
24 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
25 unsigned long long N,
26 int IsSigned) {
27 GenericValue *GenVal = new GenericValue();
28 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
29 return wrap(GenVal);
32 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
33 GenericValue *GenVal = new GenericValue();
34 GenVal->PointerVal = P;
35 return wrap(GenVal);
38 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
39 GenericValue *GenVal = new GenericValue();
40 switch (unwrap(TyRef)->getTypeID()) {
41 case Type::FloatTyID:
42 GenVal->FloatVal = N;
43 break;
44 case Type::DoubleTyID:
45 GenVal->DoubleVal = N;
46 break;
47 default:
48 assert(0 && "LLVMGenericValueToFloat supports only float and double.");
49 break;
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 assert(0 && "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 if (ExecutionEngine *EE = ExecutionEngine::create(unwrap(MP), false, &Error)){
95 *OutEE = wrap(EE);
96 return 0;
98 *OutError = strdup(Error.c_str());
99 return 1;
102 int LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
103 LLVMModuleProviderRef MP,
104 char **OutError) {
105 std::string Error;
106 if (ExecutionEngine *Interp =
107 ExecutionEngine::create(unwrap(MP), true, &Error)) {
108 *OutInterp = wrap(Interp);
109 return 0;
111 *OutError = strdup(Error.c_str());
112 return 1;
115 int LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
116 LLVMModuleProviderRef MP,
117 int Fast,
118 char **OutError) {
119 std::string Error;
120 if (ExecutionEngine *JIT = ExecutionEngine::createJIT(unwrap(MP), &Error, 0,
121 Fast != 0)) {
122 *OutJIT = wrap(JIT);
123 return 0;
125 *OutError = strdup(Error.c_str());
126 return 1;
129 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
130 delete unwrap(EE);
133 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
134 unwrap(EE)->runStaticConstructorsDestructors(false);
137 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
138 unwrap(EE)->runStaticConstructorsDestructors(true);
141 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
142 unsigned ArgC, const char * const *ArgV,
143 const char * const *EnvP) {
144 std::vector<std::string> ArgVec;
145 for (unsigned I = 0; I != ArgC; ++I)
146 ArgVec.push_back(ArgV[I]);
148 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
151 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
152 unsigned NumArgs,
153 LLVMGenericValueRef *Args) {
154 std::vector<GenericValue> ArgVec;
155 ArgVec.reserve(NumArgs);
156 for (unsigned I = 0; I != NumArgs; ++I)
157 ArgVec.push_back(*unwrap(Args[I]));
159 GenericValue *Result = new GenericValue();
160 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
161 return wrap(Result);
164 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
165 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
168 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
169 unwrap(EE)->addModuleProvider(unwrap(MP));
172 int LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
173 LLVMModuleProviderRef MP,
174 LLVMModuleRef *OutMod, char **OutError) {
175 std::string Error;
176 if (Module *Gone = unwrap(EE)->removeModuleProvider(unwrap(MP), &Error)) {
177 *OutMod = wrap(Gone);
178 return 0;
180 if (OutError)
181 *OutError = strdup(Error.c_str());
182 return 1;
185 int LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
186 LLVMValueRef *OutFn) {
187 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
188 *OutFn = wrap(F);
189 return 0;
191 return 1;
194 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
195 return wrap(unwrap(EE)->getTargetData());
198 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
199 void* Addr) {
200 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
203 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
204 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));