• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 panda::interpreter {
23 
24 class InstructionHandlerState {
25 public:
26     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
InstructionHandlerState(ManagedThread * thread,const uint8_t * pc,Frame * frame)27     ALWAYS_INLINE InstructionHandlerState(ManagedThread *thread, const uint8_t *pc, Frame *frame)
28         : state_(thread, pc, frame)
29     {
30         instructions_ = GetFrame()->GetInstruction();
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         instructions_ = GetFrame()->GetInstruction();
37     }
38 
GetThread()39     ALWAYS_INLINE ManagedThread *GetThread() const
40     {
41         return state_.GetThread();
42     }
43 
SetThread(ManagedThread * thread)44     ALWAYS_INLINE void SetThread(ManagedThread *thread)
45     {
46         state_.SetThread(thread);
47     }
48 
SetInst(BytecodeInstruction inst)49     ALWAYS_INLINE void SetInst(BytecodeInstruction inst)
50     {
51         state_.SetInst(inst);
52     }
53 
GetFrame()54     ALWAYS_INLINE Frame *GetFrame() const
55     {
56         return state_.GetFrame();
57     }
58 
SetFrame(Frame * frame)59     ALWAYS_INLINE void SetFrame(Frame *frame)
60     {
61         state_.SetFrame(frame);
62     }
63 
GetDispatchTable()64     ALWAYS_INLINE const void *const *GetDispatchTable() const
65     {
66         return state_.GetDispatchTable();
67     }
68 
SetDispatchTable(const void * const * dispatch_table)69     ALWAYS_INLINE void SetDispatchTable(const void *const *dispatch_table)
70     {
71         return state_.SetDispatchTable(dispatch_table);
72     }
73 
SaveState()74     ALWAYS_INLINE void SaveState()
75     {
76         state_.SaveState();
77     }
78 
RestoreState()79     ALWAYS_INLINE void RestoreState()
80     {
81         state_.RestoreState();
82     }
83 
GetOpcodeExtension()84     ALWAYS_INLINE uint16_t GetOpcodeExtension() const
85     {
86         return opcode_extension_;
87     }
88 
SetOpcodeExtension(uint16_t opcode_extension)89     ALWAYS_INLINE void SetOpcodeExtension(uint16_t opcode_extension)
90     {
91         opcode_extension_ = opcode_extension;
92     }
93 
GetPrimaryOpcode()94     ALWAYS_INLINE uint8_t GetPrimaryOpcode() const
95     {
96         return static_cast<unsigned>(GetInst().GetOpcode()) & OPCODE_MASK;
97     }
98 
GetSecondaryOpcode()99     ALWAYS_INLINE uint8_t GetSecondaryOpcode() const
100     {
101         return (static_cast<unsigned>(GetInst().GetOpcode()) >> 8U) & OPCODE_MASK;
102     }
103 
IsPrimaryOpcodeValid()104     ALWAYS_INLINE bool IsPrimaryOpcodeValid() const
105     {
106         return GetInst().IsPrimaryOpcodeValid();
107     }
108 
GetInst()109     ALWAYS_INLINE BytecodeInstruction GetInst() const
110     {
111         return state_.GetInst();
112     }
113 
GetAcc()114     ALWAYS_INLINE const AccVRegisterT &GetAcc() const
115     {
116         return state_.GetAcc();
117     }
118 
GetAcc()119     ALWAYS_INLINE AccVRegisterT &GetAcc()
120     {
121         return state_.GetAcc();
122     }
123 
GetFakeInstBuf()124     ALWAYS_INLINE auto &GetFakeInstBuf()
125     {
126         return fake_inst_buf_;
127     }
128 
GetBytecodeOffset()129     ALWAYS_INLINE uint32_t GetBytecodeOffset() const
130     {
131         return GetInst().GetAddress() - instructions_;
132     }
133 
134 private:
135     static constexpr size_t FAKE_INST_BUF_SIZE = 4;
136     static constexpr uint8_t OPCODE_MASK = 0xFFU;
137 
138     State state_;
139     std::array<uint8_t, FAKE_INST_BUF_SIZE> fake_inst_buf_;
140     uint16_t opcode_extension_ {0};
141     const uint8_t *instructions_ {nullptr};
142 };
143 
144 }  // namespace panda::interpreter
145 
146 #endif  // PANDA_INTERPRETER_INSTRUCTION_HANDLER_STATE_H_
147