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