blob: 47f5c95f152e3630aef46775cbf6155e7c95b884 [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 Lattnerfe11a972002-12-23 23:59:41 +000010#include "../ExecutionEngine.h"
Chris Lattnerd7916e92003-05-10 21:22:39 +000011#include "../GenericValue.h"
Chris Lattner360e17e2001-11-26 23:04:08 +000012#include "Support/DataTypes.h"
Chris Lattner5af0c482001-11-07 04:23:00 +000013#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerfe11a972002-12-23 23:59:41 +000014#include "llvm/Target/TargetData.h"
15#include "llvm/BasicBlock.h"
Chris Lattnerd7916e92003-05-10 21:22:39 +000016#include "llvm/Support/InstVisitor.h"
Chris Lattner5af0c482001-11-07 04:23:00 +000017
Chris Lattnerfe11a972002-12-23 23:59:41 +000018extern CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner92101ac2001-08-23 17:05:04 +000019
Chris Lattnerda82ed52003-05-08 16:18:31 +000020struct FunctionInfo; // Defined in ExecutionAnnotations.h
Chris Lattner92101ac2001-08-23 17:05:04 +000021
Chris Lattner9bffa732002-02-19 18:50:09 +000022// AllocaHolder - Object to track all of the blocks of memory allocated by
23// alloca. When the function returns, this object is poped off the execution
24// stack, which causes the dtor to be run, which frees all the alloca'd memory.
25//
26class AllocaHolder {
27 friend class AllocaHolderHandle;
28 std::vector<void*> Allocations;
29 unsigned RefCnt;
30public:
31 AllocaHolder() : RefCnt(0) {}
32 void add(void *mem) { Allocations.push_back(mem); }
33 ~AllocaHolder() {
34 for (unsigned i = 0; i < Allocations.size(); ++i)
35 free(Allocations[i]);
36 }
37};
38
39// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
40// a vector...
41//
42class AllocaHolderHandle {
43 AllocaHolder *H;
44public:
45 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
46 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
47 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
48
49 void add(void *mem) { H->add(mem); }
50};
51
Chris Lattner697954c2002-01-20 22:54:45 +000052typedef std::vector<GenericValue> ValuePlaneTy;
Chris Lattner92101ac2001-08-23 17:05:04 +000053
54// ExecutionContext struct - This struct represents one stack frame currently
55// executing.
56//
57struct ExecutionContext {
Chris Lattnerda82ed52003-05-08 16:18:31 +000058 Function *CurFunction;// The currently executing function
Chris Lattner92101ac2001-08-23 17:05:04 +000059 BasicBlock *CurBB; // The currently executing BB
60 BasicBlock::iterator CurInst; // The next instruction to execute
Chris Lattnerda82ed52003-05-08 16:18:31 +000061 FunctionInfo *FuncInfo; // The FuncInfo annotation for the function
Chris Lattner697954c2002-01-20 22:54:45 +000062 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
Chris Lattnercdf51782003-05-08 16:06:52 +000063 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
Chris Lattner92101ac2001-08-23 17:05:04 +000064
Chris Lattner92101ac2001-08-23 17:05:04 +000065 CallInst *Caller; // Holds the call that called subframes.
66 // NULL if main func or debugger invoked fn
Chris Lattner9bffa732002-02-19 18:50:09 +000067 AllocaHolderHandle Allocas; // Track memory allocated by alloca
Chris Lattner92101ac2001-08-23 17:05:04 +000068};
69
Chris Lattner92101ac2001-08-23 17:05:04 +000070// Interpreter - This class represents the entirety of the interpreter.
71//
Chris Lattnerd7916e92003-05-10 21:22:39 +000072class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
Chris Lattner92101ac2001-08-23 17:05:04 +000073 int ExitCode; // The exit code to be returned by the lli util
74 bool Profile; // Profiling enabled?
Chris Lattner43e3f7c2001-10-27 08:43:52 +000075 bool Trace; // Tracing enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000076 int CurFrame; // The current stack frame being inspected
Chris Lattnerfe11a972002-12-23 23:59:41 +000077 TargetData TD;
Chris Lattner92101ac2001-08-23 17:05:04 +000078
79 // The runtime stack of executing code. The top of the stack is the current
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000080 // function record.
Chris Lattner697954c2002-01-20 22:54:45 +000081 std::vector<ExecutionContext> ECStack;
Chris Lattner92101ac2001-08-23 17:05:04 +000082
Chris Lattner44edb6b2003-05-14 14:21:30 +000083 // AtExitHandlers - List of functions to call when the program exits.
84 std::vector<Function*> AtExitHandlers;
Chris Lattner92101ac2001-08-23 17:05:04 +000085public:
Chris Lattner39c07262003-08-24 19:50:53 +000086 Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
Brian Gaekef58815e2003-09-04 22:21:24 +000087 bool TraceMode);
Chris Lattnerfe11a972002-12-23 23:59:41 +000088 inline ~Interpreter() { CW.setModule(0); }
Chris Lattner92101ac2001-08-23 17:05:04 +000089
Brian Gaeke82d82772003-09-03 20:34:19 +000090 /// create - Create an interpreter ExecutionEngine. This can never fail.
91 ///
Brian Gaekef58815e2003-09-04 22:21:24 +000092 static ExecutionEngine *create(Module *M, bool TraceMode);
Brian Gaeke82d82772003-09-03 20:34:19 +000093
94 /// getExitCode - return the code that should be the exit code for the lli
95 /// utility.
96 ///
Chris Lattner92101ac2001-08-23 17:05:04 +000097 inline int getExitCode() const { return ExitCode; }
Chris Lattnerfe11a972002-12-23 23:59:41 +000098
99 /// run - Start execution with the specified function and arguments.
100 ///
101 virtual int run(const std::string &FnName,
John Criswell69582b32003-08-21 21:12:30 +0000102 const std::vector<std::string> &Args,
103 const char ** envp);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000104
Chris Lattner92101ac2001-08-23 17:05:04 +0000105
106 // enableProfiling() - Turn profiling on, clear stats?
107 void enableProfiling() { Profile = true; }
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000108 void enableTracing() { Trace = true; }
Chris Lattner92101ac2001-08-23 17:05:04 +0000109
Chris Lattner92101ac2001-08-23 17:05:04 +0000110 void handleUserInput();
111
112 // User Interation Methods...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000113 bool callFunction(const std::string &Name); // return true on failure
Chris Lattner697954c2002-01-20 22:54:45 +0000114 void setBreakpoint(const std::string &Name);
115 void infoValue(const std::string &Name);
116 void print(const std::string &Name);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000117 static void print(const Type *Ty, GenericValue V);
Chris Lattner365a76e2001-09-10 04:49:44 +0000118 static void printValue(const Type *Ty, GenericValue V);
Chris Lattner92101ac2001-08-23 17:05:04 +0000119
Chris Lattnerda82ed52003-05-08 16:18:31 +0000120 bool callMainFunction(const std::string &MainName,
121 const std::vector<std::string> &InputFilename);
Chris Lattner92101ac2001-08-23 17:05:04 +0000122
123 void list(); // Do the 'list' command
124 void printStackTrace(); // Do the 'backtrace' command
125
126 // Code execution methods...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000127 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000128 bool executeInstruction(); // Execute one instruction...
129
130 void stepInstruction(); // Do the 'step' command
131 void nextInstruction(); // Do the 'next' command
132 void run(); // Do the 'run' command
133 void finish(); // Do the 'finish' command
134
135 // Opcode Implementations
Chris Lattnerd7916e92003-05-10 21:22:39 +0000136 void visitReturnInst(ReturnInst &I);
137 void visitBranchInst(BranchInst &I);
138 void visitSwitchInst(SwitchInst &I);
139
140 void visitBinaryOperator(BinaryOperator &I);
141 void visitAllocationInst(AllocationInst &I);
142 void visitFreeInst(FreeInst &I);
143 void visitLoadInst(LoadInst &I);
144 void visitStoreInst(StoreInst &I);
145 void visitGetElementPtrInst(GetElementPtrInst &I);
146
147 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
148 void visitCastInst(CastInst &I);
149 void visitCallInst(CallInst &I);
150 void visitShl(ShiftInst &I);
151 void visitShr(ShiftInst &I);
152 void visitVarArgInst(VarArgInst &I);
153 void visitInstruction(Instruction &I) {
154 std::cerr << I;
155 assert(0 && "Instruction not interpretable yet!");
156 }
157
Chris Lattnerda82ed52003-05-08 16:18:31 +0000158 GenericValue callExternalFunction(Function *F,
159 const std::vector<GenericValue> &ArgVals);
Chris Lattnere43db882001-10-27 04:15:57 +0000160 void exitCalled(GenericValue GV);
Chris Lattner92101ac2001-08-23 17:05:04 +0000161
Chris Lattnerda82ed52003-05-08 16:18:31 +0000162 // getCurrentFunction - Return the currently executing function
163 inline Function *getCurrentFunction() const {
164 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurFunction;
Chris Lattner92101ac2001-08-23 17:05:04 +0000165 }
166
167 // isStopped - Return true if a program is stopped. Return false if no
168 // program is running.
169 //
170 inline bool isStopped() const { return !ECStack.empty(); }
171
Chris Lattner44edb6b2003-05-14 14:21:30 +0000172 void addAtExitHandler(Function *F) {
173 AtExitHandlers.push_back(F);
174 }
175
Chris Lattnerfe11a972002-12-23 23:59:41 +0000176 //FIXME: private:
177public:
178 GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
179 User::op_iterator E, ExecutionContext &SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000180
Chris Lattner92101ac2001-08-23 17:05:04 +0000181private: // Helper functions
Chris Lattner77113b62003-05-10 20:21:16 +0000182 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
183 // PHI nodes in the top of the block. This is used for intraprocedural
184 // control flow.
185 //
186 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
187
Brian Gaekefb0ef2e2003-08-13 18:17:54 +0000188 void *getPointerToFunction(Function *F) { return (void*)F; }
Chris Lattnerfe11a972002-12-23 23:59:41 +0000189
Chris Lattnere43db882001-10-27 04:15:57 +0000190 // getCurrentExecutablePath() - Return the directory that the lli executable
191 // lives in.
192 //
Chris Lattner697954c2002-01-20 22:54:45 +0000193 std::string getCurrentExecutablePath() const;
Chris Lattnere43db882001-10-27 04:15:57 +0000194
Chris Lattner92101ac2001-08-23 17:05:04 +0000195 // printCurrentInstruction - Print out the instruction that the virtual PC is
196 // at, or fail silently if no program is running.
197 //
198 void printCurrentInstruction();
199
Chris Lattner461f02f2001-11-07 05:31:27 +0000200 // printStackFrame - Print information about the specified stack frame, or -1
201 // for the default one.
202 //
203 void printStackFrame(int FrameNo = -1);
204
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000205 // LookupMatchingNames - Search the current function namespace, then the
206 // global namespace looking for values that match the specified name. Return
207 // ALL matches to that name. This is obviously slow, and should only be used
208 // for user interaction.
Chris Lattner92101ac2001-08-23 17:05:04 +0000209 //
Chris Lattner697954c2002-01-20 22:54:45 +0000210 std::vector<Value*> LookupMatchingNames(const std::string &Name);
Chris Lattner92101ac2001-08-23 17:05:04 +0000211
212 // ChooseOneOption - Prompt the user to choose among the specified options to
213 // pick one value. If no options are provided, emit an error. If a single
214 // option is provided, just return that option.
215 //
Chris Lattner697954c2002-01-20 22:54:45 +0000216 Value *ChooseOneOption(const std::string &Name,
217 const std::vector<Value*> &Opts);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000218
Chris Lattner5deea3c2001-10-30 20:28:23 +0000219 void initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000220 void initializeExternalFunctions();
Chris Lattner92101ac2001-08-23 17:05:04 +0000221};
222
223#endif