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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 //  External functions in the interpreter are implemented by
00014 //  using the system's dynamic loader to look up the address of the function
00015 //  we want to invoke.  If a function is found, then one of the
00016 //  many lle_* wrapper functions in this file will translate its arguments from
00017 //  GenericValues to the types the function is actually expecting, before the
00018 //  function is called.
00019 //
00020 //===----------------------------------------------------------------------===//
00021 
00022 #include "Interpreter.h"
00023 #include "llvm/DerivedTypes.h"
00024 #include "llvm/Module.h"
00025 #include "llvm/System/DynamicLibrary.h"
00026 #include "llvm/Target/TargetData.h"
00027 #include <cmath>
00028 #include <csignal>
00029 #include <map>
00030 using std::vector;
00031 
00032 using namespace llvm;
00033 
00034 typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
00035 static std::map<const Function *, ExFunc> Functions;
00036 static std::map<std::string, ExFunc> FuncNames;
00037 
00038 static Interpreter *TheInterpreter;
00039 
00040 static char getTypeID(const Type *Ty) {
00041   switch (Ty->getTypeID()) {
00042   case Type::VoidTyID:    return 'V';
00043   case Type::BoolTyID:    return 'o';
00044   case Type::UByteTyID:   return 'B';
00045   case Type::SByteTyID:   return 'b';
00046   case Type::UShortTyID:  return 'S';
00047   case Type::ShortTyID:   return 's';
00048   case Type::UIntTyID:    return 'I';
00049   case Type::IntTyID:     return 'i';
00050   case Type::ULongTyID:   return 'L';
00051   case Type::LongTyID:    return 'l';
00052   case Type::FloatTyID:   return 'F';
00053   case Type::DoubleTyID:  return 'D';
00054   case Type::PointerTyID: return 'P';
00055   case Type::FunctionTyID:  return 'M';
00056   case Type::StructTyID:  return 'T';
00057   case Type::ArrayTyID:   return 'A';
00058   case Type::OpaqueTyID:  return 'O';
00059   default: return 'U';
00060   }
00061 }
00062 
00063 static ExFunc lookupFunction(const Function *F) {
00064   // Function not found, look it up... start by figuring out what the
00065   // composite function name should be.
00066   std::string ExtName = "lle_";
00067   const FunctionType *FT = F->getFunctionType();
00068   for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
00069     ExtName += getTypeID(FT->getContainedType(i));
00070   ExtName += "_" + F->getName();
00071 
00072   ExFunc FnPtr = FuncNames[ExtName];
00073   if (FnPtr == 0)
00074     FnPtr = (ExFunc)sys::DynamicLibrary::SearchForAddressOfSymbol(ExtName);
00075   if (FnPtr == 0)
00076     FnPtr = FuncNames["lle_X_"+F->getName()];
00077   if (FnPtr == 0)  // Try calling a generic function... if it exists...
00078     FnPtr = (ExFunc)sys::DynamicLibrary::SearchForAddressOfSymbol(
00079             ("lle_X_"+F->getName()).c_str());
00080   if (FnPtr != 0)
00081     Functions.insert(std::make_pair(F, FnPtr));  // Cache for later
00082   return FnPtr;
00083 }
00084 
00085 GenericValue Interpreter::callExternalFunction(Function *F,
00086                                      const std::vector<GenericValue> &ArgVals) {
00087   TheInterpreter = this;
00088 
00089   // Do a lookup to see if the function is in our cache... this should just be a
00090   // deferred annotation!
00091   std::map<const Function *, ExFunc>::iterator FI = Functions.find(F);
00092   ExFunc Fn = (FI == Functions.end()) ? lookupFunction(F) : FI->second;
00093   if (Fn == 0) {
00094     std::cout << "Tried to execute an unknown external function: "
00095               << F->getType()->getDescription() << " " << F->getName() << "\n";
00096     if (F->getName() == "__main")
00097       return GenericValue();
00098     abort();
00099   }
00100 
00101   // TODO: FIXME when types are not const!
00102   GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),
00103                            ArgVals);
00104   return Result;
00105 }
00106 
00107 
00108 //===----------------------------------------------------------------------===//
00109 //  Functions "exported" to the running application...
00110 //
00111 extern "C" {  // Don't add C++ manglings to llvm mangling :)
00112 
00113 // void putchar(sbyte)
00114 GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) {
00115   std::cout << Args[0].SByteVal;
00116   return GenericValue();
00117 }
00118 
00119 // int putchar(int)
00120 GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) {
00121   std::cout << ((char)Args[0].IntVal) << std::flush;
00122   return Args[0];
00123 }
00124 
00125 // void putchar(ubyte)
00126 GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) {
00127   std::cout << Args[0].SByteVal << std::flush;
00128   return Args[0];
00129 }
00130 
00131 // void atexit(Function*)
00132 GenericValue lle_X_atexit(FunctionType *M, const vector<GenericValue> &Args) {
00133   assert(Args.size() == 1);
00134   TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
00135   GenericValue GV;
00136   GV.IntVal = 0;
00137   return GV;
00138 }
00139 
00140 // void exit(int)
00141 GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) {
00142   TheInterpreter->exitCalled(Args[0]);
00143   return GenericValue();
00144 }
00145 
00146 // void abort(void)
00147 GenericValue lle_X_abort(FunctionType *M, const vector<GenericValue> &Args) {
00148   raise (SIGABRT);
00149   return GenericValue();
00150 }
00151 
00152 // void *malloc(uint)
00153 GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
00154   assert(Args.size() == 1 && "Malloc expects one argument!");
00155   return PTOGV(malloc(Args[0].UIntVal));
00156 }
00157 
00158 // void *calloc(uint, uint)
00159 GenericValue lle_X_calloc(FunctionType *M, const vector<GenericValue> &Args) {
00160   assert(Args.size() == 2 && "calloc expects two arguments!");
00161   return PTOGV(calloc(Args[0].UIntVal, Args[1].UIntVal));
00162 }
00163 
00164 // void free(void *)
00165 GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
00166   assert(Args.size() == 1);
00167   free(GVTOP(Args[0]));
00168   return GenericValue();
00169 }
00170 
00171 // int atoi(char *)
00172 GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
00173   assert(Args.size() == 1);
00174   GenericValue GV;
00175   GV.IntVal = atoi((char*)GVTOP(Args[0]));
00176   return GV;
00177 }
00178 
00179 // double pow(double, double)
00180 GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
00181   assert(Args.size() == 2);
00182   GenericValue GV;
00183   GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
00184   return GV;
00185 }
00186 
00187 // double exp(double)
00188 GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
00189   assert(Args.size() == 1);
00190   GenericValue GV;
00191   GV.DoubleVal = exp(Args[0].DoubleVal);
00192   return GV;
00193 }
00194 
00195 // double sqrt(double)
00196 GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
00197   assert(Args.size() == 1);
00198   GenericValue GV;
00199   GV.DoubleVal = sqrt(Args[0].DoubleVal);
00200   return GV;
00201 }
00202 
00203 // double log(double)
00204 GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
00205   assert(Args.size() == 1);
00206   GenericValue GV;
00207   GV.DoubleVal = log(Args[0].DoubleVal);
00208   return GV;
00209 }
00210 
00211 // double floor(double)
00212 GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
00213   assert(Args.size() == 1);
00214   GenericValue GV;
00215   GV.DoubleVal = floor(Args[0].DoubleVal);
00216   return GV;
00217 }
00218 
00219 #ifdef HAVE_RAND48
00220 
00221 // double drand48()
00222 GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
00223   assert(Args.size() == 0);
00224   GenericValue GV;
00225   GV.DoubleVal = drand48();
00226   return GV;
00227 }
00228 
00229 // long lrand48()
00230 GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
00231   assert(Args.size() == 0);
00232   GenericValue GV;
00233   GV.IntVal = lrand48();
00234   return GV;
00235 }
00236 
00237 // void srand48(long)
00238 GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
00239   assert(Args.size() == 1);
00240   srand48(Args[0].IntVal);
00241   return GenericValue();
00242 }
00243 
00244 #endif
00245 
00246 // int rand()
00247 GenericValue lle_X_rand(FunctionType *M, const vector<GenericValue> &Args) {
00248   assert(Args.size() == 0);
00249   GenericValue GV;
00250   GV.IntVal = rand();
00251   return GV;
00252 }
00253 
00254 // void srand(uint)
00255 GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
00256   assert(Args.size() == 1);
00257   srand(Args[0].UIntVal);
00258   return GenericValue();
00259 }
00260 
00261 // int puts(const char*)
00262 GenericValue lle_X_puts(FunctionType *M, const vector<GenericValue> &Args) {
00263   assert(Args.size() == 1);
00264   GenericValue GV;
00265   GV.IntVal = puts((char*)GVTOP(Args[0]));
00266   return GV;
00267 }
00268 
00269 // int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
00270 // output useful.
00271 GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
00272   char *OutputBuffer = (char *)GVTOP(Args[0]);
00273   const char *FmtStr = (const char *)GVTOP(Args[1]);
00274   unsigned ArgNo = 2;
00275 
00276   // printf should return # chars printed.  This is completely incorrect, but
00277   // close enough for now.
00278   GenericValue GV; GV.IntVal = strlen(FmtStr);
00279   while (1) {
00280     switch (*FmtStr) {
00281     case 0: return GV;             // Null terminator...
00282     default:                       // Normal nonspecial character
00283       sprintf(OutputBuffer++, "%c", *FmtStr++);
00284       break;
00285     case '\\': {                   // Handle escape codes
00286       sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
00287       FmtStr += 2; OutputBuffer += 2;
00288       break;
00289     }
00290     case '%': {                    // Handle format specifiers
00291       char FmtBuf[100] = "", Buffer[1000] = "";
00292       char *FB = FmtBuf;
00293       *FB++ = *FmtStr++;
00294       char Last = *FB++ = *FmtStr++;
00295       unsigned HowLong = 0;
00296       while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
00297              Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
00298              Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
00299              Last != 'p' && Last != 's' && Last != '%') {
00300         if (Last == 'l' || Last == 'L') HowLong++;  // Keep track of l's
00301         Last = *FB++ = *FmtStr++;
00302       }
00303       *FB = 0;
00304 
00305       switch (Last) {
00306       case '%':
00307         sprintf(Buffer, FmtBuf); break;
00308       case 'c':
00309         sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
00310       case 'd': case 'i':
00311       case 'u': case 'o':
00312       case 'x': case 'X':
00313         if (HowLong >= 1) {
00314           if (HowLong == 1 &&
00315               TheInterpreter->getModule().getPointerSize()==Module::Pointer64 &&
00316               sizeof(long) < sizeof(long long)) {
00317             // Make sure we use %lld with a 64 bit argument because we might be
00318             // compiling LLI on a 32 bit compiler.
00319             unsigned Size = strlen(FmtBuf);
00320             FmtBuf[Size] = FmtBuf[Size-1];
00321             FmtBuf[Size+1] = 0;
00322             FmtBuf[Size-1] = 'l';
00323           }
00324           sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal);
00325         } else
00326           sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
00327       case 'e': case 'E': case 'g': case 'G': case 'f':
00328         sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
00329       case 'p':
00330         sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
00331       case 's':
00332         sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
00333       default:  std::cout << "<unknown printf code '" << *FmtStr << "'!>";
00334         ArgNo++; break;
00335       }
00336       strcpy(OutputBuffer, Buffer);
00337       OutputBuffer += strlen(Buffer);
00338       }
00339       break;
00340     }
00341   }
00342 }
00343 
00344 // int printf(sbyte *, ...) - a very rough implementation to make output useful.
00345 GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
00346   char Buffer[10000];
00347   vector<GenericValue> NewArgs;
00348   NewArgs.push_back(PTOGV(Buffer));
00349   NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
00350   GenericValue GV = lle_X_sprintf(M, NewArgs);
00351   std::cout << Buffer;
00352   return GV;
00353 }
00354 
00355 static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
00356                                  void *Arg2, void *Arg3, void *Arg4, void *Arg5,
00357                                  void *Arg6, void *Arg7, void *Arg8) {
00358   void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
00359 
00360   // Loop over the format string, munging read values as appropriate (performs
00361   // byteswaps as necessary).
00362   unsigned ArgNo = 0;
00363   while (*Fmt) {
00364     if (*Fmt++ == '%') {
00365       // Read any flag characters that may be present...
00366       bool Suppress = false;
00367       bool Half = false;
00368       bool Long = false;
00369       bool LongLong = false;  // long long or long double
00370 
00371       while (1) {
00372         switch (*Fmt++) {
00373         case '*': Suppress = true; break;
00374         case 'a': /*Allocate = true;*/ break;  // We don't need to track this
00375         case 'h': Half = true; break;
00376         case 'l': Long = true; break;
00377         case 'q':
00378         case 'L': LongLong = true; break;
00379         default:
00380           if (Fmt[-1] > '9' || Fmt[-1] < '0')   // Ignore field width specs
00381             goto Out;
00382         }
00383       }
00384     Out:
00385 
00386       // Read the conversion character
00387       if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
00388         unsigned Size = 0;
00389         const Type *Ty = 0;
00390 
00391         switch (Fmt[-1]) {
00392         case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
00393         case 'd':
00394           if (Long || LongLong) {
00395             Size = 8; Ty = Type::ULongTy;
00396           } else if (Half) {
00397             Size = 4; Ty = Type::UShortTy;
00398           } else {
00399             Size = 4; Ty = Type::UIntTy;
00400           }
00401           break;
00402 
00403         case 'e': case 'g': case 'E':
00404         case 'f':
00405           if (Long || LongLong) {
00406             Size = 8; Ty = Type::DoubleTy;
00407           } else {
00408             Size = 4; Ty = Type::FloatTy;
00409           }
00410           break;
00411 
00412         case 's': case 'c': case '[':  // No byteswap needed
00413           Size = 1;
00414           Ty = Type::SByteTy;
00415           break;
00416 
00417         default: break;
00418         }
00419 
00420         if (Size) {
00421           GenericValue GV;
00422           void *Arg = Args[ArgNo++];
00423           memcpy(&GV, Arg, Size);
00424           TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
00425         }
00426       }
00427     }
00428   }
00429 }
00430 
00431 // int sscanf(const char *format, ...);
00432 GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
00433   assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
00434 
00435   char *Args[10];
00436   for (unsigned i = 0; i < args.size(); ++i)
00437     Args[i] = (char*)GVTOP(args[i]);
00438 
00439   GenericValue GV;
00440   GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
00441                      Args[5], Args[6], Args[7], Args[8], Args[9]);
00442   ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
00443                        Args[5], Args[6], Args[7], Args[8], Args[9], 0);
00444   return GV;
00445 }
00446 
00447 // int scanf(const char *format, ...);
00448 GenericValue lle_X_scanf(FunctionType *M, const vector<GenericValue> &args) {
00449   assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
00450 
00451   char *Args[10];
00452   for (unsigned i = 0; i < args.size(); ++i)
00453     Args[i] = (char*)GVTOP(args[i]);
00454 
00455   GenericValue GV;
00456   GV.IntVal = scanf(Args[0], Args[1], Args[2], Args[3], Args[4],
00457                     Args[5], Args[6], Args[7], Args[8], Args[9]);
00458   ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
00459                        Args[5], Args[6], Args[7], Args[8], Args[9]);
00460   return GV;
00461 }
00462 
00463 
00464 // int clock(void) - Profiling implementation
00465 GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
00466   extern unsigned int clock(void);
00467   GenericValue GV; GV.IntVal = clock();
00468   return GV;
00469 }
00470 
00471 
00472 //===----------------------------------------------------------------------===//
00473 // String Functions...
00474 //===----------------------------------------------------------------------===//
00475 
00476 // int strcmp(const char *S1, const char *S2);
00477 GenericValue lle_X_strcmp(FunctionType *M, const vector<GenericValue> &Args) {
00478   assert(Args.size() == 2);
00479   GenericValue Ret;
00480   Ret.IntVal = strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]));
00481   return Ret;
00482 }
00483 
00484 // char *strcat(char *Dest, const char *src);
00485 GenericValue lle_X_strcat(FunctionType *M, const vector<GenericValue> &Args) {
00486   assert(Args.size() == 2);
00487   return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
00488 }
00489 
00490 // char *strcpy(char *Dest, const char *src);
00491 GenericValue lle_X_strcpy(FunctionType *M, const vector<GenericValue> &Args) {
00492   assert(Args.size() == 2);
00493   return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
00494 }
00495 
00496 static GenericValue size_t_to_GV (size_t n) {
00497   GenericValue Ret;
00498   if (sizeof (size_t) == sizeof (uint64_t)) {
00499     Ret.ULongVal = n;
00500   } else {
00501     assert (sizeof (size_t) == sizeof (unsigned int));
00502     Ret.UIntVal = n;
00503   }
00504   return Ret;
00505 }
00506 
00507 static size_t GV_to_size_t (GenericValue GV) {
00508   size_t count;
00509   if (sizeof (size_t) == sizeof (uint64_t)) {
00510     count = (size_t)GV.ULongVal;
00511   } else {
00512     assert (sizeof (size_t) == sizeof (unsigned int));
00513     count = (size_t)GV.UIntVal;
00514   }
00515   return count;
00516 }
00517 
00518 // size_t strlen(const char *src);
00519 GenericValue lle_X_strlen(FunctionType *M, const vector<GenericValue> &Args) {
00520   assert(Args.size() == 1);
00521   size_t strlenResult = strlen ((char *) GVTOP (Args[0]));
00522   return size_t_to_GV (strlenResult);
00523 }
00524 
00525 // char *strdup(const char *src);
00526 GenericValue lle_X_strdup(FunctionType *M, const vector<GenericValue> &Args) {
00527   assert(Args.size() == 1);
00528   return PTOGV(strdup((char*)GVTOP(Args[0])));
00529 }
00530 
00531 // char *__strdup(const char *src);
00532 GenericValue lle_X___strdup(FunctionType *M, const vector<GenericValue> &Args) {
00533   assert(Args.size() == 1);
00534   return PTOGV(strdup((char*)GVTOP(Args[0])));
00535 }
00536 
00537 // void *memset(void *S, int C, size_t N)
00538 GenericValue lle_X_memset(FunctionType *M, const vector<GenericValue> &Args) {
00539   assert(Args.size() == 3);
00540   size_t count = GV_to_size_t (Args[2]);
00541   return PTOGV(memset(GVTOP(Args[0]), Args[1].IntVal, count));
00542 }
00543 
00544 // void *memcpy(void *Dest, void *src, size_t Size);
00545 GenericValue lle_X_memcpy(FunctionType *M, const vector<GenericValue> &Args) {
00546   assert(Args.size() == 3);
00547   size_t count = GV_to_size_t (Args[2]);
00548   return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
00549 }
00550 
00551 //===----------------------------------------------------------------------===//
00552 // IO Functions...
00553 //===----------------------------------------------------------------------===//
00554 
00555 // getFILE - Turn a pointer in the host address space into a legit pointer in
00556 // the interpreter address space.  This is an identity transformation.
00557 #define getFILE(ptr) ((FILE*)ptr)
00558 
00559 // FILE *fopen(const char *filename, const char *mode);
00560 GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
00561   assert(Args.size() == 2);
00562   return PTOGV(fopen((const char *)GVTOP(Args[0]),
00563                      (const char *)GVTOP(Args[1])));
00564 }
00565 
00566 // int fclose(FILE *F);
00567 GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
00568   assert(Args.size() == 1);
00569   GenericValue GV;
00570   GV.IntVal = fclose(getFILE(GVTOP(Args[0])));
00571   return GV;
00572 }
00573 
00574 // int feof(FILE *stream);
00575 GenericValue lle_X_feof(FunctionType *M, const vector<GenericValue> &Args) {
00576   assert(Args.size() == 1);
00577   GenericValue GV;
00578 
00579   GV.IntVal = feof(getFILE(GVTOP(Args[0])));
00580   return GV;
00581 }
00582 
00583 // size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
00584 GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) {
00585   assert(Args.size() == 4);
00586   size_t result;
00587 
00588   result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
00589                  GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
00590   return size_t_to_GV (result);
00591 }
00592 
00593 // size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
00594 GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
00595   assert(Args.size() == 4);
00596   size_t result;
00597 
00598   result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
00599                   GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
00600   return size_t_to_GV (result);
00601 }
00602 
00603 // char *fgets(char *s, int n, FILE *stream);
00604 GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
00605   assert(Args.size() == 3);
00606   return GVTOP(fgets((char*)GVTOP(Args[0]), Args[1].IntVal,
00607                      getFILE(GVTOP(Args[2]))));
00608 }
00609 
00610 // FILE *freopen(const char *path, const char *mode, FILE *stream);
00611 GenericValue lle_X_freopen(FunctionType *M, const vector<GenericValue> &Args) {
00612   assert(Args.size() == 3);
00613   return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
00614                        getFILE(GVTOP(Args[2]))));
00615 }
00616 
00617 // int fflush(FILE *stream);
00618 GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
00619   assert(Args.size() == 1);
00620   GenericValue GV;
00621   GV.IntVal = fflush(getFILE(GVTOP(Args[0])));
00622   return GV;
00623 }
00624 
00625 // int getc(FILE *stream);
00626 GenericValue lle_X_getc(FunctionType *M, const vector<GenericValue> &Args) {
00627   assert(Args.size() == 1);
00628   GenericValue GV;
00629   GV.IntVal = getc(getFILE(GVTOP(Args[0])));
00630   return GV;
00631 }
00632 
00633 // int _IO_getc(FILE *stream);
00634 GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
00635   return lle_X_getc(F, Args);
00636 }
00637 
00638 // int fputc(int C, FILE *stream);
00639 GenericValue lle_X_fputc(FunctionType *M, const vector<GenericValue> &Args) {
00640   assert(Args.size() == 2);
00641   GenericValue GV;
00642   GV.IntVal = fputc(Args[0].IntVal, getFILE(GVTOP(Args[1])));
00643   return GV;
00644 }
00645 
00646 // int ungetc(int C, FILE *stream);
00647 GenericValue lle_X_ungetc(FunctionType *M, const vector<GenericValue> &Args) {
00648   assert(Args.size() == 2);
00649   GenericValue GV;
00650   GV.IntVal = ungetc(Args[0].IntVal, getFILE(GVTOP(Args[1])));
00651   return GV;
00652 }
00653 
00654 // int ferror (FILE *stream);
00655 GenericValue lle_X_ferror(FunctionType *M, const vector<GenericValue> &Args) {
00656   assert(Args.size() == 1);
00657   GenericValue GV;
00658   GV.IntVal = ferror (getFILE(GVTOP(Args[0])));
00659   return GV;
00660 }
00661 
00662 // int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
00663 // useful.
00664 GenericValue lle_X_fprintf(FunctionType *M, const vector<GenericValue> &Args) {
00665   assert(Args.size() >= 2);
00666   char Buffer[10000];
00667   vector<GenericValue> NewArgs;
00668   NewArgs.push_back(PTOGV(Buffer));
00669   NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
00670   GenericValue GV = lle_X_sprintf(M, NewArgs);
00671 
00672   fputs(Buffer, getFILE(GVTOP(Args[0])));
00673   return GV;
00674 }
00675 
00676 } // End extern "C"
00677 
00678 
00679 void Interpreter::initializeExternalFunctions() {
00680   FuncNames["lle_Vb_putchar"]     = lle_Vb_putchar;
00681   FuncNames["lle_ii_putchar"]     = lle_ii_putchar;
00682   FuncNames["lle_VB_putchar"]     = lle_VB_putchar;
00683   FuncNames["lle_X_exit"]         = lle_X_exit;
00684   FuncNames["lle_X_abort"]        = lle_X_abort;
00685   FuncNames["lle_X_malloc"]       = lle_X_malloc;
00686   FuncNames["lle_X_calloc"]       = lle_X_calloc;
00687   FuncNames["lle_X_free"]         = lle_X_free;
00688   FuncNames["lle_X_atoi"]         = lle_X_atoi;
00689   FuncNames["lle_X_pow"]          = lle_X_pow;
00690   FuncNames["lle_X_exp"]          = lle_X_exp;
00691   FuncNames["lle_X_log"]          = lle_X_log;
00692   FuncNames["lle_X_floor"]        = lle_X_floor;
00693   FuncNames["lle_X_srand"]        = lle_X_srand;
00694   FuncNames["lle_X_rand"]         = lle_X_rand;
00695 #ifdef HAVE_RAND48
00696   FuncNames["lle_X_drand48"]      = lle_X_drand48;
00697   FuncNames["lle_X_srand48"]      = lle_X_srand48;
00698   FuncNames["lle_X_lrand48"]      = lle_X_lrand48;
00699 #endif
00700   FuncNames["lle_X_sqrt"]         = lle_X_sqrt;
00701   FuncNames["lle_X_puts"]         = lle_X_puts;
00702   FuncNames["lle_X_printf"]       = lle_X_printf;
00703   FuncNames["lle_X_sprintf"]      = lle_X_sprintf;
00704   FuncNames["lle_X_sscanf"]       = lle_X_sscanf;
00705   FuncNames["lle_X_scanf"]        = lle_X_scanf;
00706   FuncNames["lle_i_clock"]        = lle_i_clock;
00707 
00708   FuncNames["lle_X_strcmp"]       = lle_X_strcmp;
00709   FuncNames["lle_X_strcat"]       = lle_X_strcat;
00710   FuncNames["lle_X_strcpy"]       = lle_X_strcpy;
00711   FuncNames["lle_X_strlen"]       = lle_X_strlen;
00712   FuncNames["lle_X___strdup"]     = lle_X___strdup;
00713   FuncNames["lle_X_memset"]       = lle_X_memset;
00714   FuncNames["lle_X_memcpy"]       = lle_X_memcpy;
00715 
00716   FuncNames["lle_X_fopen"]        = lle_X_fopen;
00717   FuncNames["lle_X_fclose"]       = lle_X_fclose;
00718   FuncNames["lle_X_feof"]         = lle_X_feof;
00719   FuncNames["lle_X_fread"]        = lle_X_fread;
00720   FuncNames["lle_X_fwrite"]       = lle_X_fwrite;
00721   FuncNames["lle_X_fgets"]        = lle_X_fgets;
00722   FuncNames["lle_X_fflush"]       = lle_X_fflush;
00723   FuncNames["lle_X_fgetc"]        = lle_X_getc;
00724   FuncNames["lle_X_getc"]         = lle_X_getc;
00725   FuncNames["lle_X__IO_getc"]     = lle_X__IO_getc;
00726   FuncNames["lle_X_fputc"]        = lle_X_fputc;
00727   FuncNames["lle_X_ungetc"]       = lle_X_ungetc;
00728   FuncNames["lle_X_fprintf"]      = lle_X_fprintf;
00729   FuncNames["lle_X_freopen"]      = lle_X_freopen;
00730 }
00731