11cb0ef41Sopenharmony_ci// Copyright 2018 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#include <cstring> 61cb0ef41Sopenharmony_ci#include <iomanip> 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "src/codegen/assembler-inl.h" 91cb0ef41Sopenharmony_ci#include "src/codegen/code-comments.h" 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cinamespace v8 { 121cb0ef41Sopenharmony_cinamespace internal { 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cinamespace { 151cb0ef41Sopenharmony_cistatic constexpr uint8_t kOffsetToFirstCommentEntry = kUInt32Size; 161cb0ef41Sopenharmony_cistatic constexpr uint8_t kOffsetToPCOffset = 0; 171cb0ef41Sopenharmony_cistatic constexpr uint8_t kOffsetToCommentSize = kOffsetToPCOffset + kUInt32Size; 181cb0ef41Sopenharmony_cistatic constexpr uint8_t kOffsetToCommentString = 191cb0ef41Sopenharmony_ci kOffsetToCommentSize + kUInt32Size; 201cb0ef41Sopenharmony_ci} // namespace 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciuint32_t CodeCommentEntry::comment_length() const { 231cb0ef41Sopenharmony_ci return static_cast<uint32_t>(comment.size() + 1); 241cb0ef41Sopenharmony_ci} 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciuint32_t CodeCommentEntry::size() const { 271cb0ef41Sopenharmony_ci return kOffsetToCommentString + comment_length(); 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciCodeCommentsIterator::CodeCommentsIterator(Address code_comments_start, 311cb0ef41Sopenharmony_ci uint32_t code_comments_size) 321cb0ef41Sopenharmony_ci : code_comments_start_(code_comments_start), 331cb0ef41Sopenharmony_ci code_comments_size_(code_comments_size), 341cb0ef41Sopenharmony_ci current_entry_(code_comments_start + kOffsetToFirstCommentEntry) { 351cb0ef41Sopenharmony_ci DCHECK_NE(kNullAddress, code_comments_start); 361cb0ef41Sopenharmony_ci DCHECK_IMPLIES(code_comments_size, 371cb0ef41Sopenharmony_ci code_comments_size == 381cb0ef41Sopenharmony_ci base::ReadUnalignedValue<uint32_t>(code_comments_start_)); 391cb0ef41Sopenharmony_ci} 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciuint32_t CodeCommentsIterator::size() const { return code_comments_size_; } 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ciconst char* CodeCommentsIterator::GetComment() const { 441cb0ef41Sopenharmony_ci const char* comment_string = 451cb0ef41Sopenharmony_ci reinterpret_cast<const char*>(current_entry_ + kOffsetToCommentString); 461cb0ef41Sopenharmony_ci CHECK_EQ(GetCommentSize(), strlen(comment_string) + 1); 471cb0ef41Sopenharmony_ci return comment_string; 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciuint32_t CodeCommentsIterator::GetCommentSize() const { 511cb0ef41Sopenharmony_ci return ReadUnalignedValue<uint32_t>(current_entry_ + kOffsetToCommentSize); 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciuint32_t CodeCommentsIterator::GetPCOffset() const { 551cb0ef41Sopenharmony_ci return ReadUnalignedValue<uint32_t>(current_entry_ + kOffsetToPCOffset); 561cb0ef41Sopenharmony_ci} 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_civoid CodeCommentsIterator::Next() { 591cb0ef41Sopenharmony_ci current_entry_ += kOffsetToCommentString + GetCommentSize(); 601cb0ef41Sopenharmony_ci} 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_cibool CodeCommentsIterator::HasCurrent() const { 631cb0ef41Sopenharmony_ci return current_entry_ < code_comments_start_ + size(); 641cb0ef41Sopenharmony_ci} 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_civoid CodeCommentsWriter::Emit(Assembler* assm) { 671cb0ef41Sopenharmony_ci assm->dd(section_size()); 681cb0ef41Sopenharmony_ci for (auto i = comments_.begin(); i != comments_.end(); ++i) { 691cb0ef41Sopenharmony_ci assm->dd(i->pc_offset); 701cb0ef41Sopenharmony_ci assm->dd(i->comment_length()); 711cb0ef41Sopenharmony_ci for (char c : i->comment) { 721cb0ef41Sopenharmony_ci EnsureSpace ensure_space(assm); 731cb0ef41Sopenharmony_ci assm->db(c); 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci assm->db('\0'); 761cb0ef41Sopenharmony_ci } 771cb0ef41Sopenharmony_ci} 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_civoid CodeCommentsWriter::Add(uint32_t pc_offset, std::string comment) { 801cb0ef41Sopenharmony_ci CodeCommentEntry entry = {pc_offset, std::move(comment)}; 811cb0ef41Sopenharmony_ci byte_count_ += entry.size(); 821cb0ef41Sopenharmony_ci comments_.push_back(std::move(entry)); 831cb0ef41Sopenharmony_ci} 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_cisize_t CodeCommentsWriter::entry_count() const { return comments_.size(); } 861cb0ef41Sopenharmony_ciuint32_t CodeCommentsWriter::section_size() const { 871cb0ef41Sopenharmony_ci return kOffsetToFirstCommentEntry + static_cast<uint32_t>(byte_count_); 881cb0ef41Sopenharmony_ci} 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci} // namespace internal 911cb0ef41Sopenharmony_ci} // namespace v8 92