Interpreter.h revision 218893
1193323Sed//===-- Interpreter.h ------------------------------------------*- C++ -*--===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This header file defines the interpreter structure
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed#ifndef LLI_INTERPRETER_H
15193323Sed#define LLI_INTERPRETER_H
16193323Sed
17193323Sed#include "llvm/Function.h"
18193323Sed#include "llvm/ExecutionEngine/ExecutionEngine.h"
19193323Sed#include "llvm/ExecutionEngine/GenericValue.h"
20198090Srdivacky#include "llvm/Target/TargetData.h"
21193323Sed#include "llvm/Support/CallSite.h"
22218893Sdim#include "llvm/Support/DataTypes.h"
23198090Srdivacky#include "llvm/Support/ErrorHandling.h"
24198090Srdivacky#include "llvm/Support/InstVisitor.h"
25198090Srdivacky#include "llvm/Support/raw_ostream.h"
26193323Sednamespace llvm {
27193323Sed
28193323Sedclass IntrinsicLowering;
29193323Sedstruct FunctionInfo;
30193323Sedtemplate<typename T> class generic_gep_type_iterator;
31193323Sedclass ConstantExpr;
32193323Sedtypedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
33193323Sed
34193323Sed
35193323Sed// AllocaHolder - Object to track all of the blocks of memory allocated by
36193323Sed// alloca.  When the function returns, this object is popped off the execution
37193323Sed// stack, which causes the dtor to be run, which frees all the alloca'd memory.
38193323Sed//
39193323Sedclass AllocaHolder {
40193323Sed  friend class AllocaHolderHandle;
41193323Sed  std::vector<void*> Allocations;
42193323Sed  unsigned RefCnt;
43193323Sedpublic:
44193323Sed  AllocaHolder() : RefCnt(0) {}
45193323Sed  void add(void *mem) { Allocations.push_back(mem); }
46193323Sed  ~AllocaHolder() {
47193323Sed    for (unsigned i = 0; i < Allocations.size(); ++i)
48193323Sed      free(Allocations[i]);
49193323Sed  }
50193323Sed};
51193323Sed
52193323Sed// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
53193323Sed// a vector...
54193323Sed//
55193323Sedclass AllocaHolderHandle {
56193323Sed  AllocaHolder *H;
57193323Sedpublic:
58193323Sed  AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
59193323Sed  AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
60193323Sed  ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
61193323Sed
62193323Sed  void add(void *mem) { H->add(mem); }
63193323Sed};
64193323Sed
65193323Sedtypedef std::vector<GenericValue> ValuePlaneTy;
66193323Sed
67193323Sed// ExecutionContext struct - This struct represents one stack frame currently
68193323Sed// executing.
69193323Sed//
70193323Sedstruct ExecutionContext {
71193323Sed  Function             *CurFunction;// The currently executing function
72193323Sed  BasicBlock           *CurBB;      // The currently executing BB
73193323Sed  BasicBlock::iterator  CurInst;    // The next instruction to execute
74193323Sed  std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
75193323Sed  std::vector<GenericValue>  VarArgs; // Values passed through an ellipsis
76193323Sed  CallSite             Caller;     // Holds the call that called subframes.
77193323Sed                                   // NULL if main func or debugger invoked fn
78193323Sed  AllocaHolderHandle    Allocas;    // Track memory allocated by alloca
79193323Sed};
80193323Sed
81193323Sed// Interpreter - This class represents the entirety of the interpreter.
82193323Sed//
83193323Sedclass Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
84193323Sed  GenericValue ExitValue;          // The return value of the called function
85193323Sed  TargetData TD;
86193323Sed  IntrinsicLowering *IL;
87193323Sed
88193323Sed  // The runtime stack of executing code.  The top of the stack is the current
89193323Sed  // function record.
90193323Sed  std::vector<ExecutionContext> ECStack;
91193323Sed
92193323Sed  // AtExitHandlers - List of functions to call when the program exits,
93193323Sed  // registered with the atexit() library function.
94193323Sed  std::vector<Function*> AtExitHandlers;
95193323Sed
96193323Sedpublic:
97203954Srdivacky  explicit Interpreter(Module *M);
98193323Sed  ~Interpreter();
99193323Sed
100193323Sed  /// runAtExitHandlers - Run any functions registered by the program's calls to
101193323Sed  /// atexit(3), which we intercept and store in AtExitHandlers.
102193323Sed  ///
103193323Sed  void runAtExitHandlers();
104193323Sed
105193323Sed  static void Register() {
106193323Sed    InterpCtor = create;
107193323Sed  }
108193323Sed
109193323Sed  /// create - Create an interpreter ExecutionEngine. This can never fail.
110193323Sed  ///
111203954Srdivacky  static ExecutionEngine *create(Module *M, std::string *ErrorStr = 0);
112193323Sed
113193323Sed  /// run - Start execution with the specified function and arguments.
114193323Sed  ///
115193323Sed  virtual GenericValue runFunction(Function *F,
116193323Sed                                   const std::vector<GenericValue> &ArgValues);
117193323Sed
118193323Sed  /// recompileAndRelinkFunction - For the interpreter, functions are always
119193323Sed  /// up-to-date.
120193323Sed  ///
121193323Sed  virtual void *recompileAndRelinkFunction(Function *F) {
122193323Sed    return getPointerToFunction(F);
123193323Sed  }
124193323Sed
125193323Sed  /// freeMachineCodeForFunction - The interpreter does not generate any code.
126193323Sed  ///
127193323Sed  void freeMachineCodeForFunction(Function *F) { }
128193323Sed
129193323Sed  // Methods used to execute code:
130193323Sed  // Place a call on the stack
131193323Sed  void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
132193323Sed  void run();                // Execute instructions until nothing left to do
133193323Sed
134193323Sed  // Opcode Implementations
135193323Sed  void visitReturnInst(ReturnInst &I);
136193323Sed  void visitBranchInst(BranchInst &I);
137193323Sed  void visitSwitchInst(SwitchInst &I);
138198892Srdivacky  void visitIndirectBrInst(IndirectBrInst &I);
139193323Sed
140193323Sed  void visitBinaryOperator(BinaryOperator &I);
141193323Sed  void visitICmpInst(ICmpInst &I);
142193323Sed  void visitFCmpInst(FCmpInst &I);
143198892Srdivacky  void visitAllocaInst(AllocaInst &I);
144193323Sed  void visitLoadInst(LoadInst &I);
145193323Sed  void visitStoreInst(StoreInst &I);
146193323Sed  void visitGetElementPtrInst(GetElementPtrInst &I);
147198090Srdivacky  void visitPHINode(PHINode &PN) {
148198090Srdivacky    llvm_unreachable("PHI nodes already handled!");
149198090Srdivacky  }
150193323Sed  void visitTruncInst(TruncInst &I);
151193323Sed  void visitZExtInst(ZExtInst &I);
152193323Sed  void visitSExtInst(SExtInst &I);
153193323Sed  void visitFPTruncInst(FPTruncInst &I);
154193323Sed  void visitFPExtInst(FPExtInst &I);
155193323Sed  void visitUIToFPInst(UIToFPInst &I);
156193323Sed  void visitSIToFPInst(SIToFPInst &I);
157193323Sed  void visitFPToUIInst(FPToUIInst &I);
158193323Sed  void visitFPToSIInst(FPToSIInst &I);
159193323Sed  void visitPtrToIntInst(PtrToIntInst &I);
160193323Sed  void visitIntToPtrInst(IntToPtrInst &I);
161193323Sed  void visitBitCastInst(BitCastInst &I);
162193323Sed  void visitSelectInst(SelectInst &I);
163193323Sed
164193323Sed
165193323Sed  void visitCallSite(CallSite CS);
166193323Sed  void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
167193323Sed  void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
168193323Sed  void visitUnwindInst(UnwindInst &I);
169193323Sed  void visitUnreachableInst(UnreachableInst &I);
170193323Sed
171193323Sed  void visitShl(BinaryOperator &I);
172193323Sed  void visitLShr(BinaryOperator &I);
173193323Sed  void visitAShr(BinaryOperator &I);
174193323Sed
175193323Sed  void visitVAArgInst(VAArgInst &I);
176193323Sed  void visitInstruction(Instruction &I) {
177198090Srdivacky    errs() << I;
178198090Srdivacky    llvm_unreachable("Instruction not interpretable yet!");
179193323Sed  }
180193323Sed
181193323Sed  GenericValue callExternalFunction(Function *F,
182193323Sed                                    const std::vector<GenericValue> &ArgVals);
183193323Sed  void exitCalled(GenericValue GV);
184193323Sed
185193323Sed  void addAtExitHandler(Function *F) {
186193323Sed    AtExitHandlers.push_back(F);
187193323Sed  }
188193323Sed
189193323Sed  GenericValue *getFirstVarArg () {
190193323Sed    return &(ECStack.back ().VarArgs[0]);
191193323Sed  }
192193323Sed
193207618Srdivackyprivate:  // Helper functions
194193323Sed  GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
195193323Sed                                   gep_type_iterator E, ExecutionContext &SF);
196193323Sed
197193323Sed  // SwitchToNewBasicBlock - Start execution in a new basic block and run any
198193323Sed  // PHI nodes in the top of the block.  This is used for intraprocedural
199193323Sed  // control flow.
200193323Sed  //
201193323Sed  void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
202193323Sed
203193323Sed  void *getPointerToFunction(Function *F) { return (void*)F; }
204198892Srdivacky  void *getPointerToBasicBlock(BasicBlock *BB) { return (void*)BB; }
205193323Sed
206195098Sed  void initializeExecutionEngine() { }
207193323Sed  void initializeExternalFunctions();
208193323Sed  GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
209193323Sed  GenericValue getOperandValue(Value *V, ExecutionContext &SF);
210193323Sed  GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy,
211193323Sed                                ExecutionContext &SF);
212193323Sed  GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy,
213193323Sed                               ExecutionContext &SF);
214193323Sed  GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy,
215193323Sed                               ExecutionContext &SF);
216193323Sed  GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy,
217193323Sed                                  ExecutionContext &SF);
218193323Sed  GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy,
219193323Sed                                ExecutionContext &SF);
220193323Sed  GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy,
221193323Sed                                 ExecutionContext &SF);
222193323Sed  GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy,
223193323Sed                                 ExecutionContext &SF);
224193323Sed  GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy,
225193323Sed                                 ExecutionContext &SF);
226193323Sed  GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy,
227193323Sed                                 ExecutionContext &SF);
228193323Sed  GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy,
229193323Sed                                   ExecutionContext &SF);
230193323Sed  GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
231193323Sed                                   ExecutionContext &SF);
232193323Sed  GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy,
233193323Sed                                  ExecutionContext &SF);
234193323Sed  GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
235193323Sed                                    const Type *Ty, ExecutionContext &SF);
236193323Sed  void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
237193323Sed
238193323Sed};
239193323Sed
240193323Sed} // End llvm namespace
241193323Sed
242193323Sed#endif
243