blob: 4d8c44ad7601ef4b154b8c776082ad36cb80b377 [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 Lattner360e17e2001-11-26 23:04:08 +000014#include "Support/DataTypes.h"
Chris Lattner5af0c482001-11-07 04:23:00 +000015#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerfe11a972002-12-23 23:59:41 +000016#include "llvm/Target/TargetData.h"
17#include "llvm/BasicBlock.h"
18#include "../GenericValue.h"
Chris Lattner5af0c482001-11-07 04:23:00 +000019
Chris Lattnerfe11a972002-12-23 23:59:41 +000020extern CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner92101ac2001-08-23 17:05:04 +000021
Chris Lattnerda82ed52003-05-08 16:18:31 +000022struct FunctionInfo; // Defined in ExecutionAnnotations.h
Chris Lattner92101ac2001-08-23 17:05:04 +000023class CallInst;
24class ReturnInst;
25class BranchInst;
Chris Lattner77113b62003-05-10 20:21:16 +000026class SwitchInst;
Chris Lattnerfe11a972002-12-23 23:59:41 +000027class LoadInst;
28class StoreInst;
Chris Lattner86660982001-08-27 05:16:50 +000029class AllocationInst;
Chris Lattner92101ac2001-08-23 17:05:04 +000030
Chris Lattner9bffa732002-02-19 18:50:09 +000031// AllocaHolder - Object to track all of the blocks of memory allocated by
32// alloca. When the function returns, this object is poped off the execution
33// stack, which causes the dtor to be run, which frees all the alloca'd memory.
34//
35class AllocaHolder {
36 friend class AllocaHolderHandle;
37 std::vector<void*> Allocations;
38 unsigned RefCnt;
39public:
40 AllocaHolder() : RefCnt(0) {}
41 void add(void *mem) { Allocations.push_back(mem); }
42 ~AllocaHolder() {
43 for (unsigned i = 0; i < Allocations.size(); ++i)
44 free(Allocations[i]);
45 }
46};
47
48// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
49// a vector...
50//
51class AllocaHolderHandle {
52 AllocaHolder *H;
53public:
54 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
55 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
56 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
57
58 void add(void *mem) { H->add(mem); }
59};
60
Chris Lattner697954c2002-01-20 22:54:45 +000061typedef std::vector<GenericValue> ValuePlaneTy;
Chris Lattner92101ac2001-08-23 17:05:04 +000062
63// ExecutionContext struct - This struct represents one stack frame currently
64// executing.
65//
66struct ExecutionContext {
Chris Lattnerda82ed52003-05-08 16:18:31 +000067 Function *CurFunction;// The currently executing function
Chris Lattner92101ac2001-08-23 17:05:04 +000068 BasicBlock *CurBB; // The currently executing BB
69 BasicBlock::iterator CurInst; // The next instruction to execute
Chris Lattnerda82ed52003-05-08 16:18:31 +000070 FunctionInfo *FuncInfo; // The FuncInfo annotation for the function
Chris Lattner697954c2002-01-20 22:54:45 +000071 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
Chris Lattnercdf51782003-05-08 16:06:52 +000072 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
Chris Lattner92101ac2001-08-23 17:05:04 +000073
Chris Lattner92101ac2001-08-23 17:05:04 +000074 CallInst *Caller; // Holds the call that called subframes.
75 // NULL if main func or debugger invoked fn
Chris Lattner9bffa732002-02-19 18:50:09 +000076 AllocaHolderHandle Allocas; // Track memory allocated by alloca
Chris Lattner92101ac2001-08-23 17:05:04 +000077};
78
Chris Lattner92101ac2001-08-23 17:05:04 +000079// Interpreter - This class represents the entirety of the interpreter.
80//
Chris Lattnerfe11a972002-12-23 23:59:41 +000081class Interpreter : public ExecutionEngine {
Chris Lattner92101ac2001-08-23 17:05:04 +000082 int ExitCode; // The exit code to be returned by the lli util
Chris Lattnerfe11a972002-12-23 23:59:41 +000083 bool Debug; // Debug mode enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000084 bool Profile; // Profiling enabled?
Chris Lattner43e3f7c2001-10-27 08:43:52 +000085 bool Trace; // Tracing enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000086 int CurFrame; // The current stack frame being inspected
Chris Lattnerfe11a972002-12-23 23:59:41 +000087 TargetData TD;
Chris Lattner92101ac2001-08-23 17:05:04 +000088
89 // The runtime stack of executing code. The top of the stack is the current
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000090 // function record.
Chris Lattner697954c2002-01-20 22:54:45 +000091 std::vector<ExecutionContext> ECStack;
Chris Lattner92101ac2001-08-23 17:05:04 +000092
93public:
Chris Lattnerfe11a972002-12-23 23:59:41 +000094 Interpreter(Module *M, unsigned Config, bool DebugMode, bool TraceMode);
95 inline ~Interpreter() { CW.setModule(0); }
Chris Lattner92101ac2001-08-23 17:05:04 +000096
97 // getExitCode - return the code that should be the exit code for the lli
98 // utility.
99 inline int getExitCode() const { return ExitCode; }
Chris Lattnerfe11a972002-12-23 23:59:41 +0000100
101 /// run - Start execution with the specified function and arguments.
102 ///
103 virtual int run(const std::string &FnName,
104 const std::vector<std::string> &Args);
105
Chris Lattner92101ac2001-08-23 17:05:04 +0000106
107 // enableProfiling() - Turn profiling on, clear stats?
108 void enableProfiling() { Profile = true; }
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000109 void enableTracing() { Trace = true; }
Chris Lattner92101ac2001-08-23 17:05:04 +0000110
Chris Lattner92101ac2001-08-23 17:05:04 +0000111 void handleUserInput();
112
113 // User Interation Methods...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000114 bool callFunction(const std::string &Name); // return true on failure
Chris Lattner697954c2002-01-20 22:54:45 +0000115 void setBreakpoint(const std::string &Name);
116 void infoValue(const std::string &Name);
117 void print(const std::string &Name);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000118 static void print(const Type *Ty, GenericValue V);
Chris Lattner365a76e2001-09-10 04:49:44 +0000119 static void printValue(const Type *Ty, GenericValue V);
Chris Lattner92101ac2001-08-23 17:05:04 +0000120
Chris Lattnerda82ed52003-05-08 16:18:31 +0000121 bool callMainFunction(const std::string &MainName,
122 const std::vector<std::string> &InputFilename);
Chris Lattner92101ac2001-08-23 17:05:04 +0000123
124 void list(); // Do the 'list' command
125 void printStackTrace(); // Do the 'backtrace' command
126
127 // Code execution methods...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000128 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000129 bool executeInstruction(); // Execute one instruction...
130
131 void stepInstruction(); // Do the 'step' command
132 void nextInstruction(); // Do the 'next' command
133 void run(); // Do the 'run' command
134 void finish(); // Do the 'finish' command
135
136 // Opcode Implementations
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000137 void executeCallInst(CallInst &I, ExecutionContext &SF);
138 void executeRetInst(ReturnInst &I, ExecutionContext &SF);
139 void executeBrInst(BranchInst &I, ExecutionContext &SF);
Chris Lattner77113b62003-05-10 20:21:16 +0000140 void executeSwitchInst(SwitchInst &I, ExecutionContext &SF);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000141 void executeAllocInst(AllocationInst &I, ExecutionContext &SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000142 GenericValue callExternalFunction(Function *F,
143 const std::vector<GenericValue> &ArgVals);
Chris Lattnere43db882001-10-27 04:15:57 +0000144 void exitCalled(GenericValue GV);
Chris Lattner92101ac2001-08-23 17:05:04 +0000145
Chris Lattnerda82ed52003-05-08 16:18:31 +0000146 // getCurrentFunction - Return the currently executing function
147 inline Function *getCurrentFunction() const {
148 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurFunction;
Chris Lattner92101ac2001-08-23 17:05:04 +0000149 }
150
151 // isStopped - Return true if a program is stopped. Return false if no
152 // program is running.
153 //
154 inline bool isStopped() const { return !ECStack.empty(); }
155
Chris Lattnerfe11a972002-12-23 23:59:41 +0000156 //FIXME: private:
157public:
158 GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
159 User::op_iterator E, ExecutionContext &SF);
160 void executeLoadInst(LoadInst &I, ExecutionContext &SF);
161 void executeStoreInst(StoreInst &I, ExecutionContext &SF);
162
163
Chris Lattner92101ac2001-08-23 17:05:04 +0000164private: // Helper functions
Chris Lattner77113b62003-05-10 20:21:16 +0000165 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
166 // PHI nodes in the top of the block. This is used for intraprocedural
167 // control flow.
168 //
169 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
170
Chris Lattnerfe11a972002-12-23 23:59:41 +0000171 void *getPointerToFunction(const Function *F) { return (void*)F; }
172
Chris Lattnere43db882001-10-27 04:15:57 +0000173 // getCurrentExecutablePath() - Return the directory that the lli executable
174 // lives in.
175 //
Chris Lattner697954c2002-01-20 22:54:45 +0000176 std::string getCurrentExecutablePath() const;
Chris Lattnere43db882001-10-27 04:15:57 +0000177
Chris Lattner92101ac2001-08-23 17:05:04 +0000178 // printCurrentInstruction - Print out the instruction that the virtual PC is
179 // at, or fail silently if no program is running.
180 //
181 void printCurrentInstruction();
182
Chris Lattner461f02f2001-11-07 05:31:27 +0000183 // printStackFrame - Print information about the specified stack frame, or -1
184 // for the default one.
185 //
186 void printStackFrame(int FrameNo = -1);
187
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000188 // LookupMatchingNames - Search the current function namespace, then the
189 // global namespace looking for values that match the specified name. Return
190 // ALL matches to that name. This is obviously slow, and should only be used
191 // for user interaction.
Chris Lattner92101ac2001-08-23 17:05:04 +0000192 //
Chris Lattner697954c2002-01-20 22:54:45 +0000193 std::vector<Value*> LookupMatchingNames(const std::string &Name);
Chris Lattner92101ac2001-08-23 17:05:04 +0000194
195 // ChooseOneOption - Prompt the user to choose among the specified options to
196 // pick one value. If no options are provided, emit an error. If a single
197 // option is provided, just return that option.
198 //
Chris Lattner697954c2002-01-20 22:54:45 +0000199 Value *ChooseOneOption(const std::string &Name,
200 const std::vector<Value*> &Opts);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000201
202
203 void initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000204 void initializeExternalFunctions();
Chris Lattner92101ac2001-08-23 17:05:04 +0000205};
206
207#endif