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 LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
15 #define LLVM_LIB_EXECUTIONENGINE_INTERPRETER_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;
29 template<typename T> class generic_gep_type_iterator;
31 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
34 // AllocaHolder - Object to track all of the blocks of memory allocated by
35 // alloca. When the function returns, this object is popped off the execution
36 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
39 std::vector<void *> Allocations;
44 // Make this type move-only. Define explicit move special members for MSVC.
45 AllocaHolder(AllocaHolder &&RHS) : Allocations(std::move(RHS.Allocations)) {}
46 AllocaHolder &operator=(AllocaHolder &&RHS) {
47 Allocations = std::move(RHS.Allocations);
52 for (void *Allocation : Allocations)
56 void add(void *Mem) { Allocations.push_back(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 CallSite Caller; // Holds the call that called subframes.
69 // NULL if main func or debugger invoked fn
70 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
71 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
72 AllocaHolder Allocas; // Track memory allocated by alloca
74 ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}
76 ExecutionContext(ExecutionContext &&O)
77 : CurFunction(O.CurFunction), CurBB(O.CurBB), CurInst(O.CurInst),
78 Caller(O.Caller), Values(std::move(O.Values)),
79 VarArgs(std::move(O.VarArgs)), Allocas(std::move(O.Allocas)) {}
81 ExecutionContext &operator=(ExecutionContext &&O) {
82 CurFunction = O.CurFunction;
86 Values = std::move(O.Values);
87 VarArgs = std::move(O.VarArgs);
88 Allocas = std::move(O.Allocas);
93 // Interpreter - This class represents the entirety of the interpreter.
95 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
96 GenericValue ExitValue; // The return value of the called function
97 IntrinsicLowering *IL;
99 // The runtime stack of executing code. The top of the stack is the current
101 std::vector<ExecutionContext> ECStack;
103 // AtExitHandlers - List of functions to call when the program exits,
104 // registered with the atexit() library function.
105 std::vector<Function*> AtExitHandlers;
108 explicit Interpreter(std::unique_ptr<Module> M);
109 ~Interpreter() override;
111 /// runAtExitHandlers - Run any functions registered by the program's calls to
112 /// atexit(3), which we intercept and store in AtExitHandlers.
114 void runAtExitHandlers();
116 static void Register() {
120 /// Create an interpreter ExecutionEngine.
122 static ExecutionEngine *create(std::unique_ptr<Module> M,
123 std::string *ErrorStr = nullptr);
125 /// run - Start execution with the specified function and arguments.
127 GenericValue runFunction(Function *F,
128 ArrayRef<GenericValue> ArgValues) override;
130 void *getPointerToNamedFunction(StringRef Name,
131 bool AbortOnFailure = true) override {
132 // FIXME: not implemented.
136 // Methods used to execute code:
137 // Place a call on the stack
138 void callFunction(Function *F, ArrayRef<GenericValue> ArgVals);
139 void run(); // Execute instructions until nothing left to do
141 // Opcode Implementations
142 void visitReturnInst(ReturnInst &I);
143 void visitBranchInst(BranchInst &I);
144 void visitSwitchInst(SwitchInst &I);
145 void visitIndirectBrInst(IndirectBrInst &I);
147 void visitBinaryOperator(BinaryOperator &I);
148 void visitICmpInst(ICmpInst &I);
149 void visitFCmpInst(FCmpInst &I);
150 void visitAllocaInst(AllocaInst &I);
151 void visitLoadInst(LoadInst &I);
152 void visitStoreInst(StoreInst &I);
153 void visitGetElementPtrInst(GetElementPtrInst &I);
154 void visitPHINode(PHINode &PN) {
155 llvm_unreachable("PHI nodes already handled!");
157 void visitTruncInst(TruncInst &I);
158 void visitZExtInst(ZExtInst &I);
159 void visitSExtInst(SExtInst &I);
160 void visitFPTruncInst(FPTruncInst &I);
161 void visitFPExtInst(FPExtInst &I);
162 void visitUIToFPInst(UIToFPInst &I);
163 void visitSIToFPInst(SIToFPInst &I);
164 void visitFPToUIInst(FPToUIInst &I);
165 void visitFPToSIInst(FPToSIInst &I);
166 void visitPtrToIntInst(PtrToIntInst &I);
167 void visitIntToPtrInst(IntToPtrInst &I);
168 void visitBitCastInst(BitCastInst &I);
169 void visitSelectInst(SelectInst &I);
172 void visitCallSite(CallSite CS);
173 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
174 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
175 void visitUnreachableInst(UnreachableInst &I);
177 void visitShl(BinaryOperator &I);
178 void visitLShr(BinaryOperator &I);
179 void visitAShr(BinaryOperator &I);
181 void visitVAArgInst(VAArgInst &I);
182 void visitExtractElementInst(ExtractElementInst &I);
183 void visitInsertElementInst(InsertElementInst &I);
184 void visitShuffleVectorInst(ShuffleVectorInst &I);
186 void visitExtractValueInst(ExtractValueInst &I);
187 void visitInsertValueInst(InsertValueInst &I);
189 void visitInstruction(Instruction &I) {
191 llvm_unreachable("Instruction not interpretable yet!");
194 GenericValue callExternalFunction(Function *F,
195 ArrayRef<GenericValue> ArgVals);
196 void exitCalled(GenericValue GV);
198 void addAtExitHandler(Function *F) {
199 AtExitHandlers.push_back(F);
202 GenericValue *getFirstVarArg () {
203 return &(ECStack.back ().VarArgs[0]);
206 private: // Helper functions
207 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
208 gep_type_iterator E, ExecutionContext &SF);
210 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
211 // PHI nodes in the top of the block. This is used for intraprocedural
214 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
216 void *getPointerToFunction(Function *F) override { return (void*)F; }
218 void initializeExecutionEngine() { }
219 void initializeExternalFunctions();
220 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
221 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
222 GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
223 ExecutionContext &SF);
224 GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
225 ExecutionContext &SF);
226 GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
227 ExecutionContext &SF);
228 GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
229 ExecutionContext &SF);
230 GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
231 ExecutionContext &SF);
232 GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
233 ExecutionContext &SF);
234 GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
235 ExecutionContext &SF);
236 GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
237 ExecutionContext &SF);
238 GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
239 ExecutionContext &SF);
240 GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
241 ExecutionContext &SF);
242 GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
243 ExecutionContext &SF);
244 GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
245 ExecutionContext &SF);
246 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
247 Type *Ty, ExecutionContext &SF);
248 void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
252 } // End llvm namespace