blob: 3573d17fb4b3ce3fc04a55cea408bed5679f8a48 [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
Chris Lattnere2409062001-11-12 16:19:45 +000010// Uncomment this line to enable profiling of structure field accesses.
Chris Lattner849735c2002-10-02 21:11:16 +000011//#define PROFILE_STRUCTURE_FIELDS 1
Chris Lattnere2409062001-11-12 16:19:45 +000012
Chris Lattner92101ac2001-08-23 17:05:04 +000013#include "llvm/Module.h"
Chris Lattner360e17e2001-11-26 23:04:08 +000014#include "Support/DataTypes.h"
Chris Lattner5af0c482001-11-07 04:23:00 +000015#include "llvm/Assembly/CachedWriter.h"
16
17extern CachedWriter CW; // Object to accellerate printing of LLVM
Chris Lattner92101ac2001-08-23 17:05:04 +000018
19struct MethodInfo; // Defined in ExecutionAnnotations.h
20class CallInst;
21class ReturnInst;
22class BranchInst;
Chris Lattner86660982001-08-27 05:16:50 +000023class AllocationInst;
Chris Lattner92101ac2001-08-23 17:05:04 +000024
Chris Lattnerea38c0e2001-11-07 19:46:27 +000025typedef uint64_t PointerTy;
26
Chris Lattner92101ac2001-08-23 17:05:04 +000027union GenericValue {
28 bool BoolVal;
29 unsigned char UByteVal;
30 signed char SByteVal;
31 unsigned short UShortVal;
32 signed short ShortVal;
33 unsigned int UIntVal;
34 signed int IntVal;
Chris Lattner7b851ab2001-10-15 19:18:26 +000035 uint64_t ULongVal;
36 int64_t LongVal;
Chris Lattner92101ac2001-08-23 17:05:04 +000037 double DoubleVal;
38 float FloatVal;
Chris Lattnerea38c0e2001-11-07 19:46:27 +000039 PointerTy PointerVal;
Chris Lattner92101ac2001-08-23 17:05:04 +000040};
41
Chris Lattner9bffa732002-02-19 18:50:09 +000042// AllocaHolder - Object to track all of the blocks of memory allocated by
43// alloca. When the function returns, this object is poped off the execution
44// stack, which causes the dtor to be run, which frees all the alloca'd memory.
45//
46class AllocaHolder {
47 friend class AllocaHolderHandle;
48 std::vector<void*> Allocations;
49 unsigned RefCnt;
50public:
51 AllocaHolder() : RefCnt(0) {}
52 void add(void *mem) { Allocations.push_back(mem); }
53 ~AllocaHolder() {
54 for (unsigned i = 0; i < Allocations.size(); ++i)
55 free(Allocations[i]);
56 }
57};
58
59// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
60// a vector...
61//
62class AllocaHolderHandle {
63 AllocaHolder *H;
64public:
65 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
66 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
67 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
68
69 void add(void *mem) { H->add(mem); }
70};
71
Chris Lattner697954c2002-01-20 22:54:45 +000072typedef std::vector<GenericValue> ValuePlaneTy;
Chris Lattner92101ac2001-08-23 17:05:04 +000073
74// ExecutionContext struct - This struct represents one stack frame currently
75// executing.
76//
77struct ExecutionContext {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000078 Function *CurMethod; // The currently executing function
Chris Lattner92101ac2001-08-23 17:05:04 +000079 BasicBlock *CurBB; // The currently executing BB
80 BasicBlock::iterator CurInst; // The next instruction to execute
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000081 MethodInfo *MethInfo; // The MethInfo annotation for the function
Chris Lattner697954c2002-01-20 22:54:45 +000082 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
Chris Lattner92101ac2001-08-23 17:05:04 +000083
84 BasicBlock *PrevBB; // The previous BB or null if in first BB
85 CallInst *Caller; // Holds the call that called subframes.
86 // NULL if main func or debugger invoked fn
Chris Lattner9bffa732002-02-19 18:50:09 +000087 AllocaHolderHandle Allocas; // Track memory allocated by alloca
Chris Lattner92101ac2001-08-23 17:05:04 +000088};
89
Chris Lattner92101ac2001-08-23 17:05:04 +000090// Interpreter - This class represents the entirety of the interpreter.
91//
92class Interpreter {
93 Module *CurMod; // The current Module being executed (0 if none)
94 int ExitCode; // The exit code to be returned by the lli util
95 bool Profile; // Profiling enabled?
Chris Lattner43e3f7c2001-10-27 08:43:52 +000096 bool Trace; // Tracing enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000097 int CurFrame; // The current stack frame being inspected
98
99 // The runtime stack of executing code. The top of the stack is the current
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000100 // function record.
Chris Lattner697954c2002-01-20 22:54:45 +0000101 std::vector<ExecutionContext> ECStack;
Chris Lattner92101ac2001-08-23 17:05:04 +0000102
103public:
104 Interpreter();
Chris Lattner5af0c482001-11-07 04:23:00 +0000105 inline ~Interpreter() { CW.setModule(0); delete CurMod; }
Chris Lattner92101ac2001-08-23 17:05:04 +0000106
107 // getExitCode - return the code that should be the exit code for the lli
108 // utility.
109 inline int getExitCode() const { return ExitCode; }
Chris Lattner849735c2002-10-02 21:11:16 +0000110 inline Module *getModule() const { return CurMod; }
Chris Lattner92101ac2001-08-23 17:05:04 +0000111
112 // enableProfiling() - Turn profiling on, clear stats?
113 void enableProfiling() { Profile = true; }
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000114 void enableTracing() { Trace = true; }
Chris Lattner92101ac2001-08-23 17:05:04 +0000115
Chris Lattner92101ac2001-08-23 17:05:04 +0000116 void handleUserInput();
117
118 // User Interation Methods...
Chris Lattner697954c2002-01-20 22:54:45 +0000119 void loadModule(const std::string &Filename);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000120 bool flushModule();
Chris Lattner697954c2002-01-20 22:54:45 +0000121 bool callMethod(const std::string &Name); // return true on failure
122 void setBreakpoint(const std::string &Name);
123 void infoValue(const std::string &Name);
124 void print(const std::string &Name);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000125 static void print(const Type *Ty, GenericValue V);
Chris Lattner365a76e2001-09-10 04:49:44 +0000126 static void printValue(const Type *Ty, GenericValue V);
Chris Lattner92101ac2001-08-23 17:05:04 +0000127
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000128 // Hack until we can parse command line args...
Chris Lattner697954c2002-01-20 22:54:45 +0000129 bool callMainMethod(const std::string &MainName,
130 const std::vector<std::string> &InputFilename);
Chris Lattner92101ac2001-08-23 17:05:04 +0000131
132 void list(); // Do the 'list' command
133 void printStackTrace(); // Do the 'backtrace' command
134
135 // Code execution methods...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000136 void callMethod(Function *F, const std::vector<GenericValue> &ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000137 bool executeInstruction(); // Execute one instruction...
138
139 void stepInstruction(); // Do the 'step' command
140 void nextInstruction(); // Do the 'next' command
141 void run(); // Do the 'run' command
142 void finish(); // Do the 'finish' command
143
144 // Opcode Implementations
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000145 void executeCallInst(CallInst &I, ExecutionContext &SF);
146 void executeRetInst(ReturnInst &I, ExecutionContext &SF);
147 void executeBrInst(BranchInst &I, ExecutionContext &SF);
148 void executeAllocInst(AllocationInst &I, ExecutionContext &SF);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000149 GenericValue callExternalMethod(Function *F,
Chris Lattner697954c2002-01-20 22:54:45 +0000150 const std::vector<GenericValue> &ArgVals);
Chris Lattnere43db882001-10-27 04:15:57 +0000151 void exitCalled(GenericValue GV);
Chris Lattner92101ac2001-08-23 17:05:04 +0000152
153 // getCurrentMethod - Return the currently executing method
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000154 inline Function *getCurrentMethod() const {
Chris Lattner92101ac2001-08-23 17:05:04 +0000155 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
156 }
157
158 // isStopped - Return true if a program is stopped. Return false if no
159 // program is running.
160 //
161 inline bool isStopped() const { return !ECStack.empty(); }
162
163private: // Helper functions
Chris Lattnere43db882001-10-27 04:15:57 +0000164 // getCurrentExecutablePath() - Return the directory that the lli executable
165 // lives in.
166 //
Chris Lattner697954c2002-01-20 22:54:45 +0000167 std::string getCurrentExecutablePath() const;
Chris Lattnere43db882001-10-27 04:15:57 +0000168
Chris Lattner92101ac2001-08-23 17:05:04 +0000169 // printCurrentInstruction - Print out the instruction that the virtual PC is
170 // at, or fail silently if no program is running.
171 //
172 void printCurrentInstruction();
173
Chris Lattner461f02f2001-11-07 05:31:27 +0000174 // printStackFrame - Print information about the specified stack frame, or -1
175 // for the default one.
176 //
177 void printStackFrame(int FrameNo = -1);
178
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000179 // LookupMatchingNames - Search the current function namespace, then the
180 // global namespace looking for values that match the specified name. Return
181 // ALL matches to that name. This is obviously slow, and should only be used
182 // for user interaction.
Chris Lattner92101ac2001-08-23 17:05:04 +0000183 //
Chris Lattner697954c2002-01-20 22:54:45 +0000184 std::vector<Value*> LookupMatchingNames(const std::string &Name);
Chris Lattner92101ac2001-08-23 17:05:04 +0000185
186 // ChooseOneOption - Prompt the user to choose among the specified options to
187 // pick one value. If no options are provided, emit an error. If a single
188 // option is provided, just return that option.
189 //
Chris Lattner697954c2002-01-20 22:54:45 +0000190 Value *ChooseOneOption(const std::string &Name,
191 const std::vector<Value*> &Opts);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000192
193
194 void initializeExecutionEngine();
195 void initializeExternalMethods();
Chris Lattner92101ac2001-08-23 17:05:04 +0000196};
197
198#endif