summaryrefslogtreecommitdiff
path: root/tools/lli/lli.cpp
blob: dc3519454608114c9d14fedb1626d5edd42783b9 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//===----------------------------------------------------------------------===//
// LLVM INTERPRETER/DEBUGGER/PROFILER UTILITY 
//
// This utility is an interactive frontend to almost all other LLVM
// functionality.  It may be used as an interpreter to run code, a debugger to
// find problems, or a profiler to analyze execution frequencies.
//
//===----------------------------------------------------------------------===//

#include "Interpreter.h"
#include "Support/CommandLine.h"

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

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

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

static cl::opt<bool>
DebugMode("debug", cl::desc("Start program in debugger"));

static cl::alias
DebugModeA("d", cl::desc("Alias for -debug"), cl::aliasopt(DebugMode));

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

static cl::opt<bool>
ProfileMode("profile", cl::desc("Enable Profiling [unimp]"));


//===----------------------------------------------------------------------===//
// Interpreter ctor - Initialize stuff
//
Interpreter::Interpreter() : ExitCode(0), Profile(ProfileMode), 
                             Trace(TraceMode), CurFrame(-1) {
  CurMod = 0;
  loadModule(InputFile);

  // Initialize the "backend"
  initializeExecutionEngine();
  initializeExternalMethods();
}

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

  // Add the module name to the start of the argv vector...
  //
  InputArgv.insert(InputArgv.begin(), InputFile);



  // Create the interpreter...
  Interpreter I;

  // Handle alternate names of the program.  If started as llp, enable profiling
  // if started as ldb, enable debugging...
  //
  if (argv[0] == "ldb")       // TODO: Obviously incorrect, but you get the idea
    DebugMode = true;
  else if (argv[0] == "llp")
    ProfileMode = true;

  // If running with the profiler, enable it now...
  if (ProfileMode) I.enableProfiling();
  if (TraceMode) I.enableTracing();

  // Start interpreter into the main function...
  //
  if (!I.callMainMethod(MainFunction, InputArgv) && !DebugMode) {
    // If not in debug mode and if the call succeeded, run the code now...
    I.run();
  }

  // If debug mode, allow the user to interact... also, if the user pressed 
  // ctrl-c or execution hit an error, enter the event loop...
  if (DebugMode || I.isStopped())
    I.handleUserInput();

  // Return the status code of the program executed...
  return I.getExitCode();
}