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
99 IntrinsicLowering *IL;
101 // The runtime stack of executing code. The top of the stack is the current
103 std::vector<ExecutionContext> ECStack;
105 // AtExitHandlers - List of functions to call when the program exits,
106 // registered with the atexit() library function.
107 std::vector<Function*> AtExitHandlers;
110 explicit Interpreter(std::unique_ptr<Module> M);
113 /// runAtExitHandlers - Run any functions registered by the program's calls to
114 /// atexit(3), which we intercept and store in AtExitHandlers.
116 void runAtExitHandlers();
118 static void Register() {
122 /// Create an interpreter ExecutionEngine.
124 static ExecutionEngine *create(std::unique_ptr<Module> M,
125 std::string *ErrorStr = nullptr);
127 /// run - Start execution with the specified function and arguments.
129 GenericValue runFunction(Function *F,
130 const std::vector<GenericValue> &ArgValues) override;
132 void *getPointerToNamedFunction(StringRef Name,
133 bool AbortOnFailure = true) override {
134 // FIXME: not implemented.
138 // Methods used to execute code:
139 // Place a call on the stack
140 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
141 void run(); // Execute instructions until nothing left to do
143 // Opcode Implementations
144 void visitReturnInst(ReturnInst &I);
145 void visitBranchInst(BranchInst &I);
146 void visitSwitchInst(SwitchInst &I);
147 void visitIndirectBrInst(IndirectBrInst &I);
149 void visitBinaryOperator(BinaryOperator &I);
150 void visitICmpInst(ICmpInst &I);
151 void visitFCmpInst(FCmpInst &I);
152 void visitAllocaInst(AllocaInst &I);
153 void visitLoadInst(LoadInst &I);
154 void visitStoreInst(StoreInst &I);
155 void visitGetElementPtrInst(GetElementPtrInst &I);
156 void visitPHINode(PHINode &PN) {
157 llvm_unreachable("PHI nodes already handled!");
159 void visitTruncInst(TruncInst &I);
160 void visitZExtInst(ZExtInst &I);
161 void visitSExtInst(SExtInst &I);
162 void visitFPTruncInst(FPTruncInst &I);
163 void visitFPExtInst(FPExtInst &I);
164 void visitUIToFPInst(UIToFPInst &I);
165 void visitSIToFPInst(SIToFPInst &I);
166 void visitFPToUIInst(FPToUIInst &I);
167 void visitFPToSIInst(FPToSIInst &I);
168 void visitPtrToIntInst(PtrToIntInst &I);
169 void visitIntToPtrInst(IntToPtrInst &I);
170 void visitBitCastInst(BitCastInst &I);
171 void visitSelectInst(SelectInst &I);
174 void visitCallSite(CallSite CS);
175 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
176 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
177 void visitUnreachableInst(UnreachableInst &I);
179 void visitShl(BinaryOperator &I);
180 void visitLShr(BinaryOperator &I);
181 void visitAShr(BinaryOperator &I);
183 void visitVAArgInst(VAArgInst &I);
184 void visitExtractElementInst(ExtractElementInst &I);
185 void visitInsertElementInst(InsertElementInst &I);
186 void visitShuffleVectorInst(ShuffleVectorInst &I);
188 void visitExtractValueInst(ExtractValueInst &I);
189 void visitInsertValueInst(InsertValueInst &I);
191 void visitInstruction(Instruction &I) {
193 llvm_unreachable("Instruction not interpretable yet!");
196 GenericValue callExternalFunction(Function *F,
197 const std::vector<GenericValue> &ArgVals);
198 void exitCalled(GenericValue GV);
200 void addAtExitHandler(Function *F) {
201 AtExitHandlers.push_back(F);
204 GenericValue *getFirstVarArg () {
205 return &(ECStack.back ().VarArgs[0]);
208 private: // Helper functions
209 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
210 gep_type_iterator E, ExecutionContext &SF);
212 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
213 // PHI nodes in the top of the block. This is used for intraprocedural
216 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
218 void *getPointerToFunction(Function *F) override { return (void*)F; }
220 void initializeExecutionEngine() { }
221 void initializeExternalFunctions();
222 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
223 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
224 GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
225 ExecutionContext &SF);
226 GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
227 ExecutionContext &SF);
228 GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
229 ExecutionContext &SF);
230 GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
231 ExecutionContext &SF);
232 GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
233 ExecutionContext &SF);
234 GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
235 ExecutionContext &SF);
236 GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
237 ExecutionContext &SF);
238 GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
239 ExecutionContext &SF);
240 GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
241 ExecutionContext &SF);
242 GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
243 ExecutionContext &SF);
244 GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
245 ExecutionContext &SF);
246 GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
247 ExecutionContext &SF);
248 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
249 Type *Ty, ExecutionContext &SF);
250 void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
254 } // End llvm namespace