2002-12-24 07:59:41 +08:00
|
|
|
//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
|
2001-08-24 01:05:04 +08:00
|
|
|
//
|
2002-12-24 07:59:41 +08: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-24 01:05:04 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
#include "ExecutionEngine.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include "Support/CommandLine.h"
|
2002-12-24 07:59:41 +08:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Target/TargetMachineImpls.h"
|
2001-08-24 01:05:04 +08:00
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
namespace {
|
|
|
|
cl::opt<std::string>
|
|
|
|
InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
cl::list<std::string>
|
|
|
|
InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
cl::opt<std::string>
|
|
|
|
MainFunction ("f", cl::desc("Function to execute"), cl::init("main"),
|
|
|
|
cl::value_desc("function name"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
cl::opt<bool> DebugMode("d", cl::desc("Start program in debugger"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing"));
|
2001-08-24 01:05:04 +08:00
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
cl::opt<bool> ForceInterpreter("force-interpreter",
|
|
|
|
cl::desc("Force interpretation: disable JIT"),
|
2003-05-12 22:31:57 +08:00
|
|
|
cl::init(false));
|
2002-12-24 07:59:41 +08:00
|
|
|
}
|
2001-10-27 16:43:52 +08:00
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-24 07:59:41 +08:00
|
|
|
// ExecutionEngine Class Implementation
|
2001-08-24 01:05:04 +08:00
|
|
|
//
|
2002-12-24 07:59:41 +08:00
|
|
|
|
|
|
|
ExecutionEngine::~ExecutionEngine() {
|
|
|
|
delete &CurMod;
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// main Driver function
|
|
|
|
//
|
2003-08-22 05:12:30 +08:00
|
|
|
int main(int argc, char** argv, const char ** envp) {
|
2002-12-24 07:59:41 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
" llvm interpreter & dynamic compiler\n");
|
|
|
|
|
|
|
|
// Load the bytecode...
|
2002-12-24 08:39:16 +08:00
|
|
|
std::string ErrorMsg;
|
2002-12-24 07:59:41 +08:00
|
|
|
Module *M = ParseBytecodeFile(InputFile, &ErrorMsg);
|
|
|
|
if (M == 0) {
|
2002-12-24 08:39:16 +08:00
|
|
|
std::cout << "Error parsing '" << InputFile << "': "
|
|
|
|
<< ErrorMsg << "\n";
|
2002-12-24 07:59:41 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-09-04 04:34:19 +08:00
|
|
|
ExecutionEngine *EE =
|
|
|
|
ExecutionEngine::create (M, ForceInterpreter, DebugMode, TraceMode);
|
|
|
|
assert (EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
|
2001-08-24 01:05:04 +08:00
|
|
|
|
2002-07-22 10:10:13 +08:00
|
|
|
// Add the module name to the start of the argv vector...
|
2003-05-24 04:28:07 +08: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 10:10:13 +08:00
|
|
|
InputArgv.insert(InputArgv.begin(), InputFile);
|
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
// Run the main function!
|
2003-08-22 05:12:30 +08:00
|
|
|
int ExitCode = EE->run(MainFunction, InputArgv, envp);
|
2001-08-24 01:05:04 +08:00
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
// Now that we are done executing the program, shut down the execution engine
|
|
|
|
delete EE;
|
|
|
|
return ExitCode;
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|