11cb0ef41Sopenharmony_ci// Copyright (c) 1994-2006 Sun Microsystems Inc.
21cb0ef41Sopenharmony_ci// All Rights Reserved.
31cb0ef41Sopenharmony_ci//
41cb0ef41Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
51cb0ef41Sopenharmony_ci// modification, are permitted provided that the following conditions are
61cb0ef41Sopenharmony_ci// met:
71cb0ef41Sopenharmony_ci//
81cb0ef41Sopenharmony_ci// - Redistributions of source code must retain the above copyright notice,
91cb0ef41Sopenharmony_ci// this list of conditions and the following disclaimer.
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// - Redistribution in binary form must reproduce the above copyright
121cb0ef41Sopenharmony_ci// notice, this list of conditions and the following disclaimer in the
131cb0ef41Sopenharmony_ci// documentation and/or other materials provided with the distribution.
141cb0ef41Sopenharmony_ci//
151cb0ef41Sopenharmony_ci// - Neither the name of Sun Microsystems or the names of contributors may
161cb0ef41Sopenharmony_ci// be used to endorse or promote products derived from this software without
171cb0ef41Sopenharmony_ci// specific prior written permission.
181cb0ef41Sopenharmony_ci//
191cb0ef41Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
201cb0ef41Sopenharmony_ci// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
211cb0ef41Sopenharmony_ci// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221cb0ef41Sopenharmony_ci// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
231cb0ef41Sopenharmony_ci// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
241cb0ef41Sopenharmony_ci// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
251cb0ef41Sopenharmony_ci// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
261cb0ef41Sopenharmony_ci// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
271cb0ef41Sopenharmony_ci// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
281cb0ef41Sopenharmony_ci// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
291cb0ef41Sopenharmony_ci// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci// The original source code covered by the above license above has been
321cb0ef41Sopenharmony_ci// modified significantly by Google Inc.
331cb0ef41Sopenharmony_ci// Copyright 2012 the V8 project authors. All rights reserved.
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci#include "src/codegen/assembler.h"
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci#ifdef V8_CODE_COMMENTS
381cb0ef41Sopenharmony_ci#include <iomanip>
391cb0ef41Sopenharmony_ci#endif
401cb0ef41Sopenharmony_ci#include "src/base/vector.h"
411cb0ef41Sopenharmony_ci#include "src/codegen/assembler-inl.h"
421cb0ef41Sopenharmony_ci#include "src/codegen/string-constants.h"
431cb0ef41Sopenharmony_ci#include "src/deoptimizer/deoptimizer.h"
441cb0ef41Sopenharmony_ci#include "src/diagnostics/disassembler.h"
451cb0ef41Sopenharmony_ci#include "src/execution/isolate.h"
461cb0ef41Sopenharmony_ci#include "src/heap/heap-inl.h"  // For MemoryAllocator. TODO(jkummerow): Drop.
471cb0ef41Sopenharmony_ci#include "src/snapshot/embedded/embedded-data.h"
481cb0ef41Sopenharmony_ci#include "src/snapshot/snapshot.h"
491cb0ef41Sopenharmony_ci#include "src/utils/ostreams.h"
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cinamespace v8 {
521cb0ef41Sopenharmony_cinamespace internal {
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciAssemblerOptions AssemblerOptions::Default(Isolate* isolate) {
551cb0ef41Sopenharmony_ci  AssemblerOptions options;
561cb0ef41Sopenharmony_ci  const bool serializer = isolate->serializer_enabled();
571cb0ef41Sopenharmony_ci  const bool generating_embedded_builtin =
581cb0ef41Sopenharmony_ci      isolate->IsGeneratingEmbeddedBuiltins();
591cb0ef41Sopenharmony_ci  options.record_reloc_info_for_serialization = serializer;
601cb0ef41Sopenharmony_ci  options.enable_root_relative_access =
611cb0ef41Sopenharmony_ci      !serializer && !generating_embedded_builtin;
621cb0ef41Sopenharmony_ci#ifdef USE_SIMULATOR
631cb0ef41Sopenharmony_ci  // Even though the simulator is enabled, we may still need to generate code
641cb0ef41Sopenharmony_ci  // that may need to run on both the simulator and real hardware. For example,
651cb0ef41Sopenharmony_ci  // if we are cross-compiling and embedding a script into the snapshot, the
661cb0ef41Sopenharmony_ci  // script will need to run on the host causing the embedded builtins to run in
671cb0ef41Sopenharmony_ci  // the simulator. While the final cross-compiled V8 will not have a simulator.
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  // So here we enable simulator specific code if not generating the snapshot or
701cb0ef41Sopenharmony_ci  // if we are but we are targetting the simulator *only*.
711cb0ef41Sopenharmony_ci  options.enable_simulator_code = !serializer || FLAG_target_is_simulator;
721cb0ef41Sopenharmony_ci#endif
731cb0ef41Sopenharmony_ci  options.inline_offheap_trampolines &= !generating_embedded_builtin;
741cb0ef41Sopenharmony_ci#if V8_TARGET_ARCH_X64 || V8_TARGET_ARCH_ARM64
751cb0ef41Sopenharmony_ci  options.code_range_base = isolate->heap()->code_range_base();
761cb0ef41Sopenharmony_ci#endif
771cb0ef41Sopenharmony_ci  options.short_builtin_calls =
781cb0ef41Sopenharmony_ci      isolate->is_short_builtin_calls_enabled() &&
791cb0ef41Sopenharmony_ci      !generating_embedded_builtin &&
801cb0ef41Sopenharmony_ci      (options.code_range_base != kNullAddress) &&
811cb0ef41Sopenharmony_ci      // Serialization of RUNTIME_ENTRY reloc infos is not supported yet.
821cb0ef41Sopenharmony_ci      !serializer;
831cb0ef41Sopenharmony_ci  return options;
841cb0ef41Sopenharmony_ci}
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciAssemblerOptions AssemblerOptions::DefaultForOffHeapTrampoline(
871cb0ef41Sopenharmony_ci    Isolate* isolate) {
881cb0ef41Sopenharmony_ci  AssemblerOptions options = AssemblerOptions::Default(isolate);
891cb0ef41Sopenharmony_ci  // Off-heap trampolines may not contain any metadata since their metadata
901cb0ef41Sopenharmony_ci  // offsets refer to the off-heap metadata area.
911cb0ef41Sopenharmony_ci  options.emit_code_comments = false;
921cb0ef41Sopenharmony_ci  return options;
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_cinamespace {
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ciclass DefaultAssemblerBuffer : public AssemblerBuffer {
981cb0ef41Sopenharmony_ci public:
991cb0ef41Sopenharmony_ci  explicit DefaultAssemblerBuffer(int size)
1001cb0ef41Sopenharmony_ci      : buffer_(base::OwnedVector<uint8_t>::NewForOverwrite(
1011cb0ef41Sopenharmony_ci            std::max(AssemblerBase::kMinimalBufferSize, size))) {
1021cb0ef41Sopenharmony_ci#ifdef DEBUG
1031cb0ef41Sopenharmony_ci    ZapCode(reinterpret_cast<Address>(buffer_.start()), buffer_.size());
1041cb0ef41Sopenharmony_ci#endif
1051cb0ef41Sopenharmony_ci  }
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  byte* start() const override { return buffer_.start(); }
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  int size() const override { return static_cast<int>(buffer_.size()); }
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  std::unique_ptr<AssemblerBuffer> Grow(int new_size) override {
1121cb0ef41Sopenharmony_ci    DCHECK_LT(size(), new_size);
1131cb0ef41Sopenharmony_ci    return std::make_unique<DefaultAssemblerBuffer>(new_size);
1141cb0ef41Sopenharmony_ci  }
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci private:
1171cb0ef41Sopenharmony_ci  base::OwnedVector<uint8_t> buffer_;
1181cb0ef41Sopenharmony_ci};
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ciclass ExternalAssemblerBufferImpl : public AssemblerBuffer {
1211cb0ef41Sopenharmony_ci public:
1221cb0ef41Sopenharmony_ci  ExternalAssemblerBufferImpl(byte* start, int size)
1231cb0ef41Sopenharmony_ci      : start_(start), size_(size) {}
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  byte* start() const override { return start_; }
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  int size() const override { return size_; }
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci  std::unique_ptr<AssemblerBuffer> Grow(int new_size) override {
1301cb0ef41Sopenharmony_ci    FATAL("Cannot grow external assembler buffer");
1311cb0ef41Sopenharmony_ci  }
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  void* operator new(std::size_t count);
1341cb0ef41Sopenharmony_ci  void operator delete(void* ptr) noexcept;
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci private:
1371cb0ef41Sopenharmony_ci  byte* const start_;
1381cb0ef41Sopenharmony_ci  const int size_;
1391cb0ef41Sopenharmony_ci};
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_cistatic thread_local std::aligned_storage_t<sizeof(ExternalAssemblerBufferImpl),
1421cb0ef41Sopenharmony_ci                                           alignof(ExternalAssemblerBufferImpl)>
1431cb0ef41Sopenharmony_ci    tls_singleton_storage;
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_cistatic thread_local bool tls_singleton_taken{false};
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_civoid* ExternalAssemblerBufferImpl::operator new(std::size_t count) {
1481cb0ef41Sopenharmony_ci  DCHECK_EQ(count, sizeof(ExternalAssemblerBufferImpl));
1491cb0ef41Sopenharmony_ci  if (V8_LIKELY(!tls_singleton_taken)) {
1501cb0ef41Sopenharmony_ci    tls_singleton_taken = true;
1511cb0ef41Sopenharmony_ci    return &tls_singleton_storage;
1521cb0ef41Sopenharmony_ci  }
1531cb0ef41Sopenharmony_ci  return ::operator new(count);
1541cb0ef41Sopenharmony_ci}
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_civoid ExternalAssemblerBufferImpl::operator delete(void* ptr) noexcept {
1571cb0ef41Sopenharmony_ci  if (V8_LIKELY(ptr == &tls_singleton_storage)) {
1581cb0ef41Sopenharmony_ci    DCHECK(tls_singleton_taken);
1591cb0ef41Sopenharmony_ci    tls_singleton_taken = false;
1601cb0ef41Sopenharmony_ci    return;
1611cb0ef41Sopenharmony_ci  }
1621cb0ef41Sopenharmony_ci  ::operator delete(ptr);
1631cb0ef41Sopenharmony_ci}
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci}  // namespace
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_cistd::unique_ptr<AssemblerBuffer> ExternalAssemblerBuffer(void* start,
1681cb0ef41Sopenharmony_ci                                                         int size) {
1691cb0ef41Sopenharmony_ci  return std::make_unique<ExternalAssemblerBufferImpl>(
1701cb0ef41Sopenharmony_ci      reinterpret_cast<byte*>(start), size);
1711cb0ef41Sopenharmony_ci}
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_cistd::unique_ptr<AssemblerBuffer> NewAssemblerBuffer(int size) {
1741cb0ef41Sopenharmony_ci  return std::make_unique<DefaultAssemblerBuffer>(size);
1751cb0ef41Sopenharmony_ci}
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci// -----------------------------------------------------------------------------
1781cb0ef41Sopenharmony_ci// Implementation of AssemblerBase
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci// static
1811cb0ef41Sopenharmony_ciconstexpr int AssemblerBase::kMinimalBufferSize;
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci// static
1841cb0ef41Sopenharmony_ciconstexpr int AssemblerBase::kDefaultBufferSize;
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ciAssemblerBase::AssemblerBase(const AssemblerOptions& options,
1871cb0ef41Sopenharmony_ci                             std::unique_ptr<AssemblerBuffer> buffer)
1881cb0ef41Sopenharmony_ci    : buffer_(std::move(buffer)),
1891cb0ef41Sopenharmony_ci      options_(options),
1901cb0ef41Sopenharmony_ci      enabled_cpu_features_(0),
1911cb0ef41Sopenharmony_ci      predictable_code_size_(false),
1921cb0ef41Sopenharmony_ci      constant_pool_available_(false),
1931cb0ef41Sopenharmony_ci      jump_optimization_info_(nullptr) {
1941cb0ef41Sopenharmony_ci  if (!buffer_) buffer_ = NewAssemblerBuffer(kDefaultBufferSize);
1951cb0ef41Sopenharmony_ci  buffer_start_ = buffer_->start();
1961cb0ef41Sopenharmony_ci  pc_ = buffer_start_;
1971cb0ef41Sopenharmony_ci}
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ciAssemblerBase::~AssemblerBase() = default;
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_civoid AssemblerBase::Print(Isolate* isolate) {
2021cb0ef41Sopenharmony_ci  StdoutStream os;
2031cb0ef41Sopenharmony_ci  v8::internal::Disassembler::Decode(isolate, os, buffer_start_, pc_);
2041cb0ef41Sopenharmony_ci}
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ci// -----------------------------------------------------------------------------
2071cb0ef41Sopenharmony_ci// Implementation of CpuFeatureScope
2081cb0ef41Sopenharmony_ci
2091cb0ef41Sopenharmony_ci#ifdef DEBUG
2101cb0ef41Sopenharmony_ciCpuFeatureScope::CpuFeatureScope(AssemblerBase* assembler, CpuFeature f,
2111cb0ef41Sopenharmony_ci                                 CheckPolicy check)
2121cb0ef41Sopenharmony_ci    : assembler_(assembler) {
2131cb0ef41Sopenharmony_ci  DCHECK_IMPLIES(check == kCheckSupported, CpuFeatures::IsSupported(f));
2141cb0ef41Sopenharmony_ci  old_enabled_ = assembler_->enabled_cpu_features();
2151cb0ef41Sopenharmony_ci  assembler_->EnableCpuFeature(f);
2161cb0ef41Sopenharmony_ci}
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ciCpuFeatureScope::~CpuFeatureScope() {
2191cb0ef41Sopenharmony_ci  assembler_->set_enabled_cpu_features(old_enabled_);
2201cb0ef41Sopenharmony_ci}
2211cb0ef41Sopenharmony_ci#endif
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_cibool CpuFeatures::initialized_ = false;
2241cb0ef41Sopenharmony_cibool CpuFeatures::supports_wasm_simd_128_ = false;
2251cb0ef41Sopenharmony_cibool CpuFeatures::supports_cetss_ = false;
2261cb0ef41Sopenharmony_ciunsigned CpuFeatures::supported_ = 0;
2271cb0ef41Sopenharmony_ciunsigned CpuFeatures::icache_line_size_ = 0;
2281cb0ef41Sopenharmony_ciunsigned CpuFeatures::dcache_line_size_ = 0;
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ciHeapObjectRequest::HeapObjectRequest(double heap_number, int offset)
2311cb0ef41Sopenharmony_ci    : kind_(kHeapNumber), offset_(offset) {
2321cb0ef41Sopenharmony_ci  value_.heap_number = heap_number;
2331cb0ef41Sopenharmony_ci  DCHECK(!IsSmiDouble(value_.heap_number));
2341cb0ef41Sopenharmony_ci}
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ciHeapObjectRequest::HeapObjectRequest(const StringConstantBase* string,
2371cb0ef41Sopenharmony_ci                                     int offset)
2381cb0ef41Sopenharmony_ci    : kind_(kStringConstant), offset_(offset) {
2391cb0ef41Sopenharmony_ci  value_.string = string;
2401cb0ef41Sopenharmony_ci  DCHECK_NOT_NULL(value_.string);
2411cb0ef41Sopenharmony_ci}
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci// Platform specific but identical code for all the platforms.
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_civoid Assembler::RecordDeoptReason(DeoptimizeReason reason, uint32_t node_id,
2461cb0ef41Sopenharmony_ci                                  SourcePosition position, int id) {
2471cb0ef41Sopenharmony_ci  EnsureSpace ensure_space(this);
2481cb0ef41Sopenharmony_ci  RecordRelocInfo(RelocInfo::DEOPT_SCRIPT_OFFSET, position.ScriptOffset());
2491cb0ef41Sopenharmony_ci  RecordRelocInfo(RelocInfo::DEOPT_INLINING_ID, position.InliningId());
2501cb0ef41Sopenharmony_ci  RecordRelocInfo(RelocInfo::DEOPT_REASON, static_cast<int>(reason));
2511cb0ef41Sopenharmony_ci  RecordRelocInfo(RelocInfo::DEOPT_ID, id);
2521cb0ef41Sopenharmony_ci#ifdef DEBUG
2531cb0ef41Sopenharmony_ci  RecordRelocInfo(RelocInfo::DEOPT_NODE_ID, node_id);
2541cb0ef41Sopenharmony_ci#endif  // DEBUG
2551cb0ef41Sopenharmony_ci}
2561cb0ef41Sopenharmony_ci
2571cb0ef41Sopenharmony_civoid Assembler::DataAlign(int m) {
2581cb0ef41Sopenharmony_ci  DCHECK(m >= 2 && base::bits::IsPowerOfTwo(m));
2591cb0ef41Sopenharmony_ci  while ((pc_offset() & (m - 1)) != 0) {
2601cb0ef41Sopenharmony_ci    // Pad with 0xcc (= int3 on ia32 and x64); the primary motivation is that
2611cb0ef41Sopenharmony_ci    // the disassembler expects to find valid instructions, but this is also
2621cb0ef41Sopenharmony_ci    // nice from a security point of view.
2631cb0ef41Sopenharmony_ci    db(0xcc);
2641cb0ef41Sopenharmony_ci  }
2651cb0ef41Sopenharmony_ci}
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_civoid AssemblerBase::RequestHeapObject(HeapObjectRequest request) {
2681cb0ef41Sopenharmony_ci  request.set_offset(pc_offset());
2691cb0ef41Sopenharmony_ci  heap_object_requests_.push_front(request);
2701cb0ef41Sopenharmony_ci}
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ciint AssemblerBase::AddCodeTarget(Handle<CodeT> target) {
2731cb0ef41Sopenharmony_ci  int current = static_cast<int>(code_targets_.size());
2741cb0ef41Sopenharmony_ci  if (current > 0 && !target.is_null() &&
2751cb0ef41Sopenharmony_ci      code_targets_.back().address() == target.address()) {
2761cb0ef41Sopenharmony_ci    // Optimization if we keep jumping to the same code target.
2771cb0ef41Sopenharmony_ci    return current - 1;
2781cb0ef41Sopenharmony_ci  } else {
2791cb0ef41Sopenharmony_ci    code_targets_.push_back(target);
2801cb0ef41Sopenharmony_ci    return current;
2811cb0ef41Sopenharmony_ci  }
2821cb0ef41Sopenharmony_ci}
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ciHandle<CodeT> AssemblerBase::GetCodeTarget(intptr_t code_target_index) const {
2851cb0ef41Sopenharmony_ci  DCHECK_LT(static_cast<size_t>(code_target_index), code_targets_.size());
2861cb0ef41Sopenharmony_ci  return code_targets_[code_target_index];
2871cb0ef41Sopenharmony_ci}
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ciAssemblerBase::EmbeddedObjectIndex AssemblerBase::AddEmbeddedObject(
2901cb0ef41Sopenharmony_ci    Handle<HeapObject> object) {
2911cb0ef41Sopenharmony_ci  EmbeddedObjectIndex current = embedded_objects_.size();
2921cb0ef41Sopenharmony_ci  // Do not deduplicate invalid handles, they are to heap object requests.
2931cb0ef41Sopenharmony_ci  if (!object.is_null()) {
2941cb0ef41Sopenharmony_ci    auto entry = embedded_objects_map_.find(object);
2951cb0ef41Sopenharmony_ci    if (entry != embedded_objects_map_.end()) {
2961cb0ef41Sopenharmony_ci      return entry->second;
2971cb0ef41Sopenharmony_ci    }
2981cb0ef41Sopenharmony_ci    embedded_objects_map_[object] = current;
2991cb0ef41Sopenharmony_ci  }
3001cb0ef41Sopenharmony_ci  embedded_objects_.push_back(object);
3011cb0ef41Sopenharmony_ci  return current;
3021cb0ef41Sopenharmony_ci}
3031cb0ef41Sopenharmony_ci
3041cb0ef41Sopenharmony_ciHandle<HeapObject> AssemblerBase::GetEmbeddedObject(
3051cb0ef41Sopenharmony_ci    EmbeddedObjectIndex index) const {
3061cb0ef41Sopenharmony_ci  DCHECK_LT(index, embedded_objects_.size());
3071cb0ef41Sopenharmony_ci  return embedded_objects_[index];
3081cb0ef41Sopenharmony_ci}
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_ci
3111cb0ef41Sopenharmony_ciint Assembler::WriteCodeComments() {
3121cb0ef41Sopenharmony_ci  if (!FLAG_code_comments) return 0;
3131cb0ef41Sopenharmony_ci  CHECK_IMPLIES(code_comments_writer_.entry_count() > 0,
3141cb0ef41Sopenharmony_ci                options().emit_code_comments);
3151cb0ef41Sopenharmony_ci  if (code_comments_writer_.entry_count() == 0) return 0;
3161cb0ef41Sopenharmony_ci  int offset = pc_offset();
3171cb0ef41Sopenharmony_ci  code_comments_writer_.Emit(this);
3181cb0ef41Sopenharmony_ci  int size = pc_offset() - offset;
3191cb0ef41Sopenharmony_ci  DCHECK_EQ(size, code_comments_writer_.section_size());
3201cb0ef41Sopenharmony_ci  return size;
3211cb0ef41Sopenharmony_ci}
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci#ifdef V8_CODE_COMMENTS
3241cb0ef41Sopenharmony_ciint Assembler::CodeComment::depth() const { return assembler_->comment_depth_; }
3251cb0ef41Sopenharmony_civoid Assembler::CodeComment::Open(const std::string& comment) {
3261cb0ef41Sopenharmony_ci  std::stringstream sstream;
3271cb0ef41Sopenharmony_ci  sstream << std::setfill(' ') << std::setw(depth() * kIndentWidth + 2);
3281cb0ef41Sopenharmony_ci  sstream << "[ " << comment;
3291cb0ef41Sopenharmony_ci  assembler_->comment_depth_++;
3301cb0ef41Sopenharmony_ci  assembler_->RecordComment(sstream.str());
3311cb0ef41Sopenharmony_ci}
3321cb0ef41Sopenharmony_ci
3331cb0ef41Sopenharmony_civoid Assembler::CodeComment::Close() {
3341cb0ef41Sopenharmony_ci  assembler_->comment_depth_--;
3351cb0ef41Sopenharmony_ci  std::string comment = "]";
3361cb0ef41Sopenharmony_ci  comment.insert(0, depth() * kIndentWidth, ' ');
3371cb0ef41Sopenharmony_ci  DCHECK_LE(0, depth());
3381cb0ef41Sopenharmony_ci  assembler_->RecordComment(comment);
3391cb0ef41Sopenharmony_ci}
3401cb0ef41Sopenharmony_ci#endif
3411cb0ef41Sopenharmony_ci
3421cb0ef41Sopenharmony_ci}  // namespace internal
3431cb0ef41Sopenharmony_ci}  // namespace v8
344