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