Initial checkin of interpreter
[oota-llvm.git] / tools / lli / lli.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM INTERPRETER/DEBUGGER/PROFILER UTILITY 
3 //
4 // This utility is an interactive frontend to almost all other LLVM
5 // functionality.  It may be used as an interpreter to run code, a debugger to
6 // find problems, or a profiler to analyze execution frequencies.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "Interpreter.h"
11 #include "llvm/Support/CommandLine.h"
12 #include "llvm/Bytecode/Reader.h"
13
14 cl::String InputFilename(""       , "Input filename", cl::NoFlags, "-");
15 cl::String MainFunction ("f"      , "Function to execute", cl::NoFlags, "main");
16 cl::Flag   DebugMode    ("debug"  , "Start program in debugger");
17 cl::Alias  DebugModeA   ("d"      , "Alias for -debug", cl::NoFlags, DebugMode);
18 cl::Flag   ProfileMode  ("profile", "Enable Profiling [unimp]");
19
20 //===----------------------------------------------------------------------===//
21 // Interpreter ctor - Initialize stuff
22 //
23 Interpreter::Interpreter() : ExitCode(0), Profile(ProfileMode), CurFrame(-1) {
24   CurMod = ParseBytecodeFile(InputFilename);
25   if (CurMod == 0) {
26     cout << "Error parsing '" << InputFilename << "': No module loaded.\n";
27   }
28
29   // Initialize the "backend"
30   initializeExecutionEngine();
31 }
32
33 //===----------------------------------------------------------------------===//
34 // main Driver function
35 //
36 int main(int argc, char** argv) {
37   cl::ParseCommandLineOptions(argc, argv, " llvm interpreter\n");
38
39   // Create the interpreter...
40   Interpreter I;
41
42   // Handle alternate names of the program.  If started as llp, enable profiling
43   // if started as ldb, enable debugging...
44   //
45   if (argv[0] == "ldb")       // TODO: Obviously incorrect, but you get the idea
46     DebugMode = true;
47   else if (argv[0] == "llp")
48     ProfileMode = true;
49
50   // If running with the profiler, enable it now...
51   if (ProfileMode) I.enableProfiling();
52
53   // Start interpreter into the main function...
54   //
55   if (!I.callMethod(MainFunction) && !DebugMode) {
56     // If not in debug mode and if the call succeeded, run the code now...
57     I.run();
58   }
59
60   // If debug mode, allow the user to interact... also, if the user pressed 
61   // ctrl-c or execution hit an error, enter the event loop...
62   if (DebugMode || I.isStopped())
63     I.handleUserInput();
64
65   // Return the status code of the program executed...
66   return I.getExitCode();
67 }