ExternalFunctions.cpp revision 344779
1193323Sed//===-- ExternalFunctions.cpp - Implement External Functions --------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file contains both code to deal with invoking "external" functions, but 11193323Sed// also contains code that implements "exported" external functions. 12193323Sed// 13193323Sed// There are currently two mechanisms for handling external functions in the 14193323Sed// Interpreter. The first is to implement lle_* wrapper functions that are 15193323Sed// specific to well-known library functions which manually translate the 16193323Sed// arguments from GenericValues and make the call. If such a wrapper does 17193323Sed// not exist, and libffi is available, then the Interpreter will attempt to 18193323Sed// invoke the function using libffi, after finding its address. 19193323Sed// 20193323Sed//===----------------------------------------------------------------------===// 21193323Sed 22193323Sed#include "Interpreter.h" 23314564Sdim#include "llvm/ADT/APInt.h" 24314564Sdim#include "llvm/ADT/ArrayRef.h" 25321369Sdim#include "llvm/Config/config.h" // Detect libffi 26314564Sdim#include "llvm/ExecutionEngine/GenericValue.h" 27249423Sdim#include "llvm/IR/DataLayout.h" 28249423Sdim#include "llvm/IR/DerivedTypes.h" 29314564Sdim#include "llvm/IR/Function.h" 30314564Sdim#include "llvm/IR/Type.h" 31314564Sdim#include "llvm/Support/Casting.h" 32249423Sdim#include "llvm/Support/DynamicLibrary.h" 33198090Srdivacky#include "llvm/Support/ErrorHandling.h" 34193323Sed#include "llvm/Support/ManagedStatic.h" 35218893Sdim#include "llvm/Support/Mutex.h" 36321369Sdim#include "llvm/Support/UniqueLock.h" 37314564Sdim#include "llvm/Support/raw_ostream.h" 38314564Sdim#include <cassert> 39249423Sdim#include <cmath> 40193323Sed#include <csignal> 41314564Sdim#include <cstdint> 42193323Sed#include <cstdio> 43249423Sdim#include <cstring> 44193323Sed#include <map> 45314564Sdim#include <string> 46314564Sdim#include <utility> 47314564Sdim#include <vector> 48193323Sed 49193323Sed#ifdef HAVE_FFI_CALL 50193323Sed#ifdef HAVE_FFI_H 51193323Sed#include <ffi.h> 52193323Sed#define USE_LIBFFI 53193323Sed#elif HAVE_FFI_FFI_H 54193323Sed#include <ffi/ffi.h> 55193323Sed#define USE_LIBFFI 56193323Sed#endif 57193323Sed#endif 58193323Sed 59193323Sedusing namespace llvm; 60193323Sed 61194710Sedstatic ManagedStatic<sys::Mutex> FunctionsLock; 62194710Sed 63288943Sdimtypedef GenericValue (*ExFunc)(FunctionType *, ArrayRef<GenericValue>); 64193323Sedstatic ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions; 65280031Sdimstatic ManagedStatic<std::map<std::string, ExFunc> > FuncNames; 66193323Sed 67193323Sed#ifdef USE_LIBFFI 68198090Srdivackytypedef void (*RawFunc)(); 69193323Sedstatic ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions; 70193323Sed#endif 71193323Sed 72193323Sedstatic Interpreter *TheInterpreter; 73193323Sed 74226633Sdimstatic char getTypeID(Type *Ty) { 75193323Sed switch (Ty->getTypeID()) { 76193323Sed case Type::VoidTyID: return 'V'; 77193323Sed case Type::IntegerTyID: 78193323Sed switch (cast<IntegerType>(Ty)->getBitWidth()) { 79193323Sed case 1: return 'o'; 80193323Sed case 8: return 'B'; 81193323Sed case 16: return 'S'; 82193323Sed case 32: return 'I'; 83193323Sed case 64: return 'L'; 84193323Sed default: return 'N'; 85193323Sed } 86193323Sed case Type::FloatTyID: return 'F'; 87193323Sed case Type::DoubleTyID: return 'D'; 88193323Sed case Type::PointerTyID: return 'P'; 89193323Sed case Type::FunctionTyID:return 'M'; 90193323Sed case Type::StructTyID: return 'T'; 91193323Sed case Type::ArrayTyID: return 'A'; 92193323Sed default: return 'U'; 93193323Sed } 94193323Sed} 95193323Sed 96193323Sed// Try to find address of external function given a Function object. 97193323Sed// Please note, that interpreter doesn't know how to assemble a 98193323Sed// real call in general case (this is JIT job), that's why it assumes, 99193323Sed// that all external functions has the same (and pretty "general") signature. 100193323Sed// The typical example of such functions are "lle_X_" ones. 101193323Sedstatic ExFunc lookupFunction(const Function *F) { 102193323Sed // Function not found, look it up... start by figuring out what the 103193323Sed // composite function name should be. 104193323Sed std::string ExtName = "lle_"; 105226633Sdim FunctionType *FT = F->getFunctionType(); 106344779Sdim ExtName += getTypeID(FT->getReturnType()); 107344779Sdim for (Type *T : FT->params()) 108344779Sdim ExtName += getTypeID(T); 109288943Sdim ExtName += ("_" + F->getName()).str(); 110193323Sed 111198090Srdivacky sys::ScopedLock Writer(*FunctionsLock); 112280031Sdim ExFunc FnPtr = (*FuncNames)[ExtName]; 113276479Sdim if (!FnPtr) 114288943Sdim FnPtr = (*FuncNames)[("lle_X_" + F->getName()).str()]; 115276479Sdim if (!FnPtr) // Try calling a generic function... if it exists... 116288943Sdim FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol( 117288943Sdim ("lle_X_" + F->getName()).str()); 118276479Sdim if (FnPtr) 119193323Sed ExportedFunctions->insert(std::make_pair(F, FnPtr)); // Cache for later 120193323Sed return FnPtr; 121193323Sed} 122193323Sed 123193323Sed#ifdef USE_LIBFFI 124226633Sdimstatic ffi_type *ffiTypeFor(Type *Ty) { 125193323Sed switch (Ty->getTypeID()) { 126193323Sed case Type::VoidTyID: return &ffi_type_void; 127193323Sed case Type::IntegerTyID: 128193323Sed switch (cast<IntegerType>(Ty)->getBitWidth()) { 129193323Sed case 8: return &ffi_type_sint8; 130193323Sed case 16: return &ffi_type_sint16; 131193323Sed case 32: return &ffi_type_sint32; 132193323Sed case 64: return &ffi_type_sint64; 133193323Sed } 134193323Sed case Type::FloatTyID: return &ffi_type_float; 135193323Sed case Type::DoubleTyID: return &ffi_type_double; 136193323Sed case Type::PointerTyID: return &ffi_type_pointer; 137193323Sed default: break; 138193323Sed } 139193323Sed // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc. 140207618Srdivacky report_fatal_error("Type could not be mapped for use with libffi."); 141193323Sed return NULL; 142193323Sed} 143193323Sed 144226633Sdimstatic void *ffiValueFor(Type *Ty, const GenericValue &AV, 145193323Sed void *ArgDataPtr) { 146193323Sed switch (Ty->getTypeID()) { 147193323Sed case Type::IntegerTyID: 148193323Sed switch (cast<IntegerType>(Ty)->getBitWidth()) { 149193323Sed case 8: { 150193323Sed int8_t *I8Ptr = (int8_t *) ArgDataPtr; 151193323Sed *I8Ptr = (int8_t) AV.IntVal.getZExtValue(); 152193323Sed return ArgDataPtr; 153193323Sed } 154193323Sed case 16: { 155193323Sed int16_t *I16Ptr = (int16_t *) ArgDataPtr; 156193323Sed *I16Ptr = (int16_t) AV.IntVal.getZExtValue(); 157193323Sed return ArgDataPtr; 158193323Sed } 159193323Sed case 32: { 160193323Sed int32_t *I32Ptr = (int32_t *) ArgDataPtr; 161193323Sed *I32Ptr = (int32_t) AV.IntVal.getZExtValue(); 162193323Sed return ArgDataPtr; 163193323Sed } 164193323Sed case 64: { 165193323Sed int64_t *I64Ptr = (int64_t *) ArgDataPtr; 166193323Sed *I64Ptr = (int64_t) AV.IntVal.getZExtValue(); 167193323Sed return ArgDataPtr; 168193323Sed } 169193323Sed } 170193323Sed case Type::FloatTyID: { 171193323Sed float *FloatPtr = (float *) ArgDataPtr; 172199481Srdivacky *FloatPtr = AV.FloatVal; 173193323Sed return ArgDataPtr; 174193323Sed } 175193323Sed case Type::DoubleTyID: { 176193323Sed double *DoublePtr = (double *) ArgDataPtr; 177193323Sed *DoublePtr = AV.DoubleVal; 178193323Sed return ArgDataPtr; 179193323Sed } 180193323Sed case Type::PointerTyID: { 181193323Sed void **PtrPtr = (void **) ArgDataPtr; 182193323Sed *PtrPtr = GVTOP(AV); 183193323Sed return ArgDataPtr; 184193323Sed } 185193323Sed default: break; 186193323Sed } 187193323Sed // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc. 188207618Srdivacky report_fatal_error("Type value could not be mapped for use with libffi."); 189193323Sed return NULL; 190193323Sed} 191193323Sed 192288943Sdimstatic bool ffiInvoke(RawFunc Fn, Function *F, ArrayRef<GenericValue> ArgVals, 193296417Sdim const DataLayout &TD, GenericValue &Result) { 194193323Sed ffi_cif cif; 195226633Sdim FunctionType *FTy = F->getFunctionType(); 196193323Sed const unsigned NumArgs = F->arg_size(); 197193323Sed 198193323Sed // TODO: We don't have type information about the remaining arguments, because 199193323Sed // this information is never passed into ExecutionEngine::runFunction(). 200193323Sed if (ArgVals.size() > NumArgs && F->isVarArg()) { 201207618Srdivacky report_fatal_error("Calling external var arg function '" + F->getName() 202198090Srdivacky + "' is not supported by the Interpreter."); 203193323Sed } 204193323Sed 205193323Sed unsigned ArgBytes = 0; 206193323Sed 207193323Sed std::vector<ffi_type*> args(NumArgs); 208193323Sed for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end(); 209193323Sed A != E; ++A) { 210193323Sed const unsigned ArgNo = A->getArgNo(); 211226633Sdim Type *ArgTy = FTy->getParamType(ArgNo); 212193323Sed args[ArgNo] = ffiTypeFor(ArgTy); 213296417Sdim ArgBytes += TD.getTypeStoreSize(ArgTy); 214193323Sed } 215193323Sed 216198090Srdivacky SmallVector<uint8_t, 128> ArgData; 217198090Srdivacky ArgData.resize(ArgBytes); 218198090Srdivacky uint8_t *ArgDataPtr = ArgData.data(); 219198090Srdivacky SmallVector<void*, 16> values(NumArgs); 220193323Sed for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end(); 221193323Sed A != E; ++A) { 222193323Sed const unsigned ArgNo = A->getArgNo(); 223226633Sdim Type *ArgTy = FTy->getParamType(ArgNo); 224193323Sed values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr); 225296417Sdim ArgDataPtr += TD.getTypeStoreSize(ArgTy); 226193323Sed } 227193323Sed 228226633Sdim Type *RetTy = FTy->getReturnType(); 229193323Sed ffi_type *rtype = ffiTypeFor(RetTy); 230193323Sed 231344779Sdim if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, args.data()) == 232344779Sdim FFI_OK) { 233198090Srdivacky SmallVector<uint8_t, 128> ret; 234193323Sed if (RetTy->getTypeID() != Type::VoidTyID) 235296417Sdim ret.resize(TD.getTypeStoreSize(RetTy)); 236198090Srdivacky ffi_call(&cif, Fn, ret.data(), values.data()); 237193323Sed switch (RetTy->getTypeID()) { 238193323Sed case Type::IntegerTyID: 239193323Sed switch (cast<IntegerType>(RetTy)->getBitWidth()) { 240198090Srdivacky case 8: Result.IntVal = APInt(8 , *(int8_t *) ret.data()); break; 241198090Srdivacky case 16: Result.IntVal = APInt(16, *(int16_t*) ret.data()); break; 242198090Srdivacky case 32: Result.IntVal = APInt(32, *(int32_t*) ret.data()); break; 243198090Srdivacky case 64: Result.IntVal = APInt(64, *(int64_t*) ret.data()); break; 244193323Sed } 245193323Sed break; 246198090Srdivacky case Type::FloatTyID: Result.FloatVal = *(float *) ret.data(); break; 247198090Srdivacky case Type::DoubleTyID: Result.DoubleVal = *(double*) ret.data(); break; 248198090Srdivacky case Type::PointerTyID: Result.PointerVal = *(void **) ret.data(); break; 249193323Sed default: break; 250193323Sed } 251193323Sed return true; 252193323Sed } 253193323Sed 254193323Sed return false; 255193323Sed} 256193323Sed#endif // USE_LIBFFI 257193323Sed 258193323SedGenericValue Interpreter::callExternalFunction(Function *F, 259288943Sdim ArrayRef<GenericValue> ArgVals) { 260193323Sed TheInterpreter = this; 261193323Sed 262280031Sdim unique_lock<sys::Mutex> Guard(*FunctionsLock); 263194710Sed 264193323Sed // Do a lookup to see if the function is in our cache... this should just be a 265193323Sed // deferred annotation! 266193323Sed std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F); 267193323Sed if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F) 268194710Sed : FI->second) { 269280031Sdim Guard.unlock(); 270193323Sed return Fn(F->getFunctionType(), ArgVals); 271194710Sed } 272193323Sed 273193323Sed#ifdef USE_LIBFFI 274193323Sed std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F); 275193323Sed RawFunc RawFn; 276193323Sed if (RF == RawFunctions->end()) { 277193323Sed RawFn = (RawFunc)(intptr_t) 278193323Sed sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName()); 279206083Srdivacky if (!RawFn) 280210299Sed RawFn = (RawFunc)(intptr_t)getPointerToGlobalIfAvailable(F); 281193323Sed if (RawFn != 0) 282193323Sed RawFunctions->insert(std::make_pair(F, RawFn)); // Cache for later 283193323Sed } else { 284193323Sed RawFn = RF->second; 285193323Sed } 286198090Srdivacky 287280031Sdim Guard.unlock(); 288193323Sed 289193323Sed GenericValue Result; 290243830Sdim if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getDataLayout(), Result)) 291193323Sed return Result; 292193323Sed#endif // USE_LIBFFI 293193323Sed 294198090Srdivacky if (F->getName() == "__main") 295198090Srdivacky errs() << "Tried to execute an unknown external function: " 296224145Sdim << *F->getType() << " __main\n"; 297198090Srdivacky else 298207618Srdivacky report_fatal_error("Tried to execute an unknown external function: " + 299224145Sdim F->getName()); 300199481Srdivacky#ifndef USE_LIBFFI 301199481Srdivacky errs() << "Recompiling LLVM with --enable-libffi might help.\n"; 302199481Srdivacky#endif 303193323Sed return GenericValue(); 304193323Sed} 305193323Sed 306193323Sed//===----------------------------------------------------------------------===// 307193323Sed// Functions "exported" to the running application... 308193323Sed// 309198090Srdivacky 310193323Sed// void atexit(Function*) 311288943Sdimstatic GenericValue lle_X_atexit(FunctionType *FT, 312288943Sdim ArrayRef<GenericValue> Args) { 313193323Sed assert(Args.size() == 1); 314193323Sed TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0])); 315193323Sed GenericValue GV; 316193323Sed GV.IntVal = 0; 317193323Sed return GV; 318193323Sed} 319193323Sed 320193323Sed// void exit(int) 321288943Sdimstatic GenericValue lle_X_exit(FunctionType *FT, ArrayRef<GenericValue> Args) { 322193323Sed TheInterpreter->exitCalled(Args[0]); 323193323Sed return GenericValue(); 324193323Sed} 325193323Sed 326193323Sed// void abort(void) 327288943Sdimstatic GenericValue lle_X_abort(FunctionType *FT, ArrayRef<GenericValue> Args) { 328198090Srdivacky //FIXME: should we report or raise here? 329207618Srdivacky //report_fatal_error("Interpreted program raised SIGABRT"); 330193323Sed raise (SIGABRT); 331193323Sed return GenericValue(); 332193323Sed} 333193323Sed 334193323Sed// int sprintf(char *, const char *, ...) - a very rough implementation to make 335193323Sed// output useful. 336288943Sdimstatic GenericValue lle_X_sprintf(FunctionType *FT, 337288943Sdim ArrayRef<GenericValue> Args) { 338193323Sed char *OutputBuffer = (char *)GVTOP(Args[0]); 339193323Sed const char *FmtStr = (const char *)GVTOP(Args[1]); 340193323Sed unsigned ArgNo = 2; 341193323Sed 342193323Sed // printf should return # chars printed. This is completely incorrect, but 343193323Sed // close enough for now. 344198090Srdivacky GenericValue GV; 345193323Sed GV.IntVal = APInt(32, strlen(FmtStr)); 346314564Sdim while (true) { 347193323Sed switch (*FmtStr) { 348193323Sed case 0: return GV; // Null terminator... 349193323Sed default: // Normal nonspecial character 350193323Sed sprintf(OutputBuffer++, "%c", *FmtStr++); 351193323Sed break; 352193323Sed case '\\': { // Handle escape codes 353193323Sed sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1)); 354193323Sed FmtStr += 2; OutputBuffer += 2; 355193323Sed break; 356193323Sed } 357193323Sed case '%': { // Handle format specifiers 358193323Sed char FmtBuf[100] = "", Buffer[1000] = ""; 359193323Sed char *FB = FmtBuf; 360193323Sed *FB++ = *FmtStr++; 361193323Sed char Last = *FB++ = *FmtStr++; 362193323Sed unsigned HowLong = 0; 363193323Sed while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' && 364193323Sed Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' && 365193323Sed Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' && 366193323Sed Last != 'p' && Last != 's' && Last != '%') { 367193323Sed if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's 368193323Sed Last = *FB++ = *FmtStr++; 369193323Sed } 370193323Sed *FB = 0; 371193323Sed 372193323Sed switch (Last) { 373193323Sed case '%': 374203954Srdivacky memcpy(Buffer, "%", 2); break; 375193323Sed case 'c': 376193323Sed sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue())); 377193323Sed break; 378193323Sed case 'd': case 'i': 379193323Sed case 'u': case 'o': 380193323Sed case 'x': case 'X': 381193323Sed if (HowLong >= 1) { 382193323Sed if (HowLong == 1 && 383296417Sdim TheInterpreter->getDataLayout().getPointerSizeInBits() == 64 && 384193323Sed sizeof(long) < sizeof(int64_t)) { 385193323Sed // Make sure we use %lld with a 64 bit argument because we might be 386193323Sed // compiling LLI on a 32 bit compiler. 387193323Sed unsigned Size = strlen(FmtBuf); 388193323Sed FmtBuf[Size] = FmtBuf[Size-1]; 389193323Sed FmtBuf[Size+1] = 0; 390193323Sed FmtBuf[Size-1] = 'l'; 391193323Sed } 392193323Sed sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue()); 393193323Sed } else 394193323Sed sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue())); 395193323Sed break; 396193323Sed case 'e': case 'E': case 'g': case 'G': case 'f': 397193323Sed sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break; 398193323Sed case 'p': 399193323Sed sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break; 400193323Sed case 's': 401193323Sed sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break; 402198090Srdivacky default: 403198090Srdivacky errs() << "<unknown printf code '" << *FmtStr << "'!>"; 404193323Sed ArgNo++; break; 405193323Sed } 406203954Srdivacky size_t Len = strlen(Buffer); 407203954Srdivacky memcpy(OutputBuffer, Buffer, Len + 1); 408203954Srdivacky OutputBuffer += Len; 409193323Sed } 410193323Sed break; 411193323Sed } 412193323Sed } 413261991Sdim return GV; 414193323Sed} 415193323Sed 416193323Sed// int printf(const char *, ...) - a very rough implementation to make output 417193323Sed// useful. 418288943Sdimstatic GenericValue lle_X_printf(FunctionType *FT, 419288943Sdim ArrayRef<GenericValue> Args) { 420193323Sed char Buffer[10000]; 421193323Sed std::vector<GenericValue> NewArgs; 422193323Sed NewArgs.push_back(PTOGV((void*)&Buffer[0])); 423193323Sed NewArgs.insert(NewArgs.end(), Args.begin(), Args.end()); 424193323Sed GenericValue GV = lle_X_sprintf(FT, NewArgs); 425198090Srdivacky outs() << Buffer; 426193323Sed return GV; 427193323Sed} 428193323Sed 429193323Sed// int sscanf(const char *format, ...); 430288943Sdimstatic GenericValue lle_X_sscanf(FunctionType *FT, 431288943Sdim ArrayRef<GenericValue> args) { 432193323Sed assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!"); 433193323Sed 434193323Sed char *Args[10]; 435193323Sed for (unsigned i = 0; i < args.size(); ++i) 436193323Sed Args[i] = (char*)GVTOP(args[i]); 437193323Sed 438193323Sed GenericValue GV; 439193323Sed GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4], 440261991Sdim Args[5], Args[6], Args[7], Args[8], Args[9])); 441193323Sed return GV; 442193323Sed} 443193323Sed 444193323Sed// int scanf(const char *format, ...); 445288943Sdimstatic GenericValue lle_X_scanf(FunctionType *FT, ArrayRef<GenericValue> args) { 446193323Sed assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!"); 447193323Sed 448193323Sed char *Args[10]; 449193323Sed for (unsigned i = 0; i < args.size(); ++i) 450193323Sed Args[i] = (char*)GVTOP(args[i]); 451193323Sed 452193323Sed GenericValue GV; 453193323Sed GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4], 454261991Sdim Args[5], Args[6], Args[7], Args[8], Args[9])); 455193323Sed return GV; 456193323Sed} 457193323Sed 458193323Sed// int fprintf(FILE *, const char *, ...) - a very rough implementation to make 459193323Sed// output useful. 460288943Sdimstatic GenericValue lle_X_fprintf(FunctionType *FT, 461288943Sdim ArrayRef<GenericValue> Args) { 462193323Sed assert(Args.size() >= 2); 463193323Sed char Buffer[10000]; 464193323Sed std::vector<GenericValue> NewArgs; 465193323Sed NewArgs.push_back(PTOGV(Buffer)); 466193323Sed NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end()); 467193323Sed GenericValue GV = lle_X_sprintf(FT, NewArgs); 468193323Sed 469193323Sed fputs(Buffer, (FILE *) GVTOP(Args[0])); 470193323Sed return GV; 471193323Sed} 472193323Sed 473261991Sdimstatic GenericValue lle_X_memset(FunctionType *FT, 474288943Sdim ArrayRef<GenericValue> Args) { 475261991Sdim int val = (int)Args[1].IntVal.getSExtValue(); 476261991Sdim size_t len = (size_t)Args[2].IntVal.getZExtValue(); 477261991Sdim memset((void *)GVTOP(Args[0]), val, len); 478261991Sdim // llvm.memset.* returns void, lle_X_* returns GenericValue, 479261991Sdim // so here we return GenericValue with IntVal set to zero 480261991Sdim GenericValue GV; 481261991Sdim GV.IntVal = 0; 482261991Sdim return GV; 483261991Sdim} 484261991Sdim 485261991Sdimstatic GenericValue lle_X_memcpy(FunctionType *FT, 486288943Sdim ArrayRef<GenericValue> Args) { 487261991Sdim memcpy(GVTOP(Args[0]), GVTOP(Args[1]), 488261991Sdim (size_t)(Args[2].IntVal.getLimitedValue())); 489261991Sdim 490261991Sdim // llvm.memcpy* returns void, lle_X_* returns GenericValue, 491261991Sdim // so here we return GenericValue with IntVal set to zero 492261991Sdim GenericValue GV; 493261991Sdim GV.IntVal = 0; 494261991Sdim return GV; 495261991Sdim} 496261991Sdim 497193323Sedvoid Interpreter::initializeExternalFunctions() { 498198090Srdivacky sys::ScopedLock Writer(*FunctionsLock); 499280031Sdim (*FuncNames)["lle_X_atexit"] = lle_X_atexit; 500280031Sdim (*FuncNames)["lle_X_exit"] = lle_X_exit; 501280031Sdim (*FuncNames)["lle_X_abort"] = lle_X_abort; 502193323Sed 503280031Sdim (*FuncNames)["lle_X_printf"] = lle_X_printf; 504280031Sdim (*FuncNames)["lle_X_sprintf"] = lle_X_sprintf; 505280031Sdim (*FuncNames)["lle_X_sscanf"] = lle_X_sscanf; 506280031Sdim (*FuncNames)["lle_X_scanf"] = lle_X_scanf; 507280031Sdim (*FuncNames)["lle_X_fprintf"] = lle_X_fprintf; 508280031Sdim (*FuncNames)["lle_X_memset"] = lle_X_memset; 509280031Sdim (*FuncNames)["lle_X_memcpy"] = lle_X_memcpy; 510193323Sed} 511