1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/ExecutionEngine/GenericValue.h"
19 #include "llvm/IR/CallSite.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/InstVisitor.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
28 class IntrinsicLowering;
30 template<typename T> class generic_gep_type_iterator;
32 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
35 // AllocaHolder - Object to track all of the blocks of memory allocated by
36 // alloca. When the function returns, this object is popped off the execution
37 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
40 friend class AllocaHolderHandle;
41 std::vector<void*> Allocations;
44 AllocaHolder() : RefCnt(0) {}
45 void add(void *mem) { Allocations.push_back(mem); }
47 for (unsigned i = 0; i < Allocations.size(); ++i)
52 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
55 class AllocaHolderHandle {
58 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
59 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
60 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
62 void add(void *mem) { H->add(mem); }
65 typedef std::vector<GenericValue> ValuePlaneTy;
67 // ExecutionContext struct - This struct represents one stack frame currently
70 struct ExecutionContext {
71 Function *CurFunction;// The currently executing function
72 BasicBlock *CurBB; // The currently executing BB
73 BasicBlock::iterator CurInst; // The next instruction to execute
74 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
75 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
76 CallSite Caller; // Holds the call that called subframes.
77 // NULL if main func or debugger invoked fn
78 AllocaHolderHandle Allocas; // Track memory allocated by alloca
81 // Interpreter - This class represents the entirety of the interpreter.
83 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
84 GenericValue ExitValue; // The return value of the called function
86 IntrinsicLowering *IL;
88 // The runtime stack of executing code. The top of the stack is the current
90 std::vector<ExecutionContext> ECStack;
92 // AtExitHandlers - List of functions to call when the program exits,
93 // registered with the atexit() library function.
94 std::vector<Function*> AtExitHandlers;
97 explicit Interpreter(Module *M);
100 /// runAtExitHandlers - Run any functions registered by the program's calls to
101 /// atexit(3), which we intercept and store in AtExitHandlers.
103 void runAtExitHandlers();
105 static void Register() {
109 /// create - Create an interpreter ExecutionEngine. This can never fail.
111 static ExecutionEngine *create(Module *M, std::string *ErrorStr = nullptr);
113 /// run - Start execution with the specified function and arguments.
115 GenericValue runFunction(Function *F,
116 const std::vector<GenericValue> &ArgValues) override;
118 void *getPointerToNamedFunction(const std::string &Name,
119 bool AbortOnFailure = true) override {
120 // FIXME: not implemented.
124 // Methods used to execute code:
125 // Place a call on the stack
126 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
127 void run(); // Execute instructions until nothing left to do
129 // Opcode Implementations
130 void visitReturnInst(ReturnInst &I);
131 void visitBranchInst(BranchInst &I);
132 void visitSwitchInst(SwitchInst &I);
133 void visitIndirectBrInst(IndirectBrInst &I);
135 void visitBinaryOperator(BinaryOperator &I);
136 void visitICmpInst(ICmpInst &I);
137 void visitFCmpInst(FCmpInst &I);
138 void visitAllocaInst(AllocaInst &I);
139 void visitLoadInst(LoadInst &I);
140 void visitStoreInst(StoreInst &I);
141 void visitGetElementPtrInst(GetElementPtrInst &I);
142 void visitPHINode(PHINode &PN) {
143 llvm_unreachable("PHI nodes already handled!");
145 void visitTruncInst(TruncInst &I);
146 void visitZExtInst(ZExtInst &I);
147 void visitSExtInst(SExtInst &I);
148 void visitFPTruncInst(FPTruncInst &I);
149 void visitFPExtInst(FPExtInst &I);
150 void visitUIToFPInst(UIToFPInst &I);
151 void visitSIToFPInst(SIToFPInst &I);
152 void visitFPToUIInst(FPToUIInst &I);
153 void visitFPToSIInst(FPToSIInst &I);
154 void visitPtrToIntInst(PtrToIntInst &I);
155 void visitIntToPtrInst(IntToPtrInst &I);
156 void visitBitCastInst(BitCastInst &I);
157 void visitSelectInst(SelectInst &I);
160 void visitCallSite(CallSite CS);
161 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
162 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
163 void visitUnreachableInst(UnreachableInst &I);
165 void visitShl(BinaryOperator &I);
166 void visitLShr(BinaryOperator &I);
167 void visitAShr(BinaryOperator &I);
169 void visitVAArgInst(VAArgInst &I);
170 void visitExtractElementInst(ExtractElementInst &I);
171 void visitInsertElementInst(InsertElementInst &I);
172 void visitShuffleVectorInst(ShuffleVectorInst &I);
174 void visitExtractValueInst(ExtractValueInst &I);
175 void visitInsertValueInst(InsertValueInst &I);
177 void visitInstruction(Instruction &I) {
179 llvm_unreachable("Instruction not interpretable yet!");
182 GenericValue callExternalFunction(Function *F,
183 const std::vector<GenericValue> &ArgVals);
184 void exitCalled(GenericValue GV);
186 void addAtExitHandler(Function *F) {
187 AtExitHandlers.push_back(F);
190 GenericValue *getFirstVarArg () {
191 return &(ECStack.back ().VarArgs[0]);
194 private: // Helper functions
195 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
196 gep_type_iterator E, ExecutionContext &SF);
198 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
199 // PHI nodes in the top of the block. This is used for intraprocedural
202 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
204 void *getPointerToFunction(Function *F) override { return (void*)F; }
206 void initializeExecutionEngine() { }
207 void initializeExternalFunctions();
208 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
209 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
210 GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
211 ExecutionContext &SF);
212 GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
213 ExecutionContext &SF);
214 GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
215 ExecutionContext &SF);
216 GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
217 ExecutionContext &SF);
218 GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
219 ExecutionContext &SF);
220 GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
221 ExecutionContext &SF);
222 GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
223 ExecutionContext &SF);
224 GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
225 ExecutionContext &SF);
226 GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
227 ExecutionContext &SF);
228 GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
229 ExecutionContext &SF);
230 GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
231 ExecutionContext &SF);
232 GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
233 ExecutionContext &SF);
234 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
235 Type *Ty, ExecutionContext &SF);
236 void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
240 } // End llvm namespace