Cleanup
[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 "Support/CommandLine.h"
12
13 cl::StringList InputArgv(""   , "Input command line", cl::ConsumeAfter);
14 cl::String MainFunction ("f"      , "Function to execute", cl::NoFlags, "main");
15 cl::Flag   DebugMode    ("debug"  , "Start program in debugger");
16 cl::Alias  DebugModeA   ("d"      , "Alias for -debug", cl::NoFlags, DebugMode);
17 cl::Flag   TraceMode    ("trace"  , "Enable Tracing");
18 cl::Flag   ProfileMode  ("profile", "Enable Profiling [unimp]");
19
20
21 //===----------------------------------------------------------------------===//
22 // Interpreter ctor - Initialize stuff
23 //
24 Interpreter::Interpreter() : ExitCode(0), Profile(ProfileMode), 
25                              Trace(TraceMode), CurFrame(-1) {
26   CurMod = 0;
27   loadModule(InputArgv.size() ? InputArgv[0] : "-");
28
29   // Initialize the "backend"
30   initializeExecutionEngine();
31   initializeExternalMethods();
32 }
33
34 //===----------------------------------------------------------------------===//
35 // main Driver function
36 //
37 int main(int argc, char** argv) {
38   cl::ParseCommandLineOptions(argc, argv, " llvm interpreter\n");
39
40   // Create the interpreter...
41   Interpreter I;
42
43   // Handle alternate names of the program.  If started as llp, enable profiling
44   // if started as ldb, enable debugging...
45   //
46   if (argv[0] == "ldb")       // TODO: Obviously incorrect, but you get the idea
47     DebugMode = true;
48   else if (argv[0] == "llp")
49     ProfileMode = true;
50
51   // If running with the profiler, enable it now...
52   if (ProfileMode) I.enableProfiling();
53   if (TraceMode) I.enableTracing();
54
55   // Ensure that there is at least one argument... the name of the program.
56   // This is only unavailable if the program was read from stdin, instead of a
57   // file.
58   //
59   if (InputArgv.empty())
60     InputArgv.push_back("from-stdin-prog");
61
62   // Start interpreter into the main function...
63   //
64   if (!I.callMainMethod(MainFunction, InputArgv) && !DebugMode) {
65     // If not in debug mode and if the call succeeded, run the code now...
66     I.run();
67   }
68
69   // If debug mode, allow the user to interact... also, if the user pressed 
70   // ctrl-c or execution hit an error, enter the event loop...
71   if (DebugMode || I.isStopped())
72     I.handleUserInput();
73
74   // Return the status code of the program executed...
75   return I.getExitCode();
76 }