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
16#include "llvm/ExecutionEngine/ExecutionEngine.h"
17#include "llvm/ExecutionEngine/GenericValue.h"
18#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/InstVisitor.h"
21#include "llvm/Support/DataTypes.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
24namespace llvm {
25
26class IntrinsicLowering;
27template<typename T> class generic_gep_type_iterator;
28class ConstantExpr;
29typedef 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//
36class AllocaHolder {
37 std::vector<void *> Allocations;
38
39public:
40 AllocaHolder() = default;
41
42 // Make this type move-only.
43 AllocaHolder(AllocaHolder &&) = default;
44 AllocaHolder &operator=(AllocaHolder &&RHS) = default;
45
46 ~AllocaHolder() {
47 for (void *Allocation : Allocations)
48 free(ptr: Allocation);
49 }
50
51 void add(void *Mem) { Allocations.push_back(x: Mem); }
52};
53
54typedef std::vector<GenericValue> ValuePlaneTy;
55
56// ExecutionContext struct - This struct represents one stack frame currently
57// executing.
58//
59struct ExecutionContext {
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
76 IntrinsicLowering *IL;
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() {
96 InterpCtor = create;
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 ///
106 GenericValue runFunction(Function *F,
107 ArrayRef<GenericValue> ArgValues) override;
108
109 void *getPointerToNamedFunction(StringRef Name,
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
117 void callFunction(Function *F, ArrayRef<GenericValue> ArgVals);
118 void run(); // Execute instructions until nothing left to do
119
120 // Opcode Implementations
121 void visitReturnInst(ReturnInst &I);
122 void visitUncondBrInst(UncondBrInst &I);
123 void visitCondBrInst(CondBrInst &I);
124 void visitSwitchInst(SwitchInst &I);
125 void visitIndirectBrInst(IndirectBrInst &I);
126
127 void visitUnaryOperator(UnaryOperator &I);
128 void visitBinaryOperator(BinaryOperator &I);
129 void visitICmpInst(ICmpInst &I);
130 void visitFCmpInst(FCmpInst &I);
131 void visitAllocaInst(AllocaInst &I);
132 void visitLoadInst(LoadInst &I);
133 void visitStoreInst(StoreInst &I);
134 void visitGetElementPtrInst(GetElementPtrInst &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);
141 void visitFPTruncInst(FPTruncInst &I);
142 void visitFPExtInst(FPExtInst &I);
143 void visitUIToFPInst(UIToFPInst &I);
144 void visitSIToFPInst(SIToFPInst &I);
145 void visitFPToUIInst(FPToUIInst &I);
146 void visitFPToSIInst(FPToSIInst &I);
147 void visitPtrToIntInst(PtrToIntInst &I);
148 void visitIntToPtrInst(IntToPtrInst &I);
149 void visitBitCastInst(BitCastInst &I);
150 void visitSelectInst(SelectInst &I);
151
152 void visitVAStartInst(VAStartInst &I);
153 void visitVAEndInst(VAEndInst &I);
154 void visitVACopyInst(VACopyInst &I);
155 void visitIntrinsicInst(IntrinsicInst &I);
156 void visitCallBase(CallBase &I);
157 void visitUnreachableInst(UnreachableInst &I);
158
159 void visitShl(BinaryOperator &I);
160 void visitLShr(BinaryOperator &I);
161 void visitAShr(BinaryOperator &I);
162
163 void visitVAArgInst(VAArgInst &I);
164 void visitExtractElementInst(ExtractElementInst &I);
165 void visitInsertElementInst(InsertElementInst &I);
166 void visitShuffleVectorInst(ShuffleVectorInst &I);
167
168 void visitExtractValueInst(ExtractValueInst &I);
169 void visitInsertValueInst(InsertValueInst &I);
170
171 void visitInstruction(Instruction &I) {
172 errs() << I << "\n";
173 llvm_unreachable("Instruction not interpretable yet!");
174 }
175
176 GenericValue callExternalFunction(Function *F,
177 ArrayRef<GenericValue> ArgVals);
178 void exitCalled(GenericValue GV);
179
180 void addAtExitHandler(Function *F) {
181 AtExitHandlers.push_back(x: F);
182 }
183
184 GenericValue *getFirstVarArg () {
185 return &(ECStack.back ().VarArgs[0]);
186 }
187
188private: // Helper functions
189 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
190 gep_type_iterator E, ExecutionContext &SF);
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
235