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
14280031Sdim#ifndef LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
15280031Sdim#define LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
16193323Sed
17193323Sed#include "llvm/ExecutionEngine/ExecutionEngine.h"
18193323Sed#include "llvm/ExecutionEngine/GenericValue.h"
19276479Sdim#include "llvm/IR/CallSite.h"
20249423Sdim#include "llvm/IR/DataLayout.h"
21249423Sdim#include "llvm/IR/Function.h"
22276479Sdim#include "llvm/IR/InstVisitor.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;
29193323Sedtemplate<typename T> class generic_gep_type_iterator;
30193323Sedclass ConstantExpr;
31193323Sedtypedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
32193323Sed
33193323Sed
34193323Sed// AllocaHolder - Object to track all of the blocks of memory allocated by
35193323Sed// alloca.  When the function returns, this object is popped off the execution
36193323Sed// stack, which causes the dtor to be run, which frees all the alloca'd memory.
37193323Sed//
38193323Sedclass AllocaHolder {
39280031Sdim  std::vector<void *> Allocations;
40280031Sdim
41193323Sedpublic:
42280031Sdim  AllocaHolder() {}
43280031Sdim
44280031Sdim  // Make this type move-only. Define explicit move special members for MSVC.
45280031Sdim  AllocaHolder(AllocaHolder &&RHS) : Allocations(std::move(RHS.Allocations)) {}
46280031Sdim  AllocaHolder &operator=(AllocaHolder &&RHS) {
47280031Sdim    Allocations = std::move(RHS.Allocations);
48280031Sdim    return *this;
49280031Sdim  }
50280031Sdim
51193323Sed  ~AllocaHolder() {
52280031Sdim    for (void *Allocation : Allocations)
53280031Sdim      free(Allocation);
54193323Sed  }
55193323Sed
56280031Sdim  void add(void *Mem) { Allocations.push_back(Mem); }
57193323Sed};
58193323Sed
59193323Sedtypedef std::vector<GenericValue> ValuePlaneTy;
60193323Sed
61193323Sed// ExecutionContext struct - This struct represents one stack frame currently
62193323Sed// executing.
63193323Sed//
64193323Sedstruct ExecutionContext {
65193323Sed  Function             *CurFunction;// The currently executing function
66193323Sed  BasicBlock           *CurBB;      // The currently executing BB
67193323Sed  BasicBlock::iterator  CurInst;    // The next instruction to execute
68280031Sdim  CallSite             Caller;     // Holds the call that called subframes.
69280031Sdim                                   // NULL if main func or debugger invoked fn
70193323Sed  std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
71193323Sed  std::vector<GenericValue>  VarArgs; // Values passed through an ellipsis
72280031Sdim  AllocaHolder Allocas;            // Track memory allocated by alloca
73280031Sdim
74280031Sdim  ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}
75280031Sdim
76280031Sdim  ExecutionContext(ExecutionContext &&O)
77280031Sdim      : CurFunction(O.CurFunction), CurBB(O.CurBB), CurInst(O.CurInst),
78280031Sdim        Caller(O.Caller), Values(std::move(O.Values)),
79280031Sdim        VarArgs(std::move(O.VarArgs)), Allocas(std::move(O.Allocas)) {}
80280031Sdim
81280031Sdim  ExecutionContext &operator=(ExecutionContext &&O) {
82280031Sdim    CurFunction = O.CurFunction;
83280031Sdim    CurBB = O.CurBB;
84280031Sdim    CurInst = O.CurInst;
85280031Sdim    Caller = O.Caller;
86280031Sdim    Values = std::move(O.Values);
87280031Sdim    VarArgs = std::move(O.VarArgs);
88280031Sdim    Allocas = std::move(O.Allocas);
89280031Sdim    return *this;
90280031Sdim  }
91193323Sed};
92193323Sed
93193323Sed// Interpreter - This class represents the entirety of the interpreter.
94193323Sed//
95193323Sedclass Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
96193323Sed  GenericValue ExitValue;          // The return value of the called function
97193323Sed  IntrinsicLowering *IL;
98193323Sed
99193323Sed  // The runtime stack of executing code.  The top of the stack is the current
100193323Sed  // function record.
101193323Sed  std::vector<ExecutionContext> ECStack;
102193323Sed
103193323Sed  // AtExitHandlers - List of functions to call when the program exits,
104193323Sed  // registered with the atexit() library function.
105193323Sed  std::vector<Function*> AtExitHandlers;
106193323Sed
107193323Sedpublic:
108280031Sdim  explicit Interpreter(std::unique_ptr<Module> M);
109288943Sdim  ~Interpreter() override;
110193323Sed
111193323Sed  /// runAtExitHandlers - Run any functions registered by the program's calls to
112193323Sed  /// atexit(3), which we intercept and store in AtExitHandlers.
113193323Sed  ///
114193323Sed  void runAtExitHandlers();
115193323Sed
116193323Sed  static void Register() {
117193323Sed    InterpCtor = create;
118193323Sed  }
119280031Sdim
120280031Sdim  /// Create an interpreter ExecutionEngine.
121193323Sed  ///
122280031Sdim  static ExecutionEngine *create(std::unique_ptr<Module> M,
123280031Sdim                                 std::string *ErrorStr = nullptr);
124193323Sed
125193323Sed  /// run - Start execution with the specified function and arguments.
126193323Sed  ///
127276479Sdim  GenericValue runFunction(Function *F,
128288943Sdim                           ArrayRef<GenericValue> ArgValues) override;
129193323Sed
130280031Sdim  void *getPointerToNamedFunction(StringRef Name,
131276479Sdim                                  bool AbortOnFailure = true) override {
132234353Sdim    // FIXME: not implemented.
133276479Sdim    return nullptr;
134234353Sdim  }
135234353Sdim
136193323Sed  // Methods used to execute code:
137193323Sed  // Place a call on the stack
138288943Sdim  void callFunction(Function *F, ArrayRef<GenericValue> ArgVals);
139193323Sed  void run();                // Execute instructions until nothing left to do
140193323Sed
141193323Sed  // Opcode Implementations
142193323Sed  void visitReturnInst(ReturnInst &I);
143193323Sed  void visitBranchInst(BranchInst &I);
144193323Sed  void visitSwitchInst(SwitchInst &I);
145198892Srdivacky  void visitIndirectBrInst(IndirectBrInst &I);
146193323Sed
147193323Sed  void visitBinaryOperator(BinaryOperator &I);
148193323Sed  void visitICmpInst(ICmpInst &I);
149193323Sed  void visitFCmpInst(FCmpInst &I);
150198892Srdivacky  void visitAllocaInst(AllocaInst &I);
151193323Sed  void visitLoadInst(LoadInst &I);
152193323Sed  void visitStoreInst(StoreInst &I);
153193323Sed  void visitGetElementPtrInst(GetElementPtrInst &I);
154198090Srdivacky  void visitPHINode(PHINode &PN) {
155198090Srdivacky    llvm_unreachable("PHI nodes already handled!");
156198090Srdivacky  }
157193323Sed  void visitTruncInst(TruncInst &I);
158193323Sed  void visitZExtInst(ZExtInst &I);
159193323Sed  void visitSExtInst(SExtInst &I);
160193323Sed  void visitFPTruncInst(FPTruncInst &I);
161193323Sed  void visitFPExtInst(FPExtInst &I);
162193323Sed  void visitUIToFPInst(UIToFPInst &I);
163193323Sed  void visitSIToFPInst(SIToFPInst &I);
164193323Sed  void visitFPToUIInst(FPToUIInst &I);
165193323Sed  void visitFPToSIInst(FPToSIInst &I);
166193323Sed  void visitPtrToIntInst(PtrToIntInst &I);
167193323Sed  void visitIntToPtrInst(IntToPtrInst &I);
168193323Sed  void visitBitCastInst(BitCastInst &I);
169193323Sed  void visitSelectInst(SelectInst &I);
170193323Sed
171193323Sed
172193323Sed  void visitCallSite(CallSite CS);
173193323Sed  void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
174193323Sed  void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
175193323Sed  void visitUnreachableInst(UnreachableInst &I);
176193323Sed
177193323Sed  void visitShl(BinaryOperator &I);
178193323Sed  void visitLShr(BinaryOperator &I);
179193323Sed  void visitAShr(BinaryOperator &I);
180193323Sed
181193323Sed  void visitVAArgInst(VAArgInst &I);
182249423Sdim  void visitExtractElementInst(ExtractElementInst &I);
183261991Sdim  void visitInsertElementInst(InsertElementInst &I);
184261991Sdim  void visitShuffleVectorInst(ShuffleVectorInst &I);
185261991Sdim
186261991Sdim  void visitExtractValueInst(ExtractValueInst &I);
187261991Sdim  void visitInsertValueInst(InsertValueInst &I);
188261991Sdim
189193323Sed  void visitInstruction(Instruction &I) {
190226633Sdim    errs() << I << "\n";
191198090Srdivacky    llvm_unreachable("Instruction not interpretable yet!");
192193323Sed  }
193193323Sed
194193323Sed  GenericValue callExternalFunction(Function *F,
195288943Sdim                                    ArrayRef<GenericValue> ArgVals);
196193323Sed  void exitCalled(GenericValue GV);
197193323Sed
198193323Sed  void addAtExitHandler(Function *F) {
199193323Sed    AtExitHandlers.push_back(F);
200193323Sed  }
201193323Sed
202193323Sed  GenericValue *getFirstVarArg () {
203193323Sed    return &(ECStack.back ().VarArgs[0]);
204193323Sed  }
205193323Sed
206207618Srdivackyprivate:  // Helper functions
207193323Sed  GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
208193323Sed                                   gep_type_iterator E, ExecutionContext &SF);
209193323Sed
210193323Sed  // SwitchToNewBasicBlock - Start execution in a new basic block and run any
211193323Sed  // PHI nodes in the top of the block.  This is used for intraprocedural
212193323Sed  // control flow.
213193323Sed  //
214193323Sed  void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
215193323Sed
216276479Sdim  void *getPointerToFunction(Function *F) override { return (void*)F; }
217193323Sed
218195098Sed  void initializeExecutionEngine() { }
219193323Sed  void initializeExternalFunctions();
220193323Sed  GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
221193323Sed  GenericValue getOperandValue(Value *V, ExecutionContext &SF);
222226633Sdim  GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
223193323Sed                                ExecutionContext &SF);
224226633Sdim  GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
225193323Sed                               ExecutionContext &SF);
226226633Sdim  GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
227193323Sed                               ExecutionContext &SF);
228226633Sdim  GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
229193323Sed                                  ExecutionContext &SF);
230226633Sdim  GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
231193323Sed                                ExecutionContext &SF);
232226633Sdim  GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
233193323Sed                                 ExecutionContext &SF);
234226633Sdim  GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
235193323Sed                                 ExecutionContext &SF);
236226633Sdim  GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
237193323Sed                                 ExecutionContext &SF);
238226633Sdim  GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
239193323Sed                                 ExecutionContext &SF);
240226633Sdim  GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
241193323Sed                                   ExecutionContext &SF);
242226633Sdim  GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
243193323Sed                                   ExecutionContext &SF);
244226633Sdim  GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
245193323Sed                                  ExecutionContext &SF);
246193323Sed  GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
247226633Sdim                                    Type *Ty, ExecutionContext &SF);
248226633Sdim  void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
249193323Sed
250193323Sed};
251193323Sed
252193323Sed} // End llvm namespace
253193323Sed
254193323Sed#endif
255