summaryrefslogtreecommitdiff
path: root/tools/lli/lli.cpp
blob: db3526824a59dfefddabd69b86b5f0877dcb3028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//

#include "ExecutionEngine.h"
#include "Support/CommandLine.h"
#include "llvm/Bytecode/Reader.h"
#include "llvm/Module.h"
#include "llvm/Target/TargetMachineImpls.h"

namespace {
  cl::opt<std::string>
  InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));

  cl::list<std::string>
  InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));

  cl::opt<std::string>
  MainFunction ("f", cl::desc("Function to execute"), cl::init("main"),
		cl::value_desc("function name"));

  cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing"));

  cl::opt<bool> ForceInterpreter("force-interpreter",
				 cl::desc("Force interpretation: disable JIT"),
				 cl::init(false));
}

//===----------------------------------------------------------------------===//
// main Driver function
//
int main(int argc, char** argv, const char ** envp) {
  cl::ParseCommandLineOptions(argc, argv,
			      " llvm interpreter & dynamic compiler\n");

  // Load the bytecode...
  std::string ErrorMsg;
  Module *M = ParseBytecodeFile(InputFile, &ErrorMsg);
  if (M == 0) {
    std::cout << "Error parsing '" << InputFile << "': "
              << ErrorMsg << "\n";
    exit(1);
  }

  ExecutionEngine *EE =
    ExecutionEngine::create (M, ForceInterpreter, TraceMode);
  assert (EE && "Couldn't create an ExecutionEngine, not even an interpreter?");

  // Add the module's name to the start of the vector of arguments to main().
  // 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 ());
  }
  InputArgv.insert(InputArgv.begin(), InputFile);

  // Run the main function!
  int ExitCode = EE->run(MainFunction, InputArgv, envp);

  // Now that we are done executing the program, shut down the execution engine
  delete EE;
  return ExitCode;
}