| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 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" | 
| Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 11 | #include "Support/CommandLine.h" | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 12 |  | 
| Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame^] | 13 | static cl::opt<string> | 
|  | 14 | InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-")); | 
|  | 15 |  | 
|  | 16 | static cl::list<std::string> | 
|  | 17 | InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>...")); | 
|  | 18 |  | 
|  | 19 | static cl::opt<string> | 
|  | 20 | MainFunction ("f", cl::desc("Function to execute"), cl::init("main"), | 
|  | 21 | cl::value_desc("function name")); | 
|  | 22 |  | 
|  | 23 | static cl::opt<bool> | 
|  | 24 | DebugMode("debug", cl::desc("Start program in debugger")); | 
|  | 25 |  | 
|  | 26 | static cl::alias | 
|  | 27 | DebugModeA("d", cl::desc("Alias for -debug"), cl::aliasopt(DebugMode)); | 
|  | 28 |  | 
|  | 29 | static cl::opt<bool> | 
|  | 30 | TraceMode("trace", cl::desc("Enable Tracing")); | 
|  | 31 |  | 
|  | 32 | static cl::opt<bool> | 
|  | 33 | ProfileMode("profile", cl::desc("Enable Profiling [unimp]")); | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 34 |  | 
| Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 35 |  | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// | 
|  | 37 | // Interpreter ctor - Initialize stuff | 
|  | 38 | // | 
| Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 39 | Interpreter::Interpreter() : ExitCode(0), Profile(ProfileMode), | 
|  | 40 | Trace(TraceMode), CurFrame(-1) { | 
| Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 41 | CurMod = 0; | 
| Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame^] | 42 | loadModule(InputFile); | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 43 |  | 
|  | 44 | // Initialize the "backend" | 
|  | 45 | initializeExecutionEngine(); | 
| Chris Lattner | 2b2e5b3 | 2001-10-30 20:28:46 +0000 | [diff] [blame] | 46 | initializeExternalMethods(); | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
|  | 49 | //===----------------------------------------------------------------------===// | 
|  | 50 | // main Driver function | 
|  | 51 | // | 
|  | 52 | int main(int argc, char** argv) { | 
|  | 53 | cl::ParseCommandLineOptions(argc, argv, " llvm interpreter\n"); | 
|  | 54 |  | 
| Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame^] | 55 | // Add the module name to the start of the argv vector... | 
|  | 56 | // | 
|  | 57 | InputArgv.insert(InputArgv.begin(), InputFile); | 
|  | 58 |  | 
|  | 59 |  | 
|  | 60 |  | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 61 | // Create the interpreter... | 
|  | 62 | Interpreter I; | 
|  | 63 |  | 
|  | 64 | // Handle alternate names of the program.  If started as llp, enable profiling | 
|  | 65 | // if started as ldb, enable debugging... | 
|  | 66 | // | 
|  | 67 | if (argv[0] == "ldb")       // TODO: Obviously incorrect, but you get the idea | 
|  | 68 | DebugMode = true; | 
|  | 69 | else if (argv[0] == "llp") | 
|  | 70 | ProfileMode = true; | 
|  | 71 |  | 
|  | 72 | // If running with the profiler, enable it now... | 
|  | 73 | if (ProfileMode) I.enableProfiling(); | 
| Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 74 | if (TraceMode) I.enableTracing(); | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 75 |  | 
|  | 76 | // Start interpreter into the main function... | 
|  | 77 | // | 
| Chris Lattner | 204eec3 | 2001-10-27 05:54:31 +0000 | [diff] [blame] | 78 | if (!I.callMainMethod(MainFunction, InputArgv) && !DebugMode) { | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 79 | // If not in debug mode and if the call succeeded, run the code now... | 
|  | 80 | I.run(); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | // If debug mode, allow the user to interact... also, if the user pressed | 
|  | 84 | // ctrl-c or execution hit an error, enter the event loop... | 
|  | 85 | if (DebugMode || I.isStopped()) | 
|  | 86 | I.handleUserInput(); | 
|  | 87 |  | 
|  | 88 | // Return the status code of the program executed... | 
|  | 89 | return I.getExitCode(); | 
|  | 90 | } |