• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "debuginfoDumper.h"
17 
18 namespace panda::es2panda::debuginfo {
19 
DebugInfoDumper(const pandasm::Program * prog)20 DebugInfoDumper::DebugInfoDumper(const pandasm::Program *prog) : prog_(prog) {}
21 
PutComma(bool comma)22 static const char *PutComma(bool comma)
23 {
24     return comma ? "," : "";
25 }
26 
27 template <typename T>
WrapArray(const char * name,const std::vector<T> & array,bool comma)28 void DebugInfoDumper::WrapArray(const char *name, const std::vector<T> &array, bool comma)
29 {
30     ss_ << std::endl;
31     Indent();
32     ss_ << "\"" << name << "\": "
33         << "[";
34 
35     if (array.empty()) {
36         ss_ << "]" << PutComma(comma);
37         return;
38     }
39 
40     ss_ << "\n";
41     indent_++;
42     // dump VariableDebugInfo in reverse order to match ts2panda
43     // NOLINTNEXTLINE
44     if constexpr (std::is_same_v<T, pandasm::debuginfo::LocalVariable>) {
45         typename std::vector<T>::const_reverse_iterator elem;
46         for (elem = array.rbegin(); elem != array.rend(); ++elem) {
47             Indent();
48             WriteVariableInfo(*elem);
49             (std::next(elem) == array.rend()) ? ss_ << "" : ss_ << ",";
50             ss_ << "\n";
51         }
52         // NOLINTNEXTLINE
53     } else {
54         typename std::vector<T>::const_iterator elem;
55         for (elem = array.begin(); elem != array.end(); ++elem) {
56             Indent();
57             // NOLINTNEXTLINE
58             if constexpr (std::is_same_v<T, pandasm::InsPtr>) {
59                 WriteIns(*elem);
60                 // NOLINTNEXTLINE
61             } else if constexpr (std::is_same_v<T, pandasm::Function::Parameter>) {
62                 ss_ << "\"" << (*elem).type.GetName() << "\"";
63                 // NOLINTNEXTLINE
64             } else if constexpr (std::is_same_v<T, std::string>) {
65                 ss_ << "\"" << *elem << "\"";
66                 // NOLINTNEXTLINE
67             } else if constexpr (std::is_same_v<T, std::variant<int64_t, double>>) {
68                 if (std::holds_alternative<int64_t>(*elem)) {
69                     ss_ << std::to_string(std::get<int64_t>(*elem));
70                 } else {
71                     ss_ << std::to_string(std::get<double>(*elem));
72                 }
73                 // NOLINTNEXTLINE
74             } else {
75                 ss_ << std::to_string(*elem);
76             }
77 
78             (std::next(elem) == array.end()) ? ss_ << "" : ss_ << ",";
79             ss_ << "\n";
80         }
81     }
82 
83     indent_--;
84     Indent();
85 
86     ss_ << "]" << PutComma(comma);
87 }
88 
WriteIns(const pandasm::InsPtr & ins)89 void DebugInfoDumper::WriteIns(const pandasm::InsPtr &ins)
90 {
91     ss_ << "{";
92     WriteProperty("opcode", ins->OpcodeToString());
93     indent_++;
94     WrapArray("regs", ins->Regs());
95     WrapArray("ids", ins->Ids());
96     WrapArray("imms", ins->Imms());
97     ss_ << std::endl;
98     Indent();
99     ss_ << "\"label\": "
100         << "\"" << (ins->IsLabel() ? ins->Label() : "") << "\",";
101     WritePosInfo(ins->ins_debug);
102     indent_--;
103     Indent();
104     ss_ << "}";
105 }
106 
WriteMetaData(const std::vector<pandasm::AnnotationData> & metaData)107 void DebugInfoDumper::WriteMetaData(const std::vector<pandasm::AnnotationData> &metaData)
108 {
109     for (const auto &it : metaData) {
110         for (const auto &elem : it.GetElements()) {
111             pandasm::ScalarValue *value = elem.GetValue()->GetAsScalar();
112             if (value->GetType() == pandasm::Value::Type::STRING) {
113                 WriteProperty(elem.GetName().c_str(), value->GetValue<std::string>(), false);
114             } else if (value->GetType() == pandasm::Value::Type::U32) {
115                 WriteProperty(elem.GetName().c_str(), value->GetValue<size_t>());
116             }
117         }
118     }
119 }
120 
WritePosInfo(const pandasm::debuginfo::Ins & posInfo)121 void DebugInfoDumper::WritePosInfo(const pandasm::debuginfo::Ins &posInfo)
122 {
123     ss_ << std::endl;
124     Indent();
125     ss_ << "\"debug_pos_info\": {";
126     WriteProperty("sourceLineNum", static_cast<int32_t>(posInfo.line_number), false);
127     Indent();
128     ss_ << "}" << std::endl;
129 }
130 
WriteVariableInfo(const pandasm::debuginfo::LocalVariable & localVariableDebug)131 void DebugInfoDumper::WriteVariableInfo(const pandasm::debuginfo::LocalVariable &localVariableDebug)
132 {
133     ss_ << "{";
134     WriteProperty("name", localVariableDebug.name);
135     WriteProperty("signature", localVariableDebug.signature);
136     WriteProperty("signatureType", localVariableDebug.signature_type);
137     WriteProperty("reg", localVariableDebug.reg);
138     WriteProperty("start", static_cast<size_t>(localVariableDebug.start));
139     WriteProperty("length", static_cast<size_t>(localVariableDebug.length), false);
140     Indent();
141     ss_ << "}";
142 }
143 
Dump()144 void DebugInfoDumper::Dump()
145 {
146     ss_ << "{\n";
147     indent_++;
148     Indent();
149     ss_ << "\"functions\": [" << std::endl;
150 
151     auto iter = prog_->function_table.begin();
152 
153     for (; iter != prog_->function_table.end(); ++iter) {
154         indent_++;
155         Indent();
156         ss_ << "{";
157         WriteProperty("name", iter->first);
158         ss_ << std::endl;
159 
160         indent_++;
161         Indent();
162         ss_ << "\"signature\": {";
163         WriteProperty("retType", iter->second.return_type.GetName());
164         indent_++;
165         WrapArray("params", iter->second.params, false);
166         indent_ -= 2U;
167         ss_ << std::endl;
168         Indent();
169         ss_ << "},";
170 
171         WrapArray("ins", iter->second.ins);
172         WrapArray("variables", iter->second.local_variable_debug);
173         WriteProperty("sourceFile", iter->second.source_file);
174         WriteProperty("sourceCode", iter->second.source_code);
175         // icSize - parameterLength - funcName
176         WriteMetaData(iter->second.metadata->GetAnnotations());
177 
178         indent_--;
179         Indent();
180         ss_ << "}";
181 
182         if (std::next(iter) != prog_->function_table.end()) {
183             ss_ << ",";
184         }
185 
186         ss_ << std::endl;
187     }
188 
189     indent_--;
190     Indent();
191     ss_ << "]" << std::endl;
192     ss_ << "}";
193     ss_ << std::endl;
194     std::cout << ss_.str();
195 }
196 
WriteProperty(const char * key,const Value & value,bool comma)197 void DebugInfoDumper::WriteProperty(const char *key, const Value &value, bool comma)
198 {
199     ss_ << std::endl;
200     indent_++;
201     Indent();
202     ss_ << "\"" << key << "\": ";
203     if (std::holds_alternative<std::string>(value)) {
204         ss_ << "\"" << std::get<std::string>(value) << "\"";
205     } else if (std::holds_alternative<size_t>(value)) {
206         ss_ << std::to_string(std::get<size_t>(value));
207     } else if (std::holds_alternative<int32_t>(value)) {
208         ss_ << std::to_string(std::get<int32_t>(value));
209     }
210 
211     comma ? ss_ << "," : ss_ << std::endl;
212     indent_--;
213 }
214 
Indent()215 void DebugInfoDumper::Indent()
216 {
217     for (int32_t i = 0; i <= indent_; i++) {
218         ss_ << "  ";
219     }
220 }
221 
222 }  // namespace panda::es2panda::debuginfo
223