25 #include "llvm/Config/config.h" 68 typedef void (*RawFunc)();
78 switch (cast<IntegerType>(Ty)->getBitWidth()) {
104 std::string ExtName =
"lle_";
109 ExtName += (
"_" + F->
getName()).str();
112 ExFunc FnPtr = (*FuncNames)[ExtName];
114 FnPtr = (*FuncNames)[(
"lle_X_" + F->
getName()).str()];
117 (
"lle_X_" + F->
getName()).str());
119 ExportedFunctions->insert(std::make_pair(F, FnPtr));
124 static ffi_type *ffiTypeFor(
Type *Ty) {
128 switch (cast<IntegerType>(Ty)->getBitWidth()) {
129 case 8:
return &ffi_type_sint8;
130 case 16:
return &ffi_type_sint16;
131 case 32:
return &ffi_type_sint32;
132 case 64:
return &ffi_type_sint64;
148 switch (cast<IntegerType>(Ty)->getBitWidth()) {
150 int8_t *I8Ptr = (int8_t *) ArgDataPtr;
155 int16_t *I16Ptr = (int16_t *) ArgDataPtr;
160 int32_t *I32Ptr = (int32_t *) ArgDataPtr;
165 int64_t *I64Ptr = (int64_t *) ArgDataPtr;
171 float *FloatPtr = (
float *) ArgDataPtr;
176 double *DoublePtr = (
double *) ArgDataPtr;
181 void **PtrPtr = (
void **) ArgDataPtr;
196 const unsigned NumArgs = F->
arg_size();
202 +
"' is not supported by the Interpreter.");
205 unsigned ArgBytes = 0;
207 std::vector<ffi_type*> args(NumArgs);
210 const unsigned ArgNo = A->getArgNo();
212 args[ArgNo] = ffiTypeFor(ArgTy);
218 uint8_t *ArgDataPtr = ArgData.
data();
222 const unsigned ArgNo = A->getArgNo();
224 values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
229 ffi_type *rtype = ffiTypeFor(RetTy);
231 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, args.data()) ==
236 ffi_call(&cif, Fn, ret.
data(), values.
data());
239 switch (cast<IntegerType>(RetTy)->getBitWidth()) {
260 TheInterpreter =
this;
266 std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F);
274 std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
276 if (RF == RawFunctions->end()) {
282 RawFunctions->insert(std::make_pair(F, RawFn));
290 if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals,
getDataLayout(), Result))
295 errs() <<
"Tried to execute an unknown external function: " 296 << *F->
getType() <<
" __main\n";
301 errs() <<
"Recompiling LLVM with --enable-libffi might help.\n";
338 char *OutputBuffer = (
char *)
GVTOP(Args[0]);
339 const char *FmtStr = (
const char *)
GVTOP(Args[1]);
350 sprintf(OutputBuffer++,
"%c", *FmtStr++);
353 sprintf(OutputBuffer,
"%c%c", *FmtStr, *(FmtStr+1));
354 FmtStr += 2; OutputBuffer += 2;
358 char FmtBuf[100] =
"", Buffer[1000] =
"";
361 char Last = *FB++ = *FmtStr++;
362 unsigned HowLong = 0;
363 while (Last !=
'c' && Last !=
'd' && Last !=
'i' && Last !=
'u' &&
364 Last !=
'o' && Last !=
'x' && Last !=
'X' && Last !=
'e' &&
365 Last !=
'E' && Last !=
'g' && Last !=
'G' && Last !=
'f' &&
366 Last !=
'p' && Last !=
's' && Last !=
'%') {
367 if (Last ==
'l' || Last ==
'L') HowLong++;
368 Last = *FB++ = *FmtStr++;
374 memcpy(Buffer,
"%", 2);
break;
376 sprintf(Buffer, FmtBuf,
uint32_t(Args[ArgNo++].
IntVal.getZExtValue()));
384 sizeof(long) <
sizeof(int64_t)) {
387 unsigned Size = strlen(FmtBuf);
388 FmtBuf[
Size] = FmtBuf[Size-1];
390 FmtBuf[Size-1] =
'l';
392 sprintf(Buffer, FmtBuf, Args[ArgNo++].
IntVal.getZExtValue());
394 sprintf(Buffer, FmtBuf,
uint32_t(Args[ArgNo++].
IntVal.getZExtValue()));
396 case 'e':
case 'E':
case 'g':
case 'G':
case 'f':
397 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal);
break;
399 sprintf(Buffer, FmtBuf, (
void*)
GVTOP(Args[ArgNo++]));
break;
401 sprintf(Buffer, FmtBuf, (
char*)
GVTOP(Args[ArgNo++]));
break;
403 errs() <<
"<unknown printf code '" << *FmtStr <<
"'!>";
406 size_t Len = strlen(Buffer);
407 memcpy(OutputBuffer, Buffer, Len + 1);
421 std::vector<GenericValue> NewArgs;
422 NewArgs.push_back(
PTOGV((
void*)&Buffer[0]));
423 NewArgs.insert(NewArgs.end(), Args.
begin(), Args.
end());
432 assert(args.
size() < 10 &&
"Only handle up to 10 args to sscanf right now!");
435 for (
unsigned i = 0; i < args.
size(); ++i)
436 Args[i] = (
char*)
GVTOP(args[i]);
439 GV.
IntVal =
APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
440 Args[5], Args[6], Args[7], Args[8], Args[9]));
446 assert(args.
size() < 10 &&
"Only handle up to 10 args to scanf right now!");
449 for (
unsigned i = 0; i < args.
size(); ++i)
450 Args[i] = (
char*)
GVTOP(args[i]);
453 GV.
IntVal =
APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
454 Args[5], Args[6], Args[7], Args[8], Args[9]));
464 std::vector<GenericValue> NewArgs;
465 NewArgs.push_back(
PTOGV(Buffer));
466 NewArgs.insert(NewArgs.end(), Args.
begin()+1, Args.
end());
469 fputs(Buffer, (FILE *)
GVTOP(Args[0]));
475 int val = (int)Args[1].
IntVal.getSExtValue();
476 size_t len = (size_t)Args[2].
IntVal.getZExtValue();
488 (
size_t)(Args[2].
IntVal.getLimitedValue()));
497 void Interpreter::initializeExternalFunctions() {
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
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...
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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)
2: 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.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
11: Arbitrary bit width integers
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
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
A pared-down imitation of std::unique_lock from C++11.
void exitCalled(GenericValue GV)
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)
FunctionType * getFunctionType() const
Returns the FunctionType for me.
GenericValue PTOGV(void *P)
Class for arbitrary precision integers.
static char getTypeID(Type *Ty)
pointer data()
Return a pointer to the vector's buffer, even if empty().
StringRef getName() const
Return a constant reference to the value's name.
3: 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)
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
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.
PointerType * getType() const
Global values are always pointers.