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

Generated by: LCOV version 1.13