1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 "Support/DataTypes.h"
28 template<typename T> class generic_gep_type_iterator;
30 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
33 // AllocaHolder - Object to track all of the blocks of memory allocated by
34 // alloca. When the function returns, this object is popped off the execution
35 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
38 friend class AllocaHolderHandle;
39 std::vector<void*> Allocations;
42 AllocaHolder() : RefCnt(0) {}
43 void add(void *mem) { Allocations.push_back(mem); }
45 for (unsigned i = 0; i < Allocations.size(); ++i)
50 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
53 class AllocaHolderHandle {
56 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
57 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
58 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
60 void add(void *mem) { H->add(mem); }
63 typedef std::vector<GenericValue> ValuePlaneTy;
65 // ExecutionContext struct - This struct represents one stack frame currently
68 struct ExecutionContext {
69 Function *CurFunction;// The currently executing function
70 BasicBlock *CurBB; // The currently executing BB
71 BasicBlock::iterator CurInst; // The next instruction to execute
72 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
73 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
74 CallSite Caller; // Holds the call that called subframes.
75 // NULL if main func or debugger invoked fn
76 AllocaHolderHandle Allocas; // Track memory allocated by alloca
79 // Interpreter - This class represents the entirety of the interpreter.
81 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
82 int ExitCode; // The exit code to be returned by the lli util
84 IntrinsicLowering *IL;
86 // The runtime stack of executing code. The top of the stack is the current
88 std::vector<ExecutionContext> ECStack;
90 // AtExitHandlers - List of functions to call when the program exits,
91 // registered with the atexit() library function.
92 std::vector<Function*> AtExitHandlers;
95 Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
96 IntrinsicLowering *IL);
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 /// create - Create an interpreter ExecutionEngine. This can never fail. The
105 /// specified IntrinsicLowering implementation will be deleted when the
106 /// Interpreter execution engine is destroyed.
108 static ExecutionEngine *create(Module *M, IntrinsicLowering *IL);
110 /// run - Start execution with the specified function and arguments.
112 virtual GenericValue runFunction(Function *F,
113 const std::vector<GenericValue> &ArgValues);
115 /// recompileAndRelinkFunction - For the interpreter, functions are always
118 virtual void *recompileAndRelinkFunction(Function *F) {
119 return getPointerToFunction(F);
122 // Methods used to execute code:
123 // Place a call on the stack
124 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
125 void run(); // Execute instructions until nothing left to do
127 // Opcode Implementations
128 void visitReturnInst(ReturnInst &I);
129 void visitBranchInst(BranchInst &I);
130 void visitSwitchInst(SwitchInst &I);
132 void visitBinaryOperator(BinaryOperator &I);
133 void visitAllocationInst(AllocationInst &I);
134 void visitFreeInst(FreeInst &I);
135 void visitLoadInst(LoadInst &I);
136 void visitStoreInst(StoreInst &I);
137 void visitGetElementPtrInst(GetElementPtrInst &I);
138 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
139 void visitCastInst(CastInst &I);
140 void visitSelectInst(SelectInst &I);
143 void visitCallSite(CallSite CS);
144 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
145 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
146 void visitUnwindInst(UnwindInst &I);
148 void visitShl(ShiftInst &I);
149 void visitShr(ShiftInst &I);
150 void visitVANextInst(VANextInst &I);
151 void visitVAArgInst(VAArgInst &I);
152 void visitInstruction(Instruction &I) {
154 assert(0 && "Instruction not interpretable yet!");
157 GenericValue callExternalFunction(Function *F,
158 const std::vector<GenericValue> &ArgVals);
159 void exitCalled(GenericValue GV);
161 void addAtExitHandler(Function *F) {
162 AtExitHandlers.push_back(F);
165 GenericValue *getFirstVarArg () {
166 return &(ECStack.back ().VarArgs[0]);
171 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
172 gep_type_iterator E, ExecutionContext &SF);
174 private: // Helper functions
175 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
176 // PHI nodes in the top of the block. This is used for intraprocedural
179 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
181 void *getPointerToFunction(Function *F) { return (void*)F; }
183 void initializeExecutionEngine();
184 void initializeExternalFunctions();
185 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
186 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
187 GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
188 ExecutionContext &SF);
189 void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
192 } // End llvm namespace