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
19namespace panda::abc2program {
20
21LiteralTagToStringMap 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
52FunctionKindToStringMap 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
63std::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
70std::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
77bool 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
83size_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
90std::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
100pandasm::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.whole_line = input.ins_debug.whole_line;
116    debug_ins.bound_left = input.ins_debug.bound_left;
117    debug_ins.bound_right = input.ins_debug.bound_right;
118    return res;
119}
120
121pandasm::Function::CatchBlock PandasmDumperUtils::DeepCopyCatchBlock(
122    const pandasm::Function::CatchBlock &catch_block)
123{
124    pandasm::Function::CatchBlock res{};
125    res.whole_line = catch_block.whole_line;
126    res.exception_record = catch_block.exception_record;
127    res.try_begin_label = catch_block.try_begin_label;
128    res.try_end_label = catch_block.try_end_label;
129    res.catch_begin_label = catch_block.catch_begin_label;
130    res.catch_end_label = catch_block.catch_end_label;
131    return res;
132}
133
134uint32_t PandasmDumperUtils::GetLiteralArrayIdFromName(const std::string &literal_array_id_name)
135{
136    auto pos = literal_array_id_name.rfind(UNDERLINE);
137    ASSERT(pos != std::string::npos);
138    std::stringstream id_str(literal_array_id_name.substr(pos + 1));
139    uint32_t id = 0;
140    id_str >> id;
141    ASSERT(!id_str.fail());
142    return id;
143}
144
145}  // namespace panda::abc2program