* Copyright (c) 2021-2026 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "debuginfoDumper.h"
namespace panda::es2panda::debuginfo {
DebugInfoDumper::DebugInfoDumper(const pandasm::Program *prog) : prog_(prog) {}
static const char *PutComma(bool comma)
{
return comma ? "," : "";
}
template <typename T>
void DebugInfoDumper::WrapArray(const char *name, const std::vector<T> &array, bool comma)
{
ss_ << std::endl;
Indent();
ss_ << "\"" << name << "\": "
<< "[";
if (array.empty()) {
ss_ << "]" << PutComma(comma);
return;
}
ss_ << "\n";
indent_++;
if constexpr (std::is_same_v<T, pandasm::debuginfo::LocalVariable>) {
typename std::vector<T>::const_reverse_iterator elem;
for (elem = array.rbegin(); elem != array.rend(); ++elem) {
Indent();
WriteVariableInfo(*elem);
(std::next(elem) == array.rend()) ? ss_ << "" : ss_ << ",";
ss_ << "\n";
}
} else {
typename std::vector<T>::const_iterator elem;
for (elem = array.begin(); elem != array.end(); ++elem) {
Indent();
if constexpr (std::is_same_v<T, pandasm::InsPtr>) {
WriteIns(*elem);
} else if constexpr (std::is_same_v<T, pandasm::Function::Parameter>) {
ss_ << "\"" << (*elem).type.GetName() << "\"";
} else if constexpr (std::is_same_v<T, std::string>) {
ss_ << "\"" << *elem << "\"";
} else if constexpr (std::is_same_v<T, std::variant<int64_t, double>>) {
if (std::holds_alternative<int64_t>(*elem)) {
ss_ << std::to_string(std::get<int64_t>(*elem));
} else {
ss_ << std::to_string(std::get<double>(*elem));
}
} else {
ss_ << std::to_string(*elem);
}
(std::next(elem) == array.end()) ? ss_ << "" : ss_ << ",";
ss_ << "\n";
}
}
indent_--;
Indent();
ss_ << "]" << PutComma(comma);
}
void DebugInfoDumper::WriteIns(const pandasm::InsPtr &ins)
{
ss_ << "{";
WriteProperty("opcode", ins->OpcodeToString());
indent_++;
WrapArray("regs", ins->Regs());
WrapArray("ids", ins->Ids());
WrapArray("imms", ins->Imms());
ss_ << std::endl;
Indent();
ss_ << "\"label\": "
<< "\"" << (ins->IsLabel() ? ins->Label() : "") << "\",";
WritePosInfo(ins->ins_debug);
indent_--;
Indent();
ss_ << "}";
}
void DebugInfoDumper::WriteMetaData(const std::vector<pandasm::AnnotationData> &metaData)
{
for (const auto &it : metaData) {
for (const auto &elem : it.GetElements()) {
pandasm::ScalarValue *value = elem.GetValue()->GetAsScalar();
if (value->GetType() == pandasm::Value::Type::STRING) {
WriteProperty(elem.GetName().c_str(), value->GetValue<std::string>(), false);
} else if (value->GetType() == pandasm::Value::Type::U32) {
WriteProperty(elem.GetName().c_str(), value->GetValue<size_t>());
}
}
}
}
void DebugInfoDumper::WritePosInfo(const pandasm::debuginfo::Ins &posInfo)
{
ss_ << std::endl;
Indent();
ss_ << "\"debug_pos_info\": {";
WriteProperty("sourceLineNum", static_cast<int32_t>(posInfo.line_number), false);
Indent();
ss_ << "}" << std::endl;
}
void DebugInfoDumper::WriteVariableInfo(const pandasm::debuginfo::LocalVariable &localVariableDebug)
{
ss_ << "{";
WriteProperty("name", localVariableDebug.name);
WriteProperty("signature", "any");
WriteProperty("signatureType", "any");
WriteProperty("reg", localVariableDebug.reg);
WriteProperty("start", static_cast<size_t>(localVariableDebug.start));
WriteProperty("length", static_cast<size_t>(localVariableDebug.length), false);
Indent();
ss_ << "}";
}
void DebugInfoDumper::Dump()
{
ss_ << "{\n";
indent_++;
Indent();
ss_ << "\"functions\": [" << std::endl;
auto iter = prog_->function_table.begin();
for (; iter != prog_->function_table.end(); ++iter) {
indent_++;
Indent();
ss_ << "{";
WriteProperty("name", iter->first);
ss_ << std::endl;
indent_++;
Indent();
ss_ << "\"signature\": {";
WriteProperty("retType", iter->second.ReturnType().GetName());
indent_++;
WrapArray("params", iter->second.params, false);
indent_ -= 2U;
ss_ << std::endl;
Indent();
ss_ << "},";
WrapArray("ins", iter->second.ins);
WrapArray("variables", iter->second.local_variable_debug);
WriteProperty("sourceFile", iter->second.source_file);
WriteProperty("sourceCode", iter->second.source_code);
WriteMetaData(iter->second.metadata->GetAnnotations());
indent_--;
Indent();
ss_ << "}";
if (std::next(iter) != prog_->function_table.end()) {
ss_ << ",";
}
ss_ << std::endl;
}
indent_--;
Indent();
ss_ << "]" << std::endl;
ss_ << "}";
ss_ << std::endl;
std::cout << ss_.str();
}
void DebugInfoDumper::WriteProperty(const char *key, const Value &value, bool comma)
{
ss_ << std::endl;
indent_++;
Indent();
ss_ << "\"" << key << "\": ";
if (std::holds_alternative<std::string>(value)) {
ss_ << "\"" << std::get<std::string>(value) << "\"";
} else if (std::holds_alternative<size_t>(value)) {
ss_ << std::to_string(std::get<size_t>(value));
} else if (std::holds_alternative<int32_t>(value)) {
ss_ << std::to_string(std::get<int32_t>(value));
}
comma ? ss_ << "," : ss_ << std::endl;
indent_--;
}
void DebugInfoDumper::Indent()
{
for (int32_t i = 0; i <= indent_; i++) {
ss_ << " ";
}
}
}