LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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