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 LLI_INTERPRETER_H
15 #define LLI_INTERPRETER_H
17 #include "llvm/Function.h"
18 #include "llvm/ExecutionEngine/ExecutionEngine.h"
19 #include "llvm/ExecutionEngine/GenericValue.h"
20 #include "llvm/Support/InstVisitor.h"
21 #include "llvm/Support/CallSite.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Support/DataTypes.h"
27 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 friend class AllocaHolderHandle;
40 std::vector<void*> Allocations;
43 AllocaHolder() : RefCnt(0) {}
44 void add(void *mem) { Allocations.push_back(mem); }
46 for (unsigned i = 0; i < Allocations.size(); ++i)
51 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
54 class AllocaHolderHandle {
57 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
58 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
59 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
61 void add(void *mem) { H->add(mem); }
64 typedef std::vector<GenericValue> ValuePlaneTy;
66 // ExecutionContext struct - This struct represents one stack frame currently
69 struct ExecutionContext {
70 Function *CurFunction;// The currently executing function
71 BasicBlock *CurBB; // The currently executing BB
72 BasicBlock::iterator CurInst; // The next instruction to execute
73 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
74 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
75 CallSite Caller; // Holds the call that called subframes.
76 // NULL if main func or debugger invoked fn
77 AllocaHolderHandle Allocas; // Track memory allocated by alloca
80 // Interpreter - This class represents the entirety of the interpreter.
82 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
83 GenericValue ExitValue; // The return value of the called function
85 IntrinsicLowering *IL;
87 // The runtime stack of executing code. The top of the stack is the current
89 std::vector<ExecutionContext> ECStack;
91 // AtExitHandlers - List of functions to call when the program exits,
92 // registered with the atexit() library function.
93 std::vector<Function*> AtExitHandlers;
96 explicit Interpreter(ModuleProvider *M);
99 /// runAtExitHandlers - Run any functions registered by the program's calls to
100 /// atexit(3), which we intercept and store in AtExitHandlers.
102 void runAtExitHandlers();
104 static void Register() {
108 /// create - Create an interpreter ExecutionEngine. This can never fail.
110 static ExecutionEngine *create(ModuleProvider *M, std::string *ErrorStr = 0,
111 CodeGenOpt::Level = CodeGenOpt::Default,
112 bool GVsWithCode = true);
114 /// run - Start execution with the specified function and arguments.
116 virtual GenericValue runFunction(Function *F,
117 const std::vector<GenericValue> &ArgValues);
119 /// recompileAndRelinkFunction - For the interpreter, functions are always
122 virtual void *recompileAndRelinkFunction(Function *F) {
123 return getPointerToFunction(F);
126 /// freeMachineCodeForFunction - The interpreter does not generate any code.
128 void freeMachineCodeForFunction(Function *F) { }
130 // Methods used to execute code:
131 // Place a call on the stack
132 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
133 void run(); // Execute instructions until nothing left to do
135 // Opcode Implementations
136 void visitReturnInst(ReturnInst &I);
137 void visitBranchInst(BranchInst &I);
138 void visitSwitchInst(SwitchInst &I);
140 void visitBinaryOperator(BinaryOperator &I);
141 void visitICmpInst(ICmpInst &I);
142 void visitFCmpInst(FCmpInst &I);
143 void visitAllocationInst(AllocationInst &I);
144 void visitFreeInst(FreeInst &I);
145 void visitLoadInst(LoadInst &I);
146 void visitStoreInst(StoreInst &I);
147 void visitGetElementPtrInst(GetElementPtrInst &I);
148 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
149 void visitTruncInst(TruncInst &I);
150 void visitZExtInst(ZExtInst &I);
151 void visitSExtInst(SExtInst &I);
152 void visitFPTruncInst(FPTruncInst &I);
153 void visitFPExtInst(FPExtInst &I);
154 void visitUIToFPInst(UIToFPInst &I);
155 void visitSIToFPInst(SIToFPInst &I);
156 void visitFPToUIInst(FPToUIInst &I);
157 void visitFPToSIInst(FPToSIInst &I);
158 void visitPtrToIntInst(PtrToIntInst &I);
159 void visitIntToPtrInst(IntToPtrInst &I);
160 void visitBitCastInst(BitCastInst &I);
161 void visitSelectInst(SelectInst &I);
164 void visitCallSite(CallSite CS);
165 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
166 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
167 void visitUnwindInst(UnwindInst &I);
168 void visitUnreachableInst(UnreachableInst &I);
170 void visitShl(BinaryOperator &I);
171 void visitLShr(BinaryOperator &I);
172 void visitAShr(BinaryOperator &I);
174 void visitVAArgInst(VAArgInst &I);
175 void visitInstruction(Instruction &I) {
177 assert(0 && "Instruction not interpretable yet!");
180 GenericValue callExternalFunction(Function *F,
181 const std::vector<GenericValue> &ArgVals);
182 void exitCalled(GenericValue GV);
184 void addAtExitHandler(Function *F) {
185 AtExitHandlers.push_back(F);
188 GenericValue *getFirstVarArg () {
189 return &(ECStack.back ().VarArgs[0]);
194 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
195 gep_type_iterator E, ExecutionContext &SF);
197 private: // Helper functions
198 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
199 // PHI nodes in the top of the block. This is used for intraprocedural
202 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
204 void *getPointerToFunction(Function *F) { return (void*)F; }
206 void initializeExecutionEngine() { }
207 void initializeExternalFunctions();
208 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
209 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
210 GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy,
211 ExecutionContext &SF);
212 GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy,
213 ExecutionContext &SF);
214 GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy,
215 ExecutionContext &SF);
216 GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy,
217 ExecutionContext &SF);
218 GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy,
219 ExecutionContext &SF);
220 GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy,
221 ExecutionContext &SF);
222 GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy,
223 ExecutionContext &SF);
224 GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy,
225 ExecutionContext &SF);
226 GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy,
227 ExecutionContext &SF);
228 GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy,
229 ExecutionContext &SF);
230 GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
231 ExecutionContext &SF);
232 GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy,
233 ExecutionContext &SF);
234 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
235 const Type *Ty, ExecutionContext &SF);
236 void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
240 } // End llvm namespace