blob: 6b85893910fc10dfdfc41a581916c242a6894e2d [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// 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#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "execution.h"
33#include "global-handles.h"
34#include "ic-inl.h"
35#include "natives.h"
36#include "platform.h"
37#include "runtime.h"
38#include "serialize.h"
39#include "stub-cache.h"
40#include "v8threads.h"
Steve Block3ce2e202009-11-05 08:53:23 +000041#include "top.h"
Steve Blockd0582a62009-12-15 09:54:21 +000042#include "bootstrapper.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44namespace v8 {
45namespace internal {
46
Steve Blocka7e24c12009-10-30 11:49:00 +000047
48// -----------------------------------------------------------------------------
49// Coding of external references.
50
51// The encoding of an external reference. The type is in the high word.
52// The id is in the low word.
53static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
54 return static_cast<uint32_t>(type) << 16 | id;
55}
56
57
58static int* GetInternalPointer(StatsCounter* counter) {
59 // All counters refer to dummy_counter, if deserializing happens without
60 // setting up counters.
61 static int dummy_counter = 0;
62 return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
63}
64
65
66// ExternalReferenceTable is a helper class that defines the relationship
67// between external references and their encodings. It is used to build
68// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
69class ExternalReferenceTable {
70 public:
71 static ExternalReferenceTable* instance() {
72 if (!instance_) instance_ = new ExternalReferenceTable();
73 return instance_;
74 }
75
76 int size() const { return refs_.length(); }
77
78 Address address(int i) { return refs_[i].address; }
79
80 uint32_t code(int i) { return refs_[i].code; }
81
82 const char* name(int i) { return refs_[i].name; }
83
84 int max_id(int code) { return max_id_[code]; }
85
86 private:
87 static ExternalReferenceTable* instance_;
88
89 ExternalReferenceTable() : refs_(64) { PopulateTable(); }
90 ~ExternalReferenceTable() { }
91
92 struct ExternalReferenceEntry {
93 Address address;
94 uint32_t code;
95 const char* name;
96 };
97
98 void PopulateTable();
99
100 // For a few types of references, we can get their address from their id.
101 void AddFromId(TypeCode type, uint16_t id, const char* name);
102
103 // For other types of references, the caller will figure out the address.
104 void Add(Address address, TypeCode type, uint16_t id, const char* name);
105
106 List<ExternalReferenceEntry> refs_;
107 int max_id_[kTypeCodeCount];
108};
109
110
111ExternalReferenceTable* ExternalReferenceTable::instance_ = NULL;
112
113
114void ExternalReferenceTable::AddFromId(TypeCode type,
115 uint16_t id,
116 const char* name) {
117 Address address;
118 switch (type) {
119 case C_BUILTIN: {
120 ExternalReference ref(static_cast<Builtins::CFunctionId>(id));
121 address = ref.address();
122 break;
123 }
124 case BUILTIN: {
125 ExternalReference ref(static_cast<Builtins::Name>(id));
126 address = ref.address();
127 break;
128 }
129 case RUNTIME_FUNCTION: {
130 ExternalReference ref(static_cast<Runtime::FunctionId>(id));
131 address = ref.address();
132 break;
133 }
134 case IC_UTILITY: {
135 ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)));
136 address = ref.address();
137 break;
138 }
139 default:
140 UNREACHABLE();
141 return;
142 }
143 Add(address, type, id, name);
144}
145
146
147void ExternalReferenceTable::Add(Address address,
148 TypeCode type,
149 uint16_t id,
150 const char* name) {
Steve Blockd0582a62009-12-15 09:54:21 +0000151 ASSERT_NE(NULL, address);
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 ExternalReferenceEntry entry;
153 entry.address = address;
154 entry.code = EncodeExternal(type, id);
155 entry.name = name;
Steve Blockd0582a62009-12-15 09:54:21 +0000156 ASSERT_NE(0, entry.code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 refs_.Add(entry);
158 if (id > max_id_[type]) max_id_[type] = id;
159}
160
161
162void ExternalReferenceTable::PopulateTable() {
163 for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
164 max_id_[type_code] = 0;
165 }
166
167 // The following populates all of the different type of external references
168 // into the ExternalReferenceTable.
169 //
170 // NOTE: This function was originally 100k of code. It has since been
171 // rewritten to be mostly table driven, as the callback macro style tends to
172 // very easily cause code bloat. Please be careful in the future when adding
173 // new references.
174
175 struct RefTableEntry {
176 TypeCode type;
177 uint16_t id;
178 const char* name;
179 };
180
181 static const RefTableEntry ref_table[] = {
182 // Builtins
Leon Clarkee46be812010-01-19 14:06:41 +0000183#define DEF_ENTRY_C(name, ignored) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000184 { C_BUILTIN, \
185 Builtins::c_##name, \
186 "Builtins::" #name },
187
188 BUILTIN_LIST_C(DEF_ENTRY_C)
189#undef DEF_ENTRY_C
190
Leon Clarkee46be812010-01-19 14:06:41 +0000191#define DEF_ENTRY_C(name, ignored) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 { BUILTIN, \
193 Builtins::name, \
194 "Builtins::" #name },
Leon Clarkee46be812010-01-19 14:06:41 +0000195#define DEF_ENTRY_A(name, kind, state) DEF_ENTRY_C(name, ignored)
Steve Blocka7e24c12009-10-30 11:49:00 +0000196
197 BUILTIN_LIST_C(DEF_ENTRY_C)
198 BUILTIN_LIST_A(DEF_ENTRY_A)
199 BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
200#undef DEF_ENTRY_C
201#undef DEF_ENTRY_A
202
203 // Runtime functions
204#define RUNTIME_ENTRY(name, nargs, ressize) \
205 { RUNTIME_FUNCTION, \
206 Runtime::k##name, \
207 "Runtime::" #name },
208
209 RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
210#undef RUNTIME_ENTRY
211
212 // IC utilities
213#define IC_ENTRY(name) \
214 { IC_UTILITY, \
215 IC::k##name, \
216 "IC::" #name },
217
218 IC_UTIL_LIST(IC_ENTRY)
219#undef IC_ENTRY
220 }; // end of ref_table[].
221
222 for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
223 AddFromId(ref_table[i].type, ref_table[i].id, ref_table[i].name);
224 }
225
226#ifdef ENABLE_DEBUGGER_SUPPORT
227 // Debug addresses
228 Add(Debug_Address(Debug::k_after_break_target_address).address(),
229 DEBUG_ADDRESS,
230 Debug::k_after_break_target_address << kDebugIdShift,
231 "Debug::after_break_target_address()");
232 Add(Debug_Address(Debug::k_debug_break_return_address).address(),
233 DEBUG_ADDRESS,
234 Debug::k_debug_break_return_address << kDebugIdShift,
235 "Debug::debug_break_return_address()");
236 const char* debug_register_format = "Debug::register_address(%i)";
Steve Blockd0582a62009-12-15 09:54:21 +0000237 int dr_format_length = StrLength(debug_register_format);
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 for (int i = 0; i < kNumJSCallerSaved; ++i) {
239 Vector<char> name = Vector<char>::New(dr_format_length + 1);
240 OS::SNPrintF(name, debug_register_format, i);
241 Add(Debug_Address(Debug::k_register_address, i).address(),
242 DEBUG_ADDRESS,
243 Debug::k_register_address << kDebugIdShift | i,
244 name.start());
245 }
246#endif
247
248 // Stat counters
249 struct StatsRefTableEntry {
250 StatsCounter* counter;
251 uint16_t id;
252 const char* name;
253 };
254
255 static const StatsRefTableEntry stats_ref_table[] = {
256#define COUNTER_ENTRY(name, caption) \
257 { &Counters::name, \
258 Counters::k_##name, \
259 "Counters::" #name },
260
261 STATS_COUNTER_LIST_1(COUNTER_ENTRY)
262 STATS_COUNTER_LIST_2(COUNTER_ENTRY)
263#undef COUNTER_ENTRY
264 }; // end of stats_ref_table[].
265
266 for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
267 Add(reinterpret_cast<Address>(
268 GetInternalPointer(stats_ref_table[i].counter)),
269 STATS_COUNTER,
270 stats_ref_table[i].id,
271 stats_ref_table[i].name);
272 }
273
274 // Top addresses
Steve Block3ce2e202009-11-05 08:53:23 +0000275 const char* top_address_format = "Top::%s";
276
277 const char* AddressNames[] = {
278#define C(name) #name,
279 TOP_ADDRESS_LIST(C)
280 TOP_ADDRESS_LIST_PROF(C)
281 NULL
282#undef C
283 };
284
Steve Blockd0582a62009-12-15 09:54:21 +0000285 int top_format_length = StrLength(top_address_format) - 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 for (uint16_t i = 0; i < Top::k_top_address_count; ++i) {
Steve Block3ce2e202009-11-05 08:53:23 +0000287 const char* address_name = AddressNames[i];
288 Vector<char> name =
Steve Blockd0582a62009-12-15 09:54:21 +0000289 Vector<char>::New(top_format_length + StrLength(address_name) + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000290 const char* chars = name.start();
Steve Block3ce2e202009-11-05 08:53:23 +0000291 OS::SNPrintF(name, top_address_format, address_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 Add(Top::get_address_from_id((Top::AddressId)i), TOP_ADDRESS, i, chars);
293 }
294
295 // Extensions
296 Add(FUNCTION_ADDR(GCExtension::GC), EXTENSION, 1,
297 "GCExtension::GC");
298
299 // Accessors
300#define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
301 Add((Address)&Accessors::name, \
302 ACCESSOR, \
303 Accessors::k##name, \
304 "Accessors::" #name);
305
306 ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
307#undef ACCESSOR_DESCRIPTOR_DECLARATION
308
309 // Stub cache tables
310 Add(SCTableReference::keyReference(StubCache::kPrimary).address(),
311 STUB_CACHE_TABLE,
312 1,
313 "StubCache::primary_->key");
314 Add(SCTableReference::valueReference(StubCache::kPrimary).address(),
315 STUB_CACHE_TABLE,
316 2,
317 "StubCache::primary_->value");
318 Add(SCTableReference::keyReference(StubCache::kSecondary).address(),
319 STUB_CACHE_TABLE,
320 3,
321 "StubCache::secondary_->key");
322 Add(SCTableReference::valueReference(StubCache::kSecondary).address(),
323 STUB_CACHE_TABLE,
324 4,
325 "StubCache::secondary_->value");
326
327 // Runtime entries
328 Add(ExternalReference::perform_gc_function().address(),
329 RUNTIME_ENTRY,
330 1,
331 "Runtime::PerformGC");
332 Add(ExternalReference::random_positive_smi_function().address(),
333 RUNTIME_ENTRY,
334 2,
335 "V8::RandomPositiveSmi");
336
337 // Miscellaneous
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 Add(ExternalReference::the_hole_value_location().address(),
339 UNCLASSIFIED,
340 2,
341 "Factory::the_hole_value().location()");
342 Add(ExternalReference::roots_address().address(),
343 UNCLASSIFIED,
344 3,
345 "Heap::roots_address()");
Steve Blockd0582a62009-12-15 09:54:21 +0000346 Add(ExternalReference::address_of_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 UNCLASSIFIED,
348 4,
349 "StackGuard::address_of_jslimit()");
Steve Blockd0582a62009-12-15 09:54:21 +0000350 Add(ExternalReference::address_of_real_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 UNCLASSIFIED,
352 5,
Steve Blockd0582a62009-12-15 09:54:21 +0000353 "StackGuard::address_of_real_jslimit()");
354 Add(ExternalReference::address_of_regexp_stack_limit().address(),
355 UNCLASSIFIED,
356 6,
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 "RegExpStack::limit_address()");
358 Add(ExternalReference::new_space_start().address(),
359 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000360 7,
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 "Heap::NewSpaceStart()");
362 Add(ExternalReference::heap_always_allocate_scope_depth().address(),
363 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000364 8,
Steve Blocka7e24c12009-10-30 11:49:00 +0000365 "Heap::always_allocate_scope_depth()");
366 Add(ExternalReference::new_space_allocation_limit_address().address(),
367 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000368 9,
Steve Blocka7e24c12009-10-30 11:49:00 +0000369 "Heap::NewSpaceAllocationLimitAddress()");
370 Add(ExternalReference::new_space_allocation_top_address().address(),
371 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000372 10,
Steve Blocka7e24c12009-10-30 11:49:00 +0000373 "Heap::NewSpaceAllocationTopAddress()");
374#ifdef ENABLE_DEBUGGER_SUPPORT
375 Add(ExternalReference::debug_break().address(),
376 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000377 11,
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 "Debug::Break()");
379 Add(ExternalReference::debug_step_in_fp_address().address(),
380 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000381 12,
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 "Debug::step_in_fp_addr()");
383#endif
384 Add(ExternalReference::double_fp_operation(Token::ADD).address(),
385 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000386 13,
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 "add_two_doubles");
388 Add(ExternalReference::double_fp_operation(Token::SUB).address(),
389 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000390 14,
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 "sub_two_doubles");
392 Add(ExternalReference::double_fp_operation(Token::MUL).address(),
393 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000394 15,
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 "mul_two_doubles");
396 Add(ExternalReference::double_fp_operation(Token::DIV).address(),
397 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000398 16,
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 "div_two_doubles");
400 Add(ExternalReference::double_fp_operation(Token::MOD).address(),
401 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000402 17,
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 "mod_two_doubles");
404 Add(ExternalReference::compare_doubles().address(),
405 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000406 18,
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 "compare_doubles");
408#ifdef V8_NATIVE_REGEXP
409 Add(ExternalReference::re_case_insensitive_compare_uc16().address(),
410 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000411 19,
Steve Blocka7e24c12009-10-30 11:49:00 +0000412 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
413 Add(ExternalReference::re_check_stack_guard_state().address(),
414 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000415 20,
Steve Blocka7e24c12009-10-30 11:49:00 +0000416 "RegExpMacroAssembler*::CheckStackGuardState()");
417 Add(ExternalReference::re_grow_stack().address(),
418 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000419 21,
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 "NativeRegExpMacroAssembler::GrowStack()");
Leon Clarkee46be812010-01-19 14:06:41 +0000421 Add(ExternalReference::re_word_character_map().address(),
422 UNCLASSIFIED,
423 22,
424 "NativeRegExpMacroAssembler::word_character_map");
Steve Blocka7e24c12009-10-30 11:49:00 +0000425#endif
Leon Clarkee46be812010-01-19 14:06:41 +0000426 // Keyed lookup cache.
427 Add(ExternalReference::keyed_lookup_cache_keys().address(),
428 UNCLASSIFIED,
429 23,
430 "KeyedLookupCache::keys()");
431 Add(ExternalReference::keyed_lookup_cache_field_offsets().address(),
432 UNCLASSIFIED,
433 24,
434 "KeyedLookupCache::field_offsets()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000435}
436
437
438ExternalReferenceEncoder::ExternalReferenceEncoder()
439 : encodings_(Match) {
440 ExternalReferenceTable* external_references =
441 ExternalReferenceTable::instance();
442 for (int i = 0; i < external_references->size(); ++i) {
443 Put(external_references->address(i), i);
444 }
445}
446
447
448uint32_t ExternalReferenceEncoder::Encode(Address key) const {
449 int index = IndexOf(key);
450 return index >=0 ? ExternalReferenceTable::instance()->code(index) : 0;
451}
452
453
454const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
455 int index = IndexOf(key);
456 return index >=0 ? ExternalReferenceTable::instance()->name(index) : NULL;
457}
458
459
460int ExternalReferenceEncoder::IndexOf(Address key) const {
461 if (key == NULL) return -1;
462 HashMap::Entry* entry =
463 const_cast<HashMap &>(encodings_).Lookup(key, Hash(key), false);
464 return entry == NULL
465 ? -1
466 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
467}
468
469
470void ExternalReferenceEncoder::Put(Address key, int index) {
471 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
472 entry->value = reinterpret_cast<void *>(index);
473}
474
475
476ExternalReferenceDecoder::ExternalReferenceDecoder()
477 : encodings_(NewArray<Address*>(kTypeCodeCount)) {
478 ExternalReferenceTable* external_references =
479 ExternalReferenceTable::instance();
480 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
481 int max = external_references->max_id(type) + 1;
482 encodings_[type] = NewArray<Address>(max + 1);
483 }
484 for (int i = 0; i < external_references->size(); ++i) {
485 Put(external_references->code(i), external_references->address(i));
486 }
487}
488
489
490ExternalReferenceDecoder::~ExternalReferenceDecoder() {
491 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
492 DeleteArray(encodings_[type]);
493 }
494 DeleteArray(encodings_);
495}
496
497
Steve Blocka7e24c12009-10-30 11:49:00 +0000498bool Serializer::serialization_enabled_ = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000499bool Serializer::too_late_to_enable_now_ = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000500ExternalReferenceDecoder* Deserializer::external_reference_decoder_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000501
502
Leon Clarkee46be812010-01-19 14:06:41 +0000503Deserializer::Deserializer(SnapshotByteSource* source) : source_(source) {
Steve Blockd0582a62009-12-15 09:54:21 +0000504}
505
506
507// This routine both allocates a new object, and also keeps
508// track of where objects have been allocated so that we can
509// fix back references when deserializing.
510Address Deserializer::Allocate(int space_index, Space* space, int size) {
511 Address address;
512 if (!SpaceIsLarge(space_index)) {
513 ASSERT(!SpaceIsPaged(space_index) ||
514 size <= Page::kPageSize - Page::kObjectStartOffset);
515 Object* new_allocation;
516 if (space_index == NEW_SPACE) {
517 new_allocation = reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
518 } else {
519 new_allocation = reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
520 }
521 HeapObject* new_object = HeapObject::cast(new_allocation);
522 ASSERT(!new_object->IsFailure());
523 address = new_object->address();
524 high_water_[space_index] = address + size;
525 } else {
526 ASSERT(SpaceIsLarge(space_index));
527 ASSERT(size > Page::kPageSize - Page::kObjectStartOffset);
528 LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
529 Object* new_allocation;
530 if (space_index == kLargeData) {
531 new_allocation = lo_space->AllocateRaw(size);
532 } else if (space_index == kLargeFixedArray) {
533 new_allocation = lo_space->AllocateRawFixedArray(size);
534 } else {
535 ASSERT_EQ(kLargeCode, space_index);
536 new_allocation = lo_space->AllocateRawCode(size);
537 }
538 ASSERT(!new_allocation->IsFailure());
539 HeapObject* new_object = HeapObject::cast(new_allocation);
540 // Record all large objects in the same space.
541 address = new_object->address();
542 high_water_[LO_SPACE] = address + size;
543 }
544 last_object_address_ = address;
545 return address;
546}
547
548
549// This returns the address of an object that has been described in the
550// snapshot as being offset bytes back in a particular space.
551HeapObject* Deserializer::GetAddressFromEnd(int space) {
552 int offset = source_->GetInt();
553 ASSERT(!SpaceIsLarge(space));
554 offset <<= kObjectAlignmentBits;
555 return HeapObject::FromAddress(high_water_[space] - offset);
556}
557
558
559// This returns the address of an object that has been described in the
560// snapshot as being offset bytes into a particular space.
561HeapObject* Deserializer::GetAddressFromStart(int space) {
562 int offset = source_->GetInt();
563 if (SpaceIsLarge(space)) {
564 // Large spaces have one object per 'page'.
565 return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
566 }
567 offset <<= kObjectAlignmentBits;
568 if (space == NEW_SPACE) {
569 // New space has only one space - numbered 0.
570 return HeapObject::FromAddress(pages_[space][0] + offset);
571 }
572 ASSERT(SpaceIsPaged(space));
Leon Clarkee46be812010-01-19 14:06:41 +0000573 int page_of_pointee = offset >> kPageSizeBits;
Steve Blockd0582a62009-12-15 09:54:21 +0000574 Address object_address = pages_[space][page_of_pointee] +
575 (offset & Page::kPageAlignmentMask);
576 return HeapObject::FromAddress(object_address);
577}
578
579
580void Deserializer::Deserialize() {
581 // Don't GC while deserializing - just expand the heap.
582 AlwaysAllocateScope always_allocate;
583 // Don't use the free lists while deserializing.
584 LinearAllocationScope allocate_linearly;
585 // No active threads.
586 ASSERT_EQ(NULL, ThreadState::FirstInUse());
587 // No active handles.
588 ASSERT(HandleScopeImplementer::instance()->blocks()->is_empty());
Leon Clarke888f6722010-01-27 15:57:47 +0000589 // Make sure the entire partial snapshot cache is traversed, filling it with
590 // valid object pointers.
591 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000592 ASSERT_EQ(NULL, external_reference_decoder_);
593 external_reference_decoder_ = new ExternalReferenceDecoder();
Leon Clarke888f6722010-01-27 15:57:47 +0000594 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
595 Heap::IterateWeakRoots(this, VISIT_ALL);
Leon Clarkee46be812010-01-19 14:06:41 +0000596}
597
598
599void Deserializer::DeserializePartial(Object** root) {
600 // Don't GC while deserializing - just expand the heap.
601 AlwaysAllocateScope always_allocate;
602 // Don't use the free lists while deserializing.
603 LinearAllocationScope allocate_linearly;
604 if (external_reference_decoder_ == NULL) {
605 external_reference_decoder_ = new ExternalReferenceDecoder();
606 }
607 VisitPointer(root);
608}
609
610
Leon Clarke888f6722010-01-27 15:57:47 +0000611Deserializer::~Deserializer() {
612 ASSERT(source_->AtEOF());
Leon Clarkee46be812010-01-19 14:06:41 +0000613 if (external_reference_decoder_ != NULL) {
614 delete external_reference_decoder_;
615 external_reference_decoder_ = NULL;
616 }
Steve Blockd0582a62009-12-15 09:54:21 +0000617}
618
619
620// This is called on the roots. It is the driver of the deserialization
621// process. It is also called on the body of each function.
622void Deserializer::VisitPointers(Object** start, Object** end) {
623 // The space must be new space. Any other space would cause ReadChunk to try
624 // to update the remembered using NULL as the address.
625 ReadChunk(start, end, NEW_SPACE, NULL);
626}
627
628
629// This routine writes the new object into the pointer provided and then
630// returns true if the new object was in young space and false otherwise.
631// The reason for this strange interface is that otherwise the object is
632// written very late, which means the ByteArray map is not set up by the
633// time we need to use it to mark the space at the end of a page free (by
634// making it into a byte array).
635void Deserializer::ReadObject(int space_number,
636 Space* space,
637 Object** write_back) {
638 int size = source_->GetInt() << kObjectAlignmentBits;
639 Address address = Allocate(space_number, space, size);
640 *write_back = HeapObject::FromAddress(address);
641 Object** current = reinterpret_cast<Object**>(address);
642 Object** limit = current + (size >> kPointerSizeLog2);
Leon Clarkee46be812010-01-19 14:06:41 +0000643 if (FLAG_log_snapshot_positions) {
644 LOG(SnapshotPositionEvent(address, source_->position()));
645 }
Steve Blockd0582a62009-12-15 09:54:21 +0000646 ReadChunk(current, limit, space_number, address);
647}
648
649
650#define ONE_CASE_PER_SPACE(base_tag) \
651 case (base_tag) + NEW_SPACE: /* NOLINT */ \
652 case (base_tag) + OLD_POINTER_SPACE: /* NOLINT */ \
653 case (base_tag) + OLD_DATA_SPACE: /* NOLINT */ \
654 case (base_tag) + CODE_SPACE: /* NOLINT */ \
655 case (base_tag) + MAP_SPACE: /* NOLINT */ \
656 case (base_tag) + CELL_SPACE: /* NOLINT */ \
657 case (base_tag) + kLargeData: /* NOLINT */ \
658 case (base_tag) + kLargeCode: /* NOLINT */ \
659 case (base_tag) + kLargeFixedArray: /* NOLINT */
660
661
662void Deserializer::ReadChunk(Object** current,
663 Object** limit,
664 int space,
665 Address address) {
666 while (current < limit) {
667 int data = source_->Get();
668 switch (data) {
669#define RAW_CASE(index, size) \
670 case RAW_DATA_SERIALIZATION + index: { \
671 byte* raw_data_out = reinterpret_cast<byte*>(current); \
672 source_->CopyRaw(raw_data_out, size); \
673 current = reinterpret_cast<Object**>(raw_data_out + size); \
674 break; \
675 }
676 COMMON_RAW_LENGTHS(RAW_CASE)
677#undef RAW_CASE
678 case RAW_DATA_SERIALIZATION: {
679 int size = source_->GetInt();
680 byte* raw_data_out = reinterpret_cast<byte*>(current);
681 source_->CopyRaw(raw_data_out, size);
682 current = reinterpret_cast<Object**>(raw_data_out + size);
683 break;
684 }
685 case OBJECT_SERIALIZATION + NEW_SPACE: {
686 ReadObject(NEW_SPACE, Heap::new_space(), current);
687 if (space != NEW_SPACE) {
688 Heap::RecordWrite(address, static_cast<int>(
689 reinterpret_cast<Address>(current) - address));
690 }
691 current++;
692 break;
693 }
694 case OBJECT_SERIALIZATION + OLD_DATA_SPACE:
695 ReadObject(OLD_DATA_SPACE, Heap::old_data_space(), current++);
696 break;
697 case OBJECT_SERIALIZATION + OLD_POINTER_SPACE:
698 ReadObject(OLD_POINTER_SPACE, Heap::old_pointer_space(), current++);
699 break;
700 case OBJECT_SERIALIZATION + MAP_SPACE:
701 ReadObject(MAP_SPACE, Heap::map_space(), current++);
702 break;
703 case OBJECT_SERIALIZATION + CODE_SPACE:
704 ReadObject(CODE_SPACE, Heap::code_space(), current++);
705 LOG(LogCodeObject(current[-1]));
706 break;
707 case OBJECT_SERIALIZATION + CELL_SPACE:
708 ReadObject(CELL_SPACE, Heap::cell_space(), current++);
709 break;
710 case OBJECT_SERIALIZATION + kLargeData:
711 ReadObject(kLargeData, Heap::lo_space(), current++);
712 break;
713 case OBJECT_SERIALIZATION + kLargeCode:
714 ReadObject(kLargeCode, Heap::lo_space(), current++);
715 LOG(LogCodeObject(current[-1]));
716 break;
717 case OBJECT_SERIALIZATION + kLargeFixedArray:
718 ReadObject(kLargeFixedArray, Heap::lo_space(), current++);
719 break;
720 case CODE_OBJECT_SERIALIZATION + kLargeCode: {
721 Object* new_code_object = NULL;
722 ReadObject(kLargeCode, Heap::lo_space(), &new_code_object);
723 Code* code_object = reinterpret_cast<Code*>(new_code_object);
724 LOG(LogCodeObject(code_object));
725 // Setting a branch/call to another code object from code.
726 Address location_of_branch_data = reinterpret_cast<Address>(current);
727 Assembler::set_target_at(location_of_branch_data,
728 code_object->instruction_start());
729 location_of_branch_data += Assembler::kCallTargetSize;
730 current = reinterpret_cast<Object**>(location_of_branch_data);
731 break;
732 }
733 case CODE_OBJECT_SERIALIZATION + CODE_SPACE: {
734 Object* new_code_object = NULL;
735 ReadObject(CODE_SPACE, Heap::code_space(), &new_code_object);
736 Code* code_object = reinterpret_cast<Code*>(new_code_object);
737 LOG(LogCodeObject(code_object));
738 // Setting a branch/call to another code object from code.
739 Address location_of_branch_data = reinterpret_cast<Address>(current);
740 Assembler::set_target_at(location_of_branch_data,
741 code_object->instruction_start());
742 location_of_branch_data += Assembler::kCallTargetSize;
743 current = reinterpret_cast<Object**>(location_of_branch_data);
744 break;
745 }
746 ONE_CASE_PER_SPACE(BACKREF_SERIALIZATION) {
747 // Write a backreference to an object we unpacked earlier.
748 int backref_space = (data & kSpaceMask);
749 if (backref_space == NEW_SPACE && space != NEW_SPACE) {
750 Heap::RecordWrite(address, static_cast<int>(
751 reinterpret_cast<Address>(current) - address));
752 }
753 *current++ = GetAddressFromEnd(backref_space);
754 break;
755 }
756 ONE_CASE_PER_SPACE(REFERENCE_SERIALIZATION) {
757 // Write a reference to an object we unpacked earlier.
758 int reference_space = (data & kSpaceMask);
759 if (reference_space == NEW_SPACE && space != NEW_SPACE) {
760 Heap::RecordWrite(address, static_cast<int>(
761 reinterpret_cast<Address>(current) - address));
762 }
763 *current++ = GetAddressFromStart(reference_space);
764 break;
765 }
766#define COMMON_REFS_CASE(index, reference_space, address) \
767 case REFERENCE_SERIALIZATION + index: { \
768 ASSERT(SpaceIsPaged(reference_space)); \
769 Address object_address = \
770 pages_[reference_space][0] + (address << kObjectAlignmentBits); \
771 *current++ = HeapObject::FromAddress(object_address); \
772 break; \
773 }
774 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
775#undef COMMON_REFS_CASE
776 ONE_CASE_PER_SPACE(CODE_BACKREF_SERIALIZATION) {
777 int backref_space = (data & kSpaceMask);
778 // Can't use Code::cast because heap is not set up yet and assertions
779 // will fail.
780 Code* code_object =
781 reinterpret_cast<Code*>(GetAddressFromEnd(backref_space));
782 // Setting a branch/call to previously decoded code object from code.
783 Address location_of_branch_data = reinterpret_cast<Address>(current);
784 Assembler::set_target_at(location_of_branch_data,
785 code_object->instruction_start());
786 location_of_branch_data += Assembler::kCallTargetSize;
787 current = reinterpret_cast<Object**>(location_of_branch_data);
788 break;
789 }
790 ONE_CASE_PER_SPACE(CODE_REFERENCE_SERIALIZATION) {
791 int backref_space = (data & kSpaceMask);
792 // Can't use Code::cast because heap is not set up yet and assertions
793 // will fail.
794 Code* code_object =
795 reinterpret_cast<Code*>(GetAddressFromStart(backref_space));
796 // Setting a branch/call to previously decoded code object from code.
797 Address location_of_branch_data = reinterpret_cast<Address>(current);
798 Assembler::set_target_at(location_of_branch_data,
799 code_object->instruction_start());
800 location_of_branch_data += Assembler::kCallTargetSize;
801 current = reinterpret_cast<Object**>(location_of_branch_data);
802 break;
803 }
804 case EXTERNAL_REFERENCE_SERIALIZATION: {
805 int reference_id = source_->GetInt();
806 Address address = external_reference_decoder_->Decode(reference_id);
807 *current++ = reinterpret_cast<Object*>(address);
808 break;
809 }
810 case EXTERNAL_BRANCH_TARGET_SERIALIZATION: {
811 int reference_id = source_->GetInt();
812 Address address = external_reference_decoder_->Decode(reference_id);
813 Address location_of_branch_data = reinterpret_cast<Address>(current);
814 Assembler::set_external_target_at(location_of_branch_data, address);
815 location_of_branch_data += Assembler::kExternalTargetSize;
816 current = reinterpret_cast<Object**>(location_of_branch_data);
817 break;
818 }
819 case START_NEW_PAGE_SERIALIZATION: {
820 int space = source_->Get();
821 pages_[space].Add(last_object_address_);
822 break;
823 }
824 case NATIVES_STRING_RESOURCE: {
825 int index = source_->Get();
826 Vector<const char> source_vector = Natives::GetScriptSource(index);
827 NativesExternalStringResource* resource =
828 new NativesExternalStringResource(source_vector.start());
829 *current++ = reinterpret_cast<Object*>(resource);
830 break;
831 }
Leon Clarkee46be812010-01-19 14:06:41 +0000832 case ROOT_SERIALIZATION: {
833 int root_id = source_->GetInt();
834 *current++ = Heap::roots_address()[root_id];
835 break;
836 }
Leon Clarke888f6722010-01-27 15:57:47 +0000837 case PARTIAL_SNAPSHOT_CACHE_ENTRY: {
838 int cache_index = source_->GetInt();
839 *current++ = partial_snapshot_cache_[cache_index];
840 break;
841 }
842 case SYNCHRONIZE: {
843 // If we get here then that indicates that you have a mismatch between
844 // the number of GC roots when serializing and deserializing.
845 UNREACHABLE();
846 }
Steve Blockd0582a62009-12-15 09:54:21 +0000847 default:
848 UNREACHABLE();
849 }
850 }
851 ASSERT_EQ(current, limit);
852}
853
854
855void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
856 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
857 for (int shift = max_shift; shift > 0; shift -= 7) {
858 if (integer >= static_cast<uintptr_t>(1u) << shift) {
859 Put(((integer >> shift) & 0x7f) | 0x80, "IntPart");
860 }
861 }
862 PutSection(integer & 0x7f, "IntLastPart");
863}
864
Steve Blocka7e24c12009-10-30 11:49:00 +0000865#ifdef DEBUG
Steve Blockd0582a62009-12-15 09:54:21 +0000866
867void Deserializer::Synchronize(const char* tag) {
868 int data = source_->Get();
869 // If this assert fails then that indicates that you have a mismatch between
870 // the number of GC roots when serializing and deserializing.
871 ASSERT_EQ(SYNCHRONIZE, data);
872 do {
873 int character = source_->Get();
874 if (character == 0) break;
875 if (FLAG_debug_serialization) {
876 PrintF("%c", character);
877 }
878 } while (true);
879 if (FLAG_debug_serialization) {
880 PrintF("\n");
881 }
882}
883
Steve Blocka7e24c12009-10-30 11:49:00 +0000884
885void Serializer::Synchronize(const char* tag) {
Steve Blockd0582a62009-12-15 09:54:21 +0000886 sink_->Put(SYNCHRONIZE, tag);
887 int character;
888 do {
889 character = *tag++;
890 sink_->PutSection(character, "TagCharacter");
891 } while (character != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000892}
Steve Blockd0582a62009-12-15 09:54:21 +0000893
Steve Blocka7e24c12009-10-30 11:49:00 +0000894#endif
895
Steve Blockd0582a62009-12-15 09:54:21 +0000896Serializer::Serializer(SnapshotByteSink* sink)
897 : sink_(sink),
898 current_root_index_(0),
Leon Clarkee46be812010-01-19 14:06:41 +0000899 external_reference_encoder_(NULL),
Leon Clarkee46be812010-01-19 14:06:41 +0000900 large_object_total_(0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000901 for (int i = 0; i <= LAST_SPACE; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +0000902 fullness_[i] = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000903 }
904}
905
906
Leon Clarke888f6722010-01-27 15:57:47 +0000907void StartupSerializer::SerializeStrongReferences() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000908 // No active threads.
909 CHECK_EQ(NULL, ThreadState::FirstInUse());
910 // No active or weak handles.
911 CHECK(HandleScopeImplementer::instance()->blocks()->is_empty());
912 CHECK_EQ(0, GlobalHandles::NumberOfWeakHandles());
Steve Blockd0582a62009-12-15 09:54:21 +0000913 CHECK_EQ(NULL, external_reference_encoder_);
914 // We don't support serializing installed extensions.
915 for (RegisteredExtension* ext = RegisteredExtension::first_extension();
916 ext != NULL;
917 ext = ext->next()) {
918 CHECK_NE(v8::INSTALLED, ext->state());
919 }
920 external_reference_encoder_ = new ExternalReferenceEncoder();
Leon Clarke888f6722010-01-27 15:57:47 +0000921 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
Steve Blockd0582a62009-12-15 09:54:21 +0000922 delete external_reference_encoder_;
923 external_reference_encoder_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000924}
925
926
Leon Clarke888f6722010-01-27 15:57:47 +0000927void PartialSerializer::Serialize(Object** object) {
Leon Clarkee46be812010-01-19 14:06:41 +0000928 external_reference_encoder_ = new ExternalReferenceEncoder();
929 this->VisitPointer(object);
Leon Clarke888f6722010-01-27 15:57:47 +0000930
931 // After we have done the partial serialization the partial snapshot cache
932 // will contain some references needed to decode the partial snapshot. We
933 // fill it up with undefineds so it has a predictable length so the
934 // deserialization code doesn't need to know the length.
935 for (int index = partial_snapshot_cache_length_;
936 index < kPartialSnapshotCacheCapacity;
937 index++) {
938 partial_snapshot_cache_[index] = Heap::undefined_value();
939 startup_serializer_->VisitPointer(&partial_snapshot_cache_[index]);
940 }
941 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
942
Leon Clarkee46be812010-01-19 14:06:41 +0000943 delete external_reference_encoder_;
944 external_reference_encoder_ = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000945}
946
947
Steve Blocka7e24c12009-10-30 11:49:00 +0000948void Serializer::VisitPointers(Object** start, Object** end) {
Steve Blockd0582a62009-12-15 09:54:21 +0000949 for (Object** current = start; current < end; current++) {
950 if ((*current)->IsSmi()) {
951 sink_->Put(RAW_DATA_SERIALIZATION, "RawData");
952 sink_->PutInt(kPointerSize, "length");
953 for (int i = 0; i < kPointerSize; i++) {
954 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
955 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000956 } else {
Steve Blockd0582a62009-12-15 09:54:21 +0000957 SerializeObject(*current, TAGGED_REPRESENTATION);
Steve Blocka7e24c12009-10-30 11:49:00 +0000958 }
959 }
960}
961
962
Leon Clarke888f6722010-01-27 15:57:47 +0000963Object* SerializerDeserializer::partial_snapshot_cache_[
964 kPartialSnapshotCacheCapacity];
965int SerializerDeserializer::partial_snapshot_cache_length_ = 0;
966
967
968// This ensures that the partial snapshot cache keeps things alive during GC and
969// tracks their movement. When it is called during serialization of the startup
970// snapshot the partial snapshot is empty, so nothing happens. When the partial
971// (context) snapshot is created, this array is populated with the pointers that
972// the partial snapshot will need. As that happens we emit serialized objects to
973// the startup snapshot that correspond to the elements of this cache array. On
974// deserialization we therefore need to visit the cache array. This fills it up
975// with pointers to deserialized objects.
976void SerializerDeserializer::Iterate(ObjectVisitor *visitor) {
977 visitor->VisitPointers(
978 &partial_snapshot_cache_[0],
979 &partial_snapshot_cache_[partial_snapshot_cache_length_]);
980}
981
982
983// When deserializing we need to set the size of the snapshot cache. This means
984// the root iteration code (above) will iterate over array elements, writing the
985// references to deserialized objects in them.
986void SerializerDeserializer::SetSnapshotCacheSize(int size) {
987 partial_snapshot_cache_length_ = size;
988}
989
990
991int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
992 for (int i = 0; i < partial_snapshot_cache_length_; i++) {
993 Object* entry = partial_snapshot_cache_[i];
994 if (entry == heap_object) return i;
995 }
996 // We didn't find the object in the cache. So we add it to the cache and
997 // then visit the pointer so that it becomes part of the startup snapshot
998 // and we can refer to it from the partial snapshot.
999 int length = partial_snapshot_cache_length_;
1000 CHECK(length < kPartialSnapshotCacheCapacity);
1001 partial_snapshot_cache_[length] = heap_object;
1002 startup_serializer_->VisitPointer(&partial_snapshot_cache_[length]);
1003 // We don't recurse from the startup snapshot generator into the partial
1004 // snapshot generator.
1005 ASSERT(length == partial_snapshot_cache_length_);
1006 return partial_snapshot_cache_length_++;
1007}
1008
1009
1010int PartialSerializer::RootIndex(HeapObject* heap_object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001011 for (int i = 0; i < Heap::kRootListLength; i++) {
1012 Object* root = Heap::roots_address()[i];
1013 if (root == heap_object) return i;
1014 }
1015 return kInvalidRootIndex;
1016}
1017
1018
Leon Clarke888f6722010-01-27 15:57:47 +00001019// Encode the location of an already deserialized object in order to write its
1020// location into a later object. We can encode the location as an offset from
1021// the start of the deserialized objects or as an offset backwards from the
1022// current allocation pointer.
1023void Serializer::SerializeReferenceToPreviousObject(
1024 int space,
1025 int address,
1026 ReferenceRepresentation reference_representation) {
1027 int offset = CurrentAllocationAddress(space) - address;
1028 bool from_start = true;
1029 if (SpaceIsPaged(space)) {
1030 // For paged space it is simple to encode back from current allocation if
1031 // the object is on the same page as the current allocation pointer.
1032 if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1033 (address >> kPageSizeBits)) {
1034 from_start = false;
1035 address = offset;
1036 }
1037 } else if (space == NEW_SPACE) {
1038 // For new space it is always simple to encode back from current allocation.
1039 if (offset < address) {
1040 from_start = false;
1041 address = offset;
1042 }
1043 }
1044 // If we are actually dealing with real offsets (and not a numbering of
1045 // all objects) then we should shift out the bits that are always 0.
1046 if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
1047 // On some architectures references between code objects are encoded
1048 // specially (as relative offsets). Such references have their own
1049 // special tags to simplify the deserializer.
1050 if (reference_representation == CODE_TARGET_REPRESENTATION) {
1051 if (from_start) {
1052 sink_->Put(CODE_REFERENCE_SERIALIZATION + space, "RefCodeSer");
1053 sink_->PutInt(address, "address");
1054 } else {
1055 sink_->Put(CODE_BACKREF_SERIALIZATION + space, "BackRefCodeSer");
1056 sink_->PutInt(address, "address");
1057 }
1058 } else {
1059 // Regular absolute references.
1060 CHECK_EQ(TAGGED_REPRESENTATION, reference_representation);
1061 if (from_start) {
1062 // There are some common offsets that have their own specialized encoding.
1063#define COMMON_REFS_CASE(tag, common_space, common_offset) \
1064 if (space == common_space && address == common_offset) { \
1065 sink_->PutSection(tag + REFERENCE_SERIALIZATION, "RefSer"); \
1066 } else /* NOLINT */
1067 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
1068#undef COMMON_REFS_CASE
1069 { /* NOLINT */
1070 sink_->Put(REFERENCE_SERIALIZATION + space, "RefSer");
1071 sink_->PutInt(address, "address");
1072 }
1073 } else {
1074 sink_->Put(BACKREF_SERIALIZATION + space, "BackRefSer");
1075 sink_->PutInt(address, "address");
1076 }
1077 }
1078}
1079
1080
1081void StartupSerializer::SerializeObject(
Steve Blockd0582a62009-12-15 09:54:21 +00001082 Object* o,
1083 ReferenceRepresentation reference_representation) {
1084 CHECK(o->IsHeapObject());
1085 HeapObject* heap_object = HeapObject::cast(o);
Leon Clarke888f6722010-01-27 15:57:47 +00001086
1087 if (address_mapper_.IsMapped(heap_object)) {
Steve Blockd0582a62009-12-15 09:54:21 +00001088 int space = SpaceOfAlreadySerializedObject(heap_object);
Leon Clarke888f6722010-01-27 15:57:47 +00001089 int address = address_mapper_.MappedTo(heap_object);
1090 SerializeReferenceToPreviousObject(space,
1091 address,
1092 reference_representation);
1093 } else {
1094 // Object has not yet been serialized. Serialize it here.
1095 ObjectSerializer object_serializer(this,
1096 heap_object,
1097 sink_,
1098 reference_representation);
1099 object_serializer.Serialize();
1100 }
1101}
1102
1103
1104void StartupSerializer::SerializeWeakReferences() {
1105 for (int i = partial_snapshot_cache_length_;
1106 i < kPartialSnapshotCacheCapacity;
1107 i++) {
1108 sink_->Put(ROOT_SERIALIZATION, "RootSerialization");
1109 sink_->PutInt(Heap::kUndefinedValueRootIndex, "root_index");
1110 }
1111 Heap::IterateWeakRoots(this, VISIT_ALL);
1112}
1113
1114
1115void PartialSerializer::SerializeObject(
1116 Object* o,
1117 ReferenceRepresentation reference_representation) {
1118 CHECK(o->IsHeapObject());
1119 HeapObject* heap_object = HeapObject::cast(o);
1120
1121 int root_index;
1122 if ((root_index = RootIndex(heap_object)) != kInvalidRootIndex) {
1123 sink_->Put(ROOT_SERIALIZATION, "RootSerialization");
1124 sink_->PutInt(root_index, "root_index");
1125 return;
1126 }
1127
1128 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1129 int cache_index = PartialSnapshotCacheIndex(heap_object);
1130 sink_->Put(PARTIAL_SNAPSHOT_CACHE_ENTRY, "PartialSnapshotCache");
1131 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1132 return;
1133 }
1134
1135 // Pointers from the partial snapshot to the objects in the startup snapshot
1136 // should go through the root array or through the partial snapshot cache.
1137 // If this is not the case you may have to add something to the root array.
1138 ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1139 // All the symbols that the partial snapshot needs should be either in the
1140 // root table or in the partial snapshot cache.
1141 ASSERT(!heap_object->IsSymbol());
1142
1143 if (address_mapper_.IsMapped(heap_object)) {
1144 int space = SpaceOfAlreadySerializedObject(heap_object);
1145 int address = address_mapper_.MappedTo(heap_object);
1146 SerializeReferenceToPreviousObject(space,
1147 address,
1148 reference_representation);
Steve Blockd0582a62009-12-15 09:54:21 +00001149 } else {
1150 // Object has not yet been serialized. Serialize it here.
1151 ObjectSerializer serializer(this,
1152 heap_object,
1153 sink_,
1154 reference_representation);
1155 serializer.Serialize();
1156 }
1157}
1158
1159
Steve Blockd0582a62009-12-15 09:54:21 +00001160void Serializer::ObjectSerializer::Serialize() {
1161 int space = Serializer::SpaceOfObject(object_);
1162 int size = object_->Size();
1163
1164 if (reference_representation_ == TAGGED_REPRESENTATION) {
1165 sink_->Put(OBJECT_SERIALIZATION + space, "ObjectSerialization");
1166 } else {
1167 CHECK_EQ(CODE_TARGET_REPRESENTATION, reference_representation_);
1168 sink_->Put(CODE_OBJECT_SERIALIZATION + space, "ObjectSerialization");
1169 }
1170 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1171
Leon Clarkee46be812010-01-19 14:06:41 +00001172 LOG(SnapshotPositionEvent(object_->address(), sink_->Position()));
1173
Steve Blockd0582a62009-12-15 09:54:21 +00001174 // Mark this object as already serialized.
1175 bool start_new_page;
Leon Clarke888f6722010-01-27 15:57:47 +00001176 int offset = serializer_->Allocate(space, size, &start_new_page);
1177 serializer_->address_mapper()->AddMapping(object_, offset);
Steve Blockd0582a62009-12-15 09:54:21 +00001178 if (start_new_page) {
1179 sink_->Put(START_NEW_PAGE_SERIALIZATION, "NewPage");
1180 sink_->PutSection(space, "NewPageSpace");
1181 }
1182
1183 // Serialize the map (first word of the object).
1184 serializer_->SerializeObject(object_->map(), TAGGED_REPRESENTATION);
1185
1186 // Serialize the rest of the object.
1187 CHECK_EQ(0, bytes_processed_so_far_);
1188 bytes_processed_so_far_ = kPointerSize;
1189 object_->IterateBody(object_->map()->instance_type(), size, this);
1190 OutputRawData(object_->address() + size);
1191}
1192
1193
1194void Serializer::ObjectSerializer::VisitPointers(Object** start,
1195 Object** end) {
1196 Object** current = start;
1197 while (current < end) {
1198 while (current < end && (*current)->IsSmi()) current++;
1199 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1200
1201 while (current < end && !(*current)->IsSmi()) {
1202 serializer_->SerializeObject(*current, TAGGED_REPRESENTATION);
1203 bytes_processed_so_far_ += kPointerSize;
1204 current++;
1205 }
1206 }
1207}
1208
1209
1210void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1211 Address* end) {
1212 Address references_start = reinterpret_cast<Address>(start);
1213 OutputRawData(references_start);
1214
1215 for (Address* current = start; current < end; current++) {
1216 sink_->Put(EXTERNAL_REFERENCE_SERIALIZATION, "ExternalReference");
1217 int reference_id = serializer_->EncodeExternalReference(*current);
1218 sink_->PutInt(reference_id, "reference id");
1219 }
1220 bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1221}
1222
1223
1224void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1225 Address target_start = rinfo->target_address_address();
1226 OutputRawData(target_start);
1227 Address target = rinfo->target_address();
1228 uint32_t encoding = serializer_->EncodeExternalReference(target);
1229 CHECK(target == NULL ? encoding == 0 : encoding != 0);
1230 sink_->Put(EXTERNAL_BRANCH_TARGET_SERIALIZATION, "ExternalReference");
1231 sink_->PutInt(encoding, "reference id");
1232 bytes_processed_so_far_ += Assembler::kExternalTargetSize;
1233}
1234
1235
1236void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1237 CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1238 Address target_start = rinfo->target_address_address();
1239 OutputRawData(target_start);
1240 Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
1241 serializer_->SerializeObject(target, CODE_TARGET_REPRESENTATION);
1242 bytes_processed_so_far_ += Assembler::kCallTargetSize;
1243}
1244
1245
1246void Serializer::ObjectSerializer::VisitExternalAsciiString(
1247 v8::String::ExternalAsciiStringResource** resource_pointer) {
1248 Address references_start = reinterpret_cast<Address>(resource_pointer);
1249 OutputRawData(references_start);
1250 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1251 Object* source = Heap::natives_source_cache()->get(i);
1252 if (!source->IsUndefined()) {
1253 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1254 typedef v8::String::ExternalAsciiStringResource Resource;
1255 Resource* resource = string->resource();
1256 if (resource == *resource_pointer) {
1257 sink_->Put(NATIVES_STRING_RESOURCE, "NativesStringResource");
1258 sink_->PutSection(i, "NativesStringResourceEnd");
1259 bytes_processed_so_far_ += sizeof(resource);
1260 return;
1261 }
1262 }
1263 }
1264 // One of the strings in the natives cache should match the resource. We
1265 // can't serialize any other kinds of external strings.
1266 UNREACHABLE();
1267}
1268
1269
1270void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1271 Address object_start = object_->address();
1272 int up_to_offset = static_cast<int>(up_to - object_start);
1273 int skipped = up_to_offset - bytes_processed_so_far_;
1274 // This assert will fail if the reloc info gives us the target_address_address
1275 // locations in a non-ascending order. Luckily that doesn't happen.
1276 ASSERT(skipped >= 0);
1277 if (skipped != 0) {
1278 Address base = object_start + bytes_processed_so_far_;
1279#define RAW_CASE(index, length) \
1280 if (skipped == length) { \
1281 sink_->PutSection(RAW_DATA_SERIALIZATION + index, "RawDataFixed"); \
1282 } else /* NOLINT */
1283 COMMON_RAW_LENGTHS(RAW_CASE)
1284#undef RAW_CASE
1285 { /* NOLINT */
1286 sink_->Put(RAW_DATA_SERIALIZATION, "RawData");
1287 sink_->PutInt(skipped, "length");
1288 }
1289 for (int i = 0; i < skipped; i++) {
1290 unsigned int data = base[i];
1291 sink_->PutSection(data, "Byte");
1292 }
1293 bytes_processed_so_far_ += skipped;
1294 }
1295}
1296
1297
1298int Serializer::SpaceOfObject(HeapObject* object) {
1299 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1300 AllocationSpace s = static_cast<AllocationSpace>(i);
1301 if (Heap::InSpace(object, s)) {
1302 if (i == LO_SPACE) {
1303 if (object->IsCode()) {
1304 return kLargeCode;
1305 } else if (object->IsFixedArray()) {
1306 return kLargeFixedArray;
1307 } else {
1308 return kLargeData;
1309 }
1310 }
1311 return i;
1312 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001313 }
1314 UNREACHABLE();
Steve Blockd0582a62009-12-15 09:54:21 +00001315 return 0;
1316}
1317
1318
1319int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1320 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1321 AllocationSpace s = static_cast<AllocationSpace>(i);
1322 if (Heap::InSpace(object, s)) {
1323 return i;
1324 }
1325 }
1326 UNREACHABLE();
1327 return 0;
1328}
1329
1330
1331int Serializer::Allocate(int space, int size, bool* new_page) {
1332 CHECK(space >= 0 && space < kNumberOfSpaces);
1333 if (SpaceIsLarge(space)) {
1334 // In large object space we merely number the objects instead of trying to
1335 // determine some sort of address.
1336 *new_page = true;
Leon Clarkee46be812010-01-19 14:06:41 +00001337 large_object_total_ += size;
Steve Blockd0582a62009-12-15 09:54:21 +00001338 return fullness_[LO_SPACE]++;
1339 }
1340 *new_page = false;
1341 if (fullness_[space] == 0) {
1342 *new_page = true;
1343 }
1344 if (SpaceIsPaged(space)) {
1345 // Paged spaces are a little special. We encode their addresses as if the
1346 // pages were all contiguous and each page were filled up in the range
1347 // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1348 // and allocation does not start at offset 0 in the page, but this scheme
1349 // means the deserializer can get the page number quickly by shifting the
1350 // serialized address.
1351 CHECK(IsPowerOf2(Page::kPageSize));
1352 int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1353 CHECK(size <= Page::kObjectAreaSize);
1354 if (used_in_this_page + size > Page::kObjectAreaSize) {
1355 *new_page = true;
1356 fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1357 }
1358 }
1359 int allocation_address = fullness_[space];
1360 fullness_[space] = allocation_address + size;
1361 return allocation_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001362}
1363
1364
1365} } // namespace v8::internal