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