24#include "llvm/Config/config.h"
60typedef void (*RawFunc)();
64 std::map<const Function *, ExFunc> ExportedFunctions;
65 std::map<std::string, ExFunc> FuncNames;
67 std::map<const Function *, RawFunc> RawFunctions;
71Functions &getFunctions() {
81 switch (Ty->getTypeID()) {
110 std::string ExtName =
"lle_";
112 ExtName +=
getTypeID(FT->getReturnType());
113 for (
Type *
T : FT->params())
115 ExtName += (
"_" +
F->getName()).str();
117 auto &Fns = getFunctions();
119 ExFunc FnPtr = Fns.FuncNames[ExtName];
121 FnPtr = Fns.FuncNames[(
"lle_X_" +
F->getName()).str()];
124 (
"lle_X_" +
F->getName()).str());
126 Fns.ExportedFunctions.insert(std::make_pair(
F, FnPtr));
131static ffi_type *ffiTypeFor(
Type *Ty) {
132 switch (Ty->getTypeID()) {
136 case 8:
return &ffi_type_sint8;
137 case 16:
return &ffi_type_sint16;
138 case 32:
return &ffi_type_sint32;
139 case 64:
return &ffi_type_sint64;
154 switch (Ty->getTypeID()) {
158 int8_t *I8Ptr = (int8_t *) ArgDataPtr;
163 int16_t *I16Ptr = (int16_t *) ArgDataPtr;
168 int32_t *I32Ptr = (int32_t *) ArgDataPtr;
173 int64_t *I64Ptr = (int64_t *) ArgDataPtr;
180 float *FloatPtr = (
float *) ArgDataPtr;
185 double *DoublePtr = (
double *) ArgDataPtr;
190 void **PtrPtr = (
void **) ArgDataPtr;
205 const unsigned NumArgs =
F->arg_size();
209 if (ArgVals.
size() > NumArgs &&
F->isVarArg()) {
211 +
"' is not supported by the Interpreter.");
214 unsigned ArgBytes = 0;
216 std::vector<ffi_type*>
args(NumArgs);
219 const unsigned ArgNo =
A->getArgNo();
220 Type *ArgTy = FTy->getParamType(ArgNo);
221 args[ArgNo] = ffiTypeFor(ArgTy);
231 const unsigned ArgNo =
A->getArgNo();
232 Type *ArgTy = FTy->getParamType(ArgNo);
233 values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
237 Type *RetTy = FTy->getReturnType();
238 ffi_type *rtype = ffiTypeFor(RetTy);
240 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype,
args.data()) ==
245 ffi_call(&cif, Fn, ret.
data(),
values.data());
271 auto &Fns = getFunctions();
272 std::unique_lock<sys::Mutex> Guard(Fns.Lock);
276 std::map<const Function *, ExFunc>::iterator FI =
277 Fns.ExportedFunctions.find(
F);
278 if (ExFunc Fn = (FI == Fns.ExportedFunctions.end()) ?
lookupFunction(
F)
281 return Fn(
F->getFunctionType(), ArgVals);
285 std::map<const Function *, RawFunc>::iterator RF = Fns.RawFunctions.find(
F);
287 if (RF == Fns.RawFunctions.end()) {
288 RawFn = (RawFunc)(intptr_t)
293 Fns.RawFunctions.insert(std::make_pair(
F, RawFn));
301 if (RawFn != 0 && ffiInvoke(RawFn,
F, ArgVals,
getDataLayout(), Result))
305 if (
F->getName() ==
"__main")
306 errs() <<
"Tried to execute an unknown external function: "
307 << *
F->getType() <<
" __main\n";
312 errs() <<
"Recompiling LLVM with --enable-libffi might help.\n";
347#if defined(__clang__)
348#pragma clang diagnostic push
349#pragma clang diagnostic ignored "-Wdeprecated-declarations"
356 const char *FmtStr = (
const char *)
GVTOP(Args[1]);
375 char FmtBuf[100] =
"", Buffer[1000] =
"";
378 char Last = *FB++ = *FmtStr++;
379 unsigned HowLong = 0;
384 if (
Last ==
'l' ||
Last ==
'L') HowLong++;
385 Last = *FB++ = *FmtStr++;
391 memcpy(Buffer,
"%", 2);
break;
393 sprintf(Buffer, FmtBuf,
uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
401 sizeof(
long) <
sizeof(int64_t)) {
404 unsigned Size = strlen(FmtBuf);
407 FmtBuf[
Size-1] =
'l';
409 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
411 sprintf(Buffer, FmtBuf,
uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
413 case 'e':
case 'E':
case 'g':
case 'G':
case 'f':
414 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal);
break;
416 sprintf(Buffer, FmtBuf, (
void*)
GVTOP(Args[ArgNo++]));
break;
418 sprintf(Buffer, FmtBuf, (
char*)
GVTOP(Args[ArgNo++]));
break;
420 errs() <<
"<unknown printf code '" << *FmtStr <<
"'!>";
423 size_t Len = strlen(Buffer);
432#if defined(__clang__)
433#pragma clang diagnostic pop
441 std::vector<GenericValue> NewArgs;
442 NewArgs.push_back(
PTOGV((
void*)&Buffer[0]));
452 assert(
args.size() < 10 &&
"Only handle up to 10 args to sscanf right now!");
455 for (
unsigned i = 0; i <
args.size(); ++i)
459 GV.
IntVal =
APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
460 Args[5], Args[6], Args[7], Args[8], Args[9]));
466 assert(
args.size() < 10 &&
"Only handle up to 10 args to scanf right now!");
469 for (
unsigned i = 0; i <
args.size(); ++i)
473 GV.
IntVal =
APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
474 Args[5], Args[6], Args[7], Args[8], Args[9]));
484 std::vector<GenericValue> NewArgs;
485 NewArgs.push_back(
PTOGV(Buffer));
489 fputs(Buffer, (FILE *)
GVTOP(Args[0]));
495 int val = (int)Args[1].IntVal.getSExtValue();
496 size_t len = (
size_t)Args[2].IntVal.getZExtValue();
497 memset((
void *)
GVTOP(Args[0]), val, len);
508 (
size_t)(Args[2].IntVal.getLimitedValue()));
517void Interpreter::initializeExternalFunctions() {
518 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.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
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.