LLVM API Documentation

ExternalFunctions.cpp
Go to the documentation of this file.
00001 //===-- ExternalFunctions.cpp - Implement External Functions --------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file contains both code to deal with invoking "external" functions, but
00011 //  also contains code that implements "exported" external functions.
00012 //
00013 //  There are currently two mechanisms for handling external functions in the
00014 //  Interpreter.  The first is to implement lle_* wrapper functions that are
00015 //  specific to well-known library functions which manually translate the
00016 //  arguments from GenericValues and make the call.  If such a wrapper does
00017 //  not exist, and libffi is available, then the Interpreter will attempt to
00018 //  invoke the function using libffi, after finding its address.
00019 //
00020 //===----------------------------------------------------------------------===//
00021 
00022 #include "Interpreter.h"
00023 #include "llvm/Config/config.h"     // Detect libffi
00024 #include "llvm/IR/DataLayout.h"
00025 #include "llvm/IR/DerivedTypes.h"
00026 #include "llvm/IR/Module.h"
00027 #include "llvm/Support/DynamicLibrary.h"
00028 #include "llvm/Support/ErrorHandling.h"
00029 #include "llvm/Support/ManagedStatic.h"
00030 #include "llvm/Support/Mutex.h"
00031 #include "llvm/Support/UniqueLock.h"
00032 #include <cmath>
00033 #include <csignal>
00034 #include <cstdio>
00035 #include <cstring>
00036 #include <map>
00037 
00038 #ifdef HAVE_FFI_CALL
00039 #ifdef HAVE_FFI_H
00040 #include <ffi.h>
00041 #define USE_LIBFFI
00042 #elif HAVE_FFI_FFI_H
00043 #include <ffi/ffi.h>
00044 #define USE_LIBFFI
00045 #endif
00046 #endif
00047 
00048 using namespace llvm;
00049 
00050 static ManagedStatic<sys::Mutex> FunctionsLock;
00051 
00052 typedef GenericValue (*ExFunc)(FunctionType *,
00053                                const std::vector<GenericValue> &);
00054 static ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions;
00055 static ManagedStatic<std::map<std::string, ExFunc> > FuncNames;
00056 
00057 #ifdef USE_LIBFFI
00058 typedef void (*RawFunc)();
00059 static ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions;
00060 #endif
00061 
00062 static Interpreter *TheInterpreter;
00063 
00064 static char getTypeID(Type *Ty) {
00065   switch (Ty->getTypeID()) {
00066   case Type::VoidTyID:    return 'V';
00067   case Type::IntegerTyID:
00068     switch (cast<IntegerType>(Ty)->getBitWidth()) {
00069       case 1:  return 'o';
00070       case 8:  return 'B';
00071       case 16: return 'S';
00072       case 32: return 'I';
00073       case 64: return 'L';
00074       default: return 'N';
00075     }
00076   case Type::FloatTyID:   return 'F';
00077   case Type::DoubleTyID:  return 'D';
00078   case Type::PointerTyID: return 'P';
00079   case Type::FunctionTyID:return 'M';
00080   case Type::StructTyID:  return 'T';
00081   case Type::ArrayTyID:   return 'A';
00082   default: return 'U';
00083   }
00084 }
00085 
00086 // Try to find address of external function given a Function object.
00087 // Please note, that interpreter doesn't know how to assemble a
00088 // real call in general case (this is JIT job), that's why it assumes,
00089 // that all external functions has the same (and pretty "general") signature.
00090 // The typical example of such functions are "lle_X_" ones.
00091 static ExFunc lookupFunction(const Function *F) {
00092   // Function not found, look it up... start by figuring out what the
00093   // composite function name should be.
00094   std::string ExtName = "lle_";
00095   FunctionType *FT = F->getFunctionType();
00096   for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
00097     ExtName += getTypeID(FT->getContainedType(i));
00098   ExtName += "_" + F->getName().str();
00099 
00100   sys::ScopedLock Writer(*FunctionsLock);
00101   ExFunc FnPtr = (*FuncNames)[ExtName];
00102   if (!FnPtr)
00103     FnPtr = (*FuncNames)["lle_X_" + F->getName().str()];
00104   if (!FnPtr)  // Try calling a generic function... if it exists...
00105     FnPtr = (ExFunc)(intptr_t)
00106       sys::DynamicLibrary::SearchForAddressOfSymbol("lle_X_" +
00107                                                     F->getName().str());
00108   if (FnPtr)
00109     ExportedFunctions->insert(std::make_pair(F, FnPtr));  // Cache for later
00110   return FnPtr;
00111 }
00112 
00113 #ifdef USE_LIBFFI
00114 static ffi_type *ffiTypeFor(Type *Ty) {
00115   switch (Ty->getTypeID()) {
00116     case Type::VoidTyID: return &ffi_type_void;
00117     case Type::IntegerTyID:
00118       switch (cast<IntegerType>(Ty)->getBitWidth()) {
00119         case 8:  return &ffi_type_sint8;
00120         case 16: return &ffi_type_sint16;
00121         case 32: return &ffi_type_sint32;
00122         case 64: return &ffi_type_sint64;
00123       }
00124     case Type::FloatTyID:   return &ffi_type_float;
00125     case Type::DoubleTyID:  return &ffi_type_double;
00126     case Type::PointerTyID: return &ffi_type_pointer;
00127     default: break;
00128   }
00129   // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
00130   report_fatal_error("Type could not be mapped for use with libffi.");
00131   return NULL;
00132 }
00133 
00134 static void *ffiValueFor(Type *Ty, const GenericValue &AV,
00135                          void *ArgDataPtr) {
00136   switch (Ty->getTypeID()) {
00137     case Type::IntegerTyID:
00138       switch (cast<IntegerType>(Ty)->getBitWidth()) {
00139         case 8: {
00140           int8_t *I8Ptr = (int8_t *) ArgDataPtr;
00141           *I8Ptr = (int8_t) AV.IntVal.getZExtValue();
00142           return ArgDataPtr;
00143         }
00144         case 16: {
00145           int16_t *I16Ptr = (int16_t *) ArgDataPtr;
00146           *I16Ptr = (int16_t) AV.IntVal.getZExtValue();
00147           return ArgDataPtr;
00148         }
00149         case 32: {
00150           int32_t *I32Ptr = (int32_t *) ArgDataPtr;
00151           *I32Ptr = (int32_t) AV.IntVal.getZExtValue();
00152           return ArgDataPtr;
00153         }
00154         case 64: {
00155           int64_t *I64Ptr = (int64_t *) ArgDataPtr;
00156           *I64Ptr = (int64_t) AV.IntVal.getZExtValue();
00157           return ArgDataPtr;
00158         }
00159       }
00160     case Type::FloatTyID: {
00161       float *FloatPtr = (float *) ArgDataPtr;
00162       *FloatPtr = AV.FloatVal;
00163       return ArgDataPtr;
00164     }
00165     case Type::DoubleTyID: {
00166       double *DoublePtr = (double *) ArgDataPtr;
00167       *DoublePtr = AV.DoubleVal;
00168       return ArgDataPtr;
00169     }
00170     case Type::PointerTyID: {
00171       void **PtrPtr = (void **) ArgDataPtr;
00172       *PtrPtr = GVTOP(AV);
00173       return ArgDataPtr;
00174     }
00175     default: break;
00176   }
00177   // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
00178   report_fatal_error("Type value could not be mapped for use with libffi.");
00179   return NULL;
00180 }
00181 
00182 static bool ffiInvoke(RawFunc Fn, Function *F,
00183                       const std::vector<GenericValue> &ArgVals,
00184                       const DataLayout *TD, GenericValue &Result) {
00185   ffi_cif cif;
00186   FunctionType *FTy = F->getFunctionType();
00187   const unsigned NumArgs = F->arg_size();
00188 
00189   // TODO: We don't have type information about the remaining arguments, because
00190   // this information is never passed into ExecutionEngine::runFunction().
00191   if (ArgVals.size() > NumArgs && F->isVarArg()) {
00192     report_fatal_error("Calling external var arg function '" + F->getName()
00193                       + "' is not supported by the Interpreter.");
00194   }
00195 
00196   unsigned ArgBytes = 0;
00197 
00198   std::vector<ffi_type*> args(NumArgs);
00199   for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
00200        A != E; ++A) {
00201     const unsigned ArgNo = A->getArgNo();
00202     Type *ArgTy = FTy->getParamType(ArgNo);
00203     args[ArgNo] = ffiTypeFor(ArgTy);
00204     ArgBytes += TD->getTypeStoreSize(ArgTy);
00205   }
00206 
00207   SmallVector<uint8_t, 128> ArgData;
00208   ArgData.resize(ArgBytes);
00209   uint8_t *ArgDataPtr = ArgData.data();
00210   SmallVector<void*, 16> values(NumArgs);
00211   for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
00212        A != E; ++A) {
00213     const unsigned ArgNo = A->getArgNo();
00214     Type *ArgTy = FTy->getParamType(ArgNo);
00215     values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
00216     ArgDataPtr += TD->getTypeStoreSize(ArgTy);
00217   }
00218 
00219   Type *RetTy = FTy->getReturnType();
00220   ffi_type *rtype = ffiTypeFor(RetTy);
00221 
00222   if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) {
00223     SmallVector<uint8_t, 128> ret;
00224     if (RetTy->getTypeID() != Type::VoidTyID)
00225       ret.resize(TD->getTypeStoreSize(RetTy));
00226     ffi_call(&cif, Fn, ret.data(), values.data());
00227     switch (RetTy->getTypeID()) {
00228       case Type::IntegerTyID:
00229         switch (cast<IntegerType>(RetTy)->getBitWidth()) {
00230           case 8:  Result.IntVal = APInt(8 , *(int8_t *) ret.data()); break;
00231           case 16: Result.IntVal = APInt(16, *(int16_t*) ret.data()); break;
00232           case 32: Result.IntVal = APInt(32, *(int32_t*) ret.data()); break;
00233           case 64: Result.IntVal = APInt(64, *(int64_t*) ret.data()); break;
00234         }
00235         break;
00236       case Type::FloatTyID:   Result.FloatVal   = *(float *) ret.data(); break;
00237       case Type::DoubleTyID:  Result.DoubleVal  = *(double*) ret.data(); break;
00238       case Type::PointerTyID: Result.PointerVal = *(void **) ret.data(); break;
00239       default: break;
00240     }
00241     return true;
00242   }
00243 
00244   return false;
00245 }
00246 #endif // USE_LIBFFI
00247 
00248 GenericValue Interpreter::callExternalFunction(Function *F,
00249                                      const std::vector<GenericValue> &ArgVals) {
00250   TheInterpreter = this;
00251 
00252   unique_lock<sys::Mutex> Guard(*FunctionsLock);
00253 
00254   // Do a lookup to see if the function is in our cache... this should just be a
00255   // deferred annotation!
00256   std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F);
00257   if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F)
00258                                                    : FI->second) {
00259     Guard.unlock();
00260     return Fn(F->getFunctionType(), ArgVals);
00261   }
00262 
00263 #ifdef USE_LIBFFI
00264   std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
00265   RawFunc RawFn;
00266   if (RF == RawFunctions->end()) {
00267     RawFn = (RawFunc)(intptr_t)
00268       sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
00269     if (!RawFn)
00270       RawFn = (RawFunc)(intptr_t)getPointerToGlobalIfAvailable(F);
00271     if (RawFn != 0)
00272       RawFunctions->insert(std::make_pair(F, RawFn));  // Cache for later
00273   } else {
00274     RawFn = RF->second;
00275   }
00276 
00277   Guard.unlock();
00278 
00279   GenericValue Result;
00280   if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getDataLayout(), Result))
00281     return Result;
00282 #endif // USE_LIBFFI
00283 
00284   if (F->getName() == "__main")
00285     errs() << "Tried to execute an unknown external function: "
00286       << *F->getType() << " __main\n";
00287   else
00288     report_fatal_error("Tried to execute an unknown external function: " +
00289                        F->getName());
00290 #ifndef USE_LIBFFI
00291   errs() << "Recompiling LLVM with --enable-libffi might help.\n";
00292 #endif
00293   return GenericValue();
00294 }
00295 
00296 
00297 //===----------------------------------------------------------------------===//
00298 //  Functions "exported" to the running application...
00299 //
00300 
00301 // void atexit(Function*)
00302 static
00303 GenericValue lle_X_atexit(FunctionType *FT,
00304                           const std::vector<GenericValue> &Args) {
00305   assert(Args.size() == 1);
00306   TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
00307   GenericValue GV;
00308   GV.IntVal = 0;
00309   return GV;
00310 }
00311 
00312 // void exit(int)
00313 static
00314 GenericValue lle_X_exit(FunctionType *FT,
00315                         const std::vector<GenericValue> &Args) {
00316   TheInterpreter->exitCalled(Args[0]);
00317   return GenericValue();
00318 }
00319 
00320 // void abort(void)
00321 static
00322 GenericValue lle_X_abort(FunctionType *FT,
00323                          const std::vector<GenericValue> &Args) {
00324   //FIXME: should we report or raise here?
00325   //report_fatal_error("Interpreted program raised SIGABRT");
00326   raise (SIGABRT);
00327   return GenericValue();
00328 }
00329 
00330 // int sprintf(char *, const char *, ...) - a very rough implementation to make
00331 // output useful.
00332 static
00333 GenericValue lle_X_sprintf(FunctionType *FT,
00334                            const std::vector<GenericValue> &Args) {
00335   char *OutputBuffer = (char *)GVTOP(Args[0]);
00336   const char *FmtStr = (const char *)GVTOP(Args[1]);
00337   unsigned ArgNo = 2;
00338 
00339   // printf should return # chars printed.  This is completely incorrect, but
00340   // close enough for now.
00341   GenericValue GV;
00342   GV.IntVal = APInt(32, strlen(FmtStr));
00343   while (1) {
00344     switch (*FmtStr) {
00345     case 0: return GV;             // Null terminator...
00346     default:                       // Normal nonspecial character
00347       sprintf(OutputBuffer++, "%c", *FmtStr++);
00348       break;
00349     case '\\': {                   // Handle escape codes
00350       sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
00351       FmtStr += 2; OutputBuffer += 2;
00352       break;
00353     }
00354     case '%': {                    // Handle format specifiers
00355       char FmtBuf[100] = "", Buffer[1000] = "";
00356       char *FB = FmtBuf;
00357       *FB++ = *FmtStr++;
00358       char Last = *FB++ = *FmtStr++;
00359       unsigned HowLong = 0;
00360       while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
00361              Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
00362              Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
00363              Last != 'p' && Last != 's' && Last != '%') {
00364         if (Last == 'l' || Last == 'L') HowLong++;  // Keep track of l's
00365         Last = *FB++ = *FmtStr++;
00366       }
00367       *FB = 0;
00368 
00369       switch (Last) {
00370       case '%':
00371         memcpy(Buffer, "%", 2); break;
00372       case 'c':
00373         sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
00374         break;
00375       case 'd': case 'i':
00376       case 'u': case 'o':
00377       case 'x': case 'X':
00378         if (HowLong >= 1) {
00379           if (HowLong == 1 &&
00380               TheInterpreter->getDataLayout()->getPointerSizeInBits() == 64 &&
00381               sizeof(long) < sizeof(int64_t)) {
00382             // Make sure we use %lld with a 64 bit argument because we might be
00383             // compiling LLI on a 32 bit compiler.
00384             unsigned Size = strlen(FmtBuf);
00385             FmtBuf[Size] = FmtBuf[Size-1];
00386             FmtBuf[Size+1] = 0;
00387             FmtBuf[Size-1] = 'l';
00388           }
00389           sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
00390         } else
00391           sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
00392         break;
00393       case 'e': case 'E': case 'g': case 'G': case 'f':
00394         sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
00395       case 'p':
00396         sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
00397       case 's':
00398         sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
00399       default:
00400         errs() << "<unknown printf code '" << *FmtStr << "'!>";
00401         ArgNo++; break;
00402       }
00403       size_t Len = strlen(Buffer);
00404       memcpy(OutputBuffer, Buffer, Len + 1);
00405       OutputBuffer += Len;
00406       }
00407       break;
00408     }
00409   }
00410   return GV;
00411 }
00412 
00413 // int printf(const char *, ...) - a very rough implementation to make output
00414 // useful.
00415 static
00416 GenericValue lle_X_printf(FunctionType *FT,
00417                           const std::vector<GenericValue> &Args) {
00418   char Buffer[10000];
00419   std::vector<GenericValue> NewArgs;
00420   NewArgs.push_back(PTOGV((void*)&Buffer[0]));
00421   NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
00422   GenericValue GV = lle_X_sprintf(FT, NewArgs);
00423   outs() << Buffer;
00424   return GV;
00425 }
00426 
00427 // int sscanf(const char *format, ...);
00428 static
00429 GenericValue lle_X_sscanf(FunctionType *FT,
00430                           const std::vector<GenericValue> &args) {
00431   assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
00432 
00433   char *Args[10];
00434   for (unsigned i = 0; i < args.size(); ++i)
00435     Args[i] = (char*)GVTOP(args[i]);
00436 
00437   GenericValue GV;
00438   GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
00439                     Args[5], Args[6], Args[7], Args[8], Args[9]));
00440   return GV;
00441 }
00442 
00443 // int scanf(const char *format, ...);
00444 static
00445 GenericValue lle_X_scanf(FunctionType *FT,
00446                          const std::vector<GenericValue> &args) {
00447   assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
00448 
00449   char *Args[10];
00450   for (unsigned i = 0; i < args.size(); ++i)
00451     Args[i] = (char*)GVTOP(args[i]);
00452 
00453   GenericValue GV;
00454   GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
00455                     Args[5], Args[6], Args[7], Args[8], Args[9]));
00456   return GV;
00457 }
00458 
00459 // int fprintf(FILE *, const char *, ...) - a very rough implementation to make
00460 // output useful.
00461 static
00462 GenericValue lle_X_fprintf(FunctionType *FT,
00463                            const std::vector<GenericValue> &Args) {
00464   assert(Args.size() >= 2);
00465   char Buffer[10000];
00466   std::vector<GenericValue> NewArgs;
00467   NewArgs.push_back(PTOGV(Buffer));
00468   NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
00469   GenericValue GV = lle_X_sprintf(FT, NewArgs);
00470 
00471   fputs(Buffer, (FILE *) GVTOP(Args[0]));
00472   return GV;
00473 }
00474 
00475 static GenericValue lle_X_memset(FunctionType *FT,
00476                                  const std::vector<GenericValue> &Args) {
00477   int val = (int)Args[1].IntVal.getSExtValue();
00478   size_t len = (size_t)Args[2].IntVal.getZExtValue();
00479   memset((void *)GVTOP(Args[0]), val, len);
00480   // llvm.memset.* returns void, lle_X_* returns GenericValue,
00481   // so here we return GenericValue with IntVal set to zero
00482   GenericValue GV;
00483   GV.IntVal = 0;
00484   return GV;
00485 }
00486 
00487 static GenericValue lle_X_memcpy(FunctionType *FT,
00488                                  const std::vector<GenericValue> &Args) {
00489   memcpy(GVTOP(Args[0]), GVTOP(Args[1]),
00490          (size_t)(Args[2].IntVal.getLimitedValue()));
00491 
00492   // llvm.memcpy* returns void, lle_X_* returns GenericValue,
00493   // so here we return GenericValue with IntVal set to zero
00494   GenericValue GV;
00495   GV.IntVal = 0;
00496   return GV;
00497 }
00498 
00499 void Interpreter::initializeExternalFunctions() {
00500   sys::ScopedLock Writer(*FunctionsLock);
00501   (*FuncNames)["lle_X_atexit"]       = lle_X_atexit;
00502   (*FuncNames)["lle_X_exit"]         = lle_X_exit;
00503   (*FuncNames)["lle_X_abort"]        = lle_X_abort;
00504 
00505   (*FuncNames)["lle_X_printf"]       = lle_X_printf;
00506   (*FuncNames)["lle_X_sprintf"]      = lle_X_sprintf;
00507   (*FuncNames)["lle_X_sscanf"]       = lle_X_sscanf;
00508   (*FuncNames)["lle_X_scanf"]        = lle_X_scanf;
00509   (*FuncNames)["lle_X_fprintf"]      = lle_X_fprintf;
00510   (*FuncNames)["lle_X_memset"]       = lle_X_memset;
00511   (*FuncNames)["lle_X_memcpy"]       = lle_X_memcpy;
00512 }