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 ABC2PROGRAM_ABC_CODE_PROCESSOR_H 17 #define ABC2PROGRAM_ABC_CODE_PROCESSOR_H 18 19 #include <unordered_map> 20 #include <vector> 21 #include "abc_file_entity_processor.h" 22 #include "common/abc_code_converter.h" 23 #include "code_data_accessor-inl.h" 24 25 26 namespace panda::abc2program { 27 28 class AbcCodeProcessor : public AbcFileEntityProcessor { 29 public: 30 AbcCodeProcessor(panda_file::File::EntityId entity_id, Abc2ProgramEntityContainer &entity_container, 31 panda_file::File::EntityId method_id, pandasm::Function &function); 32 void FillProgramData() override; 33 34 private: 35 void FillFunctionRegsNum(); 36 void FillIns(); 37 void FillInsWithoutLabels(); 38 void AddLabels(); 39 bool NeedToAddDummyEndIns() const; 40 void AddDummyEndIns(); 41 void AddJumpLabels() const; 42 void AddJumpLabel4InsAtIndex(uint32_t inst_idx, pandasm::InsPtr &curr_pa_ins) const; 43 void AddLabel4InsAtIndex(uint32_t inst_idx) const; 44 void AddLabel4InsAtPc(uint32_t inst_pc) const; 45 std::string GetLabelNameAtPc(uint32_t inst_pc) const; 46 void FillCatchBlocks(); 47 void HandleTryBlock(panda_file::CodeDataAccessor::TryBlock &try_block); 48 void HandleCatchBlock(panda_file::CodeDataAccessor::CatchBlock &catch_block); 49 void FillCatchBlockLabels(pandasm::Function::CatchBlock &pa_catch_block) const; 50 void FillExceptionRecord(panda_file::CodeDataAccessor::CatchBlock &catch_block, 51 pandasm::Function::CatchBlock &pa_catch_block) const; 52 void FillLocalVariableTable(); 53 template <typename T> 54 void SkipToNextEntryIfNeeded(uint32_t &idx, 55 uint32_t &offset_start, 56 uint32_t &offset_end, 57 uint32_t inst_idx, 58 const T &table); 59 void FillInsDebug(); 60 uint32_t GetInstIdxByInstPc(uint32_t inst_pc) const; 61 uint32_t GetInstPcByInstIdx(uint32_t inst_idx) const; 62 panda_file::File::EntityId method_id_; 63 pandasm::Function &function_; 64 std::unique_ptr<panda_file::CodeDataAccessor> code_data_accessor_; 65 std::unique_ptr<AbcCodeConverter> code_converter_; 66 std::vector<uint32_t> jump_inst_idx_vec_; 67 std::map<uint32_t, uint32_t> inst_pc_idx_map_; 68 std::unordered_map<uint32_t, uint32_t> inst_idx_pc_map_; 69 mutable std::unordered_map<uint32_t, std::string> inst_idx_label_map_; 70 uint32_t ins_size_ = 0; 71 uint32_t curr_try_begin_inst_pc_ = 0; 72 uint32_t curr_try_end_inst_pc_ = 0; 73 uint32_t curr_catch_begin_pc_ = 0; 74 uint32_t curr_catch_end_pc_ = 0; 75 const panda_file::DebugInfoExtractor &debug_info_extractor_; 76 }; // AbcCodeProcessor 77 78 } // namespace panda::abc2program 79 80 #endif // ABC2PROGRAM_ABC_CODE_PROCESSOR_H 81