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#ifndef ABC2PROGRAM_DUMP_UTILS_H
17b1994897Sopenharmony_ci#define ABC2PROGRAM_DUMP_UTILS_H
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ci#include <string>
20b1994897Sopenharmony_ci#include <string_view>
21b1994897Sopenharmony_ci#include <unordered_map>
22b1994897Sopenharmony_ci#include <iostream>
23b1994897Sopenharmony_ci#include <assembly-program.h>
24b1994897Sopenharmony_ci#include "file.h"
25b1994897Sopenharmony_ci
26b1994897Sopenharmony_cinamespace panda::abc2program {
27b1994897Sopenharmony_ci
28b1994897Sopenharmony_ciusing LiteralTagToStringMap = std::unordered_map<panda_file::LiteralTag, std::string>;
29b1994897Sopenharmony_ciusing LabelMap = std::unordered_map<std::string, std::string>;
30b1994897Sopenharmony_ciusing FunctionKindToStringMap = std::unordered_map<panda_file::FunctionKind, std::string>;
31b1994897Sopenharmony_ciusing OpcodeLiteralIdIndexMap = std::unordered_map<pandasm::Opcode, size_t>;
32b1994897Sopenharmony_ci
33b1994897Sopenharmony_ci// dump constant
34b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_SOURCE_BINARY = "# source binary: ";
35b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_LANGUAGE = ".language ";
36b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_LITERALS = "# LITERALS";
37b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_RECORDS = "# RECORDS";
38b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_RECORD = ".record ";
39b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_RECORD_SOURCE_FILE = ".record.source_file ";
40b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_METHODS = "# METHODS";
41b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_FUNCTION = ".function ";
42b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_FUNCTION_KIND = ".function_kind ";
43b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_CATCH_ALL = ".catchall :";
44b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_CATCH = ".catch :";
45b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_STRING = "# STRING\n";
46b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_SEPARATOR = "# ====================\n";
47b1994897Sopenharmony_ciconstexpr std::string_view DUMP_TITLE_LOCAL_VAR_TABLE = "# LOCAL_VARIABLE_TABLE:";
48b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_ECMASCRIPT = "ECMAScript";
49b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_PANDA_ASSEMBLY = "PandaAssembly";
50b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_SPACE = " ";
51b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_DOUBLE_SPACES = "  ";
52b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_TRIPLE_SPACES = "   ";
53b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_NONUPLE_SPACES = "         ";
54b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_TAB = "\t";
55b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_SINGLE_ENDL = "\n";
56b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_DOUBLE_ENDL = "\n\n";
57b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_COLON = ":";
58b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_ATTR_EXTERNAL = "<external>";
59b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_ATTR_STATIC = "<static>";
60b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_LEFT_CURLY_BRACKET = "{";
61b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_RIGHT_CURLY_BRACKET = "}";
62b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_LEFT_SQUARE_BRACKET = "[";
63b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_RIGHT_SQUARE_BRACKET = "]";
64b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_LEFT_PARENTHESIS = "(";
65b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_RIGHT_PARENTHESIS = ")";
66b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_COMMA = ",";
67b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_LOCAL_VAR_TABLE = "#\t Start   Length  Register           Name   Signature\n";
68b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_LINE_NUMBER = " # line: ";
69b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_COLUMN_NUMBER = " # column: ";
70b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_FUNCTION_PARAM_NAME_PREFIX = "a";
71b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_TRY_BEGIN_LABEL = "try_begin_label : ";
72b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_TRY_END_LABEL = "try_end_label : ";
73b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_CATCH_BEGIN_LABEL = "catch_begin_label : ";
74b1994897Sopenharmony_ciconstexpr std::string_view DUMP_CONTENT_CATCH_END_LABEL = "catch_end_label : ";
75b1994897Sopenharmony_ci
76b1994897Sopenharmony_ciclass PandasmDumperUtils {
77b1994897Sopenharmony_cipublic:
78b1994897Sopenharmony_ci    static std::string GetFunctionKindString(panda_file::FunctionKind function_kind);
79b1994897Sopenharmony_ci    static std::string LiteralTagToString(const panda_file::LiteralTag &tag);
80b1994897Sopenharmony_ci    static bool IsMatchLiteralId(const pandasm::Ins &pa_ins);
81b1994897Sopenharmony_ci    static size_t GetLiteralIdIndex4Ins(const pandasm::Ins &pa_ins);
82b1994897Sopenharmony_ci    static std::string GetMappedLabel(const std::string &label, const LabelMap &label_map);
83b1994897Sopenharmony_ci    static pandasm::Ins DeepCopyIns(const pandasm::Ins &input);
84b1994897Sopenharmony_ci    static pandasm::Function::CatchBlock DeepCopyCatchBlock(const pandasm::Function::CatchBlock &catch_block);
85b1994897Sopenharmony_ci    static uint32_t GetLiteralArrayIdFromName(const std::string &literal_array_id_name);
86b1994897Sopenharmony_ci
87b1994897Sopenharmony_ci    static LiteralTagToStringMap literal_tag_to_string_map_;
88b1994897Sopenharmony_ci    static OpcodeLiteralIdIndexMap opcode_literal_id_index_map_;
89b1994897Sopenharmony_ci    static FunctionKindToStringMap function_kind_to_string_map_;
90b1994897Sopenharmony_ci};  // class PandasmDumperUtils
91b1994897Sopenharmony_ci
92b1994897Sopenharmony_ci}  // namespace panda::abc2program
93b1994897Sopenharmony_ci
94b1994897Sopenharmony_ci#endif  // ABC2PROGRAM_DUMP_UTILS_H