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 COMPILER_OPTIMIZER_IR_BUILDER_PBC_ITERATOR_H 17 #define COMPILER_OPTIMIZER_IR_BUILDER_PBC_ITERATOR_H 18 19 #include "bytecode_instruction.h" 20 #include "bytecode_instruction-inl.h" 21 22 namespace panda::compiler { 23 struct BytecodeIterator { BytecodeIteratorBytecodeIterator24 explicit BytecodeIterator(BytecodeInstruction inst) : inst_(inst) {} BytecodeIteratorBytecodeIterator25 explicit BytecodeIterator(const uint8_t *data) : inst_(data) {} 26 27 BytecodeIterator &operator++() 28 { 29 inst_ = inst_.GetNext(); 30 return *this; 31 } 32 33 BytecodeInstruction operator*() 34 { 35 return inst_; 36 } 37 bool operator!=(const BytecodeIterator &rhs) 38 { 39 return inst_.GetAddress() != rhs.inst_.GetAddress(); 40 } 41 42 private: 43 BytecodeInstruction inst_; 44 }; 45 46 struct BytecodeInstructions { BytecodeInstructionsBytecodeInstructions47 BytecodeInstructions(const uint8_t *data, size_t size) : data_(data), size_(size) {} 48 49 // NOLINTNEXTLINE(readability-identifier-naming) beginBytecodeInstructions50 BytecodeIterator begin() const 51 { 52 return BytecodeIterator(data_); 53 } 54 // NOLINTNEXTLINE(readability-identifier-naming) endBytecodeInstructions55 BytecodeIterator end() const 56 { 57 return BytecodeIterator(data_ + size_); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 58 } 59 GetPcBytecodeInstructions60 size_t GetPc(const BytecodeInstruction INST) const 61 { 62 return INST.GetAddress() - data_; 63 } GetSizeBytecodeInstructions64 size_t GetSize() const 65 { 66 return size_; 67 } 68 69 private: 70 const uint8_t *data_; 71 size_t size_; 72 }; 73 } // namespace panda::compiler 74 75 #endif // COMPILER_OPTIMIZER_IR_BUILDER_PBC_ITERATOR_H 76