1 // Copyright 2012 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
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.
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.
30 #include "accessors.h"
32 #include "bootstrapper.h"
33 #include "execution.h"
34 #include "global-handles.h"
39 #include "serialize.h"
40 #include "stub-cache.h"
41 #include "v8threads.h"
47 // -----------------------------------------------------------------------------
48 // Coding of external references.
50 // The encoding of an external reference. The type is in the high word.
51 // The id is in the low word.
52 static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
53 return static_cast<uint32_t>(type) << 16 | id;
57 static int* GetInternalPointer(StatsCounter* counter) {
58 // All counters refer to dummy_counter, if deserializing happens without
59 // setting up counters.
60 static int dummy_counter = 0;
61 return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
65 ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) {
66 ExternalReferenceTable* external_reference_table =
67 isolate->external_reference_table();
68 if (external_reference_table == NULL) {
69 external_reference_table = new ExternalReferenceTable(isolate);
70 isolate->set_external_reference_table(external_reference_table);
72 return external_reference_table;
76 void ExternalReferenceTable::AddFromId(TypeCode type,
83 ExternalReference ref(static_cast<Builtins::CFunctionId>(id), isolate);
84 address = ref.address();
88 ExternalReference ref(static_cast<Builtins::Name>(id), isolate);
89 address = ref.address();
92 case RUNTIME_FUNCTION: {
93 ExternalReference ref(static_cast<Runtime::FunctionId>(id), isolate);
94 address = ref.address();
98 ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)),
100 address = ref.address();
107 Add(address, type, id, name);
111 void ExternalReferenceTable::Add(Address address,
115 ASSERT_NE(NULL, address);
116 ExternalReferenceEntry entry;
117 entry.address = address;
118 entry.code = EncodeExternal(type, id);
120 ASSERT_NE(0, entry.code);
122 if (id > max_id_[type]) max_id_[type] = id;
126 void ExternalReferenceTable::PopulateTable(Isolate* isolate) {
127 for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
128 max_id_[type_code] = 0;
131 // The following populates all of the different type of external references
132 // into the ExternalReferenceTable.
134 // NOTE: This function was originally 100k of code. It has since been
135 // rewritten to be mostly table driven, as the callback macro style tends to
136 // very easily cause code bloat. Please be careful in the future when adding
139 struct RefTableEntry {
145 static const RefTableEntry ref_table[] = {
147 #define DEF_ENTRY_C(name, ignored) \
149 Builtins::c_##name, \
150 "Builtins::" #name },
152 BUILTIN_LIST_C(DEF_ENTRY_C)
155 #define DEF_ENTRY_C(name, ignored) \
158 "Builtins::" #name },
159 #define DEF_ENTRY_A(name, kind, state, extra) DEF_ENTRY_C(name, ignored)
161 BUILTIN_LIST_C(DEF_ENTRY_C)
162 BUILTIN_LIST_A(DEF_ENTRY_A)
163 BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
168 #define RUNTIME_ENTRY(name, nargs, ressize) \
169 { RUNTIME_FUNCTION, \
173 RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
177 #define IC_ENTRY(name) \
182 IC_UTIL_LIST(IC_ENTRY)
184 }; // end of ref_table[].
186 for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
187 AddFromId(ref_table[i].type,
193 #ifdef ENABLE_DEBUGGER_SUPPORT
195 Add(Debug_Address(Debug::k_after_break_target_address).address(isolate),
197 Debug::k_after_break_target_address << kDebugIdShift,
198 "Debug::after_break_target_address()");
199 Add(Debug_Address(Debug::k_debug_break_slot_address).address(isolate),
201 Debug::k_debug_break_slot_address << kDebugIdShift,
202 "Debug::debug_break_slot_address()");
203 Add(Debug_Address(Debug::k_debug_break_return_address).address(isolate),
205 Debug::k_debug_break_return_address << kDebugIdShift,
206 "Debug::debug_break_return_address()");
207 Add(Debug_Address(Debug::k_restarter_frame_function_pointer).address(isolate),
209 Debug::k_restarter_frame_function_pointer << kDebugIdShift,
210 "Debug::restarter_frame_function_pointer_address()");
214 struct StatsRefTableEntry {
215 StatsCounter* (Counters::*counter)();
220 const StatsRefTableEntry stats_ref_table[] = {
221 #define COUNTER_ENTRY(name, caption) \
223 Counters::k_##name, \
224 "Counters::" #name },
226 STATS_COUNTER_LIST_1(COUNTER_ENTRY)
227 STATS_COUNTER_LIST_2(COUNTER_ENTRY)
229 }; // end of stats_ref_table[].
231 Counters* counters = isolate->counters();
232 for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
233 Add(reinterpret_cast<Address>(GetInternalPointer(
234 (counters->*(stats_ref_table[i].counter))())),
236 stats_ref_table[i].id,
237 stats_ref_table[i].name);
242 const char* AddressNames[] = {
243 #define BUILD_NAME_LITERAL(CamelName, hacker_name) \
244 "Isolate::" #hacker_name "_address",
245 FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL)
247 #undef BUILD_NAME_LITERAL
250 for (uint16_t i = 0; i < Isolate::kIsolateAddressCount; ++i) {
251 Add(isolate->get_address_from_id((Isolate::AddressId)i),
252 TOP_ADDRESS, i, AddressNames[i]);
256 #define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
257 Add((Address)&Accessors::name, \
259 Accessors::k##name, \
260 "Accessors::" #name);
262 ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
263 #undef ACCESSOR_DESCRIPTOR_DECLARATION
265 StubCache* stub_cache = isolate->stub_cache();
268 Add(stub_cache->key_reference(StubCache::kPrimary).address(),
271 "StubCache::primary_->key");
272 Add(stub_cache->value_reference(StubCache::kPrimary).address(),
275 "StubCache::primary_->value");
276 Add(stub_cache->map_reference(StubCache::kPrimary).address(),
279 "StubCache::primary_->map");
280 Add(stub_cache->key_reference(StubCache::kSecondary).address(),
283 "StubCache::secondary_->key");
284 Add(stub_cache->value_reference(StubCache::kSecondary).address(),
287 "StubCache::secondary_->value");
288 Add(stub_cache->map_reference(StubCache::kSecondary).address(),
291 "StubCache::secondary_->map");
294 Add(ExternalReference::perform_gc_function(isolate).address(),
297 "Runtime::PerformGC");
298 Add(ExternalReference::fill_heap_number_with_random_function(
302 "V8::FillHeapNumberWithRandom");
303 Add(ExternalReference::random_uint32_function(isolate).address(),
307 Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
310 "HandleScope::DeleteExtensions");
311 Add(ExternalReference::
312 incremental_marking_record_write_function(isolate).address(),
315 "IncrementalMarking::RecordWrite");
316 Add(ExternalReference::store_buffer_overflow_function(isolate).address(),
319 "StoreBuffer::StoreBufferOverflow");
320 Add(ExternalReference::
321 incremental_evacuation_record_write_function(isolate).address(),
324 "IncrementalMarking::RecordWrite");
329 Add(ExternalReference::roots_array_start(isolate).address(),
332 "Heap::roots_array_start()");
333 Add(ExternalReference::address_of_stack_limit(isolate).address(),
336 "StackGuard::address_of_jslimit()");
337 Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
340 "StackGuard::address_of_real_jslimit()");
341 #ifndef V8_INTERPRETED_REGEXP
342 Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
345 "RegExpStack::limit_address()");
346 Add(ExternalReference::address_of_regexp_stack_memory_address(
350 "RegExpStack::memory_address()");
351 Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
354 "RegExpStack::memory_size()");
355 Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
358 "OffsetsVector::static_offsets_vector");
359 #endif // V8_INTERPRETED_REGEXP
360 Add(ExternalReference::new_space_start(isolate).address(),
363 "Heap::NewSpaceStart()");
364 Add(ExternalReference::new_space_mask(isolate).address(),
367 "Heap::NewSpaceMask()");
368 Add(ExternalReference::heap_always_allocate_scope_depth(isolate).address(),
371 "Heap::always_allocate_scope_depth()");
372 Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
375 "Heap::NewSpaceAllocationLimitAddress()");
376 Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
379 "Heap::NewSpaceAllocationTopAddress()");
380 #ifdef ENABLE_DEBUGGER_SUPPORT
381 Add(ExternalReference::debug_break(isolate).address(),
385 Add(ExternalReference::debug_step_in_fp_address(isolate).address(),
388 "Debug::step_in_fp_addr()");
390 Add(ExternalReference::double_fp_operation(Token::ADD, isolate).address(),
394 Add(ExternalReference::double_fp_operation(Token::SUB, isolate).address(),
398 Add(ExternalReference::double_fp_operation(Token::MUL, isolate).address(),
402 Add(ExternalReference::double_fp_operation(Token::DIV, isolate).address(),
406 Add(ExternalReference::double_fp_operation(Token::MOD, isolate).address(),
410 Add(ExternalReference::compare_doubles(isolate).address(),
414 #ifndef V8_INTERPRETED_REGEXP
415 Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
418 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
419 Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
422 "RegExpMacroAssembler*::CheckStackGuardState()");
423 Add(ExternalReference::re_grow_stack(isolate).address(),
426 "NativeRegExpMacroAssembler::GrowStack()");
427 Add(ExternalReference::re_word_character_map().address(),
430 "NativeRegExpMacroAssembler::word_character_map");
431 #endif // V8_INTERPRETED_REGEXP
432 // Keyed lookup cache.
433 Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(),
436 "KeyedLookupCache::keys()");
437 Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(),
440 "KeyedLookupCache::field_offsets()");
441 Add(ExternalReference::transcendental_cache_array_address(isolate).address(),
444 "TranscendentalCache::caches()");
445 Add(ExternalReference::handle_scope_next_address().address(),
448 "HandleScope::next");
449 Add(ExternalReference::handle_scope_limit_address().address(),
452 "HandleScope::limit");
453 Add(ExternalReference::handle_scope_level_address().address(),
456 "HandleScope::level");
457 Add(ExternalReference::new_deoptimizer_function(isolate).address(),
460 "Deoptimizer::New()");
461 Add(ExternalReference::compute_output_frames_function(isolate).address(),
464 "Deoptimizer::ComputeOutputFrames()");
465 Add(ExternalReference::address_of_min_int().address(),
468 "LDoubleConstant::min_int");
469 Add(ExternalReference::address_of_one_half().address(),
472 "LDoubleConstant::one_half");
473 Add(ExternalReference::isolate_address().address(),
477 Add(ExternalReference::address_of_minus_zero().address(),
480 "LDoubleConstant::minus_zero");
481 Add(ExternalReference::address_of_negative_infinity().address(),
484 "LDoubleConstant::negative_infinity");
485 Add(ExternalReference::power_double_double_function(isolate).address(),
488 "power_double_double_function");
489 Add(ExternalReference::power_double_int_function(isolate).address(),
492 "power_double_int_function");
493 Add(ExternalReference::store_buffer_top(isolate).address(),
497 Add(ExternalReference::address_of_canonical_non_hole_nan().address(),
501 Add(ExternalReference::address_of_the_hole_nan().address(),
505 Add(ExternalReference::get_date_field_function(isolate).address(),
509 Add(ExternalReference::date_cache_stamp(isolate).address(),
516 ExternalReferenceEncoder::ExternalReferenceEncoder()
518 isolate_(Isolate::Current()) {
519 ExternalReferenceTable* external_references =
520 ExternalReferenceTable::instance(isolate_);
521 for (int i = 0; i < external_references->size(); ++i) {
522 Put(external_references->address(i), i);
527 uint32_t ExternalReferenceEncoder::Encode(Address key) const {
528 int index = IndexOf(key);
529 ASSERT(key == NULL || index >= 0);
531 ExternalReferenceTable::instance(isolate_)->code(index) : 0;
535 const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
536 int index = IndexOf(key);
538 ExternalReferenceTable::instance(isolate_)->name(index) : NULL;
542 int ExternalReferenceEncoder::IndexOf(Address key) const {
543 if (key == NULL) return -1;
544 HashMap::Entry* entry =
545 const_cast<HashMap&>(encodings_).Lookup(key, Hash(key), false);
548 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
552 void ExternalReferenceEncoder::Put(Address key, int index) {
553 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
554 entry->value = reinterpret_cast<void*>(index);
558 ExternalReferenceDecoder::ExternalReferenceDecoder()
559 : encodings_(NewArray<Address*>(kTypeCodeCount)),
560 isolate_(Isolate::Current()) {
561 ExternalReferenceTable* external_references =
562 ExternalReferenceTable::instance(isolate_);
563 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
564 int max = external_references->max_id(type) + 1;
565 encodings_[type] = NewArray<Address>(max + 1);
567 for (int i = 0; i < external_references->size(); ++i) {
568 Put(external_references->code(i), external_references->address(i));
573 ExternalReferenceDecoder::~ExternalReferenceDecoder() {
574 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
575 DeleteArray(encodings_[type]);
577 DeleteArray(encodings_);
581 bool Serializer::serialization_enabled_ = false;
582 bool Serializer::too_late_to_enable_now_ = false;
585 Deserializer::Deserializer(SnapshotByteSource* source)
588 external_reference_decoder_(NULL) {
592 // This routine both allocates a new object, and also keeps
593 // track of where objects have been allocated so that we can
594 // fix back references when deserializing.
595 Address Deserializer::Allocate(int space_index, Space* space, int size) {
597 if (!SpaceIsLarge(space_index)) {
598 ASSERT(!SpaceIsPaged(space_index) ||
599 size <= Page::kPageSize - Page::kObjectStartOffset);
600 MaybeObject* maybe_new_allocation;
601 if (space_index == NEW_SPACE) {
602 maybe_new_allocation =
603 reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
605 maybe_new_allocation =
606 reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
608 ASSERT(!maybe_new_allocation->IsFailure());
609 Object* new_allocation = maybe_new_allocation->ToObjectUnchecked();
610 HeapObject* new_object = HeapObject::cast(new_allocation);
611 address = new_object->address();
612 high_water_[space_index] = address + size;
614 ASSERT(SpaceIsLarge(space_index));
615 LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
616 Object* new_allocation;
617 if (space_index == kLargeData || space_index == kLargeFixedArray) {
619 lo_space->AllocateRaw(size, NOT_EXECUTABLE)->ToObjectUnchecked();
621 ASSERT_EQ(kLargeCode, space_index);
623 lo_space->AllocateRaw(size, EXECUTABLE)->ToObjectUnchecked();
625 HeapObject* new_object = HeapObject::cast(new_allocation);
626 // Record all large objects in the same space.
627 address = new_object->address();
628 pages_[LO_SPACE].Add(address);
630 last_object_address_ = address;
635 // This returns the address of an object that has been described in the
636 // snapshot as being offset bytes back in a particular space.
637 HeapObject* Deserializer::GetAddressFromEnd(int space) {
638 int offset = source_->GetInt();
639 ASSERT(!SpaceIsLarge(space));
640 offset <<= kObjectAlignmentBits;
641 return HeapObject::FromAddress(high_water_[space] - offset);
645 // This returns the address of an object that has been described in the
646 // snapshot as being offset bytes into a particular space.
647 HeapObject* Deserializer::GetAddressFromStart(int space) {
648 int offset = source_->GetInt();
649 if (SpaceIsLarge(space)) {
650 // Large spaces have one object per 'page'.
651 return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
653 offset <<= kObjectAlignmentBits;
654 if (space == NEW_SPACE) {
655 // New space has only one space - numbered 0.
656 return HeapObject::FromAddress(pages_[space][0] + offset);
658 ASSERT(SpaceIsPaged(space));
659 int page_of_pointee = offset >> kPageSizeBits;
660 Address object_address = pages_[space][page_of_pointee] +
661 (offset & Page::kPageAlignmentMask);
662 return HeapObject::FromAddress(object_address);
666 void Deserializer::Deserialize() {
667 isolate_ = Isolate::Current();
668 ASSERT(isolate_ != NULL);
669 // Don't GC while deserializing - just expand the heap.
670 AlwaysAllocateScope always_allocate;
671 // Don't use the free lists while deserializing.
672 LinearAllocationScope allocate_linearly;
673 // No active threads.
674 ASSERT_EQ(NULL, isolate_->thread_manager()->FirstThreadStateInUse());
675 // No active handles.
676 ASSERT(isolate_->handle_scope_implementer()->blocks()->is_empty());
677 // Make sure the entire partial snapshot cache is traversed, filling it with
678 // valid object pointers.
679 isolate_->set_serialize_partial_snapshot_cache_length(
680 Isolate::kPartialSnapshotCacheCapacity);
681 ASSERT_EQ(NULL, external_reference_decoder_);
682 external_reference_decoder_ = new ExternalReferenceDecoder();
683 isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
684 isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
686 isolate_->heap()->set_global_contexts_list(
687 isolate_->heap()->undefined_value());
689 // Update data pointers to the external strings containing natives sources.
690 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
691 Object* source = isolate_->heap()->natives_source_cache()->get(i);
692 if (!source->IsUndefined()) {
693 ExternalAsciiString::cast(source)->update_data_cache();
699 void Deserializer::DeserializePartial(Object** root) {
700 isolate_ = Isolate::Current();
701 // Don't GC while deserializing - just expand the heap.
702 AlwaysAllocateScope always_allocate;
703 // Don't use the free lists while deserializing.
704 LinearAllocationScope allocate_linearly;
705 if (external_reference_decoder_ == NULL) {
706 external_reference_decoder_ = new ExternalReferenceDecoder();
712 Deserializer::~Deserializer() {
713 ASSERT(source_->AtEOF());
714 if (external_reference_decoder_) {
715 delete external_reference_decoder_;
716 external_reference_decoder_ = NULL;
721 // This is called on the roots. It is the driver of the deserialization
722 // process. It is also called on the body of each function.
723 void Deserializer::VisitPointers(Object** start, Object** end) {
724 // The space must be new space. Any other space would cause ReadChunk to try
725 // to update the remembered using NULL as the address.
726 ReadChunk(start, end, NEW_SPACE, NULL);
730 // This routine writes the new object into the pointer provided and then
731 // returns true if the new object was in young space and false otherwise.
732 // The reason for this strange interface is that otherwise the object is
733 // written very late, which means the FreeSpace map is not set up by the
734 // time we need to use it to mark the space at the end of a page free.
735 void Deserializer::ReadObject(int space_number,
737 Object** write_back) {
738 int size = source_->GetInt() << kObjectAlignmentBits;
739 Address address = Allocate(space_number, space, size);
740 *write_back = HeapObject::FromAddress(address);
741 Object** current = reinterpret_cast<Object**>(address);
742 Object** limit = current + (size >> kPointerSizeLog2);
743 if (FLAG_log_snapshot_positions) {
744 LOG(isolate_, SnapshotPositionEvent(address, source_->position()));
746 ReadChunk(current, limit, space_number, address);
748 bool is_codespace = (space == HEAP->code_space()) ||
749 ((space == HEAP->lo_space()) && (space_number == kLargeCode));
750 ASSERT(HeapObject::FromAddress(address)->IsCode() == is_codespace);
755 // This macro is always used with a constant argument so it should all fold
756 // away to almost nothing in the generated code. It might be nicer to do this
757 // with the ternary operator but there are type issues with that.
758 #define ASSIGN_DEST_SPACE(space_number) \
760 if (space_number == NEW_SPACE) { \
761 dest_space = isolate->heap()->new_space(); \
762 } else if (space_number == OLD_POINTER_SPACE) { \
763 dest_space = isolate->heap()->old_pointer_space(); \
764 } else if (space_number == OLD_DATA_SPACE) { \
765 dest_space = isolate->heap()->old_data_space(); \
766 } else if (space_number == CODE_SPACE) { \
767 dest_space = isolate->heap()->code_space(); \
768 } else if (space_number == MAP_SPACE) { \
769 dest_space = isolate->heap()->map_space(); \
770 } else if (space_number == CELL_SPACE) { \
771 dest_space = isolate->heap()->cell_space(); \
773 ASSERT(space_number >= LO_SPACE); \
774 dest_space = isolate->heap()->lo_space(); \
778 static const int kUnknownOffsetFromStart = -1;
781 void Deserializer::ReadChunk(Object** current,
784 Address current_object_address) {
785 Isolate* const isolate = isolate_;
786 bool write_barrier_needed = (current_object_address != NULL &&
787 source_space != NEW_SPACE &&
788 source_space != CELL_SPACE &&
789 source_space != CODE_SPACE &&
790 source_space != OLD_DATA_SPACE);
791 while (current < limit) {
792 int data = source_->Get();
794 #define CASE_STATEMENT(where, how, within, space_number) \
795 case where + how + within + space_number: \
796 ASSERT((where & ~kPointedToMask) == 0); \
797 ASSERT((how & ~kHowToCodeMask) == 0); \
798 ASSERT((within & ~kWhereToPointMask) == 0); \
799 ASSERT((space_number & ~kSpaceMask) == 0);
801 #define CASE_BODY(where, how, within, space_number_if_any, offset_from_start) \
803 bool emit_write_barrier = false; \
804 bool current_was_incremented = false; \
805 int space_number = space_number_if_any == kAnyOldSpace ? \
806 (data & kSpaceMask) : space_number_if_any; \
807 if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
808 ASSIGN_DEST_SPACE(space_number) \
809 ReadObject(space_number, dest_space, current); \
810 emit_write_barrier = (space_number == NEW_SPACE); \
812 Object* new_object = NULL; /* May not be a real Object pointer. */ \
813 if (where == kNewObject) { \
814 ASSIGN_DEST_SPACE(space_number) \
815 ReadObject(space_number, dest_space, &new_object); \
816 } else if (where == kRootArray) { \
817 int root_id = source_->GetInt(); \
818 new_object = isolate->heap()->roots_array_start()[root_id]; \
819 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
820 } else if (where == kPartialSnapshotCache) { \
821 int cache_index = source_->GetInt(); \
822 new_object = isolate->serialize_partial_snapshot_cache() \
824 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
825 } else if (where == kExternalReference) { \
826 int reference_id = source_->GetInt(); \
827 Address address = external_reference_decoder_-> \
828 Decode(reference_id); \
829 new_object = reinterpret_cast<Object*>(address); \
830 } else if (where == kBackref) { \
831 emit_write_barrier = (space_number == NEW_SPACE); \
832 new_object = GetAddressFromEnd(data & kSpaceMask); \
834 ASSERT(where == kFromStart); \
835 if (offset_from_start == kUnknownOffsetFromStart) { \
836 emit_write_barrier = (space_number == NEW_SPACE); \
837 new_object = GetAddressFromStart(data & kSpaceMask); \
839 Address object_address = pages_[space_number][0] + \
840 (offset_from_start << kObjectAlignmentBits); \
841 new_object = HeapObject::FromAddress(object_address); \
844 if (within == kFirstInstruction) { \
845 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
846 new_object = reinterpret_cast<Object*>( \
847 new_code_object->instruction_start()); \
849 if (how == kFromCode) { \
850 Address location_of_branch_data = \
851 reinterpret_cast<Address>(current); \
852 Assembler::deserialization_set_special_target_at( \
853 location_of_branch_data, \
854 reinterpret_cast<Address>(new_object)); \
855 location_of_branch_data += Assembler::kSpecialTargetSize; \
856 current = reinterpret_cast<Object**>(location_of_branch_data); \
857 current_was_incremented = true; \
859 *current = new_object; \
862 if (emit_write_barrier && write_barrier_needed) { \
863 Address current_address = reinterpret_cast<Address>(current); \
864 isolate->heap()->RecordWrite( \
865 current_object_address, \
866 static_cast<int>(current_address - current_object_address)); \
868 if (!current_was_incremented) { \
874 // This generates a case and a body for each space. The large object spaces are
875 // very rare in snapshots so they are grouped in one body.
876 #define ONE_PER_SPACE(where, how, within) \
877 CASE_STATEMENT(where, how, within, NEW_SPACE) \
878 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
879 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
880 CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart) \
881 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
882 CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart) \
883 CASE_STATEMENT(where, how, within, CODE_SPACE) \
884 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
885 CASE_STATEMENT(where, how, within, CELL_SPACE) \
886 CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart) \
887 CASE_STATEMENT(where, how, within, MAP_SPACE) \
888 CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart) \
889 CASE_STATEMENT(where, how, within, kLargeData) \
890 CASE_STATEMENT(where, how, within, kLargeCode) \
891 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
892 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
894 // This generates a case and a body for the new space (which has to do extra
895 // write barrier handling) and handles the other spaces with 8 fall-through
896 // cases and one body.
897 #define ALL_SPACES(where, how, within) \
898 CASE_STATEMENT(where, how, within, NEW_SPACE) \
899 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
900 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
901 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
902 CASE_STATEMENT(where, how, within, CODE_SPACE) \
903 CASE_STATEMENT(where, how, within, CELL_SPACE) \
904 CASE_STATEMENT(where, how, within, MAP_SPACE) \
905 CASE_STATEMENT(where, how, within, kLargeData) \
906 CASE_STATEMENT(where, how, within, kLargeCode) \
907 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
908 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
910 #define ONE_PER_CODE_SPACE(where, how, within) \
911 CASE_STATEMENT(where, how, within, CODE_SPACE) \
912 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
913 CASE_STATEMENT(where, how, within, kLargeCode) \
914 CASE_BODY(where, how, within, kLargeCode, kUnknownOffsetFromStart)
916 #define FOUR_CASES(byte_code) \
918 case byte_code + 1: \
919 case byte_code + 2: \
922 #define SIXTEEN_CASES(byte_code) \
923 FOUR_CASES(byte_code) \
924 FOUR_CASES(byte_code + 4) \
925 FOUR_CASES(byte_code + 8) \
926 FOUR_CASES(byte_code + 12)
928 // We generate 15 cases and bodies that process special tags that combine
929 // the raw data tag and the length into one byte.
930 #define RAW_CASE(index, size) \
931 case kRawData + index: { \
932 byte* raw_data_out = reinterpret_cast<byte*>(current); \
933 source_->CopyRaw(raw_data_out, size); \
934 current = reinterpret_cast<Object**>(raw_data_out + size); \
937 COMMON_RAW_LENGTHS(RAW_CASE)
940 // Deserialize a chunk of raw data that doesn't have one of the popular
943 int size = source_->GetInt();
944 byte* raw_data_out = reinterpret_cast<byte*>(current);
945 source_->CopyRaw(raw_data_out, size);
946 current = reinterpret_cast<Object**>(raw_data_out + size);
950 SIXTEEN_CASES(kRootArrayLowConstants)
951 SIXTEEN_CASES(kRootArrayHighConstants) {
952 int root_id = RootArrayConstantFromByteCode(data);
953 Object* object = isolate->heap()->roots_array_start()[root_id];
954 ASSERT(!isolate->heap()->InNewSpace(object));
960 int repeats = source_->GetInt();
961 Object* object = current[-1];
962 ASSERT(!isolate->heap()->InNewSpace(object));
963 for (int i = 0; i < repeats; i++) current[i] = object;
968 STATIC_ASSERT(kRootArrayNumberOfConstantEncodings ==
969 Heap::kOldSpaceRoots);
970 STATIC_ASSERT(kMaxRepeats == 12);
971 FOUR_CASES(kConstantRepeat)
972 FOUR_CASES(kConstantRepeat + 4)
973 FOUR_CASES(kConstantRepeat + 8) {
974 int repeats = RepeatsForCode(data);
975 Object* object = current[-1];
976 ASSERT(!isolate->heap()->InNewSpace(object));
977 for (int i = 0; i < repeats; i++) current[i] = object;
982 // Deserialize a new object and write a pointer to it to the current
984 ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject)
985 // Support for direct instruction pointers in functions
986 ONE_PER_CODE_SPACE(kNewObject, kPlain, kFirstInstruction)
987 // Deserialize a new code object and write a pointer to its first
988 // instruction to the current code object.
989 ONE_PER_SPACE(kNewObject, kFromCode, kFirstInstruction)
990 // Find a recently deserialized object using its offset from the current
991 // allocation point and write a pointer to it to the current object.
992 ALL_SPACES(kBackref, kPlain, kStartOfObject)
993 #if V8_TARGET_ARCH_MIPS
994 // Deserialize a new object from pointer found in code and write
995 // a pointer to it to the current object. Required only for MIPS, and
996 // omitted on the other architectures because it is fully unrolled and
997 // would cause bloat.
998 ONE_PER_SPACE(kNewObject, kFromCode, kStartOfObject)
999 // Find a recently deserialized code object using its offset from the
1000 // current allocation point and write a pointer to it to the current
1001 // object. Required only for MIPS.
1002 ALL_SPACES(kBackref, kFromCode, kStartOfObject)
1003 // Find an already deserialized code object using its offset from
1004 // the start and write a pointer to it to the current object.
1005 // Required only for MIPS.
1006 ALL_SPACES(kFromStart, kFromCode, kStartOfObject)
1008 // Find a recently deserialized code object using its offset from the
1009 // current allocation point and write a pointer to its first instruction
1010 // to the current code object or the instruction pointer in a function
1012 ALL_SPACES(kBackref, kFromCode, kFirstInstruction)
1013 ALL_SPACES(kBackref, kPlain, kFirstInstruction)
1014 // Find an already deserialized object using its offset from the start
1015 // and write a pointer to it to the current object.
1016 ALL_SPACES(kFromStart, kPlain, kStartOfObject)
1017 ALL_SPACES(kFromStart, kPlain, kFirstInstruction)
1018 // Find an already deserialized code object using its offset from the
1019 // start and write a pointer to its first instruction to the current code
1021 ALL_SPACES(kFromStart, kFromCode, kFirstInstruction)
1022 // Find an object in the roots array and write a pointer to it to the
1024 CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
1025 CASE_BODY(kRootArray, kPlain, kStartOfObject, 0, kUnknownOffsetFromStart)
1026 // Find an object in the partial snapshots cache and write a pointer to it
1027 // to the current object.
1028 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
1029 CASE_BODY(kPartialSnapshotCache,
1033 kUnknownOffsetFromStart)
1034 // Find an code entry in the partial snapshots cache and
1035 // write a pointer to it to the current object.
1036 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kFirstInstruction, 0)
1037 CASE_BODY(kPartialSnapshotCache,
1041 kUnknownOffsetFromStart)
1042 // Find an external reference and write a pointer to it to the current
1044 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
1045 CASE_BODY(kExternalReference,
1049 kUnknownOffsetFromStart)
1050 // Find an external reference and write a pointer to it in the current
1052 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
1053 CASE_BODY(kExternalReference,
1057 kUnknownOffsetFromStart)
1059 #undef CASE_STATEMENT
1061 #undef ONE_PER_SPACE
1063 #undef ASSIGN_DEST_SPACE
1066 int space = source_->Get();
1067 pages_[space].Add(last_object_address_);
1068 if (space == CODE_SPACE) {
1069 CPU::FlushICache(last_object_address_, Page::kPageSize);
1079 case kNativesStringResource: {
1080 int index = source_->Get();
1081 Vector<const char> source_vector = Natives::GetRawScriptSource(index);
1082 NativesExternalStringResource* resource =
1083 new NativesExternalStringResource(isolate->bootstrapper(),
1084 source_vector.start(),
1085 source_vector.length());
1086 *current++ = reinterpret_cast<Object*>(resource);
1090 case kSynchronize: {
1091 // If we get here then that indicates that you have a mismatch between
1092 // the number of GC roots when serializing and deserializing.
1100 ASSERT_EQ(current, limit);
1104 void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
1105 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
1106 for (int shift = max_shift; shift > 0; shift -= 7) {
1107 if (integer >= static_cast<uintptr_t>(1u) << shift) {
1108 Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart");
1111 PutSection(static_cast<int>(integer & 0x7f), "IntLastPart");
1115 Serializer::Serializer(SnapshotByteSink* sink)
1117 current_root_index_(0),
1118 external_reference_encoder_(new ExternalReferenceEncoder),
1119 large_object_total_(0),
1120 root_index_wave_front_(0) {
1121 isolate_ = Isolate::Current();
1122 // The serializer is meant to be used only to generate initial heap images
1123 // from a context in which there is only one isolate.
1124 ASSERT(isolate_->IsDefaultIsolate());
1125 for (int i = 0; i <= LAST_SPACE; i++) {
1131 Serializer::~Serializer() {
1132 delete external_reference_encoder_;
1136 void StartupSerializer::SerializeStrongReferences() {
1137 Isolate* isolate = Isolate::Current();
1138 // No active threads.
1139 CHECK_EQ(NULL, Isolate::Current()->thread_manager()->FirstThreadStateInUse());
1140 // No active or weak handles.
1141 CHECK(isolate->handle_scope_implementer()->blocks()->is_empty());
1142 CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles());
1143 // We don't support serializing installed extensions.
1144 CHECK(!isolate->has_installed_extensions());
1146 HEAP->IterateStrongRoots(this, VISIT_ONLY_STRONG);
1150 void PartialSerializer::Serialize(Object** object) {
1151 this->VisitPointer(object);
1152 Isolate* isolate = Isolate::Current();
1154 // After we have done the partial serialization the partial snapshot cache
1155 // will contain some references needed to decode the partial snapshot. We
1156 // fill it up with undefineds so it has a predictable length so the
1157 // deserialization code doesn't need to know the length.
1158 for (int index = isolate->serialize_partial_snapshot_cache_length();
1159 index < Isolate::kPartialSnapshotCacheCapacity;
1161 isolate->serialize_partial_snapshot_cache()[index] =
1162 isolate->heap()->undefined_value();
1163 startup_serializer_->VisitPointer(
1164 &isolate->serialize_partial_snapshot_cache()[index]);
1166 isolate->set_serialize_partial_snapshot_cache_length(
1167 Isolate::kPartialSnapshotCacheCapacity);
1171 void Serializer::VisitPointers(Object** start, Object** end) {
1172 Isolate* isolate = Isolate::Current();
1174 for (Object** current = start; current < end; current++) {
1175 if (start == isolate->heap()->roots_array_start()) {
1176 root_index_wave_front_ =
1177 Max(root_index_wave_front_, static_cast<intptr_t>(current - start));
1179 if (reinterpret_cast<Address>(current) ==
1180 isolate->heap()->store_buffer()->TopAddress()) {
1181 sink_->Put(kSkip, "Skip");
1182 } else if ((*current)->IsSmi()) {
1183 sink_->Put(kRawData, "RawData");
1184 sink_->PutInt(kPointerSize, "length");
1185 for (int i = 0; i < kPointerSize; i++) {
1186 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1189 SerializeObject(*current, kPlain, kStartOfObject);
1195 // This ensures that the partial snapshot cache keeps things alive during GC and
1196 // tracks their movement. When it is called during serialization of the startup
1197 // snapshot the partial snapshot is empty, so nothing happens. When the partial
1198 // (context) snapshot is created, this array is populated with the pointers that
1199 // the partial snapshot will need. As that happens we emit serialized objects to
1200 // the startup snapshot that correspond to the elements of this cache array. On
1201 // deserialization we therefore need to visit the cache array. This fills it up
1202 // with pointers to deserialized objects.
1203 void SerializerDeserializer::Iterate(ObjectVisitor* visitor) {
1204 Isolate* isolate = Isolate::Current();
1205 visitor->VisitPointers(
1206 isolate->serialize_partial_snapshot_cache(),
1207 &isolate->serialize_partial_snapshot_cache()[
1208 isolate->serialize_partial_snapshot_cache_length()]);
1212 // When deserializing we need to set the size of the snapshot cache. This means
1213 // the root iteration code (above) will iterate over array elements, writing the
1214 // references to deserialized objects in them.
1215 void SerializerDeserializer::SetSnapshotCacheSize(int size) {
1216 Isolate::Current()->set_serialize_partial_snapshot_cache_length(size);
1220 int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1221 Isolate* isolate = Isolate::Current();
1224 i < isolate->serialize_partial_snapshot_cache_length();
1226 Object* entry = isolate->serialize_partial_snapshot_cache()[i];
1227 if (entry == heap_object) return i;
1230 // We didn't find the object in the cache. So we add it to the cache and
1231 // then visit the pointer so that it becomes part of the startup snapshot
1232 // and we can refer to it from the partial snapshot.
1233 int length = isolate->serialize_partial_snapshot_cache_length();
1234 CHECK(length < Isolate::kPartialSnapshotCacheCapacity);
1235 isolate->serialize_partial_snapshot_cache()[length] = heap_object;
1236 startup_serializer_->VisitPointer(
1237 &isolate->serialize_partial_snapshot_cache()[length]);
1238 // We don't recurse from the startup snapshot generator into the partial
1239 // snapshot generator.
1240 ASSERT(length == isolate->serialize_partial_snapshot_cache_length());
1241 isolate->set_serialize_partial_snapshot_cache_length(length + 1);
1246 int Serializer::RootIndex(HeapObject* heap_object, HowToCode from) {
1248 if (heap->InNewSpace(heap_object)) return kInvalidRootIndex;
1249 for (int i = 0; i < root_index_wave_front_; i++) {
1250 Object* root = heap->roots_array_start()[i];
1251 if (!root->IsSmi() && root == heap_object) {
1252 #if V8_TARGET_ARCH_MIPS
1253 if (from == kFromCode) {
1254 // In order to avoid code bloat in the deserializer we don't have
1255 // support for the encoding that specifies a particular root should
1256 // be written into the lui/ori instructions on MIPS. Therefore we
1257 // should not generate such serialization data for MIPS.
1258 return kInvalidRootIndex;
1264 return kInvalidRootIndex;
1268 // Encode the location of an already deserialized object in order to write its
1269 // location into a later object. We can encode the location as an offset from
1270 // the start of the deserialized objects or as an offset backwards from the
1271 // current allocation pointer.
1272 void Serializer::SerializeReferenceToPreviousObject(
1275 HowToCode how_to_code,
1276 WhereToPoint where_to_point) {
1277 int offset = CurrentAllocationAddress(space) - address;
1278 bool from_start = true;
1279 if (SpaceIsPaged(space)) {
1280 // For paged space it is simple to encode back from current allocation if
1281 // the object is on the same page as the current allocation pointer.
1282 if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1283 (address >> kPageSizeBits)) {
1287 } else if (space == NEW_SPACE) {
1288 // For new space it is always simple to encode back from current allocation.
1289 if (offset < address) {
1294 // If we are actually dealing with real offsets (and not a numbering of
1295 // all objects) then we should shift out the bits that are always 0.
1296 if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
1298 sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer");
1299 sink_->PutInt(address, "address");
1301 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1302 sink_->PutInt(address, "address");
1307 void StartupSerializer::SerializeObject(
1309 HowToCode how_to_code,
1310 WhereToPoint where_to_point) {
1311 CHECK(o->IsHeapObject());
1312 HeapObject* heap_object = HeapObject::cast(o);
1315 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1316 PutRoot(root_index, heap_object, how_to_code, where_to_point);
1320 if (address_mapper_.IsMapped(heap_object)) {
1321 int space = SpaceOfAlreadySerializedObject(heap_object);
1322 int address = address_mapper_.MappedTo(heap_object);
1323 SerializeReferenceToPreviousObject(space,
1328 // Object has not yet been serialized. Serialize it here.
1329 ObjectSerializer object_serializer(this,
1334 object_serializer.Serialize();
1339 void StartupSerializer::SerializeWeakReferences() {
1340 for (int i = Isolate::Current()->serialize_partial_snapshot_cache_length();
1341 i < Isolate::kPartialSnapshotCacheCapacity;
1343 sink_->Put(kRootArray + kPlain + kStartOfObject, "RootSerialization");
1344 sink_->PutInt(Heap::kUndefinedValueRootIndex, "root_index");
1346 HEAP->IterateWeakRoots(this, VISIT_ALL);
1350 void Serializer::PutRoot(int root_index,
1352 SerializerDeserializer::HowToCode how_to_code,
1353 SerializerDeserializer::WhereToPoint where_to_point) {
1354 if (how_to_code == kPlain &&
1355 where_to_point == kStartOfObject &&
1356 root_index < kRootArrayNumberOfConstantEncodings &&
1357 !HEAP->InNewSpace(object)) {
1358 if (root_index < kRootArrayNumberOfLowConstantEncodings) {
1359 sink_->Put(kRootArrayLowConstants + root_index, "RootLoConstant");
1361 sink_->Put(kRootArrayHighConstants + root_index -
1362 kRootArrayNumberOfLowConstantEncodings,
1366 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
1367 sink_->PutInt(root_index, "root_index");
1372 void PartialSerializer::SerializeObject(
1374 HowToCode how_to_code,
1375 WhereToPoint where_to_point) {
1376 CHECK(o->IsHeapObject());
1377 HeapObject* heap_object = HeapObject::cast(o);
1379 if (heap_object->IsMap()) {
1380 // The code-caches link to context-specific code objects, which
1381 // the startup and context serializes cannot currently handle.
1382 ASSERT(Map::cast(heap_object)->code_cache() ==
1383 heap_object->GetHeap()->raw_unchecked_empty_fixed_array());
1387 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1388 PutRoot(root_index, heap_object, how_to_code, where_to_point);
1392 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1393 int cache_index = PartialSnapshotCacheIndex(heap_object);
1394 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1395 "PartialSnapshotCache");
1396 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1400 // Pointers from the partial snapshot to the objects in the startup snapshot
1401 // should go through the root array or through the partial snapshot cache.
1402 // If this is not the case you may have to add something to the root array.
1403 ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1404 // All the symbols that the partial snapshot needs should be either in the
1405 // root table or in the partial snapshot cache.
1406 ASSERT(!heap_object->IsSymbol());
1408 if (address_mapper_.IsMapped(heap_object)) {
1409 int space = SpaceOfAlreadySerializedObject(heap_object);
1410 int address = address_mapper_.MappedTo(heap_object);
1411 SerializeReferenceToPreviousObject(space,
1416 // Object has not yet been serialized. Serialize it here.
1417 ObjectSerializer serializer(this,
1422 serializer.Serialize();
1427 void Serializer::ObjectSerializer::Serialize() {
1428 int space = Serializer::SpaceOfObject(object_);
1429 int size = object_->Size();
1431 sink_->Put(kNewObject + reference_representation_ + space,
1432 "ObjectSerialization");
1433 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1435 LOG(i::Isolate::Current(),
1436 SnapshotPositionEvent(object_->address(), sink_->Position()));
1438 // Mark this object as already serialized.
1439 bool start_new_page;
1440 int offset = serializer_->Allocate(space, size, &start_new_page);
1441 serializer_->address_mapper()->AddMapping(object_, offset);
1442 if (start_new_page) {
1443 sink_->Put(kNewPage, "NewPage");
1444 sink_->PutSection(space, "NewPageSpace");
1447 // Serialize the map (first word of the object).
1448 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject);
1450 // Serialize the rest of the object.
1451 CHECK_EQ(0, bytes_processed_so_far_);
1452 bytes_processed_so_far_ = kPointerSize;
1453 object_->IterateBody(object_->map()->instance_type(), size, this);
1454 OutputRawData(object_->address() + size);
1458 void Serializer::ObjectSerializer::VisitPointers(Object** start,
1460 Object** current = start;
1461 while (current < end) {
1462 while (current < end && (*current)->IsSmi()) current++;
1463 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1465 while (current < end && !(*current)->IsSmi()) {
1466 HeapObject* current_contents = HeapObject::cast(*current);
1467 int root_index = serializer_->RootIndex(current_contents, kPlain);
1468 // Repeats are not subject to the write barrier so there are only some
1469 // objects that can be used in a repeat encoding. These are the early
1470 // ones in the root array that are never in new space.
1471 if (current != start &&
1472 root_index != kInvalidRootIndex &&
1473 root_index < kRootArrayNumberOfConstantEncodings &&
1474 current_contents == current[-1]) {
1475 ASSERT(!HEAP->InNewSpace(current_contents));
1476 int repeat_count = 1;
1477 while (current < end - 1 && current[repeat_count] == current_contents) {
1480 current += repeat_count;
1481 bytes_processed_so_far_ += repeat_count * kPointerSize;
1482 if (repeat_count > kMaxRepeats) {
1483 sink_->Put(kRepeat, "SerializeRepeats");
1484 sink_->PutInt(repeat_count, "SerializeRepeats");
1486 sink_->Put(CodeForRepeats(repeat_count), "SerializeRepeats");
1489 serializer_->SerializeObject(current_contents, kPlain, kStartOfObject);
1490 bytes_processed_so_far_ += kPointerSize;
1498 void Serializer::ObjectSerializer::VisitEmbeddedPointer(RelocInfo* rinfo) {
1499 Object** current = rinfo->target_object_address();
1501 OutputRawData(rinfo->target_address_address());
1502 HowToCode representation = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1503 serializer_->SerializeObject(*current, representation, kStartOfObject);
1504 bytes_processed_so_far_ += rinfo->target_address_size();
1508 void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1510 Address references_start = reinterpret_cast<Address>(start);
1511 OutputRawData(references_start);
1513 for (Address* current = start; current < end; current++) {
1514 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
1515 int reference_id = serializer_->EncodeExternalReference(*current);
1516 sink_->PutInt(reference_id, "reference id");
1518 bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1522 void Serializer::ObjectSerializer::VisitExternalReference(RelocInfo* rinfo) {
1523 Address references_start = rinfo->target_address_address();
1524 OutputRawData(references_start);
1526 Address* current = rinfo->target_reference_address();
1527 int representation = rinfo->IsCodedSpecially() ?
1528 kFromCode + kStartOfObject : kPlain + kStartOfObject;
1529 sink_->Put(kExternalReference + representation, "ExternalRef");
1530 int reference_id = serializer_->EncodeExternalReference(*current);
1531 sink_->PutInt(reference_id, "reference id");
1532 bytes_processed_so_far_ += rinfo->target_address_size();
1536 void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1537 Address target_start = rinfo->target_address_address();
1538 OutputRawData(target_start);
1539 Address target = rinfo->target_address();
1540 uint32_t encoding = serializer_->EncodeExternalReference(target);
1541 CHECK(target == NULL ? encoding == 0 : encoding != 0);
1543 // Can't use a ternary operator because of gcc.
1544 if (rinfo->IsCodedSpecially()) {
1545 representation = kStartOfObject + kFromCode;
1547 representation = kStartOfObject + kPlain;
1549 sink_->Put(kExternalReference + representation, "ExternalReference");
1550 sink_->PutInt(encoding, "reference id");
1551 bytes_processed_so_far_ += rinfo->target_address_size();
1555 void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1556 CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1557 Address target_start = rinfo->target_address_address();
1558 OutputRawData(target_start);
1559 Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
1560 serializer_->SerializeObject(target, kFromCode, kFirstInstruction);
1561 bytes_processed_so_far_ += rinfo->target_address_size();
1565 void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1566 Code* target = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1567 OutputRawData(entry_address);
1568 serializer_->SerializeObject(target, kPlain, kFirstInstruction);
1569 bytes_processed_so_far_ += kPointerSize;
1573 void Serializer::ObjectSerializer::VisitGlobalPropertyCell(RelocInfo* rinfo) {
1574 // We shouldn't have any global property cell references in code
1575 // objects in the snapshot.
1580 void Serializer::ObjectSerializer::VisitExternalAsciiString(
1581 v8::String::ExternalAsciiStringResource** resource_pointer) {
1582 Address references_start = reinterpret_cast<Address>(resource_pointer);
1583 OutputRawData(references_start);
1584 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1585 Object* source = HEAP->natives_source_cache()->get(i);
1586 if (!source->IsUndefined()) {
1587 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1588 typedef v8::String::ExternalAsciiStringResource Resource;
1589 const Resource* resource = string->resource();
1590 if (resource == *resource_pointer) {
1591 sink_->Put(kNativesStringResource, "NativesStringResource");
1592 sink_->PutSection(i, "NativesStringResourceEnd");
1593 bytes_processed_so_far_ += sizeof(resource);
1598 // One of the strings in the natives cache should match the resource. We
1599 // can't serialize any other kinds of external strings.
1604 void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1605 Address object_start = object_->address();
1606 int up_to_offset = static_cast<int>(up_to - object_start);
1607 int skipped = up_to_offset - bytes_processed_so_far_;
1608 // This assert will fail if the reloc info gives us the target_address_address
1609 // locations in a non-ascending order. Luckily that doesn't happen.
1610 ASSERT(skipped >= 0);
1612 Address base = object_start + bytes_processed_so_far_;
1613 #define RAW_CASE(index, length) \
1614 if (skipped == length) { \
1615 sink_->PutSection(kRawData + index, "RawDataFixed"); \
1617 COMMON_RAW_LENGTHS(RAW_CASE)
1620 sink_->Put(kRawData, "RawData");
1621 sink_->PutInt(skipped, "length");
1623 for (int i = 0; i < skipped; i++) {
1624 unsigned int data = base[i];
1625 sink_->PutSection(data, "Byte");
1627 bytes_processed_so_far_ += skipped;
1632 int Serializer::SpaceOfObject(HeapObject* object) {
1633 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1634 AllocationSpace s = static_cast<AllocationSpace>(i);
1635 if (HEAP->InSpace(object, s)) {
1636 if (i == LO_SPACE) {
1637 if (object->IsCode()) {
1639 } else if (object->IsFixedArray()) {
1640 return kLargeFixedArray;
1653 int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1654 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1655 AllocationSpace s = static_cast<AllocationSpace>(i);
1656 if (HEAP->InSpace(object, s)) {
1665 int Serializer::Allocate(int space, int size, bool* new_page) {
1666 CHECK(space >= 0 && space < kNumberOfSpaces);
1667 if (SpaceIsLarge(space)) {
1668 // In large object space we merely number the objects instead of trying to
1669 // determine some sort of address.
1671 large_object_total_ += size;
1672 return fullness_[LO_SPACE]++;
1675 if (fullness_[space] == 0) {
1678 if (SpaceIsPaged(space)) {
1679 // Paged spaces are a little special. We encode their addresses as if the
1680 // pages were all contiguous and each page were filled up in the range
1681 // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1682 // and allocation does not start at offset 0 in the page, but this scheme
1683 // means the deserializer can get the page number quickly by shifting the
1684 // serialized address.
1685 CHECK(IsPowerOf2(Page::kPageSize));
1686 int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1687 CHECK(size <= SpaceAreaSize(space));
1688 if (used_in_this_page + size > SpaceAreaSize(space)) {
1690 fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1693 int allocation_address = fullness_[space];
1694 fullness_[space] = allocation_address + size;
1695 return allocation_address;
1699 int Serializer::SpaceAreaSize(int space) {
1700 if (space == CODE_SPACE) {
1701 return isolate_->memory_allocator()->CodePageAreaSize();
1703 return Page::kPageSize - Page::kObjectStartOffset;
1708 } } // namespace v8::internal