24#include "llvm/Config/config.h"
62typedef void (*RawFunc)();
66 std::map<const Function *, ExFunc> ExportedFunctions;
67 std::map<std::string, ExFunc> FuncNames;
69 std::map<const Function *, RawFunc> RawFunctions;
73Functions &getFunctions() {
112 std::string ExtName =
"lle_";
114 ExtName +=
getTypeID(FT->getReturnType());
115 for (
Type *
T : FT->params())
117 ExtName += (
"_" +
F->getName()).str();
119 auto &Fns = getFunctions();
121 ExFunc FnPtr = Fns.FuncNames[ExtName];
123 FnPtr = Fns.FuncNames[(
"lle_X_" +
F->getName()).str()];
126 (
"lle_X_" +
F->getName()).str());
128 Fns.ExportedFunctions.insert(std::make_pair(
F, FnPtr));
133static ffi_type *ffiTypeFor(
Type *Ty) {
138 case 8:
return &ffi_type_sint8;
139 case 16:
return &ffi_type_sint16;
140 case 32:
return &ffi_type_sint32;
141 case 64:
return &ffi_type_sint64;
160 int8_t *I8Ptr = (int8_t *) ArgDataPtr;
165 int16_t *I16Ptr = (int16_t *) ArgDataPtr;
170 int32_t *I32Ptr = (int32_t *) ArgDataPtr;
175 int64_t *I64Ptr = (int64_t *) ArgDataPtr;
182 float *FloatPtr = (
float *) ArgDataPtr;
187 double *DoublePtr = (
double *) ArgDataPtr;
192 void **PtrPtr = (
void **) ArgDataPtr;
207 const unsigned NumArgs =
F->arg_size();
211 if (ArgVals.
size() > NumArgs &&
F->isVarArg()) {
213 +
"' is not supported by the Interpreter.");
216 unsigned ArgBytes = 0;
218 std::vector<ffi_type*>
args(NumArgs);
221 const unsigned ArgNo =
A->getArgNo();
222 Type *ArgTy = FTy->getParamType(ArgNo);
223 args[ArgNo] = ffiTypeFor(ArgTy);
229 uint8_t *ArgDataPtr = ArgData.
data();
233 const unsigned ArgNo =
A->getArgNo();
234 Type *ArgTy = FTy->getParamType(ArgNo);
235 values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
240 ffi_type *rtype = ffiTypeFor(
RetTy);
242 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype,
args.data()) ==
247 ffi_call(&cif, Fn, ret.
data(),
values.data());
248 switch (
RetTy->getTypeID()) {
273 auto &Fns = getFunctions();
274 std::unique_lock<sys::Mutex> Guard(Fns.Lock);
278 std::map<const Function *, ExFunc>::iterator FI =
279 Fns.ExportedFunctions.find(
F);
280 if (ExFunc Fn = (FI == Fns.ExportedFunctions.end()) ?
lookupFunction(
F)
283 return Fn(
F->getFunctionType(), ArgVals);
287 std::map<const Function *, RawFunc>::iterator RF = Fns.RawFunctions.find(
F);
289 if (RF == Fns.RawFunctions.end()) {
290 RawFn = (RawFunc)(intptr_t)
295 Fns.RawFunctions.insert(std::make_pair(
F, RawFn));
303 if (RawFn != 0 && ffiInvoke(RawFn,
F, ArgVals,
getDataLayout(), Result))
307 if (
F->getName() ==
"__main")
308 errs() <<
"Tried to execute an unknown external function: "
309 << *
F->getType() <<
" __main\n";
314 errs() <<
"Recompiling LLVM with --enable-libffi might help.\n";
349#if defined(__clang__)
350#pragma clang diagnostic push
351#pragma clang diagnostic ignored "-Wdeprecated-declarations"
358 const char *FmtStr = (
const char *)
GVTOP(Args[1]);
377 char FmtBuf[100] =
"", Buffer[1000] =
"";
380 char Last = *FB++ = *FmtStr++;
381 unsigned HowLong = 0;
386 if (
Last ==
'l' ||
Last ==
'L') HowLong++;
387 Last = *FB++ = *FmtStr++;
393 memcpy(Buffer,
"%", 2);
break;
395 sprintf(Buffer, FmtBuf,
uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
403 sizeof(
long) <
sizeof(int64_t)) {
406 unsigned Size = strlen(FmtBuf);
409 FmtBuf[
Size-1] =
'l';
411 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
413 sprintf(Buffer, FmtBuf,
uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
415 case 'e':
case 'E':
case 'g':
case 'G':
case 'f':
416 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal);
break;
418 sprintf(Buffer, FmtBuf, (
void*)
GVTOP(Args[ArgNo++]));
break;
420 sprintf(Buffer, FmtBuf, (
char*)
GVTOP(Args[ArgNo++]));
break;
422 errs() <<
"<unknown printf code '" << *FmtStr <<
"'!>";
425 size_t Len = strlen(Buffer);
434#if defined(__clang__)
435#pragma clang diagnostic pop
443 std::vector<GenericValue> NewArgs;
444 NewArgs.push_back(
PTOGV((
void*)&Buffer[0]));
454 assert(
args.size() < 10 &&
"Only handle up to 10 args to sscanf right now!");
457 for (
unsigned i = 0; i <
args.size(); ++i)
461 GV.
IntVal =
APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
462 Args[5], Args[6], Args[7], Args[8], Args[9]));
468 assert(
args.size() < 10 &&
"Only handle up to 10 args to scanf right now!");
471 for (
unsigned i = 0; i <
args.size(); ++i)
475 GV.
IntVal =
APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
476 Args[5], Args[6], Args[7], Args[8], Args[9]));
486 std::vector<GenericValue> NewArgs;
487 NewArgs.push_back(
PTOGV(Buffer));
488 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
491 fputs(Buffer, (FILE *)
GVTOP(Args[0]));
497 int val = (int)Args[1].IntVal.getSExtValue();
498 size_t len = (size_t)Args[2].IntVal.getZExtValue();
499 memset((
void *)
GVTOP(Args[0]), val, len);
510 (
size_t)(Args[2].IntVal.getLimitedValue()));
519void Interpreter::initializeExternalFunctions() {
520 auto &Fns = getFunctions();
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Mark the given Function as meaning that it cannot be changed in any way mark any values that are used as this function s parameters or by its return values(according to Uses) live as well. void DeadArgumentEliminationPass
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
This class represents an incoming formal argument to a Function.
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.
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
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...
void addAtExitHandler(Function *F)
void exitCalled(GenericValue GV)
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 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.
SmartScopedLock< false > ScopedLock
This is an optimization pass for GlobalISel generic memory operations.
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)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void * GVTOP(const GenericValue &GV)