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_PROGRANM_DUMPER_PROGRAM_DUMP_H 17 #define ABC2PROGRAM_PROGRANM_DUMPER_PROGRAM_DUMP_H 18 19 #include <iostream> 20 #include <assembly-program.h> 21 #include "dump_utils.h" 22 23 namespace panda::abc2program { 24 25 class PandasmProgramDumper { 26 public: PandasmProgramDumper()27 PandasmProgramDumper() {} PandasmProgramDumper(bool is_normalized, bool is_debug)28 PandasmProgramDumper(bool is_normalized, bool is_debug) : is_normalized_(is_normalized), is_debug_(is_debug) {} 29 void Dump(std::ostream &os, const pandasm::Program &program); 30 void SetAbcFilePath(const std::string &abc_file_path); 31 32 private: 33 void DumpAbcFilePath(std::ostream &os) const; 34 void DumpProgramLanguage(std::ostream &os) const; 35 void DumpLiteralArrayTable(std::ostream &os) const; 36 void DumpRecordTable(std::ostream &os) const; 37 void DumpRecord(std::ostream &os, const pandasm::Record &record) const; 38 bool DumpRecordMetaData(std::ostream &os, const pandasm::Record &record) const; 39 void DumpFieldList(std::ostream &os, const pandasm::Record &record) const; 40 void DumpField(std::ostream &os, const pandasm::Field &field) const; 41 void DumpFieldMetaData(std::ostream &os, const pandasm::Field &field) const; 42 void DumpRecordSourceFile(std::ostream &os, const pandasm::Record &record) const; 43 void DumpFunctionTable(std::ostream &os); 44 void DumpFunction(std::ostream &os, const pandasm::Function &function); 45 void DumpFunctionKind(std::ostream &os, const pandasm::Function &function) const; 46 void DumpFunctionAnnotations(std::ostream &os, const pandasm::Function &function) const; 47 void DumpFunctionHead(std::ostream &os, const pandasm::Function &function) const; 48 void DumpFunctionReturnType(std::ostream &os, const pandasm::Function &function) const; 49 void DumpFunctionName(std::ostream &os, const pandasm::Function &function) const; 50 void DumpFunctionParams(std::ostream &os, const pandasm::Function &function) const; 51 void DumpFunctionParamAtIndex(std::ostream &os, const pandasm::Function::Parameter ¶m, size_t idx) const; 52 void DumpFunctionAttributes(std::ostream &os, const pandasm::Function &function) const; 53 void DumpFunctionBody(std::ostream &os, const pandasm::Function &function); 54 void DumpFunctionIns(std::ostream &os, const pandasm::Function &function); 55 void DumpOriginalFunctionIns(std::ostream &os, const pandasm::Function &function); 56 void DumpNormalizedFunctionIns(std::ostream &os, const pandasm::Function &function); 57 void DumpFunctionDebugInfo(std::ostream &os, const pandasm::Function &function); 58 void UpdateLocalVarMap(const pandasm::Function &function, 59 std::map<int32_t, panda::pandasm::debuginfo::LocalVariable>& local_variable_table); 60 void DumpAnnotationData(std::ostream &os, const pandasm::AnnotationData &anno) const; 61 void DumpArrayValue(std::ostream &os, const pandasm::ArrayValue &array) const; 62 void DumpScalarValue(std::ostream &os, const pandasm::ScalarValue &scalar) const; 63 void GetOriginalDumpIns(const pandasm::Function &function); 64 void GetFinalDumpIns(); 65 void GetInvalidOpLabelMap(); 66 void HandleInvalidopInsLabel(size_t invalid_op_idx, pandasm::Ins &invalid_op_ins); 67 pandasm::Ins *GetNearestValidopIns4InvalidopIns(size_t invalid_op_ins_idx); 68 void GetFinalLabelMap(); 69 void UpdateLabels4DumpIns(std::vector<pandasm::Ins*> &dump_ins, const LabelMap &label_map) const; 70 void UpdateLabels4DumpInsAtIndex(size_t idx, std::vector<pandasm::Ins*> &dump_ins, 71 const LabelMap &label_map) const; 72 std::string GetMappedLabel(const std::string &label, const LabelMap &label_map) const; 73 void HandleFinalLabelAtIndex(size_t idx); 74 void DumpFinalIns(std::ostream &os); 75 void DumpFunctionCatchBlocks(std::ostream &os, const pandasm::Function &function) const; 76 void DumpOriginalFunctionCatchBlocks(std::ostream &os, const pandasm::Function &function) const; 77 void DumpNormalizedFunctionCatchBlocks(std::ostream &os, const pandasm::Function &function) const; 78 void DumpCatchBlock(std::ostream &os, const pandasm::Function::CatchBlock &catch_block) const; 79 void UpdateCatchBlock(pandasm::Function::CatchBlock &catch_block) const; 80 std::string GetUpdatedCatchBlockLabel(const std::string &orignal_label) const; 81 void ReplaceLiteralId4Ins(pandasm::Ins &pa_ins) const; 82 void DumpStrings(std::ostream &os) const; 83 std::string SerializeLiteralArray(const pandasm::LiteralArray &lit_array, uint32_t id) const; 84 void SerializeLiterals(const pandasm::LiteralArray &lit_array, std::stringstream &os) const; 85 void SerializeLiteralsAtIndex(const pandasm::LiteralArray &lit_array, std::stringstream &os, size_t i) const; 86 void SerializeNestedLiteralArrayById(std::stringstream &os, const std::string &literal_array_id_name) const; 87 // True when the option 'dump-normalized-asm-program' is enabled. See option description for details 88 bool is_normalized_ = false; 89 // True when the option 'debug-info' is enabled. When both it and is_normalized_ are true, skip dump 90 // of function annotation and the record with name '_ESSlotNumberAnnotation' 91 bool is_debug_ = false; 92 std::string abc_file_path_; 93 std::vector<pandasm::Ins> original_dump_ins_; 94 std::vector<pandasm::Ins*> original_dump_ins_ptrs_; 95 std::vector<pandasm::Ins*> final_dump_ins_ptrs_; 96 LabelMap invalid_op_label_map_; 97 LabelMap final_label_map_; 98 const pandasm::Program *program_ = nullptr; 99 size_t regs_num_ = 0; 100 std::unordered_map<pandasm::Ins*, uint32_t> original_ins_index_map_; 101 std::unordered_map<pandasm::Ins*, uint32_t> final_ins_index_map_; 102 mutable std::unordered_set<uint32_t> processing_literal_array_id_set_; 103 }; 104 105 } // namespace panda::abc2program 106 107 #endif // ABC2PROGRAM_PROGRANM_DUMPER_PROGRAM_DUMP_H 108