24#include "llvm/Config/config.h"
61typedef void (*RawFunc)();
65 std::map<const Function *, ExFunc> ExportedFunctions;
66 std::map<std::string, ExFunc> FuncNames;
68 std::map<const Function *, RawFunc> RawFunctions;
72Functions &getFunctions() {
82 switch (Ty->getTypeID()) {
111 std::string ExtName =
"lle_";
113 ExtName +=
getTypeID(FT->getReturnType());
114 for (
Type *
T : FT->params())
116 ExtName += (
"_" +
F->getName()).str();
118 auto &Fns = getFunctions();
120 ExFunc FnPtr = Fns.FuncNames[ExtName];
122 FnPtr = Fns.FuncNames[(
"lle_X_" +
F->getName()).str()];
125 (
"lle_X_" +
F->getName()).str());
127 Fns.ExportedFunctions.insert(std::make_pair(
F, FnPtr));
132static ffi_type *ffiTypeFor(
Type *Ty) {
133 switch (Ty->getTypeID()) {
137 case 8:
return &ffi_type_sint8;
138 case 16:
return &ffi_type_sint16;
139 case 32:
return &ffi_type_sint32;
140 case 64:
return &ffi_type_sint64;
155 switch (Ty->getTypeID()) {
159 int8_t *I8Ptr = (int8_t *) ArgDataPtr;
164 int16_t *I16Ptr = (int16_t *) ArgDataPtr;
169 int32_t *I32Ptr = (int32_t *) ArgDataPtr;
174 int64_t *I64Ptr = (int64_t *) ArgDataPtr;
181 float *FloatPtr = (
float *) ArgDataPtr;
186 double *DoublePtr = (
double *) ArgDataPtr;
191 void **PtrPtr = (
void **) ArgDataPtr;
206 const unsigned NumArgs =
F->arg_size();
210 if (ArgVals.
size() > NumArgs &&
F->isVarArg()) {
212 +
"' is not supported by the Interpreter.");
215 unsigned ArgBytes = 0;
217 std::vector<ffi_type*>
args(NumArgs);
220 const unsigned ArgNo =
A->getArgNo();
221 Type *ArgTy = FTy->getParamType(ArgNo);
222 args[ArgNo] = ffiTypeFor(ArgTy);
232 const unsigned ArgNo =
A->getArgNo();
233 Type *ArgTy = FTy->getParamType(ArgNo);
234 values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
238 Type *RetTy = FTy->getReturnType();
239 ffi_type *rtype = ffiTypeFor(RetTy);
241 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype,
args.data()) ==
246 ffi_call(&cif, Fn, ret.
data(),
values.data());
272 auto &Fns = getFunctions();
273 std::unique_lock<sys::Mutex> Guard(Fns.Lock);
277 std::map<const Function *, ExFunc>::iterator FI =
278 Fns.ExportedFunctions.find(
F);
279 if (ExFunc Fn = (FI == Fns.ExportedFunctions.end()) ?
lookupFunction(
F)
282 return Fn(
F->getFunctionType(), ArgVals);
286 std::map<const Function *, RawFunc>::iterator RF = Fns.RawFunctions.find(
F);
288 if (RF == Fns.RawFunctions.end()) {
289 RawFn = (RawFunc)(intptr_t)
294 Fns.RawFunctions.insert(std::make_pair(
F, RawFn));
302 if (RawFn != 0 && ffiInvoke(RawFn,
F, ArgVals,
getDataLayout(), Result))
306 if (
F->getName() ==
"__main")
307 errs() <<
"Tried to execute an unknown external function: "
308 << *
F->getType() <<
" __main\n";
313 errs() <<
"Recompiling LLVM with --enable-libffi might help.\n";
348#if defined(__clang__)
349#pragma clang diagnostic push
350#pragma clang diagnostic ignored "-Wdeprecated-declarations"
357 const char *FmtStr = (
const char *)
GVTOP(Args[1]);
376 char FmtBuf[100] =
"", Buffer[1000] =
"";
379 char Last = *FB++ = *FmtStr++;
380 unsigned HowLong = 0;
385 if (
Last ==
'l' ||
Last ==
'L') HowLong++;
386 Last = *FB++ = *FmtStr++;
392 memcpy(Buffer,
"%", 2);
break;
394 sprintf(Buffer, FmtBuf,
uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
402 sizeof(
long) <
sizeof(int64_t)) {
405 unsigned Size = strlen(FmtBuf);
408 FmtBuf[
Size-1] =
'l';
410 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
412 sprintf(Buffer, FmtBuf,
uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
414 case 'e':
case 'E':
case 'g':
case 'G':
case 'f':
415 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal);
break;
417 sprintf(Buffer, FmtBuf, (
void*)
GVTOP(Args[ArgNo++]));
break;
419 sprintf(Buffer, FmtBuf, (
char*)
GVTOP(Args[ArgNo++]));
break;
421 errs() <<
"<unknown printf code '" << *FmtStr <<
"'!>";
424 size_t Len = strlen(Buffer);
433#if defined(__clang__)
434#pragma clang diagnostic pop
442 std::vector<GenericValue> NewArgs;
443 NewArgs.push_back(
PTOGV((
void*)&Buffer[0]));
453 assert(
args.size() < 10 &&
"Only handle up to 10 args to sscanf right now!");
456 for (
unsigned i = 0; i <
args.size(); ++i)
460 GV.
IntVal =
APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
461 Args[5], Args[6], Args[7], Args[8], Args[9]));
467 assert(
args.size() < 10 &&
"Only handle up to 10 args to scanf right now!");
470 for (
unsigned i = 0; i <
args.size(); ++i)
474 GV.
IntVal =
APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
475 Args[5], Args[6], Args[7], Args[8], Args[9]));
485 std::vector<GenericValue> NewArgs;
486 NewArgs.push_back(
PTOGV(Buffer));
490 fputs(Buffer, (FILE *)
GVTOP(Args[0]));
496 int val = (int)Args[1].IntVal.getSExtValue();
497 size_t len = (size_t)Args[2].IntVal.getZExtValue();
498 memset((
void *)
GVTOP(Args[0]), val, len);
509 (
size_t)(Args[2].IntVal.getLimitedValue()));
518void Interpreter::initializeExternalFunctions() {
519 auto &Fns = getFunctions();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static ExFunc lookupFunction(const Function *F)
static Interpreter * TheInterpreter
static GenericValue lle_X_memset(FunctionType *FT, ArrayRef< GenericValue > Args)
static char getTypeID(Type *Ty)
static GenericValue lle_X_fprintf(FunctionType *FT, ArrayRef< GenericValue > Args)
static GenericValue lle_X_scanf(FunctionType *FT, ArrayRef< GenericValue > args)
static GenericValue lle_X_printf(FunctionType *FT, ArrayRef< GenericValue > Args)
static GenericValue lle_X_memcpy(FunctionType *FT, ArrayRef< GenericValue > Args)
static GenericValue lle_X_atexit(FunctionType *FT, ArrayRef< GenericValue > Args)
static GenericValue lle_X_sscanf(FunctionType *FT, ArrayRef< GenericValue > args)
static GenericValue lle_X_abort(FunctionType *FT, ArrayRef< GenericValue > Args)
static GenericValue lle_X_exit(FunctionType *FT, ArrayRef< GenericValue > Args)
static GenericValue lle_X_sprintf(FunctionType *FT, ArrayRef< GenericValue > Args)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Class for arbitrary precision integers.
uint64_t getZExtValue() const
Get zero extended value.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
A parsed version of the target data layout string in and methods for querying it.
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
const DataLayout & getDataLayout() const
void * getPointerToGlobalIfAvailable(StringRef S)
getPointerToGlobalIfAvailable - This returns the address of the specified global value if it is has a...
const Argument * const_arg_iterator
GenericValue callExternalFunction(Function *F, ArrayRef< GenericValue > ArgVals)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
@ VoidTyID
type with no size
@ FloatTyID
32-bit floating point type
@ IntegerTyID
Arbitrary bit width integers.
@ DoubleTyID
64-bit floating point type
TypeID getTypeID() const
Return the type id for the type.
static LLVM_ABI void * SearchForAddressOfSymbol(const char *symbolName)
This function will search through all previously loaded dynamic libraries for the symbol symbolName.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
SmartScopedLock< false > ScopedLock
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
GenericValue PTOGV(void *P)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void * GVTOP(const GenericValue &GV)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.