Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(760)

Unified Diff: runtime/vm/dwarf.cc

Issue 2723213002: DWARF and unwind support for AOT assembly output. (Closed)
Patch Set: . Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/vm/dwarf.cc
diff --git a/runtime/vm/dwarf.cc b/runtime/vm/dwarf.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2e6d2a274be772c66c623d89754de652f0a7ebb5
--- /dev/null
+++ b/runtime/vm/dwarf.cc
@@ -0,0 +1,647 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#include "vm/dwarf.h"
+
+#include "vm/code_descriptors.h"
+
+namespace dart {
+
+#ifdef DART_PRECOMPILER
+
+class InliningNode : public ZoneAllocated {
+ public:
+ InliningNode(const Function& function,
+ TokenPosition call_pos,
+ int32_t start_pc_offset)
+ : function(function),
+ call_pos(call_pos),
+ start_pc_offset(start_pc_offset),
+ end_pc_offset(-1),
+ children_head(NULL),
+ children_tail(NULL),
+ children_next(NULL) {}
+
+ void AppendChild(InliningNode* child) {
+ if (children_tail == NULL) {
+ children_head = children_tail = child;
+ } else {
+ children_tail->children_next = child;
+ children_tail = child;
+ }
+ }
+
+ const Function& function;
+ TokenPosition call_pos;
+ int32_t start_pc_offset;
+ int32_t end_pc_offset;
+ InliningNode* children_head;
+ InliningNode* children_tail;
+ InliningNode* children_next;
+};
+
+
+Dwarf::Dwarf(Zone* zone, WriteStream* stream)
+ : zone_(zone),
+ stream_(stream),
+ codes_(zone, 1024),
+ code_to_index_(zone),
+ functions_(zone, 1024),
+ function_to_index_(zone),
+ scripts_(zone, 1024),
+ script_to_index_(zone),
+ temp_(0) {}
+
+
+intptr_t Dwarf::AddCode(const Code& code) {
+ ASSERT(!code.IsNull());
+ CodeIndexPair* pair = code_to_index_.Lookup(&code);
+ if (pair != NULL) {
+ return pair->index_;
+ }
+ intptr_t index = codes_.length();
+ const Code& zone_code = Code::ZoneHandle(zone_, code.raw());
+ code_to_index_.Insert(CodeIndexPair(&zone_code, index));
+ codes_.Add(&zone_code);
+ if (code.IsFunctionCode()) {
+ const Function& function = Function::Handle(zone_, code.function());
+ AddFunction(function);
+ }
+ const Array& inline_functions =
+ Array::Handle(zone_, code.inlined_id_to_function());
+ if (!inline_functions.IsNull()) {
+ Function& function = Function::Handle(zone_);
+ for (intptr_t i = 0; i < inline_functions.Length(); i++) {
+ function ^= inline_functions.At(i);
+ AddFunction(function);
+ }
+ }
+ return index;
+}
+
+
+intptr_t Dwarf::AddFunction(const Function& function) {
+ ASSERT(!function.IsNull());
+ FunctionIndexPair* pair = function_to_index_.Lookup(&function);
+ if (pair != NULL) {
+ return pair->index_;
+ }
+ intptr_t index = functions_.length();
+ const Function& zone_func = Function::ZoneHandle(zone_, function.raw());
+ function_to_index_.Insert(FunctionIndexPair(&zone_func, index));
+ functions_.Add(&zone_func);
+ const Script& script = Script::Handle(zone_, function.script());
+ AddScript(script);
+ return index;
+}
+
+
+intptr_t Dwarf::AddScript(const Script& script) {
+ ASSERT(!script.IsNull());
+ ScriptIndexPair* pair = script_to_index_.Lookup(&script);
+ if (pair != NULL) {
+ return pair->index_;
+ }
+ // DWARF file numbers start from 1.
+ intptr_t index = scripts_.length() + 1;
+ const Script& zone_script = Script::ZoneHandle(zone_, script.raw());
+ script_to_index_.Insert(ScriptIndexPair(&zone_script, index));
+ scripts_.Add(&zone_script);
+ return index;
+}
+
+
+intptr_t Dwarf::LookupFunction(const Function& function) {
+ ASSERT(!function.IsNull());
+ FunctionIndexPair* pair = function_to_index_.Lookup(&function);
+ if (pair == NULL) {
+ FATAL1("Function detected too late during DWARF generation: %s",
+ function.ToCString());
+ }
+ return pair->index_;
+}
+
+
+intptr_t Dwarf::LookupScript(const Script& script) {
+ ASSERT(!script.IsNull());
+ ScriptIndexPair* pair = script_to_index_.Lookup(&script);
+ if (pair == NULL) {
+ FATAL1("Script detected too late during DWARF generation: %s",
+ script.ToCString());
+ }
+ return pair->index_;
+}
+
+
+void Dwarf::Print(const char* format, ...) {
+ va_list args;
+ va_start(args, format);
+ stream_->VPrint(format, args);
+ va_end(args);
+}
+
+
+void Dwarf::WriteAbbreviations() {
+#if defined(TARGET_OS_MACOS)
+ Print(".section __DWARF,__debug_abbrev,regular,debug\n");
+#else
+ Print(".section .debug_abbrev,\"\"\n");
+#endif
+
+ 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.
+ 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.
+ Print(".byte %" Pd "\n", DW_CHILDREN_yes);
+ Print(".uleb128 %" Pd "\n", DW_AT_name);
+ Print(".uleb128 %" Pd "\n", DW_FORM_string);
+ Print(".uleb128 %" Pd "\n", DW_AT_producer);
+ Print(".uleb128 %" Pd "\n", DW_FORM_string);
+ Print(".uleb128 %" Pd "\n", DW_AT_comp_dir);
+ Print(".uleb128 %" Pd "\n", DW_FORM_string);
+ Print(".uleb128 %" Pd "\n", DW_AT_low_pc);
+ Print(".uleb128 %" Pd "\n", DW_FORM_addr);
+ Print(".uleb128 %" Pd "\n", DW_AT_high_pc);
+ Print(".uleb128 %" Pd "\n", DW_FORM_addr);
+ Print(".uleb128 %" Pd "\n", DW_AT_stmt_list);
+ Print(".uleb128 %" Pd "\n", DW_FORM_sec_offset);
+ Print(".uleb128 %" Pd "\n", 0);
+ Print(".uleb128 %" Pd "\n", 0); // End of attributes.
+
+ Print(".uleb128 %" Pd "\n", kAbstractFunction);
+ Print(".uleb128 %" Pd "\n", DW_TAG_subprogram);
+ Print(".byte %" Pd "\n", DW_CHILDREN_yes);
+ Print(".uleb128 %" Pd "\n", DW_AT_name);
+ Print(".uleb128 %" Pd "\n", DW_FORM_string);
+ Print(".uleb128 %" Pd "\n", DW_AT_decl_file);
+ Print(".uleb128 %" Pd "\n", DW_FORM_udata);
+ Print(".uleb128 %" Pd "\n", DW_AT_decl_line);
+ Print(".uleb128 %" Pd "\n", DW_FORM_udata);
+ Print(".uleb128 %" Pd "\n", DW_AT_inline);
+ Print(".uleb128 %" Pd "\n", DW_FORM_udata);
+ Print(".uleb128 %" Pd "\n", 0);
+ Print(".uleb128 %" Pd "\n", 0); // End of attributes.
+
+ Print(".uleb128 %" Pd "\n", kConcreteFunction);
+ Print(".uleb128 %" Pd "\n", DW_TAG_subprogram);
+ Print(".byte %" Pd "\n", DW_CHILDREN_yes);
+ Print(".uleb128 %" Pd "\n", DW_AT_abstract_origin);
+ Print(".uleb128 %" Pd "\n", DW_FORM_ref4);
+ Print(".uleb128 %" Pd "\n", DW_AT_low_pc);
+ Print(".uleb128 %" Pd "\n", DW_FORM_addr);
+ Print(".uleb128 %" Pd "\n", DW_AT_high_pc);
+ Print(".uleb128 %" Pd "\n", DW_FORM_addr);
+ Print(".uleb128 %" Pd "\n", 0);
+ Print(".uleb128 %" Pd "\n", 0); // End of attributes.
+
+ Print(".uleb128 %" Pd "\n", kInlinedFunction);
+ Print(".uleb128 %" Pd "\n", DW_TAG_inlined_subroutine);
+ Print(".byte %" Pd "\n", DW_CHILDREN_yes);
+ Print(".uleb128 %" Pd "\n", DW_AT_abstract_origin);
+ Print(".uleb128 %" Pd "\n", DW_FORM_ref4);
+ Print(".uleb128 %" Pd "\n", DW_AT_low_pc);
+ Print(".uleb128 %" Pd "\n", DW_FORM_addr);
+ Print(".uleb128 %" Pd "\n", DW_AT_high_pc);
+ Print(".uleb128 %" Pd "\n", DW_FORM_addr);
+ Print(".uleb128 %" Pd "\n", DW_AT_call_file);
+ Print(".uleb128 %" Pd "\n", DW_FORM_udata);
+ Print(".uleb128 %" Pd "\n", DW_AT_call_line);
+ Print(".uleb128 %" Pd "\n", DW_FORM_udata);
+ Print(".uleb128 %" Pd "\n", 0);
+ Print(".uleb128 %" Pd "\n", 0); // End of attributes.
+
+ Print(".uleb128 %" Pd "\n", 0); // End of abbreviations.
+}
+
+
+void Dwarf::WriteCompilationUnit() {
+// 7.5.1.1 Compilation Unit Header
+
+#if defined(TARGET_OS_MACOS)
+ Print(".section __DWARF,__debug_info,regular,debug\n");
+#else
+ Print(".section .debug_info,\"\"\n");
+#endif
+ Print(".Ldebug_info:\n");
+
+ // Unit length. Assignment to temp works around buggy Mac assembler.
+ Print("Lcu_size = .Lcu_end - .Lcu_start\n");
+ Print(".4byte Lcu_size\n");
+ Print(".Lcu_start:\n");
+
+ Print(".2byte %" Pd "\n", 2); // DWARF version 2
+ Print(".4byte %" Pd "\n", 0); // debug_abbrev_offset
+ Print(".byte %" Pd "\n", sizeof(void*)); // address_size
+
+ // 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.
+ Print(".uleb128 %" Pd "\n", kCompilationUnit);
+ 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.
+ Print(".string \"Dart VM\"\n"); // DW_AT_producer
+ Print(".string \"\"\n"); // DW_AT_comp_dir
+
+// 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.
+#if defined(ARCH_IS_32_BIT)
+ Print(".4byte _kDartIsolateSnapshotInstructions\n");
+#else
+ Print(".8byte _kDartIsolateSnapshotInstructions\n");
+#endif
+
+ // DW_AT_high_pc
+ intptr_t last_code_index = codes_.length() - 1;
+ const Code& last_code = *(codes_[last_code_index]);
+#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.
+ 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
+#else
+ Print(".8byte .Lcode%" Pd " + %" Pd "\n", last_code_index, last_code.Size());
+#endif
+
+ // DW_AT_stmt_list (offset into .debug_line)
+ Print(".4byte 0\n");
+
+ WriteAbstractFunctions();
+ WriteConcreteFunctions();
+
+ Print(".uleb128 0\n"); // End of children.
+
+ Print(".uleb128 0\n"); // End of entries.
+ Print(".Lcu_end:\n");
+}
+
+
+void Dwarf::WriteAbstractFunctions() {
+ Script& script = Script::Handle(zone_);
+ String& name = String::Handle(zone_);
+ for (intptr_t i = 0; i < functions_.length(); i++) {
+ const Function& function = *(functions_[i]);
+ name = function.QualifiedUserVisibleName();
+ script = function.script();
+ intptr_t file = LookupScript(script);
+ intptr_t line = 0; // Not known. Script has already lost its token stream.
+
+ Print(".Lfunc%" Pd ":\n", i); // Label for DW_AT_abstract_origin references
+ Print(".uleb128 %" Pd "\n", kAbstractFunction);
+ Print(".string \"%s\"\n", name.ToCString()); // DW_AT_name
+ Print(".uleb128 %" Pd "\n", file); // DW_AT_decl_file
+ Print(".uleb128 %" Pd "\n", line); // DW_AT_decl_line
+ Print(".uleb128 %" Pd "\n", DW_INL_inlined); // DW_AT_inline
+ Print(".uleb128 %" Pd "\n", 0); // End of children.
+ }
+}
+
+
+void Dwarf::WriteConcreteFunctions() {
+ Function& function = Function::Handle(zone_);
+ Script& script = Script::Handle(zone_);
+ for (intptr_t i = 0; i < codes_.length(); i++) {
+ const Code& code = *(codes_[i]);
+ if (!code.IsFunctionCode()) {
+ continue;
+ }
+ function = code.function();
+ intptr_t function_index = LookupFunction(function);
+ script = function.script();
+
+ Print(".uleb128 %" Pd "\n", kConcreteFunction);
+ // DW_AT_abstract_origin
+ // Assignment to temp works around buggy Mac assembler.
+ intptr_t temp = temp_++;
+ Print("Ltemp%" Pd " = .Lfunc%" Pd " - .Ldebug_info\n", temp,
+ function_index);
+ Print(".4byte Ltemp%" Pd "\n", temp);
+
+// DW_AT_low_pc
+#if defined(ARCH_IS_32_BIT)
+ Print(".4byte .Lcode%" Pd "\n", i);
+#else
+ Print(".8byte .Lcode%" Pd "\n", i);
+#endif
+// DW_AT_high_pc
+#if defined(ARCH_IS_32_BIT)
+ Print(".4byte .Lcode%" Pd " + %" Pd "\n", i, code.Size());
+#else
+ Print(".8byte .Lcode%" Pd " + %" Pd "\n", i, code.Size());
+#endif
+
+ InliningNode* node = ExpandInliningTree(code);
+ if (node != NULL) {
+ for (InliningNode* child = node->children_head; child != NULL;
+ child = child->children_next) {
+ WriteInliningNode(child, i, script);
+ }
+ }
+
+ Print(".uleb128 %" Pd "\n", 0); // End of children.
+ }
+}
+
+
+// Our state machine encodes position metadata such that we don't know the
+// end pc for an inlined function until it is popped, but DWARF DIEs encode
+// it where the function is pushed. We expand the state transitions into
+// an in-memory tree to do the conversion.
+InliningNode* Dwarf::ExpandInliningTree(const Code& code) {
+ const CodeSourceMap& map =
+ CodeSourceMap::Handle(zone_, code.code_source_map());
+ if (map.IsNull()) {
+ return NULL;
+ }
+ const Array& functions = Array::Handle(zone_, code.inlined_id_to_function());
+ const Function& root_function = Function::Handle(zone_, code.function());
+
+ GrowableArray<InliningNode*> node_stack(zone_, 4);
+ GrowableArray<TokenPosition> token_positions(zone_, 4);
+
+ NoSafepointScope no_safepoint;
+ ReadStream stream(map.Data(), map.Length());
+
+ int32_t current_pc_offset = 0;
+ InliningNode* root_node =
+ new (zone_) InliningNode(root_function, TokenPosition(), 0);
+ root_node->end_pc_offset = code.Size();
+ node_stack.Add(root_node);
+ token_positions.Add(CodeSourceMapBuilder::kInitialPosition);
+
+ while (stream.PendingBytes() > 0) {
+ uint8_t opcode = stream.Read<uint8_t>();
+ switch (opcode) {
+ case CodeSourceMapBuilder::kChangePosition: {
+ int32_t position = stream.Read<int32_t>();
+ token_positions[token_positions.length() - 1] = TokenPosition(position);
+ break;
+ }
+ case CodeSourceMapBuilder::kAdvancePC: {
+ int32_t delta = stream.Read<int32_t>();
+ current_pc_offset += delta;
+ break;
+ }
+ case CodeSourceMapBuilder::kPushFunction: {
+ 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.
+ const Function& child_func =
+ Function::Handle(zone_, Function::RawCast(functions.At(func)));
+ TokenPosition call_pos = token_positions.Last();
+ InliningNode* child_node =
+ new (zone_) InliningNode(child_func, call_pos, current_pc_offset);
+ node_stack.Last()->AppendChild(child_node);
+ node_stack.Add(child_node);
+ token_positions.Add(CodeSourceMapBuilder::kInitialPosition);
+ break;
+ }
+ case CodeSourceMapBuilder::kPopFunction: {
+ // We never pop the root function.
+ ASSERT(node_stack.length() > 1);
+ ASSERT(token_positions.length() > 1);
+ node_stack.Last()->end_pc_offset = current_pc_offset;
+ node_stack.RemoveLast();
+ token_positions.RemoveLast();
+ break;
+ }
+ default:
+ UNREACHABLE();
+ }
+ }
+
+ while (node_stack.length() > 1) {
+ node_stack.Last()->end_pc_offset = current_pc_offset;
+ node_stack.RemoveLast();
+ token_positions.RemoveLast();
+ }
+ ASSERT(node_stack[0] == root_node);
+ return root_node;
+}
+
+
+void Dwarf::WriteInliningNode(InliningNode* node,
+ intptr_t root_code_index,
+ const Script& parent_script) {
+ intptr_t file = LookupScript(parent_script);
+ intptr_t line = node->call_pos.value();
+ intptr_t function_index = LookupFunction(node->function);
+ const Script& script = Script::Handle(zone_, node->function.script());
+
+ Print(".uleb128 %" Pd "\n", kInlinedFunction);
+ // DW_AT_abstract_origin
+ // Assignment to temp works around buggy Mac assembler.
+ intptr_t temp = temp_++;
+ Print("Ltemp%" Pd " = .Lfunc%" Pd " - .Ldebug_info\n", temp, function_index);
+ Print(".4byte Ltemp%" Pd "\n", temp);
+// DW_AT_low_pc
+#if defined(ARCH_IS_32_BIT)
+ Print(".4byte .Lcode%" Pd " + %" Pd "\n", root_code_index,
+ node->start_pc_offset);
+#else
+ Print(".8byte .Lcode%" Pd " + %" Pd "\n", root_code_index,
+ node->start_pc_offset);
+#endif
+// DW_AT_high_pc
+#if defined(ARCH_IS_32_BIT)
+ Print(".4byte .Lcode%" Pd " + %" Pd "\n", root_code_index,
+ node->end_pc_offset);
+#else
+ Print(".8byte .Lcode%" Pd " + %" Pd "\n", root_code_index,
+ node->end_pc_offset);
+#endif
+ // DW_AT_call_file
+ Print(".uleb128 %" Pd "\n", file);
+ // DW_AT_call_line
+ Print(".uleb128 %" Pd "\n", line);
+
+ for (InliningNode* child = node->children_head; child != NULL;
+ child = child->children_next) {
+ WriteInliningNode(child, root_code_index, script);
+ }
+
+ Print(".uleb128 %" Pd "\n", 0); // End of children.
+}
+
+
+void Dwarf::WriteLines() {
+#if defined(TARGET_OS_MACOS)
+ Print(".section __DWARF,__debug_line,regular,debug\n");
+#else
+ Print(".section .debug_line,\"\"\n");
+#endif
+
+ // 6.2.4 The Line Number Program Header
+
+ // 1. unit_length. This encoding implies 32-bit DWARF.
+ Print("Lline_size = .Lline_end - .Lline_start\n");
+ Print(".4byte Lline_size\n");
+
+ Print(".Lline_start:\n");
+
+ Print(".2byte 2\n"); // 2. version
+
+ // 3. header_length
+ // Assignment to temp works around buggy Mac assembler.
+ Print("Llineheader_size = .Llineheader_end - .Llineheader_start\n");
+ Print(".4byte Llineheader_size\n");
+ Print(".Llineheader_start:\n");
+
+ Print(".byte 1\n"); // 4. minimum_instruction_length
+ Print(".byte 0\n"); // 5. default_is_stmt
+ Print(".byte 0\n"); // 6. line_base
+ Print(".byte 1\n"); // 7. line_range
+ Print(".byte 13\n"); // 8. opcode_base (12 standard opcodes in Dwarf 2)
+
+ // 9. standard_opcode_lengths
+ Print(".byte 0\n"); // DW_LNS_copy, 0 operands
+ Print(".byte 1\n"); // DW_LNS_advance_pc, 1 operands
+ Print(".byte 1\n"); // DW_LNS_advance_list, 1 operands
+ Print(".byte 1\n"); // DW_LNS_set_file, 1 operands
+ Print(".byte 1\n"); // DW_LNS_set_column, 1 operands
+ Print(".byte 0\n"); // DW_LNS_negate_stmt, 0 operands
+ Print(".byte 0\n"); // DW_LNS_set_basic_block, 0 operands
+ Print(".byte 0\n"); // DW_LNS_const_add_pc, 0 operands
+ Print(".byte 1\n"); // DW_LNS_fixed_advance_pc, 1 operands
+ Print(".byte 0\n"); // DW_LNS_set_prolog_end, 0 operands
+ Print(".byte 0\n"); // DW_LNS_set_epligoue_begin, 0 operands
+ Print(".byte 1\n"); // DW_LNS_set_isa, 1 operands
+
+ // 10. include_directories (sequence of path names)
+ Print(".byte 0\n");
+
+ // 11. file_names (sequence of file entries)
+ String& uri = String::Handle(zone_);
+ for (intptr_t i = 0; i < scripts_.length(); i++) {
+ const Script& script = *(scripts_[i]);
+ uri ^= script.url();
+ Print(".string \"%s\"\n", uri.ToCString());
+ Print(".uleb128 0\n"); // Include directory index.
+ Print(".uleb128 0\n"); // File modification time.
+ Print(".uleb128 0\n"); // File length.
+ }
+ Print(".byte 0\n"); // End of file names.
+
+ Print(".Llineheader_end:\n");
+
+ // 6.2.5 The Line Number Program
+
+ intptr_t previous_file = 1;
+ intptr_t previous_line = 1;
+ intptr_t previous_code_index = -1;
+ intptr_t previous_pc_offset = 0;
+
+ Function& root_function = Function::Handle(zone_);
+ Script& script = Script::Handle(zone_);
+ CodeSourceMap& map = CodeSourceMap::Handle(zone_);
+ Array& functions = Array::Handle(zone_);
+ GrowableArray<const Function*> function_stack(zone_, 8);
+ GrowableArray<TokenPosition> token_positions(zone_, 8);
+
+ for (intptr_t i = 0; i < codes_.length(); i++) {
+ const Code& code = *(codes_[i]);
+ map = code.code_source_map();
+ if (map.IsNull()) {
+ continue;
+ }
+ root_function = code.function();
+ functions = code.inlined_id_to_function();
+
+ NoSafepointScope no_safepoint;
+ ReadStream stream(map.Data(), map.Length());
+
+ function_stack.Clear();
+ token_positions.Clear();
+
+ int32_t current_pc_offset = 0;
+ function_stack.Add(&root_function);
+ token_positions.Add(CodeSourceMapBuilder::kInitialPosition);
+
+ while (stream.PendingBytes() > 0) {
+ uint8_t opcode = stream.Read<uint8_t>();
+ switch (opcode) {
+ case CodeSourceMapBuilder::kChangePosition: {
+ int32_t position = stream.Read<int32_t>();
+ token_positions[token_positions.length() - 1] =
+ TokenPosition(position);
+ break;
+ }
+ case CodeSourceMapBuilder::kAdvancePC: {
+ int32_t delta = stream.Read<int32_t>();
+ current_pc_offset += delta;
+
+ const Function& function = *(function_stack.Last());
+ script = function.script();
+ intptr_t file = LookupScript(script);
+
+ // 1. Update LNP file.
+ if (file != previous_file) {
+ Print(".byte %" Pd "\n", DW_LNS_set_file);
+ Print(".uleb128 %" Pd "\n", file);
+ previous_file = file;
+ }
+
+ // 2. Update LNP line.
+ TokenPosition pos = token_positions.Last();
+ intptr_t line = pos.value();
+ if (line != previous_line) {
+ Print(".byte %" Pd "\n", DW_LNS_advance_line);
+ Print(".sleb128 %" Pd "\n", line - previous_line);
+ previous_line = line;
+ }
+
+ // 3. Update LNP pc.
+ if (previous_code_index == -1) {
+ // 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.
+ Print(".byte 0\n"); // This is an extended opcode
+#if defined(ARCH_IS_32_BIT)
+ Print(".byte 5\n"); // that is 5 bytes long
+ Print(".byte %" Pd "\n", DW_LNE_set_address);
+ Print(".4byte .Lcode%" Pd " + %" Pd "\n", i, current_pc_offset);
+#else
+ Print(".byte 9\n"); // that is 9 bytes long
+ Print(".byte %" Pd "\n", DW_LNE_set_address);
+ Print(".8byte .Lcode%" Pd " + %" Pd "\n", i, current_pc_offset);
+#endif
+ } else {
+ Print(".byte %" Pd "\n", DW_LNS_advance_pc);
+ Print(".uleb128 .Lcode%" Pd " - .Lcode%" Pd " + %" Pd "\n", i,
+ previous_code_index, current_pc_offset - previous_pc_offset);
+ }
+ previous_code_index = i;
+ previous_pc_offset = current_pc_offset;
+
+ // 4. Emit LNP row.
+ Print(".byte %" Pd "\n", DW_LNS_copy);
+
+ break;
+ }
+ case CodeSourceMapBuilder::kPushFunction: {
+ int32_t func = stream.Read<int32_t>();
+ const Function& child_func =
+ Function::Handle(zone_, Function::RawCast(functions.At(func)));
+ function_stack.Add(&child_func);
+ token_positions.Add(CodeSourceMapBuilder::kInitialPosition);
+ break;
+ }
+ case CodeSourceMapBuilder::kPopFunction: {
+ // We never pop the root function.
+ ASSERT(function_stack.length() > 1);
+ ASSERT(token_positions.length() > 1);
+ function_stack.RemoveLast();
+ token_positions.RemoveLast();
+ break;
+ }
+ default:
+ UNREACHABLE();
+ }
+ }
+ }
+
+ // Advance pc to end of the compilation unit.
+ intptr_t last_code_index = codes_.length() - 1;
+ const Code& last_code = *(codes_[last_code_index]);
+ Print(".byte %" Pd "\n", DW_LNS_advance_pc);
+ Print(".uleb128 .Lcode%" Pd " - .Lcode%" Pd " + %" Pd "\n", last_code_index,
+ previous_code_index, last_code.Size() - previous_pc_offset);
+
+ // End of contiguous machine code.
+ Print(".byte 0\n"); // This is an extended opcode
+ Print(".byte 1\n"); // that is 1 byte long
+ Print(".byte %" Pd "\n", DW_LNE_end_sequence);
+
+ Print(".Lline_end:\n");
+}
+
+#endif // DART_PRECOMPILER
+
+} // namespace dart

Powered by Google App Engine
This is Rietveld 408576698