1 // Copyright 2022 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 #ifndef V8_SNAPSHOT_EMBEDDED_EMBEDDED_DATA_INL_H_
6 #define V8_SNAPSHOT_EMBEDDED_EMBEDDED_DATA_INL_H_
7
8 #include "src/snapshot/embedded/embedded-data.h"
9
10 namespace v8 {
11 namespace internal {
12
InstructionStartOfBuiltin(Builtin builtin)13 Address EmbeddedData::InstructionStartOfBuiltin(Builtin builtin) const {
14 DCHECK(Builtins::IsBuiltinId(builtin));
15 const struct LayoutDescription& desc = LayoutDescription(builtin);
16 const uint8_t* result = RawCode() + desc.instruction_offset;
17 DCHECK_LT(result, code_ + code_size_);
18 return reinterpret_cast<Address>(result);
19 }
20
InstructionSizeOfBuiltin(Builtin builtin)21 uint32_t EmbeddedData::InstructionSizeOfBuiltin(Builtin builtin) const {
22 DCHECK(Builtins::IsBuiltinId(builtin));
23 const struct LayoutDescription& desc = LayoutDescription(builtin);
24 return desc.instruction_length;
25 }
26
MetadataStartOfBuiltin(Builtin builtin)27 Address EmbeddedData::MetadataStartOfBuiltin(Builtin builtin) const {
28 DCHECK(Builtins::IsBuiltinId(builtin));
29 const struct LayoutDescription& desc = LayoutDescription(builtin);
30 const uint8_t* result = RawMetadata() + desc.metadata_offset;
31 DCHECK_LE(desc.metadata_offset, data_size_);
32 return reinterpret_cast<Address>(result);
33 }
34
MetadataSizeOfBuiltin(Builtin builtin)35 uint32_t EmbeddedData::MetadataSizeOfBuiltin(Builtin builtin) const {
36 DCHECK(Builtins::IsBuiltinId(builtin));
37 const struct LayoutDescription& desc = LayoutDescription(builtin);
38 return desc.metadata_length;
39 }
40
SafepointTableStartOf(Builtin builtin)41 Address EmbeddedData::SafepointTableStartOf(Builtin builtin) const {
42 DCHECK(Builtins::IsBuiltinId(builtin));
43 const struct LayoutDescription& desc = LayoutDescription(builtin);
44 const uint8_t* result = RawMetadata() + desc.metadata_offset;
45 DCHECK_LE(desc.handler_table_offset, data_size_);
46 return reinterpret_cast<Address>(result);
47 }
48
SafepointTableSizeOf(Builtin builtin)49 uint32_t EmbeddedData::SafepointTableSizeOf(Builtin builtin) const {
50 DCHECK(Builtins::IsBuiltinId(builtin));
51 const struct LayoutDescription& desc = LayoutDescription(builtin);
52 #if V8_EMBEDDED_CONSTANT_POOL
53 DCHECK_LE(desc.handler_table_offset, desc.constant_pool_offset);
54 #else
55 DCHECK_LE(desc.handler_table_offset, desc.code_comments_offset_offset);
56 #endif
57 return desc.handler_table_offset;
58 }
59
HandlerTableStartOf(Builtin builtin)60 Address EmbeddedData::HandlerTableStartOf(Builtin builtin) const {
61 DCHECK(Builtins::IsBuiltinId(builtin));
62 const struct LayoutDescription& desc = LayoutDescription(builtin);
63 const uint8_t* result = RawMetadata() + desc.handler_table_offset;
64 DCHECK_LE(desc.handler_table_offset, data_size_);
65 return reinterpret_cast<Address>(result);
66 }
67
HandlerTableSizeOf(Builtin builtin)68 uint32_t EmbeddedData::HandlerTableSizeOf(Builtin builtin) const {
69 DCHECK(Builtins::IsBuiltinId(builtin));
70 const struct LayoutDescription& desc = LayoutDescription(builtin);
71 #if V8_EMBEDDED_CONSTANT_POOL
72 DCHECK_LE(desc.handler_table_offset, desc.constant_pool_offset);
73 return desc.constant_pool_offset - desc.handler_table_offset;
74 #else
75 DCHECK_LE(desc.handler_table_offset, desc.code_comments_offset_offset);
76 return desc.code_comments_offset_offset - desc.handler_table_offset;
77 #endif
78 }
79
ConstantPoolStartOf(Builtin builtin)80 Address EmbeddedData::ConstantPoolStartOf(Builtin builtin) const {
81 DCHECK(Builtins::IsBuiltinId(builtin));
82 #if V8_EMBEDDED_CONSTANT_POOL
83 const struct LayoutDescription& desc = LayoutDescription(builtin);
84 const uint8_t* result = RawMetadata() + desc.constant_pool_offset;
85 DCHECK_LE(desc.constant_pool_offset, data_size_);
86 return reinterpret_cast<Address>(result);
87 #else
88 return kNullAddress;
89 #endif
90 }
91
ConstantPoolSizeOf(Builtin builtin)92 uint32_t EmbeddedData::ConstantPoolSizeOf(Builtin builtin) const {
93 DCHECK(Builtins::IsBuiltinId(builtin));
94 #if V8_EMBEDDED_CONSTANT_POOL
95 const struct LayoutDescription& desc = LayoutDescription(builtin);
96 DCHECK_LE(desc.constant_pool_offset, desc.code_comments_offset_offset);
97 return desc.code_comments_offset_offset - desc.constant_pool_offset;
98 #else
99 return 0;
100 #endif
101 }
102
CodeCommentsStartOf(Builtin builtin)103 Address EmbeddedData::CodeCommentsStartOf(Builtin builtin) const {
104 DCHECK(Builtins::IsBuiltinId(builtin));
105 const struct LayoutDescription& desc = LayoutDescription(builtin);
106 const uint8_t* result = RawMetadata() + desc.code_comments_offset_offset;
107 DCHECK_LE(desc.code_comments_offset_offset, data_size_);
108 return reinterpret_cast<Address>(result);
109 }
110
CodeCommentsSizeOf(Builtin builtin)111 uint32_t EmbeddedData::CodeCommentsSizeOf(Builtin builtin) const {
112 DCHECK(Builtins::IsBuiltinId(builtin));
113 const struct LayoutDescription& desc = LayoutDescription(builtin);
114 DCHECK_LE(desc.code_comments_offset_offset,
115 desc.unwinding_info_offset_offset);
116 return desc.unwinding_info_offset_offset - desc.code_comments_offset_offset;
117 }
118
UnwindingInfoStartOf(Builtin builtin)119 Address EmbeddedData::UnwindingInfoStartOf(Builtin builtin) const {
120 DCHECK(Builtins::IsBuiltinId(builtin));
121 const struct LayoutDescription& desc = LayoutDescription(builtin);
122 const uint8_t* result = RawMetadata() + desc.unwinding_info_offset_offset;
123 DCHECK_LE(desc.unwinding_info_offset_offset, data_size_);
124 return reinterpret_cast<Address>(result);
125 }
126
UnwindingInfoSizeOf(Builtin builtin)127 uint32_t EmbeddedData::UnwindingInfoSizeOf(Builtin builtin) const {
128 DCHECK(Builtins::IsBuiltinId(builtin));
129 const struct LayoutDescription& desc = LayoutDescription(builtin);
130 DCHECK_LE(desc.unwinding_info_offset_offset, desc.metadata_length);
131 return desc.metadata_length - desc.unwinding_info_offset_offset;
132 }
133
InstructionStartOfBytecodeHandlers()134 Address EmbeddedData::InstructionStartOfBytecodeHandlers() const {
135 return InstructionStartOfBuiltin(Builtin::kFirstBytecodeHandler);
136 }
137
InstructionEndOfBytecodeHandlers()138 Address EmbeddedData::InstructionEndOfBytecodeHandlers() const {
139 STATIC_ASSERT(static_cast<int>(Builtin::kFirstBytecodeHandler) +
140 kNumberOfBytecodeHandlers +
141 2 * kNumberOfWideBytecodeHandlers ==
142 Builtins::kBuiltinCount);
143 Builtin lastBytecodeHandler = Builtins::FromInt(Builtins::kBuiltinCount - 1);
144 return InstructionStartOfBuiltin(lastBytecodeHandler) +
145 InstructionSizeOfBuiltin(lastBytecodeHandler);
146 }
147
148 // Padded with kCodeAlignment.
149 // TODO(v8:11045): Consider removing code alignment.
PaddedInstructionSizeOfBuiltin(Builtin builtin)150 uint32_t EmbeddedData::PaddedInstructionSizeOfBuiltin(Builtin builtin) const {
151 uint32_t size = InstructionSizeOfBuiltin(builtin);
152 CHECK_NE(size, 0);
153 return PadAndAlignCode(size);
154 }
155
156 } // namespace internal
157 } // namespace v8
158
159 #endif // V8_SNAPSHOT_EMBEDDED_EMBEDDED_DATA_INL_H_
160