LLVM  13.0.0git
Interpreter.h
Go to the documentation of this file.
1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header file defines the interpreter structure
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
14 #define LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
15 
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/InstVisitor.h"
21 #include "llvm/Support/DataTypes.h"
24 namespace llvm {
25 
26 class IntrinsicLowering;
27 template<typename T> class generic_gep_type_iterator;
28 class ConstantExpr;
29 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
30 
31 
32 // AllocaHolder - Object to track all of the blocks of memory allocated by
33 // alloca. When the function returns, this object is popped off the execution
34 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
35 //
36 class AllocaHolder {
37  std::vector<void *> Allocations;
38 
39 public:
41 
42  // Make this type move-only.
43  AllocaHolder(AllocaHolder &&) = default;
44  AllocaHolder &operator=(AllocaHolder &&RHS) = default;
45 
47  for (void *Allocation : Allocations)
48  free(Allocation);
49  }
50 
51  void add(void *Mem) { Allocations.push_back(Mem); }
52 };
53 
54 typedef std::vector<GenericValue> ValuePlaneTy;
55 
56 // ExecutionContext struct - This struct represents one stack frame currently
57 // executing.
58 //
60  Function *CurFunction;// The currently executing function
61  BasicBlock *CurBB; // The currently executing BB
62  BasicBlock::iterator CurInst; // The next instruction to execute
63  CallBase *Caller; // Holds the call that called subframes.
64  // NULL if main func or debugger invoked fn
65  std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
66  std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
67  AllocaHolder Allocas; // Track memory allocated by alloca
68 
69  ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}
70 };
71 
72 // Interpreter - This class represents the entirety of the interpreter.
73 //
74 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
75  GenericValue ExitValue; // The return value of the called function
77 
78  // The runtime stack of executing code. The top of the stack is the current
79  // function record.
80  std::vector<ExecutionContext> ECStack;
81 
82  // AtExitHandlers - List of functions to call when the program exits,
83  // registered with the atexit() library function.
84  std::vector<Function*> AtExitHandlers;
85 
86 public:
87  explicit Interpreter(std::unique_ptr<Module> M);
88  ~Interpreter() override;
89 
90  /// runAtExitHandlers - Run any functions registered by the program's calls to
91  /// atexit(3), which we intercept and store in AtExitHandlers.
92  ///
93  void runAtExitHandlers();
94 
95  static void Register() {
97  }
98 
99  /// Create an interpreter ExecutionEngine.
100  ///
101  static ExecutionEngine *create(std::unique_ptr<Module> M,
102  std::string *ErrorStr = nullptr);
103 
104  /// run - Start execution with the specified function and arguments.
105  ///
107  ArrayRef<GenericValue> ArgValues) override;
108 
110  bool AbortOnFailure = true) override {
111  // FIXME: not implemented.
112  return nullptr;
113  }
114 
115  // Methods used to execute code:
116  // Place a call on the stack
118  void run(); // Execute instructions until nothing left to do
119 
120  // Opcode Implementations
125 
128  void visitICmpInst(ICmpInst &I);
129  void visitFCmpInst(FCmpInst &I);
131  void visitLoadInst(LoadInst &I);
132  void visitStoreInst(StoreInst &I);
134  void visitPHINode(PHINode &PN) {
135  llvm_unreachable("PHI nodes already handled!");
136  }
137  void visitTruncInst(TruncInst &I);
138  void visitZExtInst(ZExtInst &I);
139  void visitSExtInst(SExtInst &I);
141  void visitFPExtInst(FPExtInst &I);
150 
152  void visitVAEndInst(VAEndInst &I);
155  void visitCallBase(CallBase &I);
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
llvm::Interpreter::create
static ExecutionEngine * create(std::unique_ptr< Module > M, std::string *ErrorStr=nullptr)
Create an interpreter ExecutionEngine.
Definition: Interpreter.cpp:34
llvm::Interpreter::visitUnreachableInst
void visitUnreachableInst(UnreachableInst &I)
Definition: Execution.cpp:932
llvm::Interpreter::visitCallBase
void visitCallBase(CallBase &I)
Definition: Execution.cpp:1158
llvm
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ExecutionContext::CurFunction
Function * CurFunction
Definition: Interpreter.h:60
llvm::ReturnInst
Return a value (possibly void), from a function.
Definition: Instructions.h:2925
llvm::VAArgInst
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
Definition: Instructions.h:1810
llvm::Interpreter::visitFPExtInst
void visitFPExtInst(FPExtInst &I)
Definition: Execution.cpp:1722
llvm::generic_gep_type_iterator
Definition: GetElementPtrTypeIterator.h:31
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:90
llvm::Interpreter::visitVAEndInst
void visitVAEndInst(VAEndInst &I)
Definition: Execution.cpp:1126
llvm::ExtractElementInst
This instruction extracts a single (scalar) element from a VectorType value.
Definition: Instructions.h:1850
llvm::Function
Definition: Function.h:61
llvm::Interpreter::visitLoadInst
void visitLoadInst(LoadInst &I)
Definition: Execution.cpp:1093
ExecutionEngine.h
llvm::Interpreter::visitShl
void visitShl(BinaryOperator &I)
Definition: Execution.cpp:1186
llvm::BitCastInst
This class represents a no-op cast from one type to another.
Definition: Instructions.h:5138
llvm::Interpreter::visitInsertElementInst
void visitInsertElementInst(InsertElementInst &I)
Definition: Execution.cpp:1827
ErrorHandling.h
llvm::Interpreter::visitReturnInst
void visitReturnInst(ReturnInst &I)
Definition: Execution.cpp:918
llvm::IntrinsicLowering
Definition: IntrinsicLowering.h:24
llvm::Interpreter::runAtExitHandlers
void runAtExitHandlers()
runAtExitHandlers - Run any functions registered by the program's calls to atexit(3),...
Definition: Interpreter.cpp:70
llvm::ValuePlaneTy
std::vector< GenericValue > ValuePlaneTy
Definition: Interpreter.h:54
llvm::ExecutionContext::CurBB
BasicBlock * CurBB
Definition: Interpreter.h:61
llvm::Interpreter::run
void run()
Definition: Execution.cpp:2156
llvm::Interpreter::visitBranchInst
void visitBranchInst(BranchInst &I)
Definition: Execution.cpp:936
llvm::Interpreter::visitInstruction
void visitInstruction(Instruction &I)
Definition: Interpreter.h:170
llvm::AllocaHolder
Definition: Interpreter.h:36
llvm::Interpreter::visitUnaryOperator
void visitUnaryOperator(UnaryOperator &I)
Definition: Execution.cpp:62
llvm::Interpreter::callFunction
void callFunction(Function *F, ArrayRef< GenericValue > ArgVals)
Definition: Execution.cpp:2119
llvm::Interpreter::visitFPTruncInst
void visitFPTruncInst(FPTruncInst &I)
Definition: Execution.cpp:1717
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
llvm::Interpreter::visitICmpInst
void visitICmpInst(ICmpInst &I)
Definition: Execution.cpp:332
llvm::UnaryOperator
Definition: InstrTypes.h:103
llvm::AllocaHolder::AllocaHolder
AllocaHolder()
Definition: Interpreter.h:40
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::Interpreter::visitVAArgInst
void visitVAArgInst(VAArgInst &I)
Definition: Execution.cpp:1765
llvm::Interpreter::visitStoreInst
void visitStoreInst(StoreInst &I)
Definition: Execution.cpp:1104
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::VAStartInst
This represents the llvm.va_start intrinsic.
Definition: IntrinsicInst.h:1041
llvm::Interpreter::addAtExitHandler
void addAtExitHandler(Function *F)
Definition: Interpreter.h:179
llvm::Interpreter::visitSIToFPInst
void visitSIToFPInst(SIToFPInst &I)
Definition: Execution.cpp:1732
llvm::Interpreter::~Interpreter
~Interpreter() override
Definition: Interpreter.cpp:66
llvm::ExecutionContext::Values
std::map< Value *, GenericValue > Values
Definition: Interpreter.h:65
llvm::Interpreter::exitCalled
void exitCalled(GenericValue GV)
Definition: Execution.cpp:875
llvm::Interpreter::visitZExtInst
void visitZExtInst(ZExtInst &I)
Definition: Execution.cpp:1712
llvm::GenericValue
Definition: GenericValue.h:23
llvm::Interpreter::visitIntrinsicInst
void visitIntrinsicInst(IntrinsicInst &I)
Definition: Execution.cpp:1135
llvm::Interpreter::visitIndirectBrInst
void visitIndirectBrInst(IndirectBrInst &I)
Definition: Execution.cpp:968
llvm::ExecutionContext::Caller
CallBase * Caller
Definition: Interpreter.h:63
Allocation
llvm::Interpreter::getFirstVarArg
GenericValue * getFirstVarArg()
Definition: Interpreter.h:183
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::Interpreter::runFunction
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
run - Start execution with the specified function and arguments.
Definition: Interpreter.cpp:80
llvm::Interpreter::visitAllocaInst
void visitAllocaInst(AllocaInst &I)
Definition: Execution.cpp:1017
llvm::FCmpInst
This instruction compares its operands according to the predicate given to the constructor.
Definition: Instructions.h:1344
llvm::InsertElementInst
This instruction inserts a single (scalar) element into a VectorType value.
Definition: Instructions.h:1914
llvm::Instruction::CastOps
CastOps
Definition: Instruction.h:782
llvm::Instruction
Definition: Instruction.h:45
llvm::ExecutionEngine::InterpCtor
static ExecutionEngine *(* InterpCtor)(std::unique_ptr< Module > M, std::string *ErrorStr)
Definition: ExecutionEngine.h:145
llvm::Interpreter::visitTruncInst
void visitTruncInst(TruncInst &I)
Definition: Execution.cpp:1702
GenericValue.h
llvm::Interpreter::visitBitCastInst
void visitBitCastInst(BitCastInst &I)
Definition: Execution.cpp:1757
llvm::AllocaHolder::~AllocaHolder
~AllocaHolder()
Definition: Interpreter.h:46
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:303
llvm::ExecutionContext::Allocas
AllocaHolder Allocas
Definition: Interpreter.h:67
llvm::ICmpInst
This instruction compares its operands according to the predicate given to the constructor.
Definition: Instructions.h:1178
llvm::Interpreter::visitVAStartInst
void visitVAStartInst(VAStartInst &I)
Definition: Execution.cpp:1118
llvm::FPToSIInst
This class represents a cast from floating point to signed integer.
Definition: Instructions.h:5005
llvm::AllocaHolder::add
void add(void *Mem)
Definition: Interpreter.h:51
llvm::TruncInst
This class represents a truncation of integer types.
Definition: Instructions.h:4693
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::GetElementPtrInst
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:905
llvm::ExecutionContext::ExecutionContext
ExecutionContext()
Definition: Interpreter.h:69
llvm::Interpreter::visitVACopyInst
void visitVACopyInst(VACopyInst &I)
Definition: Execution.cpp:1130
llvm::Interpreter::visitInsertValueInst
void visitInsertValueInst(InsertValueInst &I)
Definition: Execution.cpp:1969
llvm::Interpreter::visitExtractElementInst
void visitExtractElementInst(ExtractElementInst &I)
Definition: Execution.cpp:1794
llvm::gep_type_iterator
generic_gep_type_iterator<> gep_type_iterator
Definition: GetElementPtrTypeIterator.h:137
llvm::SelectInst
This class represents the LLVM 'select' instruction.
Definition: Instructions.h:1715
llvm::FPToUIInst
This class represents a cast from floating point to unsigned integer.
Definition: Instructions.h:4966
llvm::Interpreter::Register
static void Register()
Definition: Interpreter.h:95
llvm::Interpreter::visitPtrToIntInst
void visitPtrToIntInst(PtrToIntInst &I)
Definition: Execution.cpp:1747
llvm::Interpreter::visitFCmpInst
void visitFCmpInst(FCmpInst &I)
Definition: Execution.cpp:665
llvm::ZExtInst
This class represents zero extension of integer types.
Definition: Instructions.h:4732
llvm::Interpreter::visitFPToUIInst
void visitFPToUIInst(FPToUIInst &I)
Definition: Execution.cpp:1737
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::VAEndInst
This represents the llvm.va_end intrinsic.
Definition: IntrinsicInst.h:1054
llvm::Interpreter::getPointerToNamedFunction
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
Definition: Interpreter.h:109
llvm::BinaryOperator
Definition: InstrTypes.h:190
llvm::ExecutionContext::CurInst
BasicBlock::iterator CurInst
Definition: Interpreter.h:62
DataLayout.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
InstVisitor.h
llvm::AllocaHolder::operator=
AllocaHolder & operator=(AllocaHolder &&RHS)=default
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::ExecutionContext::VarArgs
std::vector< GenericValue > VarArgs
Definition: Interpreter.h:66
llvm::Interpreter::visitLShr
void visitLShr(BinaryOperator &I)
Definition: Execution.cpp:1213
llvm::Interpreter::visitSExtInst
void visitSExtInst(SExtInst &I)
Definition: Execution.cpp:1707
llvm::PtrToIntInst
This class represents a cast from a pointer to an integer.
Definition: Instructions.h:5087
llvm::Interpreter::visitSwitchInst
void visitSwitchInst(SwitchInst &I)
Definition: Execution.cpp:949
llvm::InstVisitor
Base class for instruction visitors.
Definition: InstVisitor.h:79
llvm::FPExtInst
This class represents an extension of floating point types.
Definition: Instructions.h:4849
llvm::SExtInst
This class represents a sign extension of integer types.
Definition: Instructions.h:4771
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:174
llvm::Interpreter::visitUIToFPInst
void visitUIToFPInst(UIToFPInst &I)
Definition: Execution.cpp:1727
llvm::ExecutionEngine
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
Definition: ExecutionEngine.h:100
llvm::ExecutionContext
Definition: Interpreter.h:59
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::VACopyInst
This represents the llvm.va_copy intrinsic.
Definition: IntrinsicInst.h:1067
llvm::ExtractValueInst
This instruction extracts a struct member or array element value from an aggregate value.
Definition: Instructions.h:2318
Function.h
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::Interpreter::visitBinaryOperator
void visitBinaryOperator(BinaryOperator &I)
Definition: Execution.cpp:736
llvm::UIToFPInst
This class represents a cast unsigned integer to floating point.
Definition: Instructions.h:4888
llvm::Interpreter::visitFPToSIInst
void visitFPToSIInst(FPToSIInst &I)
Definition: Execution.cpp:1742
llvm::IntToPtrInst
This class represents a cast from an integer to a pointer.
Definition: Instructions.h:5044
llvm::IndirectBrInst
Indirect Branch Instruction.
Definition: Instructions.h:3553
llvm::Interpreter::Interpreter
Interpreter(std::unique_ptr< Module > M)
Definition: Interpreter.cpp:54
llvm::IntrinsicInst
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:45
llvm::Interpreter::visitShuffleVectorInst
void visitShuffleVectorInst(ShuffleVectorInst &I)
Definition: Execution.cpp:1859
llvm::Interpreter::visitAShr
void visitAShr(BinaryOperator &I)
Definition: Execution.cpp:1240
llvm::Interpreter::visitSelectInst
void visitSelectInst(SelectInst &I)
Definition: Execution.cpp:861
llvm::ShuffleVectorInst
This instruction constructs a fixed permutation of two input vectors.
Definition: Instructions.h:1986
llvm::Interpreter
Definition: Interpreter.h:74
llvm::SIToFPInst
This class represents a cast from signed integer to floating point.
Definition: Instructions.h:4927
llvm::FPTruncInst
This class represents a truncation of floating point types.
Definition: Instructions.h:4810
llvm::PHINode
Definition: Instructions.h:2572
llvm::Interpreter::callExternalFunction
GenericValue callExternalFunction(Function *F, ArrayRef< GenericValue > ArgVals)
Definition: ExternalFunctions.cpp:257
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1164
DataTypes.h
llvm::UnreachableInst
This function has undefined behavior.
Definition: Instructions.h:4652
llvm::Interpreter::visitGetElementPtrInst
void visitGetElementPtrInst(GetElementPtrInst &I)
Definition: Execution.cpp:1087
llvm::SwitchInst
Multiway switch.
Definition: Instructions.h:3151
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition: Instructions.h:61
llvm::BranchInst
Conditional or Unconditional Branch instruction.
Definition: Instructions.h:3007
raw_ostream.h
llvm::Interpreter::visitPHINode
void visitPHINode(PHINode &PN)
Definition: Interpreter.h:134
llvm::Interpreter::visitIntToPtrInst
void visitIntToPtrInst(IntToPtrInst &I)
Definition: Execution.cpp:1752
llvm::InsertValueInst
This instruction inserts a struct field of array element value into an aggregate value.
Definition: Instructions.h:2429
llvm::Interpreter::visitExtractValueInst
void visitExtractValueInst(ExtractValueInst &I)
Definition: Execution.cpp:1926
llvm::Value
LLVM Value Representation.
Definition: Value.h:75