25 #include "llvm/Config/config.h"
68 typedef void (*RawFunc)();
78 switch (cast<IntegerType>(Ty)->getBitWidth()) {
104 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();
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[0]) == FFI_OK) {
234 ffi_call(&cif, Fn, ret.
data(),
values.data());
237 switch (cast<IntegerType>(RetTy)->getBitWidth()) {
272 std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
274 if (RF == RawFunctions->end()) {
280 RawFunctions->insert(std::make_pair(F, RawFn));
288 if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals,
getDataLayout(), Result))
293 errs() <<
"Tried to execute an unknown external function: "
294 << *F->
getType() <<
" __main\n";
299 errs() <<
"Recompiling LLVM with --enable-libffi might help.\n";
336 char *OutputBuffer = (
char *)
GVTOP(Args[0]);
337 const char *FmtStr = (
const char *)
GVTOP(Args[1]);
348 sprintf(OutputBuffer++,
"%c", *FmtStr++);
351 sprintf(OutputBuffer,
"%c%c", *FmtStr, *(FmtStr+1));
352 FmtStr += 2; OutputBuffer += 2;
356 char FmtBuf[100] =
"", Buffer[1000] =
"";
359 char Last = *FB++ = *FmtStr++;
360 unsigned HowLong = 0;
361 while (Last !=
'c' && Last !=
'd' && Last !=
'i' && Last !=
'u' &&
362 Last !=
'o' && Last !=
'x' && Last !=
'X' && Last !=
'e' &&
363 Last !=
'E' && Last !=
'g' && Last !=
'G' && Last !=
'f' &&
364 Last !=
'p' && Last !=
's' && Last !=
'%') {
365 if (Last ==
'l' || Last ==
'L') HowLong++;
366 Last = *FB++ = *FmtStr++;
372 memcpy(Buffer,
"%", 2);
break;
374 sprintf(Buffer, FmtBuf,
uint32_t(Args[ArgNo++].
IntVal.getZExtValue()));
382 sizeof(long) <
sizeof(int64_t)) {
385 unsigned Size = strlen(FmtBuf);
386 FmtBuf[Size] = FmtBuf[Size-1];
388 FmtBuf[Size-1] =
'l';
390 sprintf(Buffer, FmtBuf, Args[ArgNo++].
IntVal.getZExtValue());
392 sprintf(Buffer, FmtBuf,
uint32_t(Args[ArgNo++].
IntVal.getZExtValue()));
394 case 'e':
case 'E':
case 'g':
case 'G':
case 'f':
395 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal);
break;
397 sprintf(Buffer, FmtBuf, (
void*)
GVTOP(Args[ArgNo++]));
break;
399 sprintf(Buffer, FmtBuf, (
char*)
GVTOP(Args[ArgNo++]));
break;
401 errs() <<
"<unknown printf code '" << *FmtStr <<
"'!>";
404 size_t Len = strlen(Buffer);
405 memcpy(OutputBuffer, Buffer, Len + 1);
419 std::vector<GenericValue> NewArgs;
420 NewArgs.push_back(
PTOGV((
void*)&Buffer[0]));
421 NewArgs.insert(NewArgs.end(), Args.
begin(), Args.
end());
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]);
437 GV.
IntVal =
APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
438 Args[5], Args[6], Args[7], Args[8], Args[9]));
444 assert(args.
size() < 10 &&
"Only handle up to 10 args to scanf right now!");
447 for (
unsigned i = 0;
i < args.
size(); ++
i)
448 Args[
i] = (
char*)
GVTOP(args[
i]);
451 GV.
IntVal =
APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
452 Args[5], Args[6], Args[7], Args[8], Args[9]));
462 std::vector<GenericValue> NewArgs;
463 NewArgs.push_back(
PTOGV(Buffer));
464 NewArgs.insert(NewArgs.end(), Args.
begin()+1, Args.
end());
467 fputs(Buffer, (FILE *)
GVTOP(Args[0]));
473 int val = (int)Args[1].
IntVal.getSExtValue();
474 size_t len = (size_t)Args[2].
IntVal.getZExtValue();
475 memset((
void *)
GVTOP(Args[0]), val, len);
486 (
size_t)(Args[2].
IntVal.getLimitedValue()));
495 void Interpreter::initializeExternalFunctions() {
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
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
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)
static ExFunc lookupFunction(const Function *F)
StringRef getName() const
Return a constant reference to the value's name.
const DataLayout & getDataLayout() const
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)...
TypeID getTypeID() const
Return the type id for the type.
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
11: Arbitrary bit width integers
ValuesClass values(OptsTy...Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
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
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
Return the number of types in the derived type.
GenericValue(* ExFunc)(FunctionType *, ArrayRef< GenericValue >)
void * GVTOP(const GenericValue &GV)
Iterator for intrusive lists based on ilist_node.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
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().
FunctionType * getFunctionType() const
Returns the FunctionType for me.
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
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...
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.