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