1 //===-- ExternalFunctions.cpp - Implement External Functions --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains both code to deal with invoking "external" functions, but
11 // also contains code that implements "exported" external functions.
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.
20 //===----------------------------------------------------------------------===//
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/System/DynamicLibrary.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/System/Mutex.h"
36 // Some platforms may need malloc.h for alloca.
53 static ManagedStatic<sys::Mutex> FunctionsLock;
55 typedef GenericValue (*ExFunc)(const FunctionType *,
56 const std::vector<GenericValue> &);
57 static ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions;
58 static std::map<std::string, ExFunc> FuncNames;
61 typedef void (*RawFunc)();
62 static ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions;
65 static Interpreter *TheInterpreter;
67 static char getTypeID(const Type *Ty) {
68 switch (Ty->getTypeID()) {
69 case Type::VoidTyID: return 'V';
70 case Type::IntegerTyID:
71 switch (cast<IntegerType>(Ty)->getBitWidth()) {
79 case Type::FloatTyID: return 'F';
80 case Type::DoubleTyID: return 'D';
81 case Type::PointerTyID: return 'P';
82 case Type::FunctionTyID:return 'M';
83 case Type::StructTyID: return 'T';
84 case Type::ArrayTyID: return 'A';
85 case Type::OpaqueTyID: return 'O';
90 // Try to find address of external function given a Function object.
91 // Please note, that interpreter doesn't know how to assemble a
92 // real call in general case (this is JIT job), that's why it assumes,
93 // that all external functions has the same (and pretty "general") signature.
94 // The typical example of such functions are "lle_X_" ones.
95 static ExFunc lookupFunction(const Function *F) {
96 // Function not found, look it up... start by figuring out what the
97 // composite function name should be.
98 std::string ExtName = "lle_";
99 const FunctionType *FT = F->getFunctionType();
100 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
101 ExtName += getTypeID(FT->getContainedType(i));
102 ExtName + "_" + F->getNameStr();
104 sys::ScopedLock Writer(*FunctionsLock);
105 ExFunc FnPtr = FuncNames[ExtName];
107 FnPtr = FuncNames["lle_X_" + F->getNameStr()];
108 if (FnPtr == 0) // Try calling a generic function... if it exists...
109 FnPtr = (ExFunc)(intptr_t)
110 sys::DynamicLibrary::SearchForAddressOfSymbol("lle_X_"+F->getNameStr());
112 ExportedFunctions->insert(std::make_pair(F, FnPtr)); // Cache for later
117 static ffi_type *ffiTypeFor(const Type *Ty) {
118 switch (Ty->getTypeID()) {
119 case Type::VoidTyID: return &ffi_type_void;
120 case Type::IntegerTyID:
121 switch (cast<IntegerType>(Ty)->getBitWidth()) {
122 case 8: return &ffi_type_sint8;
123 case 16: return &ffi_type_sint16;
124 case 32: return &ffi_type_sint32;
125 case 64: return &ffi_type_sint64;
127 case Type::FloatTyID: return &ffi_type_float;
128 case Type::DoubleTyID: return &ffi_type_double;
129 case Type::PointerTyID: return &ffi_type_pointer;
132 // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
133 llvm_report_error("Type could not be mapped for use with libffi.");
137 static void *ffiValueFor(const Type *Ty, const GenericValue &AV,
139 switch (Ty->getTypeID()) {
140 case Type::IntegerTyID:
141 switch (cast<IntegerType>(Ty)->getBitWidth()) {
143 int8_t *I8Ptr = (int8_t *) ArgDataPtr;
144 *I8Ptr = (int8_t) AV.IntVal.getZExtValue();
148 int16_t *I16Ptr = (int16_t *) ArgDataPtr;
149 *I16Ptr = (int16_t) AV.IntVal.getZExtValue();
153 int32_t *I32Ptr = (int32_t *) ArgDataPtr;
154 *I32Ptr = (int32_t) AV.IntVal.getZExtValue();
158 int64_t *I64Ptr = (int64_t *) ArgDataPtr;
159 *I64Ptr = (int64_t) AV.IntVal.getZExtValue();
163 case Type::FloatTyID: {
164 float *FloatPtr = (float *) ArgDataPtr;
165 *FloatPtr = AV.DoubleVal;
168 case Type::DoubleTyID: {
169 double *DoublePtr = (double *) ArgDataPtr;
170 *DoublePtr = AV.DoubleVal;
173 case Type::PointerTyID: {
174 void **PtrPtr = (void **) ArgDataPtr;
180 // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
181 llvm_report_error("Type value could not be mapped for use with libffi.");
185 static bool ffiInvoke(RawFunc Fn, Function *F,
186 const std::vector<GenericValue> &ArgVals,
187 const TargetData *TD, GenericValue &Result) {
189 const FunctionType *FTy = F->getFunctionType();
190 const unsigned NumArgs = F->arg_size();
192 // TODO: We don't have type information about the remaining arguments, because
193 // this information is never passed into ExecutionEngine::runFunction().
194 if (ArgVals.size() > NumArgs && F->isVarArg()) {
195 llvm_report_error("Calling external var arg function '" + F->getName()
196 + "' is not supported by the Interpreter.");
199 unsigned ArgBytes = 0;
201 std::vector<ffi_type*> args(NumArgs);
202 for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
204 const unsigned ArgNo = A->getArgNo();
205 const Type *ArgTy = FTy->getParamType(ArgNo);
206 args[ArgNo] = ffiTypeFor(ArgTy);
207 ArgBytes += TD->getTypeStoreSize(ArgTy);
210 uint8_t *ArgData = (uint8_t*) alloca(ArgBytes);
211 uint8_t *ArgDataPtr = ArgData;
212 std::vector<void*> values(NumArgs);
213 for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
215 const unsigned ArgNo = A->getArgNo();
216 const Type *ArgTy = FTy->getParamType(ArgNo);
217 values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
218 ArgDataPtr += TD->getTypeStoreSize(ArgTy);
221 const Type *RetTy = FTy->getReturnType();
222 ffi_type *rtype = ffiTypeFor(RetTy);
224 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) {
226 if (RetTy->getTypeID() != Type::VoidTyID)
227 ret = alloca(TD->getTypeStoreSize(RetTy));
228 ffi_call(&cif, Fn, ret, &values[0]);
229 switch (RetTy->getTypeID()) {
230 case Type::IntegerTyID:
231 switch (cast<IntegerType>(RetTy)->getBitWidth()) {
232 case 8: Result.IntVal = APInt(8 , *(int8_t *) ret); break;
233 case 16: Result.IntVal = APInt(16, *(int16_t*) ret); break;
234 case 32: Result.IntVal = APInt(32, *(int32_t*) ret); break;
235 case 64: Result.IntVal = APInt(64, *(int64_t*) ret); break;
238 case Type::FloatTyID: Result.FloatVal = *(float *) ret; break;
239 case Type::DoubleTyID: Result.DoubleVal = *(double*) ret; break;
240 case Type::PointerTyID: Result.PointerVal = *(void **) ret; break;
250 GenericValue Interpreter::callExternalFunction(Function *F,
251 const std::vector<GenericValue> &ArgVals) {
252 TheInterpreter = this;
254 FunctionsLock->acquire();
256 // Do a lookup to see if the function is in our cache... this should just be a
257 // deferred annotation!
258 std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F);
259 if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F)
261 FunctionsLock->release();
262 return Fn(F->getFunctionType(), ArgVals);
266 std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
268 if (RF == RawFunctions->end()) {
269 RawFn = (RawFunc)(intptr_t)
270 sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
272 RawFunctions->insert(std::make_pair(F, RawFn)); // Cache for later
277 FunctionsLock->release();
280 if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getTargetData(), Result))
284 if (F->getName() == "__main")
285 errs() << "Tried to execute an unknown external function: "
286 << F->getType()->getDescription() << " __main\n";
288 llvm_report_error("Tried to execute an unknown external function: " +
289 F->getType()->getDescription() + " " +F->getName());
290 return GenericValue();
294 //===----------------------------------------------------------------------===//
295 // Functions "exported" to the running application...
298 // Visual Studio warns about returning GenericValue in extern "C" linkage
300 #pragma warning(disable : 4190)
303 extern "C" { // Don't add C++ manglings to llvm mangling :)
305 // void atexit(Function*)
306 GenericValue lle_X_atexit(const FunctionType *FT,
307 const std::vector<GenericValue> &Args) {
308 assert(Args.size() == 1);
309 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
316 GenericValue lle_X_exit(const FunctionType *FT,
317 const std::vector<GenericValue> &Args) {
318 TheInterpreter->exitCalled(Args[0]);
319 return GenericValue();
323 GenericValue lle_X_abort(const FunctionType *FT,
324 const std::vector<GenericValue> &Args) {
325 //FIXME: should we report or raise here?
326 //llvm_report_error("Interpreted program raised SIGABRT");
328 return GenericValue();
331 // int sprintf(char *, const char *, ...) - a very rough implementation to make
333 GenericValue lle_X_sprintf(const FunctionType *FT,
334 const std::vector<GenericValue> &Args) {
335 char *OutputBuffer = (char *)GVTOP(Args[0]);
336 const char *FmtStr = (const char *)GVTOP(Args[1]);
339 // printf should return # chars printed. This is completely incorrect, but
340 // close enough for now.
342 GV.IntVal = APInt(32, strlen(FmtStr));
345 case 0: return GV; // Null terminator...
346 default: // Normal nonspecial character
347 sprintf(OutputBuffer++, "%c", *FmtStr++);
349 case '\\': { // Handle escape codes
350 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
351 FmtStr += 2; OutputBuffer += 2;
354 case '%': { // Handle format specifiers
355 char FmtBuf[100] = "", Buffer[1000] = "";
358 char Last = *FB++ = *FmtStr++;
359 unsigned HowLong = 0;
360 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
361 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
362 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
363 Last != 'p' && Last != 's' && Last != '%') {
364 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
365 Last = *FB++ = *FmtStr++;
371 strcpy(Buffer, "%"); break;
373 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
380 TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
381 sizeof(long) < sizeof(int64_t)) {
382 // Make sure we use %lld with a 64 bit argument because we might be
383 // compiling LLI on a 32 bit compiler.
384 unsigned Size = strlen(FmtBuf);
385 FmtBuf[Size] = FmtBuf[Size-1];
387 FmtBuf[Size-1] = 'l';
389 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
391 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
393 case 'e': case 'E': case 'g': case 'G': case 'f':
394 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
396 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
398 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
400 errs() << "<unknown printf code '" << *FmtStr << "'!>";
403 strcpy(OutputBuffer, Buffer);
404 OutputBuffer += strlen(Buffer);
412 // int printf(const char *, ...) - a very rough implementation to make output
414 GenericValue lle_X_printf(const FunctionType *FT,
415 const std::vector<GenericValue> &Args) {
417 std::vector<GenericValue> NewArgs;
418 NewArgs.push_back(PTOGV((void*)&Buffer[0]));
419 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
420 GenericValue GV = lle_X_sprintf(FT, NewArgs);
425 static void ByteswapSCANFResults(LLVMContext &C,
426 const char *Fmt, void *Arg0, void *Arg1,
427 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
428 void *Arg6, void *Arg7, void *Arg8) {
429 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
431 // Loop over the format string, munging read values as appropriate (performs
432 // byteswaps as necessary).
436 // Read any flag characters that may be present...
437 bool Suppress = false;
440 bool LongLong = false; // long long or long double
444 case '*': Suppress = true; break;
445 case 'a': /*Allocate = true;*/ break; // We don't need to track this
446 case 'h': Half = true; break;
447 case 'l': Long = true; break;
449 case 'L': LongLong = true; break;
451 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
457 // Read the conversion character
458 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
463 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
465 if (Long || LongLong) {
466 Size = 8; Ty = Type::getInt64Ty(C);
468 Size = 4; Ty = Type::getInt16Ty(C);
470 Size = 4; Ty = Type::getInt32Ty(C);
474 case 'e': case 'g': case 'E':
476 if (Long || LongLong) {
477 Size = 8; Ty = Type::getDoubleTy(C);
479 Size = 4; Ty = Type::getFloatTy(C);
483 case 's': case 'c': case '[': // No byteswap needed
485 Ty = Type::getInt8Ty(C);
493 void *Arg = Args[ArgNo++];
494 memcpy(&GV, Arg, Size);
495 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
502 // int sscanf(const char *format, ...);
503 GenericValue lle_X_sscanf(const FunctionType *FT,
504 const std::vector<GenericValue> &args) {
505 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
508 for (unsigned i = 0; i < args.size(); ++i)
509 Args[i] = (char*)GVTOP(args[i]);
512 GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
513 Args[5], Args[6], Args[7], Args[8], Args[9]));
514 ByteswapSCANFResults(FT->getContext(),
515 Args[1], Args[2], Args[3], Args[4],
516 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
520 // int scanf(const char *format, ...);
521 GenericValue lle_X_scanf(const FunctionType *FT,
522 const std::vector<GenericValue> &args) {
523 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
526 for (unsigned i = 0; i < args.size(); ++i)
527 Args[i] = (char*)GVTOP(args[i]);
530 GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
531 Args[5], Args[6], Args[7], Args[8], Args[9]));
532 ByteswapSCANFResults(FT->getContext(),
533 Args[0], Args[1], Args[2], Args[3], Args[4],
534 Args[5], Args[6], Args[7], Args[8], Args[9]);
538 // int fprintf(FILE *, const char *, ...) - a very rough implementation to make
540 GenericValue lle_X_fprintf(const FunctionType *FT,
541 const std::vector<GenericValue> &Args) {
542 assert(Args.size() >= 2);
544 std::vector<GenericValue> NewArgs;
545 NewArgs.push_back(PTOGV(Buffer));
546 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
547 GenericValue GV = lle_X_sprintf(FT, NewArgs);
549 fputs(Buffer, (FILE *) GVTOP(Args[0]));
555 // Done with externals; turn the warning back on
557 #pragma warning(default: 4190)
561 void Interpreter::initializeExternalFunctions() {
562 sys::ScopedLock Writer(*FunctionsLock);
563 FuncNames["lle_X_atexit"] = lle_X_atexit;
564 FuncNames["lle_X_exit"] = lle_X_exit;
565 FuncNames["lle_X_abort"] = lle_X_abort;
567 FuncNames["lle_X_printf"] = lle_X_printf;
568 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
569 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
570 FuncNames["lle_X_scanf"] = lle_X_scanf;
571 FuncNames["lle_X_fprintf"] = lle_X_fprintf;