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