Interpreter.h revision da82ed52ac02497d343b898cca0bb2cf303f062d
1//===-- 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// Uncomment this line to enable profiling of structure field accesses. 11//#define PROFILE_STRUCTURE_FIELDS 1 12 13#include "../ExecutionEngine.h" 14#include "Support/DataTypes.h" 15#include "llvm/Assembly/CachedWriter.h" 16#include "llvm/Target/TargetData.h" 17#include "llvm/BasicBlock.h" 18#include "../GenericValue.h" 19 20extern CachedWriter CW; // Object to accelerate printing of LLVM 21 22struct FunctionInfo; // Defined in ExecutionAnnotations.h 23class CallInst; 24class ReturnInst; 25class BranchInst; 26class LoadInst; 27class StoreInst; 28class AllocationInst; 29 30// AllocaHolder - Object to track all of the blocks of memory allocated by 31// alloca. When the function returns, this object is poped off the execution 32// stack, which causes the dtor to be run, which frees all the alloca'd memory. 33// 34class AllocaHolder { 35 friend class AllocaHolderHandle; 36 std::vector<void*> Allocations; 37 unsigned RefCnt; 38public: 39 AllocaHolder() : RefCnt(0) {} 40 void add(void *mem) { Allocations.push_back(mem); } 41 ~AllocaHolder() { 42 for (unsigned i = 0; i < Allocations.size(); ++i) 43 free(Allocations[i]); 44 } 45}; 46 47// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into 48// a vector... 49// 50class AllocaHolderHandle { 51 AllocaHolder *H; 52public: 53 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; } 54 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; } 55 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; } 56 57 void add(void *mem) { H->add(mem); } 58}; 59 60typedef std::vector<GenericValue> ValuePlaneTy; 61 62// ExecutionContext struct - This struct represents one stack frame currently 63// executing. 64// 65struct ExecutionContext { 66 Function *CurFunction;// The currently executing function 67 BasicBlock *CurBB; // The currently executing BB 68 BasicBlock::iterator CurInst; // The next instruction to execute 69 FunctionInfo *FuncInfo; // The FuncInfo annotation for the function 70 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type 71 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis 72 73 BasicBlock *PrevBB; // The previous BB or null if in first BB 74 CallInst *Caller; // Holds the call that called subframes. 75 // NULL if main func or debugger invoked fn 76 AllocaHolderHandle Allocas; // Track memory allocated by alloca 77}; 78 79// Interpreter - This class represents the entirety of the interpreter. 80// 81class Interpreter : public ExecutionEngine { 82 int ExitCode; // The exit code to be returned by the lli util 83 bool Debug; // Debug mode enabled? 84 bool Profile; // Profiling enabled? 85 bool Trace; // Tracing enabled? 86 int CurFrame; // The current stack frame being inspected 87 TargetData TD; 88 89 // The runtime stack of executing code. The top of the stack is the current 90 // function record. 91 std::vector<ExecutionContext> ECStack; 92 93public: 94 Interpreter(Module *M, unsigned Config, bool DebugMode, bool TraceMode); 95 inline ~Interpreter() { CW.setModule(0); } 96 97 // getExitCode - return the code that should be the exit code for the lli 98 // utility. 99 inline int getExitCode() const { return ExitCode; } 100 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 106 107 // enableProfiling() - Turn profiling on, clear stats? 108 void enableProfiling() { Profile = true; } 109 void enableTracing() { Trace = true; } 110 111 void handleUserInput(); 112 113 // User Interation Methods... 114 bool callFunction(const std::string &Name); // return true on failure 115 void setBreakpoint(const std::string &Name); 116 void infoValue(const std::string &Name); 117 void print(const std::string &Name); 118 static void print(const Type *Ty, GenericValue V); 119 static void printValue(const Type *Ty, GenericValue V); 120 121 bool callMainFunction(const std::string &MainName, 122 const std::vector<std::string> &InputFilename); 123 124 void list(); // Do the 'list' command 125 void printStackTrace(); // Do the 'backtrace' command 126 127 // Code execution methods... 128 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals); 129 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 137 void executeCallInst(CallInst &I, ExecutionContext &SF); 138 void executeRetInst(ReturnInst &I, ExecutionContext &SF); 139 void executeBrInst(BranchInst &I, ExecutionContext &SF); 140 void executeAllocInst(AllocationInst &I, ExecutionContext &SF); 141 GenericValue callExternalFunction(Function *F, 142 const std::vector<GenericValue> &ArgVals); 143 void exitCalled(GenericValue GV); 144 145 // getCurrentFunction - Return the currently executing function 146 inline Function *getCurrentFunction() const { 147 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurFunction; 148 } 149 150 // isStopped - Return true if a program is stopped. Return false if no 151 // program is running. 152 // 153 inline bool isStopped() const { return !ECStack.empty(); } 154 155 //FIXME: private: 156public: 157 GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I, 158 User::op_iterator E, ExecutionContext &SF); 159 void executeLoadInst(LoadInst &I, ExecutionContext &SF); 160 void executeStoreInst(StoreInst &I, ExecutionContext &SF); 161 162 163private: // Helper functions 164 void *getPointerToFunction(const Function *F) { return (void*)F; } 165 166 // getCurrentExecutablePath() - Return the directory that the lli executable 167 // lives in. 168 // 169 std::string getCurrentExecutablePath() const; 170 171 // printCurrentInstruction - Print out the instruction that the virtual PC is 172 // at, or fail silently if no program is running. 173 // 174 void printCurrentInstruction(); 175 176 // printStackFrame - Print information about the specified stack frame, or -1 177 // for the default one. 178 // 179 void printStackFrame(int FrameNo = -1); 180 181 // LookupMatchingNames - Search the current function namespace, then the 182 // global namespace looking for values that match the specified name. Return 183 // ALL matches to that name. This is obviously slow, and should only be used 184 // for user interaction. 185 // 186 std::vector<Value*> LookupMatchingNames(const std::string &Name); 187 188 // ChooseOneOption - Prompt the user to choose among the specified options to 189 // pick one value. If no options are provided, emit an error. If a single 190 // option is provided, just return that option. 191 // 192 Value *ChooseOneOption(const std::string &Name, 193 const std::vector<Value*> &Opts); 194 195 196 void initializeExecutionEngine(); 197 void initializeExternalFunctions(); 198}; 199 200#endif 201