blob: c0d8b75aca508129300ab6321f228e0c4a9981c9 [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 Lattnerfddc7552002-10-15 20:34:05 +000040 unsigned char Untyped[8];
Chris Lattner92101ac2001-08-23 17:05:04 +000041};
42
Chris Lattner9bffa732002-02-19 18:50:09 +000043// AllocaHolder - Object to track all of the blocks of memory allocated by
44// alloca. When the function returns, this object is poped off the execution
45// stack, which causes the dtor to be run, which frees all the alloca'd memory.
46//
47class AllocaHolder {
48 friend class AllocaHolderHandle;
49 std::vector<void*> Allocations;
50 unsigned RefCnt;
51public:
52 AllocaHolder() : RefCnt(0) {}
53 void add(void *mem) { Allocations.push_back(mem); }
54 ~AllocaHolder() {
55 for (unsigned i = 0; i < Allocations.size(); ++i)
56 free(Allocations[i]);
57 }
58};
59
60// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
61// a vector...
62//
63class AllocaHolderHandle {
64 AllocaHolder *H;
65public:
66 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
67 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
68 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
69
70 void add(void *mem) { H->add(mem); }
71};
72
Chris Lattner697954c2002-01-20 22:54:45 +000073typedef std::vector<GenericValue> ValuePlaneTy;
Chris Lattner92101ac2001-08-23 17:05:04 +000074
75// ExecutionContext struct - This struct represents one stack frame currently
76// executing.
77//
78struct ExecutionContext {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000079 Function *CurMethod; // The currently executing function
Chris Lattner92101ac2001-08-23 17:05:04 +000080 BasicBlock *CurBB; // The currently executing BB
81 BasicBlock::iterator CurInst; // The next instruction to execute
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000082 MethodInfo *MethInfo; // The MethInfo annotation for the function
Chris Lattner697954c2002-01-20 22:54:45 +000083 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
Chris Lattner92101ac2001-08-23 17:05:04 +000084
85 BasicBlock *PrevBB; // The previous BB or null if in first BB
86 CallInst *Caller; // Holds the call that called subframes.
87 // NULL if main func or debugger invoked fn
Chris Lattner9bffa732002-02-19 18:50:09 +000088 AllocaHolderHandle Allocas; // Track memory allocated by alloca
Chris Lattner92101ac2001-08-23 17:05:04 +000089};
90
Chris Lattner92101ac2001-08-23 17:05:04 +000091// Interpreter - This class represents the entirety of the interpreter.
92//
93class Interpreter {
94 Module *CurMod; // The current Module being executed (0 if none)
95 int ExitCode; // The exit code to be returned by the lli util
96 bool Profile; // Profiling enabled?
Chris Lattner43e3f7c2001-10-27 08:43:52 +000097 bool Trace; // Tracing enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000098 int CurFrame; // The current stack frame being inspected
99
100 // The runtime stack of executing code. The top of the stack is the current
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000101 // function record.
Chris Lattner697954c2002-01-20 22:54:45 +0000102 std::vector<ExecutionContext> ECStack;
Chris Lattner92101ac2001-08-23 17:05:04 +0000103
104public:
105 Interpreter();
Chris Lattner5af0c482001-11-07 04:23:00 +0000106 inline ~Interpreter() { CW.setModule(0); delete CurMod; }
Chris Lattner92101ac2001-08-23 17:05:04 +0000107
108 // getExitCode - return the code that should be the exit code for the lli
109 // utility.
110 inline int getExitCode() const { return ExitCode; }
Chris Lattner849735c2002-10-02 21:11:16 +0000111 inline Module *getModule() const { return CurMod; }
Chris Lattner92101ac2001-08-23 17:05:04 +0000112
113 // enableProfiling() - Turn profiling on, clear stats?
114 void enableProfiling() { Profile = true; }
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000115 void enableTracing() { Trace = true; }
Chris Lattner92101ac2001-08-23 17:05:04 +0000116
Chris Lattner92101ac2001-08-23 17:05:04 +0000117 void handleUserInput();
118
119 // User Interation Methods...
Chris Lattner697954c2002-01-20 22:54:45 +0000120 void loadModule(const std::string &Filename);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000121 bool flushModule();
Chris Lattner697954c2002-01-20 22:54:45 +0000122 bool callMethod(const std::string &Name); // return true on failure
123 void setBreakpoint(const std::string &Name);
124 void infoValue(const std::string &Name);
125 void print(const std::string &Name);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000126 static void print(const Type *Ty, GenericValue V);
Chris Lattner365a76e2001-09-10 04:49:44 +0000127 static void printValue(const Type *Ty, GenericValue V);
Chris Lattner92101ac2001-08-23 17:05:04 +0000128
Chris Lattner697954c2002-01-20 22:54:45 +0000129 bool callMainMethod(const std::string &MainName,
130 const std::vector<std::string> &InputFilename);
Chris Lattnerab2dea52002-11-07 19:29:31 +0000131 GenericValue CreateArgv(const std::vector<std::string> &InputArgv);
Chris Lattner92101ac2001-08-23 17:05:04 +0000132
133 void list(); // Do the 'list' command
134 void printStackTrace(); // Do the 'backtrace' command
135
136 // Code execution methods...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000137 void callMethod(Function *F, const std::vector<GenericValue> &ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000138 bool executeInstruction(); // Execute one instruction...
139
140 void stepInstruction(); // Do the 'step' command
141 void nextInstruction(); // Do the 'next' command
142 void run(); // Do the 'run' command
143 void finish(); // Do the 'finish' command
144
145 // Opcode Implementations
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000146 void executeCallInst(CallInst &I, ExecutionContext &SF);
147 void executeRetInst(ReturnInst &I, ExecutionContext &SF);
148 void executeBrInst(BranchInst &I, ExecutionContext &SF);
149 void executeAllocInst(AllocationInst &I, ExecutionContext &SF);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000150 GenericValue callExternalMethod(Function *F,
Chris Lattner697954c2002-01-20 22:54:45 +0000151 const std::vector<GenericValue> &ArgVals);
Chris Lattnere43db882001-10-27 04:15:57 +0000152 void exitCalled(GenericValue GV);
Chris Lattner92101ac2001-08-23 17:05:04 +0000153
154 // getCurrentMethod - Return the currently executing method
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000155 inline Function *getCurrentMethod() const {
Chris Lattner92101ac2001-08-23 17:05:04 +0000156 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
157 }
158
159 // isStopped - Return true if a program is stopped. Return false if no
160 // program is running.
161 //
162 inline bool isStopped() const { return !ECStack.empty(); }
163
164private: // Helper functions
Chris Lattnere43db882001-10-27 04:15:57 +0000165 // getCurrentExecutablePath() - Return the directory that the lli executable
166 // lives in.
167 //
Chris Lattner697954c2002-01-20 22:54:45 +0000168 std::string getCurrentExecutablePath() const;
Chris Lattnere43db882001-10-27 04:15:57 +0000169
Chris Lattner92101ac2001-08-23 17:05:04 +0000170 // printCurrentInstruction - Print out the instruction that the virtual PC is
171 // at, or fail silently if no program is running.
172 //
173 void printCurrentInstruction();
174
Chris Lattner461f02f2001-11-07 05:31:27 +0000175 // printStackFrame - Print information about the specified stack frame, or -1
176 // for the default one.
177 //
178 void printStackFrame(int FrameNo = -1);
179
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000180 // LookupMatchingNames - Search the current function namespace, then the
181 // global namespace looking for values that match the specified name. Return
182 // ALL matches to that name. This is obviously slow, and should only be used
183 // for user interaction.
Chris Lattner92101ac2001-08-23 17:05:04 +0000184 //
Chris Lattner697954c2002-01-20 22:54:45 +0000185 std::vector<Value*> LookupMatchingNames(const std::string &Name);
Chris Lattner92101ac2001-08-23 17:05:04 +0000186
187 // ChooseOneOption - Prompt the user to choose among the specified options to
188 // pick one value. If no options are provided, emit an error. If a single
189 // option is provided, just return that option.
190 //
Chris Lattner697954c2002-01-20 22:54:45 +0000191 Value *ChooseOneOption(const std::string &Name,
192 const std::vector<Value*> &Opts);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000193
194
195 void initializeExecutionEngine();
196 void initializeExternalMethods();
Chris Lattner92101ac2001-08-23 17:05:04 +0000197};
198
199#endif