blob: 3f718e599e872468357207573a8d9302f79c701c [file] [log] [blame]
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001//===----------------------------------------------------------------------===//
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 Lattner5de22042001-11-27 00:03:19 +000011#include "Support/CommandLine.h"
Chris Lattnerd7ff5782001-08-23 17:05:04 +000012
Chris Lattner6d044b52002-07-25 16:39:56 +000013static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000014InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
15
16static cl::list<std::string>
17InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
18
Chris Lattner6d044b52002-07-25 16:39:56 +000019static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000020MainFunction ("f", cl::desc("Function to execute"), cl::init("main"),
21 cl::value_desc("function name"));
22
23static cl::opt<bool>
Chris Lattner4f96aa42002-12-08 05:51:08 +000024DebugMode("d", cl::desc("Start program in debugger"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000025
26static cl::opt<bool>
27TraceMode("trace", cl::desc("Enable Tracing"));
28
29static cl::opt<bool>
30ProfileMode("profile", cl::desc("Enable Profiling [unimp]"));
Chris Lattnerd7ff5782001-08-23 17:05:04 +000031
Chris Lattner009f8102001-10-27 08:43:52 +000032
Chris Lattnerd7ff5782001-08-23 17:05:04 +000033//===----------------------------------------------------------------------===//
34// Interpreter ctor - Initialize stuff
35//
Chris Lattner009f8102001-10-27 08:43:52 +000036Interpreter::Interpreter() : ExitCode(0), Profile(ProfileMode),
37 Trace(TraceMode), CurFrame(-1) {
Chris Lattnerc62e2e52001-10-15 05:51:48 +000038 CurMod = 0;
Chris Lattnerf5cad152002-07-22 02:10:13 +000039 loadModule(InputFile);
Chris Lattnerd7ff5782001-08-23 17:05:04 +000040
41 // Initialize the "backend"
42 initializeExecutionEngine();
Chris Lattner3f741a52001-10-30 20:28:46 +000043 initializeExternalMethods();
Chris Lattnerd7ff5782001-08-23 17:05:04 +000044}
45
46//===----------------------------------------------------------------------===//
47// main Driver function
48//
49int main(int argc, char** argv) {
50 cl::ParseCommandLineOptions(argc, argv, " llvm interpreter\n");
51
Chris Lattnerf5cad152002-07-22 02:10:13 +000052 // Add the module name to the start of the argv vector...
53 //
54 InputArgv.insert(InputArgv.begin(), InputFile);
55
Chris Lattnerd7ff5782001-08-23 17:05:04 +000056 // Create the interpreter...
57 Interpreter I;
58
59 // Handle alternate names of the program. If started as llp, enable profiling
60 // if started as ldb, enable debugging...
61 //
62 if (argv[0] == "ldb") // TODO: Obviously incorrect, but you get the idea
63 DebugMode = true;
64 else if (argv[0] == "llp")
65 ProfileMode = true;
66
67 // If running with the profiler, enable it now...
68 if (ProfileMode) I.enableProfiling();
Chris Lattner009f8102001-10-27 08:43:52 +000069 if (TraceMode) I.enableTracing();
Chris Lattnerd7ff5782001-08-23 17:05:04 +000070
71 // Start interpreter into the main function...
72 //
Chris Lattner6642eec2001-10-27 05:54:31 +000073 if (!I.callMainMethod(MainFunction, InputArgv) && !DebugMode) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +000074 // If not in debug mode and if the call succeeded, run the code now...
75 I.run();
76 }
77
78 // If debug mode, allow the user to interact... also, if the user pressed
79 // ctrl-c or execution hit an error, enter the event loop...
80 if (DebugMode || I.isStopped())
81 I.handleUserInput();
82
83 // Return the status code of the program executed...
84 return I.getExitCode();
85}