1 /*
2 * Copyright (c) 2024-2025 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 {panda_file::LiteralTag::ETS_IMPLEMENTS, "ets_implements"},
51 };
52
53 FunctionKindToStringMap PandasmDumperUtils::function_kind_to_string_map_ = {
54 {panda_file::FunctionKind::NONE, "FunctionKind::NONE"},
55 {panda_file::FunctionKind::FUNCTION, "FunctionKind::FUNCTION"},
56 {panda_file::FunctionKind::NC_FUNCTION, "FunctionKind::NC_FUNCTION"},
57 {panda_file::FunctionKind::GENERATOR_FUNCTION, "FunctionKind::GENERATOR_FUNCTION"},
58 {panda_file::FunctionKind::ASYNC_FUNCTION, "FunctionKind::ASYNC_FUNCTION"},
59 {panda_file::FunctionKind::ASYNC_GENERATOR_FUNCTION, "FunctionKind::ASYNC_GENERATOR_FUNCTION"},
60 {panda_file::FunctionKind::ASYNC_NC_FUNCTION, "FunctionKind::ASYNC_NC_FUNCTION"},
61 {panda_file::FunctionKind::CONCURRENT_FUNCTION, "FunctionKind::CONCURRENT_FUNCTION"},
62 {static_cast<panda_file::FunctionKind>(static_cast<uint8_t>(panda_file::FunctionKind::FUNCTION) |
63 static_cast<uint8_t>(panda_file::FunctionKind::SENDABLE_FUNCTION)),
64 "FunctionKind::SENDABLE_FUNCTION"},
65 {static_cast<panda_file::FunctionKind>(static_cast<uint8_t>(panda_file::FunctionKind::ASYNC_FUNCTION) |
66 static_cast<uint8_t>(panda_file::FunctionKind::SENDABLE_FUNCTION)),
67 "FunctionKind::SENDABLE_ASYNC_FUNCTION"},
68 };
69
GetFunctionKindString(panda_file::FunctionKind function_kind)70 std::string PandasmDumperUtils::GetFunctionKindString(panda_file::FunctionKind function_kind)
71 {
72 auto it = function_kind_to_string_map_.find(function_kind);
73 ASSERT(it != function_kind_to_string_map_.end());
74 return it->second;
75 }
76
LiteralTagToString(const panda_file::LiteralTag & tag)77 std::string PandasmDumperUtils::LiteralTagToString(const panda_file::LiteralTag &tag)
78 {
79 auto it = literal_tag_to_string_map_.find(tag);
80 ASSERT(it != literal_tag_to_string_map_.end());
81 return it->second;
82 }
83
IsMatchLiteralId(const pandasm::InsPtr & pa_ins)84 bool PandasmDumperUtils::IsMatchLiteralId(const pandasm::InsPtr &pa_ins)
85 {
86 auto it = opcode_literal_id_index_map_.find(pa_ins->opcode);
87 return (it != opcode_literal_id_index_map_.end());
88 }
89
GetLiteralIdIndex4Ins(const pandasm::InsPtr & pa_ins)90 size_t PandasmDumperUtils::GetLiteralIdIndex4Ins(const pandasm::InsPtr &pa_ins)
91 {
92 auto it = opcode_literal_id_index_map_.find(pa_ins->opcode);
93 ASSERT(it != opcode_literal_id_index_map_.end());
94 return it->second;
95 }
96
GetMappedLabel(const std::string & label,const LabelMap & label_map)97 std::string PandasmDumperUtils::GetMappedLabel(const std::string &label, const LabelMap &label_map)
98 {
99 auto it = label_map.find(label);
100 if (it != label_map.end()) {
101 return it->second;
102 } else {
103 return "";
104 }
105 }
106
DeepCopyCatchBlock(const pandasm::Function::CatchBlock & catch_block)107 pandasm::Function::CatchBlock PandasmDumperUtils::DeepCopyCatchBlock(
108 const pandasm::Function::CatchBlock &catch_block)
109 {
110 pandasm::Function::CatchBlock res{};
111 res.whole_line = catch_block.whole_line;
112 res.exception_record = catch_block.exception_record;
113 res.try_begin_label = catch_block.try_begin_label;
114 res.try_end_label = catch_block.try_end_label;
115 res.catch_begin_label = catch_block.catch_begin_label;
116 res.catch_end_label = catch_block.catch_end_label;
117 return res;
118 }
119
GetLiteralArrayIdFromName(const std::string & literal_array_id_name)120 uint32_t PandasmDumperUtils::GetLiteralArrayIdFromName(const std::string &literal_array_id_name)
121 {
122 auto pos = literal_array_id_name.rfind(UNDERLINE);
123 ASSERT(pos != std::string::npos);
124 std::stringstream id_str(literal_array_id_name.substr(pos + 1));
125 uint32_t id = 0;
126 id_str >> id;
127 ASSERT(!id_str.fail());
128 return id;
129 }
130
131 } // namespace panda::abc2program