• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 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 LIBABCKIT_SRC_IR_BUILDER_DYNAMIC_PBC_ITERATOR_H
17 #define LIBABCKIT_SRC_IR_BUILDER_DYNAMIC_PBC_ITERATOR_H
18 
19 #include "libabckit/src/irbuilder_dynamic/bytecode_inst.h"
20 #include "libabckit/src/irbuilder_dynamic/bytecode_inst-inl.h"
21 
22 namespace libabckit {
23 
24 // CC-OFFNXT(WordsTool.95) sensitive word conflict
25 // NOLINTNEXTLINE(google-build-using-namespace)
26 using namespace ark;
27 
28 struct BytecodeIterator {
BytecodeIteratorBytecodeIterator29     explicit BytecodeIterator(BytecodeInst inst) : inst_(std::move(inst)) {}
BytecodeIteratorBytecodeIterator30     explicit BytecodeIterator(const uint8_t *data) : inst_(data) {}
31 
32     BytecodeIterator &operator++()
33     {
34         inst_ = inst_.GetNext();
35         return *this;
36     }
37 
38     BytecodeInst operator*()
39     {
40         return inst_;
41     }
42     bool operator!=(const BytecodeIterator &rhs)
43     {
44         return inst_.GetAddress() != rhs.inst_.GetAddress();
45     }
46 
47 private:
48     BytecodeInst inst_;
49 };
50 
51 struct BytecodeInstructions {
BytecodeInstructionsBytecodeInstructions52     BytecodeInstructions(const uint8_t *data, size_t size) : data_(data), size_(size) {}
53 
54     // NOLINTNEXTLINE(readability-identifier-naming)
beginBytecodeInstructions55     BytecodeIterator begin() const
56     {
57         return BytecodeIterator(data_);
58     }
59     // NOLINTNEXTLINE(readability-identifier-naming)
endBytecodeInstructions60     BytecodeIterator end() const
61     {
62         return BytecodeIterator(data_ + size_);  // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
63     }
64 
GetPcBytecodeInstructions65     size_t GetPc(const BytecodeInst &inst) const
66     {
67         return inst.GetAddress() - data_;
68     }
GetSizeBytecodeInstructions69     size_t GetSize() const
70     {
71         return size_;
72     }
73 
74 private:
75     const uint8_t *data_;
76     size_t size_;
77 };
78 
79 }  // namespace libabckit
80 
81 #endif  // LIBABCKIT_SRC_IR_BUILDER_DYNAMIC_PBC_ITERATOR_H
82