blob: eba8ea9c327de575e8b7b64cb879fc5a5ae369df [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
Chris Lattnerea38c0e2001-11-07 19:46:27 +000023typedef uint64_t PointerTy;
24
Chris Lattner92101ac2001-08-23 17:05:04 +000025union GenericValue {
26 bool BoolVal;
27 unsigned char UByteVal;
28 signed char SByteVal;
29 unsigned short UShortVal;
30 signed short ShortVal;
31 unsigned int UIntVal;
32 signed int IntVal;
Chris Lattner7b851ab2001-10-15 19:18:26 +000033 uint64_t ULongVal;
34 int64_t LongVal;
Chris Lattner92101ac2001-08-23 17:05:04 +000035 double DoubleVal;
36 float FloatVal;
Chris Lattnerea38c0e2001-11-07 19:46:27 +000037 PointerTy PointerVal;
Chris Lattner92101ac2001-08-23 17:05:04 +000038};
39
40typedef vector<GenericValue> ValuePlaneTy;
41
42// ExecutionContext struct - This struct represents one stack frame currently
43// executing.
44//
45struct ExecutionContext {
46 Method *CurMethod; // The currently executing method
47 BasicBlock *CurBB; // The currently executing BB
48 BasicBlock::iterator CurInst; // The next instruction to execute
49 MethodInfo *MethInfo; // The MethInfo annotation for the method
50 vector<ValuePlaneTy> Values; // ValuePlanes for each type
51
52 BasicBlock *PrevBB; // The previous BB or null if in first BB
53 CallInst *Caller; // Holds the call that called subframes.
54 // NULL if main func or debugger invoked fn
55};
56
57
58// Interpreter - This class represents the entirety of the interpreter.
59//
60class Interpreter {
61 Module *CurMod; // The current Module being executed (0 if none)
62 int ExitCode; // The exit code to be returned by the lli util
63 bool Profile; // Profiling enabled?
Chris Lattner43e3f7c2001-10-27 08:43:52 +000064 bool Trace; // Tracing enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000065 int CurFrame; // The current stack frame being inspected
66
67 // The runtime stack of executing code. The top of the stack is the current
68 // method record.
69 vector<ExecutionContext> ECStack;
70
71public:
72 Interpreter();
Chris Lattner5af0c482001-11-07 04:23:00 +000073 inline ~Interpreter() { CW.setModule(0); delete CurMod; }
Chris Lattner92101ac2001-08-23 17:05:04 +000074
75 // getExitCode - return the code that should be the exit code for the lli
76 // utility.
77 inline int getExitCode() const { return ExitCode; }
78
79 // enableProfiling() - Turn profiling on, clear stats?
80 void enableProfiling() { Profile = true; }
Chris Lattner43e3f7c2001-10-27 08:43:52 +000081 void enableTracing() { Trace = true; }
Chris Lattner92101ac2001-08-23 17:05:04 +000082
Chris Lattner92101ac2001-08-23 17:05:04 +000083 void handleUserInput();
84
85 // User Interation Methods...
Chris Lattner2e42d3a2001-10-15 05:51:48 +000086 void loadModule(const string &Filename);
87 bool flushModule();
Chris Lattner92101ac2001-08-23 17:05:04 +000088 bool callMethod(const string &Name); // return true on failure
89 void setBreakpoint(const string &Name);
Chris Lattner86660982001-08-27 05:16:50 +000090 void infoValue(const string &Name);
Chris Lattner2e42d3a2001-10-15 05:51:48 +000091 void print(const string &Name);
92 static void print(const Type *Ty, GenericValue V);
Chris Lattner365a76e2001-09-10 04:49:44 +000093 static void printValue(const Type *Ty, GenericValue V);
Chris Lattner92101ac2001-08-23 17:05:04 +000094
Chris Lattnerf8f2afb2001-10-18 21:55:32 +000095 // Hack until we can parse command line args...
96 bool callMainMethod(const string &MainName,
Chris Lattner204eec32001-10-27 05:54:31 +000097 const vector<string> &InputFilename);
Chris Lattner92101ac2001-08-23 17:05:04 +000098
99 void list(); // Do the 'list' command
100 void printStackTrace(); // Do the 'backtrace' command
101
102 // Code execution methods...
Chris Lattner365a76e2001-09-10 04:49:44 +0000103 void callMethod (Method *Meth, const vector<GenericValue> &ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000104 bool executeInstruction(); // Execute one instruction...
105
106 void stepInstruction(); // Do the 'step' command
107 void nextInstruction(); // Do the 'next' command
108 void run(); // Do the 'run' command
109 void finish(); // Do the 'finish' command
110
111 // Opcode Implementations
112 void executeCallInst(CallInst *I, ExecutionContext &SF);
113 void executeRetInst(ReturnInst *I, ExecutionContext &SF);
114 void executeBrInst(BranchInst *I, ExecutionContext &SF);
Chris Lattner86660982001-08-27 05:16:50 +0000115 void executeAllocInst(AllocationInst *I, ExecutionContext &SF);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000116 GenericValue callExternalMethod(Method *Meth,
117 const vector<GenericValue> &ArgVals);
Chris Lattnere43db882001-10-27 04:15:57 +0000118 void exitCalled(GenericValue GV);
Chris Lattner92101ac2001-08-23 17:05:04 +0000119
120 // getCurrentMethod - Return the currently executing method
121 inline Method *getCurrentMethod() const {
122 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
123 }
124
125 // isStopped - Return true if a program is stopped. Return false if no
126 // program is running.
127 //
128 inline bool isStopped() const { return !ECStack.empty(); }
129
130private: // Helper functions
Chris Lattnere43db882001-10-27 04:15:57 +0000131 // getCurrentExecutablePath() - Return the directory that the lli executable
132 // lives in.
133 //
134 string getCurrentExecutablePath() const;
135
Chris Lattner92101ac2001-08-23 17:05:04 +0000136 // printCurrentInstruction - Print out the instruction that the virtual PC is
137 // at, or fail silently if no program is running.
138 //
139 void printCurrentInstruction();
140
Chris Lattner461f02f2001-11-07 05:31:27 +0000141 // printStackFrame - Print information about the specified stack frame, or -1
142 // for the default one.
143 //
144 void printStackFrame(int FrameNo = -1);
145
Chris Lattner92101ac2001-08-23 17:05:04 +0000146 // LookupMatchingNames - Search the current method namespace, then the global
147 // namespace looking for values that match the specified name. Return ALL
148 // matches to that name. This is obviously slow, and should only be used for
149 // user interaction.
150 //
151 vector<Value*> LookupMatchingNames(const string &Name);
152
153 // ChooseOneOption - Prompt the user to choose among the specified options to
154 // pick one value. If no options are provided, emit an error. If a single
155 // option is provided, just return that option.
156 //
157 Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000158
159
160 void initializeExecutionEngine();
161 void initializeExternalMethods();
Chris Lattner92101ac2001-08-23 17:05:04 +0000162};
163
164#endif