11cb0ef41Sopenharmony_ci// Copyright 2022 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#ifndef V8_SNAPSHOT_EMBEDDED_EMBEDDED_DATA_INL_H_
61cb0ef41Sopenharmony_ci#define V8_SNAPSHOT_EMBEDDED_EMBEDDED_DATA_INL_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/snapshot/embedded/embedded-data.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cinamespace v8 {
111cb0ef41Sopenharmony_cinamespace internal {
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciAddress EmbeddedData::InstructionStartOfBuiltin(Builtin builtin) const {
141cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
151cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
161cb0ef41Sopenharmony_ci  const uint8_t* result = RawCode() + desc.instruction_offset;
171cb0ef41Sopenharmony_ci  DCHECK_LT(result, code_ + code_size_);
181cb0ef41Sopenharmony_ci  return reinterpret_cast<Address>(result);
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciuint32_t EmbeddedData::InstructionSizeOfBuiltin(Builtin builtin) const {
221cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
231cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
241cb0ef41Sopenharmony_ci  return desc.instruction_length;
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciAddress EmbeddedData::MetadataStartOfBuiltin(Builtin builtin) const {
281cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
291cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
301cb0ef41Sopenharmony_ci  const uint8_t* result = RawMetadata() + desc.metadata_offset;
311cb0ef41Sopenharmony_ci  DCHECK_LE(desc.metadata_offset, data_size_);
321cb0ef41Sopenharmony_ci  return reinterpret_cast<Address>(result);
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciuint32_t EmbeddedData::MetadataSizeOfBuiltin(Builtin builtin) const {
361cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
371cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
381cb0ef41Sopenharmony_ci  return desc.metadata_length;
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciAddress EmbeddedData::SafepointTableStartOf(Builtin builtin) const {
421cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
431cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
441cb0ef41Sopenharmony_ci  const uint8_t* result = RawMetadata() + desc.metadata_offset;
451cb0ef41Sopenharmony_ci  DCHECK_LE(desc.handler_table_offset, data_size_);
461cb0ef41Sopenharmony_ci  return reinterpret_cast<Address>(result);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciuint32_t EmbeddedData::SafepointTableSizeOf(Builtin builtin) const {
501cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
511cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
521cb0ef41Sopenharmony_ci#if V8_EMBEDDED_CONSTANT_POOL
531cb0ef41Sopenharmony_ci  DCHECK_LE(desc.handler_table_offset, desc.constant_pool_offset);
541cb0ef41Sopenharmony_ci#else
551cb0ef41Sopenharmony_ci  DCHECK_LE(desc.handler_table_offset, desc.code_comments_offset_offset);
561cb0ef41Sopenharmony_ci#endif
571cb0ef41Sopenharmony_ci  return desc.handler_table_offset;
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ciAddress EmbeddedData::HandlerTableStartOf(Builtin builtin) const {
611cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
621cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
631cb0ef41Sopenharmony_ci  const uint8_t* result = RawMetadata() + desc.handler_table_offset;
641cb0ef41Sopenharmony_ci  DCHECK_LE(desc.handler_table_offset, data_size_);
651cb0ef41Sopenharmony_ci  return reinterpret_cast<Address>(result);
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciuint32_t EmbeddedData::HandlerTableSizeOf(Builtin builtin) const {
691cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
701cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
711cb0ef41Sopenharmony_ci#if V8_EMBEDDED_CONSTANT_POOL
721cb0ef41Sopenharmony_ci  DCHECK_LE(desc.handler_table_offset, desc.constant_pool_offset);
731cb0ef41Sopenharmony_ci  return desc.constant_pool_offset - desc.handler_table_offset;
741cb0ef41Sopenharmony_ci#else
751cb0ef41Sopenharmony_ci  DCHECK_LE(desc.handler_table_offset, desc.code_comments_offset_offset);
761cb0ef41Sopenharmony_ci  return desc.code_comments_offset_offset - desc.handler_table_offset;
771cb0ef41Sopenharmony_ci#endif
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciAddress EmbeddedData::ConstantPoolStartOf(Builtin builtin) const {
811cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
821cb0ef41Sopenharmony_ci#if V8_EMBEDDED_CONSTANT_POOL
831cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
841cb0ef41Sopenharmony_ci  const uint8_t* result = RawMetadata() + desc.constant_pool_offset;
851cb0ef41Sopenharmony_ci  DCHECK_LE(desc.constant_pool_offset, data_size_);
861cb0ef41Sopenharmony_ci  return reinterpret_cast<Address>(result);
871cb0ef41Sopenharmony_ci#else
881cb0ef41Sopenharmony_ci  return kNullAddress;
891cb0ef41Sopenharmony_ci#endif
901cb0ef41Sopenharmony_ci}
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ciuint32_t EmbeddedData::ConstantPoolSizeOf(Builtin builtin) const {
931cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
941cb0ef41Sopenharmony_ci#if V8_EMBEDDED_CONSTANT_POOL
951cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
961cb0ef41Sopenharmony_ci  DCHECK_LE(desc.constant_pool_offset, desc.code_comments_offset_offset);
971cb0ef41Sopenharmony_ci  return desc.code_comments_offset_offset - desc.constant_pool_offset;
981cb0ef41Sopenharmony_ci#else
991cb0ef41Sopenharmony_ci  return 0;
1001cb0ef41Sopenharmony_ci#endif
1011cb0ef41Sopenharmony_ci}
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ciAddress EmbeddedData::CodeCommentsStartOf(Builtin builtin) const {
1041cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
1051cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
1061cb0ef41Sopenharmony_ci  const uint8_t* result = RawMetadata() + desc.code_comments_offset_offset;
1071cb0ef41Sopenharmony_ci  DCHECK_LE(desc.code_comments_offset_offset, data_size_);
1081cb0ef41Sopenharmony_ci  return reinterpret_cast<Address>(result);
1091cb0ef41Sopenharmony_ci}
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ciuint32_t EmbeddedData::CodeCommentsSizeOf(Builtin builtin) const {
1121cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
1131cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
1141cb0ef41Sopenharmony_ci  DCHECK_LE(desc.code_comments_offset_offset,
1151cb0ef41Sopenharmony_ci            desc.unwinding_info_offset_offset);
1161cb0ef41Sopenharmony_ci  return desc.unwinding_info_offset_offset - desc.code_comments_offset_offset;
1171cb0ef41Sopenharmony_ci}
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ciAddress EmbeddedData::UnwindingInfoStartOf(Builtin builtin) const {
1201cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
1211cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
1221cb0ef41Sopenharmony_ci  const uint8_t* result = RawMetadata() + desc.unwinding_info_offset_offset;
1231cb0ef41Sopenharmony_ci  DCHECK_LE(desc.unwinding_info_offset_offset, data_size_);
1241cb0ef41Sopenharmony_ci  return reinterpret_cast<Address>(result);
1251cb0ef41Sopenharmony_ci}
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ciuint32_t EmbeddedData::UnwindingInfoSizeOf(Builtin builtin) const {
1281cb0ef41Sopenharmony_ci  DCHECK(Builtins::IsBuiltinId(builtin));
1291cb0ef41Sopenharmony_ci  const struct LayoutDescription& desc = LayoutDescription(builtin);
1301cb0ef41Sopenharmony_ci  DCHECK_LE(desc.unwinding_info_offset_offset, desc.metadata_length);
1311cb0ef41Sopenharmony_ci  return desc.metadata_length - desc.unwinding_info_offset_offset;
1321cb0ef41Sopenharmony_ci}
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ciAddress EmbeddedData::InstructionStartOfBytecodeHandlers() const {
1351cb0ef41Sopenharmony_ci  return InstructionStartOfBuiltin(Builtin::kFirstBytecodeHandler);
1361cb0ef41Sopenharmony_ci}
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ciAddress EmbeddedData::InstructionEndOfBytecodeHandlers() const {
1391cb0ef41Sopenharmony_ci  STATIC_ASSERT(static_cast<int>(Builtin::kFirstBytecodeHandler) +
1401cb0ef41Sopenharmony_ci                    kNumberOfBytecodeHandlers +
1411cb0ef41Sopenharmony_ci                    2 * kNumberOfWideBytecodeHandlers ==
1421cb0ef41Sopenharmony_ci                Builtins::kBuiltinCount);
1431cb0ef41Sopenharmony_ci  Builtin lastBytecodeHandler = Builtins::FromInt(Builtins::kBuiltinCount - 1);
1441cb0ef41Sopenharmony_ci  return InstructionStartOfBuiltin(lastBytecodeHandler) +
1451cb0ef41Sopenharmony_ci         InstructionSizeOfBuiltin(lastBytecodeHandler);
1461cb0ef41Sopenharmony_ci}
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci// Padded with kCodeAlignment.
1491cb0ef41Sopenharmony_ci// TODO(v8:11045): Consider removing code alignment.
1501cb0ef41Sopenharmony_ciuint32_t EmbeddedData::PaddedInstructionSizeOfBuiltin(Builtin builtin) const {
1511cb0ef41Sopenharmony_ci  uint32_t size = InstructionSizeOfBuiltin(builtin);
1521cb0ef41Sopenharmony_ci  CHECK_NE(size, 0);
1531cb0ef41Sopenharmony_ci  return PadAndAlignCode(size);
1541cb0ef41Sopenharmony_ci}
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci}  // namespace internal
1571cb0ef41Sopenharmony_ci}  // namespace v8
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci#endif  // V8_SNAPSHOT_EMBEDDED_EMBEDDED_DATA_INL_H_
160