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/ExecutionEngine/ExecutionEngine.h"
18193323Sed#include "llvm/ExecutionEngine/GenericValue.h"
19249423Sdim#include "llvm/IR/DataLayout.h"
20249423Sdim#include "llvm/IR/Function.h"
21249423Sdim#include "llvm/InstVisitor.h"
22193323Sed#include "llvm/Support/CallSite.h"
23218893Sdim#include "llvm/Support/DataTypes.h"
24198090Srdivacky#include "llvm/Support/ErrorHandling.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
85243830Sdim  DataLayout 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
118234353Sdim  virtual void *getPointerToNamedFunction(const std::string &Name,
119234353Sdim                                          bool AbortOnFailure = true) {
120234353Sdim    // FIXME: not implemented.
121234353Sdim    return 0;
122234353Sdim  }
123234353Sdim
124193323Sed  /// recompileAndRelinkFunction - For the interpreter, functions are always
125193323Sed  /// up-to-date.
126193323Sed  ///
127193323Sed  virtual void *recompileAndRelinkFunction(Function *F) {
128193323Sed    return getPointerToFunction(F);
129193323Sed  }
130193323Sed
131193323Sed  /// freeMachineCodeForFunction - The interpreter does not generate any code.
132193323Sed  ///
133193323Sed  void freeMachineCodeForFunction(Function *F) { }
134193323Sed
135193323Sed  // Methods used to execute code:
136193323Sed  // Place a call on the stack
137193323Sed  void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
138193323Sed  void run();                // Execute instructions until nothing left to do
139193323Sed
140193323Sed  // Opcode Implementations
141193323Sed  void visitReturnInst(ReturnInst &I);
142193323Sed  void visitBranchInst(BranchInst &I);
143193323Sed  void visitSwitchInst(SwitchInst &I);
144198892Srdivacky  void visitIndirectBrInst(IndirectBrInst &I);
145193323Sed
146193323Sed  void visitBinaryOperator(BinaryOperator &I);
147193323Sed  void visitICmpInst(ICmpInst &I);
148193323Sed  void visitFCmpInst(FCmpInst &I);
149198892Srdivacky  void visitAllocaInst(AllocaInst &I);
150193323Sed  void visitLoadInst(LoadInst &I);
151193323Sed  void visitStoreInst(StoreInst &I);
152193323Sed  void visitGetElementPtrInst(GetElementPtrInst &I);
153198090Srdivacky  void visitPHINode(PHINode &PN) {
154198090Srdivacky    llvm_unreachable("PHI nodes already handled!");
155198090Srdivacky  }
156193323Sed  void visitTruncInst(TruncInst &I);
157193323Sed  void visitZExtInst(ZExtInst &I);
158193323Sed  void visitSExtInst(SExtInst &I);
159193323Sed  void visitFPTruncInst(FPTruncInst &I);
160193323Sed  void visitFPExtInst(FPExtInst &I);
161193323Sed  void visitUIToFPInst(UIToFPInst &I);
162193323Sed  void visitSIToFPInst(SIToFPInst &I);
163193323Sed  void visitFPToUIInst(FPToUIInst &I);
164193323Sed  void visitFPToSIInst(FPToSIInst &I);
165193323Sed  void visitPtrToIntInst(PtrToIntInst &I);
166193323Sed  void visitIntToPtrInst(IntToPtrInst &I);
167193323Sed  void visitBitCastInst(BitCastInst &I);
168193323Sed  void visitSelectInst(SelectInst &I);
169193323Sed
170193323Sed
171193323Sed  void visitCallSite(CallSite CS);
172193323Sed  void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
173193323Sed  void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
174193323Sed  void visitUnreachableInst(UnreachableInst &I);
175193323Sed
176193323Sed  void visitShl(BinaryOperator &I);
177193323Sed  void visitLShr(BinaryOperator &I);
178193323Sed  void visitAShr(BinaryOperator &I);
179193323Sed
180193323Sed  void visitVAArgInst(VAArgInst &I);
181249423Sdim  void visitExtractElementInst(ExtractElementInst &I);
182193323Sed  void visitInstruction(Instruction &I) {
183226633Sdim    errs() << I << "\n";
184198090Srdivacky    llvm_unreachable("Instruction not interpretable yet!");
185193323Sed  }
186193323Sed
187193323Sed  GenericValue callExternalFunction(Function *F,
188193323Sed                                    const std::vector<GenericValue> &ArgVals);
189193323Sed  void exitCalled(GenericValue GV);
190193323Sed
191193323Sed  void addAtExitHandler(Function *F) {
192193323Sed    AtExitHandlers.push_back(F);
193193323Sed  }
194193323Sed
195193323Sed  GenericValue *getFirstVarArg () {
196193323Sed    return &(ECStack.back ().VarArgs[0]);
197193323Sed  }
198193323Sed
199207618Srdivackyprivate:  // Helper functions
200193323Sed  GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
201193323Sed                                   gep_type_iterator E, ExecutionContext &SF);
202193323Sed
203193323Sed  // SwitchToNewBasicBlock - Start execution in a new basic block and run any
204193323Sed  // PHI nodes in the top of the block.  This is used for intraprocedural
205193323Sed  // control flow.
206193323Sed  //
207193323Sed  void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
208193323Sed
209193323Sed  void *getPointerToFunction(Function *F) { return (void*)F; }
210198892Srdivacky  void *getPointerToBasicBlock(BasicBlock *BB) { return (void*)BB; }
211193323Sed
212195098Sed  void initializeExecutionEngine() { }
213193323Sed  void initializeExternalFunctions();
214193323Sed  GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
215193323Sed  GenericValue getOperandValue(Value *V, ExecutionContext &SF);
216226633Sdim  GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
217193323Sed                                ExecutionContext &SF);
218226633Sdim  GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
219193323Sed                               ExecutionContext &SF);
220226633Sdim  GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
221193323Sed                               ExecutionContext &SF);
222226633Sdim  GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
223193323Sed                                  ExecutionContext &SF);
224226633Sdim  GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
225193323Sed                                ExecutionContext &SF);
226226633Sdim  GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
227193323Sed                                 ExecutionContext &SF);
228226633Sdim  GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
229193323Sed                                 ExecutionContext &SF);
230226633Sdim  GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
231193323Sed                                 ExecutionContext &SF);
232226633Sdim  GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
233193323Sed                                 ExecutionContext &SF);
234226633Sdim  GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
235193323Sed                                   ExecutionContext &SF);
236226633Sdim  GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
237193323Sed                                   ExecutionContext &SF);
238226633Sdim  GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
239193323Sed                                  ExecutionContext &SF);
240193323Sed  GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
241226633Sdim                                    Type *Ty, ExecutionContext &SF);
242226633Sdim  void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
243193323Sed
244193323Sed};
245193323Sed
246193323Sed} // End llvm namespace
247193323Sed
248193323Sed#endif
249