We're not going to spend 100% of time in interrupts, do we? :)
[llvm/msp430.git] / lib / ExecutionEngine / ExecutionEngineBindings.cpp
blob83397a586d5372b475959a9771e682eab68f4951
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 unsigned OptLevel,
118 char **OutError) {
119 std::string Error;
120 if (ExecutionEngine *JIT =
121 ExecutionEngine::createJIT(unwrap(MP), &Error, 0,
122 (CodeGenOpt::Level)OptLevel)) {
123 *OutJIT = wrap(JIT);
124 return 0;
126 *OutError = strdup(Error.c_str());
127 return 1;
130 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
131 delete unwrap(EE);
134 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
135 unwrap(EE)->runStaticConstructorsDestructors(false);
138 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
139 unwrap(EE)->runStaticConstructorsDestructors(true);
142 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
143 unsigned ArgC, const char * const *ArgV,
144 const char * const *EnvP) {
145 std::vector<std::string> ArgVec;
146 for (unsigned I = 0; I != ArgC; ++I)
147 ArgVec.push_back(ArgV[I]);
149 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
152 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
153 unsigned NumArgs,
154 LLVMGenericValueRef *Args) {
155 std::vector<GenericValue> ArgVec;
156 ArgVec.reserve(NumArgs);
157 for (unsigned I = 0; I != NumArgs; ++I)
158 ArgVec.push_back(*unwrap(Args[I]));
160 GenericValue *Result = new GenericValue();
161 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
162 return wrap(Result);
165 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
166 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
169 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
170 unwrap(EE)->addModuleProvider(unwrap(MP));
173 int LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
174 LLVMModuleProviderRef MP,
175 LLVMModuleRef *OutMod, char **OutError) {
176 std::string Error;
177 if (Module *Gone = unwrap(EE)->removeModuleProvider(unwrap(MP), &Error)) {
178 *OutMod = wrap(Gone);
179 return 0;
181 if (OutError)
182 *OutError = strdup(Error.c_str());
183 return 1;
186 int LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
187 LLVMValueRef *OutFn) {
188 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
189 *OutFn = wrap(F);
190 return 0;
192 return 1;
195 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
196 return wrap(unwrap(EE)->getTargetData());
199 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
200 void* Addr) {
201 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
204 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
205 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));