blob: 459904da6db1772c101ea687b860c9a83ed1dd53 [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 Lattnerfe11a972002-12-23 23:59:41 +000013#include "../ExecutionEngine.h"
Chris Lattnerd7916e92003-05-10 21:22:39 +000014#include "../GenericValue.h"
Chris Lattner360e17e2001-11-26 23:04:08 +000015#include "Support/DataTypes.h"
Chris Lattner5af0c482001-11-07 04:23:00 +000016#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerfe11a972002-12-23 23:59:41 +000017#include "llvm/Target/TargetData.h"
18#include "llvm/BasicBlock.h"
Chris Lattnerd7916e92003-05-10 21:22:39 +000019#include "llvm/Support/InstVisitor.h"
Chris Lattner5af0c482001-11-07 04:23:00 +000020
Chris Lattnerfe11a972002-12-23 23:59:41 +000021extern CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner92101ac2001-08-23 17:05:04 +000022
Chris Lattnerda82ed52003-05-08 16:18:31 +000023struct FunctionInfo; // Defined in ExecutionAnnotations.h
Chris Lattner92101ac2001-08-23 17:05:04 +000024
Chris Lattner9bffa732002-02-19 18:50:09 +000025// AllocaHolder - Object to track all of the blocks of memory allocated by
26// alloca. When the function returns, this object is poped off the execution
27// stack, which causes the dtor to be run, which frees all the alloca'd memory.
28//
29class AllocaHolder {
30 friend class AllocaHolderHandle;
31 std::vector<void*> Allocations;
32 unsigned RefCnt;
33public:
34 AllocaHolder() : RefCnt(0) {}
35 void add(void *mem) { Allocations.push_back(mem); }
36 ~AllocaHolder() {
37 for (unsigned i = 0; i < Allocations.size(); ++i)
38 free(Allocations[i]);
39 }
40};
41
42// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
43// a vector...
44//
45class AllocaHolderHandle {
46 AllocaHolder *H;
47public:
48 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
49 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
50 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
51
52 void add(void *mem) { H->add(mem); }
53};
54
Chris Lattner697954c2002-01-20 22:54:45 +000055typedef std::vector<GenericValue> ValuePlaneTy;
Chris Lattner92101ac2001-08-23 17:05:04 +000056
57// ExecutionContext struct - This struct represents one stack frame currently
58// executing.
59//
60struct ExecutionContext {
Chris Lattnerda82ed52003-05-08 16:18:31 +000061 Function *CurFunction;// The currently executing function
Chris Lattner92101ac2001-08-23 17:05:04 +000062 BasicBlock *CurBB; // The currently executing BB
63 BasicBlock::iterator CurInst; // The next instruction to execute
Chris Lattnerda82ed52003-05-08 16:18:31 +000064 FunctionInfo *FuncInfo; // The FuncInfo annotation for the function
Chris Lattner697954c2002-01-20 22:54:45 +000065 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
Chris Lattnercdf51782003-05-08 16:06:52 +000066 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
Chris Lattner92101ac2001-08-23 17:05:04 +000067
Chris Lattner92101ac2001-08-23 17:05:04 +000068 CallInst *Caller; // Holds the call that called subframes.
69 // NULL if main func or debugger invoked fn
Chris Lattner9bffa732002-02-19 18:50:09 +000070 AllocaHolderHandle Allocas; // Track memory allocated by alloca
Chris Lattner92101ac2001-08-23 17:05:04 +000071};
72
Chris Lattner92101ac2001-08-23 17:05:04 +000073// Interpreter - This class represents the entirety of the interpreter.
74//
Chris Lattnerd7916e92003-05-10 21:22:39 +000075class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
Chris Lattner92101ac2001-08-23 17:05:04 +000076 int ExitCode; // The exit code to be returned by the lli util
Chris Lattnerfe11a972002-12-23 23:59:41 +000077 bool Debug; // Debug mode enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000078 bool Profile; // Profiling enabled?
Chris Lattner43e3f7c2001-10-27 08:43:52 +000079 bool Trace; // Tracing enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000080 int CurFrame; // The current stack frame being inspected
Chris Lattnerfe11a972002-12-23 23:59:41 +000081 TargetData TD;
Chris Lattner92101ac2001-08-23 17:05:04 +000082
83 // The runtime stack of executing code. The top of the stack is the current
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000084 // function record.
Chris Lattner697954c2002-01-20 22:54:45 +000085 std::vector<ExecutionContext> ECStack;
Chris Lattner92101ac2001-08-23 17:05:04 +000086
Chris Lattner44edb6b2003-05-14 14:21:30 +000087 // AtExitHandlers - List of functions to call when the program exits.
88 std::vector<Function*> AtExitHandlers;
Chris Lattner92101ac2001-08-23 17:05:04 +000089public:
Chris Lattner39c07262003-08-24 19:50:53 +000090 Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
91 bool DebugMode, bool TraceMode);
Chris Lattnerfe11a972002-12-23 23:59:41 +000092 inline ~Interpreter() { CW.setModule(0); }
Chris Lattner92101ac2001-08-23 17:05:04 +000093
Brian Gaeke82d82772003-09-03 20:34:19 +000094 /// create - Create an interpreter ExecutionEngine. This can never fail.
95 ///
96 static ExecutionEngine *create(Module *M, bool DebugMode, bool TraceMode);
97
98 /// getExitCode - return the code that should be the exit code for the lli
99 /// utility.
100 ///
Chris Lattner92101ac2001-08-23 17:05:04 +0000101 inline int getExitCode() const { return ExitCode; }
Chris Lattnerfe11a972002-12-23 23:59:41 +0000102
103 /// run - Start execution with the specified function and arguments.
104 ///
105 virtual int run(const std::string &FnName,
John Criswell69582b32003-08-21 21:12:30 +0000106 const std::vector<std::string> &Args,
107 const char ** envp);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000108
Chris Lattner92101ac2001-08-23 17:05:04 +0000109
110 // enableProfiling() - Turn profiling on, clear stats?
111 void enableProfiling() { Profile = true; }
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000112 void enableTracing() { Trace = true; }
Chris Lattner92101ac2001-08-23 17:05:04 +0000113
Chris Lattner92101ac2001-08-23 17:05:04 +0000114 void handleUserInput();
115
116 // User Interation Methods...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000117 bool callFunction(const std::string &Name); // return true on failure
Chris Lattner697954c2002-01-20 22:54:45 +0000118 void setBreakpoint(const std::string &Name);
119 void infoValue(const std::string &Name);
120 void print(const std::string &Name);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000121 static void print(const Type *Ty, GenericValue V);
Chris Lattner365a76e2001-09-10 04:49:44 +0000122 static void printValue(const Type *Ty, GenericValue V);
Chris Lattner92101ac2001-08-23 17:05:04 +0000123
Chris Lattnerda82ed52003-05-08 16:18:31 +0000124 bool callMainFunction(const std::string &MainName,
125 const std::vector<std::string> &InputFilename);
Chris Lattner92101ac2001-08-23 17:05:04 +0000126
127 void list(); // Do the 'list' command
128 void printStackTrace(); // Do the 'backtrace' command
129
130 // Code execution methods...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000131 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000132 bool executeInstruction(); // Execute one instruction...
133
134 void stepInstruction(); // Do the 'step' command
135 void nextInstruction(); // Do the 'next' command
136 void run(); // Do the 'run' command
137 void finish(); // Do the 'finish' command
138
139 // Opcode Implementations
Chris Lattnerd7916e92003-05-10 21:22:39 +0000140 void visitReturnInst(ReturnInst &I);
141 void visitBranchInst(BranchInst &I);
142 void visitSwitchInst(SwitchInst &I);
143
144 void visitBinaryOperator(BinaryOperator &I);
145 void visitAllocationInst(AllocationInst &I);
146 void visitFreeInst(FreeInst &I);
147 void visitLoadInst(LoadInst &I);
148 void visitStoreInst(StoreInst &I);
149 void visitGetElementPtrInst(GetElementPtrInst &I);
150
151 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
152 void visitCastInst(CastInst &I);
153 void visitCallInst(CallInst &I);
154 void visitShl(ShiftInst &I);
155 void visitShr(ShiftInst &I);
156 void visitVarArgInst(VarArgInst &I);
157 void visitInstruction(Instruction &I) {
158 std::cerr << I;
159 assert(0 && "Instruction not interpretable yet!");
160 }
161
Chris Lattnerda82ed52003-05-08 16:18:31 +0000162 GenericValue callExternalFunction(Function *F,
163 const std::vector<GenericValue> &ArgVals);
Chris Lattnere43db882001-10-27 04:15:57 +0000164 void exitCalled(GenericValue GV);
Chris Lattner92101ac2001-08-23 17:05:04 +0000165
Chris Lattnerda82ed52003-05-08 16:18:31 +0000166 // getCurrentFunction - Return the currently executing function
167 inline Function *getCurrentFunction() const {
168 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurFunction;
Chris Lattner92101ac2001-08-23 17:05:04 +0000169 }
170
171 // isStopped - Return true if a program is stopped. Return false if no
172 // program is running.
173 //
174 inline bool isStopped() const { return !ECStack.empty(); }
175
Chris Lattner44edb6b2003-05-14 14:21:30 +0000176 void addAtExitHandler(Function *F) {
177 AtExitHandlers.push_back(F);
178 }
179
Chris Lattnerfe11a972002-12-23 23:59:41 +0000180 //FIXME: private:
181public:
182 GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
183 User::op_iterator E, ExecutionContext &SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000184
Chris Lattner92101ac2001-08-23 17:05:04 +0000185private: // Helper functions
Chris Lattner77113b62003-05-10 20:21:16 +0000186 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
187 // PHI nodes in the top of the block. This is used for intraprocedural
188 // control flow.
189 //
190 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
191
Brian Gaekefb0ef2e2003-08-13 18:17:54 +0000192 void *getPointerToFunction(Function *F) { return (void*)F; }
Chris Lattnerfe11a972002-12-23 23:59:41 +0000193
Chris Lattnere43db882001-10-27 04:15:57 +0000194 // getCurrentExecutablePath() - Return the directory that the lli executable
195 // lives in.
196 //
Chris Lattner697954c2002-01-20 22:54:45 +0000197 std::string getCurrentExecutablePath() const;
Chris Lattnere43db882001-10-27 04:15:57 +0000198
Chris Lattner92101ac2001-08-23 17:05:04 +0000199 // printCurrentInstruction - Print out the instruction that the virtual PC is
200 // at, or fail silently if no program is running.
201 //
202 void printCurrentInstruction();
203
Chris Lattner461f02f2001-11-07 05:31:27 +0000204 // printStackFrame - Print information about the specified stack frame, or -1
205 // for the default one.
206 //
207 void printStackFrame(int FrameNo = -1);
208
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000209 // LookupMatchingNames - Search the current function namespace, then the
210 // global namespace looking for values that match the specified name. Return
211 // ALL matches to that name. This is obviously slow, and should only be used
212 // for user interaction.
Chris Lattner92101ac2001-08-23 17:05:04 +0000213 //
Chris Lattner697954c2002-01-20 22:54:45 +0000214 std::vector<Value*> LookupMatchingNames(const std::string &Name);
Chris Lattner92101ac2001-08-23 17:05:04 +0000215
216 // ChooseOneOption - Prompt the user to choose among the specified options to
217 // pick one value. If no options are provided, emit an error. If a single
218 // option is provided, just return that option.
219 //
Chris Lattner697954c2002-01-20 22:54:45 +0000220 Value *ChooseOneOption(const std::string &Name,
221 const std::vector<Value*> &Opts);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000222
Chris Lattner44edb6b2003-05-14 14:21:30 +0000223 // PerformExitStuff - Print out counters and profiling information if
224 // applicable...
225 void PerformExitStuff();
Chris Lattner5deea3c2001-10-30 20:28:23 +0000226
227 void initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000228 void initializeExternalFunctions();
Chris Lattner92101ac2001-08-23 17:05:04 +0000229};
230
231#endif