blob: 050b7d332d2f9038a75299675bb94725f18f1f4a [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.
11#define PROFILE_STRUCTURE_FIELDS 1
12
13
Chris Lattner92101ac2001-08-23 17:05:04 +000014#include "llvm/Module.h"
15#include "llvm/Method.h"
Chris Lattner221d6882002-02-12 21:07:25 +000016#include "llvm/BasicBlock.h"
Chris Lattner360e17e2001-11-26 23:04:08 +000017#include "Support/DataTypes.h"
Chris Lattner5af0c482001-11-07 04:23:00 +000018#include "llvm/Assembly/CachedWriter.h"
19
20extern CachedWriter CW; // Object to accellerate printing of LLVM
Chris Lattner92101ac2001-08-23 17:05:04 +000021
22struct MethodInfo; // Defined in ExecutionAnnotations.h
23class CallInst;
24class ReturnInst;
25class BranchInst;
Chris Lattner86660982001-08-27 05:16:50 +000026class AllocationInst;
Chris Lattner92101ac2001-08-23 17:05:04 +000027
Chris Lattnerea38c0e2001-11-07 19:46:27 +000028typedef uint64_t PointerTy;
29
Chris Lattner92101ac2001-08-23 17:05:04 +000030union GenericValue {
31 bool BoolVal;
32 unsigned char UByteVal;
33 signed char SByteVal;
34 unsigned short UShortVal;
35 signed short ShortVal;
36 unsigned int UIntVal;
37 signed int IntVal;
Chris Lattner7b851ab2001-10-15 19:18:26 +000038 uint64_t ULongVal;
39 int64_t LongVal;
Chris Lattner92101ac2001-08-23 17:05:04 +000040 double DoubleVal;
41 float FloatVal;
Chris Lattnerea38c0e2001-11-07 19:46:27 +000042 PointerTy PointerVal;
Chris Lattner92101ac2001-08-23 17:05:04 +000043};
44
Chris Lattner697954c2002-01-20 22:54:45 +000045typedef std::vector<GenericValue> ValuePlaneTy;
Chris Lattner92101ac2001-08-23 17:05:04 +000046
47// ExecutionContext struct - This struct represents one stack frame currently
48// executing.
49//
50struct ExecutionContext {
51 Method *CurMethod; // The currently executing method
52 BasicBlock *CurBB; // The currently executing BB
53 BasicBlock::iterator CurInst; // The next instruction to execute
54 MethodInfo *MethInfo; // The MethInfo annotation for the method
Chris Lattner697954c2002-01-20 22:54:45 +000055 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
Chris Lattner92101ac2001-08-23 17:05:04 +000056
57 BasicBlock *PrevBB; // The previous BB or null if in first BB
58 CallInst *Caller; // Holds the call that called subframes.
59 // NULL if main func or debugger invoked fn
60};
61
Chris Lattner92101ac2001-08-23 17:05:04 +000062// Interpreter - This class represents the entirety of the interpreter.
63//
64class Interpreter {
65 Module *CurMod; // The current Module being executed (0 if none)
66 int ExitCode; // The exit code to be returned by the lli util
67 bool Profile; // Profiling enabled?
Chris Lattner43e3f7c2001-10-27 08:43:52 +000068 bool Trace; // Tracing enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000069 int CurFrame; // The current stack frame being inspected
70
71 // The runtime stack of executing code. The top of the stack is the current
72 // method record.
Chris Lattner697954c2002-01-20 22:54:45 +000073 std::vector<ExecutionContext> ECStack;
Chris Lattner92101ac2001-08-23 17:05:04 +000074
75public:
76 Interpreter();
Chris Lattner5af0c482001-11-07 04:23:00 +000077 inline ~Interpreter() { CW.setModule(0); delete CurMod; }
Chris Lattner92101ac2001-08-23 17:05:04 +000078
79 // getExitCode - return the code that should be the exit code for the lli
80 // utility.
81 inline int getExitCode() const { return ExitCode; }
82
83 // enableProfiling() - Turn profiling on, clear stats?
84 void enableProfiling() { Profile = true; }
Chris Lattner43e3f7c2001-10-27 08:43:52 +000085 void enableTracing() { Trace = true; }
Chris Lattner92101ac2001-08-23 17:05:04 +000086
Chris Lattner92101ac2001-08-23 17:05:04 +000087 void handleUserInput();
88
89 // User Interation Methods...
Chris Lattner697954c2002-01-20 22:54:45 +000090 void loadModule(const std::string &Filename);
Chris Lattner2e42d3a2001-10-15 05:51:48 +000091 bool flushModule();
Chris Lattner697954c2002-01-20 22:54:45 +000092 bool callMethod(const std::string &Name); // return true on failure
93 void setBreakpoint(const std::string &Name);
94 void infoValue(const std::string &Name);
95 void print(const std::string &Name);
Chris Lattner2e42d3a2001-10-15 05:51:48 +000096 static void print(const Type *Ty, GenericValue V);
Chris Lattner365a76e2001-09-10 04:49:44 +000097 static void printValue(const Type *Ty, GenericValue V);
Chris Lattner92101ac2001-08-23 17:05:04 +000098
Chris Lattnerf8f2afb2001-10-18 21:55:32 +000099 // Hack until we can parse command line args...
Chris Lattner697954c2002-01-20 22:54:45 +0000100 bool callMainMethod(const std::string &MainName,
101 const std::vector<std::string> &InputFilename);
Chris Lattner92101ac2001-08-23 17:05:04 +0000102
103 void list(); // Do the 'list' command
104 void printStackTrace(); // Do the 'backtrace' command
105
106 // Code execution methods...
Chris Lattner697954c2002-01-20 22:54:45 +0000107 void callMethod(Method *Meth, const std::vector<GenericValue> &ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000108 bool executeInstruction(); // Execute one instruction...
109
110 void stepInstruction(); // Do the 'step' command
111 void nextInstruction(); // Do the 'next' command
112 void run(); // Do the 'run' command
113 void finish(); // Do the 'finish' command
114
115 // Opcode Implementations
116 void executeCallInst(CallInst *I, ExecutionContext &SF);
117 void executeRetInst(ReturnInst *I, ExecutionContext &SF);
118 void executeBrInst(BranchInst *I, ExecutionContext &SF);
Chris Lattner86660982001-08-27 05:16:50 +0000119 void executeAllocInst(AllocationInst *I, ExecutionContext &SF);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000120 GenericValue callExternalMethod(Method *Meth,
Chris Lattner697954c2002-01-20 22:54:45 +0000121 const std::vector<GenericValue> &ArgVals);
Chris Lattnere43db882001-10-27 04:15:57 +0000122 void exitCalled(GenericValue GV);
Chris Lattner92101ac2001-08-23 17:05:04 +0000123
124 // getCurrentMethod - Return the currently executing method
125 inline Method *getCurrentMethod() const {
126 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
127 }
128
129 // isStopped - Return true if a program is stopped. Return false if no
130 // program is running.
131 //
132 inline bool isStopped() const { return !ECStack.empty(); }
133
134private: // Helper functions
Chris Lattnere43db882001-10-27 04:15:57 +0000135 // getCurrentExecutablePath() - Return the directory that the lli executable
136 // lives in.
137 //
Chris Lattner697954c2002-01-20 22:54:45 +0000138 std::string getCurrentExecutablePath() const;
Chris Lattnere43db882001-10-27 04:15:57 +0000139
Chris Lattner92101ac2001-08-23 17:05:04 +0000140 // printCurrentInstruction - Print out the instruction that the virtual PC is
141 // at, or fail silently if no program is running.
142 //
143 void printCurrentInstruction();
144
Chris Lattner461f02f2001-11-07 05:31:27 +0000145 // printStackFrame - Print information about the specified stack frame, or -1
146 // for the default one.
147 //
148 void printStackFrame(int FrameNo = -1);
149
Chris Lattner92101ac2001-08-23 17:05:04 +0000150 // LookupMatchingNames - Search the current method namespace, then the global
151 // namespace looking for values that match the specified name. Return ALL
152 // matches to that name. This is obviously slow, and should only be used for
153 // user interaction.
154 //
Chris Lattner697954c2002-01-20 22:54:45 +0000155 std::vector<Value*> LookupMatchingNames(const std::string &Name);
Chris Lattner92101ac2001-08-23 17:05:04 +0000156
157 // ChooseOneOption - Prompt the user to choose among the specified options to
158 // pick one value. If no options are provided, emit an error. If a single
159 // option is provided, just return that option.
160 //
Chris Lattner697954c2002-01-20 22:54:45 +0000161 Value *ChooseOneOption(const std::string &Name,
162 const std::vector<Value*> &Opts);
Chris Lattner5deea3c2001-10-30 20:28:23 +0000163
164
165 void initializeExecutionEngine();
166 void initializeExternalMethods();
Chris Lattner92101ac2001-08-23 17:05:04 +0000167};
168
169#endif