1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <cstring>
6 #include <iomanip>
7
8 #include "src/codegen/assembler-inl.h"
9 #include "src/codegen/code-comments.h"
10
11 namespace v8 {
12 namespace internal {
13
14 namespace {
15 static constexpr uint8_t kOffsetToFirstCommentEntry = kUInt32Size;
16 static constexpr uint8_t kOffsetToPCOffset = 0;
17 static constexpr uint8_t kOffsetToCommentSize = kOffsetToPCOffset + kUInt32Size;
18 static constexpr uint8_t kOffsetToCommentString =
19 kOffsetToCommentSize + kUInt32Size;
20 } // namespace
21
comment_length() const22 uint32_t CodeCommentEntry::comment_length() const {
23 return static_cast<uint32_t>(comment.size() + 1);
24 }
25
size() const26 uint32_t CodeCommentEntry::size() const {
27 return kOffsetToCommentString + comment_length();
28 }
29
CodeCommentsIterator(Address code_comments_start,uint32_t code_comments_size)30 CodeCommentsIterator::CodeCommentsIterator(Address code_comments_start,
31 uint32_t code_comments_size)
32 : code_comments_start_(code_comments_start),
33 code_comments_size_(code_comments_size),
34 current_entry_(code_comments_start + kOffsetToFirstCommentEntry) {
35 DCHECK_NE(kNullAddress, code_comments_start);
36 DCHECK_IMPLIES(code_comments_size,
37 code_comments_size ==
38 base::ReadUnalignedValue<uint32_t>(code_comments_start_));
39 }
40
size() const41 uint32_t CodeCommentsIterator::size() const { return code_comments_size_; }
42
GetComment() const43 const char* CodeCommentsIterator::GetComment() const {
44 const char* comment_string =
45 reinterpret_cast<const char*>(current_entry_ + kOffsetToCommentString);
46 CHECK_EQ(GetCommentSize(), strlen(comment_string) + 1);
47 return comment_string;
48 }
49
GetCommentSize() const50 uint32_t CodeCommentsIterator::GetCommentSize() const {
51 return ReadUnalignedValue<uint32_t>(current_entry_ + kOffsetToCommentSize);
52 }
53
GetPCOffset() const54 uint32_t CodeCommentsIterator::GetPCOffset() const {
55 return ReadUnalignedValue<uint32_t>(current_entry_ + kOffsetToPCOffset);
56 }
57
Next()58 void CodeCommentsIterator::Next() {
59 current_entry_ += kOffsetToCommentString + GetCommentSize();
60 }
61
HasCurrent() const62 bool CodeCommentsIterator::HasCurrent() const {
63 return current_entry_ < code_comments_start_ + size();
64 }
65
Emit(Assembler * assm)66 void CodeCommentsWriter::Emit(Assembler* assm) {
67 assm->dd(section_size());
68 for (auto i = comments_.begin(); i != comments_.end(); ++i) {
69 assm->dd(i->pc_offset);
70 assm->dd(i->comment_length());
71 for (char c : i->comment) {
72 EnsureSpace ensure_space(assm);
73 assm->db(c);
74 }
75 assm->db('\0');
76 }
77 }
78
Add(uint32_t pc_offset,std::string comment)79 void CodeCommentsWriter::Add(uint32_t pc_offset, std::string comment) {
80 CodeCommentEntry entry = {pc_offset, std::move(comment)};
81 byte_count_ += entry.size();
82 comments_.push_back(std::move(entry));
83 }
84
entry_count() const85 size_t CodeCommentsWriter::entry_count() const { return comments_.size(); }
section_size() const86 uint32_t CodeCommentsWriter::section_size() const {
87 return kOffsetToFirstCommentEntry + static_cast<uint32_t>(byte_count_);
88 }
89
PrintCodeCommentsSection(std::ostream & out,Address code_comments_start,uint32_t code_comments_size)90 void PrintCodeCommentsSection(std::ostream& out, Address code_comments_start,
91 uint32_t code_comments_size) {
92 CodeCommentsIterator it(code_comments_start, code_comments_size);
93 out << "CodeComments (size = " << it.size() << ")\n";
94 if (it.HasCurrent()) {
95 out << std::setw(6) << "pc" << std::setw(6) << "len"
96 << " comment\n";
97 }
98 for (; it.HasCurrent(); it.Next()) {
99 out << std::hex << std::setw(6) << it.GetPCOffset() << std::dec
100 << std::setw(6) << it.GetCommentSize() << " (" << it.GetComment()
101 << ")\n";
102 }
103 }
104
105 } // namespace internal
106 } // namespace v8
107