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