24 #include "llvm/Config/config.h" 67 typedef void (*RawFunc)();
77 switch (cast<IntegerType>(Ty)->getBitWidth()) {
103 std::string ExtName =
"lle_";
108 ExtName += (
"_" +
F->getName()).str();
111 ExFunc FnPtr = (*FuncNames)[ExtName];
113 FnPtr = (*FuncNames)[(
"lle_X_" +
F->getName()).str()];
116 (
"lle_X_" +
F->getName()).str());
123 static ffi_type *ffiTypeFor(
Type *Ty) {
127 switch (cast<IntegerType>(Ty)->getBitWidth()) {
128 case 8:
return &ffi_type_sint8;
129 case 16:
return &ffi_type_sint16;
130 case 32:
return &ffi_type_sint32;
131 case 64:
return &ffi_type_sint64;
147 switch (cast<IntegerType>(Ty)->getBitWidth()) {
149 int8_t *I8Ptr = (int8_t *) ArgDataPtr;
154 int16_t *I16Ptr = (int16_t *) ArgDataPtr;
159 int32_t *I32Ptr = (int32_t *) ArgDataPtr;
164 int64_t *I64Ptr = (int64_t *) ArgDataPtr;
170 float *FloatPtr = (
float *) ArgDataPtr;
175 double *DoublePtr = (
double *) ArgDataPtr;
180 void **PtrPtr = (
void **) ArgDataPtr;
195 const unsigned NumArgs =
F->arg_size();
199 if (ArgVals.
size() > NumArgs &&
F->isVarArg()) {
201 +
"' is not supported by the Interpreter.");
204 unsigned ArgBytes = 0;
206 std::vector<ffi_type*> args(NumArgs);
209 const unsigned ArgNo =
A->getArgNo();
211 args[ArgNo] = ffiTypeFor(ArgTy);
217 uint8_t *ArgDataPtr = ArgData.
data();
221 const unsigned ArgNo =
A->getArgNo();
223 values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
228 ffi_type *rtype = ffiTypeFor(RetTy);
230 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, args.data()) ==
235 ffi_call(&cif, Fn, ret.
data(),
values.data());
238 switch (cast<IntegerType>(RetTy)->getBitWidth()) {
269 return Fn(
F->getFunctionType(), ArgVals);
273 std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(
F);
275 if (RF == RawFunctions->end()) {
281 RawFunctions->insert(std::make_pair(
F, RawFn));
289 if (RawFn != 0 && ffiInvoke(RawFn,
F, ArgVals,
getDataLayout(), Result))
293 if (
F->getName() ==
"__main")
294 errs() <<
"Tried to execute an unknown external function: " 295 << *
F->getType() <<
" __main\n";
300 errs() <<
"Recompiling LLVM with --enable-libffi might help.\n";
337 char *OutputBuffer = (
char *)
GVTOP(
Args[0]);
338 const char *FmtStr = (
const char *)
GVTOP(
Args[1]);
349 sprintf(OutputBuffer++,
"%c", *FmtStr++);
352 sprintf(OutputBuffer,
"%c%c", *FmtStr, *(FmtStr+1));
353 FmtStr += 2; OutputBuffer += 2;
357 char FmtBuf[100] =
"", Buffer[1000] =
"";
360 char Last = *FB++ = *FmtStr++;
361 unsigned HowLong = 0;
362 while (Last !=
'c' && Last !=
'd' && Last !=
'i' && Last !=
'u' &&
363 Last !=
'o' && Last !=
'x' && Last !=
'X' && Last !=
'e' &&
364 Last !=
'E' && Last !=
'g' && Last !=
'G' && Last !=
'f' &&
365 Last !=
'p' && Last !=
's' && Last !=
'%') {
366 if (Last ==
'l' || Last ==
'L') HowLong++;
367 Last = *FB++ = *FmtStr++;
373 memcpy(Buffer,
"%", 2);
break;
383 sizeof(long) <
sizeof(int64_t)) {
386 unsigned Size = strlen(FmtBuf);
389 FmtBuf[
Size-1] =
'l';
391 sprintf(Buffer, FmtBuf,
Args[ArgNo++].
IntVal.getZExtValue());
395 case 'e':
case 'E':
case 'g':
case 'G':
case 'f':
396 sprintf(Buffer, FmtBuf,
Args[ArgNo++].DoubleVal);
break;
398 sprintf(Buffer, FmtBuf, (
void*)
GVTOP(
Args[ArgNo++]));
break;
400 sprintf(Buffer, FmtBuf, (
char*)
GVTOP(
Args[ArgNo++]));
break;
402 errs() <<
"<unknown printf code '" << *FmtStr <<
"'!>";
405 size_t Len = strlen(Buffer);
406 memcpy(OutputBuffer, Buffer, Len + 1);
420 std::vector<GenericValue> NewArgs;
421 NewArgs.push_back(
PTOGV((
void*)&Buffer[0]));
431 assert(args.
size() < 10 &&
"Only handle up to 10 args to sscanf right now!");
434 for (
unsigned i = 0; i < args.
size(); ++i)
445 assert(args.
size() < 10 &&
"Only handle up to 10 args to scanf right now!");
448 for (
unsigned i = 0; i < args.
size(); ++i)
463 std::vector<GenericValue> NewArgs;
464 NewArgs.push_back(
PTOGV(Buffer));
465 NewArgs.insert(NewArgs.end(),
Args.begin()+1,
Args.end());
475 size_t len = (size_t)
Args[2].
IntVal.getZExtValue();
476 memset((
void *)
GVTOP(
Args[0]), val, len);
496 void Interpreter::initializeExternalFunctions() {
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
A parsed version of the target data layout string in and methods for querying it.
static void * SearchForAddressOfSymbol(const char *symbolName)
This function will search through all previously loaded dynamic libraries for the symbol symbolName.
uint64_t getZExtValue() const
Get zero extended value.
This class represents an incoming formal argument to a Function.
static Interpreter * TheInterpreter
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
Type * getParamType(unsigned i) const
Parameter type accessors.
static GenericValue lle_X_atexit(FunctionType *FT, ArrayRef< GenericValue > Args)
32-bit floating point type
void * getPointerToGlobalIfAvailable(StringRef S)
getPointerToGlobalIfAvailable - This returns the address of the specified global value if it is has a...
static GenericValue lle_X_exit(FunctionType *FT, ArrayRef< GenericValue > Args)
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
const DataLayout & getDataLayout() const
static GenericValue lle_X_sprintf(FunctionType *FT, ArrayRef< GenericValue > Args)
static ExFunc lookupFunction(const Function *F)
TypeID getTypeID() const
Return the type id for the type.
static GenericValue lle_X_sscanf(FunctionType *FT, ArrayRef< GenericValue > args)
This file implements a class to represent arbitrary precision integral constant values and operations...
Class to represent function types.
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Arbitrary bit width integers.
static ManagedStatic< sys::Mutex > FunctionsLock
static GenericValue lle_X_memcpy(FunctionType *FT, ArrayRef< GenericValue > Args)
The instances of the Type class are immutable: once they are created, they are never changed.
static ManagedStatic< std::map< const Function *, ExFunc > > ExportedFunctions
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
static ManagedStatic< std::map< std::string, ExFunc > > FuncNames
GenericValue(* ExFunc)(FunctionType *, ArrayRef< GenericValue >)
ArrayRef< Type * > params() const
void exitCalled(GenericValue GV)
SmartScopedLock< false > ScopedLock
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static GenericValue lle_X_abort(FunctionType *FT, ArrayRef< GenericValue > Args)
void * GVTOP(const GenericValue &GV)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Type * getReturnType() const
static GenericValue lle_X_fprintf(FunctionType *FT, ArrayRef< GenericValue > Args)
GenericValue PTOGV(void *P)
Class for arbitrary precision integers.
static char getTypeID(Type *Ty)
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
pointer data()
Return a pointer to the vector's buffer, even if empty().
64-bit floating point type
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static GenericValue lle_X_memset(FunctionType *FT, ArrayRef< GenericValue > Args)
static GenericValue lle_X_printf(FunctionType *FT, ArrayRef< GenericValue > Args)
void addAtExitHandler(Function *F)
GenericValue callExternalFunction(Function *F, ArrayRef< GenericValue > ArgVals)
static GenericValue lle_X_scanf(FunctionType *FT, ArrayRef< GenericValue > args)
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
constexpr char Args[]
Key for Kernel::Metadata::mArgs.