2001-09-10 12:50:17 +08:00
|
|
|
//===-- ExternalMethods.cpp - Implement External Methods ------------------===//
|
|
|
|
//
|
|
|
|
// This file contains both code to deal with invoking "external" methods, but
|
|
|
|
// also contains code that implements "exported" external methods.
|
|
|
|
//
|
|
|
|
// External methods in LLI are implemented by dlopen'ing the lli executable and
|
|
|
|
// using dlsym to look op the methods that we want to invoke. If a method is
|
|
|
|
// found, then the arguments are mangled and passed in to the function call.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Interpreter.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include <map>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <link.h>
|
2001-10-27 16:28:11 +08:00
|
|
|
#include <math.h>
|
2001-11-07 05:52:18 +08:00
|
|
|
#include <stdio.h>
|
2001-09-10 12:50:17 +08:00
|
|
|
|
|
|
|
typedef GenericValue (*ExFunc)(MethodType *, const vector<GenericValue> &);
|
|
|
|
static map<const Method *, ExFunc> Functions;
|
2001-10-31 04:28:00 +08:00
|
|
|
static map<string, ExFunc> FuncNames;
|
2001-09-10 12:50:17 +08:00
|
|
|
|
2001-10-27 12:15:57 +08:00
|
|
|
static Interpreter *TheInterpreter;
|
|
|
|
|
|
|
|
// getCurrentExecutablePath() - Return the directory that the lli executable
|
|
|
|
// lives in.
|
|
|
|
//
|
|
|
|
string Interpreter::getCurrentExecutablePath() const {
|
|
|
|
Dl_info Info;
|
|
|
|
if (dladdr(&TheInterpreter, &Info) == 0) return "";
|
|
|
|
|
|
|
|
string LinkAddr(Info.dli_fname);
|
|
|
|
unsigned SlashPos = LinkAddr.rfind('/');
|
|
|
|
if (SlashPos != string::npos)
|
|
|
|
LinkAddr.resize(SlashPos); // Trim the executable name off...
|
|
|
|
|
|
|
|
return LinkAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-10 12:50:17 +08:00
|
|
|
static char getTypeID(const Type *Ty) {
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
case Type::VoidTyID: return 'V';
|
|
|
|
case Type::BoolTyID: return 'o';
|
|
|
|
case Type::UByteTyID: return 'B';
|
|
|
|
case Type::SByteTyID: return 'b';
|
|
|
|
case Type::UShortTyID: return 'S';
|
|
|
|
case Type::ShortTyID: return 's';
|
|
|
|
case Type::UIntTyID: return 'I';
|
|
|
|
case Type::IntTyID: return 'i';
|
|
|
|
case Type::ULongTyID: return 'L';
|
|
|
|
case Type::LongTyID: return 'l';
|
|
|
|
case Type::FloatTyID: return 'F';
|
|
|
|
case Type::DoubleTyID: return 'D';
|
|
|
|
case Type::PointerTyID: return 'P';
|
|
|
|
case Type::MethodTyID: return 'M';
|
|
|
|
case Type::StructTyID: return 'T';
|
|
|
|
case Type::ArrayTyID: return 'A';
|
|
|
|
case Type::OpaqueTyID: return 'O';
|
|
|
|
default: return 'U';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExFunc lookupMethod(const Method *M) {
|
|
|
|
// Function not found, look it up... start by figuring out what the
|
|
|
|
// composite function name should be.
|
|
|
|
string ExtName = "lle_";
|
2001-10-03 22:53:21 +08:00
|
|
|
const MethodType *MT = M->getMethodType();
|
2001-09-10 12:50:17 +08:00
|
|
|
for (unsigned i = 0; const Type *Ty = MT->getContainedType(i); ++i)
|
|
|
|
ExtName += getTypeID(Ty);
|
|
|
|
ExtName += "_" + M->getName();
|
|
|
|
|
|
|
|
//cout << "Tried: '" << ExtName << "'\n";
|
2001-10-31 04:28:00 +08:00
|
|
|
ExFunc FnPtr = FuncNames[ExtName];
|
|
|
|
if (FnPtr == 0)
|
|
|
|
FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ExtName.c_str());
|
|
|
|
if (FnPtr == 0)
|
|
|
|
FnPtr = FuncNames["lle_X_"+M->getName()];
|
2001-09-10 12:50:17 +08:00
|
|
|
if (FnPtr == 0) // Try calling a generic function... if it exists...
|
|
|
|
FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ("lle_X_"+M->getName()).c_str());
|
|
|
|
if (FnPtr != 0)
|
|
|
|
Functions.insert(make_pair(M, FnPtr)); // Cache for later
|
|
|
|
return FnPtr;
|
|
|
|
}
|
|
|
|
|
2001-10-31 04:28:00 +08:00
|
|
|
GenericValue Interpreter::callExternalMethod(Method *M,
|
|
|
|
const vector<GenericValue> &ArgVals) {
|
2001-10-27 12:15:57 +08:00
|
|
|
TheInterpreter = this;
|
|
|
|
|
2001-09-10 12:50:17 +08:00
|
|
|
// Do a lookup to see if the method is in our cache... this should just be a
|
|
|
|
// defered annotation!
|
|
|
|
map<const Method *, ExFunc>::iterator FI = Functions.find(M);
|
|
|
|
ExFunc Fn = (FI == Functions.end()) ? lookupMethod(M) : FI->second;
|
|
|
|
if (Fn == 0) {
|
|
|
|
cout << "Tried to execute an unknown external method: "
|
|
|
|
<< M->getType()->getDescription() << " " << M->getName() << endl;
|
2001-10-31 04:28:00 +08:00
|
|
|
return GenericValue();
|
2001-09-10 12:50:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: FIXME when types are not const!
|
2001-10-03 22:53:21 +08:00
|
|
|
GenericValue Result = Fn(const_cast<MethodType*>(M->getMethodType()),ArgVals);
|
2001-10-31 04:28:00 +08:00
|
|
|
return Result;
|
2001-09-10 12:50:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods "exported" to the running application...
|
|
|
|
//
|
|
|
|
extern "C" { // Don't add C++ manglings to llvm mangling :)
|
|
|
|
|
2001-10-15 13:51:48 +08:00
|
|
|
// Implement void printstr([ubyte {x N}] *)
|
|
|
|
GenericValue lle_VP_printstr(MethodType *M, const vector<GenericValue> &ArgVal){
|
|
|
|
assert(ArgVal.size() == 1 && "printstr only takes one argument!");
|
|
|
|
cout << (char*)ArgVal[0].PointerVal;
|
|
|
|
return GenericValue();
|
|
|
|
}
|
|
|
|
|
2001-09-10 12:50:17 +08:00
|
|
|
// Implement 'void print(X)' for every type...
|
|
|
|
GenericValue lle_X_print(MethodType *M, const vector<GenericValue> &ArgVals) {
|
|
|
|
assert(ArgVals.size() == 1 && "generic print only takes one argument!");
|
2001-10-15 13:51:48 +08:00
|
|
|
|
|
|
|
Interpreter::print(M->getParamTypes()[0], ArgVals[0]);
|
|
|
|
return GenericValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement 'void printVal(X)' for every type...
|
|
|
|
GenericValue lle_X_printVal(MethodType *M, const vector<GenericValue> &ArgVal) {
|
|
|
|
assert(ArgVal.size() == 1 && "generic print only takes one argument!");
|
|
|
|
|
2001-10-19 05:55:32 +08:00
|
|
|
// Specialize print([ubyte {x N} ] *) and print(sbyte *)
|
2001-10-15 13:51:48 +08:00
|
|
|
if (PointerType *PTy = dyn_cast<PointerType>(M->getParamTypes()[0].get()))
|
2001-10-19 05:55:32 +08:00
|
|
|
if (PTy->getValueType() == Type::SByteTy ||
|
|
|
|
isa<ArrayType>(PTy->getValueType())) {
|
2001-10-15 13:51:48 +08:00
|
|
|
return lle_VP_printstr(M, ArgVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);
|
2001-09-10 12:50:17 +08:00
|
|
|
return GenericValue();
|
|
|
|
}
|
|
|
|
|
2001-10-29 04:52:27 +08:00
|
|
|
// Implement 'void printString(X)'
|
|
|
|
// Argument must be [ubyte {x N} ] * or sbyte *
|
|
|
|
GenericValue lle_X_printString(MethodType *M, const vector<GenericValue> &ArgVal) {
|
|
|
|
assert(ArgVal.size() == 1 && "generic print only takes one argument!");
|
|
|
|
return lle_VP_printstr(M, ArgVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement 'void print<TYPE>(X)' for each primitive type or pointer type
|
|
|
|
#define PRINT_TYPE_FUNC(TYPENAME,TYPEID) \
|
2001-10-29 06:38:22 +08:00
|
|
|
GenericValue lle_X_print##TYPENAME(MethodType *M,\
|
|
|
|
const vector<GenericValue> &ArgVal) {\
|
2001-10-29 04:52:27 +08:00
|
|
|
assert(ArgVal.size() == 1 && "generic print only takes one argument!");\
|
2001-10-29 06:38:22 +08:00
|
|
|
assert(M->getParamTypes()[0].get()->getPrimitiveID() == Type::##TYPEID);\
|
2001-10-29 04:52:27 +08:00
|
|
|
Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);\
|
|
|
|
return GenericValue();\
|
|
|
|
}
|
|
|
|
|
2001-10-31 04:28:00 +08:00
|
|
|
PRINT_TYPE_FUNC(SByte, SByteTyID)
|
2001-10-29 04:52:27 +08:00
|
|
|
PRINT_TYPE_FUNC(UByte, UByteTyID)
|
|
|
|
PRINT_TYPE_FUNC(Short, ShortTyID)
|
|
|
|
PRINT_TYPE_FUNC(UShort, UShortTyID)
|
|
|
|
PRINT_TYPE_FUNC(Int, IntTyID)
|
|
|
|
PRINT_TYPE_FUNC(UInt, UIntTyID)
|
|
|
|
PRINT_TYPE_FUNC(Long, LongTyID)
|
|
|
|
PRINT_TYPE_FUNC(ULong, ULongTyID)
|
|
|
|
PRINT_TYPE_FUNC(Float, FloatTyID)
|
|
|
|
PRINT_TYPE_FUNC(Double, DoubleTyID)
|
|
|
|
PRINT_TYPE_FUNC(Pointer, PointerTyID)
|
|
|
|
|
|
|
|
|
2001-09-10 12:50:17 +08:00
|
|
|
// void "putchar"(sbyte)
|
|
|
|
GenericValue lle_Vb_putchar(MethodType *M, const vector<GenericValue> &Args) {
|
|
|
|
cout << Args[0].SByteVal;
|
|
|
|
return GenericValue();
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:15:57 +08:00
|
|
|
// int "putchar"(int)
|
|
|
|
GenericValue lle_ii_putchar(MethodType *M, const vector<GenericValue> &Args) {
|
|
|
|
cout << ((char)Args[0].IntVal) << flush;
|
|
|
|
return Args[0];
|
|
|
|
}
|
|
|
|
|
2001-10-15 13:51:48 +08:00
|
|
|
// void "putchar"(ubyte)
|
|
|
|
GenericValue lle_VB_putchar(MethodType *M, const vector<GenericValue> &Args) {
|
2001-10-27 12:15:57 +08:00
|
|
|
cout << Args[0].SByteVal << flush;
|
|
|
|
return Args[0];
|
2001-10-15 13:51:48 +08:00
|
|
|
}
|
|
|
|
|
2001-10-19 05:55:32 +08:00
|
|
|
// void "__main"()
|
|
|
|
GenericValue lle_V___main(MethodType *M, const vector<GenericValue> &Args) {
|
|
|
|
return GenericValue();
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:15:57 +08:00
|
|
|
// void "exit"(int)
|
2001-11-03 18:15:32 +08:00
|
|
|
GenericValue lle_X_exit(MethodType *M, const vector<GenericValue> &Args) {
|
2001-10-27 12:15:57 +08:00
|
|
|
TheInterpreter->exitCalled(Args[0]);
|
|
|
|
return GenericValue();
|
|
|
|
}
|
|
|
|
|
2001-10-27 16:28:11 +08:00
|
|
|
// void *malloc(uint)
|
2001-11-03 18:15:32 +08:00
|
|
|
GenericValue lle_X_malloc(MethodType *M, const vector<GenericValue> &Args) {
|
2001-10-31 04:28:00 +08:00
|
|
|
assert(Args.size() == 1 && "Malloc expects one argument!");
|
2001-10-27 16:28:11 +08:00
|
|
|
GenericValue GV;
|
2001-10-31 04:28:00 +08:00
|
|
|
GV.PointerVal = (uint64_t)malloc(Args[0].UIntVal);
|
2001-10-27 16:28:11 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
|
|
|
// void free(void *)
|
2001-11-03 18:15:32 +08:00
|
|
|
GenericValue lle_X_free(MethodType *M, const vector<GenericValue> &Args) {
|
2001-11-07 05:52:18 +08:00
|
|
|
assert(Args.size() == 1);
|
2001-10-31 04:28:00 +08:00
|
|
|
free((void*)Args[0].PointerVal);
|
2001-10-27 16:28:11 +08:00
|
|
|
return GenericValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
// double pow(double, double)
|
2001-11-03 18:15:32 +08:00
|
|
|
GenericValue lle_X_pow(MethodType *M, const vector<GenericValue> &Args) {
|
2001-11-07 05:52:18 +08:00
|
|
|
assert(Args.size() == 2);
|
2001-10-27 16:28:11 +08:00
|
|
|
GenericValue GV;
|
2001-10-30 04:27:45 +08:00
|
|
|
GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
|
2001-10-27 16:28:11 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2001-11-07 05:52:18 +08:00
|
|
|
// double sqrt(double)
|
|
|
|
GenericValue lle_X_sqrt(MethodType *M, const vector<GenericValue> &Args) {
|
|
|
|
assert(Args.size() == 1);
|
|
|
|
GenericValue GV;
|
|
|
|
GV.DoubleVal = sqrt(Args[0].DoubleVal);
|
|
|
|
return GV;
|
|
|
|
}
|
2001-10-27 16:28:11 +08:00
|
|
|
|
2001-11-07 06:53:25 +08:00
|
|
|
// double log(double)
|
|
|
|
GenericValue lle_X_log(MethodType *M, const vector<GenericValue> &Args) {
|
|
|
|
assert(Args.size() == 1);
|
|
|
|
GenericValue GV;
|
|
|
|
GV.DoubleVal = log(Args[0].DoubleVal);
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
|
|
|
// double drand48()
|
|
|
|
GenericValue lle_X_drand48(MethodType *M, const vector<GenericValue> &Args) {
|
|
|
|
assert(Args.size() == 0);
|
|
|
|
GenericValue GV;
|
|
|
|
GV.DoubleVal = drand48();
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-30 04:27:45 +08:00
|
|
|
// int printf(sbyte *, ...) - a very rough implementation to make output useful.
|
2001-11-03 18:15:32 +08:00
|
|
|
GenericValue lle_X_printf(MethodType *M, const vector<GenericValue> &Args) {
|
2001-10-30 04:27:45 +08:00
|
|
|
const char *FmtStr = (const char *)Args[0].PointerVal;
|
|
|
|
unsigned ArgNo = 1;
|
|
|
|
|
|
|
|
// printf should return # chars printed. This is completely incorrect, but
|
|
|
|
// close enough for now.
|
|
|
|
GenericValue GV; GV.IntVal = strlen(FmtStr);
|
|
|
|
while (1) {
|
|
|
|
switch (*FmtStr) {
|
|
|
|
case 0: return GV; // Null terminator...
|
|
|
|
default: // Normal nonspecial character
|
|
|
|
cout << *FmtStr++;
|
|
|
|
break;
|
|
|
|
case '\\': { // Handle escape codes
|
|
|
|
char Buffer[3];
|
|
|
|
Buffer[0] = *FmtStr++;
|
|
|
|
Buffer[1] = *FmtStr++;
|
|
|
|
Buffer[2] = 0;
|
|
|
|
cout << Buffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '%': { // Handle format specifiers
|
|
|
|
bool isLong = false;
|
|
|
|
++FmtStr;
|
|
|
|
if (*FmtStr == 'l') {
|
|
|
|
isLong = true;
|
|
|
|
FmtStr++;
|
|
|
|
}
|
|
|
|
|
2001-11-07 05:52:18 +08:00
|
|
|
if (*FmtStr == '%')
|
|
|
|
cout << *FmtStr; // %%
|
|
|
|
else {
|
|
|
|
char Fmt[] = "%d", Buffer[1000] = "";
|
|
|
|
Fmt[1] = *FmtStr;
|
|
|
|
|
|
|
|
switch (*FmtStr) {
|
|
|
|
case 'c':
|
|
|
|
sprintf(Buffer, Fmt, Args[ArgNo++].SByteVal); break;
|
|
|
|
case 'd': case 'i':
|
|
|
|
case 'u': case 'o':
|
|
|
|
case 'x': case 'X':
|
|
|
|
sprintf(Buffer, Fmt, Args[ArgNo++].IntVal); break;
|
|
|
|
case 'e': case 'E': case 'g': case 'G': case 'f':
|
|
|
|
sprintf(Buffer, Fmt, Args[ArgNo++].DoubleVal); break;
|
2001-11-07 10:57:33 +08:00
|
|
|
case 'p':
|
|
|
|
sprintf(Buffer, Fmt, (void*)Args[ArgNo++].PointerVal); break;
|
2001-11-07 05:52:18 +08:00
|
|
|
case 's': cout << (char*)Args[ArgNo++].PointerVal; break; // %s
|
|
|
|
default: cout << "<unknown printf code '" << *FmtStr << "'!>";
|
|
|
|
ArgNo++; break;
|
|
|
|
}
|
|
|
|
cout << Buffer;
|
2001-10-30 04:27:45 +08:00
|
|
|
}
|
|
|
|
++FmtStr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-10 12:50:17 +08:00
|
|
|
} // End extern "C"
|
2001-10-31 04:28:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
void Interpreter::initializeExternalMethods() {
|
|
|
|
FuncNames["lle_VP_printstr"] = lle_VP_printstr;
|
|
|
|
FuncNames["lle_X_print"] = lle_X_print;
|
|
|
|
FuncNames["lle_X_printVal"] = lle_X_printVal;
|
|
|
|
FuncNames["lle_X_printString"] = lle_X_printString;
|
|
|
|
FuncNames["lle_X_printUByte"] = lle_X_printUByte;
|
|
|
|
FuncNames["lle_X_printSByte"] = lle_X_printSByte;
|
|
|
|
FuncNames["lle_X_printUShort"] = lle_X_printUShort;
|
|
|
|
FuncNames["lle_X_printShort"] = lle_X_printShort;
|
|
|
|
FuncNames["lle_X_printInt"] = lle_X_printInt;
|
|
|
|
FuncNames["lle_X_printUInt"] = lle_X_printUInt;
|
|
|
|
FuncNames["lle_X_printLong"] = lle_X_printLong;
|
|
|
|
FuncNames["lle_X_printULong"] = lle_X_printULong;
|
|
|
|
FuncNames["lle_X_printFloat"] = lle_X_printFloat;
|
|
|
|
FuncNames["lle_X_printDouble"] = lle_X_printDouble;
|
|
|
|
FuncNames["lle_X_printPointer"] = lle_X_printPointer;
|
2001-11-03 18:15:32 +08:00
|
|
|
FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
|
|
|
|
FuncNames["lle_ii_putchar"] = lle_ii_putchar;
|
|
|
|
FuncNames["lle_VB_putchar"] = lle_VB_putchar;
|
|
|
|
FuncNames["lle_V___main"] = lle_V___main;
|
|
|
|
FuncNames["lle_X_exit"] = lle_X_exit;
|
|
|
|
FuncNames["lle_X_malloc"] = lle_X_malloc;
|
|
|
|
FuncNames["lle_X_free"] = lle_X_free;
|
|
|
|
FuncNames["lle_X_pow"] = lle_X_pow;
|
2001-11-07 05:52:18 +08:00
|
|
|
FuncNames["lle_X_sqrt"] = lle_X_sqrt;
|
2001-11-03 18:15:32 +08:00
|
|
|
FuncNames["lle_X_printf"] = lle_X_printf;
|
2001-10-31 04:28:00 +08:00
|
|
|
}
|