23 #include "llvm/Config/config.h"
52 const std::vector<GenericValue> &);
57 typedef void (*RawFunc)();
67 switch (cast<IntegerType>(Ty)->getBitWidth()) {
93 std::string ExtName =
"lle_";
113 static ffi_type *ffiTypeFor(
Type *Ty) {
117 switch (cast<IntegerType>(Ty)->getBitWidth()) {
118 case 8:
return &ffi_type_sint8;
119 case 16:
return &ffi_type_sint16;
120 case 32:
return &ffi_type_sint32;
121 case 64:
return &ffi_type_sint64;
137 switch (cast<IntegerType>(Ty)->getBitWidth()) {
139 int8_t *I8Ptr = (int8_t *) ArgDataPtr;
144 int16_t *I16Ptr = (int16_t *) ArgDataPtr;
149 int32_t *I32Ptr = (int32_t *) ArgDataPtr;
154 int64_t *I64Ptr = (int64_t *) ArgDataPtr;
160 float *FloatPtr = (
float *) ArgDataPtr;
165 double *DoublePtr = (
double *) ArgDataPtr;
170 void **PtrPtr = (
void **) ArgDataPtr;
181 static bool ffiInvoke(RawFunc Fn,
Function *
F,
182 const std::vector<GenericValue> &ArgVals,
186 const unsigned NumArgs = F->
arg_size();
190 if (ArgVals.size() > NumArgs && F->
isVarArg()) {
192 +
"' is not supported by the Interpreter.");
195 unsigned ArgBytes = 0;
197 std::vector<ffi_type*> args(NumArgs);
200 const unsigned ArgNo =
A->getArgNo();
202 args[ArgNo] = ffiTypeFor(ArgTy);
208 uint8_t *ArgDataPtr = ArgData.
data();
212 const unsigned ArgNo =
A->getArgNo();
214 values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
219 ffi_type *rtype = ffiTypeFor(RetTy);
221 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) {
225 ffi_call(&cif, Fn, ret.
data(),
values.data());
228 switch (cast<IntegerType>(RetTy)->getBitWidth()) {
248 const std::vector<GenericValue> &ArgVals) {
263 std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
265 if (RF == RawFunctions->end()) {
271 RawFunctions->insert(std::make_pair(F, RawFn));
279 if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals,
getDataLayout(), Result))
284 errs() <<
"Tried to execute an unknown external function: "
285 << *F->
getType() <<
" __main\n";
290 errs() <<
"Recompiling LLVM with --enable-libffi might help.\n";
303 const std::vector<GenericValue> &Args) {
304 assert(Args.size() == 1);
314 const std::vector<GenericValue> &Args) {
322 const std::vector<GenericValue> &Args) {
333 const std::vector<GenericValue> &Args) {
335 const char *FmtStr = (
const char *)
GVTOP(Args[1]);
346 sprintf(OutputBuffer++,
"%c", *FmtStr++);
349 sprintf(OutputBuffer,
"%c%c", *FmtStr, *(FmtStr+1));
350 FmtStr += 2; OutputBuffer += 2;
354 char FmtBuf[100] =
"", Buffer[1000] =
"";
357 char Last = *FB++ = *FmtStr++;
358 unsigned HowLong = 0;
359 while (Last !=
'c' && Last !=
'd' && Last !=
'i' && Last !=
'u' &&
360 Last !=
'o' && Last !=
'x' && Last !=
'X' && Last !=
'e' &&
361 Last !=
'E' && Last !=
'g' && Last !=
'G' && Last !=
'f' &&
362 Last !=
'p' && Last !=
's' && Last !=
'%') {
363 if (Last ==
'l' || Last ==
'L') HowLong++;
364 Last = *FB++ = *FmtStr++;
370 memcpy(Buffer,
"%", 2);
break;
372 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].
IntVal.getZExtValue()));
380 sizeof(long) <
sizeof(int64_t)) {
383 unsigned Size =
strlen(FmtBuf);
384 FmtBuf[Size] = FmtBuf[Size-1];
386 FmtBuf[Size-1] =
'l';
388 sprintf(Buffer, FmtBuf, Args[ArgNo++].
IntVal.getZExtValue());
390 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].
IntVal.getZExtValue()));
392 case 'e':
case 'E':
case 'g':
case 'G':
case 'f':
393 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal);
break;
395 sprintf(Buffer, FmtBuf, (
void*)
GVTOP(Args[ArgNo++]));
break;
397 sprintf(Buffer, FmtBuf, (
char*)
GVTOP(Args[ArgNo++]));
break;
399 errs() <<
"<unknown printf code '" << *FmtStr <<
"'!>";
402 size_t Len =
strlen(Buffer);
403 memcpy(OutputBuffer, Buffer, Len + 1);
416 const std::vector<GenericValue> &Args) {
418 std::vector<GenericValue> NewArgs;
419 NewArgs.push_back(
PTOGV((
void*)&Buffer[0]));
420 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
429 const std::vector<GenericValue> &args) {
430 assert(args.size() < 10 &&
"Only handle up to 10 args to sscanf right now!");
433 for (
unsigned i = 0; i < args.size(); ++i)
434 Args[i] = (
char*)
GVTOP(args[i]);
438 Args[5], Args[6], Args[7], Args[8], Args[9]));
445 const std::vector<GenericValue> &args) {
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]);
454 Args[5], Args[6], Args[7], Args[8], Args[9]));
462 const std::vector<GenericValue> &Args) {
463 assert(Args.size() >= 2);
465 std::vector<GenericValue> NewArgs;
466 NewArgs.push_back(
PTOGV(Buffer));
467 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
475 const std::vector<GenericValue> &Args) {
476 int val = (int)Args[1].
IntVal.getSExtValue();
477 size_t len = (size_t)Args[2].
IntVal.getZExtValue();
487 const std::vector<GenericValue> &Args) {
489 (
size_t)(Args[2].
IntVal.getLimitedValue()));
498 void Interpreter::initializeExternalFunctions() {
int sscanf(const char *s, const char *format, ... );
static std::map< std::string, ExFunc > FuncNames
static void * SearchForAddressOfSymbol(const char *symbolName)
Search through libraries for address of a symbol.
int sprintf(char *str, const char *format, ...);
uint64_t getZExtValue() const
Get zero extended value.
static Interpreter * TheInterpreter
GenericValue callExternalFunction(Function *F, const std::vector< GenericValue > &ArgVals)
2: 32-bit floating point type
ValuesClass< DataType > END_WITH_NULL values(const char *Arg, DataType Val, const char *Desc,...)
std::string str() const
str - Get the contents as an std::string.
static GenericValue lle_X_printf(FunctionType *FT, const std::vector< GenericValue > &Args)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
static ExFunc lookupFunction(const Function *F)
StringRef getName() const
static GenericValue lle_X_sscanf(FunctionType *FT, const std::vector< GenericValue > &args)
GenericValue(* ExFunc)(FunctionType *, const std::vector< GenericValue > &)
static GenericValue lle_X_sprintf(FunctionType *FT, const std::vector< GenericValue > &Args)
10: Arbitrary bit width integers
static ManagedStatic< sys::Mutex > FunctionsLock
Type * getParamType(unsigned i) const
Parameter type accessors.
static GenericValue lle_X_abort(FunctionType *FT, const std::vector< GenericValue > &Args)
static ManagedStatic< std::map< const Function *, ExFunc > > ExportedFunctions
Type * getContainedType(unsigned i) const
void exitCalled(GenericValue GV)
static GenericValue lle_X_exit(FunctionType *FT, const std::vector< GenericValue > &Args)
static GenericValue lle_X_atexit(FunctionType *FT, const std::vector< GenericValue > &Args)
void * getPointerToGlobalIfAvailable(const GlobalValue *GV)
unsigned getNumContainedTypes() const
static GenericValue lle_X_memset(FunctionType *FT, const std::vector< GenericValue > &Args)
int fputs(const char *s, FILE *stream);
void * GVTOP(const GenericValue &GV)
size_t strlen(const char *s);
static GenericValue lle_X_scanf(FunctionType *FT, const std::vector< GenericValue > &args)
GenericValue PTOGV(void *P)
Class for arbitrary precision integers.
static char getTypeID(Type *Ty)
static GenericValue lle_X_memcpy(FunctionType *FT, const std::vector< GenericValue > &Args)
PointerType * getType() const
getType - Global values are always pointers.
pointer data()
data - Return a pointer to the vector's buffer, even if empty().
const DataLayout * getDataLayout() const
FunctionType * getFunctionType() const
unsigned getPointerSizeInBits(unsigned AS=0) const
static GenericValue lle_X_fprintf(FunctionType *FT, const std::vector< GenericValue > &Args)
uint64_t getTypeStoreSize(Type *Ty) const
3: 64-bit floating point type
Type * getReturnType() const
void addAtExitHandler(Function *F)
int scanf(const char *restrict format, ... );
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD