blob: 645a9af3bf647ded9672abfe47cb042b709601e1 [file] [log] [blame]
Ben Murdochda12d292016-06-02 14:46:10 +01001// Copyright 2016 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_SERIALIZER_COMMON_H_
6#define V8_SNAPSHOT_SERIALIZER_COMMON_H_
7
8#include "src/address-map.h"
9#include "src/external-reference-table.h"
10#include "src/globals.h"
11
12namespace v8 {
13namespace internal {
14
15class Isolate;
16
17class ExternalReferenceEncoder {
18 public:
19 explicit ExternalReferenceEncoder(Isolate* isolate);
20
21 uint32_t Encode(Address key) const;
22
23 const char* NameOfAddress(Isolate* isolate, Address address) const;
24
25 private:
26 static uint32_t Hash(Address key) {
27 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >>
28 kPointerSizeLog2);
29 }
30
31 HashMap* map_;
32
33 DISALLOW_COPY_AND_ASSIGN(ExternalReferenceEncoder);
34};
35
36class HotObjectsList {
37 public:
38 HotObjectsList() : index_(0) {
39 for (int i = 0; i < kSize; i++) circular_queue_[i] = NULL;
40 }
41
42 void Add(HeapObject* object) {
43 circular_queue_[index_] = object;
44 index_ = (index_ + 1) & kSizeMask;
45 }
46
47 HeapObject* Get(int index) {
48 DCHECK_NOT_NULL(circular_queue_[index]);
49 return circular_queue_[index];
50 }
51
52 static const int kNotFound = -1;
53
54 int Find(HeapObject* object) {
55 for (int i = 0; i < kSize; i++) {
56 if (circular_queue_[i] == object) return i;
57 }
58 return kNotFound;
59 }
60
61 static const int kSize = 8;
62
63 private:
64 STATIC_ASSERT(IS_POWER_OF_TWO(kSize));
65 static const int kSizeMask = kSize - 1;
66 HeapObject* circular_queue_[kSize];
67 int index_;
68
69 DISALLOW_COPY_AND_ASSIGN(HotObjectsList);
70};
71
72// The Serializer/Deserializer class is a common superclass for Serializer and
73// Deserializer which is used to store common constants and methods used by
74// both.
75class SerializerDeserializer : public ObjectVisitor {
76 public:
77 static void Iterate(Isolate* isolate, ObjectVisitor* visitor);
78
79 // No reservation for large object space necessary.
80 static const int kNumberOfPreallocatedSpaces = LAST_PAGED_SPACE + 1;
81 static const int kNumberOfSpaces = LAST_SPACE + 1;
82
83 protected:
84 static bool CanBeDeferred(HeapObject* o);
85
86 // ---------- byte code range 0x00..0x7f ----------
87 // Byte codes in this range represent Where, HowToCode and WhereToPoint.
88 // Where the pointed-to object can be found:
89 // The static assert below will trigger when the number of preallocated spaces
90 // changed. If that happens, update the bytecode ranges in the comments below.
91 STATIC_ASSERT(5 == kNumberOfSpaces);
92 enum Where {
93 // 0x00..0x04 Allocate new object, in specified space.
94 kNewObject = 0,
95 // 0x05 Unused (including 0x25, 0x45, 0x65).
96 // 0x06 Unused (including 0x26, 0x46, 0x66).
97 // 0x07 Unused (including 0x27, 0x47, 0x67).
98 // 0x08..0x0c Reference to previous object from space.
99 kBackref = 0x08,
100 // 0x0d Unused (including 0x2d, 0x4d, 0x6d).
101 // 0x0e Unused (including 0x2e, 0x4e, 0x6e).
102 // 0x0f Unused (including 0x2f, 0x4f, 0x6f).
103 // 0x10..0x14 Reference to previous object from space after skip.
104 kBackrefWithSkip = 0x10,
105 // 0x15 Unused (including 0x35, 0x55, 0x75).
106 // 0x16 Unused (including 0x36, 0x56, 0x76).
107 // 0x17 Misc (including 0x37, 0x57, 0x77).
108 // 0x18 Root array item.
109 kRootArray = 0x18,
110 // 0x19 Object in the partial snapshot cache.
111 kPartialSnapshotCache = 0x19,
112 // 0x1a External reference referenced by id.
113 kExternalReference = 0x1a,
114 // 0x1b Object provided in the attached list.
115 kAttachedReference = 0x1b,
116 // 0x1c Builtin code referenced by index.
117 kBuiltin = 0x1c
118 // 0x1d..0x1f Misc (including 0x3d..0x3f, 0x5d..0x5f, 0x7d..0x7f)
119 };
120
121 static const int kWhereMask = 0x1f;
122 static const int kSpaceMask = 7;
123 STATIC_ASSERT(kNumberOfSpaces <= kSpaceMask + 1);
124
125 // How to code the pointer to the object.
126 enum HowToCode {
127 // Straight pointer.
128 kPlain = 0,
129 // A pointer inlined in code. What this means depends on the architecture.
130 kFromCode = 0x20
131 };
132
133 static const int kHowToCodeMask = 0x20;
134
135 // Where to point within the object.
136 enum WhereToPoint {
137 // Points to start of object
138 kStartOfObject = 0,
139 // Points to instruction in code object or payload of cell.
140 kInnerPointer = 0x40
141 };
142
143 static const int kWhereToPointMask = 0x40;
144
145 // ---------- Misc ----------
146 // Skip.
147 static const int kSkip = 0x1d;
148 // Internal reference encoded as offsets of pc and target from code entry.
149 static const int kInternalReference = 0x1e;
150 static const int kInternalReferenceEncoded = 0x1f;
151 // Do nothing, used for padding.
152 static const int kNop = 0x3d;
153 // Move to next reserved chunk.
154 static const int kNextChunk = 0x3e;
155 // Deferring object content.
156 static const int kDeferred = 0x3f;
157 // Used for the source code of the natives, which is in the executable, but
158 // is referred to from external strings in the snapshot.
159 static const int kNativesStringResource = 0x5d;
160 // Used for the source code for compiled stubs, which is in the executable,
161 // but is referred to from external strings in the snapshot.
162 static const int kExtraNativesStringResource = 0x5e;
163 // A tag emitted at strategic points in the snapshot to delineate sections.
164 // If the deserializer does not find these at the expected moments then it
165 // is an indication that the snapshot and the VM do not fit together.
166 // Examine the build process for architecture, version or configuration
167 // mismatches.
168 static const int kSynchronize = 0x17;
169 // Repeats of variable length.
170 static const int kVariableRepeat = 0x37;
171 // Raw data of variable length.
172 static const int kVariableRawData = 0x57;
173 // Alignment prefixes 0x7d..0x7f
174 static const int kAlignmentPrefix = 0x7d;
175
176 // 0x77 unused
177
178 // ---------- byte code range 0x80..0xff ----------
179 // First 32 root array items.
180 static const int kNumberOfRootArrayConstants = 0x20;
181 // 0x80..0x9f
182 static const int kRootArrayConstants = 0x80;
183 // 0xa0..0xbf
184 static const int kRootArrayConstantsWithSkip = 0xa0;
185 static const int kRootArrayConstantsMask = 0x1f;
186
187 // 8 hot (recently seen or back-referenced) objects with optional skip.
188 static const int kNumberOfHotObjects = 0x08;
189 // 0xc0..0xc7
190 static const int kHotObject = 0xc0;
191 // 0xc8..0xcf
192 static const int kHotObjectWithSkip = 0xc8;
193 static const int kHotObjectMask = 0x07;
194
195 // 32 common raw data lengths.
196 static const int kNumberOfFixedRawData = 0x20;
197 // 0xd0..0xef
198 static const int kFixedRawData = 0xd0;
199 static const int kOnePointerRawData = kFixedRawData;
200 static const int kFixedRawDataStart = kFixedRawData - 1;
201
202 // 16 repeats lengths.
203 static const int kNumberOfFixedRepeat = 0x10;
204 // 0xf0..0xff
205 static const int kFixedRepeat = 0xf0;
206 static const int kFixedRepeatStart = kFixedRepeat - 1;
207
208 // ---------- special values ----------
209 static const int kAnyOldSpace = -1;
210
211 // Sentinel after a new object to indicate that double alignment is needed.
212 static const int kDoubleAlignmentSentinel = 0;
213
214 // Used as index for the attached reference representing the source object.
215 static const int kSourceObjectReference = 0;
216
217 // Used as index for the attached reference representing the global proxy.
218 static const int kGlobalProxyReference = 0;
219
220 // ---------- member variable ----------
221 HotObjectsList hot_objects_;
222};
223
224class SerializedData {
225 public:
226 class Reservation {
227 public:
228 explicit Reservation(uint32_t size)
229 : reservation_(ChunkSizeBits::encode(size)) {}
230
231 uint32_t chunk_size() const { return ChunkSizeBits::decode(reservation_); }
232 bool is_last() const { return IsLastChunkBits::decode(reservation_); }
233
234 void mark_as_last() { reservation_ |= IsLastChunkBits::encode(true); }
235
236 private:
237 uint32_t reservation_;
238 };
239
240 SerializedData(byte* data, int size)
241 : data_(data), size_(size), owns_data_(false) {}
242 SerializedData() : data_(NULL), size_(0), owns_data_(false) {}
243
244 ~SerializedData() {
245 if (owns_data_) DeleteArray<byte>(data_);
246 }
247
248 uint32_t GetMagicNumber() const { return GetHeaderValue(kMagicNumberOffset); }
249
250 class ChunkSizeBits : public BitField<uint32_t, 0, 31> {};
251 class IsLastChunkBits : public BitField<bool, 31, 1> {};
252
253 static uint32_t ComputeMagicNumber(ExternalReferenceTable* table) {
254 uint32_t external_refs = table->size();
255 return 0xC0DE0000 ^ external_refs;
256 }
257
258 protected:
259 void SetHeaderValue(int offset, uint32_t value) {
260 uint32_t* address = reinterpret_cast<uint32_t*>(data_ + offset);
261 memcpy(reinterpret_cast<uint32_t*>(address), &value, sizeof(value));
262 }
263
264 uint32_t GetHeaderValue(int offset) const {
265 uint32_t value;
266 memcpy(&value, reinterpret_cast<int*>(data_ + offset), sizeof(value));
267 return value;
268 }
269
270 void AllocateData(int size);
271
272 static uint32_t ComputeMagicNumber(Isolate* isolate) {
273 return ComputeMagicNumber(ExternalReferenceTable::instance(isolate));
274 }
275
276 void SetMagicNumber(Isolate* isolate) {
277 SetHeaderValue(kMagicNumberOffset, ComputeMagicNumber(isolate));
278 }
279
280 static const int kMagicNumberOffset = 0;
281
282 byte* data_;
283 int size_;
284 bool owns_data_;
285};
286
287} // namespace internal
288} // namespace v8
289
290#endif // V8_SNAPSHOT_SERIALIZER_COMMON_H_