Interpreter.h revision 198090
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"
22193323Sed#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:
97193323Sed  explicit Interpreter(ModuleProvider *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  ///
111198090Srdivacky  static ExecutionEngine *create(ModuleProvider *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);
138193323Sed
139193323Sed  void visitBinaryOperator(BinaryOperator &I);
140193323Sed  void visitICmpInst(ICmpInst &I);
141193323Sed  void visitFCmpInst(FCmpInst &I);
142193323Sed  void visitAllocationInst(AllocationInst &I);
143193323Sed  void visitFreeInst(FreeInst &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
193193323Sed  //FIXME: private:
194193323Sedpublic:
195193323Sed  GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
196193323Sed                                   gep_type_iterator E, ExecutionContext &SF);
197193323Sed
198193323Sedprivate:  // Helper functions
199193323Sed  // SwitchToNewBasicBlock - Start execution in a new basic block and run any
200193323Sed  // PHI nodes in the top of the block.  This is used for intraprocedural
201193323Sed  // control flow.
202193323Sed  //
203193323Sed  void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
204193323Sed
205193323Sed  void *getPointerToFunction(Function *F) { return (void*)F; }
206193323Sed
207195098Sed  void initializeExecutionEngine() { }
208193323Sed  void initializeExternalFunctions();
209193323Sed  GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
210193323Sed  GenericValue getOperandValue(Value *V, ExecutionContext &SF);
211193323Sed  GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy,
212193323Sed                                ExecutionContext &SF);
213193323Sed  GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy,
214193323Sed                               ExecutionContext &SF);
215193323Sed  GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy,
216193323Sed                               ExecutionContext &SF);
217193323Sed  GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy,
218193323Sed                                  ExecutionContext &SF);
219193323Sed  GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy,
220193323Sed                                ExecutionContext &SF);
221193323Sed  GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy,
222193323Sed                                 ExecutionContext &SF);
223193323Sed  GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy,
224193323Sed                                 ExecutionContext &SF);
225193323Sed  GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy,
226193323Sed                                 ExecutionContext &SF);
227193323Sed  GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy,
228193323Sed                                 ExecutionContext &SF);
229193323Sed  GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy,
230193323Sed                                   ExecutionContext &SF);
231193323Sed  GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
232193323Sed                                   ExecutionContext &SF);
233193323Sed  GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy,
234193323Sed                                  ExecutionContext &SF);
235193323Sed  GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
236193323Sed                                    const Type *Ty, ExecutionContext &SF);
237193323Sed  void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
238193323Sed
239193323Sed};
240193323Sed
241193323Sed} // End llvm namespace
242193323Sed
243193323Sed#endif
244