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/Streams.h"
27 #include "llvm/System/DynamicLibrary.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Support/ManagedStatic.h"
48 typedef GenericValue (*ExFunc
)(const FunctionType
*,
49 const std::vector
<GenericValue
> &);
50 static ManagedStatic
<std::map
<const Function
*, ExFunc
> > ExportedFunctions
;
51 static std::map
<std::string
, ExFunc
> FuncNames
;
54 typedef void (*RawFunc
)(void);
55 static ManagedStatic
<std::map
<const Function
*, RawFunc
> > RawFunctions
;
58 static Interpreter
*TheInterpreter
;
60 static char getTypeID(const Type
*Ty
) {
61 switch (Ty
->getTypeID()) {
62 case Type::VoidTyID
: return 'V';
63 case Type::IntegerTyID
:
64 switch (cast
<IntegerType
>(Ty
)->getBitWidth()) {
72 case Type::FloatTyID
: return 'F';
73 case Type::DoubleTyID
: return 'D';
74 case Type::PointerTyID
: return 'P';
75 case Type::FunctionTyID
:return 'M';
76 case Type::StructTyID
: return 'T';
77 case Type::ArrayTyID
: return 'A';
78 case Type::OpaqueTyID
: return 'O';
83 // Try to find address of external function given a Function object.
84 // Please note, that interpreter doesn't know how to assemble a
85 // real call in general case (this is JIT job), that's why it assumes,
86 // that all external functions has the same (and pretty "general") signature.
87 // The typical example of such functions are "lle_X_" ones.
88 static ExFunc
lookupFunction(const Function
*F
) {
89 // Function not found, look it up... start by figuring out what the
90 // composite function name should be.
91 std::string ExtName
= "lle_";
92 const FunctionType
*FT
= F
->getFunctionType();
93 for (unsigned i
= 0, e
= FT
->getNumContainedTypes(); i
!= e
; ++i
)
94 ExtName
+= getTypeID(FT
->getContainedType(i
));
95 ExtName
+= "_" + F
->getName();
97 ExFunc FnPtr
= FuncNames
[ExtName
];
99 FnPtr
= FuncNames
["lle_X_"+F
->getName()];
100 if (FnPtr
== 0) // Try calling a generic function... if it exists...
101 FnPtr
= (ExFunc
)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
102 ("lle_X_"+F
->getName()).c_str());
104 ExportedFunctions
->insert(std::make_pair(F
, FnPtr
)); // Cache for later
109 static ffi_type
*ffiTypeFor(const Type
*Ty
) {
110 switch (Ty
->getTypeID()) {
111 case Type::VoidTyID
: return &ffi_type_void
;
112 case Type::IntegerTyID
:
113 switch (cast
<IntegerType
>(Ty
)->getBitWidth()) {
114 case 8: return &ffi_type_sint8
;
115 case 16: return &ffi_type_sint16
;
116 case 32: return &ffi_type_sint32
;
117 case 64: return &ffi_type_sint64
;
119 case Type::FloatTyID
: return &ffi_type_float
;
120 case Type::DoubleTyID
: return &ffi_type_double
;
121 case Type::PointerTyID
: return &ffi_type_pointer
;
124 // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
125 cerr
<< "Type could not be mapped for use with libffi.\n";
130 static void *ffiValueFor(const Type
*Ty
, const GenericValue
&AV
,
132 switch (Ty
->getTypeID()) {
133 case Type::IntegerTyID
:
134 switch (cast
<IntegerType
>(Ty
)->getBitWidth()) {
136 int8_t *I8Ptr
= (int8_t *) ArgDataPtr
;
137 *I8Ptr
= (int8_t) AV
.IntVal
.getZExtValue();
141 int16_t *I16Ptr
= (int16_t *) ArgDataPtr
;
142 *I16Ptr
= (int16_t) AV
.IntVal
.getZExtValue();
146 int32_t *I32Ptr
= (int32_t *) ArgDataPtr
;
147 *I32Ptr
= (int32_t) AV
.IntVal
.getZExtValue();
151 int64_t *I64Ptr
= (int64_t *) ArgDataPtr
;
152 *I64Ptr
= (int64_t) AV
.IntVal
.getZExtValue();
156 case Type::FloatTyID
: {
157 float *FloatPtr
= (float *) ArgDataPtr
;
158 *FloatPtr
= AV
.DoubleVal
;
161 case Type::DoubleTyID
: {
162 double *DoublePtr
= (double *) ArgDataPtr
;
163 *DoublePtr
= AV
.DoubleVal
;
166 case Type::PointerTyID
: {
167 void **PtrPtr
= (void **) ArgDataPtr
;
173 // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
174 cerr
<< "Type value could not be mapped for use with libffi.\n";
179 static bool ffiInvoke(RawFunc Fn
, Function
*F
,
180 const std::vector
<GenericValue
> &ArgVals
,
181 const TargetData
*TD
, GenericValue
&Result
) {
183 const FunctionType
*FTy
= F
->getFunctionType();
184 const unsigned NumArgs
= F
->arg_size();
186 // TODO: We don't have type information about the remaining arguments, because
187 // this information is never passed into ExecutionEngine::runFunction().
188 if (ArgVals
.size() > NumArgs
&& F
->isVarArg()) {
189 cerr
<< "Calling external var arg function '" << F
->getName()
190 << "' is not supported by the Interpreter.\n";
194 unsigned ArgBytes
= 0;
196 std::vector
<ffi_type
*> args(NumArgs
);
197 for (Function::const_arg_iterator A
= F
->arg_begin(), E
= F
->arg_end();
199 const unsigned ArgNo
= A
->getArgNo();
200 const Type
*ArgTy
= FTy
->getParamType(ArgNo
);
201 args
[ArgNo
] = ffiTypeFor(ArgTy
);
202 ArgBytes
+= TD
->getTypeStoreSize(ArgTy
);
205 uint8_t *ArgData
= (uint8_t*) alloca(ArgBytes
);
206 uint8_t *ArgDataPtr
= ArgData
;
207 std::vector
<void*> values(NumArgs
);
208 for (Function::const_arg_iterator A
= F
->arg_begin(), E
= F
->arg_end();
210 const unsigned ArgNo
= A
->getArgNo();
211 const Type
*ArgTy
= FTy
->getParamType(ArgNo
);
212 values
[ArgNo
] = ffiValueFor(ArgTy
, ArgVals
[ArgNo
], ArgDataPtr
);
213 ArgDataPtr
+= TD
->getTypeStoreSize(ArgTy
);
216 const Type
*RetTy
= FTy
->getReturnType();
217 ffi_type
*rtype
= ffiTypeFor(RetTy
);
219 if (ffi_prep_cif(&cif
, FFI_DEFAULT_ABI
, NumArgs
, rtype
, &args
[0]) == FFI_OK
) {
221 if (RetTy
->getTypeID() != Type::VoidTyID
)
222 ret
= alloca(TD
->getTypeStoreSize(RetTy
));
223 ffi_call(&cif
, Fn
, ret
, &values
[0]);
224 switch (RetTy
->getTypeID()) {
225 case Type::IntegerTyID
:
226 switch (cast
<IntegerType
>(RetTy
)->getBitWidth()) {
227 case 8: Result
.IntVal
= APInt(8 , *(int8_t *) ret
); break;
228 case 16: Result
.IntVal
= APInt(16, *(int16_t*) ret
); break;
229 case 32: Result
.IntVal
= APInt(32, *(int32_t*) ret
); break;
230 case 64: Result
.IntVal
= APInt(64, *(int64_t*) ret
); break;
233 case Type::FloatTyID
: Result
.FloatVal
= *(float *) ret
; break;
234 case Type::DoubleTyID
: Result
.DoubleVal
= *(double*) ret
; break;
235 case Type::PointerTyID
: Result
.PointerVal
= *(void **) ret
; break;
245 GenericValue
Interpreter::callExternalFunction(Function
*F
,
246 const std::vector
<GenericValue
> &ArgVals
) {
247 TheInterpreter
= this;
249 // Do a lookup to see if the function is in our cache... this should just be a
250 // deferred annotation!
251 std::map
<const Function
*, ExFunc
>::iterator FI
= ExportedFunctions
->find(F
);
252 if (ExFunc Fn
= (FI
== ExportedFunctions
->end()) ? lookupFunction(F
)
254 return Fn(F
->getFunctionType(), ArgVals
);
257 std::map
<const Function
*, RawFunc
>::iterator RF
= RawFunctions
->find(F
);
259 if (RF
== RawFunctions
->end()) {
260 RawFn
= (RawFunc
)(intptr_t)
261 sys::DynamicLibrary::SearchForAddressOfSymbol(F
->getName());
263 RawFunctions
->insert(std::make_pair(F
, RawFn
)); // Cache for later
269 if (RawFn
!= 0 && ffiInvoke(RawFn
, F
, ArgVals
, getTargetData(), Result
))
273 cerr
<< "Tried to execute an unknown external function: "
274 << F
->getType()->getDescription() << " " << F
->getName() << "\n";
275 if (F
->getName() != "__main")
277 return GenericValue();
281 //===----------------------------------------------------------------------===//
282 // Functions "exported" to the running application...
284 extern "C" { // Don't add C++ manglings to llvm mangling :)
286 // void atexit(Function*)
287 GenericValue
lle_X_atexit(const FunctionType
*FT
,
288 const std::vector
<GenericValue
> &Args
) {
289 assert(Args
.size() == 1);
290 TheInterpreter
->addAtExitHandler((Function
*)GVTOP(Args
[0]));
297 GenericValue
lle_X_exit(const FunctionType
*FT
,
298 const std::vector
<GenericValue
> &Args
) {
299 TheInterpreter
->exitCalled(Args
[0]);
300 return GenericValue();
304 GenericValue
lle_X_abort(const FunctionType
*FT
,
305 const std::vector
<GenericValue
> &Args
) {
307 return GenericValue();
310 // int sprintf(char *, const char *, ...) - a very rough implementation to make
312 GenericValue
lle_X_sprintf(const FunctionType
*FT
,
313 const std::vector
<GenericValue
> &Args
) {
314 char *OutputBuffer
= (char *)GVTOP(Args
[0]);
315 const char *FmtStr
= (const char *)GVTOP(Args
[1]);
318 // printf should return # chars printed. This is completely incorrect, but
319 // close enough for now.
321 GV
.IntVal
= APInt(32, strlen(FmtStr
));
324 case 0: return GV
; // Null terminator...
325 default: // Normal nonspecial character
326 sprintf(OutputBuffer
++, "%c", *FmtStr
++);
328 case '\\': { // Handle escape codes
329 sprintf(OutputBuffer
, "%c%c", *FmtStr
, *(FmtStr
+1));
330 FmtStr
+= 2; OutputBuffer
+= 2;
333 case '%': { // Handle format specifiers
334 char FmtBuf
[100] = "", Buffer
[1000] = "";
337 char Last
= *FB
++ = *FmtStr
++;
338 unsigned HowLong
= 0;
339 while (Last
!= 'c' && Last
!= 'd' && Last
!= 'i' && Last
!= 'u' &&
340 Last
!= 'o' && Last
!= 'x' && Last
!= 'X' && Last
!= 'e' &&
341 Last
!= 'E' && Last
!= 'g' && Last
!= 'G' && Last
!= 'f' &&
342 Last
!= 'p' && Last
!= 's' && Last
!= '%') {
343 if (Last
== 'l' || Last
== 'L') HowLong
++; // Keep track of l's
344 Last
= *FB
++ = *FmtStr
++;
350 strcpy(Buffer
, "%"); break;
352 sprintf(Buffer
, FmtBuf
, uint32_t(Args
[ArgNo
++].IntVal
.getZExtValue()));
359 TheInterpreter
->getTargetData()->getPointerSizeInBits() == 64 &&
360 sizeof(long) < sizeof(int64_t)) {
361 // Make sure we use %lld with a 64 bit argument because we might be
362 // compiling LLI on a 32 bit compiler.
363 unsigned Size
= strlen(FmtBuf
);
364 FmtBuf
[Size
] = FmtBuf
[Size
-1];
366 FmtBuf
[Size
-1] = 'l';
368 sprintf(Buffer
, FmtBuf
, Args
[ArgNo
++].IntVal
.getZExtValue());
370 sprintf(Buffer
, FmtBuf
,uint32_t(Args
[ArgNo
++].IntVal
.getZExtValue()));
372 case 'e': case 'E': case 'g': case 'G': case 'f':
373 sprintf(Buffer
, FmtBuf
, Args
[ArgNo
++].DoubleVal
); break;
375 sprintf(Buffer
, FmtBuf
, (void*)GVTOP(Args
[ArgNo
++])); break;
377 sprintf(Buffer
, FmtBuf
, (char*)GVTOP(Args
[ArgNo
++])); break;
378 default: cerr
<< "<unknown printf code '" << *FmtStr
<< "'!>";
381 strcpy(OutputBuffer
, Buffer
);
382 OutputBuffer
+= strlen(Buffer
);
390 // int printf(const char *, ...) - a very rough implementation to make output
392 GenericValue
lle_X_printf(const FunctionType
*FT
,
393 const std::vector
<GenericValue
> &Args
) {
395 std::vector
<GenericValue
> NewArgs
;
396 NewArgs
.push_back(PTOGV((void*)&Buffer
[0]));
397 NewArgs
.insert(NewArgs
.end(), Args
.begin(), Args
.end());
398 GenericValue GV
= lle_X_sprintf(FT
, NewArgs
);
403 static void ByteswapSCANFResults(const char *Fmt
, void *Arg0
, void *Arg1
,
404 void *Arg2
, void *Arg3
, void *Arg4
, void *Arg5
,
405 void *Arg6
, void *Arg7
, void *Arg8
) {
406 void *Args
[] = { Arg0
, Arg1
, Arg2
, Arg3
, Arg4
, Arg5
, Arg6
, Arg7
, Arg8
, 0 };
408 // Loop over the format string, munging read values as appropriate (performs
409 // byteswaps as necessary).
413 // Read any flag characters that may be present...
414 bool Suppress
= false;
417 bool LongLong
= false; // long long or long double
421 case '*': Suppress
= true; break;
422 case 'a': /*Allocate = true;*/ break; // We don't need to track this
423 case 'h': Half
= true; break;
424 case 'l': Long
= true; break;
426 case 'L': LongLong
= true; break;
428 if (Fmt
[-1] > '9' || Fmt
[-1] < '0') // Ignore field width specs
434 // Read the conversion character
435 if (!Suppress
&& Fmt
[-1] != '%') { // Nothing to do?
440 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
442 if (Long
|| LongLong
) {
443 Size
= 8; Ty
= Type::Int64Ty
;
445 Size
= 4; Ty
= Type::Int16Ty
;
447 Size
= 4; Ty
= Type::Int32Ty
;
451 case 'e': case 'g': case 'E':
453 if (Long
|| LongLong
) {
454 Size
= 8; Ty
= Type::DoubleTy
;
456 Size
= 4; Ty
= Type::FloatTy
;
460 case 's': case 'c': case '[': // No byteswap needed
470 void *Arg
= Args
[ArgNo
++];
471 memcpy(&GV
, Arg
, Size
);
472 TheInterpreter
->StoreValueToMemory(GV
, (GenericValue
*)Arg
, Ty
);
479 // int sscanf(const char *format, ...);
480 GenericValue
lle_X_sscanf(const FunctionType
*FT
,
481 const std::vector
<GenericValue
> &args
) {
482 assert(args
.size() < 10 && "Only handle up to 10 args to sscanf right now!");
485 for (unsigned i
= 0; i
< args
.size(); ++i
)
486 Args
[i
] = (char*)GVTOP(args
[i
]);
489 GV
.IntVal
= APInt(32, sscanf(Args
[0], Args
[1], Args
[2], Args
[3], Args
[4],
490 Args
[5], Args
[6], Args
[7], Args
[8], Args
[9]));
491 ByteswapSCANFResults(Args
[1], Args
[2], Args
[3], Args
[4],
492 Args
[5], Args
[6], Args
[7], Args
[8], Args
[9], 0);
496 // int scanf(const char *format, ...);
497 GenericValue
lle_X_scanf(const FunctionType
*FT
,
498 const std::vector
<GenericValue
> &args
) {
499 assert(args
.size() < 10 && "Only handle up to 10 args to scanf right now!");
502 for (unsigned i
= 0; i
< args
.size(); ++i
)
503 Args
[i
] = (char*)GVTOP(args
[i
]);
506 GV
.IntVal
= APInt(32, scanf( Args
[0], Args
[1], Args
[2], Args
[3], Args
[4],
507 Args
[5], Args
[6], Args
[7], Args
[8], Args
[9]));
508 ByteswapSCANFResults(Args
[0], Args
[1], Args
[2], Args
[3], Args
[4],
509 Args
[5], Args
[6], Args
[7], Args
[8], Args
[9]);
513 // int fprintf(FILE *, const char *, ...) - a very rough implementation to make
515 GenericValue
lle_X_fprintf(const FunctionType
*FT
,
516 const std::vector
<GenericValue
> &Args
) {
517 assert(Args
.size() >= 2);
519 std::vector
<GenericValue
> NewArgs
;
520 NewArgs
.push_back(PTOGV(Buffer
));
521 NewArgs
.insert(NewArgs
.end(), Args
.begin()+1, Args
.end());
522 GenericValue GV
= lle_X_sprintf(FT
, NewArgs
);
524 fputs(Buffer
, (FILE *) GVTOP(Args
[0]));
531 void Interpreter::initializeExternalFunctions() {
532 FuncNames
["lle_X_atexit"] = lle_X_atexit
;
533 FuncNames
["lle_X_exit"] = lle_X_exit
;
534 FuncNames
["lle_X_abort"] = lle_X_abort
;
536 FuncNames
["lle_X_printf"] = lle_X_printf
;
537 FuncNames
["lle_X_sprintf"] = lle_X_sprintf
;
538 FuncNames
["lle_X_sscanf"] = lle_X_sscanf
;
539 FuncNames
["lle_X_scanf"] = lle_X_scanf
;
540 FuncNames
["lle_X_fprintf"] = lle_X_fprintf
;