23 #include "llvm/Config/config.h"
57 typedef void (*RawFunc)();
67 switch (cast<IntegerType>(Ty)->getBitWidth()) {
93 std::string ExtName =
"lle_";
97 ExtName += (
"_" + F->
getName()).str();
100 ExFunc FnPtr = (*FuncNames)[ExtName];
102 FnPtr = (*FuncNames)[(
"lle_X_" + F->
getName()).str()];
105 (
"lle_X_" + F->
getName()).str());
112 static ffi_type *ffiTypeFor(
Type *Ty) {
116 switch (cast<IntegerType>(Ty)->getBitWidth()) {
117 case 8:
return &ffi_type_sint8;
118 case 16:
return &ffi_type_sint16;
119 case 32:
return &ffi_type_sint32;
120 case 64:
return &ffi_type_sint64;
136 switch (cast<IntegerType>(Ty)->getBitWidth()) {
138 int8_t *I8Ptr = (int8_t *) ArgDataPtr;
143 int16_t *I16Ptr = (int16_t *) ArgDataPtr;
148 int32_t *I32Ptr = (int32_t *) ArgDataPtr;
153 int64_t *I64Ptr = (int64_t *) ArgDataPtr;
159 float *FloatPtr = (
float *) ArgDataPtr;
164 double *DoublePtr = (
double *) ArgDataPtr;
169 void **PtrPtr = (
void **) ArgDataPtr;
184 const unsigned NumArgs = F->
arg_size();
190 +
"' is not supported by the Interpreter.");
193 unsigned ArgBytes = 0;
195 std::vector<ffi_type*> args(NumArgs);
198 const unsigned ArgNo =
A->getArgNo();
200 args[ArgNo] = ffiTypeFor(ArgTy);
206 uint8_t *ArgDataPtr = ArgData.
data();
210 const unsigned ArgNo =
A->getArgNo();
212 values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
217 ffi_type *rtype = ffiTypeFor(RetTy);
219 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) {
223 ffi_call(&cif, Fn, ret.
data(),
values.data());
226 switch (cast<IntegerType>(RetTy)->getBitWidth()) {
261 std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
263 if (RF == RawFunctions->end()) {
269 RawFunctions->insert(std::make_pair(F, RawFn));
277 if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals,
getDataLayout(), Result))
282 errs() <<
"Tried to execute an unknown external function: "
283 << *F->
getType() <<
" __main\n";
288 errs() <<
"Recompiling LLVM with --enable-libffi might help.\n";
301 assert(Args.
size() == 1);
327 const char *FmtStr = (
const char *)
GVTOP(Args[1]);
338 sprintf(OutputBuffer++,
"%c", *FmtStr++);
341 sprintf(OutputBuffer,
"%c%c", *FmtStr, *(FmtStr+1));
342 FmtStr += 2; OutputBuffer += 2;
346 char FmtBuf[100] =
"", Buffer[1000] =
"";
349 char Last = *FB++ = *FmtStr++;
350 unsigned HowLong = 0;
351 while (Last !=
'c' && Last !=
'd' && Last !=
'i' && Last !=
'u' &&
352 Last !=
'o' && Last !=
'x' && Last !=
'X' && Last !=
'e' &&
353 Last !=
'E' && Last !=
'g' && Last !=
'G' && Last !=
'f' &&
354 Last !=
'p' && Last !=
's' && Last !=
'%') {
355 if (Last ==
'l' || Last ==
'L') HowLong++;
356 Last = *FB++ = *FmtStr++;
362 memcpy(Buffer,
"%", 2);
break;
364 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].
IntVal.getZExtValue()));
372 sizeof(long) <
sizeof(int64_t)) {
375 unsigned Size = strlen(FmtBuf);
376 FmtBuf[Size] = FmtBuf[Size-1];
378 FmtBuf[Size-1] =
'l';
380 sprintf(Buffer, FmtBuf, Args[ArgNo++].
IntVal.getZExtValue());
382 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].
IntVal.getZExtValue()));
384 case 'e':
case 'E':
case 'g':
case 'G':
case 'f':
385 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal);
break;
387 sprintf(Buffer, FmtBuf, (
void*)
GVTOP(Args[ArgNo++]));
break;
389 sprintf(Buffer, FmtBuf, (
char*)
GVTOP(Args[ArgNo++]));
break;
391 errs() <<
"<unknown printf code '" << *FmtStr <<
"'!>";
394 size_t Len = strlen(Buffer);
395 memcpy(OutputBuffer, Buffer, Len + 1);
409 std::vector<GenericValue> NewArgs;
410 NewArgs.push_back(
PTOGV((
void*)&Buffer[0]));
411 NewArgs.insert(NewArgs.end(), Args.
begin(), Args.
end());
420 assert(args.
size() < 10 &&
"Only handle up to 10 args to sscanf right now!");
423 for (
unsigned i = 0; i < args.
size(); ++i)
424 Args[i] = (
char*)
GVTOP(args[i]);
427 GV.
IntVal =
APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
428 Args[5], Args[6], Args[7], Args[8], Args[9]));
434 assert(args.
size() < 10 &&
"Only handle up to 10 args to scanf right now!");
437 for (
unsigned i = 0; i < args.
size(); ++i)
438 Args[i] = (
char*)
GVTOP(args[i]);
441 GV.
IntVal =
APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
442 Args[5], Args[6], Args[7], Args[8], Args[9]));
450 assert(Args.
size() >= 2);
452 std::vector<GenericValue> NewArgs;
453 NewArgs.push_back(
PTOGV(Buffer));
454 NewArgs.insert(NewArgs.end(), Args.
begin()+1, Args.
end());
457 fputs(Buffer, (FILE *)
GVTOP(Args[0]));
463 int val = (
int)Args[1].
IntVal.getSExtValue();
464 size_t len = (size_t)Args[2].
IntVal.getZExtValue();
465 memset((
void *)
GVTOP(Args[0]), val, len);
476 (
size_t)(Args[2].
IntVal.getLimitedValue()));
485 void Interpreter::initializeExternalFunctions() {
ValuesClass< DataType > LLVM_END_WITH_NULL values(const char *Arg, DataType Val, const char *Desc,...)
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.
static Interpreter * TheInterpreter
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)
static GenericValue lle_X_sprintf(FunctionType *FT, ArrayRef< GenericValue > Args)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
static ExFunc lookupFunction(const Function *F)
StringRef getName() const
Return a constant reference to the value's name.
static GenericValue lle_X_sscanf(FunctionType *FT, ArrayRef< GenericValue > args)
FunctionType - Class to represent function types.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
TypeID getTypeID() const
getTypeID - Return the type id for the type.
size_t size() const
size - Get the array size.
10: Arbitrary bit width integers
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
static ManagedStatic< sys::Mutex > FunctionsLock
Type * getParamType(unsigned i) const
Parameter type accessors.
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
Type * getContainedType(unsigned i) const
getContainedType - This method is used to implement the type iterator (defined at the end of the file...
static ManagedStatic< std::map< std::string, ExFunc > > FuncNames
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)
unsigned getNumContainedTypes() const
getNumContainedTypes - Return the number of types in the derived type.
GenericValue(* ExFunc)(FunctionType *, ArrayRef< GenericValue >)
void * GVTOP(const GenericValue &GV)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
static GenericValue lle_X_fprintf(FunctionType *FT, ArrayRef< GenericValue > Args)
GenericValue PTOGV(void *P)
Class for arbitrary precision integers.
static char getTypeID(Type *Ty)
PointerType * getType() const
Global values are always pointers.
pointer 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
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
3: 64-bit floating point type
Type * getReturnType() const
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...
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.