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> 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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// main Driver function
|
|
|
|
//
|
2003-08-21 21:12:30 +00:00
|
|
|
int main(int argc, char** argv, const char ** envp) {
|
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);
|
|
|
|
}
|
|
|
|
|
2003-09-03 20:34:19 +00:00
|
|
|
ExecutionEngine *EE =
|
2003-09-04 22:21:24 +00:00
|
|
|
ExecutionEngine::create (M, ForceInterpreter, TraceMode);
|
2003-09-03 20:34:19 +00:00
|
|
|
assert (EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2003-09-04 22:21:24 +00:00
|
|
|
// Add the module's name to the start of the vector of arguments to main().
|
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!
|
2003-08-21 21:12:30 +00:00
|
|
|
int ExitCode = EE->run(MainFunction, InputArgv, envp);
|
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
|
|
|
}
|