Line data Source code
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/assembler-inl.h"
9 : #include "src/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 :
22 0 : uint32_t CodeCommentEntry::comment_length() const {
23 9928 : return static_cast<uint32_t>(comment.size() + 1);
24 : }
25 :
26 0 : uint32_t CodeCommentEntry::size() const {
27 4985 : return kOffsetToCommentString + comment_length();
28 : }
29 :
30 4 : 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 4 : current_entry_(code_comments_start + kOffsetToFirstCommentEntry) {
35 : DCHECK_NE(kNullAddress, code_comments_start);
36 : DCHECK_IMPLIES(
37 : code_comments_size,
38 : code_comments_size == *reinterpret_cast<uint32_t*>(code_comments_start_));
39 4 : }
40 :
41 24 : uint32_t CodeCommentsIterator::size() const { return code_comments_size_; }
42 :
43 16 : const char* CodeCommentsIterator::GetComment() const {
44 : const char* comment_string =
45 16 : reinterpret_cast<const char*>(current_entry_ + kOffsetToCommentString);
46 32 : CHECK_EQ(GetCommentSize(), strlen(comment_string) + 1);
47 16 : return comment_string;
48 : }
49 :
50 0 : uint32_t CodeCommentsIterator::GetCommentSize() const {
51 32 : return ReadUnalignedValue<uint32_t>(current_entry_ + kOffsetToCommentSize);
52 : }
53 :
54 0 : uint32_t CodeCommentsIterator::GetPCOffset() const {
55 0 : return ReadUnalignedValue<uint32_t>(current_entry_ + kOffsetToPCOffset);
56 : }
57 :
58 16 : void CodeCommentsIterator::Next() {
59 16 : current_entry_ += kOffsetToCommentString + GetCommentSize();
60 16 : }
61 :
62 24 : bool CodeCommentsIterator::HasCurrent() const {
63 48 : return current_entry_ < code_comments_start_ + size();
64 : }
65 :
66 82 : void CodeCommentsWriter::Emit(Assembler* assm) {
67 82 : assm->dd(section_size());
68 5025 : for (auto i = comments_.begin(); i != comments_.end(); ++i) {
69 4943 : assm->dd(i->pc_offset);
70 4943 : assm->dd(i->comment_length());
71 209931 : for (char c : i->comment) {
72 : EnsureSpace ensure_space(assm);
73 204988 : assm->db(c);
74 : }
75 4943 : assm->db('\0');
76 : }
77 82 : }
78 :
79 4985 : void CodeCommentsWriter::Add(uint32_t pc_offset, std::string comment) {
80 4985 : CodeCommentEntry entry = {pc_offset, std::move(comment)};
81 4985 : byte_count_ += entry.size();
82 4985 : comments_.push_back(std::move(entry));
83 4983 : }
84 :
85 194 : size_t CodeCommentsWriter::entry_count() const { return comments_.size(); }
86 0 : uint32_t CodeCommentsWriter::section_size() const {
87 82 : return kOffsetToFirstCommentEntry + static_cast<uint32_t>(byte_count_);
88 : }
89 :
90 0 : 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 0 : out << "CodeComments (size = " << it.size() << ")\n";
94 0 : if (it.HasCurrent()) {
95 : out << std::setw(6) << "pc" << std::setw(6) << "len"
96 0 : << " comment\n";
97 : }
98 0 : for (; it.HasCurrent(); it.Next()) {
99 : out << std::hex << std::setw(6) << it.GetPCOffset() << std::dec
100 0 : << std::setw(6) << it.GetCommentSize() << " (" << it.GetComment()
101 0 : << ")\n";
102 : }
103 0 : }
104 :
105 : } // namespace internal
106 122004 : } // namespace v8
|