| 1 | //===--- ExecutorBase.h - Non-visitor methods of InstExecutor -------------===// |
| 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 file declares non-visitor methods of InstExecutor for code reuse. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_TOOLS_LLUBI_EXECUTORBASE_H |
| 14 | #define LLVM_TOOLS_LLUBI_EXECUTORBASE_H |
| 15 | |
| 16 | #include "Context.h" |
| 17 | #include "Value.h" |
| 18 | |
| 19 | namespace llvm::ubi { |
| 20 | |
| 21 | enum class FrameState { |
| 22 | // It is about to enter the function. |
| 23 | // Valid transition: |
| 24 | // -> Running |
| 25 | Entry, |
| 26 | // It is executing instructions inside the function. |
| 27 | // Valid transitions: |
| 28 | // -> Pending (on call) |
| 29 | // -> Exit (on return) |
| 30 | Running, |
| 31 | // It is about to enter a callee or handle return value from the callee. |
| 32 | // Valid transitions: |
| 33 | // -> Running (after returning from callee) |
| 34 | Pending, |
| 35 | // It is about to return the control to the caller. |
| 36 | Exit, |
| 37 | }; |
| 38 | |
| 39 | /// Context for a function call. |
| 40 | /// This struct maintains the state during the execution of a function, |
| 41 | /// including the control flow, values of executed instructions, and stack |
| 42 | /// objects. |
| 43 | struct Frame { |
| 44 | Function &Func; |
| 45 | Frame *LastFrame; |
| 46 | CallBase *CallSite; |
| 47 | ArrayRef<AnyValue> Args; |
| 48 | AnyValue &RetVal; |
| 49 | |
| 50 | TargetLibraryInfo TLI; |
| 51 | BasicBlock *BB; |
| 52 | BasicBlock::iterator PC; |
| 53 | FrameState State = FrameState::Entry; |
| 54 | // Stack objects allocated in this frame. They will be automatically freed |
| 55 | // when the function returns. |
| 56 | SmallVector<IntrusiveRefCntPtr<MemoryObject>> Allocas; |
| 57 | // Values of arguments and executed instructions in this function. |
| 58 | DenseMap<Value *, AnyValue> ValueMap; |
| 59 | |
| 60 | // Reserved for in-flight subroutines. |
| 61 | Function *ResolvedCallee = nullptr; |
| 62 | SmallVector<AnyValue> CalleeArgs; |
| 63 | AnyValue CalleeRetVal; |
| 64 | |
| 65 | Frame(Function &F, CallBase *CallSite, Frame *LastFrame, |
| 66 | ArrayRef<AnyValue> Args, AnyValue &RetVal, |
| 67 | const TargetLibraryInfoImpl &TLIImpl); |
| 68 | }; |
| 69 | |
| 70 | class ExecutorBase { |
| 71 | protected: |
| 72 | Context &Ctx; |
| 73 | EventHandler &Handler; |
| 74 | // Used to indicate whether the interpreter should continue execution. |
| 75 | bool Status; |
| 76 | Frame *CurrentFrame = nullptr; |
| 77 | |
| 78 | ExecutorBase(Context &C, EventHandler &H) |
| 79 | : Ctx(C), Handler(H), Status(true) {} |
| 80 | ~ExecutorBase() = default; |
| 81 | |
| 82 | public: |
| 83 | void reportImmediateUB(StringRef Msg); |
| 84 | void reportError(StringRef Msg); |
| 85 | |
| 86 | /// Check if the upcoming memory access is valid. Returns the offset relative |
| 87 | /// to the underlying object if it is valid. |
| 88 | std::optional<uint64_t> verifyMemAccess(const MemoryObject &MO, |
| 89 | const APInt &Address, |
| 90 | uint64_t AccessSize, Align Alignment, |
| 91 | bool IsStore); |
| 92 | |
| 93 | AnyValue load(const AnyValue &Ptr, Align Alignment, Type *ValTy); |
| 94 | void store(const AnyValue &Ptr, Align Alignment, const AnyValue &Val, |
| 95 | Type *ValTy); |
| 96 | }; |
| 97 | |
| 98 | } // namespace llvm::ubi |
| 99 | |
| 100 | #endif // LLVM_TOOLS_LLUBI_EXECUTORBASE_H |
| 101 | |