ExternalFunctions.cpp revision 276479
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" 23193323Sed#include "llvm/Config/config.h" // Detect libffi 24249423Sdim#include "llvm/IR/DataLayout.h" 25249423Sdim#include "llvm/IR/DerivedTypes.h" 26249423Sdim#include "llvm/IR/Module.h" 27249423Sdim#include "llvm/Support/DynamicLibrary.h" 28198090Srdivacky#include "llvm/Support/ErrorHandling.h" 29193323Sed#include "llvm/Support/ManagedStatic.h" 30218893Sdim#include "llvm/Support/Mutex.h" 31249423Sdim#include <cmath> 32193323Sed#include <csignal> 33193323Sed#include <cstdio> 34249423Sdim#include <cstring> 35193323Sed#include <map> 36193323Sed 37193323Sed#ifdef HAVE_FFI_CALL 38193323Sed#ifdef HAVE_FFI_H 39193323Sed#include <ffi.h> 40193323Sed#define USE_LIBFFI 41193323Sed#elif HAVE_FFI_FFI_H 42193323Sed#include <ffi/ffi.h> 43193323Sed#define USE_LIBFFI 44193323Sed#endif 45193323Sed#endif 46193323Sed 47193323Sedusing namespace llvm; 48193323Sed 49194710Sedstatic ManagedStatic<sys::Mutex> FunctionsLock; 50194710Sed 51226633Sdimtypedef GenericValue (*ExFunc)(FunctionType *, 52193323Sed const std::vector<GenericValue> &); 53193323Sedstatic ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions; 54193323Sedstatic std::map<std::string, ExFunc> FuncNames; 55193323Sed 56193323Sed#ifdef USE_LIBFFI 57198090Srdivackytypedef void (*RawFunc)(); 58193323Sedstatic ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions; 59193323Sed#endif 60193323Sed 61193323Sedstatic Interpreter *TheInterpreter; 62193323Sed 63226633Sdimstatic char getTypeID(Type *Ty) { 64193323Sed switch (Ty->getTypeID()) { 65193323Sed case Type::VoidTyID: return 'V'; 66193323Sed case Type::IntegerTyID: 67193323Sed switch (cast<IntegerType>(Ty)->getBitWidth()) { 68193323Sed case 1: return 'o'; 69193323Sed case 8: return 'B'; 70193323Sed case 16: return 'S'; 71193323Sed case 32: return 'I'; 72193323Sed case 64: return 'L'; 73193323Sed default: return 'N'; 74193323Sed } 75193323Sed case Type::FloatTyID: return 'F'; 76193323Sed case Type::DoubleTyID: return 'D'; 77193323Sed case Type::PointerTyID: return 'P'; 78193323Sed case Type::FunctionTyID:return 'M'; 79193323Sed case Type::StructTyID: return 'T'; 80193323Sed case Type::ArrayTyID: return 'A'; 81193323Sed default: return 'U'; 82193323Sed } 83193323Sed} 84193323Sed 85193323Sed// Try to find address of external function given a Function object. 86193323Sed// Please note, that interpreter doesn't know how to assemble a 87193323Sed// real call in general case (this is JIT job), that's why it assumes, 88193323Sed// that all external functions has the same (and pretty "general") signature. 89193323Sed// The typical example of such functions are "lle_X_" ones. 90193323Sedstatic ExFunc lookupFunction(const Function *F) { 91193323Sed // Function not found, look it up... start by figuring out what the 92193323Sed // composite function name should be. 93193323Sed std::string ExtName = "lle_"; 94226633Sdim FunctionType *FT = F->getFunctionType(); 95193323Sed for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i) 96193323Sed ExtName += getTypeID(FT->getContainedType(i)); 97234353Sdim ExtName += "_" + F->getName().str(); 98193323Sed 99198090Srdivacky sys::ScopedLock Writer(*FunctionsLock); 100193323Sed ExFunc FnPtr = FuncNames[ExtName]; 101276479Sdim if (!FnPtr) 102234353Sdim FnPtr = FuncNames["lle_X_" + F->getName().str()]; 103276479Sdim if (!FnPtr) // Try calling a generic function... if it exists... 104198090Srdivacky FnPtr = (ExFunc)(intptr_t) 105234353Sdim sys::DynamicLibrary::SearchForAddressOfSymbol("lle_X_" + 106234353Sdim F->getName().str()); 107276479Sdim if (FnPtr) 108193323Sed ExportedFunctions->insert(std::make_pair(F, FnPtr)); // Cache for later 109193323Sed return FnPtr; 110193323Sed} 111193323Sed 112193323Sed#ifdef USE_LIBFFI 113226633Sdimstatic ffi_type *ffiTypeFor(Type *Ty) { 114193323Sed switch (Ty->getTypeID()) { 115193323Sed case Type::VoidTyID: return &ffi_type_void; 116193323Sed case Type::IntegerTyID: 117193323Sed switch (cast<IntegerType>(Ty)->getBitWidth()) { 118193323Sed case 8: return &ffi_type_sint8; 119193323Sed case 16: return &ffi_type_sint16; 120193323Sed case 32: return &ffi_type_sint32; 121193323Sed case 64: return &ffi_type_sint64; 122193323Sed } 123193323Sed case Type::FloatTyID: return &ffi_type_float; 124193323Sed case Type::DoubleTyID: return &ffi_type_double; 125193323Sed case Type::PointerTyID: return &ffi_type_pointer; 126193323Sed default: break; 127193323Sed } 128193323Sed // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc. 129207618Srdivacky report_fatal_error("Type could not be mapped for use with libffi."); 130193323Sed return NULL; 131193323Sed} 132193323Sed 133226633Sdimstatic void *ffiValueFor(Type *Ty, const GenericValue &AV, 134193323Sed void *ArgDataPtr) { 135193323Sed switch (Ty->getTypeID()) { 136193323Sed case Type::IntegerTyID: 137193323Sed switch (cast<IntegerType>(Ty)->getBitWidth()) { 138193323Sed case 8: { 139193323Sed int8_t *I8Ptr = (int8_t *) ArgDataPtr; 140193323Sed *I8Ptr = (int8_t) AV.IntVal.getZExtValue(); 141193323Sed return ArgDataPtr; 142193323Sed } 143193323Sed case 16: { 144193323Sed int16_t *I16Ptr = (int16_t *) ArgDataPtr; 145193323Sed *I16Ptr = (int16_t) AV.IntVal.getZExtValue(); 146193323Sed return ArgDataPtr; 147193323Sed } 148193323Sed case 32: { 149193323Sed int32_t *I32Ptr = (int32_t *) ArgDataPtr; 150193323Sed *I32Ptr = (int32_t) AV.IntVal.getZExtValue(); 151193323Sed return ArgDataPtr; 152193323Sed } 153193323Sed case 64: { 154193323Sed int64_t *I64Ptr = (int64_t *) ArgDataPtr; 155193323Sed *I64Ptr = (int64_t) AV.IntVal.getZExtValue(); 156193323Sed return ArgDataPtr; 157193323Sed } 158193323Sed } 159193323Sed case Type::FloatTyID: { 160193323Sed float *FloatPtr = (float *) ArgDataPtr; 161199481Srdivacky *FloatPtr = AV.FloatVal; 162193323Sed return ArgDataPtr; 163193323Sed } 164193323Sed case Type::DoubleTyID: { 165193323Sed double *DoublePtr = (double *) ArgDataPtr; 166193323Sed *DoublePtr = AV.DoubleVal; 167193323Sed return ArgDataPtr; 168193323Sed } 169193323Sed case Type::PointerTyID: { 170193323Sed void **PtrPtr = (void **) ArgDataPtr; 171193323Sed *PtrPtr = GVTOP(AV); 172193323Sed return ArgDataPtr; 173193323Sed } 174193323Sed default: break; 175193323Sed } 176193323Sed // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc. 177207618Srdivacky report_fatal_error("Type value could not be mapped for use with libffi."); 178193323Sed return NULL; 179193323Sed} 180193323Sed 181193323Sedstatic bool ffiInvoke(RawFunc Fn, Function *F, 182193323Sed const std::vector<GenericValue> &ArgVals, 183243830Sdim const DataLayout *TD, GenericValue &Result) { 184193323Sed ffi_cif cif; 185226633Sdim FunctionType *FTy = F->getFunctionType(); 186193323Sed const unsigned NumArgs = F->arg_size(); 187193323Sed 188193323Sed // TODO: We don't have type information about the remaining arguments, because 189193323Sed // this information is never passed into ExecutionEngine::runFunction(). 190193323Sed if (ArgVals.size() > NumArgs && F->isVarArg()) { 191207618Srdivacky report_fatal_error("Calling external var arg function '" + F->getName() 192198090Srdivacky + "' is not supported by the Interpreter."); 193193323Sed } 194193323Sed 195193323Sed unsigned ArgBytes = 0; 196193323Sed 197193323Sed std::vector<ffi_type*> args(NumArgs); 198193323Sed for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end(); 199193323Sed A != E; ++A) { 200193323Sed const unsigned ArgNo = A->getArgNo(); 201226633Sdim Type *ArgTy = FTy->getParamType(ArgNo); 202193323Sed args[ArgNo] = ffiTypeFor(ArgTy); 203193323Sed ArgBytes += TD->getTypeStoreSize(ArgTy); 204193323Sed } 205193323Sed 206198090Srdivacky SmallVector<uint8_t, 128> ArgData; 207198090Srdivacky ArgData.resize(ArgBytes); 208198090Srdivacky uint8_t *ArgDataPtr = ArgData.data(); 209198090Srdivacky SmallVector<void*, 16> values(NumArgs); 210193323Sed for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end(); 211193323Sed A != E; ++A) { 212193323Sed const unsigned ArgNo = A->getArgNo(); 213226633Sdim Type *ArgTy = FTy->getParamType(ArgNo); 214193323Sed values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr); 215193323Sed ArgDataPtr += TD->getTypeStoreSize(ArgTy); 216193323Sed } 217193323Sed 218226633Sdim Type *RetTy = FTy->getReturnType(); 219193323Sed ffi_type *rtype = ffiTypeFor(RetTy); 220193323Sed 221193323Sed if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) { 222198090Srdivacky SmallVector<uint8_t, 128> ret; 223193323Sed if (RetTy->getTypeID() != Type::VoidTyID) 224198090Srdivacky ret.resize(TD->getTypeStoreSize(RetTy)); 225198090Srdivacky ffi_call(&cif, Fn, ret.data(), values.data()); 226193323Sed switch (RetTy->getTypeID()) { 227193323Sed case Type::IntegerTyID: 228193323Sed switch (cast<IntegerType>(RetTy)->getBitWidth()) { 229198090Srdivacky case 8: Result.IntVal = APInt(8 , *(int8_t *) ret.data()); break; 230198090Srdivacky case 16: Result.IntVal = APInt(16, *(int16_t*) ret.data()); break; 231198090Srdivacky case 32: Result.IntVal = APInt(32, *(int32_t*) ret.data()); break; 232198090Srdivacky case 64: Result.IntVal = APInt(64, *(int64_t*) ret.data()); break; 233193323Sed } 234193323Sed break; 235198090Srdivacky case Type::FloatTyID: Result.FloatVal = *(float *) ret.data(); break; 236198090Srdivacky case Type::DoubleTyID: Result.DoubleVal = *(double*) ret.data(); break; 237198090Srdivacky case Type::PointerTyID: Result.PointerVal = *(void **) ret.data(); break; 238193323Sed default: break; 239193323Sed } 240193323Sed return true; 241193323Sed } 242193323Sed 243193323Sed return false; 244193323Sed} 245193323Sed#endif // USE_LIBFFI 246193323Sed 247193323SedGenericValue Interpreter::callExternalFunction(Function *F, 248193323Sed const std::vector<GenericValue> &ArgVals) { 249193323Sed TheInterpreter = this; 250193323Sed 251194710Sed FunctionsLock->acquire(); 252194710Sed 253193323Sed // Do a lookup to see if the function is in our cache... this should just be a 254193323Sed // deferred annotation! 255193323Sed std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F); 256193323Sed if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F) 257194710Sed : FI->second) { 258194710Sed FunctionsLock->release(); 259193323Sed return Fn(F->getFunctionType(), ArgVals); 260194710Sed } 261193323Sed 262193323Sed#ifdef USE_LIBFFI 263193323Sed std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F); 264193323Sed RawFunc RawFn; 265193323Sed if (RF == RawFunctions->end()) { 266193323Sed RawFn = (RawFunc)(intptr_t) 267193323Sed sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName()); 268206083Srdivacky if (!RawFn) 269210299Sed RawFn = (RawFunc)(intptr_t)getPointerToGlobalIfAvailable(F); 270193323Sed if (RawFn != 0) 271193323Sed RawFunctions->insert(std::make_pair(F, RawFn)); // Cache for later 272193323Sed } else { 273193323Sed RawFn = RF->second; 274193323Sed } 275198090Srdivacky 276194710Sed FunctionsLock->release(); 277193323Sed 278193323Sed GenericValue Result; 279243830Sdim if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getDataLayout(), Result)) 280193323Sed return Result; 281193323Sed#endif // USE_LIBFFI 282193323Sed 283198090Srdivacky if (F->getName() == "__main") 284198090Srdivacky errs() << "Tried to execute an unknown external function: " 285224145Sdim << *F->getType() << " __main\n"; 286198090Srdivacky else 287207618Srdivacky report_fatal_error("Tried to execute an unknown external function: " + 288224145Sdim F->getName()); 289199481Srdivacky#ifndef USE_LIBFFI 290199481Srdivacky errs() << "Recompiling LLVM with --enable-libffi might help.\n"; 291199481Srdivacky#endif 292193323Sed return GenericValue(); 293193323Sed} 294193323Sed 295193323Sed 296193323Sed//===----------------------------------------------------------------------===// 297193323Sed// Functions "exported" to the running application... 298193323Sed// 299198090Srdivacky 300193323Sed// void atexit(Function*) 301234353Sdimstatic 302226633SdimGenericValue lle_X_atexit(FunctionType *FT, 303193323Sed const std::vector<GenericValue> &Args) { 304193323Sed assert(Args.size() == 1); 305193323Sed TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0])); 306193323Sed GenericValue GV; 307193323Sed GV.IntVal = 0; 308193323Sed return GV; 309193323Sed} 310193323Sed 311193323Sed// void exit(int) 312234353Sdimstatic 313226633SdimGenericValue lle_X_exit(FunctionType *FT, 314193323Sed const std::vector<GenericValue> &Args) { 315193323Sed TheInterpreter->exitCalled(Args[0]); 316193323Sed return GenericValue(); 317193323Sed} 318193323Sed 319193323Sed// void abort(void) 320234353Sdimstatic 321226633SdimGenericValue lle_X_abort(FunctionType *FT, 322193323Sed const std::vector<GenericValue> &Args) { 323198090Srdivacky //FIXME: should we report or raise here? 324207618Srdivacky //report_fatal_error("Interpreted program raised SIGABRT"); 325193323Sed raise (SIGABRT); 326193323Sed return GenericValue(); 327193323Sed} 328193323Sed 329193323Sed// int sprintf(char *, const char *, ...) - a very rough implementation to make 330193323Sed// output useful. 331234353Sdimstatic 332226633SdimGenericValue lle_X_sprintf(FunctionType *FT, 333193323Sed const std::vector<GenericValue> &Args) { 334193323Sed char *OutputBuffer = (char *)GVTOP(Args[0]); 335193323Sed const char *FmtStr = (const char *)GVTOP(Args[1]); 336193323Sed unsigned ArgNo = 2; 337193323Sed 338193323Sed // printf should return # chars printed. This is completely incorrect, but 339193323Sed // close enough for now. 340198090Srdivacky GenericValue GV; 341193323Sed GV.IntVal = APInt(32, strlen(FmtStr)); 342193323Sed while (1) { 343193323Sed switch (*FmtStr) { 344193323Sed case 0: return GV; // Null terminator... 345193323Sed default: // Normal nonspecial character 346193323Sed sprintf(OutputBuffer++, "%c", *FmtStr++); 347193323Sed break; 348193323Sed case '\\': { // Handle escape codes 349193323Sed sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1)); 350193323Sed FmtStr += 2; OutputBuffer += 2; 351193323Sed break; 352193323Sed } 353193323Sed case '%': { // Handle format specifiers 354193323Sed char FmtBuf[100] = "", Buffer[1000] = ""; 355193323Sed char *FB = FmtBuf; 356193323Sed *FB++ = *FmtStr++; 357193323Sed char Last = *FB++ = *FmtStr++; 358193323Sed unsigned HowLong = 0; 359193323Sed while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' && 360193323Sed Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' && 361193323Sed Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' && 362193323Sed Last != 'p' && Last != 's' && Last != '%') { 363193323Sed if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's 364193323Sed Last = *FB++ = *FmtStr++; 365193323Sed } 366193323Sed *FB = 0; 367193323Sed 368193323Sed switch (Last) { 369193323Sed case '%': 370203954Srdivacky memcpy(Buffer, "%", 2); break; 371193323Sed case 'c': 372193323Sed sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue())); 373193323Sed break; 374193323Sed case 'd': case 'i': 375193323Sed case 'u': case 'o': 376193323Sed case 'x': case 'X': 377193323Sed if (HowLong >= 1) { 378193323Sed if (HowLong == 1 && 379243830Sdim TheInterpreter->getDataLayout()->getPointerSizeInBits() == 64 && 380193323Sed sizeof(long) < sizeof(int64_t)) { 381193323Sed // Make sure we use %lld with a 64 bit argument because we might be 382193323Sed // compiling LLI on a 32 bit compiler. 383193323Sed unsigned Size = strlen(FmtBuf); 384193323Sed FmtBuf[Size] = FmtBuf[Size-1]; 385193323Sed FmtBuf[Size+1] = 0; 386193323Sed FmtBuf[Size-1] = 'l'; 387193323Sed } 388193323Sed sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue()); 389193323Sed } else 390193323Sed sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue())); 391193323Sed break; 392193323Sed case 'e': case 'E': case 'g': case 'G': case 'f': 393193323Sed sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break; 394193323Sed case 'p': 395193323Sed sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break; 396193323Sed case 's': 397193323Sed sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break; 398198090Srdivacky default: 399198090Srdivacky errs() << "<unknown printf code '" << *FmtStr << "'!>"; 400193323Sed ArgNo++; break; 401193323Sed } 402203954Srdivacky size_t Len = strlen(Buffer); 403203954Srdivacky memcpy(OutputBuffer, Buffer, Len + 1); 404203954Srdivacky OutputBuffer += Len; 405193323Sed } 406193323Sed break; 407193323Sed } 408193323Sed } 409261991Sdim return GV; 410193323Sed} 411193323Sed 412193323Sed// int printf(const char *, ...) - a very rough implementation to make output 413193323Sed// useful. 414234353Sdimstatic 415226633SdimGenericValue lle_X_printf(FunctionType *FT, 416193323Sed const std::vector<GenericValue> &Args) { 417193323Sed char Buffer[10000]; 418193323Sed std::vector<GenericValue> NewArgs; 419193323Sed NewArgs.push_back(PTOGV((void*)&Buffer[0])); 420193323Sed NewArgs.insert(NewArgs.end(), Args.begin(), Args.end()); 421193323Sed GenericValue GV = lle_X_sprintf(FT, NewArgs); 422198090Srdivacky outs() << Buffer; 423193323Sed return GV; 424193323Sed} 425193323Sed 426193323Sed// int sscanf(const char *format, ...); 427234353Sdimstatic 428226633SdimGenericValue lle_X_sscanf(FunctionType *FT, 429193323Sed const std::vector<GenericValue> &args) { 430193323Sed assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!"); 431193323Sed 432193323Sed char *Args[10]; 433193323Sed for (unsigned i = 0; i < args.size(); ++i) 434193323Sed Args[i] = (char*)GVTOP(args[i]); 435193323Sed 436193323Sed GenericValue GV; 437193323Sed GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4], 438261991Sdim Args[5], Args[6], Args[7], Args[8], Args[9])); 439193323Sed return GV; 440193323Sed} 441193323Sed 442193323Sed// int scanf(const char *format, ...); 443234353Sdimstatic 444226633SdimGenericValue lle_X_scanf(FunctionType *FT, 445193323Sed const std::vector<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. 460234353Sdimstatic 461226633SdimGenericValue lle_X_fprintf(FunctionType *FT, 462193323Sed const std::vector<GenericValue> &Args) { 463193323Sed assert(Args.size() >= 2); 464193323Sed char Buffer[10000]; 465193323Sed std::vector<GenericValue> NewArgs; 466193323Sed NewArgs.push_back(PTOGV(Buffer)); 467193323Sed NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end()); 468193323Sed GenericValue GV = lle_X_sprintf(FT, NewArgs); 469193323Sed 470193323Sed fputs(Buffer, (FILE *) GVTOP(Args[0])); 471193323Sed return GV; 472193323Sed} 473193323Sed 474261991Sdimstatic GenericValue lle_X_memset(FunctionType *FT, 475261991Sdim const std::vector<GenericValue> &Args) { 476261991Sdim int val = (int)Args[1].IntVal.getSExtValue(); 477261991Sdim size_t len = (size_t)Args[2].IntVal.getZExtValue(); 478261991Sdim memset((void *)GVTOP(Args[0]), val, len); 479261991Sdim // llvm.memset.* returns void, lle_X_* returns GenericValue, 480261991Sdim // so here we return GenericValue with IntVal set to zero 481261991Sdim GenericValue GV; 482261991Sdim GV.IntVal = 0; 483261991Sdim return GV; 484261991Sdim} 485261991Sdim 486261991Sdimstatic GenericValue lle_X_memcpy(FunctionType *FT, 487261991Sdim const std::vector<GenericValue> &Args) { 488261991Sdim memcpy(GVTOP(Args[0]), GVTOP(Args[1]), 489261991Sdim (size_t)(Args[2].IntVal.getLimitedValue())); 490261991Sdim 491261991Sdim // llvm.memcpy* returns void, lle_X_* returns GenericValue, 492261991Sdim // so here we return GenericValue with IntVal set to zero 493261991Sdim GenericValue GV; 494261991Sdim GV.IntVal = 0; 495261991Sdim return GV; 496261991Sdim} 497261991Sdim 498193323Sedvoid Interpreter::initializeExternalFunctions() { 499198090Srdivacky sys::ScopedLock Writer(*FunctionsLock); 500193323Sed FuncNames["lle_X_atexit"] = lle_X_atexit; 501193323Sed FuncNames["lle_X_exit"] = lle_X_exit; 502193323Sed FuncNames["lle_X_abort"] = lle_X_abort; 503193323Sed 504193323Sed FuncNames["lle_X_printf"] = lle_X_printf; 505193323Sed FuncNames["lle_X_sprintf"] = lle_X_sprintf; 506193323Sed FuncNames["lle_X_sscanf"] = lle_X_sscanf; 507193323Sed FuncNames["lle_X_scanf"] = lle_X_scanf; 508193323Sed FuncNames["lle_X_fprintf"] = lle_X_fprintf; 509261991Sdim FuncNames["lle_X_memset"] = lle_X_memset; 510261991Sdim FuncNames["lle_X_memcpy"] = lle_X_memcpy; 511193323Sed} 512