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_PANDA_ASSEMBLY = "PandaAssembly"; 50 constexpr std::string_view DUMP_CONTENT_SPACE = " "; 51 constexpr std::string_view DUMP_CONTENT_DOUBLE_SPACES = " "; 52 constexpr std::string_view DUMP_CONTENT_TRIPLE_SPACES = " "; 53 constexpr std::string_view DUMP_CONTENT_NONUPLE_SPACES = " "; 54 constexpr std::string_view DUMP_CONTENT_TAB = "\t"; 55 constexpr std::string_view DUMP_CONTENT_SINGLE_ENDL = "\n"; 56 constexpr std::string_view DUMP_CONTENT_DOUBLE_ENDL = "\n\n"; 57 constexpr std::string_view DUMP_CONTENT_COLON = ":"; 58 constexpr std::string_view DUMP_CONTENT_ATTR_EXTERNAL = "<external>"; 59 constexpr std::string_view DUMP_CONTENT_ATTR_STATIC = "<static>"; 60 constexpr std::string_view DUMP_CONTENT_LEFT_CURLY_BRACKET = "{"; 61 constexpr std::string_view DUMP_CONTENT_RIGHT_CURLY_BRACKET = "}"; 62 constexpr std::string_view DUMP_CONTENT_LEFT_SQUARE_BRACKET = "["; 63 constexpr std::string_view DUMP_CONTENT_RIGHT_SQUARE_BRACKET = "]"; 64 constexpr std::string_view DUMP_CONTENT_LEFT_PARENTHESIS = "("; 65 constexpr std::string_view DUMP_CONTENT_RIGHT_PARENTHESIS = ")"; 66 constexpr std::string_view DUMP_CONTENT_COMMA = ","; 67 constexpr std::string_view DUMP_CONTENT_LOCAL_VAR_TABLE = "#\t Start Length Register Name Signature\n"; 68 constexpr std::string_view DUMP_CONTENT_LINE_NUMBER = " # line: "; 69 constexpr std::string_view DUMP_CONTENT_COLUMN_NUMBER = " # column: "; 70 constexpr std::string_view DUMP_CONTENT_FUNCTION_PARAM_NAME_PREFIX = "a"; 71 constexpr std::string_view DUMP_CONTENT_TRY_BEGIN_LABEL = "try_begin_label : "; 72 constexpr std::string_view DUMP_CONTENT_TRY_END_LABEL = "try_end_label : "; 73 constexpr std::string_view DUMP_CONTENT_CATCH_BEGIN_LABEL = "catch_begin_label : "; 74 constexpr std::string_view DUMP_CONTENT_CATCH_END_LABEL = "catch_end_label : "; 75 76 class PandasmDumperUtils { 77 public: 78 static std::string GetFunctionKindString(panda_file::FunctionKind function_kind); 79 static std::string LiteralTagToString(const panda_file::LiteralTag &tag); 80 static bool IsMatchLiteralId(const pandasm::Ins &pa_ins); 81 static size_t GetLiteralIdIndex4Ins(const pandasm::Ins &pa_ins); 82 static std::string GetMappedLabel(const std::string &label, const LabelMap &label_map); 83 static pandasm::Ins DeepCopyIns(const pandasm::Ins &input); 84 static pandasm::Function::CatchBlock DeepCopyCatchBlock(const pandasm::Function::CatchBlock &catch_block); 85 static uint32_t GetLiteralArrayIdFromName(const std::string &literal_array_id_name); 86 87 static LiteralTagToStringMap literal_tag_to_string_map_; 88 static OpcodeLiteralIdIndexMap opcode_literal_id_index_map_; 89 static FunctionKindToStringMap function_kind_to_string_map_; 90 }; // class PandasmDumperUtils 91 92 } // namespace panda::abc2program 93 94 #endif // ABC2PROGRAM_DUMP_UTILS_H