2002-12-23 23:59:41 +00:00
|
|
|
//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
|
2001-08-23 17:05:04 +00:00
|
|
|
//
|
2002-12-23 23:59:41 +00:00
|
|
|
// This utility provides a way to execute LLVM bytecode without static
|
|
|
|
// compilation. This consists of a very simple and slow (but portable)
|
|
|
|
// interpreter, along with capability for system specific dynamic compilers. At
|
|
|
|
// runtime, the fastest (stable) execution engine is selected to run the
|
|
|
|
// program. This means the JIT compiler for the current platform if it's
|
|
|
|
// available.
|
2001-08-23 17:05:04 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
#include "ExecutionEngine.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2002-12-23 23:59:41 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Target/TargetMachineImpls.h"
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
namespace {
|
|
|
|
cl::opt<std::string>
|
|
|
|
InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
cl::list<std::string>
|
|
|
|
InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
cl::opt<std::string>
|
|
|
|
MainFunction ("f", cl::desc("Function to execute"), cl::init("main"),
|
|
|
|
cl::value_desc("function name"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
cl::opt<bool> DebugMode("d", cl::desc("Start program in debugger"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing"));
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
cl::opt<bool> ForceInterpreter("force-interpreter",
|
|
|
|
cl::desc("Force interpretation: disable JIT"),
|
2003-05-12 14:31:57 +00:00
|
|
|
cl::init(false));
|
2002-12-23 23:59:41 +00:00
|
|
|
}
|
2001-10-27 08:43:52 +00:00
|
|
|
|
2001-08-23 17:05:04 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-23 23:59:41 +00:00
|
|
|
// ExecutionEngine Class Implementation
|
2001-08-23 17:05:04 +00:00
|
|
|
//
|
2002-12-23 23:59:41 +00:00
|
|
|
|
|
|
|
ExecutionEngine::~ExecutionEngine() {
|
|
|
|
delete &CurMod;
|
2001-08-23 17:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// main Driver function
|
|
|
|
//
|
|
|
|
int main(int argc, char** argv) {
|
2002-12-23 23:59:41 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
" llvm interpreter & dynamic compiler\n");
|
|
|
|
|
|
|
|
// Load the bytecode...
|
2002-12-24 00:39:16 +00:00
|
|
|
std::string ErrorMsg;
|
2002-12-23 23:59:41 +00:00
|
|
|
Module *M = ParseBytecodeFile(InputFile, &ErrorMsg);
|
|
|
|
if (M == 0) {
|
2002-12-24 00:39:16 +00:00
|
|
|
std::cout << "Error parsing '" << InputFile << "': "
|
|
|
|
<< ErrorMsg << "\n";
|
2002-12-23 23:59:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// Link in the runtime library for LLI...
|
2002-12-24 00:39:16 +00:00
|
|
|
std::string RuntimeLib = getCurrentExecutablePath();
|
2002-12-23 23:59:41 +00:00
|
|
|
if (!RuntimeLib.empty()) RuntimeLib += "/";
|
|
|
|
RuntimeLib += "RuntimeLib.bc";
|
|
|
|
|
|
|
|
if (Module *SupportLib = ParseBytecodeFile(RuntimeLib, &ErrorMsg)) {
|
|
|
|
if (LinkModules(M, SupportLib, &ErrorMsg))
|
|
|
|
std::cerr << "Error Linking runtime library into current module: "
|
|
|
|
<< ErrorMsg << "\n";
|
|
|
|
} else {
|
|
|
|
std::cerr << "Error loading runtime library '"+RuntimeLib+"': "
|
|
|
|
<< ErrorMsg << "\n";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-04-22 18:10:32 +00:00
|
|
|
unsigned Config = (M->isLittleEndian() ? TM::LittleEndian : TM::BigEndian) |
|
|
|
|
(M->has32BitPointers() ? TM::PtrSize32 : TM::PtrSize64);
|
2002-12-23 23:59:41 +00:00
|
|
|
ExecutionEngine *EE = 0;
|
|
|
|
|
|
|
|
// If there is nothing that is forcing us to use the interpreter, make a JIT.
|
|
|
|
if (!ForceInterpreter && !DebugMode && !TraceMode)
|
|
|
|
EE = ExecutionEngine::createJIT(M, Config);
|
|
|
|
|
|
|
|
// If we can't make a JIT, make an interpreter instead.
|
|
|
|
if (EE == 0)
|
|
|
|
EE = ExecutionEngine::createInterpreter(M, Config, DebugMode, TraceMode);
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2002-07-22 02:10:13 +00:00
|
|
|
// Add the module name to the start of the argv vector...
|
2003-05-23 20:28:07 +00:00
|
|
|
// But delete .bc first, since programs (and users) might not expect to
|
|
|
|
// see it.
|
|
|
|
const std::string ByteCodeFileSuffix (".bc");
|
|
|
|
if (InputFile.rfind (ByteCodeFileSuffix) ==
|
|
|
|
InputFile.length () - ByteCodeFileSuffix.length ()) {
|
|
|
|
InputFile.erase (InputFile.length () - ByteCodeFileSuffix.length ());
|
|
|
|
}
|
2002-07-22 02:10:13 +00:00
|
|
|
InputArgv.insert(InputArgv.begin(), InputFile);
|
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
// Run the main function!
|
|
|
|
int ExitCode = EE->run(MainFunction, InputArgv);
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
// Now that we are done executing the program, shut down the execution engine
|
|
|
|
delete EE;
|
|
|
|
return ExitCode;
|
2001-08-23 17:05:04 +00:00
|
|
|
}
|