Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Interpreter/Interpreter.h
35271 views
//===-- Interpreter.h ------------------------------------------*- C++ -*--===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This header file defines the interpreter structure9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H13#define LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H1415#include "llvm/ExecutionEngine/ExecutionEngine.h"16#include "llvm/ExecutionEngine/GenericValue.h"17#include "llvm/IR/DataLayout.h"18#include "llvm/IR/Function.h"19#include "llvm/IR/InstVisitor.h"20#include "llvm/Support/DataTypes.h"21#include "llvm/Support/ErrorHandling.h"22#include "llvm/Support/raw_ostream.h"23namespace llvm {2425class IntrinsicLowering;26template<typename T> class generic_gep_type_iterator;27class ConstantExpr;28typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;293031// AllocaHolder - Object to track all of the blocks of memory allocated by32// alloca. When the function returns, this object is popped off the execution33// stack, which causes the dtor to be run, which frees all the alloca'd memory.34//35class AllocaHolder {36std::vector<void *> Allocations;3738public:39AllocaHolder() = default;4041// Make this type move-only.42AllocaHolder(AllocaHolder &&) = default;43AllocaHolder &operator=(AllocaHolder &&RHS) = default;4445~AllocaHolder() {46for (void *Allocation : Allocations)47free(Allocation);48}4950void add(void *Mem) { Allocations.push_back(Mem); }51};5253typedef std::vector<GenericValue> ValuePlaneTy;5455// ExecutionContext struct - This struct represents one stack frame currently56// executing.57//58struct ExecutionContext {59Function *CurFunction;// The currently executing function60BasicBlock *CurBB; // The currently executing BB61BasicBlock::iterator CurInst; // The next instruction to execute62CallBase *Caller; // Holds the call that called subframes.63// NULL if main func or debugger invoked fn64std::map<Value *, GenericValue> Values; // LLVM values used in this invocation65std::vector<GenericValue> VarArgs; // Values passed through an ellipsis66AllocaHolder Allocas; // Track memory allocated by alloca6768ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}69};7071// Interpreter - This class represents the entirety of the interpreter.72//73class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {74GenericValue ExitValue; // The return value of the called function75IntrinsicLowering *IL;7677// The runtime stack of executing code. The top of the stack is the current78// function record.79std::vector<ExecutionContext> ECStack;8081// AtExitHandlers - List of functions to call when the program exits,82// registered with the atexit() library function.83std::vector<Function*> AtExitHandlers;8485public:86explicit Interpreter(std::unique_ptr<Module> M);87~Interpreter() override;8889/// runAtExitHandlers - Run any functions registered by the program's calls to90/// atexit(3), which we intercept and store in AtExitHandlers.91///92void runAtExitHandlers();9394static void Register() {95InterpCtor = create;96}9798/// Create an interpreter ExecutionEngine.99///100static ExecutionEngine *create(std::unique_ptr<Module> M,101std::string *ErrorStr = nullptr);102103/// run - Start execution with the specified function and arguments.104///105GenericValue runFunction(Function *F,106ArrayRef<GenericValue> ArgValues) override;107108void *getPointerToNamedFunction(StringRef Name,109bool AbortOnFailure = true) override {110// FIXME: not implemented.111return nullptr;112}113114// Methods used to execute code:115// Place a call on the stack116void callFunction(Function *F, ArrayRef<GenericValue> ArgVals);117void run(); // Execute instructions until nothing left to do118119// Opcode Implementations120void visitReturnInst(ReturnInst &I);121void visitBranchInst(BranchInst &I);122void visitSwitchInst(SwitchInst &I);123void visitIndirectBrInst(IndirectBrInst &I);124125void visitUnaryOperator(UnaryOperator &I);126void visitBinaryOperator(BinaryOperator &I);127void visitICmpInst(ICmpInst &I);128void visitFCmpInst(FCmpInst &I);129void visitAllocaInst(AllocaInst &I);130void visitLoadInst(LoadInst &I);131void visitStoreInst(StoreInst &I);132void visitGetElementPtrInst(GetElementPtrInst &I);133void visitPHINode(PHINode &PN) {134llvm_unreachable("PHI nodes already handled!");135}136void visitTruncInst(TruncInst &I);137void visitZExtInst(ZExtInst &I);138void visitSExtInst(SExtInst &I);139void visitFPTruncInst(FPTruncInst &I);140void visitFPExtInst(FPExtInst &I);141void visitUIToFPInst(UIToFPInst &I);142void visitSIToFPInst(SIToFPInst &I);143void visitFPToUIInst(FPToUIInst &I);144void visitFPToSIInst(FPToSIInst &I);145void visitPtrToIntInst(PtrToIntInst &I);146void visitIntToPtrInst(IntToPtrInst &I);147void visitBitCastInst(BitCastInst &I);148void visitSelectInst(SelectInst &I);149150void visitVAStartInst(VAStartInst &I);151void visitVAEndInst(VAEndInst &I);152void visitVACopyInst(VACopyInst &I);153void visitIntrinsicInst(IntrinsicInst &I);154void visitCallBase(CallBase &I);155void visitUnreachableInst(UnreachableInst &I);156157void visitShl(BinaryOperator &I);158void visitLShr(BinaryOperator &I);159void visitAShr(BinaryOperator &I);160161void visitVAArgInst(VAArgInst &I);162void visitExtractElementInst(ExtractElementInst &I);163void visitInsertElementInst(InsertElementInst &I);164void visitShuffleVectorInst(ShuffleVectorInst &I);165166void visitExtractValueInst(ExtractValueInst &I);167void visitInsertValueInst(InsertValueInst &I);168169void visitInstruction(Instruction &I) {170errs() << I << "\n";171llvm_unreachable("Instruction not interpretable yet!");172}173174GenericValue callExternalFunction(Function *F,175ArrayRef<GenericValue> ArgVals);176void exitCalled(GenericValue GV);177178void addAtExitHandler(Function *F) {179AtExitHandlers.push_back(F);180}181182GenericValue *getFirstVarArg () {183return &(ECStack.back ().VarArgs[0]);184}185186private: // Helper functions187GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,188gep_type_iterator E, ExecutionContext &SF);189190// SwitchToNewBasicBlock - Start execution in a new basic block and run any191// PHI nodes in the top of the block. This is used for intraprocedural192// control flow.193//194void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);195196void *getPointerToFunction(Function *F) override { return (void*)F; }197198void initializeExecutionEngine() { }199void initializeExternalFunctions();200GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);201GenericValue getOperandValue(Value *V, ExecutionContext &SF);202GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,203ExecutionContext &SF);204GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,205ExecutionContext &SF);206GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,207ExecutionContext &SF);208GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,209ExecutionContext &SF);210GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,211ExecutionContext &SF);212GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,213ExecutionContext &SF);214GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,215ExecutionContext &SF);216GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,217ExecutionContext &SF);218GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,219ExecutionContext &SF);220GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,221ExecutionContext &SF);222GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,223ExecutionContext &SF);224GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,225ExecutionContext &SF);226void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);227228};229230} // End llvm namespace231232#endif233234235