LLVM 23.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"
24namespace llvm {
25
27template<typename T> class generic_gep_type_iterator;
28class ConstantExpr;
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//
37 std::vector<void *> Allocations;
38
39public:
40 AllocaHolder() = default;
41
42 // Make this type move-only.
45
47 for (void *Allocation : Allocations)
48 free(Allocation);
49 }
50
51 void add(void *Mem) { Allocations.push_back(Mem); }
52};
53
54typedef 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//
74class 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
86public:
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
126
129 void visitICmpInst(ICmpInst &I);
130 void visitFCmpInst(FCmpInst &I);
132 void visitLoadInst(LoadInst &I);
136 llvm_unreachable("PHI nodes already handled!");
137 }
139 void visitZExtInst(ZExtInst &I);
140 void visitSExtInst(SExtInst &I);
151
156 void visitCallBase(CallBase &I);
158
162
167
170
172 errs() << I << "\n";
173 llvm_unreachable("Instruction not interpretable yet!");
174 }
175
177 ArrayRef<GenericValue> ArgVals);
178 void exitCalled(GenericValue GV);
179
181 AtExitHandlers.push_back(F);
182 }
183
185 return &(ECStack.back ().VarArgs[0]);
186 }
187
188private: // Helper functions
189 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
191
192 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
193 // PHI nodes in the top of the block. This is used for intraprocedural
194 // control flow.
195 //
196 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
197
198 void *getPointerToFunction(Function *F) override { return (void*)F; }
199
200 void initializeExecutionEngine() { }
201 void initializeExternalFunctions();
202 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
203 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
204 GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
205 ExecutionContext &SF);
206 GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
207 ExecutionContext &SF);
208 GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
209 ExecutionContext &SF);
210 GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
211 ExecutionContext &SF);
212 GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
213 ExecutionContext &SF);
214 GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
215 ExecutionContext &SF);
216 GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
217 ExecutionContext &SF);
218 GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
219 ExecutionContext &SF);
220 GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
221 ExecutionContext &SF);
222 GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
223 ExecutionContext &SF);
224 GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
225 ExecutionContext &SF);
226 GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
227 ExecutionContext &SF);
228 void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
229
230};
231
232} // End llvm namespace
233
234#endif
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Value * RHS
AllocaHolder()=default
AllocaHolder & operator=(AllocaHolder &&RHS)=default
void add(void *Mem)
Definition Interpreter.h:51
AllocaHolder(AllocaHolder &&)=default
an instruction to allocate memory on the stack
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
This class represents a no-op cast from one type to another.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Conditional Branch instruction.
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1291
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
static ExecutionEngine *(* InterpCtor)(std::unique_ptr< Module > M, std::string *ErrorStr)
ExecutionEngine(DataLayout DL)
This instruction extracts a single (scalar) element from a VectorType value.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
This class represents an extension of floating point types.
This class represents a cast from floating point to signed integer.
This class represents a cast from floating point to unsigned integer.
This class represents a truncation of floating point types.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
This instruction compares its operands according to the predicate given to the constructor.
Indirect Branch Instruction.
This instruction inserts a single (scalar) element into a VectorType value.
This instruction inserts a struct field of array element value into an aggregate value.
Base class for instruction visitors.
Definition InstVisitor.h:78
This class represents a cast from an integer to a pointer.
void visitSIToFPInst(SIToFPInst &I)
void visitFCmpInst(FCmpInst &I)
void visitPtrToIntInst(PtrToIntInst &I)
void visitPHINode(PHINode &PN)
void visitShuffleVectorInst(ShuffleVectorInst &I)
void addAtExitHandler(Function *F)
static void Register()
Definition Interpreter.h:95
~Interpreter() override
void visitCallBase(CallBase &I)
void visitAllocaInst(AllocaInst &I)
void visitSelectInst(SelectInst &I)
void exitCalled(GenericValue GV)
void visitReturnInst(ReturnInst &I)
void visitIntToPtrInst(IntToPtrInst &I)
void visitUncondBrInst(UncondBrInst &I)
void visitUnreachableInst(UnreachableInst &I)
void visitICmpInst(ICmpInst &I)
void visitLShr(BinaryOperator &I)
void visitUIToFPInst(UIToFPInst &I)
void visitIndirectBrInst(IndirectBrInst &I)
void visitInsertValueInst(InsertValueInst &I)
GenericValue * getFirstVarArg()
void runAtExitHandlers()
runAtExitHandlers - Run any functions registered by the program's calls to atexit(3),...
void visitVAArgInst(VAArgInst &I)
void visitStoreInst(StoreInst &I)
void visitExtractValueInst(ExtractValueInst &I)
void visitSwitchInst(SwitchInst &I)
void visitExtractElementInst(ExtractElementInst &I)
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
void visitVACopyInst(VACopyInst &I)
void visitVAEndInst(VAEndInst &I)
void visitTruncInst(TruncInst &I)
void visitFPToUIInst(FPToUIInst &I)
Interpreter(std::unique_ptr< Module > M)
void visitLoadInst(LoadInst &I)
void visitGetElementPtrInst(GetElementPtrInst &I)
void callFunction(Function *F, ArrayRef< GenericValue > ArgVals)
void visitInsertElementInst(InsertElementInst &I)
void visitUnaryOperator(UnaryOperator &I)
Definition Execution.cpp:62
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
run - Start execution with the specified function and arguments.
void visitFPExtInst(FPExtInst &I)
void visitVAStartInst(VAStartInst &I)
void visitBitCastInst(BitCastInst &I)
void visitCondBrInst(CondBrInst &I)
void visitSExtInst(SExtInst &I)
void visitAShr(BinaryOperator &I)
GenericValue callExternalFunction(Function *F, ArrayRef< GenericValue > ArgVals)
void visitFPTruncInst(FPTruncInst &I)
void visitBinaryOperator(BinaryOperator &I)
void visitShl(BinaryOperator &I)
void visitZExtInst(ZExtInst &I)
void visitFPToSIInst(FPToSIInst &I)
void visitIntrinsicInst(IntrinsicInst &I)
static ExecutionEngine * create(std::unique_ptr< Module > M, std::string *ErrorStr=nullptr)
Create an interpreter ExecutionEngine.
void visitInstruction(Instruction &I)
A wrapper class for inspecting calls to intrinsic functions.
An instruction for reading from memory.
This class represents a cast from a pointer to an integer.
Return a value (possibly void), from a function.
This class represents a sign extension of integer types.
This class represents a cast from signed integer to floating point.
This class represents the LLVM 'select' instruction.
This instruction constructs a fixed permutation of two input vectors.
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Multiway switch.
This class represents a truncation of integer types.
This class represents a cast unsigned integer to floating point.
Unconditional Branch instruction.
This function has undefined behavior.
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
This represents the llvm.va_copy intrinsic.
This represents the llvm.va_end intrinsic.
This represents the llvm.va_start intrinsic.
LLVM Value Representation.
Definition Value.h:75
This class represents zero extension of integer types.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
std::vector< GenericValue > ValuePlaneTy
Definition Interpreter.h:54
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
BasicBlock::iterator CurInst
Definition Interpreter.h:62
std::map< Value *, GenericValue > Values
Definition Interpreter.h:65
std::vector< GenericValue > VarArgs
Definition Interpreter.h:66