Interpreter.h revision fdca74c5671e01447be0f1bac3c0c7aa1727690b
1//===-- Interpreter.h ------------------------------------------*- C++ -*--===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This header file defines the interpreter structure
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLI_INTERPRETER_H
15#define LLI_INTERPRETER_H
16
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"
24
25namespace llvm {
26
27class IntrinsicLowering;
28struct FunctionInfo;
29template<typename T> class generic_gep_type_iterator;
30class ConstantExpr;
31typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
32
33
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.
37//
38class AllocaHolder {
39  friend class AllocaHolderHandle;
40  std::vector<void*> Allocations;
41  unsigned RefCnt;
42public:
43  AllocaHolder() : RefCnt(0) {}
44  void add(void *mem) { Allocations.push_back(mem); }
45  ~AllocaHolder() {
46    for (unsigned i = 0; i < Allocations.size(); ++i)
47      free(Allocations[i]);
48  }
49};
50
51// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
52// a vector...
53//
54class AllocaHolderHandle {
55  AllocaHolder *H;
56public:
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; }
60
61  void add(void *mem) { H->add(mem); }
62};
63
64typedef std::vector<GenericValue> ValuePlaneTy;
65
66// ExecutionContext struct - This struct represents one stack frame currently
67// executing.
68//
69struct 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
78};
79
80// Interpreter - This class represents the entirety of the interpreter.
81//
82class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
83  GenericValue ExitValue;          // The return value of the called function
84  TargetData TD;
85  IntrinsicLowering *IL;
86
87  // The runtime stack of executing code.  The top of the stack is the current
88  // function record.
89  std::vector<ExecutionContext> ECStack;
90
91  // AtExitHandlers - List of functions to call when the program exits,
92  // registered with the atexit() library function.
93  std::vector<Function*> AtExitHandlers;
94
95public:
96  explicit Interpreter(ModuleProvider *M);
97  ~Interpreter();
98
99  /// runAtExitHandlers - Run any functions registered by the program's calls to
100  /// atexit(3), which we intercept and store in AtExitHandlers.
101  ///
102  void runAtExitHandlers();
103
104  static void Register() {
105    InterpCtor = create;
106  }
107
108  /// create - Create an interpreter ExecutionEngine. This can never fail.
109  ///
110  static ExecutionEngine *create(ModuleProvider *M, std::string *ErrorStr = 0,
111                                 CodeGenOpt::Level = CodeGenOpt::Default);
112
113  /// run - Start execution with the specified function and arguments.
114  ///
115  virtual GenericValue runFunction(Function *F,
116                                   const std::vector<GenericValue> &ArgValues);
117
118  /// recompileAndRelinkFunction - For the interpreter, functions are always
119  /// up-to-date.
120  ///
121  virtual void *recompileAndRelinkFunction(Function *F) {
122    return getPointerToFunction(F);
123  }
124
125  /// freeMachineCodeForFunction - The interpreter does not generate any code.
126  ///
127  void freeMachineCodeForFunction(Function *F) { }
128
129  // Methods used to execute code:
130  // Place a call on the stack
131  void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
132  void run();                // Execute instructions until nothing left to do
133
134  // Opcode Implementations
135  void visitReturnInst(ReturnInst &I);
136  void visitBranchInst(BranchInst &I);
137  void visitSwitchInst(SwitchInst &I);
138
139  void visitBinaryOperator(BinaryOperator &I);
140  void visitICmpInst(ICmpInst &I);
141  void visitFCmpInst(FCmpInst &I);
142  void visitAllocationInst(AllocationInst &I);
143  void visitFreeInst(FreeInst &I);
144  void visitLoadInst(LoadInst &I);
145  void visitStoreInst(StoreInst &I);
146  void visitGetElementPtrInst(GetElementPtrInst &I);
147  void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
148  void visitTruncInst(TruncInst &I);
149  void visitZExtInst(ZExtInst &I);
150  void visitSExtInst(SExtInst &I);
151  void visitFPTruncInst(FPTruncInst &I);
152  void visitFPExtInst(FPExtInst &I);
153  void visitUIToFPInst(UIToFPInst &I);
154  void visitSIToFPInst(SIToFPInst &I);
155  void visitFPToUIInst(FPToUIInst &I);
156  void visitFPToSIInst(FPToSIInst &I);
157  void visitPtrToIntInst(PtrToIntInst &I);
158  void visitIntToPtrInst(IntToPtrInst &I);
159  void visitBitCastInst(BitCastInst &I);
160  void visitSelectInst(SelectInst &I);
161
162
163  void visitCallSite(CallSite CS);
164  void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
165  void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
166  void visitUnwindInst(UnwindInst &I);
167  void visitUnreachableInst(UnreachableInst &I);
168
169  void visitShl(BinaryOperator &I);
170  void visitLShr(BinaryOperator &I);
171  void visitAShr(BinaryOperator &I);
172
173  void visitVAArgInst(VAArgInst &I);
174  void visitInstruction(Instruction &I) {
175    cerr << I;
176    assert(0 && "Instruction not interpretable yet!");
177  }
178
179  GenericValue callExternalFunction(Function *F,
180                                    const std::vector<GenericValue> &ArgVals);
181  void exitCalled(GenericValue GV);
182
183  void addAtExitHandler(Function *F) {
184    AtExitHandlers.push_back(F);
185  }
186
187  GenericValue *getFirstVarArg () {
188    return &(ECStack.back ().VarArgs[0]);
189  }
190
191  //FIXME: private:
192public:
193  GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
194                                   gep_type_iterator E, ExecutionContext &SF);
195
196private:  // Helper functions
197  // SwitchToNewBasicBlock - Start execution in a new basic block and run any
198  // PHI nodes in the top of the block.  This is used for intraprocedural
199  // control flow.
200  //
201  void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
202
203  void *getPointerToFunction(Function *F) { return (void*)F; }
204
205  void initializeExecutionEngine() { }
206  void initializeExternalFunctions();
207  GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
208  GenericValue getOperandValue(Value *V, ExecutionContext &SF);
209  GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy,
210                                ExecutionContext &SF);
211  GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy,
212                               ExecutionContext &SF);
213  GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy,
214                               ExecutionContext &SF);
215  GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy,
216                                  ExecutionContext &SF);
217  GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy,
218                                ExecutionContext &SF);
219  GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy,
220                                 ExecutionContext &SF);
221  GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy,
222                                 ExecutionContext &SF);
223  GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy,
224                                 ExecutionContext &SF);
225  GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy,
226                                 ExecutionContext &SF);
227  GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy,
228                                   ExecutionContext &SF);
229  GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
230                                   ExecutionContext &SF);
231  GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy,
232                                  ExecutionContext &SF);
233  GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
234                                    const Type *Ty, ExecutionContext &SF);
235  void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
236
237};
238
239} // End llvm namespace
240
241#endif
242