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 "program_dump.h" 17b1994897Sopenharmony_ci#include "abc2program_log.h" 18b1994897Sopenharmony_ci#include "common/abc_file_utils.h" 19b1994897Sopenharmony_ci#include "dump_utils.h" 20b1994897Sopenharmony_ci#include "os/file.h" 21b1994897Sopenharmony_ci 22b1994897Sopenharmony_cinamespace panda::abc2program { 23b1994897Sopenharmony_ci 24b1994897Sopenharmony_civoid PandasmProgramDumper::Dump(std::ostream &os, const pandasm::Program &program) 25b1994897Sopenharmony_ci{ 26b1994897Sopenharmony_ci program_ = &program; 27b1994897Sopenharmony_ci os << std::flush; 28b1994897Sopenharmony_ci DumpAbcFilePath(os); 29b1994897Sopenharmony_ci DumpProgramLanguage(os); 30b1994897Sopenharmony_ci DumpLiteralArrayTable(os); 31b1994897Sopenharmony_ci DumpRecordTable(os); 32b1994897Sopenharmony_ci DumpFunctionTable(os); 33b1994897Sopenharmony_ci DumpStrings(os); 34b1994897Sopenharmony_ci} 35b1994897Sopenharmony_ci 36b1994897Sopenharmony_civoid PandasmProgramDumper::SetAbcFilePath(const std::string &abc_file_path) 37b1994897Sopenharmony_ci{ 38b1994897Sopenharmony_ci abc_file_path_ = abc_file_path; 39b1994897Sopenharmony_ci} 40b1994897Sopenharmony_ci 41b1994897Sopenharmony_civoid PandasmProgramDumper::DumpAbcFilePath(std::ostream &os) const 42b1994897Sopenharmony_ci{ 43b1994897Sopenharmony_ci if (abc_file_path_.empty()) { 44b1994897Sopenharmony_ci return; 45b1994897Sopenharmony_ci } 46b1994897Sopenharmony_ci std::string file_abs_path = os::file::File::GetAbsolutePath(abc_file_path_).Value(); 47b1994897Sopenharmony_ci os << DUMP_TITLE_SOURCE_BINARY << file_abs_path << DUMP_CONTENT_DOUBLE_ENDL; 48b1994897Sopenharmony_ci} 49b1994897Sopenharmony_ci 50b1994897Sopenharmony_civoid PandasmProgramDumper::DumpProgramLanguage(std::ostream &os) const 51b1994897Sopenharmony_ci{ 52b1994897Sopenharmony_ci os << DUMP_TITLE_LANGUAGE; 53b1994897Sopenharmony_ci if (program_->lang == panda::panda_file::SourceLang::ECMASCRIPT) { 54b1994897Sopenharmony_ci os << DUMP_CONTENT_ECMASCRIPT; 55b1994897Sopenharmony_ci } else { 56b1994897Sopenharmony_ci os << DUMP_CONTENT_PANDA_ASSEMBLY; 57b1994897Sopenharmony_ci } 58b1994897Sopenharmony_ci os << DUMP_CONTENT_DOUBLE_ENDL; 59b1994897Sopenharmony_ci} 60b1994897Sopenharmony_ci 61b1994897Sopenharmony_civoid PandasmProgramDumper::DumpLiteralArrayTable(std::ostream &os) const 62b1994897Sopenharmony_ci{ 63b1994897Sopenharmony_ci os << DUMP_TITLE_SEPARATOR; 64b1994897Sopenharmony_ci os << DUMP_TITLE_LITERALS; 65b1994897Sopenharmony_ci os << DUMP_CONTENT_DOUBLE_ENDL; 66b1994897Sopenharmony_ci auto it = program_->literalarray_table.begin(); 67b1994897Sopenharmony_ci auto end = program_->literalarray_table.end(); 68b1994897Sopenharmony_ci // In normalized mode, sort dump result of literal arrays before output. 69b1994897Sopenharmony_ci if (is_normalized_) { 70b1994897Sopenharmony_ci std::vector<std::string> literal_arrays; 71b1994897Sopenharmony_ci for (; it != end; ++it) { 72b1994897Sopenharmony_ci auto id = PandasmDumperUtils::GetLiteralArrayIdFromName(it->first); 73b1994897Sopenharmony_ci auto lit_arr = SerializeLiteralArray(it->second, id); 74b1994897Sopenharmony_ci lit_arr += DUMP_CONTENT_DOUBLE_ENDL; 75b1994897Sopenharmony_ci literal_arrays.emplace_back(lit_arr); 76b1994897Sopenharmony_ci } 77b1994897Sopenharmony_ci std::sort(literal_arrays.begin(), literal_arrays.end()); 78b1994897Sopenharmony_ci for (auto &str : literal_arrays) { 79b1994897Sopenharmony_ci os << str; 80b1994897Sopenharmony_ci } 81b1994897Sopenharmony_ci } else { 82b1994897Sopenharmony_ci for (; it != end; ++it) { 83b1994897Sopenharmony_ci os << it->first << DUMP_CONTENT_SPACE; 84b1994897Sopenharmony_ci auto id = PandasmDumperUtils::GetLiteralArrayIdFromName(it->first); 85b1994897Sopenharmony_ci os << SerializeLiteralArray(it->second, id); 86b1994897Sopenharmony_ci os << DUMP_CONTENT_DOUBLE_ENDL; 87b1994897Sopenharmony_ci } 88b1994897Sopenharmony_ci } 89b1994897Sopenharmony_ci os << DUMP_CONTENT_DOUBLE_ENDL; 90b1994897Sopenharmony_ci} 91b1994897Sopenharmony_ci 92b1994897Sopenharmony_civoid PandasmProgramDumper::DumpRecordTable(std::ostream &os) const 93b1994897Sopenharmony_ci{ 94b1994897Sopenharmony_ci os << DUMP_TITLE_SEPARATOR; 95b1994897Sopenharmony_ci os << DUMP_TITLE_RECORDS; 96b1994897Sopenharmony_ci os << DUMP_CONTENT_DOUBLE_ENDL; 97b1994897Sopenharmony_ci for (const auto &it : program_->record_table) { 98b1994897Sopenharmony_ci if (is_normalized_ && is_debug_ && it.first == SLOT_NUMBER_RECORD_NAME) { 99b1994897Sopenharmony_ci continue; 100b1994897Sopenharmony_ci } 101b1994897Sopenharmony_ci DumpRecord(os, it.second); 102b1994897Sopenharmony_ci } 103b1994897Sopenharmony_ci os << DUMP_CONTENT_SINGLE_ENDL; 104b1994897Sopenharmony_ci} 105b1994897Sopenharmony_ci 106b1994897Sopenharmony_civoid PandasmProgramDumper::DumpRecord(std::ostream &os, const pandasm::Record &record) const 107b1994897Sopenharmony_ci{ 108b1994897Sopenharmony_ci if (is_normalized_) { 109b1994897Sopenharmony_ci if (AbcFileUtils::IsGlobalTypeName(record.name)) { 110b1994897Sopenharmony_ci return; 111b1994897Sopenharmony_ci } 112b1994897Sopenharmony_ci if (AbcFileUtils::IsESTypeAnnotationName(record.name)) { 113b1994897Sopenharmony_ci return; 114b1994897Sopenharmony_ci } 115b1994897Sopenharmony_ci } 116b1994897Sopenharmony_ci os << DUMP_TITLE_RECORD << record.name; 117b1994897Sopenharmony_ci if (DumpRecordMetaData(os, record)) { 118b1994897Sopenharmony_ci DumpFieldList(os, record); 119b1994897Sopenharmony_ci } 120b1994897Sopenharmony_ci DumpRecordSourceFile(os, record); 121b1994897Sopenharmony_ci} 122b1994897Sopenharmony_ci 123b1994897Sopenharmony_cibool PandasmProgramDumper::DumpRecordMetaData(std::ostream &os, const pandasm::Record &record) const 124b1994897Sopenharmony_ci{ 125b1994897Sopenharmony_ci if (record.metadata->IsForeign()) { 126b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << DUMP_CONTENT_ATTR_EXTERNAL; 127b1994897Sopenharmony_ci os << DUMP_CONTENT_SINGLE_ENDL; 128b1994897Sopenharmony_ci return false; 129b1994897Sopenharmony_ci } 130b1994897Sopenharmony_ci return true; 131b1994897Sopenharmony_ci} 132b1994897Sopenharmony_ci 133b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFieldList(std::ostream &os, const pandasm::Record &record) const 134b1994897Sopenharmony_ci{ 135b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << DUMP_CONTENT_LEFT_CURLY_BRACKET << DUMP_CONTENT_SINGLE_ENDL; 136b1994897Sopenharmony_ci for (const auto &it : record.field_list) { 137b1994897Sopenharmony_ci DumpField(os, it); 138b1994897Sopenharmony_ci } 139b1994897Sopenharmony_ci os << DUMP_CONTENT_RIGHT_CURLY_BRACKET << DUMP_CONTENT_SINGLE_ENDL; 140b1994897Sopenharmony_ci} 141b1994897Sopenharmony_ci 142b1994897Sopenharmony_civoid PandasmProgramDumper::DumpField(std::ostream &os, const pandasm::Field &field) const 143b1994897Sopenharmony_ci{ 144b1994897Sopenharmony_ci os << DUMP_CONTENT_TAB << field.type.GetPandasmName() << DUMP_CONTENT_SPACE << field.name; 145b1994897Sopenharmony_ci DumpFieldMetaData(os, field); 146b1994897Sopenharmony_ci os << DUMP_CONTENT_SINGLE_ENDL; 147b1994897Sopenharmony_ci} 148b1994897Sopenharmony_ci 149b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFieldMetaData(std::ostream &os, const pandasm::Field &field) const 150b1994897Sopenharmony_ci{ 151b1994897Sopenharmony_ci if (field.metadata->GetValue()) { 152b1994897Sopenharmony_ci DumpScalarValue(os, *(field.metadata->GetValue())); 153b1994897Sopenharmony_ci } 154b1994897Sopenharmony_ci} 155b1994897Sopenharmony_ci 156b1994897Sopenharmony_civoid PandasmProgramDumper::DumpAnnotationData(std::ostream &os, const pandasm::AnnotationData &anno) const 157b1994897Sopenharmony_ci{ 158b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << anno.GetName() << DUMP_CONTENT_SINGLE_ENDL; 159b1994897Sopenharmony_ci for (const auto &element : anno.GetElements()) { 160b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << element.GetName(); 161b1994897Sopenharmony_ci if (element.GetValue()->IsArray()) { 162b1994897Sopenharmony_ci DumpArrayValue(os, *(element.GetValue()->GetAsArray())); 163b1994897Sopenharmony_ci } else { 164b1994897Sopenharmony_ci DumpScalarValue(os, *(element.GetValue()->GetAsScalar())); 165b1994897Sopenharmony_ci } 166b1994897Sopenharmony_ci } 167b1994897Sopenharmony_ci} 168b1994897Sopenharmony_ci 169b1994897Sopenharmony_civoid PandasmProgramDumper::DumpArrayValue(std::ostream &os, const pandasm::ArrayValue &array) const 170b1994897Sopenharmony_ci{ 171b1994897Sopenharmony_ci for (const auto &val : array.GetValues()) { 172b1994897Sopenharmony_ci DumpScalarValue(os, val); 173b1994897Sopenharmony_ci } 174b1994897Sopenharmony_ci} 175b1994897Sopenharmony_civoid PandasmProgramDumper::DumpScalarValue(std::ostream &os, const pandasm::ScalarValue &scalar) const 176b1994897Sopenharmony_ci{ 177b1994897Sopenharmony_ci switch (scalar.GetType()) { 178b1994897Sopenharmony_ci case pandasm::Value::Type::U1: 179b1994897Sopenharmony_ci case pandasm::Value::Type::I8: 180b1994897Sopenharmony_ci case pandasm::Value::Type::U8: 181b1994897Sopenharmony_ci case pandasm::Value::Type::I16: 182b1994897Sopenharmony_ci case pandasm::Value::Type::U16: 183b1994897Sopenharmony_ci case pandasm::Value::Type::I32: 184b1994897Sopenharmony_ci case pandasm::Value::Type::U32: 185b1994897Sopenharmony_ci case pandasm::Value::Type::I64: 186b1994897Sopenharmony_ci case pandasm::Value::Type::U64: 187b1994897Sopenharmony_ci case pandasm::Value::Type::STRING_NULLPTR: { 188b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << scalar.GetValue<uint64_t>(); 189b1994897Sopenharmony_ci break; 190b1994897Sopenharmony_ci } 191b1994897Sopenharmony_ci case pandasm::Value::Type::F32: 192b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << scalar.GetValue<float>(); 193b1994897Sopenharmony_ci break; 194b1994897Sopenharmony_ci case pandasm::Value::Type::F64: { 195b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << scalar.GetValue<double>(); 196b1994897Sopenharmony_ci break; 197b1994897Sopenharmony_ci } 198b1994897Sopenharmony_ci case pandasm::Value::Type::STRING: 199b1994897Sopenharmony_ci case pandasm::Value::Type::METHOD: 200b1994897Sopenharmony_ci case pandasm::Value::Type::ENUM: 201b1994897Sopenharmony_ci case pandasm::Value::Type::LITERALARRAY: { 202b1994897Sopenharmony_ci if (!is_normalized_) { 203b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << scalar.GetValue<std::string>(); 204b1994897Sopenharmony_ci } else { 205b1994897Sopenharmony_ci auto literal_array_id_name = scalar.GetValue<std::string>(); 206b1994897Sopenharmony_ci auto it = program_->literalarray_table.find(literal_array_id_name); 207b1994897Sopenharmony_ci ASSERT(it != program_->literalarray_table.end()); 208b1994897Sopenharmony_ci auto id = PandasmDumperUtils::GetLiteralArrayIdFromName(literal_array_id_name); 209b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << SerializeLiteralArray(it->second, id); 210b1994897Sopenharmony_ci } 211b1994897Sopenharmony_ci break; 212b1994897Sopenharmony_ci } 213b1994897Sopenharmony_ci case pandasm::Value::Type::RECORD: { 214b1994897Sopenharmony_ci pandasm::Type type = scalar.GetValue<pandasm::Type>(); 215b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << type.GetComponentName() << DUMP_CONTENT_SPACE << type.GetName(); 216b1994897Sopenharmony_ci break; 217b1994897Sopenharmony_ci } 218b1994897Sopenharmony_ci case pandasm::Value::Type::ANNOTATION: { 219b1994897Sopenharmony_ci DumpAnnotationData(os, scalar.GetValue<pandasm::AnnotationData>()); 220b1994897Sopenharmony_ci break; 221b1994897Sopenharmony_ci } 222b1994897Sopenharmony_ci default : 223b1994897Sopenharmony_ci break; 224b1994897Sopenharmony_ci } 225b1994897Sopenharmony_ci} 226b1994897Sopenharmony_ci 227b1994897Sopenharmony_civoid PandasmProgramDumper::DumpRecordSourceFile(std::ostream &os, const pandasm::Record &record) const 228b1994897Sopenharmony_ci{ 229b1994897Sopenharmony_ci os << DUMP_TITLE_RECORD_SOURCE_FILE << record.source_file << DUMP_CONTENT_DOUBLE_ENDL; 230b1994897Sopenharmony_ci} 231b1994897Sopenharmony_ci 232b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionTable(std::ostream &os) 233b1994897Sopenharmony_ci{ 234b1994897Sopenharmony_ci os << DUMP_TITLE_SEPARATOR; 235b1994897Sopenharmony_ci os << DUMP_TITLE_METHODS; 236b1994897Sopenharmony_ci os << DUMP_CONTENT_DOUBLE_ENDL; 237b1994897Sopenharmony_ci for (const auto &it : program_->function_table) { 238b1994897Sopenharmony_ci DumpFunction(os, it.second); 239b1994897Sopenharmony_ci } 240b1994897Sopenharmony_ci} 241b1994897Sopenharmony_ci 242b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunction(std::ostream &os, const pandasm::Function &function) 243b1994897Sopenharmony_ci{ 244b1994897Sopenharmony_ci regs_num_ = function.regs_num; 245b1994897Sopenharmony_ci DumpFunctionKind(os, function); 246b1994897Sopenharmony_ci if (!is_normalized_ || !is_debug_) { 247b1994897Sopenharmony_ci DumpFunctionAnnotations(os, function); 248b1994897Sopenharmony_ci } 249b1994897Sopenharmony_ci DumpFunctionHead(os, function); 250b1994897Sopenharmony_ci DumpFunctionBody(os, function); 251b1994897Sopenharmony_ci} 252b1994897Sopenharmony_ci 253b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionKind(std::ostream &os, const pandasm::Function &function) const 254b1994897Sopenharmony_ci{ 255b1994897Sopenharmony_ci os << DUMP_TITLE_FUNCTION_KIND << PandasmDumperUtils::GetFunctionKindString(function.function_kind); 256b1994897Sopenharmony_ci os << DUMP_CONTENT_SINGLE_ENDL; 257b1994897Sopenharmony_ci} 258b1994897Sopenharmony_ci 259b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionAnnotations(std::ostream &os, const pandasm::Function &function) const 260b1994897Sopenharmony_ci{ 261b1994897Sopenharmony_ci for (auto &annotation : function.metadata->GetAnnotations()) { 262b1994897Sopenharmony_ci DumpAnnotationData(os, annotation); 263b1994897Sopenharmony_ci } 264b1994897Sopenharmony_ci os << DUMP_CONTENT_SINGLE_ENDL; 265b1994897Sopenharmony_ci} 266b1994897Sopenharmony_ci 267b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionHead(std::ostream &os, const pandasm::Function &function) const 268b1994897Sopenharmony_ci{ 269b1994897Sopenharmony_ci os << DUMP_TITLE_FUNCTION; 270b1994897Sopenharmony_ci DumpFunctionReturnType(os, function); 271b1994897Sopenharmony_ci DumpFunctionName(os, function); 272b1994897Sopenharmony_ci DumpFunctionParams(os, function); 273b1994897Sopenharmony_ci os << DUMP_CONTENT_SPACE << DUMP_CONTENT_LEFT_CURLY_BRACKET << DUMP_CONTENT_SINGLE_ENDL; 274b1994897Sopenharmony_ci} 275b1994897Sopenharmony_ci 276b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionReturnType(std::ostream &os, const pandasm::Function &function) const 277b1994897Sopenharmony_ci{ 278b1994897Sopenharmony_ci os << function.return_type.GetPandasmName() << DUMP_CONTENT_SPACE; 279b1994897Sopenharmony_ci} 280b1994897Sopenharmony_ci 281b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionName(std::ostream &os, const pandasm::Function &function) const 282b1994897Sopenharmony_ci{ 283b1994897Sopenharmony_ci os << function.name; 284b1994897Sopenharmony_ci} 285b1994897Sopenharmony_ci 286b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionParams(std::ostream &os, const pandasm::Function &function) const 287b1994897Sopenharmony_ci{ 288b1994897Sopenharmony_ci os << DUMP_CONTENT_LEFT_PARENTHESIS; 289b1994897Sopenharmony_ci if (function.params.size() > 0) { 290b1994897Sopenharmony_ci DumpFunctionParamAtIndex(os, function.params[0], 0); 291b1994897Sopenharmony_ci for (size_t i = 1; i < function.params.size(); ++i) { 292b1994897Sopenharmony_ci os << DUMP_CONTENT_COMMA << DUMP_CONTENT_SPACE; 293b1994897Sopenharmony_ci DumpFunctionParamAtIndex(os, function.params[i], i); 294b1994897Sopenharmony_ci } 295b1994897Sopenharmony_ci } 296b1994897Sopenharmony_ci os << DUMP_CONTENT_RIGHT_PARENTHESIS; 297b1994897Sopenharmony_ci} 298b1994897Sopenharmony_ci 299b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionParamAtIndex(std::ostream &os, 300b1994897Sopenharmony_ci const pandasm::Function::Parameter ¶m, 301b1994897Sopenharmony_ci size_t idx) const 302b1994897Sopenharmony_ci{ 303b1994897Sopenharmony_ci os << param.type.GetPandasmName() << DUMP_CONTENT_SPACE << DUMP_CONTENT_FUNCTION_PARAM_NAME_PREFIX << idx; 304b1994897Sopenharmony_ci} 305b1994897Sopenharmony_ci 306b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionAttributes(std::ostream &os, const pandasm::Function &function) const 307b1994897Sopenharmony_ci{ 308b1994897Sopenharmony_ci log::Unimplemented(__PRETTY_FUNCTION__); 309b1994897Sopenharmony_ci} 310b1994897Sopenharmony_ci 311b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionBody(std::ostream &os, const pandasm::Function &function) 312b1994897Sopenharmony_ci{ 313b1994897Sopenharmony_ci DumpFunctionIns(os, function); 314b1994897Sopenharmony_ci DumpFunctionCatchBlocks(os, function); 315b1994897Sopenharmony_ci DumpFunctionDebugInfo(os, function); 316b1994897Sopenharmony_ci os << "}" << DUMP_CONTENT_DOUBLE_ENDL; 317b1994897Sopenharmony_ci} 318b1994897Sopenharmony_ci 319b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionIns(std::ostream &os, const pandasm::Function &function) 320b1994897Sopenharmony_ci{ 321b1994897Sopenharmony_ci if (is_normalized_) { 322b1994897Sopenharmony_ci DumpNormalizedFunctionIns(os, function); 323b1994897Sopenharmony_ci } else { 324b1994897Sopenharmony_ci DumpOriginalFunctionIns(os, function); 325b1994897Sopenharmony_ci } 326b1994897Sopenharmony_ci} 327b1994897Sopenharmony_ci 328b1994897Sopenharmony_civoid PandasmProgramDumper::DumpOriginalFunctionIns(std::ostream &os, const pandasm::Function &function) 329b1994897Sopenharmony_ci{ 330b1994897Sopenharmony_ci for (const pandasm::Ins &pa_ins : function.ins) { 331b1994897Sopenharmony_ci std::string insStr = pa_ins.ToString("", true, regs_num_); 332b1994897Sopenharmony_ci os << DUMP_CONTENT_TAB << std::setw(LINE_WIDTH) 333b1994897Sopenharmony_ci << std::left << insStr 334b1994897Sopenharmony_ci << DUMP_CONTENT_LINE_NUMBER << pa_ins.ins_debug.line_number; 335b1994897Sopenharmony_ci os << std::setw(COLUMN_WIDTH) << std::left << DUMP_CONTENT_SPACE 336b1994897Sopenharmony_ci << DUMP_CONTENT_COLUMN_NUMBER << pa_ins.ins_debug.column_number 337b1994897Sopenharmony_ci << DUMP_CONTENT_SINGLE_ENDL; 338b1994897Sopenharmony_ci } 339b1994897Sopenharmony_ci} 340b1994897Sopenharmony_ci 341b1994897Sopenharmony_civoid PandasmProgramDumper::DumpNormalizedFunctionIns(std::ostream &os, const pandasm::Function &function) 342b1994897Sopenharmony_ci{ 343b1994897Sopenharmony_ci GetOriginalDumpIns(function); 344b1994897Sopenharmony_ci GetInvalidOpLabelMap(); 345b1994897Sopenharmony_ci UpdateLabels4DumpIns(original_dump_ins_ptrs_, invalid_op_label_map_); 346b1994897Sopenharmony_ci GetFinalDumpIns(); 347b1994897Sopenharmony_ci GetFinalLabelMap(); 348b1994897Sopenharmony_ci UpdateLabels4DumpIns(final_dump_ins_ptrs_, final_label_map_); 349b1994897Sopenharmony_ci DumpFinalIns(os); 350b1994897Sopenharmony_ci} 351b1994897Sopenharmony_ci 352b1994897Sopenharmony_civoid PandasmProgramDumper::GetOriginalDumpIns(const pandasm::Function &function) 353b1994897Sopenharmony_ci{ 354b1994897Sopenharmony_ci original_dump_ins_.clear(); 355b1994897Sopenharmony_ci original_dump_ins_ptrs_.clear(); 356b1994897Sopenharmony_ci original_ins_index_map_.clear(); 357b1994897Sopenharmony_ci for (const pandasm::Ins &pa_ins : function.ins) { 358b1994897Sopenharmony_ci original_dump_ins_.emplace_back(PandasmDumperUtils::DeepCopyIns(pa_ins)); 359b1994897Sopenharmony_ci } 360b1994897Sopenharmony_ci uint32_t idx = 0; 361b1994897Sopenharmony_ci for (pandasm::Ins &pa_ins : original_dump_ins_) { 362b1994897Sopenharmony_ci original_dump_ins_ptrs_.emplace_back(&pa_ins); 363b1994897Sopenharmony_ci original_ins_index_map_[&pa_ins] = idx; 364b1994897Sopenharmony_ci idx++; 365b1994897Sopenharmony_ci } 366b1994897Sopenharmony_ci} 367b1994897Sopenharmony_ci 368b1994897Sopenharmony_civoid PandasmProgramDumper::GetFinalDumpIns() 369b1994897Sopenharmony_ci{ 370b1994897Sopenharmony_ci final_dump_ins_ptrs_.clear(); 371b1994897Sopenharmony_ci final_ins_index_map_.clear(); 372b1994897Sopenharmony_ci uint32_t idx = 0; 373b1994897Sopenharmony_ci for (pandasm::Ins *pa_ins : original_dump_ins_ptrs_) { 374b1994897Sopenharmony_ci final_ins_index_map_[pa_ins] = idx; 375b1994897Sopenharmony_ci if (pa_ins->opcode != pandasm::Opcode::INVALID) { 376b1994897Sopenharmony_ci final_dump_ins_ptrs_.emplace_back(pa_ins); 377b1994897Sopenharmony_ci idx++; 378b1994897Sopenharmony_ci } 379b1994897Sopenharmony_ci } 380b1994897Sopenharmony_ci} 381b1994897Sopenharmony_ci 382b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFinalIns(std::ostream &os) 383b1994897Sopenharmony_ci{ 384b1994897Sopenharmony_ci for (pandasm::Ins *pa_ins : final_dump_ins_ptrs_) { 385b1994897Sopenharmony_ci if (PandasmDumperUtils::IsMatchLiteralId(*pa_ins)) { 386b1994897Sopenharmony_ci ReplaceLiteralId4Ins(*pa_ins); 387b1994897Sopenharmony_ci } 388b1994897Sopenharmony_ci std::string insStr = pa_ins->ToString("", true, regs_num_); 389b1994897Sopenharmony_ci os << DUMP_CONTENT_TAB << std::setw(LINE_WIDTH) 390b1994897Sopenharmony_ci << std::left << insStr 391b1994897Sopenharmony_ci << DUMP_CONTENT_LINE_NUMBER << pa_ins->ins_debug.line_number; 392b1994897Sopenharmony_ci os << std::setw(COLUMN_WIDTH) << std::left << DUMP_CONTENT_SPACE 393b1994897Sopenharmony_ci << DUMP_CONTENT_COLUMN_NUMBER << pa_ins->ins_debug.column_number 394b1994897Sopenharmony_ci << DUMP_CONTENT_SINGLE_ENDL; 395b1994897Sopenharmony_ci } 396b1994897Sopenharmony_ci} 397b1994897Sopenharmony_ci 398b1994897Sopenharmony_civoid PandasmProgramDumper::GetInvalidOpLabelMap() 399b1994897Sopenharmony_ci{ 400b1994897Sopenharmony_ci invalid_op_label_map_.clear(); 401b1994897Sopenharmony_ci size_t dump_ins_size = original_dump_ins_.size(); 402b1994897Sopenharmony_ci for (size_t i = 0; i < dump_ins_size; ++i) { 403b1994897Sopenharmony_ci pandasm::Ins &curr_ins = original_dump_ins_[i]; 404b1994897Sopenharmony_ci if (curr_ins.opcode == pandasm::Opcode::INVALID) { 405b1994897Sopenharmony_ci HandleInvalidopInsLabel(i, curr_ins); 406b1994897Sopenharmony_ci } 407b1994897Sopenharmony_ci } 408b1994897Sopenharmony_ci} 409b1994897Sopenharmony_ci 410b1994897Sopenharmony_civoid PandasmProgramDumper::HandleInvalidopInsLabel(size_t invalid_op_idx, pandasm::Ins &invalid_op_ins) 411b1994897Sopenharmony_ci{ 412b1994897Sopenharmony_ci if (!invalid_op_ins.set_label) { 413b1994897Sopenharmony_ci return; 414b1994897Sopenharmony_ci } 415b1994897Sopenharmony_ci pandasm::Ins *nearest_valid_op_ins = GetNearestValidopIns4InvalidopIns(invalid_op_idx); 416b1994897Sopenharmony_ci if (nearest_valid_op_ins == nullptr) { 417b1994897Sopenharmony_ci return; 418b1994897Sopenharmony_ci } 419b1994897Sopenharmony_ci if (!nearest_valid_op_ins->set_label) { 420b1994897Sopenharmony_ci // here, the invalid op ins and its nearest valid op ins has the same label 421b1994897Sopenharmony_ci // the invalid op will be removed, so the label is still unique for each inst 422b1994897Sopenharmony_ci nearest_valid_op_ins->label = invalid_op_ins.label; 423b1994897Sopenharmony_ci nearest_valid_op_ins->set_label = true; 424b1994897Sopenharmony_ci } 425b1994897Sopenharmony_ci invalid_op_label_map_.emplace(invalid_op_ins.label, nearest_valid_op_ins->label); 426b1994897Sopenharmony_ci} 427b1994897Sopenharmony_ci 428b1994897Sopenharmony_cipandasm::Ins *PandasmProgramDumper::GetNearestValidopIns4InvalidopIns(size_t invalid_op_ins_idx) 429b1994897Sopenharmony_ci{ 430b1994897Sopenharmony_ci size_t dump_ins_size = original_dump_ins_.size(); 431b1994897Sopenharmony_ci // search downwards 432b1994897Sopenharmony_ci for (size_t i = invalid_op_ins_idx + 1; i < dump_ins_size; ++i) { 433b1994897Sopenharmony_ci pandasm::Ins &curr_ins = original_dump_ins_[i]; 434b1994897Sopenharmony_ci if (curr_ins.opcode != pandasm::Opcode::INVALID) { 435b1994897Sopenharmony_ci return &curr_ins; 436b1994897Sopenharmony_ci } 437b1994897Sopenharmony_ci } 438b1994897Sopenharmony_ci // search upwards 439b1994897Sopenharmony_ci for (size_t i = 0; i < invalid_op_ins_idx; ++i) { 440b1994897Sopenharmony_ci pandasm::Ins &curr_ins = original_dump_ins_[invalid_op_ins_idx - i - 1]; 441b1994897Sopenharmony_ci if (curr_ins.opcode != pandasm::Opcode::INVALID) { 442b1994897Sopenharmony_ci return &curr_ins; 443b1994897Sopenharmony_ci } 444b1994897Sopenharmony_ci } 445b1994897Sopenharmony_ci return nullptr; 446b1994897Sopenharmony_ci} 447b1994897Sopenharmony_ci 448b1994897Sopenharmony_civoid PandasmProgramDumper::UpdateLabels4DumpIns(std::vector<pandasm::Ins*> &dump_ins, const LabelMap &label_map) const 449b1994897Sopenharmony_ci{ 450b1994897Sopenharmony_ci size_t dump_ins_size = dump_ins.size(); 451b1994897Sopenharmony_ci for (size_t i = 0; i < dump_ins_size; ++i) { 452b1994897Sopenharmony_ci UpdateLabels4DumpInsAtIndex(i, dump_ins, label_map); 453b1994897Sopenharmony_ci } 454b1994897Sopenharmony_ci} 455b1994897Sopenharmony_ci 456b1994897Sopenharmony_civoid PandasmProgramDumper::UpdateLabels4DumpInsAtIndex(size_t idx, std::vector<pandasm::Ins*> &dump_ins, 457b1994897Sopenharmony_ci const LabelMap &label_map) const 458b1994897Sopenharmony_ci{ 459b1994897Sopenharmony_ci pandasm::Ins *curr_ins = dump_ins[idx]; 460b1994897Sopenharmony_ci std::string mapped_label = PandasmDumperUtils::GetMappedLabel(curr_ins->label, label_map); 461b1994897Sopenharmony_ci if (mapped_label != "") { 462b1994897Sopenharmony_ci curr_ins->label = mapped_label; 463b1994897Sopenharmony_ci } 464b1994897Sopenharmony_ci if (curr_ins->IsJump()) { 465b1994897Sopenharmony_ci mapped_label = PandasmDumperUtils::GetMappedLabel(curr_ins->ids[0], label_map); 466b1994897Sopenharmony_ci if (mapped_label != "") { 467b1994897Sopenharmony_ci curr_ins->ids.clear(); 468b1994897Sopenharmony_ci curr_ins->ids.emplace_back(mapped_label); 469b1994897Sopenharmony_ci } 470b1994897Sopenharmony_ci } 471b1994897Sopenharmony_ci} 472b1994897Sopenharmony_ci 473b1994897Sopenharmony_civoid PandasmProgramDumper::GetFinalLabelMap() 474b1994897Sopenharmony_ci{ 475b1994897Sopenharmony_ci final_label_map_.clear(); 476b1994897Sopenharmony_ci size_t dump_ins_size = final_dump_ins_ptrs_.size(); 477b1994897Sopenharmony_ci for (size_t i = 0; i < dump_ins_size; ++i) { 478b1994897Sopenharmony_ci HandleFinalLabelAtIndex(i); 479b1994897Sopenharmony_ci } 480b1994897Sopenharmony_ci} 481b1994897Sopenharmony_ci 482b1994897Sopenharmony_civoid PandasmProgramDumper::HandleFinalLabelAtIndex(size_t idx) 483b1994897Sopenharmony_ci{ 484b1994897Sopenharmony_ci pandasm::Ins *curr_ins = final_dump_ins_ptrs_[idx]; 485b1994897Sopenharmony_ci std::string new_label_name = AbcFileUtils::GetLabelNameByInstIdx(idx); 486b1994897Sopenharmony_ci if (curr_ins->set_label) { 487b1994897Sopenharmony_ci final_label_map_.emplace(curr_ins->label, new_label_name); 488b1994897Sopenharmony_ci } else { 489b1994897Sopenharmony_ci curr_ins->label = new_label_name; 490b1994897Sopenharmony_ci curr_ins->set_label = true; 491b1994897Sopenharmony_ci } 492b1994897Sopenharmony_ci} 493b1994897Sopenharmony_ci 494b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionCatchBlocks(std::ostream &os, const pandasm::Function &function) const 495b1994897Sopenharmony_ci{ 496b1994897Sopenharmony_ci if (is_normalized_) { 497b1994897Sopenharmony_ci DumpNormalizedFunctionCatchBlocks(os, function); 498b1994897Sopenharmony_ci } else { 499b1994897Sopenharmony_ci DumpOriginalFunctionCatchBlocks(os, function); 500b1994897Sopenharmony_ci } 501b1994897Sopenharmony_ci} 502b1994897Sopenharmony_ci 503b1994897Sopenharmony_civoid PandasmProgramDumper::DumpOriginalFunctionCatchBlocks(std::ostream &os, 504b1994897Sopenharmony_ci const pandasm::Function &function) const 505b1994897Sopenharmony_ci{ 506b1994897Sopenharmony_ci for (const pandasm::Function::CatchBlock &catch_block : function.catch_blocks) { 507b1994897Sopenharmony_ci DumpCatchBlock(os, catch_block); 508b1994897Sopenharmony_ci } 509b1994897Sopenharmony_ci} 510b1994897Sopenharmony_ci 511b1994897Sopenharmony_civoid PandasmProgramDumper::DumpNormalizedFunctionCatchBlocks(std::ostream &os, 512b1994897Sopenharmony_ci const pandasm::Function &function) const 513b1994897Sopenharmony_ci{ 514b1994897Sopenharmony_ci std::vector<pandasm::Function::CatchBlock> catch_blocks; 515b1994897Sopenharmony_ci for (const pandasm::Function::CatchBlock &catch_block : function.catch_blocks) { 516b1994897Sopenharmony_ci catch_blocks.emplace_back(PandasmDumperUtils::DeepCopyCatchBlock(catch_block)); 517b1994897Sopenharmony_ci } 518b1994897Sopenharmony_ci for (pandasm::Function::CatchBlock &catch_block : catch_blocks) { 519b1994897Sopenharmony_ci UpdateCatchBlock(catch_block); 520b1994897Sopenharmony_ci } 521b1994897Sopenharmony_ci for (const pandasm::Function::CatchBlock &catch_block : catch_blocks) { 522b1994897Sopenharmony_ci DumpCatchBlock(os, catch_block); 523b1994897Sopenharmony_ci } 524b1994897Sopenharmony_ci} 525b1994897Sopenharmony_ci 526b1994897Sopenharmony_civoid PandasmProgramDumper::UpdateCatchBlock(pandasm::Function::CatchBlock &catch_block) const 527b1994897Sopenharmony_ci{ 528b1994897Sopenharmony_ci catch_block.try_begin_label = GetUpdatedCatchBlockLabel(catch_block.try_begin_label); 529b1994897Sopenharmony_ci catch_block.try_end_label = GetUpdatedCatchBlockLabel(catch_block.try_end_label); 530b1994897Sopenharmony_ci catch_block.catch_begin_label = GetUpdatedCatchBlockLabel(catch_block.catch_begin_label); 531b1994897Sopenharmony_ci catch_block.catch_end_label = GetUpdatedCatchBlockLabel(catch_block.catch_end_label); 532b1994897Sopenharmony_ci} 533b1994897Sopenharmony_ci 534b1994897Sopenharmony_cistd::string PandasmProgramDumper::GetUpdatedCatchBlockLabel(const std::string &orignal_label) const 535b1994897Sopenharmony_ci{ 536b1994897Sopenharmony_ci std::string mapped_label1 = PandasmDumperUtils::GetMappedLabel(orignal_label, invalid_op_label_map_); 537b1994897Sopenharmony_ci if (mapped_label1 == "") { 538b1994897Sopenharmony_ci return orignal_label; 539b1994897Sopenharmony_ci } 540b1994897Sopenharmony_ci std::string mapped_label2 = PandasmDumperUtils::GetMappedLabel(mapped_label1, final_label_map_); 541b1994897Sopenharmony_ci if (mapped_label2 == "") { 542b1994897Sopenharmony_ci return mapped_label1; 543b1994897Sopenharmony_ci } 544b1994897Sopenharmony_ci return mapped_label2; 545b1994897Sopenharmony_ci} 546b1994897Sopenharmony_ci 547b1994897Sopenharmony_civoid PandasmProgramDumper::DumpCatchBlock(std::ostream &os, const pandasm::Function::CatchBlock &catch_block) const 548b1994897Sopenharmony_ci{ 549b1994897Sopenharmony_ci if (catch_block.exception_record.empty()) { 550b1994897Sopenharmony_ci os << DUMP_TITLE_CATCH_ALL << DUMP_CONTENT_SINGLE_ENDL; 551b1994897Sopenharmony_ci } else { 552b1994897Sopenharmony_ci os << DUMP_TITLE_CATCH << DUMP_CONTENT_SINGLE_ENDL; 553b1994897Sopenharmony_ci } 554b1994897Sopenharmony_ci os << DUMP_CONTENT_TAB << DUMP_CONTENT_TRY_BEGIN_LABEL 555b1994897Sopenharmony_ci << catch_block.try_begin_label << DUMP_CONTENT_SINGLE_ENDL; 556b1994897Sopenharmony_ci os << DUMP_CONTENT_TAB << DUMP_CONTENT_TRY_END_LABEL 557b1994897Sopenharmony_ci << catch_block.try_end_label << DUMP_CONTENT_SINGLE_ENDL; 558b1994897Sopenharmony_ci os << DUMP_CONTENT_TAB << DUMP_CONTENT_CATCH_BEGIN_LABEL 559b1994897Sopenharmony_ci << catch_block.catch_begin_label << DUMP_CONTENT_SINGLE_ENDL; 560b1994897Sopenharmony_ci if (!is_normalized_) { 561b1994897Sopenharmony_ci os << DUMP_CONTENT_TAB << DUMP_CONTENT_CATCH_END_LABEL 562b1994897Sopenharmony_ci << catch_block.catch_end_label << DUMP_CONTENT_SINGLE_ENDL; 563b1994897Sopenharmony_ci } 564b1994897Sopenharmony_ci} 565b1994897Sopenharmony_ci 566b1994897Sopenharmony_civoid PandasmProgramDumper::DumpFunctionDebugInfo(std::ostream &os, const pandasm::Function &function) 567b1994897Sopenharmony_ci{ 568b1994897Sopenharmony_ci if (function.local_variable_debug.empty()) { 569b1994897Sopenharmony_ci return; 570b1994897Sopenharmony_ci } 571b1994897Sopenharmony_ci std::map<int32_t, panda::pandasm::debuginfo::LocalVariable> local_variable_table; 572b1994897Sopenharmony_ci if (is_normalized_) { 573b1994897Sopenharmony_ci UpdateLocalVarMap(function, local_variable_table); 574b1994897Sopenharmony_ci } else { 575b1994897Sopenharmony_ci for (const auto &variable_info : function.local_variable_debug) { 576b1994897Sopenharmony_ci local_variable_table[variable_info.reg] = variable_info; 577b1994897Sopenharmony_ci } 578b1994897Sopenharmony_ci } 579b1994897Sopenharmony_ci 580b1994897Sopenharmony_ci os << DUMP_CONTENT_SINGLE_ENDL; 581b1994897Sopenharmony_ci if (local_variable_table.empty()) { 582b1994897Sopenharmony_ci return; 583b1994897Sopenharmony_ci } 584b1994897Sopenharmony_ci 585b1994897Sopenharmony_ci os << DUMP_TITLE_LOCAL_VAR_TABLE; 586b1994897Sopenharmony_ci os << DUMP_CONTENT_SINGLE_ENDL; 587b1994897Sopenharmony_ci os << DUMP_CONTENT_LOCAL_VAR_TABLE; 588b1994897Sopenharmony_ci for (const auto &iter : local_variable_table) { 589b1994897Sopenharmony_ci const auto &variable_info = iter.second; 590b1994897Sopenharmony_ci os << DUMP_CONTENT_TAB 591b1994897Sopenharmony_ci << std::setw(START_WIDTH) << std::right << variable_info.start << DUMP_CONTENT_TRIPLE_SPACES; 592b1994897Sopenharmony_ci os << std::setw(END_WIDTH) << std::right << variable_info.length << DUMP_CONTENT_DOUBLE_SPACES; 593b1994897Sopenharmony_ci os << std::setw(REG_WIDTH) << std::right << variable_info.reg << DUMP_CONTENT_DOUBLE_SPACES; 594b1994897Sopenharmony_ci os << std::setw(NAME_WIDTH) 595b1994897Sopenharmony_ci << std::right << variable_info.name << DUMP_CONTENT_NONUPLE_SPACES << variable_info.signature; 596b1994897Sopenharmony_ci if (!variable_info.signature_type.empty() && variable_info.signature_type != variable_info.signature) { 597b1994897Sopenharmony_ci os << " (" << variable_info.signature_type << ")"; 598b1994897Sopenharmony_ci } 599b1994897Sopenharmony_ci os << DUMP_CONTENT_SINGLE_ENDL; 600b1994897Sopenharmony_ci } 601b1994897Sopenharmony_ci} 602b1994897Sopenharmony_ci 603b1994897Sopenharmony_civoid PandasmProgramDumper::UpdateLocalVarMap(const pandasm::Function &function, 604b1994897Sopenharmony_ci std::map<int32_t, panda::pandasm::debuginfo::LocalVariable>& local_variable_table) 605b1994897Sopenharmony_ci{ 606b1994897Sopenharmony_ci std::unordered_map<uint32_t, uint32_t> original_to_final_index_map_; 607b1994897Sopenharmony_ci uint32_t max_original_idx = 0; 608b1994897Sopenharmony_ci uint32_t max_final_idx = 0; 609b1994897Sopenharmony_ci for (const auto &[key, value] : original_ins_index_map_) { 610b1994897Sopenharmony_ci uint32_t final_idx = final_ins_index_map_[key]; 611b1994897Sopenharmony_ci original_to_final_index_map_[value] = final_idx; 612b1994897Sopenharmony_ci if (value > max_original_idx) { 613b1994897Sopenharmony_ci max_original_idx = value; 614b1994897Sopenharmony_ci max_final_idx = final_idx; 615b1994897Sopenharmony_ci } 616b1994897Sopenharmony_ci } 617b1994897Sopenharmony_ci original_to_final_index_map_[max_original_idx + 1] = max_final_idx; 618b1994897Sopenharmony_ci 619b1994897Sopenharmony_ci for (const auto &variable_info : function.local_variable_debug) { 620b1994897Sopenharmony_ci uint32_t original_start = variable_info.start; 621b1994897Sopenharmony_ci uint32_t original_end = variable_info.length + variable_info.start; 622b1994897Sopenharmony_ci uint32_t new_start = original_to_final_index_map_[original_start]; 623b1994897Sopenharmony_ci uint32_t new_length = original_to_final_index_map_[original_end] - new_start; 624b1994897Sopenharmony_ci panda::pandasm::debuginfo::LocalVariable local_var = {variable_info.name, 625b1994897Sopenharmony_ci variable_info.signature, 626b1994897Sopenharmony_ci variable_info.signature_type, 627b1994897Sopenharmony_ci variable_info.reg, 628b1994897Sopenharmony_ci new_start, 629b1994897Sopenharmony_ci new_length}; 630b1994897Sopenharmony_ci local_variable_table[variable_info.reg] = local_var; 631b1994897Sopenharmony_ci } 632b1994897Sopenharmony_ci} 633b1994897Sopenharmony_ci 634b1994897Sopenharmony_civoid PandasmProgramDumper::DumpStrings(std::ostream &os) const 635b1994897Sopenharmony_ci{ 636b1994897Sopenharmony_ci if (is_normalized_) { 637b1994897Sopenharmony_ci return; 638b1994897Sopenharmony_ci } 639b1994897Sopenharmony_ci os << DUMP_TITLE_SEPARATOR; 640b1994897Sopenharmony_ci os << DUMP_TITLE_STRING; 641b1994897Sopenharmony_ci for (const std::string &str : program_->strings) { 642b1994897Sopenharmony_ci os << str << DUMP_CONTENT_SINGLE_ENDL; 643b1994897Sopenharmony_ci } 644b1994897Sopenharmony_ci} 645b1994897Sopenharmony_ci 646b1994897Sopenharmony_civoid PandasmProgramDumper::ReplaceLiteralId4Ins(pandasm::Ins &pa_ins) const 647b1994897Sopenharmony_ci{ 648b1994897Sopenharmony_ci size_t idx = PandasmDumperUtils::GetLiteralIdIndex4Ins(pa_ins); 649b1994897Sopenharmony_ci std::string id_str = pa_ins.ids[idx]; 650b1994897Sopenharmony_ci auto it = program_->literalarray_table.find(id_str); 651b1994897Sopenharmony_ci ASSERT(it != program_->literalarray_table.end()); 652b1994897Sopenharmony_ci const pandasm::LiteralArray &literal_array = it->second; 653b1994897Sopenharmony_ci auto id = PandasmDumperUtils::GetLiteralArrayIdFromName(it->first); 654b1994897Sopenharmony_ci std::string replaced_value = SerializeLiteralArray(literal_array, id); 655b1994897Sopenharmony_ci pa_ins.ids[idx] = replaced_value; 656b1994897Sopenharmony_ci} 657b1994897Sopenharmony_ci 658b1994897Sopenharmony_cistd::string PandasmProgramDumper::SerializeLiteralArray(const pandasm::LiteralArray &lit_array, uint32_t id) const 659b1994897Sopenharmony_ci{ 660b1994897Sopenharmony_ci if (lit_array.literals_.empty()) { 661b1994897Sopenharmony_ci return ""; 662b1994897Sopenharmony_ci } 663b1994897Sopenharmony_ci std::stringstream ss; 664b1994897Sopenharmony_ci ss << DUMP_CONTENT_LEFT_CURLY_BRACKET << DUMP_CONTENT_SPACE; 665b1994897Sopenharmony_ci ss << lit_array.literals_.size(); 666b1994897Sopenharmony_ci ss << DUMP_CONTENT_SPACE << DUMP_CONTENT_LEFT_SQUARE_BRACKET << DUMP_CONTENT_SPACE; 667b1994897Sopenharmony_ci processing_literal_array_id_set_.emplace(id); 668b1994897Sopenharmony_ci SerializeLiterals(lit_array, ss); 669b1994897Sopenharmony_ci processing_literal_array_id_set_.erase(id); 670b1994897Sopenharmony_ci ss << DUMP_CONTENT_RIGHT_SQUARE_BRACKET << DUMP_CONTENT_RIGHT_CURLY_BRACKET; 671b1994897Sopenharmony_ci return ss.str(); 672b1994897Sopenharmony_ci} 673b1994897Sopenharmony_ci 674b1994897Sopenharmony_civoid PandasmProgramDumper::SerializeLiterals(const pandasm::LiteralArray &lit_array, std::stringstream &os) const 675b1994897Sopenharmony_ci{ 676b1994897Sopenharmony_ci for (size_t i = 0; i < lit_array.literals_.size(); i++) { 677b1994897Sopenharmony_ci SerializeLiteralsAtIndex(lit_array, os, i); 678b1994897Sopenharmony_ci os << DUMP_CONTENT_COMMA << DUMP_CONTENT_SPACE; 679b1994897Sopenharmony_ci } 680b1994897Sopenharmony_ci} 681b1994897Sopenharmony_ci 682b1994897Sopenharmony_civoid PandasmProgramDumper::SerializeLiteralsAtIndex( 683b1994897Sopenharmony_ci const pandasm::LiteralArray &lit_array, std::stringstream &os, size_t i) const 684b1994897Sopenharmony_ci{ 685b1994897Sopenharmony_ci const panda_file::LiteralTag &tag = lit_array.literals_[i].tag_; 686b1994897Sopenharmony_ci os << PandasmDumperUtils::LiteralTagToString(tag) << DUMP_CONTENT_COLON; 687b1994897Sopenharmony_ci const auto &val = lit_array.literals_[i].value_; 688b1994897Sopenharmony_ci switch (tag) { 689b1994897Sopenharmony_ci case panda_file::LiteralTag::BOOL: 690b1994897Sopenharmony_ci os << (std::get<bool>(val)); 691b1994897Sopenharmony_ci break; 692b1994897Sopenharmony_ci case panda_file::LiteralTag::LITERALBUFFERINDEX: 693b1994897Sopenharmony_ci case panda_file::LiteralTag::INTEGER: 694b1994897Sopenharmony_ci os << (bit_cast<int32_t>(std::get<uint32_t>(val))); 695b1994897Sopenharmony_ci break; 696b1994897Sopenharmony_ci case panda_file::LiteralTag::DOUBLE: 697b1994897Sopenharmony_ci os << (std::get<double>(val)); 698b1994897Sopenharmony_ci break; 699b1994897Sopenharmony_ci case panda_file::LiteralTag::STRING: 700b1994897Sopenharmony_ci os << "\"" << (std::get<std::string>(val)) << "\""; 701b1994897Sopenharmony_ci break; 702b1994897Sopenharmony_ci case panda_file::LiteralTag::METHOD: 703b1994897Sopenharmony_ci case panda_file::LiteralTag::GETTER: 704b1994897Sopenharmony_ci case panda_file::LiteralTag::SETTER: 705b1994897Sopenharmony_ci case panda_file::LiteralTag::GENERATORMETHOD: 706b1994897Sopenharmony_ci case panda_file::LiteralTag::ASYNCGENERATORMETHOD: 707b1994897Sopenharmony_ci os << (std::get<std::string>(val)); 708b1994897Sopenharmony_ci break; 709b1994897Sopenharmony_ci case panda_file::LiteralTag::NULLVALUE: 710b1994897Sopenharmony_ci case panda_file::LiteralTag::ACCESSOR: 711b1994897Sopenharmony_ci os << (static_cast<int16_t>(bit_cast<int8_t>(std::get<uint8_t>(val)))); 712b1994897Sopenharmony_ci break; 713b1994897Sopenharmony_ci case panda_file::LiteralTag::METHODAFFILIATE: 714b1994897Sopenharmony_ci os << (std::get<uint16_t>(val)); 715b1994897Sopenharmony_ci break; 716b1994897Sopenharmony_ci case panda_file::LiteralTag::LITERALARRAY: 717b1994897Sopenharmony_ci SerializeNestedLiteralArrayById(os, std::get<std::string>(val)); 718b1994897Sopenharmony_ci break; 719b1994897Sopenharmony_ci case panda_file::LiteralTag::BUILTINTYPEINDEX: 720b1994897Sopenharmony_ci os << (static_cast<int16_t>(std::get<uint8_t>(val))); 721b1994897Sopenharmony_ci break; 722b1994897Sopenharmony_ci case panda_file::LiteralTag::TAGVALUE: 723b1994897Sopenharmony_ci os << (static_cast<int16_t>(std::get<uint8_t>(val))); 724b1994897Sopenharmony_ci break; 725b1994897Sopenharmony_ci default: 726b1994897Sopenharmony_ci UNREACHABLE(); 727b1994897Sopenharmony_ci } 728b1994897Sopenharmony_ci} 729b1994897Sopenharmony_ci 730b1994897Sopenharmony_civoid PandasmProgramDumper::SerializeNestedLiteralArrayById( 731b1994897Sopenharmony_ci std::stringstream &os, const std::string &literal_array_id_name) const 732b1994897Sopenharmony_ci{ 733b1994897Sopenharmony_ci if (!is_normalized_) { 734b1994897Sopenharmony_ci os << literal_array_id_name; 735b1994897Sopenharmony_ci return; 736b1994897Sopenharmony_ci } 737b1994897Sopenharmony_ci auto id = PandasmDumperUtils::GetLiteralArrayIdFromName(literal_array_id_name); 738b1994897Sopenharmony_ci if (processing_literal_array_id_set_.find(id) == processing_literal_array_id_set_.end()) { 739b1994897Sopenharmony_ci auto it = program_->literalarray_table.find(literal_array_id_name); 740b1994897Sopenharmony_ci ASSERT(it != program_->literalarray_table.end()); 741b1994897Sopenharmony_ci os << SerializeLiteralArray(it->second, id); 742b1994897Sopenharmony_ci } else { 743b1994897Sopenharmony_ci UNREACHABLE(); 744b1994897Sopenharmony_ci } 745b1994897Sopenharmony_ci} 746b1994897Sopenharmony_ci 747b1994897Sopenharmony_ci} // namespace panda::abc2program 748