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