From b071334f845a7a7b9f812e03fcc1cded8151c607 Mon Sep 17 00:00:00 2001 From: Zelentsov Dmitry Date: Thu, 19 Jun 2025 11:58:00 +0300 Subject: [PATCH] Reduce the size of Ins structure Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICGDQM Tests: use Test-U-Renner and CI testing Signed-off-by: Zelentsov Dmitry --- ets2panda/compiler/core/ETSemitter.cpp | 2 +- ets2panda/compiler/core/emitter.cpp | 29 ++-- ets2panda/compiler/core/emitter.h | 4 +- ets2panda/compiler/core/programElement.h | 4 +- .../compiler/debugger/debuginfoDumper.cpp | 137 ++++++++++++++---- ets2panda/compiler/debugger/debuginfoDumper.h | 8 +- ets2panda/compiler/templates/isa.h.erb | 20 +-- ets2panda/test/utils/asm_test.cpp | 4 +- 8 files changed, 144 insertions(+), 64 deletions(-) diff --git a/ets2panda/compiler/core/ETSemitter.cpp b/ets2panda/compiler/core/ETSemitter.cpp index 16ccde3abe..6af0e5c8a0 100644 --- a/ets2panda/compiler/core/ETSemitter.cpp +++ b/ets2panda/compiler/core/ETSemitter.cpp @@ -130,7 +130,7 @@ static pandasm::Function GenScriptFunction(const ir::ScriptFunction *scriptFunc, !var->Declaration()->Node()->IsETSParameterExpression()) { continue; } - func.params.back().GetOrCreateMetadata().SetAnnotations(emitter->GenCustomAnnotations( + func.params.back().GetOrCreateMetadata()->SetAnnotations(emitter->GenCustomAnnotations( var->Declaration()->Node()->AsETSParameterExpression()->Annotations(), var->Name().Mutf8())); } diff --git a/ets2panda/compiler/core/emitter.cpp b/ets2panda/compiler/core/emitter.cpp index 91ee21b4fb..6586372749 100644 --- a/ets2panda/compiler/core/emitter.cpp +++ b/ets2panda/compiler/core/emitter.cpp @@ -31,11 +31,6 @@ #include "util/es2pandaMacros.h" #include "public/public.h" -#include -#include -#include -#include - namespace ark::es2panda::compiler { using LiteralPair = std::pair; @@ -215,17 +210,17 @@ void FunctionEmitter::GenInstructionDebugInfo(const IRNode *ins, pandasm::Ins *p } auto nodeRange = astNode->Range(); - pandaIns->insDebug.lineNumber = nodeRange.start.line + 1; + pandaIns->insDebug.SetLineNumber(nodeRange.start.line + 1U); if (cg_->IsDebug()) { size_t insLen = GetIRNodeWholeLength(ins); if (insLen != 0) { - pandaIns->insDebug.boundLeft = offset_; - pandaIns->insDebug.boundRight = offset_ + insLen; + pandaIns->insDebug.SetBoundLeft(offset_); + pandaIns->insDebug.SetBoundRight(offset_ + insLen); } offset_ += insLen; - pandaIns->insDebug.wholeLine = WholeLine(nodeRange); + pandaIns->insDebug.SetWholeLine(WholeLine(nodeRange)); } } @@ -413,7 +408,10 @@ static void UpdateLiteralBufferId([[maybe_unused]] ark::pandasm::Ins *ins, [[may #ifdef PANDA_WITH_ECMASCRIPT switch (ins->opcode) { case pandasm::Opcode::ECMA_DEFINECLASSWITHBUFFER: { - ins->imms.back() = std::get(ins->imms.back()) + offset; + if (auto pos = ins->ImmSize(); pos > 0U) { + --pos; + ins->SetImm(pos, std::get(ins->GetImm(pos)) + offset); + } break; } case pandasm::Opcode::ECMA_CREATEARRAYWITHBUFFER: @@ -421,9 +419,12 @@ static void UpdateLiteralBufferId([[maybe_unused]] ark::pandasm::Ins *ins, [[may case pandasm::Opcode::ECMA_CREATEOBJECTHAVINGMETHOD: case pandasm::Opcode::ECMA_DEFINECLASSPRIVATEFIELDS: { constexpr int BASE10 = 10; - uint32_t storedOffset = strtoul(ins->ids.back().data(), nullptr, BASE10); - storedOffset += offset; - ins->ids.back() = std::to_string(storedOffset); + if (auto pos = ins->IDSize(); pos > 0U) { + --pos; + auto storedOffset = std::strtoul(ins->GetID(pos).data(), nullptr, BASE10); + storedOffset += offset; + ins->SetID(pos, std::to_string(storedOffset)); + } break; } default: { @@ -484,7 +485,7 @@ static std::string DumpAsmFunction(std::string name, const pandasm::Function &fu ss << ") {" << std::endl; for (const auto &ins : func.ins) { - ss << (ins.setLabel ? "" : "\t") << ins.ToString("", true, func.GetTotalRegs()) << std::endl; + ss << (ins.HasLabel() ? "" : "\t") << ins.ToString("", true, func.GetTotalRegs()) << std::endl; } ss << "}" << std::endl << std::endl; diff --git a/ets2panda/compiler/core/emitter.h b/ets2panda/compiler/core/emitter.h index 84049be833..7e2623cc38 100644 --- a/ets2panda/compiler/core/emitter.h +++ b/ets2panda/compiler/core/emitter.h @@ -29,8 +29,8 @@ namespace ark::pandasm { struct Program; -struct Function; -struct Ins; +class Function; +class Ins; namespace debuginfo { struct LocalVariable; } // namespace debuginfo diff --git a/ets2panda/compiler/core/programElement.h b/ets2panda/compiler/core/programElement.h index 59e31f6525..481f693757 100644 --- a/ets2panda/compiler/core/programElement.h +++ b/ets2panda/compiler/core/programElement.h @@ -20,8 +20,8 @@ #include "compiler/base/literals.h" namespace ark::pandasm { -struct Ins; -struct Function; +class Ins; +class Function; } // namespace ark::pandasm namespace ark::es2panda::compiler { diff --git a/ets2panda/compiler/debugger/debuginfoDumper.cpp b/ets2panda/compiler/debugger/debuginfoDumper.cpp index f51e14e13e..f98909c460 100644 --- a/ets2panda/compiler/debugger/debuginfoDumper.cpp +++ b/ets2panda/compiler/debugger/debuginfoDumper.cpp @@ -17,9 +17,6 @@ #include "util/es2pandaMacros.h" -#include -#include - namespace ark::es2panda::debuginfo { DebugInfoDumper::DebugInfoDumper(const pandasm::Program *prog) : prog_(prog) {} @@ -88,24 +85,113 @@ void DebugInfoDumper::WrapArray(const char *name, const std::vector &array, b ss_ << "]" << PutComma(comma); } +void DebugInfoDumper::WrapRegArray(ark::pandasm::Ins const &ins, bool comma) +{ + ss_ << std::endl; + Indent(); + ss_ << "\"regs\": " + << "["; + + auto const size = ins.RegSize(); + if (size == 0U) { + ss_ << "]" << PutComma(comma); + return; + } + + ss_ << "\n"; + ++indent_; + + for (std::size_t i = 0U; i < size;) { + Indent(); + ss_ << ins.GetReg(i); + if (++i < size) { + ss_ << ','; + } + ss_ << '\n'; + } + + --indent_; + Indent(); + + ss_ << "]" << PutComma(comma); +} + +void DebugInfoDumper::WrapIDArray(ark::pandasm::Ins const &ins, bool comma) +{ + ss_ << std::endl; + Indent(); + ss_ << "\"ids\": " + << "["; + + auto const size = ins.IDSize(); + if (size == 0U) { + ss_ << "]" << PutComma(comma); + return; + } + + ss_ << "\n"; + ++indent_; + + for (std::size_t i = 0U; i < size;) { + Indent(); + ss_ << ins.GetID(i); + if (++i < size) { + ss_ << ','; + } + ss_ << '\n'; + } + + --indent_; + Indent(); + + ss_ << "]" << PutComma(comma); +} + +void DebugInfoDumper::WrapImmArray(ark::pandasm::Ins const &ins, bool comma) +{ + ss_ << std::endl; + Indent(); + ss_ << "\"imms\": " + << "["; + + auto const size = ins.ImmSize(); + if (size == 0U) { + ss_ << "]" << PutComma(comma); + return; + } + + ss_ << "\n"; + ++indent_; + + for (std::size_t i = 0U; i < size;) { + Indent(); + auto value = ins.GetImm(i); + ss_ << (std::holds_alternative(value) ? std::to_string(std::get(value)) + : std::to_string(std::get(value))); + if (++i < size) { + ss_ << ','; + } + ss_ << '\n'; + } + + --indent_; + Indent(); + + ss_ << "]" << PutComma(comma); +} + void DebugInfoDumper::WriteIns(const pandasm::Ins &ins) { ss_ << "{"; - { - pandasm::Ins insCopy; - insCopy.opcode = ins.opcode; - insCopy.setLabel = ins.setLabel; - insCopy.label = ins.label; - WriteProperty("opcode", insCopy.ToString()); - } + WriteProperty("opcode", ins.ToString()); indent_++; - WrapArray("regs", ins.regs); - WrapArray("ids", ins.ids); - WrapArray("imms", ins.imms); + WrapRegArray(ins); + WrapIDArray(ins); + WrapImmArray(ins); ss_ << std::endl; Indent(); ss_ << "\"label\": " - << "\"" << ins.label << "\","; + << "\"" << (ins.HasLabel() ? ins.Label().c_str() : "") << "\","; WritePosInfo(ins.insDebug); indent_--; Indent(); @@ -131,10 +217,10 @@ void DebugInfoDumper::WritePosInfo(const pandasm::debuginfo::Ins &posInfo) ss_ << std::endl; Indent(); ss_ << "\"debug_pos_info\": {"; - WriteProperty("boundLeft", posInfo.boundLeft); - WriteProperty("boundRight", posInfo.boundRight); - WriteProperty("sourceLineNum", static_cast(posInfo.lineNumber)); - WriteProperty("wholeLine", posInfo.wholeLine, false); + WriteProperty("boundLeft", posInfo.BoundLeft()); + WriteProperty("boundRight", posInfo.BoundRight()); + WriteProperty("sourceLineNum", posInfo.LineNumber()); + WriteProperty("wholeLine", posInfo.WholeLine(), false); Indent(); ss_ << "}" << std::endl; } @@ -216,18 +302,19 @@ void DebugInfoDumper::Dump() std::cout << ss_.str(); } -void DebugInfoDumper::WriteProperty(const char *key, const Value &value, bool comma) +template +void DebugInfoDumper::WriteProperty(const char *key, T &&value, bool comma) { + static_assert(std::is_arithmetic_v> || std::is_same_v>, + "Invalid data type."); ss_ << std::endl; indent_++; Indent(); ss_ << "\"" << key << "\": "; - if (std::holds_alternative(value)) { - ss_ << "\"" << std::get(value) << "\""; - } else if (std::holds_alternative(value)) { - ss_ << std::to_string(std::get(value)); - } else if (std::holds_alternative(value)) { - ss_ << std::to_string(std::get(value)); + if constexpr (std::is_arithmetic_v>) { + ss_ << std::to_string(std::forward(value)); + } else { + ss_ << "\"" << std::forward(value) << "\""; } comma ? ss_ << "," : ss_ << std::endl; diff --git a/ets2panda/compiler/debugger/debuginfoDumper.h b/ets2panda/compiler/debugger/debuginfoDumper.h index 9ee90387c4..0fd1f9ff80 100644 --- a/ets2panda/compiler/debugger/debuginfoDumper.h +++ b/ets2panda/compiler/debugger/debuginfoDumper.h @@ -22,8 +22,6 @@ namespace ark::es2panda::debuginfo { -using Value = std::variant; - class DebugInfoDumper { public: explicit DebugInfoDumper(const pandasm::Program *prog); @@ -36,9 +34,13 @@ public: private: template void WrapArray(const char *name, const std::vector &array, bool comma = true); + void WrapRegArray(ark::pandasm::Ins const &ins, bool comma = true); + void WrapIDArray(ark::pandasm::Ins const &ins, bool comma = true); + void WrapImmArray(ark::pandasm::Ins const &ins, bool comma = true); void WriteIns(const pandasm::Ins &ins); void WriteMetaData(const std::vector &metaData); - void WriteProperty(const char *key, const Value &value, bool comma = true); + template + void WriteProperty(const char *key, T &&value, bool comma = true); void WritePosInfo(const pandasm::debuginfo::Ins &posInfo); void WriteVariableInfo(const pandasm::debuginfo::LocalVariable &localVariableDebug); void Indent(); diff --git a/ets2panda/compiler/templates/isa.h.erb b/ets2panda/compiler/templates/isa.h.erb index 485bd37fdd..87789468d6 100644 --- a/ets2panda/compiler/templates/isa.h.erb +++ b/ets2panda/compiler/templates/isa.h.erb @@ -65,8 +65,7 @@ public: [[maybe_unused]] uint32_t totalRegs) const override { ins->opcode = pandasm::Opcode::INVALID; - ins->setLabel = true; - ins->label = id_; + ins->SetLabel(id_); } private: @@ -184,31 +183,22 @@ public: void Transform(pandasm::Ins *ins, [[maybe_unused]] ProgramElement *programElement, [[maybe_unused]] uint32_t totalRegs) const override { ins->opcode = pandasm::Opcode::<%= node_kind %>; -% if op_map['reg'].length != 0 - ins->regs.reserve(<%= op_map['reg'].length %>); -% end -% if op_map['imm'].length != 0 - ins->imms.reserve(<%= op_map['imm'].length %>); -% end -% if op_map['str'].length + op_map['lbl'].length != 0 - ins->ids.reserve(<%= op_map['str'].length + op_map['lbl'].length %>); -% end % for reg in op_map['reg'] - ins->regs.emplace_back(MapRegister(<%= reg %>.GetIndex(), totalRegs)); + ins->EmplaceReg(MapRegister(<%= reg %>.GetIndex(), totalRegs)); % end % for imm in op_map['imm'] - ins->imms.emplace_back(<%= imm %>); + ins->EmplaceImm(<%= imm %>); % end % if insn.properties.include? 'literalarray_id' and insn.properties.none? 'skip_literal_id_patch' programElement->LiteralBufferIns().push_back(ins); %end % for str in op_map['str'] std::string <%= str %>mutf8 = <%= str %>.Mutf8(); - ins->ids.emplace_back(<%= str %>mutf8); + ins->EmplaceID(<%= str %>mutf8); programElement->Strings().insert(std::move(<%= str %>mutf8)); % end % for lbl in op_map['lbl'] - ins->ids.emplace_back(<%= lbl %>->Id()); + ins->EmplaceID(<%= lbl %>->Id()); % end } diff --git a/ets2panda/test/utils/asm_test.cpp b/ets2panda/test/utils/asm_test.cpp index 1f8e1c6d59..2d15e56dc6 100644 --- a/ets2panda/test/utils/asm_test.cpp +++ b/ets2panda/test/utils/asm_test.cpp @@ -225,7 +225,7 @@ void AsmTest::CheckFunctionParameterAnnotations(ark::pandasm::Program *program, ASSERT_LT(paramIndex, found->second.params.size()); for (const auto &expected : expectedAnnotations) { - auto annotations = found->second.params.at(paramIndex).GetOrCreateMetadata().GetAnnotations(); + auto annotations = found->second.params.at(paramIndex).GetOrCreateMetadata()->GetAnnotations(); auto it = std::find_if(annotations.begin(), annotations.end(), [&expected](const ark::pandasm::AnnotationData &annotation) { return annotation.GetName() == expected.first; @@ -246,7 +246,7 @@ void AsmTest::CheckFunctionParameterWithoutAnnotations(ark::pandasm::Program *pr auto found = functionTable.find(functionName); ASSERT_NE(found, functionTable.end()); ASSERT_LT(paramIndex, found->second.params.size()); - ASSERT(found->second.params.at(paramIndex).GetOrCreateMetadata().GetAnnotations().empty()); + ASSERT(found->second.params.at(paramIndex).GetOrCreateMetadata()->GetAnnotations().empty()); } void AsmTest::CheckClassFieldAnnotations(ark::pandasm::Program *program, const std::string &recordName, -- Gitee