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
11namespace v8 {
12namespace internal {
13
14namespace {
15static constexpr uint8_t kOffsetToFirstCommentEntry = kUInt32Size;
16static constexpr uint8_t kOffsetToPCOffset = 0;
17static constexpr uint8_t kOffsetToCommentSize = kOffsetToPCOffset + kUInt32Size;
18static constexpr uint8_t kOffsetToCommentString =
19 kOffsetToCommentSize + kUInt32Size;
20} // namespace
21
22uint32_t CodeCommentEntry::comment_length() const {
23 return static_cast<uint32_t>(comment.size() + 1);
24}
25
26uint32_t CodeCommentEntry::size() const {
27 return kOffsetToCommentString + comment_length();
28}
29
30CodeCommentsIterator::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(
37 code_comments_size,
38 code_comments_size == *reinterpret_cast<uint32_t*>(code_comments_start_));
39}
40
41uint32_t CodeCommentsIterator::size() const { return code_comments_size_; }
42
43const 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
50uint32_t CodeCommentsIterator::GetCommentSize() const {
51 return ReadUnalignedValue<uint32_t>(current_entry_ + kOffsetToCommentSize);
52}
53
54uint32_t CodeCommentsIterator::GetPCOffset() const {
55 return ReadUnalignedValue<uint32_t>(current_entry_ + kOffsetToPCOffset);
56}
57
58void CodeCommentsIterator::Next() {
59 current_entry_ += kOffsetToCommentString + GetCommentSize();
60}
61
62bool CodeCommentsIterator::HasCurrent() const {
63 return current_entry_ < code_comments_start_ + size();
64}
65
66void 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
79void 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
85size_t CodeCommentsWriter::entry_count() const { return comments_.size(); }
86uint32_t CodeCommentsWriter::section_size() const {
87 return kOffsetToFirstCommentEntry + static_cast<uint32_t>(byte_count_);
88}
89
90void 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