• 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 #include "common/abc_file_utils.h"
17 #include "dump_utils.h"
18 
19 namespace panda::abc2program {
20 
21 LiteralTagToStringMap PandasmDumperUtils::literal_tag_to_string_map_ = {
22     {panda_file::LiteralTag::BOOL, "u1"},
23     {panda_file::LiteralTag::ARRAY_U1, "u1[]"},
24     {panda_file::LiteralTag::ARRAY_U8, "u8[]"},
25     {panda_file::LiteralTag::ARRAY_I8, "i8[]"},
26     {panda_file::LiteralTag::ARRAY_U16, "u16[]"},
27     {panda_file::LiteralTag::ARRAY_I16, "i16[]"},
28     {panda_file::LiteralTag::ARRAY_U32, "u32[]"},
29     {panda_file::LiteralTag::INTEGER, "i32"},
30     {panda_file::LiteralTag::ARRAY_I32, "i32[]"},
31     {panda_file::LiteralTag::ARRAY_U64, "u64[]"},
32     {panda_file::LiteralTag::ARRAY_I64, "i64[]"},
33     {panda_file::LiteralTag::ARRAY_F32, "f32[]"},
34     {panda_file::LiteralTag::DOUBLE, "f64"},
35     {panda_file::LiteralTag::ARRAY_F64, "f64[]"},
36     {panda_file::LiteralTag::STRING, "string"},
37     {panda_file::LiteralTag::ARRAY_STRING, "string[]"},
38     {panda_file::LiteralTag::METHOD, "method"},
39     {panda_file::LiteralTag::GETTER, "getter"},
40     {panda_file::LiteralTag::SETTER, "setter"},
41     {panda_file::LiteralTag::GENERATORMETHOD, "generator_method"},
42     {panda_file::LiteralTag::ASYNCGENERATORMETHOD, "async_generator_method"},
43     {panda_file::LiteralTag::ACCESSOR, "accessor"},
44     {panda_file::LiteralTag::METHODAFFILIATE, "method_affiliate"},
45     {panda_file::LiteralTag::NULLVALUE, "null_value"},
46     {panda_file::LiteralTag::TAGVALUE, "tag_value"},
47     {panda_file::LiteralTag::LITERALBUFFERINDEX, "literal_buffer_index"},
48     {panda_file::LiteralTag::LITERALARRAY, "literal_array"},
49     {panda_file::LiteralTag::BUILTINTYPEINDEX, "builtin_type_index"},
50 };
51 
52 FunctionKindToStringMap PandasmDumperUtils::function_kind_to_string_map_ = {
53     {panda_file::FunctionKind::NONE, "FunctionKind::NONE"},
54     {panda_file::FunctionKind::FUNCTION, "FunctionKind::FUNCTION"},
55     {panda_file::FunctionKind::NC_FUNCTION, "FunctionKind::NC_FUNCTION"},
56     {panda_file::FunctionKind::GENERATOR_FUNCTION, "FunctionKind::GENERATOR_FUNCTION"},
57     {panda_file::FunctionKind::ASYNC_FUNCTION, "FunctionKind::ASYNC_FUNCTION"},
58     {panda_file::FunctionKind::ASYNC_GENERATOR_FUNCTION, "FunctionKind::ASYNC_GENERATOR_FUNCTION"},
59     {panda_file::FunctionKind::ASYNC_NC_FUNCTION, "FunctionKind::ASYNC_NC_FUNCTION"},
60     {panda_file::FunctionKind::CONCURRENT_FUNCTION, "FunctionKind::CONCURRENT_FUNCTION"},
61 };
62 
GetFunctionKindString(panda_file::FunctionKind function_kind)63 std::string PandasmDumperUtils::GetFunctionKindString(panda_file::FunctionKind function_kind)
64 {
65     auto it = function_kind_to_string_map_.find(function_kind);
66     ASSERT(it != function_kind_to_string_map_.end());
67     return it->second;
68 }
69 
LiteralTagToString(const panda_file::LiteralTag & tag)70 std::string PandasmDumperUtils::LiteralTagToString(const panda_file::LiteralTag &tag)
71 {
72     auto it = literal_tag_to_string_map_.find(tag);
73     ASSERT(it != literal_tag_to_string_map_.end());
74     return it->second;
75 }
76 
IsMatchLiteralId(const pandasm::Ins & pa_ins)77 bool PandasmDumperUtils::IsMatchLiteralId(const pandasm::Ins &pa_ins)
78 {
79     auto it = opcode_literal_id_index_map_.find(pa_ins.opcode);
80     return (it != opcode_literal_id_index_map_.end());
81 }
82 
GetLiteralIdIndex4Ins(const pandasm::Ins & pa_ins)83 size_t PandasmDumperUtils::GetLiteralIdIndex4Ins(const pandasm::Ins &pa_ins)
84 {
85     auto it = opcode_literal_id_index_map_.find(pa_ins.opcode);
86     ASSERT(it != opcode_literal_id_index_map_.end());
87     return it->second;
88 }
89 
GetMappedLabel(const std::string & label,const LabelMap & label_map)90 std::string PandasmDumperUtils::GetMappedLabel(const std::string &label, const LabelMap &label_map)
91 {
92     auto it = label_map.find(label);
93     if (it != label_map.end()) {
94         return it->second;
95     } else {
96         return "";
97     }
98 }
99 
DeepCopyIns(const pandasm::Ins & input)100 pandasm::Ins PandasmDumperUtils::DeepCopyIns(const pandasm::Ins &input)
101 {
102     pandasm::Ins res{};
103     res.opcode = input.opcode;
104     res.regs=input.regs;
105     res.ids=input.ids;
106     for (size_t i = 0; i < input.imms.size(); ++i) {
107         pandasm::Ins::IType new_imm = input.imms[i];
108         res.imms.emplace_back(new_imm);
109     }
110     res.label = input.label;
111     res.set_label = input.set_label;
112     auto &debug_ins = res.ins_debug;
113     debug_ins.line_number = input.ins_debug.line_number;
114     debug_ins.column_number = input.ins_debug.column_number;
115     debug_ins.bound_left = input.ins_debug.bound_left;
116     debug_ins.bound_right = input.ins_debug.bound_right;
117     return res;
118 }
119 
DeepCopyCatchBlock(const pandasm::Function::CatchBlock & catch_block)120 pandasm::Function::CatchBlock PandasmDumperUtils::DeepCopyCatchBlock(
121     const pandasm::Function::CatchBlock &catch_block)
122 {
123     pandasm::Function::CatchBlock res{};
124     res.whole_line = catch_block.whole_line;
125     res.exception_record = catch_block.exception_record;
126     res.try_begin_label = catch_block.try_begin_label;
127     res.try_end_label = catch_block.try_end_label;
128     res.catch_begin_label = catch_block.catch_begin_label;
129     res.catch_end_label = catch_block.catch_end_label;
130     return res;
131 }
132 
GetLiteralArrayIdFromName(const std::string & literal_array_id_name)133 uint32_t PandasmDumperUtils::GetLiteralArrayIdFromName(const std::string &literal_array_id_name)
134 {
135     auto pos = literal_array_id_name.rfind(UNDERLINE);
136     ASSERT(pos != std::string::npos);
137     std::stringstream id_str(literal_array_id_name.substr(pos + 1));
138     uint32_t id = 0;
139     id_str >> id;
140     ASSERT(!id_str.fail());
141     return id;
142 }
143 
144 }  // namespace panda::abc2program