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;
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 std::vector<void *> Allocations;
45 // Make this type move-only. Define explicit move special members for MSVC.
46 AllocaHolder(AllocaHolder &&RHS) : Allocations(std::move(RHS.Allocations)) {}
47 AllocaHolder &operator=(AllocaHolder &&RHS) {
48 Allocations = std::move(RHS.Allocations);
53 for (void *Allocation : Allocations)
57 void add(void *Mem) { Allocations.push_back(Mem); }
60 typedef std::vector<GenericValue> ValuePlaneTy;
62 // ExecutionContext struct - This struct represents one stack frame currently
65 struct ExecutionContext {
66 Function *CurFunction;// The currently executing function
67 BasicBlock *CurBB; // The currently executing BB
68 BasicBlock::iterator CurInst; // The next instruction to execute
69 CallSite Caller; // Holds the call that called subframes.
70 // NULL if main func or debugger invoked fn
71 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
72 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
73 AllocaHolder Allocas; // Track memory allocated by alloca
75 ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}
77 ExecutionContext(ExecutionContext &&O)
78 : CurFunction(O.CurFunction), CurBB(O.CurBB), CurInst(O.CurInst),
79 Caller(O.Caller), Values(std::move(O.Values)),
80 VarArgs(std::move(O.VarArgs)), Allocas(std::move(O.Allocas)) {}
82 ExecutionContext &operator=(ExecutionContext &&O) {
83 CurFunction = O.CurFunction;
87 Values = std::move(O.Values);
88 VarArgs = std::move(O.VarArgs);
89 Allocas = std::move(O.Allocas);
94 // Interpreter - This class represents the entirety of the interpreter.
96 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
97 GenericValue ExitValue; // The return value of the called function
98 IntrinsicLowering *IL;
100 // The runtime stack of executing code. The top of the stack is the current
102 std::vector<ExecutionContext> ECStack;
104 // AtExitHandlers - List of functions to call when the program exits,
105 // registered with the atexit() library function.
106 std::vector<Function*> AtExitHandlers;
109 explicit Interpreter(std::unique_ptr<Module> M);
110 ~Interpreter() override;
112 /// runAtExitHandlers - Run any functions registered by the program's calls to
113 /// atexit(3), which we intercept and store in AtExitHandlers.
115 void runAtExitHandlers();
117 static void Register() {
121 /// Create an interpreter ExecutionEngine.
123 static ExecutionEngine *create(std::unique_ptr<Module> M,
124 std::string *ErrorStr = nullptr);
126 /// run - Start execution with the specified function and arguments.
128 GenericValue runFunction(Function *F,
129 ArrayRef<GenericValue> ArgValues) override;
131 void *getPointerToNamedFunction(StringRef Name,
132 bool AbortOnFailure = true) override {
133 // FIXME: not implemented.
137 // Methods used to execute code:
138 // Place a call on the stack
139 void callFunction(Function *F, ArrayRef<GenericValue> ArgVals);
140 void run(); // Execute instructions until nothing left to do
142 // Opcode Implementations
143 void visitReturnInst(ReturnInst &I);
144 void visitBranchInst(BranchInst &I);
145 void visitSwitchInst(SwitchInst &I);
146 void visitIndirectBrInst(IndirectBrInst &I);
148 void visitBinaryOperator(BinaryOperator &I);
149 void visitICmpInst(ICmpInst &I);
150 void visitFCmpInst(FCmpInst &I);
151 void visitAllocaInst(AllocaInst &I);
152 void visitLoadInst(LoadInst &I);
153 void visitStoreInst(StoreInst &I);
154 void visitGetElementPtrInst(GetElementPtrInst &I);
155 void visitPHINode(PHINode &PN) {
156 llvm_unreachable("PHI nodes already handled!");
158 void visitTruncInst(TruncInst &I);
159 void visitZExtInst(ZExtInst &I);
160 void visitSExtInst(SExtInst &I);
161 void visitFPTruncInst(FPTruncInst &I);
162 void visitFPExtInst(FPExtInst &I);
163 void visitUIToFPInst(UIToFPInst &I);
164 void visitSIToFPInst(SIToFPInst &I);
165 void visitFPToUIInst(FPToUIInst &I);
166 void visitFPToSIInst(FPToSIInst &I);
167 void visitPtrToIntInst(PtrToIntInst &I);
168 void visitIntToPtrInst(IntToPtrInst &I);
169 void visitBitCastInst(BitCastInst &I);
170 void visitSelectInst(SelectInst &I);
173 void visitCallSite(CallSite CS);
174 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
175 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
176 void visitUnreachableInst(UnreachableInst &I);
178 void visitShl(BinaryOperator &I);
179 void visitLShr(BinaryOperator &I);
180 void visitAShr(BinaryOperator &I);
182 void visitVAArgInst(VAArgInst &I);
183 void visitExtractElementInst(ExtractElementInst &I);
184 void visitInsertElementInst(InsertElementInst &I);
185 void visitShuffleVectorInst(ShuffleVectorInst &I);
187 void visitExtractValueInst(ExtractValueInst &I);
188 void visitInsertValueInst(InsertValueInst &I);
190 void visitInstruction(Instruction &I) {
192 llvm_unreachable("Instruction not interpretable yet!");
195 GenericValue callExternalFunction(Function *F,
196 ArrayRef<GenericValue> ArgVals);
197 void exitCalled(GenericValue GV);
199 void addAtExitHandler(Function *F) {
200 AtExitHandlers.push_back(F);
203 GenericValue *getFirstVarArg () {
204 return &(ECStack.back ().VarArgs[0]);
207 private: // Helper functions
208 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
209 gep_type_iterator E, ExecutionContext &SF);
211 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
212 // PHI nodes in the top of the block. This is used for intraprocedural
215 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
217 void *getPointerToFunction(Function *F) override { return (void*)F; }
219 void initializeExecutionEngine() { }
220 void initializeExternalFunctions();
221 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
222 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
223 GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
224 ExecutionContext &SF);
225 GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
226 ExecutionContext &SF);
227 GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
228 ExecutionContext &SF);
229 GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
230 ExecutionContext &SF);
231 GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
232 ExecutionContext &SF);
233 GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
234 ExecutionContext &SF);
235 GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
236 ExecutionContext &SF);
237 GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
238 ExecutionContext &SF);
239 GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
240 ExecutionContext &SF);
241 GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
242 ExecutionContext &SF);
243 GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
244 ExecutionContext &SF);
245 GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
246 ExecutionContext &SF);
247 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
248 Type *Ty, ExecutionContext &SF);
249 void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
253 } // End llvm namespace