db3526824a59dfefddabd69b86b5f0877dcb3028
[oota-llvm.git] / tools / lli / lli.cpp
1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
2 //
3 // This utility provides a way to execute LLVM bytecode without static
4 // compilation.  This consists of a very simple and slow (but portable)
5 // interpreter, along with capability for system specific dynamic compilers.  At
6 // runtime, the fastest (stable) execution engine is selected to run the
7 // program.  This means the JIT compiler for the current platform if it's
8 // available.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "ExecutionEngine.h"
13 #include "Support/CommandLine.h"
14 #include "llvm/Bytecode/Reader.h"
15 #include "llvm/Module.h"
16 #include "llvm/Target/TargetMachineImpls.h"
17
18 namespace {
19   cl::opt<std::string>
20   InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
21
22   cl::list<std::string>
23   InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
24
25   cl::opt<std::string>
26   MainFunction ("f", cl::desc("Function to execute"), cl::init("main"),
27                 cl::value_desc("function name"));
28
29   cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing"));
30
31   cl::opt<bool> ForceInterpreter("force-interpreter",
32                                  cl::desc("Force interpretation: disable JIT"),
33                                  cl::init(false));
34 }
35
36 //===----------------------------------------------------------------------===//
37 // main Driver function
38 //
39 int main(int argc, char** argv, const char ** envp) {
40   cl::ParseCommandLineOptions(argc, argv,
41                               " llvm interpreter & dynamic compiler\n");
42
43   // Load the bytecode...
44   std::string ErrorMsg;
45   Module *M = ParseBytecodeFile(InputFile, &ErrorMsg);
46   if (M == 0) {
47     std::cout << "Error parsing '" << InputFile << "': "
48               << ErrorMsg << "\n";
49     exit(1);
50   }
51
52   ExecutionEngine *EE =
53     ExecutionEngine::create (M, ForceInterpreter, TraceMode);
54   assert (EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
55
56   // Add the module's name to the start of the vector of arguments to main().
57   // But delete .bc first, since programs (and users) might not expect to
58   // see it.
59   const std::string ByteCodeFileSuffix (".bc");
60   if (InputFile.rfind (ByteCodeFileSuffix) ==
61       InputFile.length () - ByteCodeFileSuffix.length ()) {
62     InputFile.erase (InputFile.length () - ByteCodeFileSuffix.length ());
63   }
64   InputArgv.insert(InputArgv.begin(), InputFile);
65
66   // Run the main function!
67   int ExitCode = EE->run(MainFunction, InputArgv, envp);
68
69   // Now that we are done executing the program, shut down the execution engine
70   delete EE;
71   return ExitCode;
72 }