1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
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
10 //===----------------------------------------------------------------------===//
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"
20 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
23 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
26 MainFunction ("f", cl::desc("Function to execute"), cl::init("main"),
27 cl::value_desc("function name"));
29 cl::opt<bool> DebugMode("d", cl::desc("Start program in debugger"));
31 cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing"));
33 cl::opt<bool> ForceInterpreter("force-interpreter",
34 cl::desc("Force interpretation: disable JIT"),
38 //===----------------------------------------------------------------------===//
39 // ExecutionEngine Class Implementation
42 ExecutionEngine::~ExecutionEngine() {
46 //===----------------------------------------------------------------------===//
47 // main Driver function
49 int main(int argc, char** argv, const char ** envp) {
50 cl::ParseCommandLineOptions(argc, argv,
51 " llvm interpreter & dynamic compiler\n");
53 // Load the bytecode...
55 Module *M = ParseBytecodeFile(InputFile, &ErrorMsg);
57 std::cout << "Error parsing '" << InputFile << "': "
62 ExecutionEngine *EE = 0;
64 // If there is nothing that is forcing us to use the interpreter, make a JIT.
65 if (!ForceInterpreter && !DebugMode && !TraceMode)
66 EE = ExecutionEngine::createJIT(M);
68 // If we can't make a JIT, make an interpreter instead.
70 EE = ExecutionEngine::createInterpreter(M, DebugMode, TraceMode);
72 // Add the module name to the start of the argv vector...
73 // But delete .bc first, since programs (and users) might not expect to
75 const std::string ByteCodeFileSuffix (".bc");
76 if (InputFile.rfind (ByteCodeFileSuffix) ==
77 InputFile.length () - ByteCodeFileSuffix.length ()) {
78 InputFile.erase (InputFile.length () - ByteCodeFileSuffix.length ());
80 InputArgv.insert(InputArgv.begin(), InputFile);
82 // Run the main function!
83 int ExitCode = EE->run(MainFunction, InputArgv, envp);
85 // Now that we are done executing the program, shut down the execution engine