LLVM  4.0.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 template<typename T> class generic_gep_type_iterator;
30 class ConstantExpr;
31 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
32 
33 
34 // AllocaHolder - Object to track all of the blocks of memory allocated by
35 // alloca. When the function returns, this object is popped off the execution
36 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
37 //
38 class AllocaHolder {
39  std::vector<void *> Allocations;
40 
41 public:
43 
44  // Make this type move-only.
45  AllocaHolder(AllocaHolder &&) = default;
46  AllocaHolder &operator=(AllocaHolder &&RHS) = default;
47 
49  for (void *Allocation : Allocations)
50  free(Allocation);
51  }
52 
53  void add(void *Mem) { Allocations.push_back(Mem); }
54 };
55 
56 typedef std::vector<GenericValue> ValuePlaneTy;
57 
58 // ExecutionContext struct - This struct represents one stack frame currently
59 // executing.
60 //
62  Function *CurFunction;// The currently executing function
63  BasicBlock *CurBB; // The currently executing BB
64  BasicBlock::iterator CurInst; // The next instruction to execute
65  CallSite Caller; // Holds the call that called subframes.
66  // NULL if main func or debugger invoked fn
67  std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
68  std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
69  AllocaHolder Allocas; // Track memory allocated by alloca
70 
71  ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}
72 };
73 
74 // Interpreter - This class represents the entirety of the interpreter.
75 //
76 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
77  GenericValue ExitValue; // The return value of the called function
79 
80  // The runtime stack of executing code. The top of the stack is the current
81  // function record.
82  std::vector<ExecutionContext> ECStack;
83 
84  // AtExitHandlers - List of functions to call when the program exits,
85  // registered with the atexit() library function.
86  std::vector<Function*> AtExitHandlers;
87 
88 public:
89  explicit Interpreter(std::unique_ptr<Module> M);
90  ~Interpreter() override;
91 
92  /// runAtExitHandlers - Run any functions registered by the program's calls to
93  /// atexit(3), which we intercept and store in AtExitHandlers.
94  ///
95  void runAtExitHandlers();
96 
97  static void Register() {
99  }
100 
101  /// Create an interpreter ExecutionEngine.
102  ///
103  static ExecutionEngine *create(std::unique_ptr<Module> M,
104  std::string *ErrorStr = nullptr);
105 
106  /// run - Start execution with the specified function and arguments.
107  ///
109  ArrayRef<GenericValue> ArgValues) override;
110 
112  bool AbortOnFailure = true) override {
113  // FIXME: not implemented.
114  return nullptr;
115  }
116 
117  // Methods used to execute code:
118  // Place a call on the stack
120  void run(); // Execute instructions until nothing left to do
121 
122  // Opcode Implementations
127 
129  void visitICmpInst(ICmpInst &I);
130  void visitFCmpInst(FCmpInst &I);
132  void visitLoadInst(LoadInst &I);
133  void visitStoreInst(StoreInst &I);
135  void visitPHINode(PHINode &PN) {
136  llvm_unreachable("PHI nodes already handled!");
137  }
138  void visitTruncInst(TruncInst &I);
139  void visitZExtInst(ZExtInst &I);
140  void visitSExtInst(SExtInst &I);
142  void visitFPExtInst(FPExtInst &I);
151 
152 
153  void visitCallSite(CallSite CS);
157 
158  void visitShl(BinaryOperator &I);
159  void visitLShr(BinaryOperator &I);
160  void visitAShr(BinaryOperator &I);
161 
162  void visitVAArgInst(VAArgInst &I);
166 
169 
171  errs() << I << "\n";
172  llvm_unreachable("Instruction not interpretable yet!");
173  }
174 
176  ArrayRef<GenericValue> ArgVals);
177  void exitCalled(GenericValue GV);
178 
180  AtExitHandlers.push_back(F);
181  }
182 
184  return &(ECStack.back ().VarArgs[0]);
185  }
186 
187 private: // Helper functions
188  GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
190 
191  // SwitchToNewBasicBlock - Start execution in a new basic block and run any
192  // PHI nodes in the top of the block. This is used for intraprocedural
193  // control flow.
194  //
195  void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
196 
197  void *getPointerToFunction(Function *F) override { return (void*)F; }
198 
199  void initializeExecutionEngine() { }
200  void initializeExternalFunctions();
201  GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
202  GenericValue getOperandValue(Value *V, ExecutionContext &SF);
203  GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
204  ExecutionContext &SF);
205  GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
206  ExecutionContext &SF);
207  GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
208  ExecutionContext &SF);
209  GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
210  ExecutionContext &SF);
211  GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
212  ExecutionContext &SF);
213  GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
214  ExecutionContext &SF);
215  GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
216  ExecutionContext &SF);
217  GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
218  ExecutionContext &SF);
219  GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
220  ExecutionContext &SF);
221  GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
222  ExecutionContext &SF);
223  GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
224  ExecutionContext &SF);
225  GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
226  ExecutionContext &SF);
227  GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
228  Type *Ty, ExecutionContext &SF);
229  void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
230 
231 };
232 
233 } // End llvm namespace
234 
235 #endif
static ExecutionEngine * create(std::unique_ptr< Module > M, std::string *ErrorStr=nullptr)
Create an interpreter ExecutionEngine.
Definition: Interpreter.cpp:35
Return a value (possibly void), from a function.
void visitVAArgInst(VAArgInst &I)
Definition: Execution.cpp:1717
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.
This instruction extracts a struct member or array element value from an aggregate value...
Base class for instruction visitors.
Definition: InstVisitor.h:81
Interpreter(std::unique_ptr< Module > M)
Definition: Interpreter.cpp:55
void visitAllocaInst(AllocaInst &I)
Definition: Execution.cpp:962
void visitStoreInst(StoreInst &I)
Definition: Execution.cpp:1049
void visitTruncInst(TruncInst &I)
Definition: Execution.cpp:1654
void visitFPToUIInst(FPToUIInst &I)
Definition: Execution.cpp:1689
This class represents zero extension of integer types.
void visitGetElementPtrInst(GetElementPtrInst &I)
Definition: Execution.cpp:1032
This class represents a function call, abstracting a target machine's calling convention.
std::map< Value *, GenericValue > Values
This instruction constructs a fixed permutation of two input vectors.
void visitExtractElementInst(ExtractElementInst &I)
Definition: Execution.cpp:1746
void visitShl(BinaryOperator &I)
Definition: Execution.cpp:1136
This class represents a sign extension of integer types.
An instruction for reading from memory.
Definition: Instructions.h:164
void visitFCmpInst(FCmpInst &I)
Definition: Execution.cpp:610
This class represents the LLVM 'select' instruction.
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:1779
#define F(x, y, z)
Definition: MD5.cpp:51
void visitFPTruncInst(FPTruncInst &I)
Definition: Execution.cpp:1669
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:2075
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void visitInsertValueInst(InsertValueInst &I)
Definition: Execution.cpp:1926
An instruction for storing to memory.
Definition: Instructions.h:300
This class represents a cast from floating point to signed integer.
void visitBitCastInst(BitCastInst &I)
Definition: Execution.cpp:1709
This class represents a truncation of integer types.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Local Stack Slot Allocation
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:830
void visitLShr(BinaryOperator &I)
Definition: Execution.cpp:1163
void visitICmpInst(ICmpInst &I)
Definition: Execution.cpp:278
This instruction inserts a single (scalar) element into a VectorType value.
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
Conditional or Unconditional Branch instruction.
void visitCallSite(CallSite CS)
Definition: Execution.cpp:1063
This function has undefined behavior.
AllocaHolder & operator=(AllocaHolder &&RHS)=default
Indirect Branch Instruction.
void visitIndirectBrInst(IndirectBrInst &I)
Definition: Execution.cpp:913
void visitZExtInst(ZExtInst &I)
Definition: Execution.cpp:1664
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:1884
This class represents a cast from an integer to a pointer.
void visitFPToSIInst(FPToSIInst &I)
Definition: Execution.cpp:1694
void visitIntToPtrInst(IntToPtrInst &I)
Definition: Execution.cpp:1704
void visitFPExtInst(FPExtInst &I)
Definition: Execution.cpp:1674
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
~Interpreter() override
Definition: Interpreter.cpp:67
void visitUnreachableInst(UnreachableInst &I)
Definition: Execution.cpp:877
static ExecutionEngine *(* InterpCtor)(std::unique_ptr< Module > M, std::string *ErrorStr)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
Iterator for intrusive lists based on ilist_node.
void visitShuffleVectorInst(ShuffleVectorInst &I)
Definition: Execution.cpp:1814
void visitReturnInst(ReturnInst &I)
Definition: Execution.cpp:863
void visitSExtInst(SExtInst &I)
Definition: Execution.cpp:1659
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:81
void visitPtrToIntInst(PtrToIntInst &I)
Definition: Execution.cpp:1699
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:71
void visitBranchInst(BranchInst &I)
Definition: Execution.cpp:881
void visitAShr(BinaryOperator &I)
Definition: Execution.cpp:1190
#define I(x, y, z)
Definition: MD5.cpp:54
void visitSIToFPInst(SIToFPInst &I)
Definition: Execution.cpp:1684
This class represents a cast unsigned integer to floating point.
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:1038
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:71
void visitUIToFPInst(UIToFPInst &I)
Definition: Execution.cpp:1679
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:47
int * Ptr
This class represents an extension of floating point types.
an instruction to allocate memory on the stack
Definition: Instructions.h:60
This instruction inserts a struct field of array element value into an aggregate value.