Skip to content

Commit 6612e52

Browse files
committed
More tweaks
1 parent b1c27ac commit 6612e52

15 files changed

Lines changed: 270 additions & 69 deletions

‎runtime/vm/code_comments.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace dart {
1010

11-
const Code::Comments& CreateCommentsFrom(compiler::Assembler* assembler) {
11+
const CodeComments* CreateCommentsFrom(compiler::Assembler* assembler) {
1212
const auto& comments = assembler->comments();
1313
Code::Comments& result = Code::Comments::New(comments.length());
1414

@@ -17,7 +17,7 @@ const Code::Comments& CreateCommentsFrom(compiler::Assembler* assembler) {
1717
result.SetCommentAt(i, comments[i]->comment());
1818
}
1919

20-
return result;
20+
return new CodeCommentsWrapper(result);
2121
}
2222

2323
} // namespace dart

‎runtime/vm/code_comments.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace dart {
1616

17-
class CodeCommentsWrapper final : public CodeComments {
17+
class CodeCommentsWrapper final : public ZoneAllocated, public CodeComments {
1818
public:
1919
explicit CodeCommentsWrapper(const Code::Comments& comments)
2020
: comments_(comments), string_(String::Handle()) {}
@@ -30,12 +30,14 @@ class CodeCommentsWrapper final : public CodeComments {
3030
return string_.ToCString();
3131
}
3232

33+
const Code::Comments& Unwrap() const { return comments_; }
34+
3335
private:
3436
const Code::Comments& comments_;
3537
String& string_;
3638
};
3739

38-
const Code::Comments& CreateCommentsFrom(compiler::Assembler* assembler);
40+
const CodeComments* CreateCommentsFrom(compiler::Assembler* assembler);
3941

4042

4143
} // namespace dart

‎runtime/vm/code_observers.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace dart {
1616

1717
// An abstract representation of comments associated with the given code
1818
// object. We assume that comments are sorted by PCOffset.
19-
class CodeComments : public ValueObject {
19+
class CodeComments {
2020
public:
2121
CodeComments() = default;
2222
virtual ~CodeComments() = default;

‎runtime/vm/compiler/assembler/disassembler.cc‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "platform/text_buffer.h"
88
#include "platform/unaligned.h"
9+
#include "vm/code_comments.h"
910
#include "vm/code_patcher.h"
1011
#include "vm/dart_entry.h"
1112
#include "vm/deopt_instructions.h"
@@ -161,9 +162,9 @@ void Disassembler::Disassemble(uword start,
161162
uword end,
162163
DisassemblyFormatter* formatter,
163164
const Code& code,
164-
const Code::Comments* comments) {
165+
const CodeComments* comments) {
165166
if (comments == nullptr) {
166-
comments = code.IsNull() ? &Code::Comments::New(0) : &code.comments();
167+
comments = code.IsNull() ? new CodeCommentsWrapper(Code::Comments::New(0)) : code.comments();
167168
}
168169
ASSERT(formatter != NULL);
169170
char hex_buffer[kHexadecimalBufferSize]; // Instruction in hexadecimal form.
@@ -179,7 +180,7 @@ void Disassembler::Disassemble(uword start,
179180
comments->PCOffsetAt(comment_finger) <= offset) {
180181
formatter->Print(
181182
" ;; %s\n",
182-
String::Handle(comments->CommentAt(comment_finger)).ToCString());
183+
comments->CommentAt(comment_finger));
183184
comment_finger++;
184185
}
185186
if (old_comment_finger != comment_finger && !code.IsNull()) {

‎runtime/vm/compiler/assembler/disassembler.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
namespace dart {
1818

1919
// Forward declaration.
20-
class MemoryRegion;
20+
class CodeComments;
2121
class JSONArray;
22+
class MemoryRegion;
2223

2324
// Disassembly formatter interface, which consumes the
2425
// disassembled instructions in any desired form.
@@ -122,7 +123,7 @@ class Disassembler : public AllStatic {
122123
uword end,
123124
DisassemblyFormatter* formatter,
124125
const Code& code,
125-
const Code::Comments* comments = nullptr);
126+
const CodeComments* comments = nullptr);
126127

127128
static void Disassemble(uword start,
128129
uword end,
@@ -133,7 +134,7 @@ class Disassembler : public AllStatic {
133134
static void Disassemble(uword start,
134135
uword end,
135136
DisassemblyFormatter* formatter,
136-
const Code::Comments* comments) {
137+
const CodeComments* comments) {
137138
Disassemble(start, end, formatter, Code::Handle(), comments);
138139
}
139140

‎runtime/vm/compiler/backend/flow_graph.cc‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,15 @@ void FlowGraph::InsertConversionsFor(Definition* def) {
19041904
static void UnboxPhi(PhiInstr* phi) {
19051905
Representation unboxed = phi->representation();
19061906

1907+
#if defined(TARGET_ARCH_IS_64_BIT)
1908+
if (phi->Type()->IsInt()) {
1909+
unboxed =
1910+
RangeUtils::Fits(phi->range(), RangeBoundary::kRangeBoundaryInt32)
1911+
? kUnboxedInt32
1912+
: kUnboxedInt64;
1913+
}
1914+
#endif
1915+
19071916
switch (phi->Type()->ToCid()) {
19081917
case kDoubleCid:
19091918
if (CanUnboxDouble()) {
@@ -1968,9 +1977,6 @@ static void UnboxPhi(PhiInstr* phi) {
19681977
Definition* input = phi->InputAt(i)->definition();
19691978
if (input->IsBox()) {
19701979
should_unbox = true;
1971-
} else if (!input->IsConstant()) {
1972-
should_unbox = false;
1973-
break;
19741980
}
19751981
}
19761982

‎runtime/vm/compiler/backend/il.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,6 +3520,10 @@ class ReachabilityFenceInstr : public TemplateInstruction<1, NoThrow> {
35203520
virtual bool ComputeCanDeoptimize() const { return false; }
35213521
virtual bool HasUnknownSideEffects() const { return false; }
35223522

3523+
virtual Representation RequiredInputRepresentation(intptr_t idx) const {
3524+
return kNoRepresentation;
3525+
}
3526+
35233527
PRINT_OPERANDS_TO_SUPPORT
35243528

35253529
private:

‎runtime/vm/compiler/backend/redundancy_elimination.cc‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4224,7 +4224,14 @@ void DeadCodeElimination::EliminateDeadCode(FlowGraph* flow_graph) {
42244224
}
42254225
}
42264226

4227+
static bool IsNoInterrupts(const Function& function) {
4228+
Object& options = Object::Handle();
4229+
return Library::FindPragma(dart::Thread::Current(), /*only_core=*/false,
4230+
function, Symbols::vm_no_interrupts(), &options);
4231+
}
4232+
42274233
void CheckStackOverflowElimination::EliminateStackOverflow(FlowGraph* graph) {
4234+
const bool remove_all = IsNoInterrupts(graph->function());
42284235
CheckStackOverflowInstr* first_stack_overflow_instr = NULL;
42294236
for (BlockIterator block_it = graph->reverse_postorder_iterator();
42304237
!block_it.Done(); block_it.Advance()) {
@@ -4234,6 +4241,11 @@ void CheckStackOverflowElimination::EliminateStackOverflow(FlowGraph* graph) {
42344241
Instruction* current = it.Current();
42354242

42364243
if (CheckStackOverflowInstr* instr = current->AsCheckStackOverflow()) {
4244+
if (remove_all) {
4245+
it.RemoveCurrentFromGraph();
4246+
continue;
4247+
}
4248+
42374249
if (first_stack_overflow_instr == NULL) {
42384250
first_stack_overflow_instr = instr;
42394251
ASSERT(!first_stack_overflow_instr->in_loop());

‎runtime/vm/dwarf.cc‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "vm/dwarf.h"
66

7+
#include "vm/code_comments.h"
78
#include "vm/code_descriptors.h"
89
#include "vm/elf.h"
910
#include "vm/image_snapshot.h"
@@ -13,6 +14,11 @@ namespace dart {
1314

1415
#if defined(DART_PRECOMPILER)
1516

17+
DEFINE_FLAG(charp,
18+
write_code_comments_as_synthetic_source_to,
19+
nullptr,
20+
"Print comments associated with instructions into the given file");
21+
1622
class DwarfPosition {
1723
public:
1824
DwarfPosition(int32_t line, int32_t column) : line_(line), column_(column) {
@@ -506,7 +512,158 @@ void Dwarf::WriteInliningNode(DwarfWriteStream* stream,
506512
stream->uleb128(0); // End of children.
507513
}
508514

515+
void Dwarf::WriteSyntheticLineNumberProgram(DwarfWriteStream* stream) {
516+
auto file_open = Dart::file_open_callback();
517+
auto file_write = Dart::file_write_callback();
518+
auto file_close = Dart::file_close_callback();
519+
if ((file_open == nullptr) || (file_write == nullptr) ||
520+
(file_close == nullptr)) {
521+
return;
522+
}
523+
524+
TextBuffer comments_buffer(128 * KB);
525+
526+
auto comments_file = file_open(FLAG_write_code_comments_as_synthetic_source_to, /*write=*/true);
527+
if (comments_file == nullptr) {
528+
OS::PrintErr("Failed to open file %s\n", FLAG_write_code_comments_as_synthetic_source_to);
529+
return;
530+
}
531+
532+
// 6.2.4 The Line Number Program Header
533+
534+
// 1. unit_length. This encoding implies 32-bit DWARF.
535+
auto const line_prefix = "line";
536+
intptr_t line_start;
537+
intptr_t line_size_fixup = stream->ReserveSize(line_prefix, &line_start);
538+
539+
stream->u2(2); // 2. DWARF version 2
540+
541+
// 3. header_length
542+
auto const lineheader_prefix = "lineheader";
543+
intptr_t lineheader_start;
544+
intptr_t lineheader_size_fixup =
545+
stream->ReserveSize(lineheader_prefix, &lineheader_start);
546+
547+
stream->u1(1); // 4. minimum_instruction_length
548+
stream->u1(1); // 5. default_is_stmt (true for compatibility with dsymutil).
549+
stream->u1(0); // 6. line_base
550+
stream->u1(1); // 7. line_range
551+
stream->u1(13); // 8. opcode_base (12 standard opcodes in Dwarf 2)
552+
553+
// 9. standard_opcode_lengths
554+
stream->u1(0); // DW_LNS_copy, 0 operands
555+
stream->u1(1); // DW_LNS_advance_pc, 1 operands
556+
stream->u1(1); // DW_LNS_advance_list, 1 operands
557+
stream->u1(1); // DW_LNS_set_file, 1 operands
558+
stream->u1(1); // DW_LNS_set_column, 1 operands
559+
stream->u1(0); // DW_LNS_negate_stmt, 0 operands
560+
stream->u1(0); // DW_LNS_set_basic_block, 0 operands
561+
stream->u1(0); // DW_LNS_const_add_pc, 0 operands
562+
stream->u1(1); // DW_LNS_fixed_advance_pc, 1 operands
563+
stream->u1(0); // DW_LNS_set_prolog_end, 0 operands
564+
stream->u1(0); // DW_LNS_set_epligoue_begin, 0 operands
565+
stream->u1(1); // DW_LNS_set_isa, 1 operands
566+
567+
// 10. include_directories (sequence of path names)
568+
// We don't emit any because we use full paths below.
569+
stream->u1(0);
570+
571+
// 11. file_names (sequence of file entries)
572+
stream->string(FLAG_write_code_comments_as_synthetic_source_to); // NOLINT
573+
stream->uleb128(0); // Include directory index.
574+
stream->uleb128(0); // File modification time.
575+
stream->uleb128(0); // File length.
576+
stream->u1(0); // End of file names.
577+
stream->SetSize(lineheader_size_fixup, lineheader_prefix, lineheader_start);
578+
579+
// 6.2.5 The Line Number Program
580+
581+
// The initial values for the line number program state machine registers
582+
// according to the DWARF standard.
583+
intptr_t previous_pc_offset = 0;
584+
intptr_t previous_line = 1;
585+
// Other info not stored in the state machine registers.
586+
const char* previous_asm_name = nullptr;
587+
588+
intptr_t current_line = 0;
589+
590+
for (intptr_t i = 0; i < codes_.length(); i++) {
591+
const Code& code = *(codes_[i]);
592+
auto const asm_name = code_to_name_.LookupValue(&code);
593+
ASSERT(asm_name != nullptr);
594+
595+
auto comments = code.comments();
596+
if (comments == nullptr) {
597+
continue;
598+
}
599+
600+
for (intptr_t i = 0, len = comments->Length(); i < len;) {
601+
intptr_t current_pc_offset = comments->PCOffsetAt(i);
602+
while (i < len && current_pc_offset == comments->PCOffsetAt(i)) {
603+
comments_buffer.AddString(comments->CommentAt(i));
604+
comments_buffer.AddChar('\n');
605+
current_line++;
606+
i++;
607+
}
608+
609+
if (current_line != previous_line) {
610+
stream->u1(DW_LNS_advance_line);
611+
stream->sleb128(current_line - previous_line);
612+
previous_line = current_line;
613+
}
614+
615+
if (previous_asm_name == nullptr) {
616+
auto const instr_size = 1 + compiler::target::kWordSize;
617+
stream->u1(0); // This is an extended opcode
618+
stream->u1(instr_size); // that is 5 or 9 bytes long
619+
stream->u1(DW_LNE_set_address);
620+
stream->OffsetFromSymbol(asm_name, current_pc_offset);
621+
} else {
622+
stream->u1(DW_LNS_advance_pc);
623+
stream->DistanceBetweenSymbolOffsets(asm_name, current_pc_offset,
624+
previous_asm_name,
625+
previous_pc_offset);
626+
}
627+
stream->u1(DW_LNS_copy);
628+
previous_asm_name = asm_name;
629+
previous_pc_offset = current_pc_offset;
630+
}
631+
}
632+
633+
// Advance pc to end of the compilation unit if not already there.
634+
if (codes_.length() != 0) {
635+
const intptr_t last_code_index = codes_.length() - 1;
636+
const Code& last_code = *(codes_[last_code_index]);
637+
const intptr_t last_pc_offset = last_code.Size();
638+
const char* last_asm_name = code_to_name_.LookupValue(&last_code);
639+
ASSERT(last_asm_name != nullptr);
640+
641+
stream->u1(DW_LNS_advance_pc);
642+
if (previous_asm_name != nullptr) {
643+
stream->DistanceBetweenSymbolOffsets(
644+
last_asm_name, last_pc_offset, previous_asm_name, previous_pc_offset);
645+
} else {
646+
// No LNP entries (e.g., only stub code).
647+
ASSERT(previous_pc_offset == 0);
648+
stream->uleb128(last_pc_offset);
649+
}
650+
}
651+
652+
// End of contiguous machine code.
653+
stream->u1(0); // This is an extended opcode
654+
stream->u1(1); // that is 1 byte long
655+
stream->u1(DW_LNE_end_sequence);
656+
stream->SetSize(line_size_fixup, line_prefix, line_start);
657+
658+
file_write(comments_buffer.buffer(), comments_buffer.length(), comments_file);
659+
file_close(comments_file);
660+
}
661+
509662
void Dwarf::WriteLineNumberProgram(DwarfWriteStream* stream) {
663+
if (FLAG_write_code_comments_as_synthetic_source_to != nullptr) {
664+
WriteSyntheticLineNumberProgram(stream);
665+
return;
666+
}
510667
// 6.2.4 The Line Number Program Header
511668

512669
// 1. unit_length. This encoding implies 32-bit DWARF.

‎runtime/vm/dwarf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ class Dwarf : public ZoneAllocated {
307307
const char* root_code_name,
308308
const Script& parent_script);
309309

310+
void WriteSyntheticLineNumberProgram(DwarfWriteStream* stream);
311+
310312
const char* Deobfuscate(const char* cstr);
311313
static Trie<const char>* CreateReverseObfuscationTrie(Zone* zone);
312314

0 commit comments

Comments
 (0)