blob: f50e23eac8a11bb9b64730b09f9b4306413c44e8 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_SERIALIZE_H_
29#define V8_SERIALIZE_H_
30
31#include "hashmap.h"
32
33namespace v8 {
34namespace internal {
35
36// A TypeCode is used to distinguish different kinds of external reference.
37// It is a single bit to make testing for types easy.
38enum TypeCode {
39 UNCLASSIFIED, // One-of-a-kind references.
40 BUILTIN,
41 RUNTIME_FUNCTION,
42 IC_UTILITY,
43 DEBUG_ADDRESS,
44 STATS_COUNTER,
45 TOP_ADDRESS,
46 C_BUILTIN,
47 EXTENSION,
48 ACCESSOR,
49 RUNTIME_ENTRY,
50 STUB_CACHE_TABLE
51};
52
53const int kTypeCodeCount = STUB_CACHE_TABLE + 1;
54const int kFirstTypeCode = UNCLASSIFIED;
55
56const int kReferenceIdBits = 16;
57const int kReferenceIdMask = (1 << kReferenceIdBits) - 1;
58const int kReferenceTypeShift = kReferenceIdBits;
59const int kDebugRegisterBits = 4;
60const int kDebugIdShift = kDebugRegisterBits;
61
62
Ben Murdoch69a99ed2011-11-30 16:03:39 +000063// ExternalReferenceTable is a helper class that defines the relationship
64// between external references and their encodings. It is used to build
65// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
66class ExternalReferenceTable {
67 public:
68 static ExternalReferenceTable* instance(Isolate* isolate);
69
70 ~ExternalReferenceTable() { }
71
72 int size() const { return refs_.length(); }
73
74 Address address(int i) { return refs_[i].address; }
75
76 uint32_t code(int i) { return refs_[i].code; }
77
78 const char* name(int i) { return refs_[i].name; }
79
80 int max_id(int code) { return max_id_[code]; }
81
82 private:
83 explicit ExternalReferenceTable(Isolate* isolate) : refs_(64) {
84 PopulateTable(isolate);
85 }
86
87 struct ExternalReferenceEntry {
88 Address address;
89 uint32_t code;
90 const char* name;
91 };
92
93 void PopulateTable(Isolate* isolate);
94
95 // For a few types of references, we can get their address from their id.
96 void AddFromId(TypeCode type,
97 uint16_t id,
98 const char* name,
99 Isolate* isolate);
100
101 // For other types of references, the caller will figure out the address.
102 void Add(Address address, TypeCode type, uint16_t id, const char* name);
103
104 List<ExternalReferenceEntry> refs_;
105 int max_id_[kTypeCodeCount];
106};
107
108
Steve Blocka7e24c12009-10-30 11:49:00 +0000109class ExternalReferenceEncoder {
110 public:
111 ExternalReferenceEncoder();
112
113 uint32_t Encode(Address key) const;
114
115 const char* NameOfAddress(Address key) const;
116
117 private:
118 HashMap encodings_;
119 static uint32_t Hash(Address key) {
120 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >> 2);
121 }
122
123 int IndexOf(Address key) const;
124
125 static bool Match(void* key1, void* key2) { return key1 == key2; }
126
127 void Put(Address key, int index);
Steve Block44f0eee2011-05-26 01:26:41 +0100128
129 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000130};
131
132
133class ExternalReferenceDecoder {
134 public:
135 ExternalReferenceDecoder();
136 ~ExternalReferenceDecoder();
137
138 Address Decode(uint32_t key) const {
139 if (key == 0) return NULL;
140 return *Lookup(key);
141 }
142
143 private:
144 Address** encodings_;
145
146 Address* Lookup(uint32_t key) const {
147 int type = key >> kReferenceTypeShift;
148 ASSERT(kFirstTypeCode <= type && type < kTypeCodeCount);
149 int id = key & kReferenceIdMask;
150 return &encodings_[type][id];
151 }
152
153 void Put(uint32_t key, Address value) {
154 *Lookup(key) = value;
155 }
Steve Block44f0eee2011-05-26 01:26:41 +0100156
157 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000158};
159
160
Steve Blockd0582a62009-12-15 09:54:21 +0000161class SnapshotByteSource {
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 public:
Steve Blockd0582a62009-12-15 09:54:21 +0000163 SnapshotByteSource(const byte* array, int length)
164 : data_(array), length_(length), position_(0) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
Steve Blockd0582a62009-12-15 09:54:21 +0000166 bool HasMore() { return position_ < length_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000167
Steve Blockd0582a62009-12-15 09:54:21 +0000168 int Get() {
169 ASSERT(position_ < length_);
170 return data_[position_++];
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 }
172
Andrei Popescu31002712010-02-23 13:46:05 +0000173 inline void CopyRaw(byte* to, int number_of_bytes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000174
Andrei Popescu31002712010-02-23 13:46:05 +0000175 inline int GetInt();
Steve Blocka7e24c12009-10-30 11:49:00 +0000176
Steve Blockd0582a62009-12-15 09:54:21 +0000177 bool AtEOF() {
178 return position_ == length_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 }
180
Leon Clarked91b9f72010-01-27 17:25:45 +0000181 int position() { return position_; }
Leon Clarkee46be812010-01-19 14:06:41 +0000182
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 private:
Steve Blockd0582a62009-12-15 09:54:21 +0000184 const byte* data_;
185 int length_;
186 int position_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000187};
188
189
Steve Blockd0582a62009-12-15 09:54:21 +0000190#define COMMON_RAW_LENGTHS(f) \
191 f(1, 1) \
192 f(2, 2) \
193 f(3, 3) \
194 f(4, 4) \
195 f(5, 5) \
196 f(6, 6) \
197 f(7, 7) \
198 f(8, 8) \
199 f(9, 12) \
200 f(10, 16) \
201 f(11, 20) \
202 f(12, 24) \
203 f(13, 28) \
204 f(14, 32) \
205 f(15, 36)
206
Leon Clarked91b9f72010-01-27 17:25:45 +0000207// The Serializer/Deserializer class is a common superclass for Serializer and
208// Deserializer which is used to store common constants and methods used by
209// both.
210class SerializerDeserializer: public ObjectVisitor {
211 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000212 static void Iterate(ObjectVisitor* visitor);
213 static void SetSnapshotCacheSize(int size);
Leon Clarked91b9f72010-01-27 17:25:45 +0000214
Steve Blockd0582a62009-12-15 09:54:21 +0000215 protected:
Leon Clarkef7060e22010-06-03 12:02:55 +0100216 // Where the pointed-to object can be found:
217 enum Where {
218 kNewObject = 0, // Object is next in snapshot.
219 // 1-8 One per space.
220 kRootArray = 0x9, // Object is found in root array.
221 kPartialSnapshotCache = 0xa, // Object is in the cache.
222 kExternalReference = 0xb, // Pointer to an external reference.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100223 kSkip = 0xc, // Skip a pointer sized cell.
224 // 0xd-0xf Free.
Leon Clarkef7060e22010-06-03 12:02:55 +0100225 kBackref = 0x10, // Object is described relative to end.
226 // 0x11-0x18 One per space.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100227 // 0x19-0x1f Free.
Leon Clarkef7060e22010-06-03 12:02:55 +0100228 kFromStart = 0x20, // Object is described relative to start.
229 // 0x21-0x28 One per space.
230 // 0x29-0x2f Free.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100231 // 0x30-0x3f Used by misc. tags below.
Leon Clarkef7060e22010-06-03 12:02:55 +0100232 kPointedToMask = 0x3f
Steve Blockd0582a62009-12-15 09:54:21 +0000233 };
Leon Clarkef7060e22010-06-03 12:02:55 +0100234
235 // How to code the pointer to the object.
236 enum HowToCode {
237 kPlain = 0, // Straight pointer.
238 // What this means depends on the architecture:
239 kFromCode = 0x40, // A pointer inlined in code.
240 kHowToCodeMask = 0x40
241 };
242
243 // Where to point within the object.
244 enum WhereToPoint {
245 kStartOfObject = 0,
246 kFirstInstruction = 0x80,
247 kWhereToPointMask = 0x80
248 };
249
250 // Misc.
251 // Raw data to be copied from the snapshot.
Ben Murdoch257744e2011-11-30 15:57:28 +0000252 static const int kRawData = 0x30;
Leon Clarkef7060e22010-06-03 12:02:55 +0100253 // Some common raw lengths: 0x31-0x3f
254 // A tag emitted at strategic points in the snapshot to delineate sections.
255 // If the deserializer does not find these at the expected moments then it
256 // is an indication that the snapshot and the VM do not fit together.
257 // Examine the build process for architecture, version or configuration
258 // mismatches.
Ben Murdoch257744e2011-11-30 15:57:28 +0000259 static const int kSynchronize = 0x70;
Leon Clarkef7060e22010-06-03 12:02:55 +0100260 // Used for the source code of the natives, which is in the executable, but
261 // is referred to from external strings in the snapshot.
Ben Murdoch257744e2011-11-30 15:57:28 +0000262 static const int kNativesStringResource = 0x71;
263 static const int kNewPage = 0x72;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100264 static const int kRepeat = 0x73;
265 static const int kConstantRepeat = 0x74;
266 // 0x74-0x7f Repeat last word (subtract 0x73 to get the count).
267 static const int kMaxRepeats = 0x7f - 0x73;
268 static int CodeForRepeats(int repeats) {
269 ASSERT(repeats >= 1 && repeats <= kMaxRepeats);
270 return 0x73 + repeats;
271 }
272 static int RepeatsForCode(int byte_code) {
273 ASSERT(byte_code >= kConstantRepeat && byte_code <= 0x7f);
274 return byte_code - 0x73;
275 }
276 static const int kRootArrayLowConstants = 0xb0;
277 // 0xb0-0xbf Things from the first 16 elements of the root array.
278 static const int kRootArrayHighConstants = 0xf0;
279 // 0xf0-0xff Things from the next 16 elements of the root array.
280 static const int kRootArrayNumberOfConstantEncodings = 0x20;
281 static const int kRootArrayNumberOfLowConstantEncodings = 0x10;
282 static int RootArrayConstantFromByteCode(int byte_code) {
283 int constant = (byte_code & 0xf) | ((byte_code & 0x40) >> 2);
284 ASSERT(constant >= 0 && constant < kRootArrayNumberOfConstantEncodings);
285 return constant;
286 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100287
288
Ben Murdoch257744e2011-11-30 15:57:28 +0000289 static const int kLargeData = LAST_SPACE;
290 static const int kLargeCode = kLargeData + 1;
291 static const int kLargeFixedArray = kLargeCode + 1;
292 static const int kNumberOfSpaces = kLargeFixedArray + 1;
293 static const int kAnyOldSpace = -1;
Steve Blockd0582a62009-12-15 09:54:21 +0000294
295 // A bitmask for getting the space out of an instruction.
Ben Murdoch257744e2011-11-30 15:57:28 +0000296 static const int kSpaceMask = 15;
Steve Blockd0582a62009-12-15 09:54:21 +0000297
Ben Murdoch257744e2011-11-30 15:57:28 +0000298 static inline bool SpaceIsLarge(int space) { return space >= kLargeData; }
299 static inline bool SpaceIsPaged(int space) {
Steve Blockd0582a62009-12-15 09:54:21 +0000300 return space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE;
301 }
302};
303
304
Andrei Popescu31002712010-02-23 13:46:05 +0000305int SnapshotByteSource::GetInt() {
306 // A little unwind to catch the really small ints.
307 int snapshot_byte = Get();
308 if ((snapshot_byte & 0x80) == 0) {
309 return snapshot_byte;
310 }
311 int accumulator = (snapshot_byte & 0x7f) << 7;
312 while (true) {
313 snapshot_byte = Get();
314 if ((snapshot_byte & 0x80) == 0) {
315 return accumulator | snapshot_byte;
316 }
317 accumulator = (accumulator | (snapshot_byte & 0x7f)) << 7;
318 }
319 UNREACHABLE();
320 return accumulator;
321}
322
323
324void SnapshotByteSource::CopyRaw(byte* to, int number_of_bytes) {
325 memcpy(to, data_ + position_, number_of_bytes);
326 position_ += number_of_bytes;
327}
328
Steve Blockd0582a62009-12-15 09:54:21 +0000329
Steve Blocka7e24c12009-10-30 11:49:00 +0000330// A Deserializer reads a snapshot and reconstructs the Object graph it defines.
Leon Clarked91b9f72010-01-27 17:25:45 +0000331class Deserializer: public SerializerDeserializer {
Steve Blocka7e24c12009-10-30 11:49:00 +0000332 public:
Steve Blockd0582a62009-12-15 09:54:21 +0000333 // Create a deserializer from a snapshot byte source.
334 explicit Deserializer(SnapshotByteSource* source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000335
Leon Clarked91b9f72010-01-27 17:25:45 +0000336 virtual ~Deserializer();
Steve Blocka7e24c12009-10-30 11:49:00 +0000337
338 // Deserialize the snapshot into an empty heap.
339 void Deserialize();
Leon Clarkee46be812010-01-19 14:06:41 +0000340
341 // Deserialize a single object and the objects reachable from it.
342 void DeserializePartial(Object** root);
343
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 private:
345 virtual void VisitPointers(Object** start, Object** end);
Steve Blocka7e24c12009-10-30 11:49:00 +0000346
Steve Blockd0582a62009-12-15 09:54:21 +0000347 virtual void VisitExternalReferences(Address* start, Address* end) {
348 UNREACHABLE();
349 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000350
Steve Blockd0582a62009-12-15 09:54:21 +0000351 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {
352 UNREACHABLE();
353 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000354
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100355 // Fills in some heap data in an area from start to end (non-inclusive). The
356 // space id is used for the write barrier. The object_address is the address
357 // of the object we are writing into, or NULL if we are not writing into an
358 // object, i.e. if we are writing a series of tagged values that are not on
359 // the heap.
360 void ReadChunk(
361 Object** start, Object** end, int space, Address object_address);
Steve Blockd0582a62009-12-15 09:54:21 +0000362 HeapObject* GetAddressFromStart(int space);
363 inline HeapObject* GetAddressFromEnd(int space);
364 Address Allocate(int space_number, Space* space, int size);
365 void ReadObject(int space_number, Space* space, Object** write_back);
Steve Blocka7e24c12009-10-30 11:49:00 +0000366
Steve Block44f0eee2011-05-26 01:26:41 +0100367 // Cached current isolate.
368 Isolate* isolate_;
369
Steve Blockd0582a62009-12-15 09:54:21 +0000370 // Keep track of the pages in the paged spaces.
371 // (In large object space we are keeping track of individual objects
372 // rather than pages.) In new space we just need the address of the
373 // first object and the others will flow from that.
Leon Clarked91b9f72010-01-27 17:25:45 +0000374 List<Address> pages_[SerializerDeserializer::kNumberOfSpaces];
Steve Blocka7e24c12009-10-30 11:49:00 +0000375
Steve Blockd0582a62009-12-15 09:54:21 +0000376 SnapshotByteSource* source_;
Steve Blockd0582a62009-12-15 09:54:21 +0000377 // This is the address of the next object that will be allocated in each
378 // space. It is used to calculate the addresses of back-references.
379 Address high_water_[LAST_SPACE + 1];
380 // This is the address of the most recent object that was allocated. It
381 // is used to set the location of the new page when we encounter a
382 // START_NEW_PAGE_SERIALIZATION tag.
383 Address last_object_address_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000384
Steve Block44f0eee2011-05-26 01:26:41 +0100385 ExternalReferenceDecoder* external_reference_decoder_;
386
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 DISALLOW_COPY_AND_ASSIGN(Deserializer);
388};
389
Steve Blockd0582a62009-12-15 09:54:21 +0000390
391class SnapshotByteSink {
392 public:
393 virtual ~SnapshotByteSink() { }
394 virtual void Put(int byte, const char* description) = 0;
395 virtual void PutSection(int byte, const char* description) {
396 Put(byte, description);
397 }
398 void PutInt(uintptr_t integer, const char* description);
Leon Clarkee46be812010-01-19 14:06:41 +0000399 virtual int Position() = 0;
Steve Blockd0582a62009-12-15 09:54:21 +0000400};
401
402
Leon Clarked91b9f72010-01-27 17:25:45 +0000403// Mapping objects to their location after deserialization.
404// This is used during building, but not at runtime by V8.
405class SerializationAddressMapper {
406 public:
407 SerializationAddressMapper()
408 : serialization_map_(new HashMap(&SerializationMatchFun)),
409 no_allocation_(new AssertNoAllocation()) { }
410
411 ~SerializationAddressMapper() {
412 delete serialization_map_;
413 delete no_allocation_;
414 }
415
416 bool IsMapped(HeapObject* obj) {
417 return serialization_map_->Lookup(Key(obj), Hash(obj), false) != NULL;
418 }
419
420 int MappedTo(HeapObject* obj) {
421 ASSERT(IsMapped(obj));
422 return static_cast<int>(reinterpret_cast<intptr_t>(
423 serialization_map_->Lookup(Key(obj), Hash(obj), false)->value));
424 }
425
426 void AddMapping(HeapObject* obj, int to) {
427 ASSERT(!IsMapped(obj));
428 HashMap::Entry* entry =
429 serialization_map_->Lookup(Key(obj), Hash(obj), true);
430 entry->value = Value(to);
431 }
432
433 private:
Ben Murdoch257744e2011-11-30 15:57:28 +0000434 static bool SerializationMatchFun(void* key1, void* key2) {
Leon Clarked91b9f72010-01-27 17:25:45 +0000435 return key1 == key2;
436 }
437
Ben Murdoch257744e2011-11-30 15:57:28 +0000438 static uint32_t Hash(HeapObject* obj) {
Leon Clarked91b9f72010-01-27 17:25:45 +0000439 return static_cast<int32_t>(reinterpret_cast<intptr_t>(obj->address()));
440 }
441
Ben Murdoch257744e2011-11-30 15:57:28 +0000442 static void* Key(HeapObject* obj) {
Leon Clarked91b9f72010-01-27 17:25:45 +0000443 return reinterpret_cast<void*>(obj->address());
444 }
445
Ben Murdoch257744e2011-11-30 15:57:28 +0000446 static void* Value(int v) {
Leon Clarked91b9f72010-01-27 17:25:45 +0000447 return reinterpret_cast<void*>(v);
448 }
449
450 HashMap* serialization_map_;
451 AssertNoAllocation* no_allocation_;
452 DISALLOW_COPY_AND_ASSIGN(SerializationAddressMapper);
453};
454
455
Steve Block44f0eee2011-05-26 01:26:41 +0100456// There can be only one serializer per V8 process.
Ben Murdoch257744e2011-11-30 15:57:28 +0000457class Serializer : public SerializerDeserializer {
Steve Blockd0582a62009-12-15 09:54:21 +0000458 public:
459 explicit Serializer(SnapshotByteSink* sink);
Andrei Popescu31002712010-02-23 13:46:05 +0000460 ~Serializer();
Steve Blockd0582a62009-12-15 09:54:21 +0000461 void VisitPointers(Object** start, Object** end);
Leon Clarkee46be812010-01-19 14:06:41 +0000462 // You can call this after serialization to find out how much space was used
463 // in each space.
464 int CurrentAllocationAddress(int space) {
465 if (SpaceIsLarge(space)) return large_object_total_;
466 return fullness_[space];
467 }
Steve Blockd0582a62009-12-15 09:54:21 +0000468
Ben Murdoch257744e2011-11-30 15:57:28 +0000469 static void Enable() {
Steve Blockd0582a62009-12-15 09:54:21 +0000470 if (!serialization_enabled_) {
471 ASSERT(!too_late_to_enable_now_);
472 }
473 serialization_enabled_ = true;
474 }
475
Ben Murdoch257744e2011-11-30 15:57:28 +0000476 static void Disable() { serialization_enabled_ = false; }
Steve Blockd0582a62009-12-15 09:54:21 +0000477 // Call this when you have made use of the fact that there is no serialization
478 // going on.
Ben Murdoch257744e2011-11-30 15:57:28 +0000479 static void TooLateToEnableNow() { too_late_to_enable_now_ = true; }
480 static bool enabled() { return serialization_enabled_; }
Leon Clarked91b9f72010-01-27 17:25:45 +0000481 SerializationAddressMapper* address_mapper() { return &address_mapper_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100482 void PutRoot(
483 int index, HeapObject* object, HowToCode how, WhereToPoint where);
Steve Blockd0582a62009-12-15 09:54:21 +0000484
Leon Clarked91b9f72010-01-27 17:25:45 +0000485 protected:
Ben Murdoch257744e2011-11-30 15:57:28 +0000486 static const int kInvalidRootIndex = -1;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100487
488 int RootIndex(HeapObject* heap_object, HowToCode from);
Leon Clarked91b9f72010-01-27 17:25:45 +0000489 virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100490 intptr_t root_index_wave_front() { return root_index_wave_front_; }
491 void set_root_index_wave_front(intptr_t value) {
492 ASSERT(value >= root_index_wave_front_);
493 root_index_wave_front_ = value;
494 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000495
Steve Blockd0582a62009-12-15 09:54:21 +0000496 class ObjectSerializer : public ObjectVisitor {
497 public:
498 ObjectSerializer(Serializer* serializer,
499 Object* o,
500 SnapshotByteSink* sink,
Leon Clarkef7060e22010-06-03 12:02:55 +0100501 HowToCode how_to_code,
502 WhereToPoint where_to_point)
Steve Blockd0582a62009-12-15 09:54:21 +0000503 : serializer_(serializer),
504 object_(HeapObject::cast(o)),
505 sink_(sink),
Leon Clarkef7060e22010-06-03 12:02:55 +0100506 reference_representation_(how_to_code + where_to_point),
Steve Blockd0582a62009-12-15 09:54:21 +0000507 bytes_processed_so_far_(0) { }
508 void Serialize();
509 void VisitPointers(Object** start, Object** end);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100510 void VisitEmbeddedPointer(RelocInfo* target);
Steve Blockd0582a62009-12-15 09:54:21 +0000511 void VisitExternalReferences(Address* start, Address* end);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100512 void VisitExternalReference(RelocInfo* rinfo);
Steve Blockd0582a62009-12-15 09:54:21 +0000513 void VisitCodeTarget(RelocInfo* target);
Steve Block791712a2010-08-27 10:21:07 +0100514 void VisitCodeEntry(Address entry_address);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100515 void VisitGlobalPropertyCell(RelocInfo* rinfo);
Steve Blockd0582a62009-12-15 09:54:21 +0000516 void VisitRuntimeEntry(RelocInfo* reloc);
517 // Used for seralizing the external strings that hold the natives source.
518 void VisitExternalAsciiString(
519 v8::String::ExternalAsciiStringResource** resource);
520 // We can't serialize a heap with external two byte strings.
521 void VisitExternalTwoByteString(
522 v8::String::ExternalStringResource** resource) {
523 UNREACHABLE();
524 }
525
526 private:
527 void OutputRawData(Address up_to);
528
529 Serializer* serializer_;
530 HeapObject* object_;
531 SnapshotByteSink* sink_;
Leon Clarkef7060e22010-06-03 12:02:55 +0100532 int reference_representation_;
Steve Blockd0582a62009-12-15 09:54:21 +0000533 int bytes_processed_so_far_;
534 };
535
Leon Clarked91b9f72010-01-27 17:25:45 +0000536 virtual void SerializeObject(Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +0100537 HowToCode how_to_code,
538 WhereToPoint where_to_point) = 0;
Leon Clarked91b9f72010-01-27 17:25:45 +0000539 void SerializeReferenceToPreviousObject(
540 int space,
541 int address,
Leon Clarkef7060e22010-06-03 12:02:55 +0100542 HowToCode how_to_code,
543 WhereToPoint where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +0000544 void InitializeAllocators();
545 // This will return the space for an object. If the object is in large
546 // object space it may return kLargeCode or kLargeFixedArray in order
547 // to indicate to the deserializer what kind of large object allocation
548 // to make.
Ben Murdoch257744e2011-11-30 15:57:28 +0000549 static int SpaceOfObject(HeapObject* object);
Steve Blockd0582a62009-12-15 09:54:21 +0000550 // This just returns the space of the object. It will return LO_SPACE
551 // for all large objects since you can't check the type of the object
552 // once the map has been used for the serialization address.
Ben Murdoch257744e2011-11-30 15:57:28 +0000553 static int SpaceOfAlreadySerializedObject(HeapObject* object);
Steve Blockd0582a62009-12-15 09:54:21 +0000554 int Allocate(int space, int size, bool* new_page_started);
Steve Blockd0582a62009-12-15 09:54:21 +0000555 int EncodeExternalReference(Address addr) {
556 return external_reference_encoder_->Encode(addr);
557 }
558
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100559 int SpaceAreaSize(int space);
560
561 Isolate* isolate_;
Steve Blockd0582a62009-12-15 09:54:21 +0000562 // Keep track of the fullness of each space in order to generate
563 // relative addresses for back references. Large objects are
564 // just numbered sequentially since relative addresses make no
565 // sense in large object space.
566 int fullness_[LAST_SPACE + 1];
567 SnapshotByteSink* sink_;
568 int current_root_index_;
569 ExternalReferenceEncoder* external_reference_encoder_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000570 static bool serialization_enabled_;
Steve Blockd0582a62009-12-15 09:54:21 +0000571 // Did we already make use of the fact that serialization was not enabled?
Ben Murdoch257744e2011-11-30 15:57:28 +0000572 static bool too_late_to_enable_now_;
Leon Clarkee46be812010-01-19 14:06:41 +0000573 int large_object_total_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000574 SerializationAddressMapper address_mapper_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100575 intptr_t root_index_wave_front_;
Steve Blockd0582a62009-12-15 09:54:21 +0000576
577 friend class ObjectSerializer;
578 friend class Deserializer;
579
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100580 private:
Steve Blockd0582a62009-12-15 09:54:21 +0000581 DISALLOW_COPY_AND_ASSIGN(Serializer);
582};
583
Leon Clarked91b9f72010-01-27 17:25:45 +0000584
585class PartialSerializer : public Serializer {
586 public:
587 PartialSerializer(Serializer* startup_snapshot_serializer,
588 SnapshotByteSink* sink)
589 : Serializer(sink),
590 startup_serializer_(startup_snapshot_serializer) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100591 set_root_index_wave_front(Heap::kStrongRootListLength);
Leon Clarked91b9f72010-01-27 17:25:45 +0000592 }
593
594 // Serialize the objects reachable from a single object pointer.
595 virtual void Serialize(Object** o);
596 virtual void SerializeObject(Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +0100597 HowToCode how_to_code,
598 WhereToPoint where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +0000599
600 protected:
Leon Clarked91b9f72010-01-27 17:25:45 +0000601 virtual int PartialSnapshotCacheIndex(HeapObject* o);
602 virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) {
Andrei Popescu31002712010-02-23 13:46:05 +0000603 // Scripts should be referred only through shared function infos. We can't
604 // allow them to be part of the partial snapshot because they contain a
605 // unique ID, and deserializing several partial snapshots containing script
606 // would cause dupes.
607 ASSERT(!o->IsScript());
Steve Block6ded16b2010-05-10 14:33:55 +0100608 return o->IsString() || o->IsSharedFunctionInfo() ||
Iain Merrick75681382010-08-19 15:07:18 +0100609 o->IsHeapNumber() || o->IsCode() ||
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100610 o->IsScopeInfo() ||
Steve Block44f0eee2011-05-26 01:26:41 +0100611 o->map() == HEAP->fixed_cow_array_map();
Leon Clarked91b9f72010-01-27 17:25:45 +0000612 }
613
614 private:
615 Serializer* startup_serializer_;
616 DISALLOW_COPY_AND_ASSIGN(PartialSerializer);
617};
618
619
620class StartupSerializer : public Serializer {
621 public:
622 explicit StartupSerializer(SnapshotByteSink* sink) : Serializer(sink) {
623 // Clear the cache of objects used by the partial snapshot. After the
624 // strong roots have been serialized we can create a partial snapshot
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100625 // which will repopulate the cache with objects needed by that partial
Leon Clarked91b9f72010-01-27 17:25:45 +0000626 // snapshot.
Steve Block44f0eee2011-05-26 01:26:41 +0100627 Isolate::Current()->set_serialize_partial_snapshot_cache_length(0);
Leon Clarked91b9f72010-01-27 17:25:45 +0000628 }
629 // Serialize the current state of the heap. The order is:
630 // 1) Strong references.
631 // 2) Partial snapshot cache.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100632 // 3) Weak references (e.g. the symbol table).
Leon Clarked91b9f72010-01-27 17:25:45 +0000633 virtual void SerializeStrongReferences();
634 virtual void SerializeObject(Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +0100635 HowToCode how_to_code,
636 WhereToPoint where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +0000637 void SerializeWeakReferences();
638 void Serialize() {
639 SerializeStrongReferences();
640 SerializeWeakReferences();
641 }
642
643 private:
Leon Clarked91b9f72010-01-27 17:25:45 +0000644 virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) {
645 return false;
646 }
647};
648
Andrei Popescu31002712010-02-23 13:46:05 +0000649
Steve Blocka7e24c12009-10-30 11:49:00 +0000650} } // namespace v8::internal
651
652#endif // V8_SERIALIZE_H_