Include the new selection library for the X86 target
[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> DebugMode("d", cl::desc("Start program in debugger"));
30
31   cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing"));
32
33   cl::opt<bool> ForceInterpreter("force-interpreter",
34                                  cl::desc("Force interpretation: disable JIT"),
35                                  cl::init(false));
36 }
37
38 //===----------------------------------------------------------------------===//
39 // ExecutionEngine Class Implementation
40 //
41
42 ExecutionEngine::~ExecutionEngine() {
43   delete &CurMod;
44 }
45
46 //===----------------------------------------------------------------------===//
47 // main Driver function
48 //
49 int main(int argc, char** argv) {
50   cl::ParseCommandLineOptions(argc, argv,
51                               " llvm interpreter & dynamic compiler\n");
52
53   // Load the bytecode...
54   std::string ErrorMsg;
55   Module *M = ParseBytecodeFile(InputFile, &ErrorMsg);
56   if (M == 0) {
57     std::cout << "Error parsing '" << InputFile << "': "
58               << ErrorMsg << "\n";
59     exit(1);
60   }
61
62 #if 0
63   // Link in the runtime library for LLI...
64   std::string RuntimeLib = getCurrentExecutablePath();
65   if (!RuntimeLib.empty()) RuntimeLib += "/";
66   RuntimeLib += "RuntimeLib.bc";
67
68   if (Module *SupportLib = ParseBytecodeFile(RuntimeLib, &ErrorMsg)) {
69     if (LinkModules(M, SupportLib, &ErrorMsg))
70       std::cerr << "Error Linking runtime library into current module: "
71                 << ErrorMsg << "\n";
72   } else {
73     std::cerr << "Error loading runtime library '"+RuntimeLib+"': "
74               << ErrorMsg << "\n";
75   }
76 #endif
77
78   unsigned Config = (M->isLittleEndian()   ? TM::LittleEndian : TM::BigEndian) |
79                     (M->has32BitPointers() ? TM::PtrSize32    : TM::PtrSize64);
80   ExecutionEngine *EE = 0;
81
82   // If there is nothing that is forcing us to use the interpreter, make a JIT.
83   if (!ForceInterpreter && !DebugMode && !TraceMode)
84     EE = ExecutionEngine::createJIT(M, Config);
85
86   // If we can't make a JIT, make an interpreter instead.
87   if (EE == 0)
88     EE = ExecutionEngine::createInterpreter(M, Config, DebugMode, TraceMode);
89
90   // Add the module name to the start of the argv vector...
91   // But delete .bc first, since programs (and users) might not expect to
92   // see it.
93   const std::string ByteCodeFileSuffix (".bc");
94   if (InputFile.rfind (ByteCodeFileSuffix) ==
95       InputFile.length () - ByteCodeFileSuffix.length ()) {
96     InputFile.erase (InputFile.length () - ByteCodeFileSuffix.length ());
97   }
98   InputArgv.insert(InputArgv.begin(), InputFile);
99
100   // Run the main function!
101   int ExitCode = EE->run(MainFunction, InputArgv);
102
103   // Now that we are done executing the program, shut down the execution engine
104   delete EE;
105   return ExitCode;
106 }