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     bool NeedToAddDummyEndIns() const;
39     void AddDummyEndIns();
40     void AddJumpLabels() const;
41     void AddJumpLabel4InsAtIndex(uint32_t inst_idx, pandasm::Ins &curr_pa_ins) const;
42     void AddLabel4InsAtIndex(uint32_t inst_idx) const;
43     void AddLabel4InsAtPc(uint32_t inst_pc) const;
44     std::string GetLabelNameAtPc(uint32_t inst_pc) const;
45     void FillCatchBlocks();
46     void HandleTryBlock(panda_file::CodeDataAccessor::TryBlock &try_block);
47     void HandleCatchBlock(panda_file::CodeDataAccessor::CatchBlock &catch_block);
48     void FillCatchBlockLabels(pandasm::Function::CatchBlock &pa_catch_block) const;
49     void FillExceptionRecord(panda_file::CodeDataAccessor::CatchBlock &catch_block,
50                              pandasm::Function::CatchBlock &pa_catch_block) const;
51     void FillLocalVariableTable();
52     template <typename T>
53     void SkipToNextEntryIfNeeded(uint32_t &idx,
54                                  uint32_t &offset_start,
55                                  uint32_t &offset_end,
56                                  uint32_t inst_idx,
57                                  const T &table);
58     void FillInsDebug();
59     uint32_t GetInstIdxByInstPc(uint32_t inst_pc) const;
60     uint32_t GetInstPcByInstIdx(uint32_t inst_idx) const;
61     panda_file::File::EntityId method_id_;
62     pandasm::Function &function_;
63     std::unique_ptr<panda_file::CodeDataAccessor> code_data_accessor_;
64     std::unique_ptr<AbcCodeConverter> code_converter_;
65     std::vector<uint32_t> jump_inst_idx_vec_;
66     std::map<uint32_t, uint32_t> inst_pc_idx_map_;
67     std::unordered_map<uint32_t, uint32_t> inst_idx_pc_map_;
68     uint32_t ins_size_ = 0;
69     uint32_t curr_try_begin_inst_pc_ = 0;
70     uint32_t curr_try_end_inst_pc_ = 0;
71     uint32_t curr_catch_begin_pc_ = 0;
72     uint32_t curr_catch_end_pc_ = 0;
73     const panda_file::DebugInfoExtractor &debug_info_extractor_;
74 };  // AbcCodeProcessor
75 
76 }  // namespace panda::abc2program
77 
78 #endif  // ABC2PROGRAM_ABC_CODE_PROCESSOR_H
79