Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "vm/dwarf.h" | |
| 6 | |
| 7 #include "vm/code_descriptors.h" | |
| 8 | |
| 9 namespace dart { | |
| 10 | |
| 11 #ifdef DART_PRECOMPILER | |
| 12 | |
| 13 class InliningNode : public ZoneAllocated { | |
| 14 public: | |
| 15 InliningNode(const Function& function, | |
| 16 TokenPosition call_pos, | |
| 17 int32_t start_pc_offset) | |
| 18 : function(function), | |
| 19 call_pos(call_pos), | |
| 20 start_pc_offset(start_pc_offset), | |
| 21 end_pc_offset(-1), | |
| 22 children_head(NULL), | |
| 23 children_tail(NULL), | |
| 24 children_next(NULL) {} | |
| 25 | |
| 26 void AppendChild(InliningNode* child) { | |
| 27 if (children_tail == NULL) { | |
| 28 children_head = children_tail = child; | |
| 29 } else { | |
| 30 children_tail->children_next = child; | |
| 31 children_tail = child; | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 const Function& function; | |
| 36 TokenPosition call_pos; | |
| 37 int32_t start_pc_offset; | |
| 38 int32_t end_pc_offset; | |
| 39 InliningNode* children_head; | |
| 40 InliningNode* children_tail; | |
| 41 InliningNode* children_next; | |
| 42 }; | |
| 43 | |
| 44 | |
| 45 Dwarf::Dwarf(Zone* zone, WriteStream* stream) | |
| 46 : zone_(zone), | |
| 47 stream_(stream), | |
| 48 codes_(zone, 1024), | |
| 49 code_to_index_(zone), | |
| 50 functions_(zone, 1024), | |
| 51 function_to_index_(zone), | |
| 52 scripts_(zone, 1024), | |
| 53 script_to_index_(zone), | |
| 54 temp_(0) {} | |
| 55 | |
| 56 | |
| 57 intptr_t Dwarf::AddCode(const Code& code) { | |
| 58 ASSERT(!code.IsNull()); | |
| 59 CodeIndexPair* pair = code_to_index_.Lookup(&code); | |
| 60 if (pair != NULL) { | |
| 61 return pair->index_; | |
| 62 } | |
| 63 intptr_t index = codes_.length(); | |
| 64 const Code& zone_code = Code::ZoneHandle(zone_, code.raw()); | |
| 65 code_to_index_.Insert(CodeIndexPair(&zone_code, index)); | |
| 66 codes_.Add(&zone_code); | |
| 67 if (code.IsFunctionCode()) { | |
| 68 const Function& function = Function::Handle(zone_, code.function()); | |
| 69 AddFunction(function); | |
| 70 } | |
| 71 const Array& inline_functions = | |
| 72 Array::Handle(zone_, code.inlined_id_to_function()); | |
| 73 if (!inline_functions.IsNull()) { | |
| 74 Function& function = Function::Handle(zone_); | |
| 75 for (intptr_t i = 0; i < inline_functions.Length(); i++) { | |
| 76 function ^= inline_functions.At(i); | |
| 77 AddFunction(function); | |
| 78 } | |
| 79 } | |
| 80 return index; | |
| 81 } | |
| 82 | |
| 83 | |
| 84 intptr_t Dwarf::AddFunction(const Function& function) { | |
| 85 ASSERT(!function.IsNull()); | |
| 86 FunctionIndexPair* pair = function_to_index_.Lookup(&function); | |
| 87 if (pair != NULL) { | |
| 88 return pair->index_; | |
| 89 } | |
| 90 intptr_t index = functions_.length(); | |
| 91 const Function& zone_func = Function::ZoneHandle(zone_, function.raw()); | |
| 92 function_to_index_.Insert(FunctionIndexPair(&zone_func, index)); | |
| 93 functions_.Add(&zone_func); | |
| 94 const Script& script = Script::Handle(zone_, function.script()); | |
| 95 AddScript(script); | |
| 96 return index; | |
| 97 } | |
| 98 | |
| 99 | |
| 100 intptr_t Dwarf::AddScript(const Script& script) { | |
| 101 ASSERT(!script.IsNull()); | |
| 102 ScriptIndexPair* pair = script_to_index_.Lookup(&script); | |
| 103 if (pair != NULL) { | |
| 104 return pair->index_; | |
| 105 } | |
| 106 // DWARF file numbers start from 1. | |
| 107 intptr_t index = scripts_.length() + 1; | |
| 108 const Script& zone_script = Script::ZoneHandle(zone_, script.raw()); | |
| 109 script_to_index_.Insert(ScriptIndexPair(&zone_script, index)); | |
| 110 scripts_.Add(&zone_script); | |
| 111 return index; | |
| 112 } | |
| 113 | |
| 114 | |
| 115 intptr_t Dwarf::LookupFunction(const Function& function) { | |
| 116 ASSERT(!function.IsNull()); | |
| 117 FunctionIndexPair* pair = function_to_index_.Lookup(&function); | |
| 118 if (pair == NULL) { | |
| 119 FATAL1("Function detected too late during DWARF generation: %s", | |
| 120 function.ToCString()); | |
| 121 } | |
| 122 return pair->index_; | |
| 123 } | |
| 124 | |
| 125 | |
| 126 intptr_t Dwarf::LookupScript(const Script& script) { | |
| 127 ASSERT(!script.IsNull()); | |
| 128 ScriptIndexPair* pair = script_to_index_.Lookup(&script); | |
| 129 if (pair == NULL) { | |
| 130 FATAL1("Script detected too late during DWARF generation: %s", | |
| 131 script.ToCString()); | |
| 132 } | |
| 133 return pair->index_; | |
| 134 } | |
| 135 | |
| 136 | |
| 137 void Dwarf::Print(const char* format, ...) { | |
| 138 va_list args; | |
| 139 va_start(args, format); | |
| 140 stream_->VPrint(format, args); | |
| 141 va_end(args); | |
| 142 } | |
| 143 | |
| 144 | |
| 145 void Dwarf::WriteAbbreviations() { | |
| 146 #if defined(TARGET_OS_MACOS) | |
| 147 Print(".section __DWARF,__debug_abbrev,regular,debug\n"); | |
| 148 #else | |
| 149 Print(".section .debug_abbrev,\"\"\n"); | |
| 150 #endif | |
| 151 | |
| 152 Print(".uleb128 %" Pd "\n", kCompilationUnit); | |
|
Cutch
2017/03/15 14:54:13
Might be a good idea to document what this all mea
rmacnak
2017/03/16 17:50:11
Done.
| |
| 153 Print(".uleb128 %" Pd "\n", DW_TAG_compile_unit); | |
|
Vyacheslav Egorov (Google)
2017/03/15 21:36:43
Maybe you can make helpers like:
u1(intptr_t x)
u
rmacnak
2017/03/16 17:50:11
Done.
| |
| 154 Print(".byte %" Pd "\n", DW_CHILDREN_yes); | |
| 155 Print(".uleb128 %" Pd "\n", DW_AT_name); | |
| 156 Print(".uleb128 %" Pd "\n", DW_FORM_string); | |
| 157 Print(".uleb128 %" Pd "\n", DW_AT_producer); | |
| 158 Print(".uleb128 %" Pd "\n", DW_FORM_string); | |
| 159 Print(".uleb128 %" Pd "\n", DW_AT_comp_dir); | |
| 160 Print(".uleb128 %" Pd "\n", DW_FORM_string); | |
| 161 Print(".uleb128 %" Pd "\n", DW_AT_low_pc); | |
| 162 Print(".uleb128 %" Pd "\n", DW_FORM_addr); | |
| 163 Print(".uleb128 %" Pd "\n", DW_AT_high_pc); | |
| 164 Print(".uleb128 %" Pd "\n", DW_FORM_addr); | |
| 165 Print(".uleb128 %" Pd "\n", DW_AT_stmt_list); | |
| 166 Print(".uleb128 %" Pd "\n", DW_FORM_sec_offset); | |
| 167 Print(".uleb128 %" Pd "\n", 0); | |
| 168 Print(".uleb128 %" Pd "\n", 0); // End of attributes. | |
| 169 | |
| 170 Print(".uleb128 %" Pd "\n", kAbstractFunction); | |
| 171 Print(".uleb128 %" Pd "\n", DW_TAG_subprogram); | |
| 172 Print(".byte %" Pd "\n", DW_CHILDREN_yes); | |
| 173 Print(".uleb128 %" Pd "\n", DW_AT_name); | |
| 174 Print(".uleb128 %" Pd "\n", DW_FORM_string); | |
| 175 Print(".uleb128 %" Pd "\n", DW_AT_decl_file); | |
| 176 Print(".uleb128 %" Pd "\n", DW_FORM_udata); | |
| 177 Print(".uleb128 %" Pd "\n", DW_AT_decl_line); | |
| 178 Print(".uleb128 %" Pd "\n", DW_FORM_udata); | |
| 179 Print(".uleb128 %" Pd "\n", DW_AT_inline); | |
| 180 Print(".uleb128 %" Pd "\n", DW_FORM_udata); | |
| 181 Print(".uleb128 %" Pd "\n", 0); | |
| 182 Print(".uleb128 %" Pd "\n", 0); // End of attributes. | |
| 183 | |
| 184 Print(".uleb128 %" Pd "\n", kConcreteFunction); | |
| 185 Print(".uleb128 %" Pd "\n", DW_TAG_subprogram); | |
| 186 Print(".byte %" Pd "\n", DW_CHILDREN_yes); | |
| 187 Print(".uleb128 %" Pd "\n", DW_AT_abstract_origin); | |
| 188 Print(".uleb128 %" Pd "\n", DW_FORM_ref4); | |
| 189 Print(".uleb128 %" Pd "\n", DW_AT_low_pc); | |
| 190 Print(".uleb128 %" Pd "\n", DW_FORM_addr); | |
| 191 Print(".uleb128 %" Pd "\n", DW_AT_high_pc); | |
| 192 Print(".uleb128 %" Pd "\n", DW_FORM_addr); | |
| 193 Print(".uleb128 %" Pd "\n", 0); | |
| 194 Print(".uleb128 %" Pd "\n", 0); // End of attributes. | |
| 195 | |
| 196 Print(".uleb128 %" Pd "\n", kInlinedFunction); | |
| 197 Print(".uleb128 %" Pd "\n", DW_TAG_inlined_subroutine); | |
| 198 Print(".byte %" Pd "\n", DW_CHILDREN_yes); | |
| 199 Print(".uleb128 %" Pd "\n", DW_AT_abstract_origin); | |
| 200 Print(".uleb128 %" Pd "\n", DW_FORM_ref4); | |
| 201 Print(".uleb128 %" Pd "\n", DW_AT_low_pc); | |
| 202 Print(".uleb128 %" Pd "\n", DW_FORM_addr); | |
| 203 Print(".uleb128 %" Pd "\n", DW_AT_high_pc); | |
| 204 Print(".uleb128 %" Pd "\n", DW_FORM_addr); | |
| 205 Print(".uleb128 %" Pd "\n", DW_AT_call_file); | |
| 206 Print(".uleb128 %" Pd "\n", DW_FORM_udata); | |
| 207 Print(".uleb128 %" Pd "\n", DW_AT_call_line); | |
| 208 Print(".uleb128 %" Pd "\n", DW_FORM_udata); | |
| 209 Print(".uleb128 %" Pd "\n", 0); | |
| 210 Print(".uleb128 %" Pd "\n", 0); // End of attributes. | |
| 211 | |
| 212 Print(".uleb128 %" Pd "\n", 0); // End of abbreviations. | |
| 213 } | |
| 214 | |
| 215 | |
| 216 void Dwarf::WriteCompilationUnit() { | |
| 217 // 7.5.1.1 Compilation Unit Header | |
| 218 | |
| 219 #if defined(TARGET_OS_MACOS) | |
| 220 Print(".section __DWARF,__debug_info,regular,debug\n"); | |
| 221 #else | |
| 222 Print(".section .debug_info,\"\"\n"); | |
| 223 #endif | |
| 224 Print(".Ldebug_info:\n"); | |
| 225 | |
| 226 // Unit length. Assignment to temp works around buggy Mac assembler. | |
| 227 Print("Lcu_size = .Lcu_end - .Lcu_start\n"); | |
| 228 Print(".4byte Lcu_size\n"); | |
| 229 Print(".Lcu_start:\n"); | |
| 230 | |
| 231 Print(".2byte %" Pd "\n", 2); // DWARF version 2 | |
| 232 Print(".4byte %" Pd "\n", 0); // debug_abbrev_offset | |
| 233 Print(".byte %" Pd "\n", sizeof(void*)); // address_size | |
| 234 | |
| 235 // Compilation Unit DIE | |
|
Cutch
2017/03/15 14:54:13
Maybe add a comment that we only have a single com
rmacnak
2017/03/16 17:50:11
Done.
| |
| 236 Print(".uleb128 %" Pd "\n", kCompilationUnit); | |
| 237 Print(".string \"hello.dart\"\n"); // DW_AT_name | |
|
Cutch
2017/03/15 14:54:13
shouldn't this be the script name?
rmacnak
2017/03/16 17:50:11
Yes, done.
| |
| 238 Print(".string \"Dart VM\"\n"); // DW_AT_producer | |
| 239 Print(".string \"\"\n"); // DW_AT_comp_dir | |
| 240 | |
| 241 // DW_AT_low_pc | |
|
Cutch
2017/03/15 14:54:13
This is encoding the lowest PC that this compilati
rmacnak
2017/03/16 17:50:11
Done.
| |
| 242 #if defined(ARCH_IS_32_BIT) | |
| 243 Print(".4byte _kDartIsolateSnapshotInstructions\n"); | |
| 244 #else | |
| 245 Print(".8byte _kDartIsolateSnapshotInstructions\n"); | |
| 246 #endif | |
| 247 | |
| 248 // DW_AT_high_pc | |
| 249 intptr_t last_code_index = codes_.length() - 1; | |
| 250 const Code& last_code = *(codes_[last_code_index]); | |
| 251 #if defined(ARCH_IS_32_BIT) | |
|
Cutch
2017/03/15 14:54:13
More comments:
This is encoding the highest PC th
rmacnak
2017/03/16 17:50:11
Done.
| |
| 252 Print(".4byte .Lcode%" Pd " + %" Pd "\n", last_code_index, last_code.Size()); | |
|
Vyacheslav Egorov (Google)
2017/03/15 21:36:43
Maybe define PTRSZ and do
Print( PTR_SZ " .Lcode
rmacnak
2017/03/16 17:50:11
Done as FORM_ADDR
| |
| 253 #else | |
| 254 Print(".8byte .Lcode%" Pd " + %" Pd "\n", last_code_index, last_code.Size()); | |
| 255 #endif | |
| 256 | |
| 257 // DW_AT_stmt_list (offset into .debug_line) | |
| 258 Print(".4byte 0\n"); | |
| 259 | |
| 260 WriteAbstractFunctions(); | |
| 261 WriteConcreteFunctions(); | |
| 262 | |
| 263 Print(".uleb128 0\n"); // End of children. | |
| 264 | |
| 265 Print(".uleb128 0\n"); // End of entries. | |
| 266 Print(".Lcu_end:\n"); | |
| 267 } | |
| 268 | |
| 269 | |
| 270 void Dwarf::WriteAbstractFunctions() { | |
| 271 Script& script = Script::Handle(zone_); | |
| 272 String& name = String::Handle(zone_); | |
| 273 for (intptr_t i = 0; i < functions_.length(); i++) { | |
| 274 const Function& function = *(functions_[i]); | |
| 275 name = function.QualifiedUserVisibleName(); | |
| 276 script = function.script(); | |
| 277 intptr_t file = LookupScript(script); | |
| 278 intptr_t line = 0; // Not known. Script has already lost its token stream. | |
| 279 | |
| 280 Print(".Lfunc%" Pd ":\n", i); // Label for DW_AT_abstract_origin references | |
| 281 Print(".uleb128 %" Pd "\n", kAbstractFunction); | |
| 282 Print(".string \"%s\"\n", name.ToCString()); // DW_AT_name | |
| 283 Print(".uleb128 %" Pd "\n", file); // DW_AT_decl_file | |
| 284 Print(".uleb128 %" Pd "\n", line); // DW_AT_decl_line | |
| 285 Print(".uleb128 %" Pd "\n", DW_INL_inlined); // DW_AT_inline | |
| 286 Print(".uleb128 %" Pd "\n", 0); // End of children. | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 | |
| 291 void Dwarf::WriteConcreteFunctions() { | |
| 292 Function& function = Function::Handle(zone_); | |
| 293 Script& script = Script::Handle(zone_); | |
| 294 for (intptr_t i = 0; i < codes_.length(); i++) { | |
| 295 const Code& code = *(codes_[i]); | |
| 296 if (!code.IsFunctionCode()) { | |
| 297 continue; | |
| 298 } | |
| 299 function = code.function(); | |
| 300 intptr_t function_index = LookupFunction(function); | |
| 301 script = function.script(); | |
| 302 | |
| 303 Print(".uleb128 %" Pd "\n", kConcreteFunction); | |
| 304 // DW_AT_abstract_origin | |
| 305 // Assignment to temp works around buggy Mac assembler. | |
| 306 intptr_t temp = temp_++; | |
| 307 Print("Ltemp%" Pd " = .Lfunc%" Pd " - .Ldebug_info\n", temp, | |
| 308 function_index); | |
| 309 Print(".4byte Ltemp%" Pd "\n", temp); | |
| 310 | |
| 311 // DW_AT_low_pc | |
| 312 #if defined(ARCH_IS_32_BIT) | |
| 313 Print(".4byte .Lcode%" Pd "\n", i); | |
| 314 #else | |
| 315 Print(".8byte .Lcode%" Pd "\n", i); | |
| 316 #endif | |
| 317 // DW_AT_high_pc | |
| 318 #if defined(ARCH_IS_32_BIT) | |
| 319 Print(".4byte .Lcode%" Pd " + %" Pd "\n", i, code.Size()); | |
| 320 #else | |
| 321 Print(".8byte .Lcode%" Pd " + %" Pd "\n", i, code.Size()); | |
| 322 #endif | |
| 323 | |
| 324 InliningNode* node = ExpandInliningTree(code); | |
| 325 if (node != NULL) { | |
| 326 for (InliningNode* child = node->children_head; child != NULL; | |
| 327 child = child->children_next) { | |
| 328 WriteInliningNode(child, i, script); | |
| 329 } | |
| 330 } | |
| 331 | |
| 332 Print(".uleb128 %" Pd "\n", 0); // End of children. | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 | |
| 337 // Our state machine encodes position metadata such that we don't know the | |
| 338 // end pc for an inlined function until it is popped, but DWARF DIEs encode | |
| 339 // it where the function is pushed. We expand the state transitions into | |
| 340 // an in-memory tree to do the conversion. | |
| 341 InliningNode* Dwarf::ExpandInliningTree(const Code& code) { | |
| 342 const CodeSourceMap& map = | |
| 343 CodeSourceMap::Handle(zone_, code.code_source_map()); | |
| 344 if (map.IsNull()) { | |
| 345 return NULL; | |
| 346 } | |
| 347 const Array& functions = Array::Handle(zone_, code.inlined_id_to_function()); | |
| 348 const Function& root_function = Function::Handle(zone_, code.function()); | |
| 349 | |
| 350 GrowableArray<InliningNode*> node_stack(zone_, 4); | |
| 351 GrowableArray<TokenPosition> token_positions(zone_, 4); | |
| 352 | |
| 353 NoSafepointScope no_safepoint; | |
| 354 ReadStream stream(map.Data(), map.Length()); | |
| 355 | |
| 356 int32_t current_pc_offset = 0; | |
| 357 InliningNode* root_node = | |
| 358 new (zone_) InliningNode(root_function, TokenPosition(), 0); | |
| 359 root_node->end_pc_offset = code.Size(); | |
| 360 node_stack.Add(root_node); | |
| 361 token_positions.Add(CodeSourceMapBuilder::kInitialPosition); | |
| 362 | |
| 363 while (stream.PendingBytes() > 0) { | |
| 364 uint8_t opcode = stream.Read<uint8_t>(); | |
| 365 switch (opcode) { | |
| 366 case CodeSourceMapBuilder::kChangePosition: { | |
| 367 int32_t position = stream.Read<int32_t>(); | |
| 368 token_positions[token_positions.length() - 1] = TokenPosition(position); | |
| 369 break; | |
| 370 } | |
| 371 case CodeSourceMapBuilder::kAdvancePC: { | |
| 372 int32_t delta = stream.Read<int32_t>(); | |
| 373 current_pc_offset += delta; | |
| 374 break; | |
| 375 } | |
| 376 case CodeSourceMapBuilder::kPushFunction: { | |
| 377 int32_t func = stream.Read<int32_t>(); | |
|
Florian Schneider
2017/03/15 18:15:06
s/func/func_index/
rmacnak
2017/03/16 17:50:11
Done.
| |
| 378 const Function& child_func = | |
| 379 Function::Handle(zone_, Function::RawCast(functions.At(func))); | |
| 380 TokenPosition call_pos = token_positions.Last(); | |
| 381 InliningNode* child_node = | |
| 382 new (zone_) InliningNode(child_func, call_pos, current_pc_offset); | |
| 383 node_stack.Last()->AppendChild(child_node); | |
| 384 node_stack.Add(child_node); | |
| 385 token_positions.Add(CodeSourceMapBuilder::kInitialPosition); | |
| 386 break; | |
| 387 } | |
| 388 case CodeSourceMapBuilder::kPopFunction: { | |
| 389 // We never pop the root function. | |
| 390 ASSERT(node_stack.length() > 1); | |
| 391 ASSERT(token_positions.length() > 1); | |
| 392 node_stack.Last()->end_pc_offset = current_pc_offset; | |
| 393 node_stack.RemoveLast(); | |
| 394 token_positions.RemoveLast(); | |
| 395 break; | |
| 396 } | |
| 397 default: | |
| 398 UNREACHABLE(); | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 while (node_stack.length() > 1) { | |
| 403 node_stack.Last()->end_pc_offset = current_pc_offset; | |
| 404 node_stack.RemoveLast(); | |
| 405 token_positions.RemoveLast(); | |
| 406 } | |
| 407 ASSERT(node_stack[0] == root_node); | |
| 408 return root_node; | |
| 409 } | |
| 410 | |
| 411 | |
| 412 void Dwarf::WriteInliningNode(InliningNode* node, | |
| 413 intptr_t root_code_index, | |
| 414 const Script& parent_script) { | |
| 415 intptr_t file = LookupScript(parent_script); | |
| 416 intptr_t line = node->call_pos.value(); | |
| 417 intptr_t function_index = LookupFunction(node->function); | |
| 418 const Script& script = Script::Handle(zone_, node->function.script()); | |
| 419 | |
| 420 Print(".uleb128 %" Pd "\n", kInlinedFunction); | |
| 421 // DW_AT_abstract_origin | |
| 422 // Assignment to temp works around buggy Mac assembler. | |
| 423 intptr_t temp = temp_++; | |
| 424 Print("Ltemp%" Pd " = .Lfunc%" Pd " - .Ldebug_info\n", temp, function_index); | |
| 425 Print(".4byte Ltemp%" Pd "\n", temp); | |
| 426 // DW_AT_low_pc | |
| 427 #if defined(ARCH_IS_32_BIT) | |
| 428 Print(".4byte .Lcode%" Pd " + %" Pd "\n", root_code_index, | |
| 429 node->start_pc_offset); | |
| 430 #else | |
| 431 Print(".8byte .Lcode%" Pd " + %" Pd "\n", root_code_index, | |
| 432 node->start_pc_offset); | |
| 433 #endif | |
| 434 // DW_AT_high_pc | |
| 435 #if defined(ARCH_IS_32_BIT) | |
| 436 Print(".4byte .Lcode%" Pd " + %" Pd "\n", root_code_index, | |
| 437 node->end_pc_offset); | |
| 438 #else | |
| 439 Print(".8byte .Lcode%" Pd " + %" Pd "\n", root_code_index, | |
| 440 node->end_pc_offset); | |
| 441 #endif | |
| 442 // DW_AT_call_file | |
| 443 Print(".uleb128 %" Pd "\n", file); | |
| 444 // DW_AT_call_line | |
| 445 Print(".uleb128 %" Pd "\n", line); | |
| 446 | |
| 447 for (InliningNode* child = node->children_head; child != NULL; | |
| 448 child = child->children_next) { | |
| 449 WriteInliningNode(child, root_code_index, script); | |
| 450 } | |
| 451 | |
| 452 Print(".uleb128 %" Pd "\n", 0); // End of children. | |
| 453 } | |
| 454 | |
| 455 | |
| 456 void Dwarf::WriteLines() { | |
| 457 #if defined(TARGET_OS_MACOS) | |
| 458 Print(".section __DWARF,__debug_line,regular,debug\n"); | |
| 459 #else | |
| 460 Print(".section .debug_line,\"\"\n"); | |
| 461 #endif | |
| 462 | |
| 463 // 6.2.4 The Line Number Program Header | |
| 464 | |
| 465 // 1. unit_length. This encoding implies 32-bit DWARF. | |
| 466 Print("Lline_size = .Lline_end - .Lline_start\n"); | |
| 467 Print(".4byte Lline_size\n"); | |
| 468 | |
| 469 Print(".Lline_start:\n"); | |
| 470 | |
| 471 Print(".2byte 2\n"); // 2. version | |
| 472 | |
| 473 // 3. header_length | |
| 474 // Assignment to temp works around buggy Mac assembler. | |
| 475 Print("Llineheader_size = .Llineheader_end - .Llineheader_start\n"); | |
| 476 Print(".4byte Llineheader_size\n"); | |
| 477 Print(".Llineheader_start:\n"); | |
| 478 | |
| 479 Print(".byte 1\n"); // 4. minimum_instruction_length | |
| 480 Print(".byte 0\n"); // 5. default_is_stmt | |
| 481 Print(".byte 0\n"); // 6. line_base | |
| 482 Print(".byte 1\n"); // 7. line_range | |
| 483 Print(".byte 13\n"); // 8. opcode_base (12 standard opcodes in Dwarf 2) | |
| 484 | |
| 485 // 9. standard_opcode_lengths | |
| 486 Print(".byte 0\n"); // DW_LNS_copy, 0 operands | |
| 487 Print(".byte 1\n"); // DW_LNS_advance_pc, 1 operands | |
| 488 Print(".byte 1\n"); // DW_LNS_advance_list, 1 operands | |
| 489 Print(".byte 1\n"); // DW_LNS_set_file, 1 operands | |
| 490 Print(".byte 1\n"); // DW_LNS_set_column, 1 operands | |
| 491 Print(".byte 0\n"); // DW_LNS_negate_stmt, 0 operands | |
| 492 Print(".byte 0\n"); // DW_LNS_set_basic_block, 0 operands | |
| 493 Print(".byte 0\n"); // DW_LNS_const_add_pc, 0 operands | |
| 494 Print(".byte 1\n"); // DW_LNS_fixed_advance_pc, 1 operands | |
| 495 Print(".byte 0\n"); // DW_LNS_set_prolog_end, 0 operands | |
| 496 Print(".byte 0\n"); // DW_LNS_set_epligoue_begin, 0 operands | |
| 497 Print(".byte 1\n"); // DW_LNS_set_isa, 1 operands | |
| 498 | |
| 499 // 10. include_directories (sequence of path names) | |
| 500 Print(".byte 0\n"); | |
| 501 | |
| 502 // 11. file_names (sequence of file entries) | |
| 503 String& uri = String::Handle(zone_); | |
| 504 for (intptr_t i = 0; i < scripts_.length(); i++) { | |
| 505 const Script& script = *(scripts_[i]); | |
| 506 uri ^= script.url(); | |
| 507 Print(".string \"%s\"\n", uri.ToCString()); | |
| 508 Print(".uleb128 0\n"); // Include directory index. | |
| 509 Print(".uleb128 0\n"); // File modification time. | |
| 510 Print(".uleb128 0\n"); // File length. | |
| 511 } | |
| 512 Print(".byte 0\n"); // End of file names. | |
| 513 | |
| 514 Print(".Llineheader_end:\n"); | |
| 515 | |
| 516 // 6.2.5 The Line Number Program | |
| 517 | |
| 518 intptr_t previous_file = 1; | |
| 519 intptr_t previous_line = 1; | |
| 520 intptr_t previous_code_index = -1; | |
| 521 intptr_t previous_pc_offset = 0; | |
| 522 | |
| 523 Function& root_function = Function::Handle(zone_); | |
| 524 Script& script = Script::Handle(zone_); | |
| 525 CodeSourceMap& map = CodeSourceMap::Handle(zone_); | |
| 526 Array& functions = Array::Handle(zone_); | |
| 527 GrowableArray<const Function*> function_stack(zone_, 8); | |
| 528 GrowableArray<TokenPosition> token_positions(zone_, 8); | |
| 529 | |
| 530 for (intptr_t i = 0; i < codes_.length(); i++) { | |
| 531 const Code& code = *(codes_[i]); | |
| 532 map = code.code_source_map(); | |
| 533 if (map.IsNull()) { | |
| 534 continue; | |
| 535 } | |
| 536 root_function = code.function(); | |
| 537 functions = code.inlined_id_to_function(); | |
| 538 | |
| 539 NoSafepointScope no_safepoint; | |
| 540 ReadStream stream(map.Data(), map.Length()); | |
| 541 | |
| 542 function_stack.Clear(); | |
| 543 token_positions.Clear(); | |
| 544 | |
| 545 int32_t current_pc_offset = 0; | |
| 546 function_stack.Add(&root_function); | |
| 547 token_positions.Add(CodeSourceMapBuilder::kInitialPosition); | |
| 548 | |
| 549 while (stream.PendingBytes() > 0) { | |
| 550 uint8_t opcode = stream.Read<uint8_t>(); | |
| 551 switch (opcode) { | |
| 552 case CodeSourceMapBuilder::kChangePosition: { | |
| 553 int32_t position = stream.Read<int32_t>(); | |
| 554 token_positions[token_positions.length() - 1] = | |
| 555 TokenPosition(position); | |
| 556 break; | |
| 557 } | |
| 558 case CodeSourceMapBuilder::kAdvancePC: { | |
| 559 int32_t delta = stream.Read<int32_t>(); | |
| 560 current_pc_offset += delta; | |
| 561 | |
| 562 const Function& function = *(function_stack.Last()); | |
| 563 script = function.script(); | |
| 564 intptr_t file = LookupScript(script); | |
| 565 | |
| 566 // 1. Update LNP file. | |
| 567 if (file != previous_file) { | |
| 568 Print(".byte %" Pd "\n", DW_LNS_set_file); | |
| 569 Print(".uleb128 %" Pd "\n", file); | |
| 570 previous_file = file; | |
| 571 } | |
| 572 | |
| 573 // 2. Update LNP line. | |
| 574 TokenPosition pos = token_positions.Last(); | |
| 575 intptr_t line = pos.value(); | |
| 576 if (line != previous_line) { | |
| 577 Print(".byte %" Pd "\n", DW_LNS_advance_line); | |
| 578 Print(".sleb128 %" Pd "\n", line - previous_line); | |
| 579 previous_line = line; | |
| 580 } | |
| 581 | |
| 582 // 3. Update LNP pc. | |
| 583 if (previous_code_index == -1) { | |
| 584 // This varient is relocatable. | |
|
Florian Schneider
2017/03/15 18:15:06
s/varient/variant/
Vyacheslav Egorov (Google)
2017/03/15 21:36:43
variant
rmacnak
2017/03/16 17:50:11
Done.
| |
| 585 Print(".byte 0\n"); // This is an extended opcode | |
| 586 #if defined(ARCH_IS_32_BIT) | |
| 587 Print(".byte 5\n"); // that is 5 bytes long | |
| 588 Print(".byte %" Pd "\n", DW_LNE_set_address); | |
| 589 Print(".4byte .Lcode%" Pd " + %" Pd "\n", i, current_pc_offset); | |
| 590 #else | |
| 591 Print(".byte 9\n"); // that is 9 bytes long | |
| 592 Print(".byte %" Pd "\n", DW_LNE_set_address); | |
| 593 Print(".8byte .Lcode%" Pd " + %" Pd "\n", i, current_pc_offset); | |
| 594 #endif | |
| 595 } else { | |
| 596 Print(".byte %" Pd "\n", DW_LNS_advance_pc); | |
| 597 Print(".uleb128 .Lcode%" Pd " - .Lcode%" Pd " + %" Pd "\n", i, | |
| 598 previous_code_index, current_pc_offset - previous_pc_offset); | |
| 599 } | |
| 600 previous_code_index = i; | |
| 601 previous_pc_offset = current_pc_offset; | |
| 602 | |
| 603 // 4. Emit LNP row. | |
| 604 Print(".byte %" Pd "\n", DW_LNS_copy); | |
| 605 | |
| 606 break; | |
| 607 } | |
| 608 case CodeSourceMapBuilder::kPushFunction: { | |
| 609 int32_t func = stream.Read<int32_t>(); | |
| 610 const Function& child_func = | |
| 611 Function::Handle(zone_, Function::RawCast(functions.At(func))); | |
| 612 function_stack.Add(&child_func); | |
| 613 token_positions.Add(CodeSourceMapBuilder::kInitialPosition); | |
| 614 break; | |
| 615 } | |
| 616 case CodeSourceMapBuilder::kPopFunction: { | |
| 617 // We never pop the root function. | |
| 618 ASSERT(function_stack.length() > 1); | |
| 619 ASSERT(token_positions.length() > 1); | |
| 620 function_stack.RemoveLast(); | |
| 621 token_positions.RemoveLast(); | |
| 622 break; | |
| 623 } | |
| 624 default: | |
| 625 UNREACHABLE(); | |
| 626 } | |
| 627 } | |
| 628 } | |
| 629 | |
| 630 // Advance pc to end of the compilation unit. | |
| 631 intptr_t last_code_index = codes_.length() - 1; | |
| 632 const Code& last_code = *(codes_[last_code_index]); | |
| 633 Print(".byte %" Pd "\n", DW_LNS_advance_pc); | |
| 634 Print(".uleb128 .Lcode%" Pd " - .Lcode%" Pd " + %" Pd "\n", last_code_index, | |
| 635 previous_code_index, last_code.Size() - previous_pc_offset); | |
| 636 | |
| 637 // End of contiguous machine code. | |
| 638 Print(".byte 0\n"); // This is an extended opcode | |
| 639 Print(".byte 1\n"); // that is 1 byte long | |
| 640 Print(".byte %" Pd "\n", DW_LNE_end_sequence); | |
| 641 | |
| 642 Print(".Lline_end:\n"); | |
| 643 } | |
| 644 | |
| 645 #endif // DART_PRECOMPILER | |
| 646 | |
| 647 } // namespace dart | |
| OLD | NEW |