blob: f8282f652994fa10f752049f0efcba04f725db28 [file] [log] [blame]
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
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
63class ExternalReferenceEncoder {
64 public:
65 ExternalReferenceEncoder();
66
67 uint32_t Encode(Address key) const;
68
69 const char* NameOfAddress(Address key) const;
70
71 private:
72 HashMap encodings_;
73 static uint32_t Hash(Address key) {
ager@chromium.org9085a012009-05-11 19:22:57 +000074 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >> 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075 }
76
77 int IndexOf(Address key) const;
78
79 static bool Match(void* key1, void* key2) { return key1 == key2; }
80
81 void Put(Address key, int index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082
83 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084};
85
86
87class ExternalReferenceDecoder {
88 public:
89 ExternalReferenceDecoder();
90 ~ExternalReferenceDecoder();
91
92 Address Decode(uint32_t key) const {
93 if (key == 0) return NULL;
94 return *Lookup(key);
95 }
96
97 private:
98 Address** encodings_;
99
100 Address* Lookup(uint32_t key) const {
101 int type = key >> kReferenceTypeShift;
102 ASSERT(kFirstTypeCode <= type && type < kTypeCodeCount);
103 int id = key & kReferenceIdMask;
104 return &encodings_[type][id];
105 }
106
107 void Put(uint32_t key, Address value) {
108 *Lookup(key) = value;
109 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000110
111 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112};
113
114
ager@chromium.org3811b432009-10-28 14:53:37 +0000115class SnapshotByteSource {
116 public:
117 SnapshotByteSource(const byte* array, int length)
118 : data_(array), length_(length), position_(0) { }
119
120 bool HasMore() { return position_ < length_; }
121
122 int Get() {
123 ASSERT(position_ < length_);
124 return data_[position_++];
125 }
126
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000127 inline void CopyRaw(byte* to, int number_of_bytes);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000128
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000129 inline int GetInt();
ager@chromium.org3811b432009-10-28 14:53:37 +0000130
131 bool AtEOF() {
132 return position_ == length_;
133 }
134
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000135 int position() { return position_; }
136
ager@chromium.org3811b432009-10-28 14:53:37 +0000137 private:
138 const byte* data_;
139 int length_;
140 int position_;
141};
142
143
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000144// It is very common to have a reference to objects at certain offsets in the
145// heap. These offsets have been determined experimentally. We code
146// references to such objects in a single byte that encodes the way the pointer
147// is written (only plain pointers allowed), the space number and the offset.
148// This only works for objects in the first page of a space. Don't use this for
149// things in newspace since it bypasses the write barrier.
150
danno@chromium.org40cb8782011-05-25 07:58:50 +0000151static const int k64 = (sizeof(uintptr_t) - 4) / 4;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000152
153#define COMMON_REFERENCE_PATTERNS(f) \
154 f(kNumberOfSpaces, 2, (11 - k64)) \
155 f((kNumberOfSpaces + 1), 2, 0) \
156 f((kNumberOfSpaces + 2), 2, (142 - 16 * k64)) \
157 f((kNumberOfSpaces + 3), 2, (74 - 15 * k64)) \
158 f((kNumberOfSpaces + 4), 2, 5) \
159 f((kNumberOfSpaces + 5), 1, 135) \
160 f((kNumberOfSpaces + 6), 2, (228 - 39 * k64))
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000161
162#define COMMON_RAW_LENGTHS(f) \
163 f(1, 1) \
164 f(2, 2) \
165 f(3, 3) \
166 f(4, 4) \
167 f(5, 5) \
168 f(6, 6) \
169 f(7, 7) \
170 f(8, 8) \
171 f(9, 12) \
172 f(10, 16) \
173 f(11, 20) \
174 f(12, 24) \
175 f(13, 28) \
176 f(14, 32) \
177 f(15, 36)
178
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000179// The Serializer/Deserializer class is a common superclass for Serializer and
180// Deserializer which is used to store common constants and methods used by
181// both.
182class SerializerDeserializer: public ObjectVisitor {
183 public:
danno@chromium.org40cb8782011-05-25 07:58:50 +0000184 static void Iterate(ObjectVisitor* visitor);
185 static void SetSnapshotCacheSize(int size);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000186
ager@chromium.org3811b432009-10-28 14:53:37 +0000187 protected:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000188 // Where the pointed-to object can be found:
189 enum Where {
190 kNewObject = 0, // Object is next in snapshot.
191 // 1-8 One per space.
192 kRootArray = 0x9, // Object is found in root array.
193 kPartialSnapshotCache = 0xa, // Object is in the cache.
194 kExternalReference = 0xb, // Pointer to an external reference.
195 // 0xc-0xf Free.
196 kBackref = 0x10, // Object is described relative to end.
197 // 0x11-0x18 One per space.
198 // 0x19-0x1f Common backref offsets.
199 kFromStart = 0x20, // Object is described relative to start.
200 // 0x21-0x28 One per space.
201 // 0x29-0x2f Free.
202 // 0x30-0x3f Used by misc tags below.
203 kPointedToMask = 0x3f
ager@chromium.org3811b432009-10-28 14:53:37 +0000204 };
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000205
206 // How to code the pointer to the object.
207 enum HowToCode {
208 kPlain = 0, // Straight pointer.
209 // What this means depends on the architecture:
210 kFromCode = 0x40, // A pointer inlined in code.
211 kHowToCodeMask = 0x40
212 };
213
214 // Where to point within the object.
215 enum WhereToPoint {
216 kStartOfObject = 0,
217 kFirstInstruction = 0x80,
218 kWhereToPointMask = 0x80
219 };
220
221 // Misc.
222 // Raw data to be copied from the snapshot.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000223 static const int kRawData = 0x30;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000224 // Some common raw lengths: 0x31-0x3f
225 // A tag emitted at strategic points in the snapshot to delineate sections.
226 // If the deserializer does not find these at the expected moments then it
227 // is an indication that the snapshot and the VM do not fit together.
228 // Examine the build process for architecture, version or configuration
229 // mismatches.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000230 static const int kSynchronize = 0x70;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000231 // Used for the source code of the natives, which is in the executable, but
232 // is referred to from external strings in the snapshot.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000233 static const int kNativesStringResource = 0x71;
234 static const int kNewPage = 0x72;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000235 // 0x73-0x7f Free.
236 // 0xb0-0xbf Free.
237 // 0xf0-0xff Free.
238
239
danno@chromium.org40cb8782011-05-25 07:58:50 +0000240 static const int kLargeData = LAST_SPACE;
241 static const int kLargeCode = kLargeData + 1;
242 static const int kLargeFixedArray = kLargeCode + 1;
243 static const int kNumberOfSpaces = kLargeFixedArray + 1;
244 static const int kAnyOldSpace = -1;
ager@chromium.org3811b432009-10-28 14:53:37 +0000245
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000246 // A bitmask for getting the space out of an instruction.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000247 static const int kSpaceMask = 15;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000248
danno@chromium.org40cb8782011-05-25 07:58:50 +0000249 static inline bool SpaceIsLarge(int space) { return space >= kLargeData; }
250 static inline bool SpaceIsPaged(int space) {
ager@chromium.org3811b432009-10-28 14:53:37 +0000251 return space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE;
252 }
253};
254
255
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000256int SnapshotByteSource::GetInt() {
257 // A little unwind to catch the really small ints.
258 int snapshot_byte = Get();
259 if ((snapshot_byte & 0x80) == 0) {
260 return snapshot_byte;
261 }
262 int accumulator = (snapshot_byte & 0x7f) << 7;
263 while (true) {
264 snapshot_byte = Get();
265 if ((snapshot_byte & 0x80) == 0) {
266 return accumulator | snapshot_byte;
267 }
268 accumulator = (accumulator | (snapshot_byte & 0x7f)) << 7;
269 }
270 UNREACHABLE();
271 return accumulator;
272}
273
274
275void SnapshotByteSource::CopyRaw(byte* to, int number_of_bytes) {
276 memcpy(to, data_ + position_, number_of_bytes);
277 position_ += number_of_bytes;
278}
279
ager@chromium.org3811b432009-10-28 14:53:37 +0000280
281// A Deserializer reads a snapshot and reconstructs the Object graph it defines.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000282class Deserializer: public SerializerDeserializer {
ager@chromium.org3811b432009-10-28 14:53:37 +0000283 public:
284 // Create a deserializer from a snapshot byte source.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000285 explicit Deserializer(SnapshotByteSource* source);
ager@chromium.org3811b432009-10-28 14:53:37 +0000286
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000287 virtual ~Deserializer();
ager@chromium.org3811b432009-10-28 14:53:37 +0000288
289 // Deserialize the snapshot into an empty heap.
290 void Deserialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000291
292 // Deserialize a single object and the objects reachable from it.
293 void DeserializePartial(Object** root);
294
ager@chromium.org3811b432009-10-28 14:53:37 +0000295#ifdef DEBUG
296 virtual void Synchronize(const char* tag);
297#endif
298
299 private:
300 virtual void VisitPointers(Object** start, Object** end);
301
302 virtual void VisitExternalReferences(Address* start, Address* end) {
303 UNREACHABLE();
304 }
305
306 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {
307 UNREACHABLE();
308 }
309
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000310 void ReadChunk(Object** start, Object** end, int space, Address address);
311 HeapObject* GetAddressFromStart(int space);
312 inline HeapObject* GetAddressFromEnd(int space);
313 Address Allocate(int space_number, Space* space, int size);
314 void ReadObject(int space_number, Space* space, Object** write_back);
ager@chromium.org3811b432009-10-28 14:53:37 +0000315
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000316 // Cached current isolate.
317 Isolate* isolate_;
318
ager@chromium.org3811b432009-10-28 14:53:37 +0000319 // Keep track of the pages in the paged spaces.
320 // (In large object space we are keeping track of individual objects
321 // rather than pages.) In new space we just need the address of the
322 // first object and the others will flow from that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000323 List<Address> pages_[SerializerDeserializer::kNumberOfSpaces];
ager@chromium.org3811b432009-10-28 14:53:37 +0000324
325 SnapshotByteSource* source_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000326 // This is the address of the next object that will be allocated in each
327 // space. It is used to calculate the addresses of back-references.
328 Address high_water_[LAST_SPACE + 1];
329 // This is the address of the most recent object that was allocated. It
330 // is used to set the location of the new page when we encounter a
331 // START_NEW_PAGE_SERIALIZATION tag.
332 Address last_object_address_;
ager@chromium.org3811b432009-10-28 14:53:37 +0000333
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000334 ExternalReferenceDecoder* external_reference_decoder_;
335
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000336 DISALLOW_COPY_AND_ASSIGN(Deserializer);
ager@chromium.org3811b432009-10-28 14:53:37 +0000337};
338
339
340class SnapshotByteSink {
341 public:
342 virtual ~SnapshotByteSink() { }
343 virtual void Put(int byte, const char* description) = 0;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000344 virtual void PutSection(int byte, const char* description) {
345 Put(byte, description);
346 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000347 void PutInt(uintptr_t integer, const char* description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000348 virtual int Position() = 0;
ager@chromium.org3811b432009-10-28 14:53:37 +0000349};
350
351
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000352// Mapping objects to their location after deserialization.
353// This is used during building, but not at runtime by V8.
354class SerializationAddressMapper {
355 public:
356 SerializationAddressMapper()
357 : serialization_map_(new HashMap(&SerializationMatchFun)),
358 no_allocation_(new AssertNoAllocation()) { }
359
360 ~SerializationAddressMapper() {
361 delete serialization_map_;
362 delete no_allocation_;
363 }
364
365 bool IsMapped(HeapObject* obj) {
366 return serialization_map_->Lookup(Key(obj), Hash(obj), false) != NULL;
367 }
368
369 int MappedTo(HeapObject* obj) {
370 ASSERT(IsMapped(obj));
371 return static_cast<int>(reinterpret_cast<intptr_t>(
372 serialization_map_->Lookup(Key(obj), Hash(obj), false)->value));
373 }
374
375 void AddMapping(HeapObject* obj, int to) {
376 ASSERT(!IsMapped(obj));
377 HashMap::Entry* entry =
378 serialization_map_->Lookup(Key(obj), Hash(obj), true);
379 entry->value = Value(to);
380 }
381
382 private:
danno@chromium.org40cb8782011-05-25 07:58:50 +0000383 static bool SerializationMatchFun(void* key1, void* key2) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000384 return key1 == key2;
385 }
386
danno@chromium.org40cb8782011-05-25 07:58:50 +0000387 static uint32_t Hash(HeapObject* obj) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000388 return static_cast<int32_t>(reinterpret_cast<intptr_t>(obj->address()));
389 }
390
danno@chromium.org40cb8782011-05-25 07:58:50 +0000391 static void* Key(HeapObject* obj) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000392 return reinterpret_cast<void*>(obj->address());
393 }
394
danno@chromium.org40cb8782011-05-25 07:58:50 +0000395 static void* Value(int v) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000396 return reinterpret_cast<void*>(v);
397 }
398
399 HashMap* serialization_map_;
400 AssertNoAllocation* no_allocation_;
401 DISALLOW_COPY_AND_ASSIGN(SerializationAddressMapper);
402};
403
404
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000405// There can be only one serializer per V8 process.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000406class Serializer : public SerializerDeserializer {
ager@chromium.org3811b432009-10-28 14:53:37 +0000407 public:
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000408 explicit Serializer(SnapshotByteSink* sink);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000409 ~Serializer();
ager@chromium.org3811b432009-10-28 14:53:37 +0000410 void VisitPointers(Object** start, Object** end);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000411 // You can call this after serialization to find out how much space was used
412 // in each space.
413 int CurrentAllocationAddress(int space) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000414 if (SpaceIsLarge(space)) return large_object_total_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000415 return fullness_[space];
416 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000417
danno@chromium.org40cb8782011-05-25 07:58:50 +0000418 static void Enable() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000419 if (!serialization_enabled_) {
420 ASSERT(!too_late_to_enable_now_);
421 }
422 serialization_enabled_ = true;
423 }
424
danno@chromium.org40cb8782011-05-25 07:58:50 +0000425 static void Disable() { serialization_enabled_ = false; }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000426 // Call this when you have made use of the fact that there is no serialization
427 // going on.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000428 static void TooLateToEnableNow() { too_late_to_enable_now_ = true; }
429 static bool enabled() { return serialization_enabled_; }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000430 SerializationAddressMapper* address_mapper() { return &address_mapper_; }
ager@chromium.org3811b432009-10-28 14:53:37 +0000431#ifdef DEBUG
432 virtual void Synchronize(const char* tag);
433#endif
434
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000435 protected:
danno@chromium.org40cb8782011-05-25 07:58:50 +0000436 static const int kInvalidRootIndex = -1;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000437 virtual int RootIndex(HeapObject* heap_object) = 0;
438 virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) = 0;
439
ager@chromium.org3811b432009-10-28 14:53:37 +0000440 class ObjectSerializer : public ObjectVisitor {
441 public:
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000442 ObjectSerializer(Serializer* serializer,
ager@chromium.org3811b432009-10-28 14:53:37 +0000443 Object* o,
444 SnapshotByteSink* sink,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000445 HowToCode how_to_code,
446 WhereToPoint where_to_point)
ager@chromium.org3811b432009-10-28 14:53:37 +0000447 : serializer_(serializer),
448 object_(HeapObject::cast(o)),
449 sink_(sink),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000450 reference_representation_(how_to_code + where_to_point),
ager@chromium.org3811b432009-10-28 14:53:37 +0000451 bytes_processed_so_far_(0) { }
452 void Serialize();
453 void VisitPointers(Object** start, Object** end);
454 void VisitExternalReferences(Address* start, Address* end);
455 void VisitCodeTarget(RelocInfo* target);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000456 void VisitCodeEntry(Address entry_address);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000457 void VisitGlobalPropertyCell(RelocInfo* rinfo);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000458 void VisitRuntimeEntry(RelocInfo* reloc);
459 // Used for seralizing the external strings that hold the natives source.
460 void VisitExternalAsciiString(
461 v8::String::ExternalAsciiStringResource** resource);
462 // We can't serialize a heap with external two byte strings.
463 void VisitExternalTwoByteString(
464 v8::String::ExternalStringResource** resource) {
465 UNREACHABLE();
466 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000467
468 private:
469 void OutputRawData(Address up_to);
470
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000471 Serializer* serializer_;
ager@chromium.org3811b432009-10-28 14:53:37 +0000472 HeapObject* object_;
473 SnapshotByteSink* sink_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000474 int reference_representation_;
ager@chromium.org3811b432009-10-28 14:53:37 +0000475 int bytes_processed_so_far_;
476 };
477
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000478 virtual void SerializeObject(Object* o,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000479 HowToCode how_to_code,
480 WhereToPoint where_to_point) = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000481 void SerializeReferenceToPreviousObject(
482 int space,
483 int address,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000484 HowToCode how_to_code,
485 WhereToPoint where_to_point);
ager@chromium.org3811b432009-10-28 14:53:37 +0000486 void InitializeAllocators();
487 // This will return the space for an object. If the object is in large
488 // object space it may return kLargeCode or kLargeFixedArray in order
489 // to indicate to the deserializer what kind of large object allocation
490 // to make.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000491 static int SpaceOfObject(HeapObject* object);
ager@chromium.org3811b432009-10-28 14:53:37 +0000492 // This just returns the space of the object. It will return LO_SPACE
493 // for all large objects since you can't check the type of the object
494 // once the map has been used for the serialization address.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000495 static int SpaceOfAlreadySerializedObject(HeapObject* object);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000496 int Allocate(int space, int size, bool* new_page_started);
ager@chromium.org3811b432009-10-28 14:53:37 +0000497 int EncodeExternalReference(Address addr) {
498 return external_reference_encoder_->Encode(addr);
499 }
500
501 // Keep track of the fullness of each space in order to generate
502 // relative addresses for back references. Large objects are
503 // just numbered sequentially since relative addresses make no
504 // sense in large object space.
505 int fullness_[LAST_SPACE + 1];
506 SnapshotByteSink* sink_;
507 int current_root_index_;
508 ExternalReferenceEncoder* external_reference_encoder_;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000509 static bool serialization_enabled_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000510 // Did we already make use of the fact that serialization was not enabled?
danno@chromium.org40cb8782011-05-25 07:58:50 +0000511 static bool too_late_to_enable_now_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000512 int large_object_total_;
513 SerializationAddressMapper address_mapper_;
ager@chromium.org3811b432009-10-28 14:53:37 +0000514
515 friend class ObjectSerializer;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000516 friend class Deserializer;
ager@chromium.org3811b432009-10-28 14:53:37 +0000517
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000518 DISALLOW_COPY_AND_ASSIGN(Serializer);
ager@chromium.org3811b432009-10-28 14:53:37 +0000519};
520
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000521
522class PartialSerializer : public Serializer {
523 public:
524 PartialSerializer(Serializer* startup_snapshot_serializer,
525 SnapshotByteSink* sink)
526 : Serializer(sink),
527 startup_serializer_(startup_snapshot_serializer) {
528 }
529
530 // Serialize the objects reachable from a single object pointer.
531 virtual void Serialize(Object** o);
532 virtual void SerializeObject(Object* o,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000533 HowToCode how_to_code,
534 WhereToPoint where_to_point);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000535
536 protected:
537 virtual int RootIndex(HeapObject* o);
538 virtual int PartialSnapshotCacheIndex(HeapObject* o);
539 virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000540 // Scripts should be referred only through shared function infos. We can't
541 // allow them to be part of the partial snapshot because they contain a
542 // unique ID, and deserializing several partial snapshots containing script
543 // would cause dupes.
544 ASSERT(!o->IsScript());
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000545 return o->IsString() || o->IsSharedFunctionInfo() ||
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000546 o->IsHeapNumber() || o->IsCode() ||
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000547 o->IsSerializedScopeInfo() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000548 o->map() == HEAP->fixed_cow_array_map();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000549 }
550
551 private:
552 Serializer* startup_serializer_;
553 DISALLOW_COPY_AND_ASSIGN(PartialSerializer);
554};
555
556
557class StartupSerializer : public Serializer {
558 public:
559 explicit StartupSerializer(SnapshotByteSink* sink) : Serializer(sink) {
560 // Clear the cache of objects used by the partial snapshot. After the
561 // strong roots have been serialized we can create a partial snapshot
562 // which will repopulate the cache with objects neede by that partial
563 // snapshot.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000564 Isolate::Current()->set_serialize_partial_snapshot_cache_length(0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000565 }
566 // Serialize the current state of the heap. The order is:
567 // 1) Strong references.
568 // 2) Partial snapshot cache.
569 // 3) Weak references (eg the symbol table).
570 virtual void SerializeStrongReferences();
571 virtual void SerializeObject(Object* o,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000572 HowToCode how_to_code,
573 WhereToPoint where_to_point);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000574 void SerializeWeakReferences();
575 void Serialize() {
576 SerializeStrongReferences();
577 SerializeWeakReferences();
578 }
579
580 private:
581 virtual int RootIndex(HeapObject* o) { return kInvalidRootIndex; }
582 virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) {
583 return false;
584 }
585};
586
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000587
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588} } // namespace v8::internal
589
590#endif // V8_SERIALIZE_H_