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"
27 struct FunctionInfo; // Defined in ExecutionAnnotations.h
29 // AllocaHolder - Object to track all of the blocks of memory allocated by
30 // alloca. When the function returns, this object is poped off the execution
31 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
34 friend class AllocaHolderHandle;
35 std::vector<void*> Allocations;
38 AllocaHolder() : RefCnt(0) {}
39 void add(void *mem) { Allocations.push_back(mem); }
41 for (unsigned i = 0; i < Allocations.size(); ++i)
46 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
49 class AllocaHolderHandle {
52 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
53 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
54 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
56 void add(void *mem) { H->add(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 AllocaHolderHandle 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 int ExitCode; // The exit code to be returned by the lli util
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 Interpreter(Module *M, bool isLittleEndian, bool isLongPointer);
91 inline ~Interpreter() { }
93 /// runAtExitHandlers - Run any functions registered by the
94 /// program's calls to atexit(3), which we intercept and store in
97 void runAtExitHandlers ();
99 /// create - Create an interpreter ExecutionEngine. This can never fail.
101 static ExecutionEngine *create(Module *M);
103 /// run - Start execution with the specified function and arguments.
105 virtual GenericValue run(Function *F,
106 const std::vector<GenericValue> &ArgValues);
108 // Methods used to execute code:
109 // Place a call on the stack
110 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
111 void run(); // Execute instructions until nothing left to do
113 // Opcode Implementations
114 void visitReturnInst(ReturnInst &I);
115 void visitBranchInst(BranchInst &I);
116 void visitSwitchInst(SwitchInst &I);
118 void visitBinaryOperator(BinaryOperator &I);
119 void visitAllocationInst(AllocationInst &I);
120 void visitFreeInst(FreeInst &I);
121 void visitLoadInst(LoadInst &I);
122 void visitStoreInst(StoreInst &I);
123 void visitGetElementPtrInst(GetElementPtrInst &I);
124 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
125 void visitCastInst(CastInst &I);
127 void visitCallSite(CallSite CS);
128 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
129 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
130 void visitUnwindInst(UnwindInst &I);
132 void visitShl(ShiftInst &I);
133 void visitShr(ShiftInst &I);
134 void visitVANextInst(VANextInst &I);
135 void visitVAArgInst(VAArgInst &I);
136 void visitInstruction(Instruction &I) {
138 assert(0 && "Instruction not interpretable yet!");
141 GenericValue callExternalFunction(Function *F,
142 const std::vector<GenericValue> &ArgVals);
143 void exitCalled(GenericValue GV);
145 void addAtExitHandler(Function *F) {
146 AtExitHandlers.push_back(F);
149 GenericValue *getFirstVarArg () {
150 return &(ECStack[ECStack.size () - 2].VarArgs[0]);
155 GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
156 User::op_iterator E, ExecutionContext &SF);
158 private: // Helper functions
159 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
160 // PHI nodes in the top of the block. This is used for intraprocedural
163 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
165 void *getPointerToFunction(Function *F) { return (void*)F; }
167 void initializeExecutionEngine();
168 void initializeExternalFunctions();
169 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
170 GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
171 ExecutionContext &SF);
172 void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
175 } // End llvm namespace