LCOV - code coverage report
Current view: top level - lib/ExecutionEngine - ExecutionEngineBindings.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 70 193 36.3 %
Date: 2017-09-14 15:23:50 Functions: 12 36 33.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #include "llvm-c/ExecutionEngine.h"
      15             : #include "llvm/ExecutionEngine/ExecutionEngine.h"
      16             : #include "llvm/ExecutionEngine/GenericValue.h"
      17             : #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
      18             : #include "llvm/IR/DerivedTypes.h"
      19             : #include "llvm/IR/Module.h"
      20             : #include "llvm/Support/CodeGenCWrappers.h"
      21             : #include "llvm/Support/ErrorHandling.h"
      22             : #include "llvm/Target/TargetOptions.h"
      23             : #include <cstring>
      24             : 
      25             : using namespace llvm;
      26             : 
      27             : #define DEBUG_TYPE "jit"
      28             : 
      29             : // Wrapping the C bindings types.
      30             : DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef)
      31             : 
      32             : 
      33             : static LLVMTargetMachineRef wrap(const TargetMachine *P) {
      34             :   return
      35             :   reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
      36             : }
      37             : 
      38             : /*===-- Operations on generic values --------------------------------------===*/
      39             : 
      40           0 : LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
      41             :                                                 unsigned long long N,
      42             :                                                 LLVMBool IsSigned) {
      43           0 :   GenericValue *GenVal = new GenericValue();
      44           0 :   GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
      45           0 :   return wrap(GenVal);
      46             : }
      47             : 
      48           0 : LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
      49           0 :   GenericValue *GenVal = new GenericValue();
      50           0 :   GenVal->PointerVal = P;
      51           0 :   return wrap(GenVal);
      52             : }
      53             : 
      54           0 : LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
      55           0 :   GenericValue *GenVal = new GenericValue();
      56           0 :   switch (unwrap(TyRef)->getTypeID()) {
      57           0 :   case Type::FloatTyID:
      58           0 :     GenVal->FloatVal = N;
      59           0 :     break;
      60           0 :   case Type::DoubleTyID:
      61           0 :     GenVal->DoubleVal = N;
      62           0 :     break;
      63           0 :   default:
      64           0 :     llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
      65             :   }
      66           0 :   return wrap(GenVal);
      67             : }
      68             : 
      69           0 : unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
      70           0 :   return unwrap(GenValRef)->IntVal.getBitWidth();
      71             : }
      72             : 
      73           0 : unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
      74             :                                          LLVMBool IsSigned) {
      75           0 :   GenericValue *GenVal = unwrap(GenValRef);
      76           0 :   if (IsSigned)
      77           0 :     return GenVal->IntVal.getSExtValue();
      78             :   else
      79           0 :     return GenVal->IntVal.getZExtValue();
      80             : }
      81             : 
      82           0 : void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
      83           0 :   return unwrap(GenVal)->PointerVal;
      84             : }
      85             : 
      86           0 : double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
      87           0 :   switch (unwrap(TyRef)->getTypeID()) {
      88           0 :   case Type::FloatTyID:
      89           0 :     return unwrap(GenVal)->FloatVal;
      90           0 :   case Type::DoubleTyID:
      91           0 :     return unwrap(GenVal)->DoubleVal;
      92           0 :   default:
      93           0 :     llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
      94             :   }
      95             : }
      96             : 
      97           0 : void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
      98           0 :   delete unwrap(GenVal);
      99           0 : }
     100             : 
     101             : /*===-- Operations on execution engines -----------------------------------===*/
     102             : 
     103           0 : LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
     104             :                                             LLVMModuleRef M,
     105             :                                             char **OutError) {
     106           0 :   std::string Error;
     107           0 :   EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
     108           0 :   builder.setEngineKind(EngineKind::Either)
     109           0 :          .setErrorStr(&Error);
     110           0 :   if (ExecutionEngine *EE = builder.create()){
     111           0 :     *OutEE = wrap(EE);
     112           0 :     return 0;
     113             :   }
     114           0 :   *OutError = strdup(Error.c_str());
     115           0 :   return 1;
     116             : }
     117             : 
     118           0 : LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
     119             :                                         LLVMModuleRef M,
     120             :                                         char **OutError) {
     121           0 :   std::string Error;
     122           0 :   EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
     123           0 :   builder.setEngineKind(EngineKind::Interpreter)
     124           0 :          .setErrorStr(&Error);
     125           0 :   if (ExecutionEngine *Interp = builder.create()) {
     126           0 :     *OutInterp = wrap(Interp);
     127           0 :     return 0;
     128             :   }
     129           0 :   *OutError = strdup(Error.c_str());
     130           0 :   return 1;
     131             : }
     132             : 
     133           0 : LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
     134             :                                         LLVMModuleRef M,
     135             :                                         unsigned OptLevel,
     136             :                                         char **OutError) {
     137           0 :   std::string Error;
     138           0 :   EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
     139           0 :   builder.setEngineKind(EngineKind::JIT)
     140           0 :          .setErrorStr(&Error)
     141           0 :          .setOptLevel((CodeGenOpt::Level)OptLevel);
     142           0 :   if (ExecutionEngine *JIT = builder.create()) {
     143           0 :     *OutJIT = wrap(JIT);
     144           0 :     return 0;
     145             :   }
     146           0 :   *OutError = strdup(Error.c_str());
     147           0 :   return 1;
     148             : }
     149             : 
     150          16 : void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions,
     151             :                                         size_t SizeOfPassedOptions) {
     152             :   LLVMMCJITCompilerOptions options;
     153          16 :   memset(&options, 0, sizeof(options)); // Most fields are zero by default.
     154          16 :   options.CodeModel = LLVMCodeModelJITDefault;
     155             :   
     156          16 :   memcpy(PassedOptions, &options,
     157          48 :          std::min(sizeof(options), SizeOfPassedOptions));
     158          16 : }
     159             : 
     160           8 : LLVMBool LLVMCreateMCJITCompilerForModule(
     161             :     LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
     162             :     LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions,
     163             :     char **OutError) {
     164             :   LLVMMCJITCompilerOptions options;
     165             :   // If the user passed a larger sized options struct, then they were compiled
     166             :   // against a newer LLVM. Tell them that something is wrong.
     167           8 :   if (SizeOfPassedOptions > sizeof(options)) {
     168           0 :     *OutError = strdup(
     169             :       "Refusing to use options struct that is larger than my own; assuming "
     170             :       "LLVM library mismatch.");
     171           0 :     return 1;
     172             :   }
     173             :   
     174             :   // Defend against the user having an old version of the API by ensuring that
     175             :   // any fields they didn't see are cleared. We must defend against fields being
     176             :   // set to the bitwise equivalent of zero, and assume that this means "do the
     177             :   // default" as if that option hadn't been available.
     178           8 :   LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
     179           8 :   memcpy(&options, PassedOptions, SizeOfPassedOptions);
     180             :   
     181           8 :   TargetOptions targetOptions;
     182           8 :   targetOptions.EnableFastISel = options.EnableFastISel;
     183          24 :   std::unique_ptr<Module> Mod(unwrap(M));
     184             : 
     185           8 :   if (Mod)
     186             :     // Set function attribute "no-frame-pointer-elim" based on
     187             :     // NoFramePointerElim.
     188          34 :     for (auto &F : *Mod) {
     189          10 :       auto Attrs = F.getAttributes();
     190          20 :       StringRef Value(options.NoFramePointerElim ? "true" : "false");
     191          10 :       Attrs = Attrs.addAttribute(F.getContext(), AttributeList::FunctionIndex,
     192          20 :                                  "no-frame-pointer-elim", Value);
     193             :       F.setAttributes(Attrs);
     194             :     }
     195             : 
     196          16 :   std::string Error;
     197          24 :   EngineBuilder builder(std::move(Mod));
     198           8 :   builder.setEngineKind(EngineKind::JIT)
     199           8 :          .setErrorStr(&Error)
     200          16 :          .setOptLevel((CodeGenOpt::Level)options.OptLevel)
     201           8 :          .setTargetOptions(targetOptions);
     202             :   bool JIT;
     203          16 :   if (Optional<CodeModel::Model> CM = unwrap(options.CodeModel, JIT))
     204           0 :     builder.setCodeModel(*CM);
     205           8 :   if (options.MCJMM)
     206           3 :     builder.setMCJITMemoryManager(
     207           9 :       std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM)));
     208           8 :   if (ExecutionEngine *JIT = builder.create()) {
     209           8 :     *OutJIT = wrap(JIT);
     210           8 :     return 0;
     211             :   }
     212           0 :   *OutError = strdup(Error.c_str());
     213           0 :   return 1;
     214             : }
     215             : 
     216           8 : void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
     217           8 :   delete unwrap(EE);
     218           8 : }
     219             : 
     220           0 : void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
     221           0 :   unwrap(EE)->finalizeObject();
     222           0 :   unwrap(EE)->runStaticConstructorsDestructors(false);
     223           0 : }
     224             : 
     225           0 : void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
     226           0 :   unwrap(EE)->finalizeObject();
     227           0 :   unwrap(EE)->runStaticConstructorsDestructors(true);
     228           0 : }
     229             : 
     230           0 : int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
     231             :                           unsigned ArgC, const char * const *ArgV,
     232             :                           const char * const *EnvP) {
     233           0 :   unwrap(EE)->finalizeObject();
     234             : 
     235           0 :   std::vector<std::string> ArgVec(ArgV, ArgV + ArgC);
     236           0 :   return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
     237             : }
     238             : 
     239           0 : LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
     240             :                                     unsigned NumArgs,
     241             :                                     LLVMGenericValueRef *Args) {
     242           0 :   unwrap(EE)->finalizeObject();
     243             :   
     244           0 :   std::vector<GenericValue> ArgVec;
     245           0 :   ArgVec.reserve(NumArgs);
     246           0 :   for (unsigned I = 0; I != NumArgs; ++I)
     247           0 :     ArgVec.push_back(*unwrap(Args[I]));
     248             :   
     249           0 :   GenericValue *Result = new GenericValue();
     250           0 :   *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
     251           0 :   return wrap(Result);
     252             : }
     253             : 
     254           0 : void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
     255           0 : }
     256             : 
     257           0 : void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
     258           0 :   unwrap(EE)->addModule(std::unique_ptr<Module>(unwrap(M)));
     259           0 : }
     260             : 
     261           0 : LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
     262             :                           LLVMModuleRef *OutMod, char **OutError) {
     263           0 :   Module *Mod = unwrap(M);
     264           0 :   unwrap(EE)->removeModule(Mod);
     265           0 :   *OutMod = wrap(Mod);
     266           0 :   return 0;
     267             : }
     268             : 
     269           0 : LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
     270             :                           LLVMValueRef *OutFn) {
     271           0 :   if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
     272           0 :     *OutFn = wrap(F);
     273           0 :     return 0;
     274             :   }
     275             :   return 1;
     276             : }
     277             : 
     278           0 : void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
     279             :                                      LLVMValueRef Fn) {
     280           0 :   return nullptr;
     281             : }
     282             : 
     283           0 : LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
     284           0 :   return wrap(&unwrap(EE)->getDataLayout());
     285             : }
     286             : 
     287             : LLVMTargetMachineRef
     288           0 : LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE) {
     289           0 :   return wrap(unwrap(EE)->getTargetMachine());
     290             : }
     291             : 
     292           1 : void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
     293             :                           void* Addr) {
     294           2 :   unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
     295           1 : }
     296             : 
     297           6 : void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
     298           6 :   unwrap(EE)->finalizeObject();
     299             :   
     300          12 :   return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
     301             : }
     302             : 
     303           1 : uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name) {
     304           4 :   return unwrap(EE)->getGlobalValueAddress(Name);
     305             : }
     306             : 
     307           2 : uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name) {
     308           8 :   return unwrap(EE)->getFunctionAddress(Name);
     309             : }
     310             : 
     311             : /*===-- Operations on memory managers -------------------------------------===*/
     312             : 
     313             : namespace {
     314             : 
     315             : struct SimpleBindingMMFunctions {
     316             :   LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection;
     317             :   LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection;
     318             :   LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory;
     319             :   LLVMMemoryManagerDestroyCallback Destroy;
     320             : };
     321             : 
     322             : class SimpleBindingMemoryManager : public RTDyldMemoryManager {
     323             : public:
     324             :   SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions,
     325             :                              void *Opaque);
     326             :   ~SimpleBindingMemoryManager() override;
     327             : 
     328             :   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
     329             :                                unsigned SectionID,
     330             :                                StringRef SectionName) override;
     331             : 
     332             :   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
     333             :                                unsigned SectionID, StringRef SectionName,
     334             :                                bool isReadOnly) override;
     335             : 
     336             :   bool finalizeMemory(std::string *ErrMsg) override;
     337             : 
     338             : private:
     339             :   SimpleBindingMMFunctions Functions;
     340             :   void *Opaque;
     341             : };
     342             : 
     343             : SimpleBindingMemoryManager::SimpleBindingMemoryManager(
     344             :   const SimpleBindingMMFunctions& Functions,
     345           2 :   void *Opaque)
     346           4 :   : Functions(Functions), Opaque(Opaque) {
     347             :   assert(Functions.AllocateCodeSection &&
     348             :          "No AllocateCodeSection function provided!");
     349             :   assert(Functions.AllocateDataSection &&
     350             :          "No AllocateDataSection function provided!");
     351             :   assert(Functions.FinalizeMemory &&
     352             :          "No FinalizeMemory function provided!");
     353             :   assert(Functions.Destroy &&
     354             :          "No Destroy function provided!");
     355             : }
     356             : 
     357           4 : SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
     358           2 :   Functions.Destroy(Opaque);
     359           2 : }
     360             : 
     361           2 : uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
     362             :   uintptr_t Size, unsigned Alignment, unsigned SectionID,
     363             :   StringRef SectionName) {
     364           4 :   return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
     365           6 :                                        SectionName.str().c_str());
     366             : }
     367             : 
     368           3 : uint8_t *SimpleBindingMemoryManager::allocateDataSection(
     369             :   uintptr_t Size, unsigned Alignment, unsigned SectionID,
     370             :   StringRef SectionName, bool isReadOnly) {
     371           6 :   return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
     372           6 :                                        SectionName.str().c_str(),
     373           6 :                                        isReadOnly);
     374             : }
     375             : 
     376           2 : bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
     377           2 :   char *errMsgCString = nullptr;
     378           2 :   bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
     379             :   assert((result || !errMsgCString) &&
     380             :          "Did not expect an error message if FinalizeMemory succeeded");
     381           2 :   if (errMsgCString) {
     382           0 :     if (ErrMsg)
     383           0 :       *ErrMsg = errMsgCString;
     384           0 :     free(errMsgCString);
     385             :   }
     386           2 :   return result;
     387             : }
     388             : 
     389             : } // anonymous namespace
     390             : 
     391           2 : LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
     392             :   void *Opaque,
     393             :   LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,
     394             :   LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,
     395             :   LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,
     396             :   LLVMMemoryManagerDestroyCallback Destroy) {
     397             :   
     398           4 :   if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
     399           2 :       !Destroy)
     400             :     return nullptr;
     401             :   
     402             :   SimpleBindingMMFunctions functions;
     403           2 :   functions.AllocateCodeSection = AllocateCodeSection;
     404           2 :   functions.AllocateDataSection = AllocateDataSection;
     405           2 :   functions.FinalizeMemory = FinalizeMemory;
     406           2 :   functions.Destroy = Destroy;
     407           4 :   return wrap(new SimpleBindingMemoryManager(functions, Opaque));
     408             : }
     409             : 
     410           0 : void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) {
     411           0 :   delete unwrap(MM);
     412           0 : }
     413             : 

Generated by: LCOV version 1.13