Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / src / serialize.cc
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "v8.h"
6
7 #include "accessors.h"
8 #include "api.h"
9 #include "bootstrapper.h"
10 #include "deoptimizer.h"
11 #include "execution.h"
12 #include "global-handles.h"
13 #include "ic-inl.h"
14 #include "natives.h"
15 #include "platform.h"
16 #include "runtime.h"
17 #include "serialize.h"
18 #include "snapshot.h"
19 #include "stub-cache.h"
20 #include "v8threads.h"
21
22 namespace v8 {
23 namespace internal {
24
25
26 // -----------------------------------------------------------------------------
27 // Coding of external references.
28
29 // The encoding of an external reference. The type is in the high word.
30 // The id is in the low word.
31 static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
32   return static_cast<uint32_t>(type) << 16 | id;
33 }
34
35
36 static int* GetInternalPointer(StatsCounter* counter) {
37   // All counters refer to dummy_counter, if deserializing happens without
38   // setting up counters.
39   static int dummy_counter = 0;
40   return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
41 }
42
43
44 ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) {
45   ExternalReferenceTable* external_reference_table =
46       isolate->external_reference_table();
47   if (external_reference_table == NULL) {
48     external_reference_table = new ExternalReferenceTable(isolate);
49     isolate->set_external_reference_table(external_reference_table);
50   }
51   return external_reference_table;
52 }
53
54
55 void ExternalReferenceTable::AddFromId(TypeCode type,
56                                        uint16_t id,
57                                        const char* name,
58                                        Isolate* isolate) {
59   Address address;
60   switch (type) {
61     case C_BUILTIN: {
62       ExternalReference ref(static_cast<Builtins::CFunctionId>(id), isolate);
63       address = ref.address();
64       break;
65     }
66     case BUILTIN: {
67       ExternalReference ref(static_cast<Builtins::Name>(id), isolate);
68       address = ref.address();
69       break;
70     }
71     case RUNTIME_FUNCTION: {
72       ExternalReference ref(static_cast<Runtime::FunctionId>(id), isolate);
73       address = ref.address();
74       break;
75     }
76     case IC_UTILITY: {
77       ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)),
78                             isolate);
79       address = ref.address();
80       break;
81     }
82     default:
83       UNREACHABLE();
84       return;
85   }
86   Add(address, type, id, name);
87 }
88
89
90 void ExternalReferenceTable::Add(Address address,
91                                  TypeCode type,
92                                  uint16_t id,
93                                  const char* name) {
94   ASSERT_NE(NULL, address);
95   ExternalReferenceEntry entry;
96   entry.address = address;
97   entry.code = EncodeExternal(type, id);
98   entry.name = name;
99   ASSERT_NE(0, entry.code);
100   refs_.Add(entry);
101   if (id > max_id_[type]) max_id_[type] = id;
102 }
103
104
105 void ExternalReferenceTable::PopulateTable(Isolate* isolate) {
106   for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
107     max_id_[type_code] = 0;
108   }
109
110   // The following populates all of the different type of external references
111   // into the ExternalReferenceTable.
112   //
113   // NOTE: This function was originally 100k of code.  It has since been
114   // rewritten to be mostly table driven, as the callback macro style tends to
115   // very easily cause code bloat.  Please be careful in the future when adding
116   // new references.
117
118   struct RefTableEntry {
119     TypeCode type;
120     uint16_t id;
121     const char* name;
122   };
123
124   static const RefTableEntry ref_table[] = {
125   // Builtins
126 #define DEF_ENTRY_C(name, ignored) \
127   { C_BUILTIN, \
128     Builtins::c_##name, \
129     "Builtins::" #name },
130
131   BUILTIN_LIST_C(DEF_ENTRY_C)
132 #undef DEF_ENTRY_C
133
134 #define DEF_ENTRY_C(name, ignored) \
135   { BUILTIN, \
136     Builtins::k##name, \
137     "Builtins::" #name },
138 #define DEF_ENTRY_A(name, kind, state, extra) DEF_ENTRY_C(name, ignored)
139
140   BUILTIN_LIST_C(DEF_ENTRY_C)
141   BUILTIN_LIST_A(DEF_ENTRY_A)
142   BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
143 #undef DEF_ENTRY_C
144 #undef DEF_ENTRY_A
145
146   // Runtime functions
147 #define RUNTIME_ENTRY(name, nargs, ressize) \
148   { RUNTIME_FUNCTION, \
149     Runtime::k##name, \
150     "Runtime::" #name },
151
152   RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
153 #undef RUNTIME_ENTRY
154
155 #define RUNTIME_HIDDEN_ENTRY(name, nargs, ressize) \
156   { RUNTIME_FUNCTION, \
157     Runtime::kHidden##name, \
158     "Runtime::Hidden" #name },
159
160   RUNTIME_HIDDEN_FUNCTION_LIST(RUNTIME_HIDDEN_ENTRY)
161 #undef RUNTIME_HIDDEN_ENTRY
162
163 #define INLINE_OPTIMIZED_ENTRY(name, nargs, ressize) \
164   { RUNTIME_FUNCTION, \
165     Runtime::kInlineOptimized##name, \
166     "Runtime::" #name },
167
168   INLINE_OPTIMIZED_FUNCTION_LIST(INLINE_OPTIMIZED_ENTRY)
169 #undef INLINE_OPTIMIZED_ENTRY
170
171   // IC utilities
172 #define IC_ENTRY(name) \
173   { IC_UTILITY, \
174     IC::k##name, \
175     "IC::" #name },
176
177   IC_UTIL_LIST(IC_ENTRY)
178 #undef IC_ENTRY
179   };  // end of ref_table[].
180
181   for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
182     AddFromId(ref_table[i].type,
183               ref_table[i].id,
184               ref_table[i].name,
185               isolate);
186   }
187
188   // Debug addresses
189   Add(Debug_Address(Debug::k_after_break_target_address).address(isolate),
190       DEBUG_ADDRESS,
191       Debug::k_after_break_target_address << kDebugIdShift,
192       "Debug::after_break_target_address()");
193   Add(Debug_Address(Debug::k_restarter_frame_function_pointer).address(isolate),
194       DEBUG_ADDRESS,
195       Debug::k_restarter_frame_function_pointer << kDebugIdShift,
196       "Debug::restarter_frame_function_pointer_address()");
197
198   // Stat counters
199   struct StatsRefTableEntry {
200     StatsCounter* (Counters::*counter)();
201     uint16_t id;
202     const char* name;
203   };
204
205   const StatsRefTableEntry stats_ref_table[] = {
206 #define COUNTER_ENTRY(name, caption) \
207   { &Counters::name,    \
208     Counters::k_##name, \
209     "Counters::" #name },
210
211   STATS_COUNTER_LIST_1(COUNTER_ENTRY)
212   STATS_COUNTER_LIST_2(COUNTER_ENTRY)
213 #undef COUNTER_ENTRY
214   };  // end of stats_ref_table[].
215
216   Counters* counters = isolate->counters();
217   for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
218     Add(reinterpret_cast<Address>(GetInternalPointer(
219             (counters->*(stats_ref_table[i].counter))())),
220         STATS_COUNTER,
221         stats_ref_table[i].id,
222         stats_ref_table[i].name);
223   }
224
225   // Top addresses
226
227   const char* AddressNames[] = {
228 #define BUILD_NAME_LITERAL(CamelName, hacker_name)      \
229     "Isolate::" #hacker_name "_address",
230     FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL)
231     NULL
232 #undef BUILD_NAME_LITERAL
233   };
234
235   for (uint16_t i = 0; i < Isolate::kIsolateAddressCount; ++i) {
236     Add(isolate->get_address_from_id((Isolate::AddressId)i),
237         TOP_ADDRESS, i, AddressNames[i]);
238   }
239
240   // Accessors
241 #define ACCESSOR_INFO_DECLARATION(name) \
242   Add(FUNCTION_ADDR(&Accessors::name##Getter), \
243       ACCESSOR, \
244       Accessors::k##name##Getter, \
245       "Accessors::" #name "Getter"); \
246   Add(FUNCTION_ADDR(&Accessors::name##Setter), \
247       ACCESSOR, \
248       Accessors::k##name##Setter, \
249       "Accessors::" #name "Setter");
250   ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION)
251 #undef ACCESSOR_INFO_DECLARATION
252
253   StubCache* stub_cache = isolate->stub_cache();
254
255   // Stub cache tables
256   Add(stub_cache->key_reference(StubCache::kPrimary).address(),
257       STUB_CACHE_TABLE,
258       1,
259       "StubCache::primary_->key");
260   Add(stub_cache->value_reference(StubCache::kPrimary).address(),
261       STUB_CACHE_TABLE,
262       2,
263       "StubCache::primary_->value");
264   Add(stub_cache->map_reference(StubCache::kPrimary).address(),
265       STUB_CACHE_TABLE,
266       3,
267       "StubCache::primary_->map");
268   Add(stub_cache->key_reference(StubCache::kSecondary).address(),
269       STUB_CACHE_TABLE,
270       4,
271       "StubCache::secondary_->key");
272   Add(stub_cache->value_reference(StubCache::kSecondary).address(),
273       STUB_CACHE_TABLE,
274       5,
275       "StubCache::secondary_->value");
276   Add(stub_cache->map_reference(StubCache::kSecondary).address(),
277       STUB_CACHE_TABLE,
278       6,
279       "StubCache::secondary_->map");
280
281   // Runtime entries
282   Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
283       RUNTIME_ENTRY,
284       4,
285       "HandleScope::DeleteExtensions");
286   Add(ExternalReference::
287           incremental_marking_record_write_function(isolate).address(),
288       RUNTIME_ENTRY,
289       5,
290       "IncrementalMarking::RecordWrite");
291   Add(ExternalReference::store_buffer_overflow_function(isolate).address(),
292       RUNTIME_ENTRY,
293       6,
294       "StoreBuffer::StoreBufferOverflow");
295
296   // Miscellaneous
297   Add(ExternalReference::roots_array_start(isolate).address(),
298       UNCLASSIFIED,
299       3,
300       "Heap::roots_array_start()");
301   Add(ExternalReference::address_of_stack_limit(isolate).address(),
302       UNCLASSIFIED,
303       4,
304       "StackGuard::address_of_jslimit()");
305   Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
306       UNCLASSIFIED,
307       5,
308       "StackGuard::address_of_real_jslimit()");
309 #ifndef V8_INTERPRETED_REGEXP
310   Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
311       UNCLASSIFIED,
312       6,
313       "RegExpStack::limit_address()");
314   Add(ExternalReference::address_of_regexp_stack_memory_address(
315           isolate).address(),
316       UNCLASSIFIED,
317       7,
318       "RegExpStack::memory_address()");
319   Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
320       UNCLASSIFIED,
321       8,
322       "RegExpStack::memory_size()");
323   Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
324       UNCLASSIFIED,
325       9,
326       "OffsetsVector::static_offsets_vector");
327 #endif  // V8_INTERPRETED_REGEXP
328   Add(ExternalReference::new_space_start(isolate).address(),
329       UNCLASSIFIED,
330       10,
331       "Heap::NewSpaceStart()");
332   Add(ExternalReference::new_space_mask(isolate).address(),
333       UNCLASSIFIED,
334       11,
335       "Heap::NewSpaceMask()");
336   Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
337       UNCLASSIFIED,
338       14,
339       "Heap::NewSpaceAllocationLimitAddress()");
340   Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
341       UNCLASSIFIED,
342       15,
343       "Heap::NewSpaceAllocationTopAddress()");
344   Add(ExternalReference::debug_break(isolate).address(),
345       UNCLASSIFIED,
346       16,
347       "Debug::Break()");
348   Add(ExternalReference::debug_step_in_fp_address(isolate).address(),
349       UNCLASSIFIED,
350       17,
351       "Debug::step_in_fp_addr()");
352   Add(ExternalReference::mod_two_doubles_operation(isolate).address(),
353       UNCLASSIFIED,
354       22,
355       "mod_two_doubles");
356 #ifndef V8_INTERPRETED_REGEXP
357   Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
358       UNCLASSIFIED,
359       24,
360       "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
361   Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
362       UNCLASSIFIED,
363       25,
364       "RegExpMacroAssembler*::CheckStackGuardState()");
365   Add(ExternalReference::re_grow_stack(isolate).address(),
366       UNCLASSIFIED,
367       26,
368       "NativeRegExpMacroAssembler::GrowStack()");
369   Add(ExternalReference::re_word_character_map().address(),
370       UNCLASSIFIED,
371       27,
372       "NativeRegExpMacroAssembler::word_character_map");
373 #endif  // V8_INTERPRETED_REGEXP
374   // Keyed lookup cache.
375   Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(),
376       UNCLASSIFIED,
377       28,
378       "KeyedLookupCache::keys()");
379   Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(),
380       UNCLASSIFIED,
381       29,
382       "KeyedLookupCache::field_offsets()");
383   Add(ExternalReference::handle_scope_next_address(isolate).address(),
384       UNCLASSIFIED,
385       31,
386       "HandleScope::next");
387   Add(ExternalReference::handle_scope_limit_address(isolate).address(),
388       UNCLASSIFIED,
389       32,
390       "HandleScope::limit");
391   Add(ExternalReference::handle_scope_level_address(isolate).address(),
392       UNCLASSIFIED,
393       33,
394       "HandleScope::level");
395   Add(ExternalReference::new_deoptimizer_function(isolate).address(),
396       UNCLASSIFIED,
397       34,
398       "Deoptimizer::New()");
399   Add(ExternalReference::compute_output_frames_function(isolate).address(),
400       UNCLASSIFIED,
401       35,
402       "Deoptimizer::ComputeOutputFrames()");
403   Add(ExternalReference::address_of_min_int().address(),
404       UNCLASSIFIED,
405       36,
406       "LDoubleConstant::min_int");
407   Add(ExternalReference::address_of_one_half().address(),
408       UNCLASSIFIED,
409       37,
410       "LDoubleConstant::one_half");
411   Add(ExternalReference::isolate_address(isolate).address(),
412       UNCLASSIFIED,
413       38,
414       "isolate");
415   Add(ExternalReference::address_of_minus_zero().address(),
416       UNCLASSIFIED,
417       39,
418       "LDoubleConstant::minus_zero");
419   Add(ExternalReference::address_of_negative_infinity().address(),
420       UNCLASSIFIED,
421       40,
422       "LDoubleConstant::negative_infinity");
423   Add(ExternalReference::power_double_double_function(isolate).address(),
424       UNCLASSIFIED,
425       41,
426       "power_double_double_function");
427   Add(ExternalReference::power_double_int_function(isolate).address(),
428       UNCLASSIFIED,
429       42,
430       "power_double_int_function");
431   Add(ExternalReference::store_buffer_top(isolate).address(),
432       UNCLASSIFIED,
433       43,
434       "store_buffer_top");
435   Add(ExternalReference::address_of_canonical_non_hole_nan().address(),
436       UNCLASSIFIED,
437       44,
438       "canonical_nan");
439   Add(ExternalReference::address_of_the_hole_nan().address(),
440       UNCLASSIFIED,
441       45,
442       "the_hole_nan");
443   Add(ExternalReference::get_date_field_function(isolate).address(),
444       UNCLASSIFIED,
445       46,
446       "JSDate::GetField");
447   Add(ExternalReference::date_cache_stamp(isolate).address(),
448       UNCLASSIFIED,
449       47,
450       "date_cache_stamp");
451   Add(ExternalReference::address_of_pending_message_obj(isolate).address(),
452       UNCLASSIFIED,
453       48,
454       "address_of_pending_message_obj");
455   Add(ExternalReference::address_of_has_pending_message(isolate).address(),
456       UNCLASSIFIED,
457       49,
458       "address_of_has_pending_message");
459   Add(ExternalReference::address_of_pending_message_script(isolate).address(),
460       UNCLASSIFIED,
461       50,
462       "pending_message_script");
463   Add(ExternalReference::get_make_code_young_function(isolate).address(),
464       UNCLASSIFIED,
465       51,
466       "Code::MakeCodeYoung");
467   Add(ExternalReference::cpu_features().address(),
468       UNCLASSIFIED,
469       52,
470       "cpu_features");
471   Add(ExternalReference(Runtime::kHiddenAllocateInNewSpace, isolate).address(),
472       UNCLASSIFIED,
473       53,
474       "Runtime::AllocateInNewSpace");
475   Add(ExternalReference(
476           Runtime::kHiddenAllocateInTargetSpace, isolate).address(),
477       UNCLASSIFIED,
478       54,
479       "Runtime::AllocateInTargetSpace");
480   Add(ExternalReference::old_pointer_space_allocation_top_address(
481       isolate).address(),
482       UNCLASSIFIED,
483       55,
484       "Heap::OldPointerSpaceAllocationTopAddress");
485   Add(ExternalReference::old_pointer_space_allocation_limit_address(
486       isolate).address(),
487       UNCLASSIFIED,
488       56,
489       "Heap::OldPointerSpaceAllocationLimitAddress");
490   Add(ExternalReference::old_data_space_allocation_top_address(
491       isolate).address(),
492       UNCLASSIFIED,
493       57,
494       "Heap::OldDataSpaceAllocationTopAddress");
495   Add(ExternalReference::old_data_space_allocation_limit_address(
496       isolate).address(),
497       UNCLASSIFIED,
498       58,
499       "Heap::OldDataSpaceAllocationLimitAddress");
500   Add(ExternalReference::new_space_high_promotion_mode_active_address(isolate).
501       address(),
502       UNCLASSIFIED,
503       59,
504       "Heap::NewSpaceAllocationLimitAddress");
505   Add(ExternalReference::allocation_sites_list_address(isolate).address(),
506       UNCLASSIFIED,
507       60,
508       "Heap::allocation_sites_list_address()");
509   Add(ExternalReference::address_of_uint32_bias().address(),
510       UNCLASSIFIED,
511       61,
512       "uint32_bias");
513   Add(ExternalReference::get_mark_code_as_executed_function(isolate).address(),
514       UNCLASSIFIED,
515       62,
516       "Code::MarkCodeAsExecuted");
517
518   Add(ExternalReference::is_profiling_address(isolate).address(),
519       UNCLASSIFIED,
520       63,
521       "CpuProfiler::is_profiling");
522
523   Add(ExternalReference::scheduled_exception_address(isolate).address(),
524       UNCLASSIFIED,
525       64,
526       "Isolate::scheduled_exception");
527
528   Add(ExternalReference::invoke_function_callback(isolate).address(),
529       UNCLASSIFIED,
530       65,
531       "InvokeFunctionCallback");
532
533   Add(ExternalReference::invoke_accessor_getter_callback(isolate).address(),
534       UNCLASSIFIED,
535       66,
536       "InvokeAccessorGetterCallback");
537
538   // Add a small set of deopt entry addresses to encoder without generating the
539   // deopt table code, which isn't possible at deserialization time.
540   HandleScope scope(isolate);
541   for (int entry = 0; entry < kDeoptTableSerializeEntryCount; ++entry) {
542     Address address = Deoptimizer::GetDeoptimizationEntry(
543         isolate,
544         entry,
545         Deoptimizer::LAZY,
546         Deoptimizer::CALCULATE_ENTRY_ADDRESS);
547     Add(address, LAZY_DEOPTIMIZATION, entry, "lazy_deopt");
548   }
549 }
550
551
552 ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate)
553     : encodings_(HashMap::PointersMatch),
554       isolate_(isolate) {
555   ExternalReferenceTable* external_references =
556       ExternalReferenceTable::instance(isolate_);
557   for (int i = 0; i < external_references->size(); ++i) {
558     Put(external_references->address(i), i);
559   }
560 }
561
562
563 uint32_t ExternalReferenceEncoder::Encode(Address key) const {
564   int index = IndexOf(key);
565   ASSERT(key == NULL || index >= 0);
566   return index >=0 ?
567          ExternalReferenceTable::instance(isolate_)->code(index) : 0;
568 }
569
570
571 const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
572   int index = IndexOf(key);
573   return index >= 0 ?
574       ExternalReferenceTable::instance(isolate_)->name(index) : NULL;
575 }
576
577
578 int ExternalReferenceEncoder::IndexOf(Address key) const {
579   if (key == NULL) return -1;
580   HashMap::Entry* entry =
581       const_cast<HashMap&>(encodings_).Lookup(key, Hash(key), false);
582   return entry == NULL
583       ? -1
584       : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
585 }
586
587
588 void ExternalReferenceEncoder::Put(Address key, int index) {
589   HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
590   entry->value = reinterpret_cast<void*>(index);
591 }
592
593
594 ExternalReferenceDecoder::ExternalReferenceDecoder(Isolate* isolate)
595     : encodings_(NewArray<Address*>(kTypeCodeCount)),
596       isolate_(isolate) {
597   ExternalReferenceTable* external_references =
598       ExternalReferenceTable::instance(isolate_);
599   for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
600     int max = external_references->max_id(type) + 1;
601     encodings_[type] = NewArray<Address>(max + 1);
602   }
603   for (int i = 0; i < external_references->size(); ++i) {
604     Put(external_references->code(i), external_references->address(i));
605   }
606 }
607
608
609 ExternalReferenceDecoder::~ExternalReferenceDecoder() {
610   for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
611     DeleteArray(encodings_[type]);
612   }
613   DeleteArray(encodings_);
614 }
615
616 AtomicWord Serializer::serialization_state_ = SERIALIZER_STATE_UNINITIALIZED;
617
618 class CodeAddressMap: public CodeEventLogger {
619  public:
620   explicit CodeAddressMap(Isolate* isolate)
621       : isolate_(isolate) {
622     isolate->logger()->addCodeEventListener(this);
623   }
624
625   virtual ~CodeAddressMap() {
626     isolate_->logger()->removeCodeEventListener(this);
627   }
628
629   virtual void CodeMoveEvent(Address from, Address to) {
630     address_to_name_map_.Move(from, to);
631   }
632
633   virtual void CodeDeleteEvent(Address from) {
634     address_to_name_map_.Remove(from);
635   }
636
637   const char* Lookup(Address address) {
638     return address_to_name_map_.Lookup(address);
639   }
640
641  private:
642   class NameMap {
643    public:
644     NameMap() : impl_(HashMap::PointersMatch) {}
645
646     ~NameMap() {
647       for (HashMap::Entry* p = impl_.Start(); p != NULL; p = impl_.Next(p)) {
648         DeleteArray(static_cast<const char*>(p->value));
649       }
650     }
651
652     void Insert(Address code_address, const char* name, int name_size) {
653       HashMap::Entry* entry = FindOrCreateEntry(code_address);
654       if (entry->value == NULL) {
655         entry->value = CopyName(name, name_size);
656       }
657     }
658
659     const char* Lookup(Address code_address) {
660       HashMap::Entry* entry = FindEntry(code_address);
661       return (entry != NULL) ? static_cast<const char*>(entry->value) : NULL;
662     }
663
664     void Remove(Address code_address) {
665       HashMap::Entry* entry = FindEntry(code_address);
666       if (entry != NULL) {
667         DeleteArray(static_cast<char*>(entry->value));
668         RemoveEntry(entry);
669       }
670     }
671
672     void Move(Address from, Address to) {
673       if (from == to) return;
674       HashMap::Entry* from_entry = FindEntry(from);
675       ASSERT(from_entry != NULL);
676       void* value = from_entry->value;
677       RemoveEntry(from_entry);
678       HashMap::Entry* to_entry = FindOrCreateEntry(to);
679       ASSERT(to_entry->value == NULL);
680       to_entry->value = value;
681     }
682
683    private:
684     static char* CopyName(const char* name, int name_size) {
685       char* result = NewArray<char>(name_size + 1);
686       for (int i = 0; i < name_size; ++i) {
687         char c = name[i];
688         if (c == '\0') c = ' ';
689         result[i] = c;
690       }
691       result[name_size] = '\0';
692       return result;
693     }
694
695     HashMap::Entry* FindOrCreateEntry(Address code_address) {
696       return impl_.Lookup(code_address, ComputePointerHash(code_address), true);
697     }
698
699     HashMap::Entry* FindEntry(Address code_address) {
700       return impl_.Lookup(code_address,
701                           ComputePointerHash(code_address),
702                           false);
703     }
704
705     void RemoveEntry(HashMap::Entry* entry) {
706       impl_.Remove(entry->key, entry->hash);
707     }
708
709     HashMap impl_;
710
711     DISALLOW_COPY_AND_ASSIGN(NameMap);
712   };
713
714   virtual void LogRecordedBuffer(Code* code,
715                                  SharedFunctionInfo*,
716                                  const char* name,
717                                  int length) {
718     address_to_name_map_.Insert(code->address(), name, length);
719   }
720
721   NameMap address_to_name_map_;
722   Isolate* isolate_;
723 };
724
725
726 CodeAddressMap* Serializer::code_address_map_ = NULL;
727
728
729 void Serializer::RequestEnable(Isolate* isolate) {
730   isolate->InitializeLoggingAndCounters();
731   code_address_map_ = new CodeAddressMap(isolate);
732 }
733
734
735 void Serializer::InitializeOncePerProcess() {
736   // InitializeOncePerProcess is called by V8::InitializeOncePerProcess, a
737   // method guaranteed to be called only once in a process lifetime.
738   // serialization_state_ is read by many threads, hence the use of
739   // Atomic primitives. Here, we don't need a barrier or mutex to
740   // write it because V8 initialization is done by one thread, and gates
741   // all reads of serialization_state_.
742   ASSERT(NoBarrier_Load(&serialization_state_) ==
743          SERIALIZER_STATE_UNINITIALIZED);
744   SerializationState state = code_address_map_
745       ? SERIALIZER_STATE_ENABLED
746       : SERIALIZER_STATE_DISABLED;
747   NoBarrier_Store(&serialization_state_, state);
748 }
749
750
751 void Serializer::TearDown() {
752   // TearDown is called by V8::TearDown() for the default isolate. It's safe
753   // to shut down the serializer by that point. Just to be safe, we restore
754   // serialization_state_ to uninitialized.
755   ASSERT(NoBarrier_Load(&serialization_state_) !=
756          SERIALIZER_STATE_UNINITIALIZED);
757   if (code_address_map_) {
758     ASSERT(NoBarrier_Load(&serialization_state_) ==
759            SERIALIZER_STATE_ENABLED);
760     delete code_address_map_;
761     code_address_map_ = NULL;
762   }
763
764   NoBarrier_Store(&serialization_state_, SERIALIZER_STATE_UNINITIALIZED);
765 }
766
767
768 Deserializer::Deserializer(SnapshotByteSource* source)
769     : isolate_(NULL),
770       source_(source),
771       external_reference_decoder_(NULL) {
772   for (int i = 0; i < LAST_SPACE + 1; i++) {
773     reservations_[i] = kUninitializedReservation;
774   }
775 }
776
777
778 void Deserializer::FlushICacheForNewCodeObjects() {
779   PageIterator it(isolate_->heap()->code_space());
780   while (it.has_next()) {
781     Page* p = it.next();
782     CPU::FlushICache(p->area_start(), p->area_end() - p->area_start());
783   }
784 }
785
786
787 void Deserializer::Deserialize(Isolate* isolate) {
788   isolate_ = isolate;
789   ASSERT(isolate_ != NULL);
790   isolate_->heap()->ReserveSpace(reservations_, &high_water_[0]);
791   // No active threads.
792   ASSERT_EQ(NULL, isolate_->thread_manager()->FirstThreadStateInUse());
793   // No active handles.
794   ASSERT(isolate_->handle_scope_implementer()->blocks()->is_empty());
795   ASSERT_EQ(NULL, external_reference_decoder_);
796   external_reference_decoder_ = new ExternalReferenceDecoder(isolate);
797   isolate_->heap()->IterateSmiRoots(this);
798   isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
799   isolate_->heap()->RepairFreeListsAfterBoot();
800   isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
801
802   isolate_->heap()->set_native_contexts_list(
803       isolate_->heap()->undefined_value());
804   isolate_->heap()->set_array_buffers_list(
805       isolate_->heap()->undefined_value());
806
807   // The allocation site list is build during root iteration, but if no sites
808   // were encountered then it needs to be initialized to undefined.
809   if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) {
810     isolate_->heap()->set_allocation_sites_list(
811         isolate_->heap()->undefined_value());
812   }
813
814   isolate_->heap()->InitializeWeakObjectToCodeTable();
815
816   // Update data pointers to the external strings containing natives sources.
817   for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
818     Object* source = isolate_->heap()->natives_source_cache()->get(i);
819     if (!source->IsUndefined()) {
820       ExternalAsciiString::cast(source)->update_data_cache();
821     }
822   }
823
824   FlushICacheForNewCodeObjects();
825
826   // Issue code events for newly deserialized code objects.
827   LOG_CODE_EVENT(isolate_, LogCodeObjects());
828   LOG_CODE_EVENT(isolate_, LogCompiledFunctions());
829 }
830
831
832 void Deserializer::DeserializePartial(Isolate* isolate, Object** root) {
833   isolate_ = isolate;
834   for (int i = NEW_SPACE; i < kNumberOfSpaces; i++) {
835     ASSERT(reservations_[i] != kUninitializedReservation);
836   }
837   isolate_->heap()->ReserveSpace(reservations_, &high_water_[0]);
838   if (external_reference_decoder_ == NULL) {
839     external_reference_decoder_ = new ExternalReferenceDecoder(isolate);
840   }
841
842   // Keep track of the code space start and end pointers in case new
843   // code objects were unserialized
844   OldSpace* code_space = isolate_->heap()->code_space();
845   Address start_address = code_space->top();
846   VisitPointer(root);
847
848   // There's no code deserialized here. If this assert fires
849   // then that's changed and logging should be added to notify
850   // the profiler et al of the new code.
851   CHECK_EQ(start_address, code_space->top());
852 }
853
854
855 Deserializer::~Deserializer() {
856   // TODO(svenpanne) Re-enable this assertion when v8 initialization is fixed.
857   // ASSERT(source_->AtEOF());
858   if (external_reference_decoder_) {
859     delete external_reference_decoder_;
860     external_reference_decoder_ = NULL;
861   }
862 }
863
864
865 // This is called on the roots.  It is the driver of the deserialization
866 // process.  It is also called on the body of each function.
867 void Deserializer::VisitPointers(Object** start, Object** end) {
868   // The space must be new space.  Any other space would cause ReadChunk to try
869   // to update the remembered using NULL as the address.
870   ReadChunk(start, end, NEW_SPACE, NULL);
871 }
872
873
874 void Deserializer::RelinkAllocationSite(AllocationSite* site) {
875   if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) {
876     site->set_weak_next(isolate_->heap()->undefined_value());
877   } else {
878     site->set_weak_next(isolate_->heap()->allocation_sites_list());
879   }
880   isolate_->heap()->set_allocation_sites_list(site);
881 }
882
883
884 // This routine writes the new object into the pointer provided and then
885 // returns true if the new object was in young space and false otherwise.
886 // The reason for this strange interface is that otherwise the object is
887 // written very late, which means the FreeSpace map is not set up by the
888 // time we need to use it to mark the space at the end of a page free.
889 void Deserializer::ReadObject(int space_number,
890                               Object** write_back) {
891   int size = source_->GetInt() << kObjectAlignmentBits;
892   Address address = Allocate(space_number, size);
893   HeapObject* obj = HeapObject::FromAddress(address);
894   *write_back = obj;
895   Object** current = reinterpret_cast<Object**>(address);
896   Object** limit = current + (size >> kPointerSizeLog2);
897   if (FLAG_log_snapshot_positions) {
898     LOG(isolate_, SnapshotPositionEvent(address, source_->position()));
899   }
900   ReadChunk(current, limit, space_number, address);
901
902   // TODO(mvstanton): consider treating the heap()->allocation_sites_list()
903   // as a (weak) root. If this root is relocated correctly,
904   // RelinkAllocationSite() isn't necessary.
905   if (obj->IsAllocationSite()) {
906     RelinkAllocationSite(AllocationSite::cast(obj));
907   }
908
909 #ifdef DEBUG
910   bool is_codespace = (space_number == CODE_SPACE);
911   ASSERT(obj->IsCode() == is_codespace);
912 #endif
913 }
914
915 void Deserializer::ReadChunk(Object** current,
916                              Object** limit,
917                              int source_space,
918                              Address current_object_address) {
919   Isolate* const isolate = isolate_;
920   // Write barrier support costs around 1% in startup time.  In fact there
921   // are no new space objects in current boot snapshots, so it's not needed,
922   // but that may change.
923   bool write_barrier_needed = (current_object_address != NULL &&
924                                source_space != NEW_SPACE &&
925                                source_space != CELL_SPACE &&
926                                source_space != PROPERTY_CELL_SPACE &&
927                                source_space != CODE_SPACE &&
928                                source_space != OLD_DATA_SPACE);
929   while (current < limit) {
930     int data = source_->Get();
931     switch (data) {
932 #define CASE_STATEMENT(where, how, within, space_number)                       \
933       case where + how + within + space_number:                                \
934       ASSERT((where & ~kPointedToMask) == 0);                                  \
935       ASSERT((how & ~kHowToCodeMask) == 0);                                    \
936       ASSERT((within & ~kWhereToPointMask) == 0);                              \
937       ASSERT((space_number & ~kSpaceMask) == 0);
938
939 #define CASE_BODY(where, how, within, space_number_if_any)                     \
940       {                                                                        \
941         bool emit_write_barrier = false;                                       \
942         bool current_was_incremented = false;                                  \
943         int space_number =  space_number_if_any == kAnyOldSpace ?              \
944                             (data & kSpaceMask) : space_number_if_any;         \
945         if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
946           ReadObject(space_number, current);                                   \
947           emit_write_barrier = (space_number == NEW_SPACE);                    \
948         } else {                                                               \
949           Object* new_object = NULL;  /* May not be a real Object pointer. */  \
950           if (where == kNewObject) {                                           \
951             ReadObject(space_number, &new_object);                             \
952           } else if (where == kRootArray) {                                    \
953             int root_id = source_->GetInt();                                   \
954             new_object = isolate->heap()->roots_array_start()[root_id];        \
955             emit_write_barrier = isolate->heap()->InNewSpace(new_object);      \
956           } else if (where == kPartialSnapshotCache) {                         \
957             int cache_index = source_->GetInt();                               \
958             new_object = isolate->serialize_partial_snapshot_cache()           \
959                 [cache_index];                                                 \
960             emit_write_barrier = isolate->heap()->InNewSpace(new_object);      \
961           } else if (where == kExternalReference) {                            \
962             int skip = source_->GetInt();                                      \
963             current = reinterpret_cast<Object**>(reinterpret_cast<Address>(    \
964                 current) + skip);                                              \
965             int reference_id = source_->GetInt();                              \
966             Address address = external_reference_decoder_->                    \
967                 Decode(reference_id);                                          \
968             new_object = reinterpret_cast<Object*>(address);                   \
969           } else if (where == kBackref) {                                      \
970             emit_write_barrier = (space_number == NEW_SPACE);                  \
971             new_object = GetAddressFromEnd(data & kSpaceMask);                 \
972           } else {                                                             \
973             ASSERT(where == kBackrefWithSkip);                                 \
974             int skip = source_->GetInt();                                      \
975             current = reinterpret_cast<Object**>(                              \
976                 reinterpret_cast<Address>(current) + skip);                    \
977             emit_write_barrier = (space_number == NEW_SPACE);                  \
978             new_object = GetAddressFromEnd(data & kSpaceMask);                 \
979           }                                                                    \
980           if (within == kInnerPointer) {                                       \
981             if (space_number != CODE_SPACE || new_object->IsCode()) {          \
982               Code* new_code_object = reinterpret_cast<Code*>(new_object);     \
983               new_object = reinterpret_cast<Object*>(                          \
984                   new_code_object->instruction_start());                       \
985             } else {                                                           \
986               ASSERT(space_number == CODE_SPACE);                              \
987               Cell* cell = Cell::cast(new_object);                             \
988               new_object = reinterpret_cast<Object*>(                          \
989                   cell->ValueAddress());                                       \
990             }                                                                  \
991           }                                                                    \
992           if (how == kFromCode) {                                              \
993             Address location_of_branch_data =                                  \
994                 reinterpret_cast<Address>(current);                            \
995             Assembler::deserialization_set_special_target_at(                  \
996                 location_of_branch_data,                                       \
997                 Code::cast(HeapObject::FromAddress(current_object_address)),   \
998                 reinterpret_cast<Address>(new_object));                        \
999             location_of_branch_data += Assembler::kSpecialTargetSize;          \
1000             current = reinterpret_cast<Object**>(location_of_branch_data);     \
1001             current_was_incremented = true;                                    \
1002           } else {                                                             \
1003             *current = new_object;                                             \
1004           }                                                                    \
1005         }                                                                      \
1006         if (emit_write_barrier && write_barrier_needed) {                      \
1007           Address current_address = reinterpret_cast<Address>(current);        \
1008           isolate->heap()->RecordWrite(                                        \
1009               current_object_address,                                          \
1010               static_cast<int>(current_address - current_object_address));     \
1011         }                                                                      \
1012         if (!current_was_incremented) {                                        \
1013           current++;                                                           \
1014         }                                                                      \
1015         break;                                                                 \
1016       }                                                                        \
1017
1018 // This generates a case and a body for the new space (which has to do extra
1019 // write barrier handling) and handles the other spaces with 8 fall-through
1020 // cases and one body.
1021 #define ALL_SPACES(where, how, within)                                         \
1022   CASE_STATEMENT(where, how, within, NEW_SPACE)                                \
1023   CASE_BODY(where, how, within, NEW_SPACE)                                     \
1024   CASE_STATEMENT(where, how, within, OLD_DATA_SPACE)                           \
1025   CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE)                        \
1026   CASE_STATEMENT(where, how, within, CODE_SPACE)                               \
1027   CASE_STATEMENT(where, how, within, CELL_SPACE)                               \
1028   CASE_STATEMENT(where, how, within, PROPERTY_CELL_SPACE)                      \
1029   CASE_STATEMENT(where, how, within, MAP_SPACE)                                \
1030   CASE_BODY(where, how, within, kAnyOldSpace)
1031
1032 #define FOUR_CASES(byte_code)             \
1033   case byte_code:                         \
1034   case byte_code + 1:                     \
1035   case byte_code + 2:                     \
1036   case byte_code + 3:
1037
1038 #define SIXTEEN_CASES(byte_code)          \
1039   FOUR_CASES(byte_code)                   \
1040   FOUR_CASES(byte_code + 4)               \
1041   FOUR_CASES(byte_code + 8)               \
1042   FOUR_CASES(byte_code + 12)
1043
1044 #define COMMON_RAW_LENGTHS(f)        \
1045   f(1)  \
1046   f(2)  \
1047   f(3)  \
1048   f(4)  \
1049   f(5)  \
1050   f(6)  \
1051   f(7)  \
1052   f(8)  \
1053   f(9)  \
1054   f(10) \
1055   f(11) \
1056   f(12) \
1057   f(13) \
1058   f(14) \
1059   f(15) \
1060   f(16) \
1061   f(17) \
1062   f(18) \
1063   f(19) \
1064   f(20) \
1065   f(21) \
1066   f(22) \
1067   f(23) \
1068   f(24) \
1069   f(25) \
1070   f(26) \
1071   f(27) \
1072   f(28) \
1073   f(29) \
1074   f(30) \
1075   f(31)
1076
1077       // We generate 15 cases and bodies that process special tags that combine
1078       // the raw data tag and the length into one byte.
1079 #define RAW_CASE(index)                                                      \
1080       case kRawData + index: {                                               \
1081         byte* raw_data_out = reinterpret_cast<byte*>(current);               \
1082         source_->CopyRaw(raw_data_out, index * kPointerSize);                \
1083         current =                                                            \
1084             reinterpret_cast<Object**>(raw_data_out + index * kPointerSize); \
1085         break;                                                               \
1086       }
1087       COMMON_RAW_LENGTHS(RAW_CASE)
1088 #undef RAW_CASE
1089
1090       // Deserialize a chunk of raw data that doesn't have one of the popular
1091       // lengths.
1092       case kRawData: {
1093         int size = source_->GetInt();
1094         byte* raw_data_out = reinterpret_cast<byte*>(current);
1095         source_->CopyRaw(raw_data_out, size);
1096         break;
1097       }
1098
1099       SIXTEEN_CASES(kRootArrayConstants + kNoSkipDistance)
1100       SIXTEEN_CASES(kRootArrayConstants + kNoSkipDistance + 16) {
1101         int root_id = RootArrayConstantFromByteCode(data);
1102         Object* object = isolate->heap()->roots_array_start()[root_id];
1103         ASSERT(!isolate->heap()->InNewSpace(object));
1104         *current++ = object;
1105         break;
1106       }
1107
1108       SIXTEEN_CASES(kRootArrayConstants + kHasSkipDistance)
1109       SIXTEEN_CASES(kRootArrayConstants + kHasSkipDistance + 16) {
1110         int root_id = RootArrayConstantFromByteCode(data);
1111         int skip = source_->GetInt();
1112         current = reinterpret_cast<Object**>(
1113             reinterpret_cast<intptr_t>(current) + skip);
1114         Object* object = isolate->heap()->roots_array_start()[root_id];
1115         ASSERT(!isolate->heap()->InNewSpace(object));
1116         *current++ = object;
1117         break;
1118       }
1119
1120       case kRepeat: {
1121         int repeats = source_->GetInt();
1122         Object* object = current[-1];
1123         ASSERT(!isolate->heap()->InNewSpace(object));
1124         for (int i = 0; i < repeats; i++) current[i] = object;
1125         current += repeats;
1126         break;
1127       }
1128
1129       STATIC_ASSERT(kRootArrayNumberOfConstantEncodings ==
1130                     Heap::kOldSpaceRoots);
1131       STATIC_ASSERT(kMaxRepeats == 13);
1132       case kConstantRepeat:
1133       FOUR_CASES(kConstantRepeat + 1)
1134       FOUR_CASES(kConstantRepeat + 5)
1135       FOUR_CASES(kConstantRepeat + 9) {
1136         int repeats = RepeatsForCode(data);
1137         Object* object = current[-1];
1138         ASSERT(!isolate->heap()->InNewSpace(object));
1139         for (int i = 0; i < repeats; i++) current[i] = object;
1140         current += repeats;
1141         break;
1142       }
1143
1144       // Deserialize a new object and write a pointer to it to the current
1145       // object.
1146       ALL_SPACES(kNewObject, kPlain, kStartOfObject)
1147       // Support for direct instruction pointers in functions.  It's an inner
1148       // pointer because it points at the entry point, not at the start of the
1149       // code object.
1150       CASE_STATEMENT(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
1151       CASE_BODY(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
1152       // Deserialize a new code object and write a pointer to its first
1153       // instruction to the current code object.
1154       ALL_SPACES(kNewObject, kFromCode, kInnerPointer)
1155       // Find a recently deserialized object using its offset from the current
1156       // allocation point and write a pointer to it to the current object.
1157       ALL_SPACES(kBackref, kPlain, kStartOfObject)
1158       ALL_SPACES(kBackrefWithSkip, kPlain, kStartOfObject)
1159 #if defined(V8_TARGET_ARCH_MIPS) || V8_OOL_CONSTANT_POOL
1160       // Deserialize a new object from pointer found in code and write
1161       // a pointer to it to the current object. Required only for MIPS or ARM
1162       // with ool constant pool, and omitted on the other architectures because
1163       // it is fully unrolled and would cause bloat.
1164       ALL_SPACES(kNewObject, kFromCode, kStartOfObject)
1165       // Find a recently deserialized code object using its offset from the
1166       // current allocation point and write a pointer to it to the current
1167       // object. Required only for MIPS or ARM with ool constant pool.
1168       ALL_SPACES(kBackref, kFromCode, kStartOfObject)
1169       ALL_SPACES(kBackrefWithSkip, kFromCode, kStartOfObject)
1170 #endif
1171       // Find a recently deserialized code object using its offset from the
1172       // current allocation point and write a pointer to its first instruction
1173       // to the current code object or the instruction pointer in a function
1174       // object.
1175       ALL_SPACES(kBackref, kFromCode, kInnerPointer)
1176       ALL_SPACES(kBackrefWithSkip, kFromCode, kInnerPointer)
1177       ALL_SPACES(kBackref, kPlain, kInnerPointer)
1178       ALL_SPACES(kBackrefWithSkip, kPlain, kInnerPointer)
1179       // Find an object in the roots array and write a pointer to it to the
1180       // current object.
1181       CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
1182       CASE_BODY(kRootArray, kPlain, kStartOfObject, 0)
1183       // Find an object in the partial snapshots cache and write a pointer to it
1184       // to the current object.
1185       CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
1186       CASE_BODY(kPartialSnapshotCache,
1187                 kPlain,
1188                 kStartOfObject,
1189                 0)
1190       // Find an code entry in the partial snapshots cache and
1191       // write a pointer to it to the current object.
1192       CASE_STATEMENT(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
1193       CASE_BODY(kPartialSnapshotCache,
1194                 kPlain,
1195                 kInnerPointer,
1196                 0)
1197       // Find an external reference and write a pointer to it to the current
1198       // object.
1199       CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
1200       CASE_BODY(kExternalReference,
1201                 kPlain,
1202                 kStartOfObject,
1203                 0)
1204       // Find an external reference and write a pointer to it in the current
1205       // code object.
1206       CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
1207       CASE_BODY(kExternalReference,
1208                 kFromCode,
1209                 kStartOfObject,
1210                 0)
1211
1212 #undef CASE_STATEMENT
1213 #undef CASE_BODY
1214 #undef ALL_SPACES
1215
1216       case kSkip: {
1217         int size = source_->GetInt();
1218         current = reinterpret_cast<Object**>(
1219             reinterpret_cast<intptr_t>(current) + size);
1220         break;
1221       }
1222
1223       case kNativesStringResource: {
1224         int index = source_->Get();
1225         Vector<const char> source_vector = Natives::GetRawScriptSource(index);
1226         NativesExternalStringResource* resource =
1227             new NativesExternalStringResource(isolate->bootstrapper(),
1228                                               source_vector.start(),
1229                                               source_vector.length());
1230         *current++ = reinterpret_cast<Object*>(resource);
1231         break;
1232       }
1233
1234       case kSynchronize: {
1235         // If we get here then that indicates that you have a mismatch between
1236         // the number of GC roots when serializing and deserializing.
1237         UNREACHABLE();
1238       }
1239
1240       default:
1241         UNREACHABLE();
1242     }
1243   }
1244   ASSERT_EQ(limit, current);
1245 }
1246
1247
1248 void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
1249   ASSERT(integer < 1 << 22);
1250   integer <<= 2;
1251   int bytes = 1;
1252   if (integer > 0xff) bytes = 2;
1253   if (integer > 0xffff) bytes = 3;
1254   integer |= bytes;
1255   Put(static_cast<int>(integer & 0xff), "IntPart1");
1256   if (bytes > 1) Put(static_cast<int>((integer >> 8) & 0xff), "IntPart2");
1257   if (bytes > 2) Put(static_cast<int>((integer >> 16) & 0xff), "IntPart3");
1258 }
1259
1260
1261 Serializer::Serializer(Isolate* isolate, SnapshotByteSink* sink)
1262     : isolate_(isolate),
1263       sink_(sink),
1264       external_reference_encoder_(new ExternalReferenceEncoder(isolate)),
1265       root_index_wave_front_(0) {
1266   // The serializer is meant to be used only to generate initial heap images
1267   // from a context in which there is only one isolate.
1268   for (int i = 0; i <= LAST_SPACE; i++) {
1269     fullness_[i] = 0;
1270   }
1271 }
1272
1273
1274 Serializer::~Serializer() {
1275   delete external_reference_encoder_;
1276 }
1277
1278
1279 void StartupSerializer::SerializeStrongReferences() {
1280   Isolate* isolate = this->isolate();
1281   // No active threads.
1282   CHECK_EQ(NULL, isolate->thread_manager()->FirstThreadStateInUse());
1283   // No active or weak handles.
1284   CHECK(isolate->handle_scope_implementer()->blocks()->is_empty());
1285   CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles());
1286   CHECK_EQ(0, isolate->eternal_handles()->NumberOfHandles());
1287   // We don't support serializing installed extensions.
1288   CHECK(!isolate->has_installed_extensions());
1289   isolate->heap()->IterateSmiRoots(this);
1290   isolate->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
1291 }
1292
1293
1294 void PartialSerializer::Serialize(Object** object) {
1295   this->VisitPointer(object);
1296   Pad();
1297 }
1298
1299
1300 bool Serializer::ShouldBeSkipped(Object** current) {
1301   Object** roots = isolate()->heap()->roots_array_start();
1302   return current == &roots[Heap::kStoreBufferTopRootIndex]
1303       || current == &roots[Heap::kStackLimitRootIndex]
1304       || current == &roots[Heap::kRealStackLimitRootIndex];
1305 }
1306
1307
1308 void Serializer::VisitPointers(Object** start, Object** end) {
1309   Isolate* isolate = this->isolate();;
1310
1311   for (Object** current = start; current < end; current++) {
1312     if (start == isolate->heap()->roots_array_start()) {
1313       root_index_wave_front_ =
1314           Max(root_index_wave_front_, static_cast<intptr_t>(current - start));
1315     }
1316     if (ShouldBeSkipped(current)) {
1317       sink_->Put(kSkip, "Skip");
1318       sink_->PutInt(kPointerSize, "SkipOneWord");
1319     } else if ((*current)->IsSmi()) {
1320       sink_->Put(kRawData + 1, "Smi");
1321       for (int i = 0; i < kPointerSize; i++) {
1322         sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1323       }
1324     } else {
1325       SerializeObject(*current, kPlain, kStartOfObject, 0);
1326     }
1327   }
1328 }
1329
1330
1331 // This ensures that the partial snapshot cache keeps things alive during GC and
1332 // tracks their movement.  When it is called during serialization of the startup
1333 // snapshot nothing happens.  When the partial (context) snapshot is created,
1334 // this array is populated with the pointers that the partial snapshot will
1335 // need. As that happens we emit serialized objects to the startup snapshot
1336 // that correspond to the elements of this cache array.  On deserialization we
1337 // therefore need to visit the cache array.  This fills it up with pointers to
1338 // deserialized objects.
1339 void SerializerDeserializer::Iterate(Isolate* isolate,
1340                                      ObjectVisitor* visitor) {
1341   if (Serializer::enabled(isolate)) return;
1342   for (int i = 0; ; i++) {
1343     if (isolate->serialize_partial_snapshot_cache_length() <= i) {
1344       // Extend the array ready to get a value from the visitor when
1345       // deserializing.
1346       isolate->PushToPartialSnapshotCache(Smi::FromInt(0));
1347     }
1348     Object** cache = isolate->serialize_partial_snapshot_cache();
1349     visitor->VisitPointers(&cache[i], &cache[i + 1]);
1350     // Sentinel is the undefined object, which is a root so it will not normally
1351     // be found in the cache.
1352     if (cache[i] == isolate->heap()->undefined_value()) {
1353       break;
1354     }
1355   }
1356 }
1357
1358
1359 int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1360   Isolate* isolate = this->isolate();
1361
1362   for (int i = 0;
1363        i < isolate->serialize_partial_snapshot_cache_length();
1364        i++) {
1365     Object* entry = isolate->serialize_partial_snapshot_cache()[i];
1366     if (entry == heap_object) return i;
1367   }
1368
1369   // We didn't find the object in the cache.  So we add it to the cache and
1370   // then visit the pointer so that it becomes part of the startup snapshot
1371   // and we can refer to it from the partial snapshot.
1372   int length = isolate->serialize_partial_snapshot_cache_length();
1373   isolate->PushToPartialSnapshotCache(heap_object);
1374   startup_serializer_->VisitPointer(reinterpret_cast<Object**>(&heap_object));
1375   // We don't recurse from the startup snapshot generator into the partial
1376   // snapshot generator.
1377   ASSERT(length == isolate->serialize_partial_snapshot_cache_length() - 1);
1378   return length;
1379 }
1380
1381
1382 int Serializer::RootIndex(HeapObject* heap_object, HowToCode from) {
1383   Heap* heap = isolate()->heap();
1384   if (heap->InNewSpace(heap_object)) return kInvalidRootIndex;
1385   for (int i = 0; i < root_index_wave_front_; i++) {
1386     Object* root = heap->roots_array_start()[i];
1387     if (!root->IsSmi() && root == heap_object) {
1388 #if defined(V8_TARGET_ARCH_MIPS) || V8_OOL_CONSTANT_POOL
1389       if (from == kFromCode) {
1390         // In order to avoid code bloat in the deserializer we don't have
1391         // support for the encoding that specifies a particular root should
1392         // be written from within code.
1393         return kInvalidRootIndex;
1394       }
1395 #endif
1396       return i;
1397     }
1398   }
1399   return kInvalidRootIndex;
1400 }
1401
1402
1403 // Encode the location of an already deserialized object in order to write its
1404 // location into a later object.  We can encode the location as an offset from
1405 // the start of the deserialized objects or as an offset backwards from the
1406 // current allocation pointer.
1407 void Serializer::SerializeReferenceToPreviousObject(
1408     int space,
1409     int address,
1410     HowToCode how_to_code,
1411     WhereToPoint where_to_point,
1412     int skip) {
1413   int offset = CurrentAllocationAddress(space) - address;
1414   // Shift out the bits that are always 0.
1415   offset >>= kObjectAlignmentBits;
1416   if (skip == 0) {
1417     sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1418   } else {
1419     sink_->Put(kBackrefWithSkip + how_to_code + where_to_point + space,
1420                "BackRefSerWithSkip");
1421     sink_->PutInt(skip, "BackRefSkipDistance");
1422   }
1423   sink_->PutInt(offset, "offset");
1424 }
1425
1426
1427 void StartupSerializer::SerializeObject(
1428     Object* o,
1429     HowToCode how_to_code,
1430     WhereToPoint where_to_point,
1431     int skip) {
1432   CHECK(o->IsHeapObject());
1433   HeapObject* heap_object = HeapObject::cast(o);
1434
1435   int root_index;
1436   if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1437     PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1438     return;
1439   }
1440
1441   if (address_mapper_.IsMapped(heap_object)) {
1442     int space = SpaceOfObject(heap_object);
1443     int address = address_mapper_.MappedTo(heap_object);
1444     SerializeReferenceToPreviousObject(space,
1445                                        address,
1446                                        how_to_code,
1447                                        where_to_point,
1448                                        skip);
1449   } else {
1450     if (skip != 0) {
1451       sink_->Put(kSkip, "FlushPendingSkip");
1452       sink_->PutInt(skip, "SkipDistance");
1453     }
1454
1455     // Object has not yet been serialized.  Serialize it here.
1456     ObjectSerializer object_serializer(this,
1457                                        heap_object,
1458                                        sink_,
1459                                        how_to_code,
1460                                        where_to_point);
1461     object_serializer.Serialize();
1462   }
1463 }
1464
1465
1466 void StartupSerializer::SerializeWeakReferences() {
1467   // This phase comes right after the partial serialization (of the snapshot).
1468   // After we have done the partial serialization the partial snapshot cache
1469   // will contain some references needed to decode the partial snapshot.  We
1470   // add one entry with 'undefined' which is the sentinel that the deserializer
1471   // uses to know it is done deserializing the array.
1472   Object* undefined = isolate()->heap()->undefined_value();
1473   VisitPointer(&undefined);
1474   isolate()->heap()->IterateWeakRoots(this, VISIT_ALL);
1475   Pad();
1476 }
1477
1478
1479 void Serializer::PutRoot(int root_index,
1480                          HeapObject* object,
1481                          SerializerDeserializer::HowToCode how_to_code,
1482                          SerializerDeserializer::WhereToPoint where_to_point,
1483                          int skip) {
1484   if (how_to_code == kPlain &&
1485       where_to_point == kStartOfObject &&
1486       root_index < kRootArrayNumberOfConstantEncodings &&
1487       !isolate()->heap()->InNewSpace(object)) {
1488     if (skip == 0) {
1489       sink_->Put(kRootArrayConstants + kNoSkipDistance + root_index,
1490                  "RootConstant");
1491     } else {
1492       sink_->Put(kRootArrayConstants + kHasSkipDistance + root_index,
1493                  "RootConstant");
1494       sink_->PutInt(skip, "SkipInPutRoot");
1495     }
1496   } else {
1497     if (skip != 0) {
1498       sink_->Put(kSkip, "SkipFromPutRoot");
1499       sink_->PutInt(skip, "SkipFromPutRootDistance");
1500     }
1501     sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
1502     sink_->PutInt(root_index, "root_index");
1503   }
1504 }
1505
1506
1507 void PartialSerializer::SerializeObject(
1508     Object* o,
1509     HowToCode how_to_code,
1510     WhereToPoint where_to_point,
1511     int skip) {
1512   CHECK(o->IsHeapObject());
1513   HeapObject* heap_object = HeapObject::cast(o);
1514
1515   if (heap_object->IsMap()) {
1516     // The code-caches link to context-specific code objects, which
1517     // the startup and context serializes cannot currently handle.
1518     ASSERT(Map::cast(heap_object)->code_cache() ==
1519            heap_object->GetHeap()->empty_fixed_array());
1520   }
1521
1522   int root_index;
1523   if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1524     PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1525     return;
1526   }
1527
1528   if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1529     if (skip != 0) {
1530       sink_->Put(kSkip, "SkipFromSerializeObject");
1531       sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1532     }
1533
1534     int cache_index = PartialSnapshotCacheIndex(heap_object);
1535     sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1536                "PartialSnapshotCache");
1537     sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1538     return;
1539   }
1540
1541   // Pointers from the partial snapshot to the objects in the startup snapshot
1542   // should go through the root array or through the partial snapshot cache.
1543   // If this is not the case you may have to add something to the root array.
1544   ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1545   // All the internalized strings that the partial snapshot needs should be
1546   // either in the root table or in the partial snapshot cache.
1547   ASSERT(!heap_object->IsInternalizedString());
1548
1549   if (address_mapper_.IsMapped(heap_object)) {
1550     int space = SpaceOfObject(heap_object);
1551     int address = address_mapper_.MappedTo(heap_object);
1552     SerializeReferenceToPreviousObject(space,
1553                                        address,
1554                                        how_to_code,
1555                                        where_to_point,
1556                                        skip);
1557   } else {
1558     if (skip != 0) {
1559       sink_->Put(kSkip, "SkipFromSerializeObject");
1560       sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1561     }
1562     // Object has not yet been serialized.  Serialize it here.
1563     ObjectSerializer serializer(this,
1564                                 heap_object,
1565                                 sink_,
1566                                 how_to_code,
1567                                 where_to_point);
1568     serializer.Serialize();
1569   }
1570 }
1571
1572
1573 void Serializer::ObjectSerializer::Serialize() {
1574   int space = Serializer::SpaceOfObject(object_);
1575   int size = object_->Size();
1576
1577   sink_->Put(kNewObject + reference_representation_ + space,
1578              "ObjectSerialization");
1579   sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1580
1581   ASSERT(code_address_map_);
1582   const char* code_name = code_address_map_->Lookup(object_->address());
1583   LOG(serializer_->isolate_,
1584       CodeNameEvent(object_->address(), sink_->Position(), code_name));
1585   LOG(serializer_->isolate_,
1586       SnapshotPositionEvent(object_->address(), sink_->Position()));
1587
1588   // Mark this object as already serialized.
1589   int offset = serializer_->Allocate(space, size);
1590   serializer_->address_mapper()->AddMapping(object_, offset);
1591
1592   // Serialize the map (first word of the object).
1593   serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject, 0);
1594
1595   // Serialize the rest of the object.
1596   CHECK_EQ(0, bytes_processed_so_far_);
1597   bytes_processed_so_far_ = kPointerSize;
1598   object_->IterateBody(object_->map()->instance_type(), size, this);
1599   OutputRawData(object_->address() + size);
1600 }
1601
1602
1603 void Serializer::ObjectSerializer::VisitPointers(Object** start,
1604                                                  Object** end) {
1605   Object** current = start;
1606   while (current < end) {
1607     while (current < end && (*current)->IsSmi()) current++;
1608     if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1609
1610     while (current < end && !(*current)->IsSmi()) {
1611       HeapObject* current_contents = HeapObject::cast(*current);
1612       int root_index = serializer_->RootIndex(current_contents, kPlain);
1613       // Repeats are not subject to the write barrier so there are only some
1614       // objects that can be used in a repeat encoding.  These are the early
1615       // ones in the root array that are never in new space.
1616       if (current != start &&
1617           root_index != kInvalidRootIndex &&
1618           root_index < kRootArrayNumberOfConstantEncodings &&
1619           current_contents == current[-1]) {
1620         ASSERT(!serializer_->isolate()->heap()->InNewSpace(current_contents));
1621         int repeat_count = 1;
1622         while (current < end - 1 && current[repeat_count] == current_contents) {
1623           repeat_count++;
1624         }
1625         current += repeat_count;
1626         bytes_processed_so_far_ += repeat_count * kPointerSize;
1627         if (repeat_count > kMaxRepeats) {
1628           sink_->Put(kRepeat, "SerializeRepeats");
1629           sink_->PutInt(repeat_count, "SerializeRepeats");
1630         } else {
1631           sink_->Put(CodeForRepeats(repeat_count), "SerializeRepeats");
1632         }
1633       } else {
1634         serializer_->SerializeObject(
1635                 current_contents, kPlain, kStartOfObject, 0);
1636         bytes_processed_so_far_ += kPointerSize;
1637         current++;
1638       }
1639     }
1640   }
1641 }
1642
1643
1644 void Serializer::ObjectSerializer::VisitEmbeddedPointer(RelocInfo* rinfo) {
1645   // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1646   if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1647
1648   int skip = OutputRawData(rinfo->target_address_address(),
1649                            kCanReturnSkipInsteadOfSkipping);
1650   HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1651   Object* object = rinfo->target_object();
1652   serializer_->SerializeObject(object, how_to_code, kStartOfObject, skip);
1653   bytes_processed_so_far_ += rinfo->target_address_size();
1654 }
1655
1656
1657 void Serializer::ObjectSerializer::VisitExternalReference(Address* p) {
1658   int skip = OutputRawData(reinterpret_cast<Address>(p),
1659                            kCanReturnSkipInsteadOfSkipping);
1660   sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
1661   sink_->PutInt(skip, "SkipB4ExternalRef");
1662   Address target = *p;
1663   sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1664   bytes_processed_so_far_ += kPointerSize;
1665 }
1666
1667
1668 void Serializer::ObjectSerializer::VisitExternalReference(RelocInfo* rinfo) {
1669   int skip = OutputRawData(rinfo->target_address_address(),
1670                            kCanReturnSkipInsteadOfSkipping);
1671   HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1672   sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
1673   sink_->PutInt(skip, "SkipB4ExternalRef");
1674   Address target = rinfo->target_reference();
1675   sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1676   bytes_processed_so_far_ += rinfo->target_address_size();
1677 }
1678
1679
1680 void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1681   int skip = OutputRawData(rinfo->target_address_address(),
1682                            kCanReturnSkipInsteadOfSkipping);
1683   HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1684   sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
1685   sink_->PutInt(skip, "SkipB4ExternalRef");
1686   Address target = rinfo->target_address();
1687   sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1688   bytes_processed_so_far_ += rinfo->target_address_size();
1689 }
1690
1691
1692 void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1693   // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1694   if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1695
1696   int skip = OutputRawData(rinfo->target_address_address(),
1697                            kCanReturnSkipInsteadOfSkipping);
1698   Code* object = Code::GetCodeFromTargetAddress(rinfo->target_address());
1699   serializer_->SerializeObject(object, kFromCode, kInnerPointer, skip);
1700   bytes_processed_so_far_ += rinfo->target_address_size();
1701 }
1702
1703
1704 void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1705   int skip = OutputRawData(entry_address, kCanReturnSkipInsteadOfSkipping);
1706   Code* object = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1707   serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
1708   bytes_processed_so_far_ += kPointerSize;
1709 }
1710
1711
1712 void Serializer::ObjectSerializer::VisitCell(RelocInfo* rinfo) {
1713   // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1714   if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1715
1716   int skip = OutputRawData(rinfo->pc(), kCanReturnSkipInsteadOfSkipping);
1717   Cell* object = Cell::cast(rinfo->target_cell());
1718   serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
1719 }
1720
1721
1722 void Serializer::ObjectSerializer::VisitExternalAsciiString(
1723     v8::String::ExternalAsciiStringResource** resource_pointer) {
1724   Address references_start = reinterpret_cast<Address>(resource_pointer);
1725   OutputRawData(references_start);
1726   for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1727     Object* source =
1728         serializer_->isolate()->heap()->natives_source_cache()->get(i);
1729     if (!source->IsUndefined()) {
1730       ExternalAsciiString* string = ExternalAsciiString::cast(source);
1731       typedef v8::String::ExternalAsciiStringResource Resource;
1732       const Resource* resource = string->resource();
1733       if (resource == *resource_pointer) {
1734         sink_->Put(kNativesStringResource, "NativesStringResource");
1735         sink_->PutSection(i, "NativesStringResourceEnd");
1736         bytes_processed_so_far_ += sizeof(resource);
1737         return;
1738       }
1739     }
1740   }
1741   // One of the strings in the natives cache should match the resource.  We
1742   // can't serialize any other kinds of external strings.
1743   UNREACHABLE();
1744 }
1745
1746
1747 static Code* CloneCodeObject(HeapObject* code) {
1748   Address copy = new byte[code->Size()];
1749   OS::MemCopy(copy, code->address(), code->Size());
1750   return Code::cast(HeapObject::FromAddress(copy));
1751 }
1752
1753
1754 static void WipeOutRelocations(Code* code) {
1755   int mode_mask =
1756       RelocInfo::kCodeTargetMask |
1757       RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
1758       RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE) |
1759       RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY);
1760   for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
1761     if (!(FLAG_enable_ool_constant_pool && it.rinfo()->IsInConstantPool())) {
1762       it.rinfo()->WipeOut();
1763     }
1764   }
1765 }
1766
1767
1768 int Serializer::ObjectSerializer::OutputRawData(
1769     Address up_to, Serializer::ObjectSerializer::ReturnSkip return_skip) {
1770   Address object_start = object_->address();
1771   int base = bytes_processed_so_far_;
1772   int up_to_offset = static_cast<int>(up_to - object_start);
1773   int to_skip = up_to_offset - bytes_processed_so_far_;
1774   int bytes_to_output = to_skip;
1775   bytes_processed_so_far_ +=  to_skip;
1776   // This assert will fail if the reloc info gives us the target_address_address
1777   // locations in a non-ascending order.  Luckily that doesn't happen.
1778   ASSERT(to_skip >= 0);
1779   bool outputting_code = false;
1780   if (to_skip != 0 && code_object_ && !code_has_been_output_) {
1781     // Output the code all at once and fix later.
1782     bytes_to_output = object_->Size() + to_skip - bytes_processed_so_far_;
1783     outputting_code = true;
1784     code_has_been_output_ = true;
1785   }
1786   if (bytes_to_output != 0 &&
1787       (!code_object_ || outputting_code)) {
1788 #define RAW_CASE(index)                                                        \
1789     if (!outputting_code && bytes_to_output == index * kPointerSize &&         \
1790         index * kPointerSize == to_skip) {                                     \
1791       sink_->PutSection(kRawData + index, "RawDataFixed");                     \
1792       to_skip = 0;  /* This insn already skips. */                             \
1793     } else  /* NOLINT */
1794     COMMON_RAW_LENGTHS(RAW_CASE)
1795 #undef RAW_CASE
1796     {  /* NOLINT */
1797       // We always end up here if we are outputting the code of a code object.
1798       sink_->Put(kRawData, "RawData");
1799       sink_->PutInt(bytes_to_output, "length");
1800     }
1801
1802     // To make snapshots reproducible, we need to wipe out all pointers in code.
1803     if (code_object_) {
1804       Code* code = CloneCodeObject(object_);
1805       WipeOutRelocations(code);
1806       // We need to wipe out the header fields *after* wiping out the
1807       // relocations, because some of these fields are needed for the latter.
1808       code->WipeOutHeader();
1809       object_start = code->address();
1810     }
1811
1812     const char* description = code_object_ ? "Code" : "Byte";
1813     for (int i = 0; i < bytes_to_output; i++) {
1814       sink_->PutSection(object_start[base + i], description);
1815     }
1816     if (code_object_) delete[] object_start;
1817   }
1818   if (to_skip != 0 && return_skip == kIgnoringReturn) {
1819     sink_->Put(kSkip, "Skip");
1820     sink_->PutInt(to_skip, "SkipDistance");
1821     to_skip = 0;
1822   }
1823   return to_skip;
1824 }
1825
1826
1827 int Serializer::SpaceOfObject(HeapObject* object) {
1828   for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1829     AllocationSpace s = static_cast<AllocationSpace>(i);
1830     if (object->GetHeap()->InSpace(object, s)) {
1831       ASSERT(i < kNumberOfSpaces);
1832       return i;
1833     }
1834   }
1835   UNREACHABLE();
1836   return 0;
1837 }
1838
1839
1840 int Serializer::Allocate(int space, int size) {
1841   CHECK(space >= 0 && space < kNumberOfSpaces);
1842   int allocation_address = fullness_[space];
1843   fullness_[space] = allocation_address + size;
1844   return allocation_address;
1845 }
1846
1847
1848 int Serializer::SpaceAreaSize(int space) {
1849   if (space == CODE_SPACE) {
1850     return isolate_->memory_allocator()->CodePageAreaSize();
1851   } else {
1852     return Page::kPageSize - Page::kObjectStartOffset;
1853   }
1854 }
1855
1856
1857 void Serializer::Pad() {
1858   // The non-branching GetInt will read up to 3 bytes too far, so we need
1859   // to pad the snapshot to make sure we don't read over the end.
1860   for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) {
1861     sink_->Put(kNop, "Padding");
1862   }
1863 }
1864
1865
1866 bool SnapshotByteSource::AtEOF() {
1867   if (0u + length_ - position_ > 2 * sizeof(uint32_t)) return false;
1868   for (int x = position_; x < length_; x++) {
1869     if (data_[x] != SerializerDeserializer::nop()) return false;
1870   }
1871   return true;
1872 }
1873
1874 } }  // namespace v8::internal