land David Blaikie's patch to de-constify Type, with a few tweaks.
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / ExternalFunctions.cpp
1 //===-- ExternalFunctions.cpp - Implement External Functions --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file contains both code to deal with invoking "external" functions, but
11 //  also contains code that implements "exported" external functions.
12 //
13 //  There are currently two mechanisms for handling external functions in the
14 //  Interpreter.  The first is to implement lle_* wrapper functions that are
15 //  specific to well-known library functions which manually translate the
16 //  arguments from GenericValues and make the call.  If such a wrapper does
17 //  not exist, and libffi is available, then the Interpreter will attempt to
18 //  invoke the function using libffi, after finding its address.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "Interpreter.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Config/config.h"     // Detect libffi
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/DynamicLibrary.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/Mutex.h"
31 #include <csignal>
32 #include <cstdio>
33 #include <map>
34 #include <cmath>
35 #include <cstring>
36
37 #ifdef HAVE_FFI_CALL
38 #ifdef HAVE_FFI_H
39 #include <ffi.h>
40 #define USE_LIBFFI
41 #elif HAVE_FFI_FFI_H
42 #include <ffi/ffi.h>
43 #define USE_LIBFFI
44 #endif
45 #endif
46
47 using namespace llvm;
48
49 static ManagedStatic<sys::Mutex> FunctionsLock;
50
51 typedef GenericValue (*ExFunc)(FunctionType *,
52                                const std::vector<GenericValue> &);
53 static ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions;
54 static std::map<std::string, ExFunc> FuncNames;
55
56 #ifdef USE_LIBFFI
57 typedef void (*RawFunc)();
58 static ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions;
59 #endif
60
61 static Interpreter *TheInterpreter;
62
63 static char getTypeID(Type *Ty) {
64   switch (Ty->getTypeID()) {
65   case Type::VoidTyID:    return 'V';
66   case Type::IntegerTyID:
67     switch (cast<IntegerType>(Ty)->getBitWidth()) {
68       case 1:  return 'o';
69       case 8:  return 'B';
70       case 16: return 'S';
71       case 32: return 'I';
72       case 64: return 'L';
73       default: return 'N';
74     }
75   case Type::FloatTyID:   return 'F';
76   case Type::DoubleTyID:  return 'D';
77   case Type::PointerTyID: return 'P';
78   case Type::FunctionTyID:return 'M';
79   case Type::StructTyID:  return 'T';
80   case Type::ArrayTyID:   return 'A';
81   default: return 'U';
82   }
83 }
84
85 // Try to find address of external function given a Function object.
86 // Please note, that interpreter doesn't know how to assemble a
87 // real call in general case (this is JIT job), that's why it assumes,
88 // that all external functions has the same (and pretty "general") signature.
89 // The typical example of such functions are "lle_X_" ones.
90 static ExFunc lookupFunction(const Function *F) {
91   // Function not found, look it up... start by figuring out what the
92   // composite function name should be.
93   std::string ExtName = "lle_";
94   FunctionType *FT = F->getFunctionType();
95   for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
96     ExtName += getTypeID(FT->getContainedType(i));
97   ExtName + "_" + F->getNameStr();
98
99   sys::ScopedLock Writer(*FunctionsLock);
100   ExFunc FnPtr = FuncNames[ExtName];
101   if (FnPtr == 0)
102     FnPtr = FuncNames["lle_X_" + F->getNameStr()];
103   if (FnPtr == 0)  // Try calling a generic function... if it exists...
104     FnPtr = (ExFunc)(intptr_t)
105       sys::DynamicLibrary::SearchForAddressOfSymbol("lle_X_"+F->getNameStr());
106   if (FnPtr != 0)
107     ExportedFunctions->insert(std::make_pair(F, FnPtr));  // Cache for later
108   return FnPtr;
109 }
110
111 #ifdef USE_LIBFFI
112 static ffi_type *ffiTypeFor(Type *Ty) {
113   switch (Ty->getTypeID()) {
114     case Type::VoidTyID: return &ffi_type_void;
115     case Type::IntegerTyID:
116       switch (cast<IntegerType>(Ty)->getBitWidth()) {
117         case 8:  return &ffi_type_sint8;
118         case 16: return &ffi_type_sint16;
119         case 32: return &ffi_type_sint32;
120         case 64: return &ffi_type_sint64;
121       }
122     case Type::FloatTyID:   return &ffi_type_float;
123     case Type::DoubleTyID:  return &ffi_type_double;
124     case Type::PointerTyID: return &ffi_type_pointer;
125     default: break;
126   }
127   // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
128   report_fatal_error("Type could not be mapped for use with libffi.");
129   return NULL;
130 }
131
132 static void *ffiValueFor(Type *Ty, const GenericValue &AV,
133                          void *ArgDataPtr) {
134   switch (Ty->getTypeID()) {
135     case Type::IntegerTyID:
136       switch (cast<IntegerType>(Ty)->getBitWidth()) {
137         case 8: {
138           int8_t *I8Ptr = (int8_t *) ArgDataPtr;
139           *I8Ptr = (int8_t) AV.IntVal.getZExtValue();
140           return ArgDataPtr;
141         }
142         case 16: {
143           int16_t *I16Ptr = (int16_t *) ArgDataPtr;
144           *I16Ptr = (int16_t) AV.IntVal.getZExtValue();
145           return ArgDataPtr;
146         }
147         case 32: {
148           int32_t *I32Ptr = (int32_t *) ArgDataPtr;
149           *I32Ptr = (int32_t) AV.IntVal.getZExtValue();
150           return ArgDataPtr;
151         }
152         case 64: {
153           int64_t *I64Ptr = (int64_t *) ArgDataPtr;
154           *I64Ptr = (int64_t) AV.IntVal.getZExtValue();
155           return ArgDataPtr;
156         }
157       }
158     case Type::FloatTyID: {
159       float *FloatPtr = (float *) ArgDataPtr;
160       *FloatPtr = AV.FloatVal;
161       return ArgDataPtr;
162     }
163     case Type::DoubleTyID: {
164       double *DoublePtr = (double *) ArgDataPtr;
165       *DoublePtr = AV.DoubleVal;
166       return ArgDataPtr;
167     }
168     case Type::PointerTyID: {
169       void **PtrPtr = (void **) ArgDataPtr;
170       *PtrPtr = GVTOP(AV);
171       return ArgDataPtr;
172     }
173     default: break;
174   }
175   // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
176   report_fatal_error("Type value could not be mapped for use with libffi.");
177   return NULL;
178 }
179
180 static bool ffiInvoke(RawFunc Fn, Function *F,
181                       const std::vector<GenericValue> &ArgVals,
182                       const TargetData *TD, GenericValue &Result) {
183   ffi_cif cif;
184   FunctionType *FTy = F->getFunctionType();
185   const unsigned NumArgs = F->arg_size();
186
187   // TODO: We don't have type information about the remaining arguments, because
188   // this information is never passed into ExecutionEngine::runFunction().
189   if (ArgVals.size() > NumArgs && F->isVarArg()) {
190     report_fatal_error("Calling external var arg function '" + F->getName()
191                       + "' is not supported by the Interpreter.");
192   }
193
194   unsigned ArgBytes = 0;
195
196   std::vector<ffi_type*> args(NumArgs);
197   for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
198        A != E; ++A) {
199     const unsigned ArgNo = A->getArgNo();
200     Type *ArgTy = FTy->getParamType(ArgNo);
201     args[ArgNo] = ffiTypeFor(ArgTy);
202     ArgBytes += TD->getTypeStoreSize(ArgTy);
203   }
204
205   SmallVector<uint8_t, 128> ArgData;
206   ArgData.resize(ArgBytes);
207   uint8_t *ArgDataPtr = ArgData.data();
208   SmallVector<void*, 16> values(NumArgs);
209   for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
210        A != E; ++A) {
211     const unsigned ArgNo = A->getArgNo();
212     Type *ArgTy = FTy->getParamType(ArgNo);
213     values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
214     ArgDataPtr += TD->getTypeStoreSize(ArgTy);
215   }
216
217   Type *RetTy = FTy->getReturnType();
218   ffi_type *rtype = ffiTypeFor(RetTy);
219
220   if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) {
221     SmallVector<uint8_t, 128> ret;
222     if (RetTy->getTypeID() != Type::VoidTyID)
223       ret.resize(TD->getTypeStoreSize(RetTy));
224     ffi_call(&cif, Fn, ret.data(), values.data());
225     switch (RetTy->getTypeID()) {
226       case Type::IntegerTyID:
227         switch (cast<IntegerType>(RetTy)->getBitWidth()) {
228           case 8:  Result.IntVal = APInt(8 , *(int8_t *) ret.data()); break;
229           case 16: Result.IntVal = APInt(16, *(int16_t*) ret.data()); break;
230           case 32: Result.IntVal = APInt(32, *(int32_t*) ret.data()); break;
231           case 64: Result.IntVal = APInt(64, *(int64_t*) ret.data()); break;
232         }
233         break;
234       case Type::FloatTyID:   Result.FloatVal   = *(float *) ret.data(); break;
235       case Type::DoubleTyID:  Result.DoubleVal  = *(double*) ret.data(); break;
236       case Type::PointerTyID: Result.PointerVal = *(void **) ret.data(); break;
237       default: break;
238     }
239     return true;
240   }
241
242   return false;
243 }
244 #endif // USE_LIBFFI
245
246 GenericValue Interpreter::callExternalFunction(Function *F,
247                                      const std::vector<GenericValue> &ArgVals) {
248   TheInterpreter = this;
249
250   FunctionsLock->acquire();
251
252   // Do a lookup to see if the function is in our cache... this should just be a
253   // deferred annotation!
254   std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F);
255   if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F)
256                                                    : FI->second) {
257     FunctionsLock->release();
258     return Fn(F->getFunctionType(), ArgVals);
259   }
260
261 #ifdef USE_LIBFFI
262   std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
263   RawFunc RawFn;
264   if (RF == RawFunctions->end()) {
265     RawFn = (RawFunc)(intptr_t)
266       sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
267     if (!RawFn)
268       RawFn = (RawFunc)(intptr_t)getPointerToGlobalIfAvailable(F);
269     if (RawFn != 0)
270       RawFunctions->insert(std::make_pair(F, RawFn));  // Cache for later
271   } else {
272     RawFn = RF->second;
273   }
274
275   FunctionsLock->release();
276
277   GenericValue Result;
278   if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getTargetData(), Result))
279     return Result;
280 #endif // USE_LIBFFI
281
282   if (F->getName() == "__main")
283     errs() << "Tried to execute an unknown external function: "
284       << *F->getType() << " __main\n";
285   else
286     report_fatal_error("Tried to execute an unknown external function: " +
287                        F->getName());
288 #ifndef USE_LIBFFI
289   errs() << "Recompiling LLVM with --enable-libffi might help.\n";
290 #endif
291   return GenericValue();
292 }
293
294
295 //===----------------------------------------------------------------------===//
296 //  Functions "exported" to the running application...
297 //
298
299 // Visual Studio warns about returning GenericValue in extern "C" linkage
300 #ifdef _MSC_VER
301     #pragma warning(disable : 4190)
302 #endif
303
304 extern "C" {  // Don't add C++ manglings to llvm mangling :)
305
306 // void atexit(Function*)
307 GenericValue lle_X_atexit(FunctionType *FT,
308                           const std::vector<GenericValue> &Args) {
309   assert(Args.size() == 1);
310   TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
311   GenericValue GV;
312   GV.IntVal = 0;
313   return GV;
314 }
315
316 // void exit(int)
317 GenericValue lle_X_exit(FunctionType *FT,
318                         const std::vector<GenericValue> &Args) {
319   TheInterpreter->exitCalled(Args[0]);
320   return GenericValue();
321 }
322
323 // void abort(void)
324 GenericValue lle_X_abort(FunctionType *FT,
325                          const std::vector<GenericValue> &Args) {
326   //FIXME: should we report or raise here?
327   //report_fatal_error("Interpreted program raised SIGABRT");
328   raise (SIGABRT);
329   return GenericValue();
330 }
331
332 // int sprintf(char *, const char *, ...) - a very rough implementation to make
333 // output useful.
334 GenericValue lle_X_sprintf(FunctionType *FT,
335                            const std::vector<GenericValue> &Args) {
336   char *OutputBuffer = (char *)GVTOP(Args[0]);
337   const char *FmtStr = (const char *)GVTOP(Args[1]);
338   unsigned ArgNo = 2;
339
340   // printf should return # chars printed.  This is completely incorrect, but
341   // close enough for now.
342   GenericValue GV;
343   GV.IntVal = APInt(32, strlen(FmtStr));
344   while (1) {
345     switch (*FmtStr) {
346     case 0: return GV;             // Null terminator...
347     default:                       // Normal nonspecial character
348       sprintf(OutputBuffer++, "%c", *FmtStr++);
349       break;
350     case '\\': {                   // Handle escape codes
351       sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
352       FmtStr += 2; OutputBuffer += 2;
353       break;
354     }
355     case '%': {                    // Handle format specifiers
356       char FmtBuf[100] = "", Buffer[1000] = "";
357       char *FB = FmtBuf;
358       *FB++ = *FmtStr++;
359       char Last = *FB++ = *FmtStr++;
360       unsigned HowLong = 0;
361       while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
362              Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
363              Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
364              Last != 'p' && Last != 's' && Last != '%') {
365         if (Last == 'l' || Last == 'L') HowLong++;  // Keep track of l's
366         Last = *FB++ = *FmtStr++;
367       }
368       *FB = 0;
369
370       switch (Last) {
371       case '%':
372         memcpy(Buffer, "%", 2); break;
373       case 'c':
374         sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
375         break;
376       case 'd': case 'i':
377       case 'u': case 'o':
378       case 'x': case 'X':
379         if (HowLong >= 1) {
380           if (HowLong == 1 &&
381               TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
382               sizeof(long) < sizeof(int64_t)) {
383             // Make sure we use %lld with a 64 bit argument because we might be
384             // compiling LLI on a 32 bit compiler.
385             unsigned Size = strlen(FmtBuf);
386             FmtBuf[Size] = FmtBuf[Size-1];
387             FmtBuf[Size+1] = 0;
388             FmtBuf[Size-1] = 'l';
389           }
390           sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
391         } else
392           sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
393         break;
394       case 'e': case 'E': case 'g': case 'G': case 'f':
395         sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
396       case 'p':
397         sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
398       case 's':
399         sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
400       default:
401         errs() << "<unknown printf code '" << *FmtStr << "'!>";
402         ArgNo++; break;
403       }
404       size_t Len = strlen(Buffer);
405       memcpy(OutputBuffer, Buffer, Len + 1);
406       OutputBuffer += Len;
407       }
408       break;
409     }
410   }
411   return GV;
412 }
413
414 // int printf(const char *, ...) - a very rough implementation to make output
415 // useful.
416 GenericValue lle_X_printf(FunctionType *FT,
417                           const std::vector<GenericValue> &Args) {
418   char Buffer[10000];
419   std::vector<GenericValue> NewArgs;
420   NewArgs.push_back(PTOGV((void*)&Buffer[0]));
421   NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
422   GenericValue GV = lle_X_sprintf(FT, NewArgs);
423   outs() << Buffer;
424   return GV;
425 }
426
427 // int sscanf(const char *format, ...);
428 GenericValue lle_X_sscanf(FunctionType *FT,
429                           const std::vector<GenericValue> &args) {
430   assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
431
432   char *Args[10];
433   for (unsigned i = 0; i < args.size(); ++i)
434     Args[i] = (char*)GVTOP(args[i]);
435
436   GenericValue GV;
437   GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
438                         Args[5], Args[6], Args[7], Args[8], Args[9]));
439   return GV;
440 }
441
442 // int scanf(const char *format, ...);
443 GenericValue lle_X_scanf(FunctionType *FT,
444                          const std::vector<GenericValue> &args) {
445   assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
446
447   char *Args[10];
448   for (unsigned i = 0; i < args.size(); ++i)
449     Args[i] = (char*)GVTOP(args[i]);
450
451   GenericValue GV;
452   GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
453                         Args[5], Args[6], Args[7], Args[8], Args[9]));
454   return GV;
455 }
456
457 // int fprintf(FILE *, const char *, ...) - a very rough implementation to make
458 // output useful.
459 GenericValue lle_X_fprintf(FunctionType *FT,
460                            const std::vector<GenericValue> &Args) {
461   assert(Args.size() >= 2);
462   char Buffer[10000];
463   std::vector<GenericValue> NewArgs;
464   NewArgs.push_back(PTOGV(Buffer));
465   NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
466   GenericValue GV = lle_X_sprintf(FT, NewArgs);
467
468   fputs(Buffer, (FILE *) GVTOP(Args[0]));
469   return GV;
470 }
471
472 } // End extern "C"
473
474 // Done with externals; turn the warning back on
475 #ifdef _MSC_VER
476     #pragma warning(default: 4190)
477 #endif
478
479
480 void Interpreter::initializeExternalFunctions() {
481   sys::ScopedLock Writer(*FunctionsLock);
482   FuncNames["lle_X_atexit"]       = lle_X_atexit;
483   FuncNames["lle_X_exit"]         = lle_X_exit;
484   FuncNames["lle_X_abort"]        = lle_X_abort;
485
486   FuncNames["lle_X_printf"]       = lle_X_printf;
487   FuncNames["lle_X_sprintf"]      = lle_X_sprintf;
488   FuncNames["lle_X_sscanf"]       = lle_X_sscanf;
489   FuncNames["lle_X_scanf"]        = lle_X_scanf;
490   FuncNames["lle_X_fprintf"]      = lle_X_fprintf;
491 }