ExternalFunctions.cpp revision 226633
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/DerivedTypes.h"
24193323Sed#include "llvm/Module.h"
25193323Sed#include "llvm/Config/config.h"     // Detect libffi
26198090Srdivacky#include "llvm/Support/ErrorHandling.h"
27218893Sdim#include "llvm/Support/DynamicLibrary.h"
28193323Sed#include "llvm/Target/TargetData.h"
29193323Sed#include "llvm/Support/ManagedStatic.h"
30218893Sdim#include "llvm/Support/Mutex.h"
31193323Sed#include <csignal>
32193323Sed#include <cstdio>
33193323Sed#include <map>
34193323Sed#include <cmath>
35193323Sed#include <cstring>
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));
97198090Srdivacky  ExtName + "_" + F->getNameStr();
98193323Sed
99198090Srdivacky  sys::ScopedLock Writer(*FunctionsLock);
100193323Sed  ExFunc FnPtr = FuncNames[ExtName];
101193323Sed  if (FnPtr == 0)
102198090Srdivacky    FnPtr = FuncNames["lle_X_" + F->getNameStr()];
103193323Sed  if (FnPtr == 0)  // Try calling a generic function... if it exists...
104198090Srdivacky    FnPtr = (ExFunc)(intptr_t)
105198090Srdivacky      sys::DynamicLibrary::SearchForAddressOfSymbol("lle_X_"+F->getNameStr());
106193323Sed  if (FnPtr != 0)
107193323Sed    ExportedFunctions->insert(std::make_pair(F, FnPtr));  // Cache for later
108193323Sed  return FnPtr;
109193323Sed}
110193323Sed
111193323Sed#ifdef USE_LIBFFI
112226633Sdimstatic ffi_type *ffiTypeFor(Type *Ty) {
113193323Sed  switch (Ty->getTypeID()) {
114193323Sed    case Type::VoidTyID: return &ffi_type_void;
115193323Sed    case Type::IntegerTyID:
116193323Sed      switch (cast<IntegerType>(Ty)->getBitWidth()) {
117193323Sed        case 8:  return &ffi_type_sint8;
118193323Sed        case 16: return &ffi_type_sint16;
119193323Sed        case 32: return &ffi_type_sint32;
120193323Sed        case 64: return &ffi_type_sint64;
121193323Sed      }
122193323Sed    case Type::FloatTyID:   return &ffi_type_float;
123193323Sed    case Type::DoubleTyID:  return &ffi_type_double;
124193323Sed    case Type::PointerTyID: return &ffi_type_pointer;
125193323Sed    default: break;
126193323Sed  }
127193323Sed  // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
128207618Srdivacky  report_fatal_error("Type could not be mapped for use with libffi.");
129193323Sed  return NULL;
130193323Sed}
131193323Sed
132226633Sdimstatic void *ffiValueFor(Type *Ty, const GenericValue &AV,
133193323Sed                         void *ArgDataPtr) {
134193323Sed  switch (Ty->getTypeID()) {
135193323Sed    case Type::IntegerTyID:
136193323Sed      switch (cast<IntegerType>(Ty)->getBitWidth()) {
137193323Sed        case 8: {
138193323Sed          int8_t *I8Ptr = (int8_t *) ArgDataPtr;
139193323Sed          *I8Ptr = (int8_t) AV.IntVal.getZExtValue();
140193323Sed          return ArgDataPtr;
141193323Sed        }
142193323Sed        case 16: {
143193323Sed          int16_t *I16Ptr = (int16_t *) ArgDataPtr;
144193323Sed          *I16Ptr = (int16_t) AV.IntVal.getZExtValue();
145193323Sed          return ArgDataPtr;
146193323Sed        }
147193323Sed        case 32: {
148193323Sed          int32_t *I32Ptr = (int32_t *) ArgDataPtr;
149193323Sed          *I32Ptr = (int32_t) AV.IntVal.getZExtValue();
150193323Sed          return ArgDataPtr;
151193323Sed        }
152193323Sed        case 64: {
153193323Sed          int64_t *I64Ptr = (int64_t *) ArgDataPtr;
154193323Sed          *I64Ptr = (int64_t) AV.IntVal.getZExtValue();
155193323Sed          return ArgDataPtr;
156193323Sed        }
157193323Sed      }
158193323Sed    case Type::FloatTyID: {
159193323Sed      float *FloatPtr = (float *) ArgDataPtr;
160199481Srdivacky      *FloatPtr = AV.FloatVal;
161193323Sed      return ArgDataPtr;
162193323Sed    }
163193323Sed    case Type::DoubleTyID: {
164193323Sed      double *DoublePtr = (double *) ArgDataPtr;
165193323Sed      *DoublePtr = AV.DoubleVal;
166193323Sed      return ArgDataPtr;
167193323Sed    }
168193323Sed    case Type::PointerTyID: {
169193323Sed      void **PtrPtr = (void **) ArgDataPtr;
170193323Sed      *PtrPtr = GVTOP(AV);
171193323Sed      return ArgDataPtr;
172193323Sed    }
173193323Sed    default: break;
174193323Sed  }
175193323Sed  // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
176207618Srdivacky  report_fatal_error("Type value could not be mapped for use with libffi.");
177193323Sed  return NULL;
178193323Sed}
179193323Sed
180193323Sedstatic bool ffiInvoke(RawFunc Fn, Function *F,
181193323Sed                      const std::vector<GenericValue> &ArgVals,
182193323Sed                      const TargetData *TD, GenericValue &Result) {
183193323Sed  ffi_cif cif;
184226633Sdim  FunctionType *FTy = F->getFunctionType();
185193323Sed  const unsigned NumArgs = F->arg_size();
186193323Sed
187193323Sed  // TODO: We don't have type information about the remaining arguments, because
188193323Sed  // this information is never passed into ExecutionEngine::runFunction().
189193323Sed  if (ArgVals.size() > NumArgs && F->isVarArg()) {
190207618Srdivacky    report_fatal_error("Calling external var arg function '" + F->getName()
191198090Srdivacky                      + "' is not supported by the Interpreter.");
192193323Sed  }
193193323Sed
194193323Sed  unsigned ArgBytes = 0;
195193323Sed
196193323Sed  std::vector<ffi_type*> args(NumArgs);
197193323Sed  for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
198193323Sed       A != E; ++A) {
199193323Sed    const unsigned ArgNo = A->getArgNo();
200226633Sdim    Type *ArgTy = FTy->getParamType(ArgNo);
201193323Sed    args[ArgNo] = ffiTypeFor(ArgTy);
202193323Sed    ArgBytes += TD->getTypeStoreSize(ArgTy);
203193323Sed  }
204193323Sed
205198090Srdivacky  SmallVector<uint8_t, 128> ArgData;
206198090Srdivacky  ArgData.resize(ArgBytes);
207198090Srdivacky  uint8_t *ArgDataPtr = ArgData.data();
208198090Srdivacky  SmallVector<void*, 16> values(NumArgs);
209193323Sed  for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
210193323Sed       A != E; ++A) {
211193323Sed    const unsigned ArgNo = A->getArgNo();
212226633Sdim    Type *ArgTy = FTy->getParamType(ArgNo);
213193323Sed    values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
214193323Sed    ArgDataPtr += TD->getTypeStoreSize(ArgTy);
215193323Sed  }
216193323Sed
217226633Sdim  Type *RetTy = FTy->getReturnType();
218193323Sed  ffi_type *rtype = ffiTypeFor(RetTy);
219193323Sed
220193323Sed  if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) {
221198090Srdivacky    SmallVector<uint8_t, 128> ret;
222193323Sed    if (RetTy->getTypeID() != Type::VoidTyID)
223198090Srdivacky      ret.resize(TD->getTypeStoreSize(RetTy));
224198090Srdivacky    ffi_call(&cif, Fn, ret.data(), values.data());
225193323Sed    switch (RetTy->getTypeID()) {
226193323Sed      case Type::IntegerTyID:
227193323Sed        switch (cast<IntegerType>(RetTy)->getBitWidth()) {
228198090Srdivacky          case 8:  Result.IntVal = APInt(8 , *(int8_t *) ret.data()); break;
229198090Srdivacky          case 16: Result.IntVal = APInt(16, *(int16_t*) ret.data()); break;
230198090Srdivacky          case 32: Result.IntVal = APInt(32, *(int32_t*) ret.data()); break;
231198090Srdivacky          case 64: Result.IntVal = APInt(64, *(int64_t*) ret.data()); break;
232193323Sed        }
233193323Sed        break;
234198090Srdivacky      case Type::FloatTyID:   Result.FloatVal   = *(float *) ret.data(); break;
235198090Srdivacky      case Type::DoubleTyID:  Result.DoubleVal  = *(double*) ret.data(); break;
236198090Srdivacky      case Type::PointerTyID: Result.PointerVal = *(void **) ret.data(); break;
237193323Sed      default: break;
238193323Sed    }
239193323Sed    return true;
240193323Sed  }
241193323Sed
242193323Sed  return false;
243193323Sed}
244193323Sed#endif // USE_LIBFFI
245193323Sed
246193323SedGenericValue Interpreter::callExternalFunction(Function *F,
247193323Sed                                     const std::vector<GenericValue> &ArgVals) {
248193323Sed  TheInterpreter = this;
249193323Sed
250194710Sed  FunctionsLock->acquire();
251194710Sed
252193323Sed  // Do a lookup to see if the function is in our cache... this should just be a
253193323Sed  // deferred annotation!
254193323Sed  std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F);
255193323Sed  if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F)
256194710Sed                                                   : FI->second) {
257194710Sed    FunctionsLock->release();
258193323Sed    return Fn(F->getFunctionType(), ArgVals);
259194710Sed  }
260193323Sed
261193323Sed#ifdef USE_LIBFFI
262193323Sed  std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
263193323Sed  RawFunc RawFn;
264193323Sed  if (RF == RawFunctions->end()) {
265193323Sed    RawFn = (RawFunc)(intptr_t)
266193323Sed      sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
267206083Srdivacky    if (!RawFn)
268210299Sed      RawFn = (RawFunc)(intptr_t)getPointerToGlobalIfAvailable(F);
269193323Sed    if (RawFn != 0)
270193323Sed      RawFunctions->insert(std::make_pair(F, RawFn));  // Cache for later
271193323Sed  } else {
272193323Sed    RawFn = RF->second;
273193323Sed  }
274198090Srdivacky
275194710Sed  FunctionsLock->release();
276193323Sed
277193323Sed  GenericValue Result;
278193323Sed  if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getTargetData(), Result))
279193323Sed    return Result;
280193323Sed#endif // USE_LIBFFI
281193323Sed
282198090Srdivacky  if (F->getName() == "__main")
283198090Srdivacky    errs() << "Tried to execute an unknown external function: "
284224145Sdim      << *F->getType() << " __main\n";
285198090Srdivacky  else
286207618Srdivacky    report_fatal_error("Tried to execute an unknown external function: " +
287224145Sdim                       F->getName());
288199481Srdivacky#ifndef USE_LIBFFI
289199481Srdivacky  errs() << "Recompiling LLVM with --enable-libffi might help.\n";
290199481Srdivacky#endif
291193323Sed  return GenericValue();
292193323Sed}
293193323Sed
294193323Sed
295193323Sed//===----------------------------------------------------------------------===//
296193323Sed//  Functions "exported" to the running application...
297193323Sed//
298198090Srdivacky
299198090Srdivacky// Visual Studio warns about returning GenericValue in extern "C" linkage
300198090Srdivacky#ifdef _MSC_VER
301198090Srdivacky    #pragma warning(disable : 4190)
302198090Srdivacky#endif
303198090Srdivacky
304193323Sedextern "C" {  // Don't add C++ manglings to llvm mangling :)
305193323Sed
306193323Sed// void atexit(Function*)
307226633SdimGenericValue lle_X_atexit(FunctionType *FT,
308193323Sed                          const std::vector<GenericValue> &Args) {
309193323Sed  assert(Args.size() == 1);
310193323Sed  TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
311193323Sed  GenericValue GV;
312193323Sed  GV.IntVal = 0;
313193323Sed  return GV;
314193323Sed}
315193323Sed
316193323Sed// void exit(int)
317226633SdimGenericValue lle_X_exit(FunctionType *FT,
318193323Sed                        const std::vector<GenericValue> &Args) {
319193323Sed  TheInterpreter->exitCalled(Args[0]);
320193323Sed  return GenericValue();
321193323Sed}
322193323Sed
323193323Sed// void abort(void)
324226633SdimGenericValue lle_X_abort(FunctionType *FT,
325193323Sed                         const std::vector<GenericValue> &Args) {
326198090Srdivacky  //FIXME: should we report or raise here?
327207618Srdivacky  //report_fatal_error("Interpreted program raised SIGABRT");
328193323Sed  raise (SIGABRT);
329193323Sed  return GenericValue();
330193323Sed}
331193323Sed
332193323Sed// int sprintf(char *, const char *, ...) - a very rough implementation to make
333193323Sed// output useful.
334226633SdimGenericValue lle_X_sprintf(FunctionType *FT,
335193323Sed                           const std::vector<GenericValue> &Args) {
336193323Sed  char *OutputBuffer = (char *)GVTOP(Args[0]);
337193323Sed  const char *FmtStr = (const char *)GVTOP(Args[1]);
338193323Sed  unsigned ArgNo = 2;
339193323Sed
340193323Sed  // printf should return # chars printed.  This is completely incorrect, but
341193323Sed  // close enough for now.
342198090Srdivacky  GenericValue GV;
343193323Sed  GV.IntVal = APInt(32, strlen(FmtStr));
344193323Sed  while (1) {
345193323Sed    switch (*FmtStr) {
346193323Sed    case 0: return GV;             // Null terminator...
347193323Sed    default:                       // Normal nonspecial character
348193323Sed      sprintf(OutputBuffer++, "%c", *FmtStr++);
349193323Sed      break;
350193323Sed    case '\\': {                   // Handle escape codes
351193323Sed      sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
352193323Sed      FmtStr += 2; OutputBuffer += 2;
353193323Sed      break;
354193323Sed    }
355193323Sed    case '%': {                    // Handle format specifiers
356193323Sed      char FmtBuf[100] = "", Buffer[1000] = "";
357193323Sed      char *FB = FmtBuf;
358193323Sed      *FB++ = *FmtStr++;
359193323Sed      char Last = *FB++ = *FmtStr++;
360193323Sed      unsigned HowLong = 0;
361193323Sed      while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
362193323Sed             Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
363193323Sed             Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
364193323Sed             Last != 'p' && Last != 's' && Last != '%') {
365193323Sed        if (Last == 'l' || Last == 'L') HowLong++;  // Keep track of l's
366193323Sed        Last = *FB++ = *FmtStr++;
367193323Sed      }
368193323Sed      *FB = 0;
369193323Sed
370193323Sed      switch (Last) {
371193323Sed      case '%':
372203954Srdivacky        memcpy(Buffer, "%", 2); break;
373193323Sed      case 'c':
374193323Sed        sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
375193323Sed        break;
376193323Sed      case 'd': case 'i':
377193323Sed      case 'u': case 'o':
378193323Sed      case 'x': case 'X':
379193323Sed        if (HowLong >= 1) {
380193323Sed          if (HowLong == 1 &&
381193323Sed              TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
382193323Sed              sizeof(long) < sizeof(int64_t)) {
383193323Sed            // Make sure we use %lld with a 64 bit argument because we might be
384193323Sed            // compiling LLI on a 32 bit compiler.
385193323Sed            unsigned Size = strlen(FmtBuf);
386193323Sed            FmtBuf[Size] = FmtBuf[Size-1];
387193323Sed            FmtBuf[Size+1] = 0;
388193323Sed            FmtBuf[Size-1] = 'l';
389193323Sed          }
390193323Sed          sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
391193323Sed        } else
392193323Sed          sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
393193323Sed        break;
394193323Sed      case 'e': case 'E': case 'g': case 'G': case 'f':
395193323Sed        sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
396193323Sed      case 'p':
397193323Sed        sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
398193323Sed      case 's':
399193323Sed        sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
400198090Srdivacky      default:
401198090Srdivacky        errs() << "<unknown printf code '" << *FmtStr << "'!>";
402193323Sed        ArgNo++; break;
403193323Sed      }
404203954Srdivacky      size_t Len = strlen(Buffer);
405203954Srdivacky      memcpy(OutputBuffer, Buffer, Len + 1);
406203954Srdivacky      OutputBuffer += Len;
407193323Sed      }
408193323Sed      break;
409193323Sed    }
410193323Sed  }
411193323Sed  return GV;
412193323Sed}
413193323Sed
414193323Sed// int printf(const char *, ...) - a very rough implementation to make output
415193323Sed// useful.
416226633SdimGenericValue lle_X_printf(FunctionType *FT,
417193323Sed                          const std::vector<GenericValue> &Args) {
418193323Sed  char Buffer[10000];
419193323Sed  std::vector<GenericValue> NewArgs;
420193323Sed  NewArgs.push_back(PTOGV((void*)&Buffer[0]));
421193323Sed  NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
422193323Sed  GenericValue GV = lle_X_sprintf(FT, NewArgs);
423198090Srdivacky  outs() << Buffer;
424193323Sed  return GV;
425193323Sed}
426193323Sed
427193323Sed// int sscanf(const char *format, ...);
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],
438193323Sed                        Args[5], Args[6], Args[7], Args[8], Args[9]));
439193323Sed  return GV;
440193323Sed}
441193323Sed
442193323Sed// int scanf(const char *format, ...);
443226633SdimGenericValue lle_X_scanf(FunctionType *FT,
444193323Sed                         const std::vector<GenericValue> &args) {
445193323Sed  assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
446193323Sed
447193323Sed  char *Args[10];
448193323Sed  for (unsigned i = 0; i < args.size(); ++i)
449193323Sed    Args[i] = (char*)GVTOP(args[i]);
450193323Sed
451193323Sed  GenericValue GV;
452193323Sed  GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
453193323Sed                        Args[5], Args[6], Args[7], Args[8], Args[9]));
454193323Sed  return GV;
455193323Sed}
456193323Sed
457193323Sed// int fprintf(FILE *, const char *, ...) - a very rough implementation to make
458193323Sed// output useful.
459226633SdimGenericValue lle_X_fprintf(FunctionType *FT,
460193323Sed                           const std::vector<GenericValue> &Args) {
461193323Sed  assert(Args.size() >= 2);
462193323Sed  char Buffer[10000];
463193323Sed  std::vector<GenericValue> NewArgs;
464193323Sed  NewArgs.push_back(PTOGV(Buffer));
465193323Sed  NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
466193323Sed  GenericValue GV = lle_X_sprintf(FT, NewArgs);
467193323Sed
468193323Sed  fputs(Buffer, (FILE *) GVTOP(Args[0]));
469193323Sed  return GV;
470193323Sed}
471193323Sed
472193323Sed} // End extern "C"
473193323Sed
474198090Srdivacky// Done with externals; turn the warning back on
475198090Srdivacky#ifdef _MSC_VER
476198090Srdivacky    #pragma warning(default: 4190)
477198090Srdivacky#endif
478193323Sed
479198090Srdivacky
480193323Sedvoid Interpreter::initializeExternalFunctions() {
481198090Srdivacky  sys::ScopedLock Writer(*FunctionsLock);
482193323Sed  FuncNames["lle_X_atexit"]       = lle_X_atexit;
483193323Sed  FuncNames["lle_X_exit"]         = lle_X_exit;
484193323Sed  FuncNames["lle_X_abort"]        = lle_X_abort;
485193323Sed
486193323Sed  FuncNames["lle_X_printf"]       = lle_X_printf;
487193323Sed  FuncNames["lle_X_sprintf"]      = lle_X_sprintf;
488193323Sed  FuncNames["lle_X_sscanf"]       = lle_X_sscanf;
489193323Sed  FuncNames["lle_X_scanf"]        = lle_X_scanf;
490193323Sed  FuncNames["lle_X_fprintf"]      = lle_X_fprintf;
491193323Sed}
492