<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><blockquote type="cite"><div><font class="Apple-style-span" color="#000000"><br></font><br>-#ifdef __linux__<br>-#include <cxxabi.h><br>+#ifdef HAVE_LIBFFI<br>+#include FFI_HEADER</div></blockquote><div><br></div>Is this portable code? I am having trouble building on one of my Mac.</div><div><br></div><div><div>llvm[3]: Compiling ExternalFunctions.cpp for Debug build </div><div>ExternalFunctions.cpp:37:10: error: #include expects "FILENAME" or <FILENAME></div><div><br></div><div>Evan</div><div><blockquote type="cite"></blockquote></div><blockquote type="cite"><div><br></div></blockquote><br><blockquote type="cite"><div> #endif<br><br>-using std::vector;<br>-<br> using namespace llvm;<br><br>-typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);<br>-static ManagedStatic<std::map<const Function *, ExFunc> > Functions;<br>+typedef GenericValue (*ExFunc)(const FunctionType *,<br>+                               const std::vector<GenericValue> &);<br>+static ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions;<br> static std::map<std::string, ExFunc> FuncNames;<br><br>+#ifdef HAVE_LIBFFI<br>+typedef void (*RawFunc)(void);<br>+static ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions;<br>+#endif // HAVE_LIBFFI<br>+<br> static Interpreter *TheInterpreter;<br><br> static char getTypeID(const Type *Ty) {<br>@@ -89,34 +94,181 @@<br>   if (FnPtr == 0)  // Try calling a generic function... if it exists...<br>     FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(<br>             ("lle_X_"+F->getName()).c_str());<br>-  if (FnPtr == 0)<br>-    FnPtr = (ExFunc)(intptr_t)<br>-      sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());<br>   if (FnPtr != 0)<br>-    Functions->insert(std::make_pair(F, FnPtr));  // Cache for later<br>+    ExportedFunctions->insert(std::make_pair(F, FnPtr));  // Cache for later<br>   return FnPtr;<br> }<br><br>+#ifdef HAVE_LIBFFI<br>+static ffi_type *ffiTypeFor(const Type *Ty) {<br>+  switch (Ty->getTypeID()) {<br>+    case Type::VoidTyID: return &ffi_type_void;<br>+    case Type::IntegerTyID:<br>+      switch (cast<IntegerType>(Ty)->getBitWidth()) {<br>+        case 8:  return &ffi_type_sint8;<br>+        case 16: return &ffi_type_sint16;<br>+        case 32: return &ffi_type_sint32;<br>+        case 64: return &ffi_type_sint64;<br>+      }<br>+    case Type::FloatTyID:   return &ffi_type_float;<br>+    case Type::DoubleTyID:  return &ffi_type_double;<br>+    case Type::PointerTyID: return &ffi_type_pointer;<br>+    default: break;<br>+  }<br>+  // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.<br>+  cerr << "Type could not be mapped for use with libffi.\n";<br>+  abort();<br>+  return NULL;<br>+}<br>+<br>+static void *ffiValueFor(const Type *Ty, const GenericValue &AV,<br>+                         void *ArgDataPtr) {<br>+  switch (Ty->getTypeID()) {<br>+    case Type::IntegerTyID:<br>+      switch (cast<IntegerType>(Ty)->getBitWidth()) {<br>+        case 8: {<br>+          int8_t *I8Ptr = (int8_t *) ArgDataPtr;<br>+          *I8Ptr = (int8_t) AV.IntVal.getZExtValue();<br>+          return ArgDataPtr;<br>+        }<br>+        case 16: {<br>+          int16_t *I16Ptr = (int16_t *) ArgDataPtr;<br>+          *I16Ptr = (int16_t) AV.IntVal.getZExtValue();<br>+          return ArgDataPtr;<br>+        }<br>+        case 32: {<br>+          int32_t *I32Ptr = (int32_t *) ArgDataPtr;<br>+          *I32Ptr = (int32_t) AV.IntVal.getZExtValue();<br>+          return ArgDataPtr;<br>+        }<br>+        case 64: {<br>+          int64_t *I64Ptr = (int64_t *) ArgDataPtr;<br>+          *I64Ptr = (int64_t) AV.IntVal.getZExtValue();<br>+          return ArgDataPtr;<br>+        }<br>+      }<br>+    case Type::FloatTyID: {<br>+      float *FloatPtr = (float *) ArgDataPtr;<br>+      *FloatPtr = AV.DoubleVal;<br>+      return ArgDataPtr;<br>+    }<br>+    case Type::DoubleTyID: {<br>+      double *DoublePtr = (double *) ArgDataPtr;<br>+      *DoublePtr = AV.DoubleVal;<br>+      return ArgDataPtr;<br>+    }<br>+    case Type::PointerTyID: {<br>+      void **PtrPtr = (void **) ArgDataPtr;<br>+      *PtrPtr = GVTOP(AV);<br>+      return ArgDataPtr;<br>+    }<br>+    default: break;<br>+  }<br>+  // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.<br>+  cerr << "Type value could not be mapped for use with libffi.\n";<br>+  abort();<br>+  return NULL;<br>+}<br>+<br>+static bool ffiInvoke(RawFunc Fn, Function *F,<br>+                      const std::vector<GenericValue> &ArgVals,<br>+                      const TargetData *TD, GenericValue &Result) {<br>+  ffi_cif cif;<br>+  const FunctionType *FTy = F->getFunctionType();<br>+  const unsigned NumArgs = F->arg_size();<br>+<br>+  // TODO: We don't have type information about the remaining arguments, because<br>+  // this information is never passed into ExecutionEngine::runFunction().<br>+  if (ArgVals.size() > NumArgs && F->isVarArg()) {<br>+    cerr << "Calling external var arg function '" << F->getName()<br>+         << "' is not supported by the Interpreter.\n";<br>+    abort();<br>+  }<br>+<br>+  unsigned ArgBytes = 0;<br>+<br>+  std::vector<ffi_type*> args(NumArgs);<br>+  for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();<br>+       A != E; ++A) {<br>+    const unsigned ArgNo = A->getArgNo();<br>+    const Type *ArgTy = FTy->getParamType(ArgNo);<br>+    args[ArgNo] = ffiTypeFor(ArgTy);<br>+    ArgBytes += TD->getTypeStoreSize(ArgTy);<br>+  }<br>+<br>+  uint8_t *ArgData = (uint8_t*) alloca(ArgBytes);<br>+  uint8_t *ArgDataPtr = ArgData;<br>+  std::vector<void*> values(NumArgs);<br>+  for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();<br>+       A != E; ++A) {<br>+    const unsigned ArgNo = A->getArgNo();<br>+    const Type *ArgTy = FTy->getParamType(ArgNo);<br>+    values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);<br>+    ArgDataPtr += TD->getTypeStoreSize(ArgTy);<br>+  }<br>+<br>+  const Type *RetTy = FTy->getReturnType();<br>+  ffi_type *rtype = ffiTypeFor(RetTy);<br>+<br>+  if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) {<br>+    void *ret = NULL;<br>+    if (RetTy->getTypeID() != Type::VoidTyID)<br>+      ret = alloca(TD->getTypeStoreSize(RetTy));<br>+    ffi_call(&cif, Fn, ret, &values[0]);<br>+    switch (RetTy->getTypeID()) {<br>+      case Type::IntegerTyID:<br>+        switch (cast<IntegerType>(RetTy)->getBitWidth()) {<br>+          case 8:  Result.IntVal = APInt(8 , *(int8_t *) ret); break;<br>+          case 16: Result.IntVal = APInt(16, *(int16_t*) ret); break;<br>+          case 32: Result.IntVal = APInt(32, *(int32_t*) ret); break;<br>+          case 64: Result.IntVal = APInt(64, *(int64_t*) ret); break;<br>+        }<br>+        break;<br>+      case Type::FloatTyID:   Result.FloatVal   = *(float *) ret; break;<br>+      case Type::DoubleTyID:  Result.DoubleVal  = *(double*) ret; break;<br>+      case Type::PointerTyID: Result.PointerVal = *(void **) ret; break;<br>+      default: break;<br>+    }<br>+    return true;<br>+  }<br>+<br>+  return false;<br>+}<br>+#endif // HAVE_LIBFFI<br>+<br> GenericValue Interpreter::callExternalFunction(Function *F,<br>                                      const std::vector<GenericValue> &ArgVals) {<br>   TheInterpreter = this;<br><br>   // Do a lookup to see if the function is in our cache... this should just be a<br>   // deferred annotation!<br>-  std::map<const Function *, ExFunc>::iterator FI = Functions->find(F);<br>-  ExFunc Fn = (FI == Functions->end()) ? lookupFunction(F) : FI->second;<br>-  if (Fn == 0) {<br>-    cerr << "Tried to execute an unknown external function: "<br>-         << F->getType()->getDescription() << " " << F->getName() << "\n";<br>-    if (F->getName() == "__main")<br>-      return GenericValue();<br>-    abort();<br>+  std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F);<br>+  if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F)<br>+                                                   : FI->second)<br>+    return Fn(F->getFunctionType(), ArgVals);<br>+<br>+#ifdef HAVE_LIBFFI<br>+  std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);<br>+  RawFunc RawFn;<br>+  if (RF == RawFunctions->end()) {<br>+    RawFn = (RawFunc)(intptr_t)<br>+      sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());<br>+    if (RawFn != 0)<br>+      RawFunctions->insert(std::make_pair(F, RawFn));  // Cache for later<br>+  } else {<br>+    RawFn = RF->second;<br>   }<br><br>-  // TODO: FIXME when types are not const!<br>-  GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),<br>-                           ArgVals);<br>-  return Result;<br>+  GenericValue Result;<br>+  if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getTargetData(), Result))<br>+    return Result;<br>+#endif // HAVE_LIBFFI<br>+<br>+  cerr << "Tried to execute an unknown external function: "<br>+       << F->getType()->getDescription() << " " << F->getName() << "\n";<br>+  if (F->getName() != "__main")<br>+    abort();<br>+  return GenericValue();<br> }<br><br><br>@@ -125,24 +277,9 @@<br> //<br> extern "C" {  // Don't add C++ manglings to llvm mangling :)<br><br>-// void putchar(ubyte)<br>-GenericValue lle_X_putchar(FunctionType *FT, const vector<GenericValue> &Args){<br>-  cout << ((char)Args[0].IntVal.getZExtValue()) << std::flush;<br>-  return Args[0];<br>-}<br>-<br>-// void _IO_putc(int c, FILE* fp)<br>-GenericValue lle_X__IO_putc(FunctionType *FT, const vector<GenericValue> &Args){<br>-#ifdef __linux__<br>-  _IO_putc((char)Args[0].IntVal.getZExtValue(), (FILE*) Args[1].PointerVal);<br>-#else<br>-  assert(0 && "Can't call _IO_putc on this platform");<br>-#endif<br>-  return Args[0];<br>-}<br>-<br> // void atexit(Function*)<br>-GenericValue lle_X_atexit(FunctionType *FT, const vector<GenericValue> &Args) {<br>+GenericValue lle_X_atexit(const FunctionType *FT,<br>+                          const std::vector<GenericValue> &Args) {<br>   assert(Args.size() == 1);<br>   TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));<br>   GenericValue GV;<br>@@ -151,163 +288,23 @@<br> }<br><br> // void exit(int)<br>-GenericValue lle_X_exit(FunctionType *FT, const vector<GenericValue> &Args) {<br>+GenericValue lle_X_exit(const FunctionType *FT,<br>+                        const std::vector<GenericValue> &Args) {<br>   TheInterpreter->exitCalled(Args[0]);<br>   return GenericValue();<br> }<br><br> // void abort(void)<br>-GenericValue lle_X_abort(FunctionType *FT, const vector<GenericValue> &Args) {<br>+GenericValue lle_X_abort(const FunctionType *FT,<br>+                         const std::vector<GenericValue> &Args) {<br>   raise (SIGABRT);<br>   return GenericValue();<br> }<br><br>-// void *malloc(uint)<br>-GenericValue lle_X_malloc(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1 && "Malloc expects one argument!");<br>-  assert(isa<PointerType>(FT->getReturnType()) && "malloc must return pointer");<br>-  return PTOGV(malloc(Args[0].IntVal.getZExtValue()));<br>-}<br>-<br>-// void *calloc(uint, uint)<br>-GenericValue lle_X_calloc(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 2 && "calloc expects two arguments!");<br>-  assert(isa<PointerType>(FT->getReturnType()) && "calloc must return pointer");<br>-  return PTOGV(calloc(Args[0].IntVal.getZExtValue(), <br>-                      Args[1].IntVal.getZExtValue()));<br>-}<br>-<br>-// void *calloc(uint, uint)<br>-GenericValue lle_X_realloc(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 2 && "calloc expects two arguments!");<br>-  assert(isa<PointerType>(FT->getReturnType()) &&"realloc must return pointer");<br>-  return PTOGV(realloc(GVTOP(Args[0]), Args[1].IntVal.getZExtValue()));<br>-}<br>-<br>-// void free(void *)<br>-GenericValue lle_X_free(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  free(GVTOP(Args[0]));<br>-  return GenericValue();<br>-}<br>-<br>-// int atoi(char *)<br>-GenericValue lle_X_atoi(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.IntVal = APInt(32, atoi((char*)GVTOP(Args[0])));<br>-  return GV;<br>-}<br>-<br>-// double pow(double, double)<br>-GenericValue lle_X_pow(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 2);<br>-  GenericValue GV;<br>-  GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);<br>-  return GV;<br>-}<br>-<br>-// double sin(double)<br>-GenericValue lle_X_sin(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.DoubleVal = sin(Args[0].DoubleVal);<br>-  return GV;<br>-}<br>-<br>-// double cos(double)<br>-GenericValue lle_X_cos(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.DoubleVal = cos(Args[0].DoubleVal);<br>-  return GV;<br>-}<br>-<br>-// double exp(double)<br>-GenericValue lle_X_exp(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.DoubleVal = exp(Args[0].DoubleVal);<br>-  return GV;<br>-}<br>-<br>-// double sqrt(double)<br>-GenericValue lle_X_sqrt(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.DoubleVal = sqrt(Args[0].DoubleVal);<br>-  return GV;<br>-}<br>-<br>-// double log(double)<br>-GenericValue lle_X_log(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.DoubleVal = log(Args[0].DoubleVal);<br>-  return GV;<br>-}<br>-<br>-// double floor(double)<br>-GenericValue lle_X_floor(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.DoubleVal = floor(Args[0].DoubleVal);<br>-  return GV;<br>-}<br>-<br>-#ifdef HAVE_RAND48<br>-<br>-// double drand48()<br>-GenericValue lle_X_drand48(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.empty());<br>-  GenericValue GV;<br>-  GV.DoubleVal = drand48();<br>-  return GV;<br>-}<br>-<br>-// long lrand48()<br>-GenericValue lle_X_lrand48(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.empty());<br>-  GenericValue GV;<br>-  GV.IntVal = APInt(32, lrand48());<br>-  return GV;<br>-}<br>-<br>-// void srand48(long)<br>-GenericValue lle_X_srand48(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  srand48(Args[0].IntVal.getZExtValue());<br>-  return GenericValue();<br>-}<br>-<br>-#endif<br>-<br>-// int rand()<br>-GenericValue lle_X_rand(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.empty());<br>-  GenericValue GV;<br>-  GV.IntVal = APInt(32, rand());<br>-  return GV;<br>-}<br>-<br>-// void srand(uint)<br>-GenericValue lle_X_srand(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  srand(Args[0].IntVal.getZExtValue());<br>-  return GenericValue();<br>-}<br>-<br>-// int puts(const char*)<br>-GenericValue lle_X_puts(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.IntVal = APInt(32, puts((char*)GVTOP(Args[0])));<br>-  return GV;<br>-}<br>-<br>-// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make<br>+// int sprintf(char *, const char *, ...) - a very rough implementation to make<br> // output useful.<br>-GenericValue lle_X_sprintf(FunctionType *FT, const vector<GenericValue> &Args) {<br>+GenericValue lle_X_sprintf(const FunctionType *FT,<br>+                           const std::vector<GenericValue> &Args) {<br>   char *OutputBuffer = (char *)GVTOP(Args[0]);<br>   const char *FmtStr = (const char *)GVTOP(Args[1]);<br>   unsigned ArgNo = 2;<br>@@ -384,10 +381,12 @@<br>   return GV;<br> }<br><br>-// int printf(sbyte *, ...) - a very rough implementation to make output useful.<br>-GenericValue lle_X_printf(FunctionType *FT, const vector<GenericValue> &Args) {<br>+// int printf(const char *, ...) - a very rough implementation to make output<br>+// useful.<br>+GenericValue lle_X_printf(const FunctionType *FT,<br>+                          const std::vector<GenericValue> &Args) {<br>   char Buffer[10000];<br>-  vector<GenericValue> NewArgs;<br>+  std::vector<GenericValue> NewArgs;<br>   NewArgs.push_back(PTOGV((void*)&Buffer[0]));<br>   NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());<br>   GenericValue GV = lle_X_sprintf(FT, NewArgs);<br>@@ -472,7 +471,8 @@<br> }<br><br> // int sscanf(const char *format, ...);<br>-GenericValue lle_X_sscanf(FunctionType *FT, const vector<GenericValue> &args) {<br>+GenericValue lle_X_sscanf(const FunctionType *FT,<br>+                          const std::vector<GenericValue> &args) {<br>   assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");<br><br>   char *Args[10];<br>@@ -488,7 +488,8 @@<br> }<br><br> // int scanf(const char *format, ...);<br>-GenericValue lle_X_scanf(FunctionType *FT, const vector<GenericValue> &args) {<br>+GenericValue lle_X_scanf(const FunctionType *FT,<br>+                         const std::vector<GenericValue> &args) {<br>   assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");<br><br>   char *Args[10];<br>@@ -503,324 +504,33 @@<br>   return GV;<br> }<br><br>-<br>-// int clock(void) - Profiling implementation<br>-GenericValue lle_i_clock(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  extern unsigned int clock(void);<br>-  GenericValue GV; <br>-  GV.IntVal = APInt(32, clock());<br>-  return GV;<br>-}<br>-<br>-<br>-//===----------------------------------------------------------------------===//<br>-// String Functions...<br>-//===----------------------------------------------------------------------===//<br>-<br>-// int strcmp(const char *S1, const char *S2);<br>-GenericValue lle_X_strcmp(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 2);<br>-  GenericValue Ret;<br>-  Ret.IntVal = APInt(32, strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));<br>-  return Ret;<br>-}<br>-<br>-// char *strcat(char *Dest, const char *src);<br>-GenericValue lle_X_strcat(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 2);<br>-  assert(isa<PointerType>(FT->getReturnType()) &&"strcat must return pointer");<br>-  return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));<br>-}<br>-<br>-// char *strcpy(char *Dest, const char *src);<br>-GenericValue lle_X_strcpy(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 2);<br>-  assert(isa<PointerType>(FT->getReturnType()) &&"strcpy must return pointer");<br>-  return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));<br>-}<br>-<br>-static GenericValue size_t_to_GV (size_t n) {<br>-  GenericValue Ret;<br>-  if (sizeof (size_t) == sizeof (uint64_t)) {<br>-    Ret.IntVal = APInt(64, n);<br>-  } else {<br>-    assert (sizeof (size_t) == sizeof (unsigned int));<br>-    Ret.IntVal = APInt(32, n);<br>-  }<br>-  return Ret;<br>-}<br>-<br>-static size_t GV_to_size_t (GenericValue GV) {<br>-  size_t count;<br>-  if (sizeof (size_t) == sizeof (uint64_t)) {<br>-    count = (size_t)GV.IntVal.getZExtValue();<br>-  } else {<br>-    assert (sizeof (size_t) == sizeof (unsigned int));<br>-    count = (size_t)GV.IntVal.getZExtValue();<br>-  }<br>-  return count;<br>-}<br>-<br>-// size_t strlen(const char *src);<br>-GenericValue lle_X_strlen(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  size_t strlenResult = strlen ((char *) GVTOP (Args[0]));<br>-  return size_t_to_GV (strlenResult);<br>-}<br>-<br>-// char *strdup(const char *src);<br>-GenericValue lle_X_strdup(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  assert(isa<PointerType>(FT->getReturnType()) && "strdup must return pointer");<br>-  return PTOGV(strdup((char*)GVTOP(Args[0])));<br>-}<br>-<br>-// char *__strdup(const char *src);<br>-GenericValue lle_X___strdup(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  assert(isa<PointerType>(FT->getReturnType()) &&"_strdup must return pointer");<br>-  return PTOGV(strdup((char*)GVTOP(Args[0])));<br>-}<br>-<br>-// void *memset(void *S, int C, size_t N)<br>-GenericValue lle_X_memset(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 3);<br>-  size_t count = GV_to_size_t (Args[2]);<br>-  assert(isa<PointerType>(FT->getReturnType()) && "memset must return pointer");<br>-  return PTOGV(memset(GVTOP(Args[0]), uint32_t(Args[1].IntVal.getZExtValue()), <br>-                      count));<br>-}<br>-<br>-// void *memcpy(void *Dest, void *src, size_t Size);<br>-GenericValue lle_X_memcpy(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 3);<br>-  assert(isa<PointerType>(FT->getReturnType()) && "memcpy must return pointer");<br>-  size_t count = GV_to_size_t (Args[2]);<br>-  return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));<br>-}<br>-<br>-// void *memcpy(void *Dest, void *src, size_t Size);<br>-GenericValue lle_X_memmove(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 3);<br>-  assert(isa<PointerType>(FT->getReturnType()) && "memmove must return pointer");<br>-  size_t count = GV_to_size_t (Args[2]);<br>-  return PTOGV(memmove((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));<br>-}<br>-<br>-//===----------------------------------------------------------------------===//<br>-// IO Functions...<br>-//===----------------------------------------------------------------------===//<br>-<br>-// getFILE - Turn a pointer in the host address space into a legit pointer in<br>-// the interpreter address space.  This is an identity transformation.<br>-#define getFILE(ptr) ((FILE*)ptr)<br>-<br>-// FILE *fopen(const char *filename, const char *mode);<br>-GenericValue lle_X_fopen(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 2);<br>-  assert(isa<PointerType>(FT->getReturnType()) && "fopen must return pointer");<br>-  return PTOGV(fopen((const char *)GVTOP(Args[0]),<br>-                     (const char *)GVTOP(Args[1])));<br>-}<br>-<br>-// int fclose(FILE *F);<br>-GenericValue lle_X_fclose(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.IntVal = APInt(32, fclose(getFILE(GVTOP(Args[0]))));<br>-  return GV;<br>-}<br>-<br>-// int feof(FILE *stream);<br>-GenericValue lle_X_feof(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-<br>-  GV.IntVal = APInt(32, feof(getFILE(GVTOP(Args[0]))));<br>-  return GV;<br>-}<br>-<br>-// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);<br>-GenericValue lle_X_fread(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 4);<br>-  size_t result;<br>-<br>-  result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),<br>-                 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));<br>-  return size_t_to_GV (result);<br>-}<br>-<br>-// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);<br>-GenericValue lle_X_fwrite(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 4);<br>-  size_t result;<br>-<br>-  result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),<br>-                  GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));<br>-  return size_t_to_GV (result);<br>-}<br>-<br>-// char *fgets(char *s, int n, FILE *stream);<br>-GenericValue lle_X_fgets(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 3);<br>-  return PTOGV(fgets((char*)GVTOP(Args[0]), Args[1].IntVal.getZExtValue(),<br>-                     getFILE(GVTOP(Args[2]))));<br>-}<br>-<br>-// FILE *freopen(const char *path, const char *mode, FILE *stream);<br>-GenericValue lle_X_freopen(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 3);<br>-  assert(isa<PointerType>(FT->getReturnType()) &&"freopen must return pointer");<br>-  return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),<br>-                       getFILE(GVTOP(Args[2]))));<br>-}<br>-<br>-// int fflush(FILE *stream);<br>-GenericValue lle_X_fflush(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.IntVal = APInt(32, fflush(getFILE(GVTOP(Args[0]))));<br>-  return GV;<br>-}<br>-<br>-// int getc(FILE *stream);<br>-GenericValue lle_X_getc(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.IntVal = APInt(32, getc(getFILE(GVTOP(Args[0]))));<br>-  return GV;<br>-}<br>-<br>-// int _IO_getc(FILE *stream);<br>-GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {<br>-  return lle_X_getc(F, Args);<br>-}<br>-<br>-// int fputc(int C, FILE *stream);<br>-GenericValue lle_X_fputc(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 2);<br>-  GenericValue GV;<br>-  GV.IntVal = APInt(32, fputc(Args[0].IntVal.getZExtValue(), <br>-                              getFILE(GVTOP(Args[1]))));<br>-  return GV;<br>-}<br>-<br>-// int ungetc(int C, FILE *stream);<br>-GenericValue lle_X_ungetc(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 2);<br>-  GenericValue GV;<br>-  GV.IntVal = APInt(32, ungetc(Args[0].IntVal.getZExtValue(), <br>-                               getFILE(GVTOP(Args[1]))));<br>-  return GV;<br>-}<br>-<br>-// int ferror (FILE *stream);<br>-GenericValue lle_X_ferror(FunctionType *FT, const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-  GV.IntVal = APInt(32, ferror (getFILE(GVTOP(Args[0]))));<br>-  return GV;<br>-}<br>-<br>-// int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output<br>-// useful.<br>-GenericValue lle_X_fprintf(FunctionType *FT, const vector<GenericValue> &Args) {<br>+// int fprintf(FILE *, const char *, ...) - a very rough implementation to make<br>+// output useful.<br>+GenericValue lle_X_fprintf(const FunctionType *FT,<br>+                           const std::vector<GenericValue> &Args) {<br>   assert(Args.size() >= 2);<br>   char Buffer[10000];<br>-  vector<GenericValue> NewArgs;<br>+  std::vector<GenericValue> NewArgs;<br>   NewArgs.push_back(PTOGV(Buffer));<br>   NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());<br>   GenericValue GV = lle_X_sprintf(FT, NewArgs);<br><br>-  fputs(Buffer, getFILE(GVTOP(Args[0])));<br>-  return GV;<br>-}<br>-<br>-// int __cxa_guard_acquire (__guard *g);<br>-GenericValue lle_X___cxa_guard_acquire(FunctionType *FT, <br>-                                       const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-  GenericValue GV;<br>-#ifdef __linux__<br>-  GV.IntVal = APInt(32, __cxxabiv1::__cxa_guard_acquire (<br>-                          (__cxxabiv1::__guard*)GVTOP(Args[0])));<br>-#else<br>-  assert(0 && "Can't call __cxa_guard_acquire on this platform");<br>-#endif<br>+  fputs(Buffer, (FILE *) GVTOP(Args[0]));<br>   return GV;<br> }<br><br>-// void __cxa_guard_release (__guard *g);<br>-GenericValue lle_X___cxa_guard_release(FunctionType *FT, <br>-                                       const vector<GenericValue> &Args) {<br>-  assert(Args.size() == 1);<br>-#ifdef __linux__<br>-  __cxxabiv1::__cxa_guard_release ((__cxxabiv1::__guard*)GVTOP(Args[0]));<br>-#else<br>-  assert(0 && "Can't call __cxa_guard_release on this platform");<br>-#endif<br>-  return GenericValue();<br>-}<br>-<br> } // End extern "C"<br><br><br> void Interpreter::initializeExternalFunctions() {<br>-  FuncNames["lle_X_putchar"]      = lle_X_putchar;<br>-  FuncNames["lle_X__IO_putc"]     = lle_X__IO_putc;<br>+  FuncNames["lle_X_atexit"]       = lle_X_atexit;<br>   FuncNames["lle_X_exit"]         = lle_X_exit;<br>   FuncNames["lle_X_abort"]        = lle_X_abort;<br>-  FuncNames["lle_X_malloc"]       = lle_X_malloc;<br>-  FuncNames["lle_X_calloc"]       = lle_X_calloc;<br>-  FuncNames["lle_X_realloc"]      = lle_X_realloc;<br>-  FuncNames["lle_X_free"]         = lle_X_free;<br>-  FuncNames["lle_X_atoi"]         = lle_X_atoi;<br>-  FuncNames["lle_X_pow"]          = lle_X_pow;<br>-  FuncNames["lle_X_sin"]          = lle_X_sin;<br>-  FuncNames["lle_X_cos"]          = lle_X_cos;<br>-  FuncNames["lle_X_exp"]          = lle_X_exp;<br>-  FuncNames["lle_X_log"]          = lle_X_log;<br>-  FuncNames["lle_X_floor"]        = lle_X_floor;<br>-  FuncNames["lle_X_srand"]        = lle_X_srand;<br>-  FuncNames["lle_X_rand"]         = lle_X_rand;<br>-#ifdef HAVE_RAND48<br>-  FuncNames["lle_X_drand48"]      = lle_X_drand48;<br>-  FuncNames["lle_X_srand48"]      = lle_X_srand48;<br>-  FuncNames["lle_X_lrand48"]      = lle_X_lrand48;<br>-#endif<br>-  FuncNames["lle_X_sqrt"]         = lle_X_sqrt;<br>-  FuncNames["lle_X_puts"]         = lle_X_puts;<br>+<br>   FuncNames["lle_X_printf"]       = lle_X_printf;<br>   FuncNames["lle_X_sprintf"]      = lle_X_sprintf;<br>   FuncNames["lle_X_sscanf"]       = lle_X_sscanf;<br>   FuncNames["lle_X_scanf"]        = lle_X_scanf;<br>-  FuncNames["lle_i_clock"]        = lle_i_clock;<br>-<br>-  FuncNames["lle_X_strcmp"]       = lle_X_strcmp;<br>-  FuncNames["lle_X_strcat"]       = lle_X_strcat;<br>-  FuncNames["lle_X_strcpy"]       = lle_X_strcpy;<br>-  FuncNames["lle_X_strlen"]       = lle_X_strlen;<br>-  FuncNames["lle_X___strdup"]     = lle_X___strdup;<br>-  FuncNames["lle_X_memset"]       = lle_X_memset;<br>-  FuncNames["lle_X_memcpy"]       = lle_X_memcpy;<br>-  FuncNames["lle_X_memmove"]      = lle_X_memmove;<br>-<br>-  FuncNames["lle_X_fopen"]        = lle_X_fopen;<br>-  FuncNames["lle_X_fclose"]       = lle_X_fclose;<br>-  FuncNames["lle_X_feof"]         = lle_X_feof;<br>-  FuncNames["lle_X_fread"]        = lle_X_fread;<br>-  FuncNames["lle_X_fwrite"]       = lle_X_fwrite;<br>-  FuncNames["lle_X_fgets"]        = lle_X_fgets;<br>-  FuncNames["lle_X_fflush"]       = lle_X_fflush;<br>-  FuncNames["lle_X_fgetc"]        = lle_X_getc;<br>-  FuncNames["lle_X_getc"]         = lle_X_getc;<br>-  FuncNames["lle_X__IO_getc"]     = lle_X__IO_getc;<br>-  FuncNames["lle_X_fputc"]        = lle_X_fputc;<br>-  FuncNames["lle_X_ungetc"]       = lle_X_ungetc;<br>   FuncNames["lle_X_fprintf"]      = lle_X_fprintf;<br>-  FuncNames["lle_X_freopen"]      = lle_X_freopen;<br>-<br>-  FuncNames["lle_X___cxa_guard_acquire"] = lle_X___cxa_guard_acquire;<br>-  FuncNames["lle_X____cxa_guard_release"] = lle_X___cxa_guard_release;<br> }<br><br><br><br>_______________________________________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits<br></div></blockquote></div><br></body></html>