1b1994897Sopenharmony_ci/*
2b1994897Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci * You may obtain a copy of the License at
6b1994897Sopenharmony_ci *
7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci *
9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci * limitations under the License.
14b1994897Sopenharmony_ci */
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#include "common/abc_file_utils.h"
17b1994897Sopenharmony_ci#include "dump_utils.h"
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_cinamespace panda::abc2program {
20b1994897Sopenharmony_ci
21b1994897Sopenharmony_ciLiteralTagToStringMap PandasmDumperUtils::literal_tag_to_string_map_ = {
22b1994897Sopenharmony_ci    {panda_file::LiteralTag::BOOL, "u1"},
23b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_U1, "u1[]"},
24b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_U8, "u8[]"},
25b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_I8, "i8[]"},
26b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_U16, "u16[]"},
27b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_I16, "i16[]"},
28b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_U32, "u32[]"},
29b1994897Sopenharmony_ci    {panda_file::LiteralTag::INTEGER, "i32"},
30b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_I32, "i32[]"},
31b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_U64, "u64[]"},
32b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_I64, "i64[]"},
33b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_F32, "f32[]"},
34b1994897Sopenharmony_ci    {panda_file::LiteralTag::DOUBLE, "f64"},
35b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_F64, "f64[]"},
36b1994897Sopenharmony_ci    {panda_file::LiteralTag::STRING, "string"},
37b1994897Sopenharmony_ci    {panda_file::LiteralTag::ARRAY_STRING, "string[]"},
38b1994897Sopenharmony_ci    {panda_file::LiteralTag::METHOD, "method"},
39b1994897Sopenharmony_ci    {panda_file::LiteralTag::GETTER, "getter"},
40b1994897Sopenharmony_ci    {panda_file::LiteralTag::SETTER, "setter"},
41b1994897Sopenharmony_ci    {panda_file::LiteralTag::GENERATORMETHOD, "generator_method"},
42b1994897Sopenharmony_ci    {panda_file::LiteralTag::ASYNCGENERATORMETHOD, "async_generator_method"},
43b1994897Sopenharmony_ci    {panda_file::LiteralTag::ACCESSOR, "accessor"},
44b1994897Sopenharmony_ci    {panda_file::LiteralTag::METHODAFFILIATE, "method_affiliate"},
45b1994897Sopenharmony_ci    {panda_file::LiteralTag::NULLVALUE, "null_value"},
46b1994897Sopenharmony_ci    {panda_file::LiteralTag::TAGVALUE, "tag_value"},
47b1994897Sopenharmony_ci    {panda_file::LiteralTag::LITERALBUFFERINDEX, "literal_buffer_index"},
48b1994897Sopenharmony_ci    {panda_file::LiteralTag::LITERALARRAY, "literal_array"},
49b1994897Sopenharmony_ci    {panda_file::LiteralTag::BUILTINTYPEINDEX, "builtin_type_index"},
50b1994897Sopenharmony_ci};
51b1994897Sopenharmony_ci
52b1994897Sopenharmony_ciFunctionKindToStringMap PandasmDumperUtils::function_kind_to_string_map_ = {
53b1994897Sopenharmony_ci    {panda_file::FunctionKind::NONE, "FunctionKind::NONE"},
54b1994897Sopenharmony_ci    {panda_file::FunctionKind::FUNCTION, "FunctionKind::FUNCTION"},
55b1994897Sopenharmony_ci    {panda_file::FunctionKind::NC_FUNCTION, "FunctionKind::NC_FUNCTION"},
56b1994897Sopenharmony_ci    {panda_file::FunctionKind::GENERATOR_FUNCTION, "FunctionKind::GENERATOR_FUNCTION"},
57b1994897Sopenharmony_ci    {panda_file::FunctionKind::ASYNC_FUNCTION, "FunctionKind::ASYNC_FUNCTION"},
58b1994897Sopenharmony_ci    {panda_file::FunctionKind::ASYNC_GENERATOR_FUNCTION, "FunctionKind::ASYNC_GENERATOR_FUNCTION"},
59b1994897Sopenharmony_ci    {panda_file::FunctionKind::ASYNC_NC_FUNCTION, "FunctionKind::ASYNC_NC_FUNCTION"},
60b1994897Sopenharmony_ci    {panda_file::FunctionKind::CONCURRENT_FUNCTION, "FunctionKind::CONCURRENT_FUNCTION"},
61b1994897Sopenharmony_ci};
62b1994897Sopenharmony_ci
63b1994897Sopenharmony_cistd::string PandasmDumperUtils::GetFunctionKindString(panda_file::FunctionKind function_kind)
64b1994897Sopenharmony_ci{
65b1994897Sopenharmony_ci    auto it = function_kind_to_string_map_.find(function_kind);
66b1994897Sopenharmony_ci    ASSERT(it != function_kind_to_string_map_.end());
67b1994897Sopenharmony_ci    return it->second;
68b1994897Sopenharmony_ci}
69b1994897Sopenharmony_ci
70b1994897Sopenharmony_cistd::string PandasmDumperUtils::LiteralTagToString(const panda_file::LiteralTag &tag)
71b1994897Sopenharmony_ci{
72b1994897Sopenharmony_ci    auto it = literal_tag_to_string_map_.find(tag);
73b1994897Sopenharmony_ci    ASSERT(it != literal_tag_to_string_map_.end());
74b1994897Sopenharmony_ci    return it->second;
75b1994897Sopenharmony_ci}
76b1994897Sopenharmony_ci
77b1994897Sopenharmony_cibool PandasmDumperUtils::IsMatchLiteralId(const pandasm::Ins &pa_ins)
78b1994897Sopenharmony_ci{
79b1994897Sopenharmony_ci    auto it = opcode_literal_id_index_map_.find(pa_ins.opcode);
80b1994897Sopenharmony_ci    return (it != opcode_literal_id_index_map_.end());
81b1994897Sopenharmony_ci}
82b1994897Sopenharmony_ci
83b1994897Sopenharmony_cisize_t PandasmDumperUtils::GetLiteralIdIndex4Ins(const pandasm::Ins &pa_ins)
84b1994897Sopenharmony_ci{
85b1994897Sopenharmony_ci    auto it = opcode_literal_id_index_map_.find(pa_ins.opcode);
86b1994897Sopenharmony_ci    ASSERT(it != opcode_literal_id_index_map_.end());
87b1994897Sopenharmony_ci    return it->second;
88b1994897Sopenharmony_ci}
89b1994897Sopenharmony_ci
90b1994897Sopenharmony_cistd::string PandasmDumperUtils::GetMappedLabel(const std::string &label, const LabelMap &label_map)
91b1994897Sopenharmony_ci{
92b1994897Sopenharmony_ci    auto it = label_map.find(label);
93b1994897Sopenharmony_ci    if (it != label_map.end()) {
94b1994897Sopenharmony_ci        return it->second;
95b1994897Sopenharmony_ci    } else {
96b1994897Sopenharmony_ci        return "";
97b1994897Sopenharmony_ci    }
98b1994897Sopenharmony_ci}
99b1994897Sopenharmony_ci
100b1994897Sopenharmony_cipandasm::Ins PandasmDumperUtils::DeepCopyIns(const pandasm::Ins &input)
101b1994897Sopenharmony_ci{
102b1994897Sopenharmony_ci    pandasm::Ins res{};
103b1994897Sopenharmony_ci    res.opcode = input.opcode;
104b1994897Sopenharmony_ci    res.regs=input.regs;
105b1994897Sopenharmony_ci    res.ids=input.ids;
106b1994897Sopenharmony_ci    for (size_t i = 0; i < input.imms.size(); ++i) {
107b1994897Sopenharmony_ci        pandasm::Ins::IType new_imm = input.imms[i];
108b1994897Sopenharmony_ci        res.imms.emplace_back(new_imm);
109b1994897Sopenharmony_ci    }
110b1994897Sopenharmony_ci    res.label = input.label;
111b1994897Sopenharmony_ci    res.set_label = input.set_label;
112b1994897Sopenharmony_ci    auto &debug_ins = res.ins_debug;
113b1994897Sopenharmony_ci    debug_ins.line_number = input.ins_debug.line_number;
114b1994897Sopenharmony_ci    debug_ins.column_number = input.ins_debug.column_number;
115b1994897Sopenharmony_ci    debug_ins.whole_line = input.ins_debug.whole_line;
116b1994897Sopenharmony_ci    debug_ins.bound_left = input.ins_debug.bound_left;
117b1994897Sopenharmony_ci    debug_ins.bound_right = input.ins_debug.bound_right;
118b1994897Sopenharmony_ci    return res;
119b1994897Sopenharmony_ci}
120b1994897Sopenharmony_ci
121b1994897Sopenharmony_cipandasm::Function::CatchBlock PandasmDumperUtils::DeepCopyCatchBlock(
122b1994897Sopenharmony_ci    const pandasm::Function::CatchBlock &catch_block)
123b1994897Sopenharmony_ci{
124b1994897Sopenharmony_ci    pandasm::Function::CatchBlock res{};
125b1994897Sopenharmony_ci    res.whole_line = catch_block.whole_line;
126b1994897Sopenharmony_ci    res.exception_record = catch_block.exception_record;
127b1994897Sopenharmony_ci    res.try_begin_label = catch_block.try_begin_label;
128b1994897Sopenharmony_ci    res.try_end_label = catch_block.try_end_label;
129b1994897Sopenharmony_ci    res.catch_begin_label = catch_block.catch_begin_label;
130b1994897Sopenharmony_ci    res.catch_end_label = catch_block.catch_end_label;
131b1994897Sopenharmony_ci    return res;
132b1994897Sopenharmony_ci}
133b1994897Sopenharmony_ci
134b1994897Sopenharmony_ciuint32_t PandasmDumperUtils::GetLiteralArrayIdFromName(const std::string &literal_array_id_name)
135b1994897Sopenharmony_ci{
136b1994897Sopenharmony_ci    auto pos = literal_array_id_name.rfind(UNDERLINE);
137b1994897Sopenharmony_ci    ASSERT(pos != std::string::npos);
138b1994897Sopenharmony_ci    std::stringstream id_str(literal_array_id_name.substr(pos + 1));
139b1994897Sopenharmony_ci    uint32_t id = 0;
140b1994897Sopenharmony_ci    id_str >> id;
141b1994897Sopenharmony_ci    ASSERT(!id_str.fail());
142b1994897Sopenharmony_ci    return id;
143b1994897Sopenharmony_ci}
144b1994897Sopenharmony_ci
145b1994897Sopenharmony_ci}  // namespace panda::abc2program