1 /** 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_INTERPRETER_INSTRUCTION_HANDLER_STATE_H_ 17 #define PANDA_INTERPRETER_INSTRUCTION_HANDLER_STATE_H_ 18 19 #include "runtime/interpreter/state.h" 20 #include "runtime/jit/profiling_data.h" 21 22 namespace ark::interpreter { 23 24 class InstructionHandlerState { 25 public: 26 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) InstructionHandlerState(ManagedThread * thread,const uint8_t * pc,Frame * frame,const void * const * dispatchTable)27 ALWAYS_INLINE InstructionHandlerState(ManagedThread *thread, const uint8_t *pc, Frame *frame, 28 const void *const *dispatchTable) 29 : state_(thread, pc, frame, dispatchTable) 30 { 31 } 32 UpdateInstructionHandlerState(const uint8_t * pc,Frame * frame)33 ALWAYS_INLINE void UpdateInstructionHandlerState(const uint8_t *pc, Frame *frame) 34 { 35 state_.UpdateState(pc, frame); 36 } 37 GetThread()38 ALWAYS_INLINE ManagedThread *GetThread() const 39 { 40 return state_.GetThread(); 41 } 42 SetThread(ManagedThread * thread)43 ALWAYS_INLINE void SetThread(ManagedThread *thread) 44 { 45 state_.SetThread(thread); 46 } 47 SetInst(BytecodeInstruction inst)48 ALWAYS_INLINE void SetInst(BytecodeInstruction inst) 49 { 50 state_.SetInst(inst); 51 } 52 GetFrame()53 ALWAYS_INLINE Frame *GetFrame() const 54 { 55 return state_.GetFrame(); 56 } 57 SetFrame(Frame * frame)58 ALWAYS_INLINE void SetFrame(Frame *frame) 59 { 60 state_.SetFrame(frame); 61 } 62 GetDispatchTable()63 ALWAYS_INLINE const void *const *GetDispatchTable() const 64 { 65 return state_.GetDispatchTable(); 66 } 67 SetDispatchTable(const void * const * dispatchTable)68 ALWAYS_INLINE void SetDispatchTable(const void *const *dispatchTable) 69 { 70 return state_.SetDispatchTable(dispatchTable); 71 } 72 SaveState()73 ALWAYS_INLINE void SaveState() 74 { 75 state_.SaveState(); 76 } 77 RestoreState()78 ALWAYS_INLINE void RestoreState() 79 { 80 state_.RestoreState(); 81 } 82 GetOpcodeExtension()83 ALWAYS_INLINE uint16_t GetOpcodeExtension() const 84 { 85 return opcodeExtension_; 86 } 87 SetOpcodeExtension(uint16_t opcodeExtension)88 ALWAYS_INLINE void SetOpcodeExtension(uint16_t opcodeExtension) 89 { 90 opcodeExtension_ = opcodeExtension; 91 } 92 GetPrimaryOpcode()93 ALWAYS_INLINE uint8_t GetPrimaryOpcode() const 94 { 95 return static_cast<unsigned>(GetInst().GetOpcode()) & OPCODE_MASK; 96 } 97 GetSecondaryOpcode()98 ALWAYS_INLINE uint8_t GetSecondaryOpcode() const 99 { 100 return (static_cast<unsigned>(GetInst().GetSecondaryOpcode())) & OPCODE_MASK; 101 } 102 IsPrimaryOpcodeValid()103 ALWAYS_INLINE bool IsPrimaryOpcodeValid() const 104 { 105 return GetInst().IsPrimaryOpcodeValid(); 106 } 107 GetInst()108 ALWAYS_INLINE BytecodeInstruction GetInst() const 109 { 110 return state_.GetInst(); 111 } 112 GetAcc()113 ALWAYS_INLINE const AccVRegisterT &GetAcc() const 114 { 115 return state_.GetAcc(); 116 } 117 GetAcc()118 ALWAYS_INLINE AccVRegisterT &GetAcc() 119 { 120 return state_.GetAcc(); 121 } 122 GetFakeInstBuf()123 ALWAYS_INLINE auto &GetFakeInstBuf() 124 { 125 return fakeInstBuf_; 126 } 127 GetBytecodeOffset()128 ALWAYS_INLINE uint32_t GetBytecodeOffset() const 129 { 130 return GetInst().GetAddress() - GetFrame()->GetInstruction(); 131 } 132 133 private: 134 static constexpr size_t FAKE_INST_BUF_SIZE = 4; 135 static constexpr uint8_t OPCODE_MASK = 0xFFU; 136 137 State state_; 138 std::array<uint8_t, FAKE_INST_BUF_SIZE> fakeInstBuf_; 139 uint16_t opcodeExtension_ {0}; 140 }; 141 142 } // namespace ark::interpreter 143 144 #endif // PANDA_INTERPRETER_INSTRUCTION_HANDLER_STATE_H_ 145