blob: 82e7f1fc3fab906faca5adc679b7f68c73d0bc13 [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
10#include "llvm/Module.h"
11#include "llvm/Method.h"
Chris Lattner7b851ab2001-10-15 19:18:26 +000012#include "llvm/Support/DataTypes.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000013
14struct MethodInfo; // Defined in ExecutionAnnotations.h
15class CallInst;
16class ReturnInst;
17class BranchInst;
Chris Lattner86660982001-08-27 05:16:50 +000018class AllocationInst;
Chris Lattner92101ac2001-08-23 17:05:04 +000019
20union GenericValue {
21 bool BoolVal;
22 unsigned char UByteVal;
23 signed char SByteVal;
24 unsigned short UShortVal;
25 signed short ShortVal;
26 unsigned int UIntVal;
27 signed int IntVal;
Chris Lattner7b851ab2001-10-15 19:18:26 +000028 uint64_t ULongVal;
29 int64_t LongVal;
Chris Lattner92101ac2001-08-23 17:05:04 +000030 double DoubleVal;
31 float FloatVal;
32 GenericValue *PointerVal;
33};
34
35typedef vector<GenericValue> ValuePlaneTy;
36
37// ExecutionContext struct - This struct represents one stack frame currently
38// executing.
39//
40struct ExecutionContext {
41 Method *CurMethod; // The currently executing method
42 BasicBlock *CurBB; // The currently executing BB
43 BasicBlock::iterator CurInst; // The next instruction to execute
44 MethodInfo *MethInfo; // The MethInfo annotation for the method
45 vector<ValuePlaneTy> Values; // ValuePlanes for each type
46
47 BasicBlock *PrevBB; // The previous BB or null if in first BB
48 CallInst *Caller; // Holds the call that called subframes.
49 // NULL if main func or debugger invoked fn
50};
51
52
53// Interpreter - This class represents the entirety of the interpreter.
54//
55class Interpreter {
56 Module *CurMod; // The current Module being executed (0 if none)
57 int ExitCode; // The exit code to be returned by the lli util
58 bool Profile; // Profiling enabled?
59 int CurFrame; // The current stack frame being inspected
60
61 // The runtime stack of executing code. The top of the stack is the current
62 // method record.
63 vector<ExecutionContext> ECStack;
64
65public:
66 Interpreter();
67 inline ~Interpreter() { delete CurMod; }
68
69 // getExitCode - return the code that should be the exit code for the lli
70 // utility.
71 inline int getExitCode() const { return ExitCode; }
72
73 // enableProfiling() - Turn profiling on, clear stats?
74 void enableProfiling() { Profile = true; }
75
76 void initializeExecutionEngine();
77 void handleUserInput();
78
79 // User Interation Methods...
Chris Lattner2e42d3a2001-10-15 05:51:48 +000080 void loadModule(const string &Filename);
81 bool flushModule();
Chris Lattner92101ac2001-08-23 17:05:04 +000082 bool callMethod(const string &Name); // return true on failure
83 void setBreakpoint(const string &Name);
Chris Lattner86660982001-08-27 05:16:50 +000084 void infoValue(const string &Name);
Chris Lattner2e42d3a2001-10-15 05:51:48 +000085 void print(const string &Name);
86 static void print(const Type *Ty, GenericValue V);
Chris Lattner365a76e2001-09-10 04:49:44 +000087 static void printValue(const Type *Ty, GenericValue V);
Chris Lattner92101ac2001-08-23 17:05:04 +000088
89
90 void list(); // Do the 'list' command
91 void printStackTrace(); // Do the 'backtrace' command
92
93 // Code execution methods...
Chris Lattner365a76e2001-09-10 04:49:44 +000094 void callMethod (Method *Meth, const vector<GenericValue> &ArgVals);
95 void callExternalMethod(Method *Meth, const vector<GenericValue> &ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +000096 bool executeInstruction(); // Execute one instruction...
97
98 void stepInstruction(); // Do the 'step' command
99 void nextInstruction(); // Do the 'next' command
100 void run(); // Do the 'run' command
101 void finish(); // Do the 'finish' command
102
103 // Opcode Implementations
104 void executeCallInst(CallInst *I, ExecutionContext &SF);
105 void executeRetInst(ReturnInst *I, ExecutionContext &SF);
106 void executeBrInst(BranchInst *I, ExecutionContext &SF);
Chris Lattner86660982001-08-27 05:16:50 +0000107 void executeAllocInst(AllocationInst *I, ExecutionContext &SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000108
109 // getCurrentMethod - Return the currently executing method
110 inline Method *getCurrentMethod() const {
111 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
112 }
113
114 // isStopped - Return true if a program is stopped. Return false if no
115 // program is running.
116 //
117 inline bool isStopped() const { return !ECStack.empty(); }
118
119private: // Helper functions
120 // printCurrentInstruction - Print out the instruction that the virtual PC is
121 // at, or fail silently if no program is running.
122 //
123 void printCurrentInstruction();
124
125 // LookupMatchingNames - Search the current method namespace, then the global
126 // namespace looking for values that match the specified name. Return ALL
127 // matches to that name. This is obviously slow, and should only be used for
128 // user interaction.
129 //
130 vector<Value*> LookupMatchingNames(const string &Name);
131
132 // ChooseOneOption - Prompt the user to choose among the specified options to
133 // pick one value. If no options are provided, emit an error. If a single
134 // option is provided, just return that option.
135 //
136 Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);
137};
138
139#endif