ExecutionEngineBindings.cpp 8.5 KB

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