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.
45 AllocaHolder(AllocaHolder &&) = default;
46 AllocaHolder &operator=(AllocaHolder &&RHS) = default;
49 for (void *Allocation : Allocations)
53 void add(void *Mem) { Allocations.push_back(Mem); }
56 typedef std::vector<GenericValue> ValuePlaneTy;
58 // ExecutionContext struct - This struct represents one stack frame currently
61 struct ExecutionContext {
62 Function *CurFunction;// The currently executing function
63 BasicBlock *CurBB; // The currently executing BB
64 BasicBlock::iterator CurInst; // The next instruction to execute
65 CallSite Caller; // Holds the call that called subframes.
66 // NULL if main func or debugger invoked fn
67 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
68 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
69 AllocaHolder Allocas; // Track memory allocated by alloca
71 ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}
74 // Interpreter - This class represents the entirety of the interpreter.
76 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
77 GenericValue ExitValue; // The return value of the called function
78 IntrinsicLowering *IL;
80 // The runtime stack of executing code. The top of the stack is the current
82 std::vector<ExecutionContext> ECStack;
84 // AtExitHandlers - List of functions to call when the program exits,
85 // registered with the atexit() library function.
86 std::vector<Function*> AtExitHandlers;
89 explicit Interpreter(std::unique_ptr<Module> M);
90 ~Interpreter() override;
92 /// runAtExitHandlers - Run any functions registered by the program's calls to
93 /// atexit(3), which we intercept and store in AtExitHandlers.
95 void runAtExitHandlers();
97 static void Register() {
101 /// Create an interpreter ExecutionEngine.
103 static ExecutionEngine *create(std::unique_ptr<Module> M,
104 std::string *ErrorStr = nullptr);
106 /// run - Start execution with the specified function and arguments.
108 GenericValue runFunction(Function *F,
109 ArrayRef<GenericValue> ArgValues) override;
111 void *getPointerToNamedFunction(StringRef Name,
112 bool AbortOnFailure = true) override {
113 // FIXME: not implemented.
117 // Methods used to execute code:
118 // Place a call on the stack
119 void callFunction(Function *F, ArrayRef<GenericValue> ArgVals);
120 void run(); // Execute instructions until nothing left to do
122 // Opcode Implementations
123 void visitReturnInst(ReturnInst &I);
124 void visitBranchInst(BranchInst &I);
125 void visitSwitchInst(SwitchInst &I);
126 void visitIndirectBrInst(IndirectBrInst &I);
128 void visitBinaryOperator(BinaryOperator &I);
129 void visitICmpInst(ICmpInst &I);
130 void visitFCmpInst(FCmpInst &I);
131 void visitAllocaInst(AllocaInst &I);
132 void visitLoadInst(LoadInst &I);
133 void visitStoreInst(StoreInst &I);
134 void visitGetElementPtrInst(GetElementPtrInst &I);
135 void visitPHINode(PHINode &PN) {
136 llvm_unreachable("PHI nodes already handled!");
138 void visitTruncInst(TruncInst &I);
139 void visitZExtInst(ZExtInst &I);
140 void visitSExtInst(SExtInst &I);
141 void visitFPTruncInst(FPTruncInst &I);
142 void visitFPExtInst(FPExtInst &I);
143 void visitUIToFPInst(UIToFPInst &I);
144 void visitSIToFPInst(SIToFPInst &I);
145 void visitFPToUIInst(FPToUIInst &I);
146 void visitFPToSIInst(FPToSIInst &I);
147 void visitPtrToIntInst(PtrToIntInst &I);
148 void visitIntToPtrInst(IntToPtrInst &I);
149 void visitBitCastInst(BitCastInst &I);
150 void visitSelectInst(SelectInst &I);
153 void visitCallSite(CallSite CS);
154 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
155 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
156 void visitUnreachableInst(UnreachableInst &I);
158 void visitShl(BinaryOperator &I);
159 void visitLShr(BinaryOperator &I);
160 void visitAShr(BinaryOperator &I);
162 void visitVAArgInst(VAArgInst &I);
163 void visitExtractElementInst(ExtractElementInst &I);
164 void visitInsertElementInst(InsertElementInst &I);
165 void visitShuffleVectorInst(ShuffleVectorInst &I);
167 void visitExtractValueInst(ExtractValueInst &I);
168 void visitInsertValueInst(InsertValueInst &I);
170 void visitInstruction(Instruction &I) {
172 llvm_unreachable("Instruction not interpretable yet!");
175 GenericValue callExternalFunction(Function *F,
176 ArrayRef<GenericValue> ArgVals);
177 void exitCalled(GenericValue GV);
179 void addAtExitHandler(Function *F) {
180 AtExitHandlers.push_back(F);
183 GenericValue *getFirstVarArg () {
184 return &(ECStack.back ().VarArgs[0]);
187 private: // Helper functions
188 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
189 gep_type_iterator E, ExecutionContext &SF);
191 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
192 // PHI nodes in the top of the block. This is used for intraprocedural
195 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
197 void *getPointerToFunction(Function *F) override { return (void*)F; }
199 void initializeExecutionEngine() { }
200 void initializeExternalFunctions();
201 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
202 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
203 GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
204 ExecutionContext &SF);
205 GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
206 ExecutionContext &SF);
207 GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
208 ExecutionContext &SF);
209 GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
210 ExecutionContext &SF);
211 GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
212 ExecutionContext &SF);
213 GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
214 ExecutionContext &SF);
215 GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
216 ExecutionContext &SF);
217 GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
218 ExecutionContext &SF);
219 GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
220 ExecutionContext &SF);
221 GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
222 ExecutionContext &SF);
223 GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
224 ExecutionContext &SF);
225 GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
226 ExecutionContext &SF);
227 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
228 Type *Ty, ExecutionContext &SF);
229 void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
233 } // End llvm namespace