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);
141 void visitCallSite(CallSite CS);
142 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
143 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
144 void visitUnwindInst(UnwindInst &I);
146 void visitShl(ShiftInst &I);
147 void visitShr(ShiftInst &I);
148 void visitVANextInst(VANextInst &I);
149 void visitVAArgInst(VAArgInst &I);
150 void visitInstruction(Instruction &I) {
152 assert(0 && "Instruction not interpretable yet!");
155 GenericValue callExternalFunction(Function *F,
156 const std::vector<GenericValue> &ArgVals);
157 void exitCalled(GenericValue GV);
159 void addAtExitHandler(Function *F) {
160 AtExitHandlers.push_back(F);
163 GenericValue *getFirstVarArg () {
164 return &(ECStack.back ().VarArgs[0]);
169 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
170 gep_type_iterator E, ExecutionContext &SF);
172 private: // Helper functions
173 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
174 // PHI nodes in the top of the block. This is used for intraprocedural
177 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
179 void *getPointerToFunction(Function *F) { return (void*)F; }
181 void initializeExecutionEngine();
182 void initializeExternalFunctions();
183 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
184 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
185 GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
186 ExecutionContext &SF);
187 void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
190 } // End llvm namespace