LLVM  3.7.0
lib/ExecutionEngine/Interpreter/Interpreter.h
Go to the documentation of this file.
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 LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
15 #define LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
16 
19 #include "llvm/IR/CallSite.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/InstVisitor.h"
23 #include "llvm/Support/DataTypes.h"
26 namespace llvm {
27 
28 class IntrinsicLowering;
29 struct FunctionInfo;
30 template<typename T> class generic_gep_type_iterator;
31 class ConstantExpr;
32 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
33 
34 
35 // AllocaHolder - Object to track all of the blocks of memory allocated by
36 // alloca. When the function returns, this object is popped off the execution
37 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
38 //
39 class AllocaHolder {
40  std::vector<void *> Allocations;
41 
42 public:
44 
45  // Make this type move-only. Define explicit move special members for MSVC.
46  AllocaHolder(AllocaHolder &&RHS) : Allocations(std::move(RHS.Allocations)) {}
48  Allocations = std::move(RHS.Allocations);
49  return *this;
50  }
51 
53  for (void *Allocation : Allocations)
54  free(Allocation);
55  }
56 
57  void add(void *Mem) { Allocations.push_back(Mem); }
58 };
59 
60 typedef std::vector<GenericValue> ValuePlaneTy;
61 
62 // ExecutionContext struct - This struct represents one stack frame currently
63 // executing.
64 //
66  Function *CurFunction;// The currently executing function
67  BasicBlock *CurBB; // The currently executing BB
68  BasicBlock::iterator CurInst; // The next instruction to execute
69  CallSite Caller; // Holds the call that called subframes.
70  // NULL if main func or debugger invoked fn
71  std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
72  std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
73  AllocaHolder Allocas; // Track memory allocated by alloca
74 
75  ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}
76 
79  Caller(O.Caller), Values(std::move(O.Values)),
80  VarArgs(std::move(O.VarArgs)), Allocas(std::move(O.Allocas)) {}
81 
83  CurFunction = O.CurFunction;
84  CurBB = O.CurBB;
85  CurInst = O.CurInst;
86  Caller = O.Caller;
87  Values = std::move(O.Values);
88  VarArgs = std::move(O.VarArgs);
89  Allocas = std::move(O.Allocas);
90  return *this;
91  }
92 };
93 
94 // Interpreter - This class represents the entirety of the interpreter.
95 //
96 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
97  GenericValue ExitValue; // The return value of the called function
98  DataLayout TD;
100 
101  // The runtime stack of executing code. The top of the stack is the current
102  // function record.
103  std::vector<ExecutionContext> ECStack;
104 
105  // AtExitHandlers - List of functions to call when the program exits,
106  // registered with the atexit() library function.
107  std::vector<Function*> AtExitHandlers;
108 
109 public:
110  explicit Interpreter(std::unique_ptr<Module> M);
111  ~Interpreter() override;
112 
113  /// runAtExitHandlers - Run any functions registered by the program's calls to
114  /// atexit(3), which we intercept and store in AtExitHandlers.
115  ///
116  void runAtExitHandlers();
117 
118  static void Register() {
119  InterpCtor = create;
120  }
121 
122  /// Create an interpreter ExecutionEngine.
123  ///
124  static ExecutionEngine *create(std::unique_ptr<Module> M,
125  std::string *ErrorStr = nullptr);
126 
127  /// run - Start execution with the specified function and arguments.
128  ///
130  ArrayRef<GenericValue> ArgValues) override;
131 
133  bool AbortOnFailure = true) override {
134  // FIXME: not implemented.
135  return nullptr;
136  }
137 
138  // Methods used to execute code:
139  // Place a call on the stack
141  void run(); // Execute instructions until nothing left to do
142 
143  // Opcode Implementations
148 
150  void visitICmpInst(ICmpInst &I);
151  void visitFCmpInst(FCmpInst &I);
153  void visitLoadInst(LoadInst &I);
154  void visitStoreInst(StoreInst &I);
156  void visitPHINode(PHINode &PN) {
157  llvm_unreachable("PHI nodes already handled!");
158  }
159  void visitTruncInst(TruncInst &I);
160  void visitZExtInst(ZExtInst &I);
161  void visitSExtInst(SExtInst &I);
163  void visitFPExtInst(FPExtInst &I);
172 
173 
174  void visitCallSite(CallSite CS);
178 
179  void visitShl(BinaryOperator &I);
180  void visitLShr(BinaryOperator &I);
181  void visitAShr(BinaryOperator &I);
182 
183  void visitVAArgInst(VAArgInst &I);
187 
190 
192  errs() << I << "\n";
193  llvm_unreachable("Instruction not interpretable yet!");
194  }
195 
197  ArrayRef<GenericValue> ArgVals);
198  void exitCalled(GenericValue GV);
199 
201  AtExitHandlers.push_back(F);
202  }
203 
205  return &(ECStack.back ().VarArgs[0]);
206  }
207 
208 private: // Helper functions
209  GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
211 
212  // SwitchToNewBasicBlock - Start execution in a new basic block and run any
213  // PHI nodes in the top of the block. This is used for intraprocedural
214  // control flow.
215  //
216  void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
217 
218  void *getPointerToFunction(Function *F) override { return (void*)F; }
219 
220  void initializeExecutionEngine() { }
221  void initializeExternalFunctions();
222  GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
223  GenericValue getOperandValue(Value *V, ExecutionContext &SF);
224  GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
225  ExecutionContext &SF);
226  GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
227  ExecutionContext &SF);
228  GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
229  ExecutionContext &SF);
230  GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
231  ExecutionContext &SF);
232  GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
233  ExecutionContext &SF);
234  GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
235  ExecutionContext &SF);
236  GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
237  ExecutionContext &SF);
238  GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
239  ExecutionContext &SF);
240  GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
241  ExecutionContext &SF);
242  GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
243  ExecutionContext &SF);
244  GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
245  ExecutionContext &SF);
246  GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
247  ExecutionContext &SF);
248  GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
249  Type *Ty, ExecutionContext &SF);
250  void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
251 
252 };
253 
254 } // End llvm namespace
255 
256 #endif
ExecutionContext & operator=(ExecutionContext &&O)
static ExecutionEngine * create(std::unique_ptr< Module > M, std::string *ErrorStr=nullptr)
Create an interpreter ExecutionEngine.
Definition: Interpreter.cpp:35
ReturnInst - Return a value (possibly void), from a function.
void visitVAArgInst(VAArgInst &I)
Definition: Execution.cpp:1718
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
generic_gep_type_iterator gep_type_iterator
std::vector< GenericValue > ValuePlaneTy
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
ExtractValueInst - This instruction extracts a struct member or array element value from an aggregate...
Base class for instruction visitors.
Definition: InstVisitor.h:81
Interpreter(std::unique_ptr< Module > M)
Definition: Interpreter.cpp:51
void visitAllocaInst(AllocaInst &I)
Definition: Execution.cpp:962
void visitStoreInst(StoreInst &I)
Definition: Execution.cpp:1050
void visitTruncInst(TruncInst &I)
Definition: Execution.cpp:1655
void visitFPToUIInst(FPToUIInst &I)
Definition: Execution.cpp:1690
This class represents zero extension of integer types.
void visitGetElementPtrInst(GetElementPtrInst &I)
Definition: Execution.cpp:1033
CallInst - This class represents a function call, abstracting a target machine's calling convention...
AllocaHolder & operator=(AllocaHolder &&RHS)
std::map< Value *, GenericValue > Values
ShuffleVectorInst - This instruction constructs a fixed permutation of two input vectors.
void visitExtractElementInst(ExtractElementInst &I)
Definition: Execution.cpp:1747
void visitShl(BinaryOperator &I)
Definition: Execution.cpp:1137
F(f)
This class represents a sign extension of integer types.
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
void visitFCmpInst(FCmpInst &I)
Definition: Execution.cpp:610
SelectInst - This class represents the LLVM 'select' instruction.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
void visitSelectInst(SelectInst &I)
Definition: Execution.cpp:806
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
This class represents a cast from a pointer to an integer.
void visitInsertElementInst(InsertElementInst &I)
Definition: Execution.cpp:1780
void visitFPTruncInst(FPTruncInst &I)
Definition: Execution.cpp:1670
This instruction compares its operands according to the predicate given to the constructor.
This class represents a no-op cast from one type to another.
void callFunction(Function *F, ArrayRef< GenericValue > ArgVals)
Definition: Execution.cpp:2076
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
void visitInsertValueInst(InsertValueInst &I)
Definition: Execution.cpp:1927
StoreInst - an instruction for storing to memory.
Definition: Instructions.h:316
This class represents a cast from floating point to signed integer.
void visitBitCastInst(BitCastInst &I)
Definition: Execution.cpp:1710
This class represents a truncation of integer types.
Local Stack Slot Allocation
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
Definition: Instructions.h:830
void visitLShr(BinaryOperator &I)
Definition: Execution.cpp:1164
void visitICmpInst(ICmpInst &I)
Definition: Execution.cpp:278
InsertElementInst - This instruction inserts a single (scalar) element into a VectorType value...
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
BranchInst - Conditional or Unconditional Branch instruction.
void visitCallSite(CallSite CS)
Definition: Execution.cpp:1064
UnreachableInst - This function has undefined behavior.
IndirectBrInst - Indirect Branch Instruction.
void visitIndirectBrInst(IndirectBrInst &I)
Definition: Execution.cpp:913
void visitZExtInst(ZExtInst &I)
Definition: Execution.cpp:1665
void exitCalled(GenericValue GV)
Definition: Execution.cpp:820
This instruction compares its operands according to the predicate given to the constructor.
void visitExtractValueInst(ExtractValueInst &I)
Definition: Execution.cpp:1885
This class represents a cast from an integer to a pointer.
void visitFPToSIInst(FPToSIInst &I)
Definition: Execution.cpp:1695
void visitIntToPtrInst(IntToPtrInst &I)
Definition: Execution.cpp:1705
void visitFPExtInst(FPExtInst &I)
Definition: Execution.cpp:1675
VAArgInst - This class represents the va_arg llvm instruction, which returns an argument of the speci...
~Interpreter() override
Definition: Interpreter.cpp:64
void visitUnreachableInst(UnreachableInst &I)
Definition: Execution.cpp:877
static ExecutionEngine *(* InterpCtor)(std::unique_ptr< Module > M, std::string *ErrorStr)
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
void visitShuffleVectorInst(ShuffleVectorInst &I)
Definition: Execution.cpp:1815
void visitReturnInst(ReturnInst &I)
Definition: Execution.cpp:863
void visitSExtInst(SExtInst &I)
Definition: Execution.cpp:1660
This class represents a cast from floating point to unsigned integer.
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
run - Start execution with the specified function and arguments.
Definition: Interpreter.cpp:78
void visitPtrToIntInst(PtrToIntInst &I)
Definition: Execution.cpp:1700
void runAtExitHandlers()
runAtExitHandlers - Run any functions registered by the program's calls to atexit(3), which we intercept and store in AtExitHandlers.
Definition: Interpreter.cpp:68
void visitBranchInst(BranchInst &I)
Definition: Execution.cpp:881
void visitAShr(BinaryOperator &I)
Definition: Execution.cpp:1191
#define I(x, y, z)
Definition: MD5.cpp:54
void visitSIToFPInst(SIToFPInst &I)
Definition: Execution.cpp:1685
This class represents a cast unsigned integer to floating point.
ExtractElementInst - This instruction extracts a single (scalar) element from a VectorType value...
void visitBinaryOperator(BinaryOperator &I)
Definition: Execution.cpp:681
void visitLoadInst(LoadInst &I)
Definition: Execution.cpp:1039
SwitchInst - Multiway switch.
This class represents a cast from signed integer to floating point.
This class represents a truncation of floating point types.
LLVM Value Representation.
Definition: Value.h:69
void visitUIToFPInst(UIToFPInst &I)
Definition: Execution.cpp:1680
InvokeInst - Invoke instruction.
GenericValue callExternalFunction(Function *F, ArrayRef< GenericValue > ArgVals)
void visitSwitchInst(SwitchInst &I)
Definition: Execution.cpp:894
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
This class represents an extension of floating point types.
AllocaInst - an instruction to allocate memory on the stack.
Definition: Instructions.h:76
InsertValueInst - This instruction inserts a struct field of array element value into an aggregate va...