blob: 23354e38184c0ce3a40ce5fef2f9a26f9fdb8caf [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Interpreter.h ------------------------------------------*- C++ -*--===//
2//
3// This header file defines the interpreter structure
4//
5//===----------------------------------------------------------------------===//
6
7#ifndef LLI_INTERPRETER_H
8#define LLI_INTERPRETER_H
9
10#include "llvm/Module.h"
11#include "llvm/Method.h"
Chris Lattner7b851ab2001-10-15 19:18:26 +000012#include "llvm/Support/DataTypes.h"
Chris Lattner5af0c482001-11-07 04:23:00 +000013#include "llvm/Assembly/CachedWriter.h"
14
15extern CachedWriter CW; // Object to accellerate printing of LLVM
Chris Lattner92101ac2001-08-23 17:05:04 +000016
17struct MethodInfo; // Defined in ExecutionAnnotations.h
18class CallInst;
19class ReturnInst;
20class BranchInst;
Chris Lattner86660982001-08-27 05:16:50 +000021class AllocationInst;
Chris Lattner92101ac2001-08-23 17:05:04 +000022
23union GenericValue {
24 bool BoolVal;
25 unsigned char UByteVal;
26 signed char SByteVal;
27 unsigned short UShortVal;
28 signed short ShortVal;
29 unsigned int UIntVal;
30 signed int IntVal;
Chris Lattner7b851ab2001-10-15 19:18:26 +000031 uint64_t ULongVal;
32 int64_t LongVal;
Chris Lattner92101ac2001-08-23 17:05:04 +000033 double DoubleVal;
34 float FloatVal;
Chris Lattnerc2593162001-10-27 08:28:11 +000035 uint64_t PointerVal;
Chris Lattner92101ac2001-08-23 17:05:04 +000036};
37
38typedef vector<GenericValue> ValuePlaneTy;
39
40// ExecutionContext struct - This struct represents one stack frame currently
41// executing.
42//
43struct ExecutionContext {
44 Method *CurMethod; // The currently executing method
45 BasicBlock *CurBB; // The currently executing BB
46 BasicBlock::iterator CurInst; // The next instruction to execute
47 MethodInfo *MethInfo; // The MethInfo annotation for the method
48 vector<ValuePlaneTy> Values; // ValuePlanes for each type
49
50 BasicBlock *PrevBB; // The previous BB or null if in first BB
51 CallInst *Caller; // Holds the call that called subframes.
52 // NULL if main func or debugger invoked fn
53};
54
55
56// Interpreter - This class represents the entirety of the interpreter.
57//
58class Interpreter {
59 Module *CurMod; // The current Module being executed (0 if none)
60 int ExitCode; // The exit code to be returned by the lli util
61 bool Profile; // Profiling enabled?
Chris Lattner43e3f7c2001-10-27 08:43:52 +000062 bool Trace; // Tracing enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000063 int CurFrame; // The current stack frame being inspected
64
65 // The runtime stack of executing code. The top of the stack is the current
66 // method record.
67 vector<ExecutionContext> ECStack;
68
69public:
70 Interpreter();
Chris Lattner5af0c482001-11-07 04:23:00 +000071 inline ~Interpreter() { CW.setModule(0); delete CurMod; }
Chris Lattner92101ac2001-08-23 17:05:04 +000072
73 // getExitCode - return the code that should be the exit code for the lli
74 // utility.
75 inline int getExitCode() const { return ExitCode; }
76
77 // enableProfiling() - Turn profiling on, clear stats?
78 void enableProfiling() { Profile = true; }
Chris Lattner43e3f7c2001-10-27 08:43:52 +000079 void enableTracing() { Trace = true; }
Chris Lattner92101ac2001-08-23 17:05:04 +000080
Chris Lattner92101ac2001-08-23 17:05:04 +000081 void handleUserInput();
82
83 // User Interation Methods...
Chris Lattner2e42d3a2001-10-15 05:51:48 +000084 void loadModule(const string &Filename);
85 bool flushModule();
Chris Lattner92101ac2001-08-23 17:05:04 +000086 bool callMethod(const string &Name); // return true on failure
87 void setBreakpoint(const string &Name);
Chris Lattner86660982001-08-27 05:16:50 +000088 void infoValue(const string &Name);
Chris Lattner2e42d3a2001-10-15 05:51:48 +000089 void print(const string &Name);
90 static void print(const Type *Ty, GenericValue V);
Chris Lattner365a76e2001-09-10 04:49:44 +000091 static void printValue(const Type *Ty, GenericValue V);
Chris Lattner92101ac2001-08-23 17:05:04 +000092
Chris Lattnerf8f2afb2001-10-18 21:55:32 +000093 // Hack until we can parse command line args...
94 bool callMainMethod(const string &MainName,
Chris Lattner204eec32001-10-27 05:54:31 +000095 const vector<string> &InputFilename);
Chris Lattner92101ac2001-08-23 17:05:04 +000096
97 void list(); // Do the 'list' command
98 void printStackTrace(); // Do the 'backtrace' command
99
100 // Code execution methods...
Chris Lattner365a76e2001-09-10 04:49:44 +0000101 void callMethod (Method *Meth, const vector<GenericValue> &ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000102 bool executeInstruction(); // Execute one instruction...
103
104 void stepInstruction(); // Do the 'step' command
105 void nextInstruction(); // Do the 'next' command
106 void run(); // Do the 'run' command
107 void finish(); // Do the 'finish' command
108
109 // Opcode Implementations
110 void executeCallInst(CallInst *I, ExecutionContext &SF);
111 void executeRetInst(ReturnInst *I, ExecutionContext &SF);
112 void executeBrInst(BranchInst *I, ExecutionContext &SF);
Chris Lattner86660982001-08-27 05:16:50 +0000113 void executeAllocInst(AllocationInst *I, ExecutionContext &SF);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000114 GenericValue callExternalMethod(Method *Meth,
115 const vector<GenericValue> &ArgVals);
Chris Lattnere43db882001-10-27 04:15:57 +0000116 void exitCalled(GenericValue GV);
Chris Lattner92101ac2001-08-23 17:05:04 +0000117
118 // getCurrentMethod - Return the currently executing method
119 inline Method *getCurrentMethod() const {
120 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
121 }
122
123 // isStopped - Return true if a program is stopped. Return false if no
124 // program is running.
125 //
126 inline bool isStopped() const { return !ECStack.empty(); }
127
128private: // Helper functions
Chris Lattnere43db882001-10-27 04:15:57 +0000129 // getCurrentExecutablePath() - Return the directory that the lli executable
130 // lives in.
131 //
132 string getCurrentExecutablePath() const;
133
Chris Lattner92101ac2001-08-23 17:05:04 +0000134 // printCurrentInstruction - Print out the instruction that the virtual PC is
135 // at, or fail silently if no program is running.
136 //
137 void printCurrentInstruction();
138
139 // LookupMatchingNames - Search the current method namespace, then the global
140 // namespace looking for values that match the specified name. Return ALL
141 // matches to that name. This is obviously slow, and should only be used for
142 // user interaction.
143 //
144 vector<Value*> LookupMatchingNames(const string &Name);
145
146 // ChooseOneOption - Prompt the user to choose among the specified options to
147 // pick one value. If no options are provided, emit an error. If a single
148 // option is provided, just return that option.
149 //
150 Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000151
152
153 void initializeExecutionEngine();
154 void initializeExternalMethods();
Chris Lattner92101ac2001-08-23 17:05:04 +0000155};
156
157#endif