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;
44 // Make this type move-only.
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 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
69 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
70 CallSite Caller; // Holds the call that called subframes.
71 // NULL if main func or debugger invoked fn
72 AllocaHolder Allocas; // Track memory allocated by alloca
75 // Interpreter - This class represents the entirety of the interpreter.
77 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
78 GenericValue ExitValue; // The return value of the called function
80 IntrinsicLowering *IL;
82 // The runtime stack of executing code. The top of the stack is the current
84 std::vector<ExecutionContext> ECStack;
86 // AtExitHandlers - List of functions to call when the program exits,
87 // registered with the atexit() library function.
88 std::vector<Function*> AtExitHandlers;
91 explicit Interpreter(std::unique_ptr<Module> M);
94 /// runAtExitHandlers - Run any functions registered by the program's calls to
95 /// atexit(3), which we intercept and store in AtExitHandlers.
97 void runAtExitHandlers();
99 static void Register() {
103 /// Create an interpreter ExecutionEngine.
105 static ExecutionEngine *create(std::unique_ptr<Module> M,
106 std::string *ErrorStr = nullptr);
108 /// run - Start execution with the specified function and arguments.
110 GenericValue runFunction(Function *F,
111 const std::vector<GenericValue> &ArgValues) override;
113 void *getPointerToNamedFunction(StringRef Name,
114 bool AbortOnFailure = true) override {
115 // FIXME: not implemented.
119 // Methods used to execute code:
120 // Place a call on the stack
121 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
122 void run(); // Execute instructions until nothing left to do
124 // Opcode Implementations
125 void visitReturnInst(ReturnInst &I);
126 void visitBranchInst(BranchInst &I);
127 void visitSwitchInst(SwitchInst &I);
128 void visitIndirectBrInst(IndirectBrInst &I);
130 void visitBinaryOperator(BinaryOperator &I);
131 void visitICmpInst(ICmpInst &I);
132 void visitFCmpInst(FCmpInst &I);
133 void visitAllocaInst(AllocaInst &I);
134 void visitLoadInst(LoadInst &I);
135 void visitStoreInst(StoreInst &I);
136 void visitGetElementPtrInst(GetElementPtrInst &I);
137 void visitPHINode(PHINode &PN) {
138 llvm_unreachable("PHI nodes already handled!");
140 void visitTruncInst(TruncInst &I);
141 void visitZExtInst(ZExtInst &I);
142 void visitSExtInst(SExtInst &I);
143 void visitFPTruncInst(FPTruncInst &I);
144 void visitFPExtInst(FPExtInst &I);
145 void visitUIToFPInst(UIToFPInst &I);
146 void visitSIToFPInst(SIToFPInst &I);
147 void visitFPToUIInst(FPToUIInst &I);
148 void visitFPToSIInst(FPToSIInst &I);
149 void visitPtrToIntInst(PtrToIntInst &I);
150 void visitIntToPtrInst(IntToPtrInst &I);
151 void visitBitCastInst(BitCastInst &I);
152 void visitSelectInst(SelectInst &I);
155 void visitCallSite(CallSite CS);
156 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
157 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
158 void visitUnreachableInst(UnreachableInst &I);
160 void visitShl(BinaryOperator &I);
161 void visitLShr(BinaryOperator &I);
162 void visitAShr(BinaryOperator &I);
164 void visitVAArgInst(VAArgInst &I);
165 void visitExtractElementInst(ExtractElementInst &I);
166 void visitInsertElementInst(InsertElementInst &I);
167 void visitShuffleVectorInst(ShuffleVectorInst &I);
169 void visitExtractValueInst(ExtractValueInst &I);
170 void visitInsertValueInst(InsertValueInst &I);
172 void visitInstruction(Instruction &I) {
174 llvm_unreachable("Instruction not interpretable yet!");
177 GenericValue callExternalFunction(Function *F,
178 const std::vector<GenericValue> &ArgVals);
179 void exitCalled(GenericValue GV);
181 void addAtExitHandler(Function *F) {
182 AtExitHandlers.push_back(F);
185 GenericValue *getFirstVarArg () {
186 return &(ECStack.back ().VarArgs[0]);
189 private: // Helper functions
190 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
191 gep_type_iterator E, ExecutionContext &SF);
193 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
194 // PHI nodes in the top of the block. This is used for intraprocedural
197 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
199 void *getPointerToFunction(Function *F) override { return (void*)F; }
201 void initializeExecutionEngine() { }
202 void initializeExternalFunctions();
203 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
204 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
205 GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
206 ExecutionContext &SF);
207 GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
208 ExecutionContext &SF);
209 GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
210 ExecutionContext &SF);
211 GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
212 ExecutionContext &SF);
213 GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
214 ExecutionContext &SF);
215 GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
216 ExecutionContext &SF);
217 GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
218 ExecutionContext &SF);
219 GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
220 ExecutionContext &SF);
221 GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
222 ExecutionContext &SF);
223 GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
224 ExecutionContext &SF);
225 GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
226 ExecutionContext &SF);
227 GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
228 ExecutionContext &SF);
229 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
230 Type *Ty, ExecutionContext &SF);
231 void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
235 } // End llvm namespace