1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file defines the interpreter structure
12 //===----------------------------------------------------------------------===//
14 #ifndef LLI_INTERPRETER_H
15 #define LLI_INTERPRETER_H
17 #include "llvm/BasicBlock.h"
18 #include "llvm/Assembly/CachedWriter.h"
19 #include "llvm/ExecutionEngine/ExecutionEngine.h"
20 #include "llvm/ExecutionEngine/GenericValue.h"
21 #include "llvm/Support/InstVisitor.h"
22 #include "llvm/Target/TargetData.h"
23 #include "Support/DataTypes.h"
25 extern CachedWriter CW; // Object to accelerate printing of LLVM
27 struct FunctionInfo; // Defined in ExecutionAnnotations.h
29 // AllocaHolder - Object to track all of the blocks of memory allocated by
30 // alloca. When the function returns, this object is poped off the execution
31 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
34 friend class AllocaHolderHandle;
35 std::vector<void*> Allocations;
38 AllocaHolder() : RefCnt(0) {}
39 void add(void *mem) { Allocations.push_back(mem); }
41 for (unsigned i = 0; i < Allocations.size(); ++i)
46 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
49 class AllocaHolderHandle {
52 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
53 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
54 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
56 void add(void *mem) { H->add(mem); }
59 typedef std::vector<GenericValue> ValuePlaneTy;
61 // ExecutionContext struct - This struct represents one stack frame currently
64 struct ExecutionContext {
65 Function *CurFunction;// The currently executing function
66 BasicBlock *CurBB; // The currently executing BB
67 BasicBlock::iterator CurInst; // The next instruction to execute
68 FunctionInfo *FuncInfo; // The FuncInfo annotation for the function
69 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
70 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
72 CallInst *Caller; // Holds the call that called subframes.
73 // NULL if main func or debugger invoked fn
74 AllocaHolderHandle Allocas; // Track memory allocated by alloca
77 // Interpreter - This class represents the entirety of the interpreter.
79 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
80 int ExitCode; // The exit code to be returned by the lli util
81 bool Trace; // Tracing enabled?
82 int CurFrame; // The current stack frame being inspected
85 // The runtime stack of executing code. The top of the stack is the current
87 std::vector<ExecutionContext> ECStack;
89 // AtExitHandlers - List of functions to call when the program exits,
90 // registered with the atexit() library function.
91 std::vector<Function*> AtExitHandlers;
93 std::map<Function*, FunctionInfo*> FunctionInfoMap;
95 Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
97 inline ~Interpreter() { CW.setModule(0); }
99 /// runAtExitHandlers - Run any functions registered by the
100 /// program's calls to atexit(3), which we intercept and store in
103 void runAtExitHandlers ();
105 /// create - Create an interpreter ExecutionEngine. This can never fail.
107 static ExecutionEngine *create(Module *M, bool TraceMode);
109 /// run - Start execution with the specified function and arguments.
111 virtual GenericValue run(Function *F,
112 const std::vector<GenericValue> &ArgValues);
114 // Methods used for debug printouts:
115 static void print(const Type *Ty, GenericValue V);
116 static void printValue(const Type *Ty, GenericValue V);
118 // Methods used to execute code:
119 // Place a call on the stack
120 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
121 void executeInstruction(); // Execute one instruction
122 void run(); // Execute instructions until nothing left to do
124 // Opcode Implementations
125 void visitReturnInst(ReturnInst &I);
126 void visitBranchInst(BranchInst &I);
127 void visitSwitchInst(SwitchInst &I);
129 void visitBinaryOperator(BinaryOperator &I);
130 void visitAllocationInst(AllocationInst &I);
131 void visitFreeInst(FreeInst &I);
132 void visitLoadInst(LoadInst &I);
133 void visitStoreInst(StoreInst &I);
134 void visitGetElementPtrInst(GetElementPtrInst &I);
136 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
137 void visitCastInst(CastInst &I);
138 void visitCallInst(CallInst &I);
139 void visitShl(ShiftInst &I);
140 void visitShr(ShiftInst &I);
141 void visitVANextInst(VANextInst &I);
142 void visitInstruction(Instruction &I) {
144 assert(0 && "Instruction not interpretable yet!");
147 GenericValue callExternalFunction(Function *F,
148 const std::vector<GenericValue> &ArgVals);
149 void exitCalled(GenericValue GV);
151 void addAtExitHandler(Function *F) {
152 AtExitHandlers.push_back(F);
157 GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
158 User::op_iterator E, ExecutionContext &SF);
160 private: // Helper functions
161 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
162 // PHI nodes in the top of the block. This is used for intraprocedural
165 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
167 void *getPointerToFunction(Function *F) { return (void*)F; }
169 void initializeExecutionEngine();
170 void initializeExternalFunctions();
171 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
172 GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
173 ExecutionContext &SF);