~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to src/third_party/v8/src/serialize.cc

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012 the V8 project authors. All rights reserved.
 
2
// Redistribution and use in source and binary forms, with or without
 
3
// modification, are permitted provided that the following conditions are
 
4
// met:
 
5
//
 
6
//     * Redistributions of source code must retain the above copyright
 
7
//       notice, this list of conditions and the following disclaimer.
 
8
//     * Redistributions in binary form must reproduce the above
 
9
//       copyright notice, this list of conditions and the following
 
10
//       disclaimer in the documentation and/or other materials provided
 
11
//       with the distribution.
 
12
//     * Neither the name of Google Inc. nor the names of its
 
13
//       contributors may be used to endorse or promote products derived
 
14
//       from this software without specific prior written permission.
 
15
//
 
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 
 
28
#include "v8.h"
 
29
 
 
30
#include "accessors.h"
 
31
#include "api.h"
 
32
#include "bootstrapper.h"
 
33
#include "execution.h"
 
34
#include "global-handles.h"
 
35
#include "ic-inl.h"
 
36
#include "natives.h"
 
37
#include "platform.h"
 
38
#include "runtime.h"
 
39
#include "serialize.h"
 
40
#include "snapshot.h"
 
41
#include "stub-cache.h"
 
42
#include "v8threads.h"
 
43
 
 
44
namespace v8 {
 
45
namespace internal {
 
46
 
 
47
 
 
48
// -----------------------------------------------------------------------------
 
49
// Coding of external references.
 
50
 
 
51
// The encoding of an external reference. The type is in the high word.
 
52
// The id is in the low word.
 
53
static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
 
54
  return static_cast<uint32_t>(type) << 16 | id;
 
55
}
 
56
 
 
57
 
 
58
static int* GetInternalPointer(StatsCounter* counter) {
 
59
  // All counters refer to dummy_counter, if deserializing happens without
 
60
  // setting up counters.
 
61
  static int dummy_counter = 0;
 
62
  return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
 
63
}
 
64
 
 
65
 
 
66
ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) {
 
67
  ExternalReferenceTable* external_reference_table =
 
68
      isolate->external_reference_table();
 
69
  if (external_reference_table == NULL) {
 
70
    external_reference_table = new ExternalReferenceTable(isolate);
 
71
    isolate->set_external_reference_table(external_reference_table);
 
72
  }
 
73
  return external_reference_table;
 
74
}
 
75
 
 
76
 
 
77
void ExternalReferenceTable::AddFromId(TypeCode type,
 
78
                                       uint16_t id,
 
79
                                       const char* name,
 
80
                                       Isolate* isolate) {
 
81
  Address address;
 
82
  switch (type) {
 
83
    case C_BUILTIN: {
 
84
      ExternalReference ref(static_cast<Builtins::CFunctionId>(id), isolate);
 
85
      address = ref.address();
 
86
      break;
 
87
    }
 
88
    case BUILTIN: {
 
89
      ExternalReference ref(static_cast<Builtins::Name>(id), isolate);
 
90
      address = ref.address();
 
91
      break;
 
92
    }
 
93
    case RUNTIME_FUNCTION: {
 
94
      ExternalReference ref(static_cast<Runtime::FunctionId>(id), isolate);
 
95
      address = ref.address();
 
96
      break;
 
97
    }
 
98
    case IC_UTILITY: {
 
99
      ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)),
 
100
                            isolate);
 
101
      address = ref.address();
 
102
      break;
 
103
    }
 
104
    default:
 
105
      UNREACHABLE();
 
106
      return;
 
107
  }
 
108
  Add(address, type, id, name);
 
109
}
 
110
 
 
111
 
 
112
void ExternalReferenceTable::Add(Address address,
 
113
                                 TypeCode type,
 
114
                                 uint16_t id,
 
115
                                 const char* name) {
 
116
  ASSERT_NE(NULL, address);
 
117
  ExternalReferenceEntry entry;
 
118
  entry.address = address;
 
119
  entry.code = EncodeExternal(type, id);
 
120
  entry.name = name;
 
121
  ASSERT_NE(0, entry.code);
 
122
  refs_.Add(entry);
 
123
  if (id > max_id_[type]) max_id_[type] = id;
 
124
}
 
125
 
 
126
 
 
127
void ExternalReferenceTable::PopulateTable(Isolate* isolate) {
 
128
  for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
 
129
    max_id_[type_code] = 0;
 
130
  }
 
131
 
 
132
  // The following populates all of the different type of external references
 
133
  // into the ExternalReferenceTable.
 
134
  //
 
135
  // NOTE: This function was originally 100k of code.  It has since been
 
136
  // rewritten to be mostly table driven, as the callback macro style tends to
 
137
  // very easily cause code bloat.  Please be careful in the future when adding
 
138
  // new references.
 
139
 
 
140
  struct RefTableEntry {
 
141
    TypeCode type;
 
142
    uint16_t id;
 
143
    const char* name;
 
144
  };
 
145
 
 
146
  static const RefTableEntry ref_table[] = {
 
147
  // Builtins
 
148
#define DEF_ENTRY_C(name, ignored) \
 
149
  { C_BUILTIN, \
 
150
    Builtins::c_##name, \
 
151
    "Builtins::" #name },
 
152
 
 
153
  BUILTIN_LIST_C(DEF_ENTRY_C)
 
154
#undef DEF_ENTRY_C
 
155
 
 
156
#define DEF_ENTRY_C(name, ignored) \
 
157
  { BUILTIN, \
 
158
    Builtins::k##name, \
 
159
    "Builtins::" #name },
 
160
#define DEF_ENTRY_A(name, kind, state, extra) DEF_ENTRY_C(name, ignored)
 
161
 
 
162
  BUILTIN_LIST_C(DEF_ENTRY_C)
 
163
  BUILTIN_LIST_A(DEF_ENTRY_A)
 
164
  BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
 
165
#undef DEF_ENTRY_C
 
166
#undef DEF_ENTRY_A
 
167
 
 
168
  // Runtime functions
 
169
#define RUNTIME_ENTRY(name, nargs, ressize) \
 
170
  { RUNTIME_FUNCTION, \
 
171
    Runtime::k##name, \
 
172
    "Runtime::" #name },
 
173
 
 
174
  RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
 
175
#undef RUNTIME_ENTRY
 
176
 
 
177
  // IC utilities
 
178
#define IC_ENTRY(name) \
 
179
  { IC_UTILITY, \
 
180
    IC::k##name, \
 
181
    "IC::" #name },
 
182
 
 
183
  IC_UTIL_LIST(IC_ENTRY)
 
184
#undef IC_ENTRY
 
185
  };  // end of ref_table[].
 
186
 
 
187
  for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
 
188
    AddFromId(ref_table[i].type,
 
189
              ref_table[i].id,
 
190
              ref_table[i].name,
 
191
              isolate);
 
192
  }
 
193
 
 
194
#ifdef ENABLE_DEBUGGER_SUPPORT
 
195
  // Debug addresses
 
196
  Add(Debug_Address(Debug::k_after_break_target_address).address(isolate),
 
197
      DEBUG_ADDRESS,
 
198
      Debug::k_after_break_target_address << kDebugIdShift,
 
199
      "Debug::after_break_target_address()");
 
200
  Add(Debug_Address(Debug::k_debug_break_slot_address).address(isolate),
 
201
      DEBUG_ADDRESS,
 
202
      Debug::k_debug_break_slot_address << kDebugIdShift,
 
203
      "Debug::debug_break_slot_address()");
 
204
  Add(Debug_Address(Debug::k_debug_break_return_address).address(isolate),
 
205
      DEBUG_ADDRESS,
 
206
      Debug::k_debug_break_return_address << kDebugIdShift,
 
207
      "Debug::debug_break_return_address()");
 
208
  Add(Debug_Address(Debug::k_restarter_frame_function_pointer).address(isolate),
 
209
      DEBUG_ADDRESS,
 
210
      Debug::k_restarter_frame_function_pointer << kDebugIdShift,
 
211
      "Debug::restarter_frame_function_pointer_address()");
 
212
#endif
 
213
 
 
214
  // Stat counters
 
215
  struct StatsRefTableEntry {
 
216
    StatsCounter* (Counters::*counter)();
 
217
    uint16_t id;
 
218
    const char* name;
 
219
  };
 
220
 
 
221
  const StatsRefTableEntry stats_ref_table[] = {
 
222
#define COUNTER_ENTRY(name, caption) \
 
223
  { &Counters::name,    \
 
224
    Counters::k_##name, \
 
225
    "Counters::" #name },
 
226
 
 
227
  STATS_COUNTER_LIST_1(COUNTER_ENTRY)
 
228
  STATS_COUNTER_LIST_2(COUNTER_ENTRY)
 
229
#undef COUNTER_ENTRY
 
230
  };  // end of stats_ref_table[].
 
231
 
 
232
  Counters* counters = isolate->counters();
 
233
  for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
 
234
    Add(reinterpret_cast<Address>(GetInternalPointer(
 
235
            (counters->*(stats_ref_table[i].counter))())),
 
236
        STATS_COUNTER,
 
237
        stats_ref_table[i].id,
 
238
        stats_ref_table[i].name);
 
239
  }
 
240
 
 
241
  // Top addresses
 
242
 
 
243
  const char* AddressNames[] = {
 
244
#define BUILD_NAME_LITERAL(CamelName, hacker_name)      \
 
245
    "Isolate::" #hacker_name "_address",
 
246
    FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL)
 
247
    NULL
 
248
#undef BUILD_NAME_LITERAL
 
249
  };
 
250
 
 
251
  for (uint16_t i = 0; i < Isolate::kIsolateAddressCount; ++i) {
 
252
    Add(isolate->get_address_from_id((Isolate::AddressId)i),
 
253
        TOP_ADDRESS, i, AddressNames[i]);
 
254
  }
 
255
 
 
256
  // Accessors
 
257
#define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
 
258
  Add((Address)&Accessors::name, \
 
259
      ACCESSOR, \
 
260
      Accessors::k##name, \
 
261
      "Accessors::" #name);
 
262
 
 
263
  ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
 
264
#undef ACCESSOR_DESCRIPTOR_DECLARATION
 
265
 
 
266
  StubCache* stub_cache = isolate->stub_cache();
 
267
 
 
268
  // Stub cache tables
 
269
  Add(stub_cache->key_reference(StubCache::kPrimary).address(),
 
270
      STUB_CACHE_TABLE,
 
271
      1,
 
272
      "StubCache::primary_->key");
 
273
  Add(stub_cache->value_reference(StubCache::kPrimary).address(),
 
274
      STUB_CACHE_TABLE,
 
275
      2,
 
276
      "StubCache::primary_->value");
 
277
  Add(stub_cache->map_reference(StubCache::kPrimary).address(),
 
278
      STUB_CACHE_TABLE,
 
279
      3,
 
280
      "StubCache::primary_->map");
 
281
  Add(stub_cache->key_reference(StubCache::kSecondary).address(),
 
282
      STUB_CACHE_TABLE,
 
283
      4,
 
284
      "StubCache::secondary_->key");
 
285
  Add(stub_cache->value_reference(StubCache::kSecondary).address(),
 
286
      STUB_CACHE_TABLE,
 
287
      5,
 
288
      "StubCache::secondary_->value");
 
289
  Add(stub_cache->map_reference(StubCache::kSecondary).address(),
 
290
      STUB_CACHE_TABLE,
 
291
      6,
 
292
      "StubCache::secondary_->map");
 
293
 
 
294
  // Runtime entries
 
295
  Add(ExternalReference::perform_gc_function(isolate).address(),
 
296
      RUNTIME_ENTRY,
 
297
      1,
 
298
      "Runtime::PerformGC");
 
299
  Add(ExternalReference::fill_heap_number_with_random_function(
 
300
          isolate).address(),
 
301
      RUNTIME_ENTRY,
 
302
      2,
 
303
      "V8::FillHeapNumberWithRandom");
 
304
  Add(ExternalReference::random_uint32_function(isolate).address(),
 
305
      RUNTIME_ENTRY,
 
306
      3,
 
307
      "V8::Random");
 
308
  Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
 
309
      RUNTIME_ENTRY,
 
310
      4,
 
311
      "HandleScope::DeleteExtensions");
 
312
  Add(ExternalReference::
 
313
          incremental_marking_record_write_function(isolate).address(),
 
314
      RUNTIME_ENTRY,
 
315
      5,
 
316
      "IncrementalMarking::RecordWrite");
 
317
  Add(ExternalReference::store_buffer_overflow_function(isolate).address(),
 
318
      RUNTIME_ENTRY,
 
319
      6,
 
320
      "StoreBuffer::StoreBufferOverflow");
 
321
  Add(ExternalReference::
 
322
          incremental_evacuation_record_write_function(isolate).address(),
 
323
      RUNTIME_ENTRY,
 
324
      7,
 
325
      "IncrementalMarking::RecordWrite");
 
326
 
 
327
 
 
328
 
 
329
  // Miscellaneous
 
330
  Add(ExternalReference::roots_array_start(isolate).address(),
 
331
      UNCLASSIFIED,
 
332
      3,
 
333
      "Heap::roots_array_start()");
 
334
  Add(ExternalReference::address_of_stack_limit(isolate).address(),
 
335
      UNCLASSIFIED,
 
336
      4,
 
337
      "StackGuard::address_of_jslimit()");
 
338
  Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
 
339
      UNCLASSIFIED,
 
340
      5,
 
341
      "StackGuard::address_of_real_jslimit()");
 
342
#ifndef V8_INTERPRETED_REGEXP
 
343
  Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
 
344
      UNCLASSIFIED,
 
345
      6,
 
346
      "RegExpStack::limit_address()");
 
347
  Add(ExternalReference::address_of_regexp_stack_memory_address(
 
348
          isolate).address(),
 
349
      UNCLASSIFIED,
 
350
      7,
 
351
      "RegExpStack::memory_address()");
 
352
  Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
 
353
      UNCLASSIFIED,
 
354
      8,
 
355
      "RegExpStack::memory_size()");
 
356
  Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
 
357
      UNCLASSIFIED,
 
358
      9,
 
359
      "OffsetsVector::static_offsets_vector");
 
360
#endif  // V8_INTERPRETED_REGEXP
 
361
  Add(ExternalReference::new_space_start(isolate).address(),
 
362
      UNCLASSIFIED,
 
363
      10,
 
364
      "Heap::NewSpaceStart()");
 
365
  Add(ExternalReference::new_space_mask(isolate).address(),
 
366
      UNCLASSIFIED,
 
367
      11,
 
368
      "Heap::NewSpaceMask()");
 
369
  Add(ExternalReference::heap_always_allocate_scope_depth(isolate).address(),
 
370
      UNCLASSIFIED,
 
371
      12,
 
372
      "Heap::always_allocate_scope_depth()");
 
373
  Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
 
374
      UNCLASSIFIED,
 
375
      14,
 
376
      "Heap::NewSpaceAllocationLimitAddress()");
 
377
  Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
 
378
      UNCLASSIFIED,
 
379
      15,
 
380
      "Heap::NewSpaceAllocationTopAddress()");
 
381
#ifdef ENABLE_DEBUGGER_SUPPORT
 
382
  Add(ExternalReference::debug_break(isolate).address(),
 
383
      UNCLASSIFIED,
 
384
      16,
 
385
      "Debug::Break()");
 
386
  Add(ExternalReference::debug_step_in_fp_address(isolate).address(),
 
387
      UNCLASSIFIED,
 
388
      17,
 
389
      "Debug::step_in_fp_addr()");
 
390
#endif
 
391
  Add(ExternalReference::double_fp_operation(Token::ADD, isolate).address(),
 
392
      UNCLASSIFIED,
 
393
      18,
 
394
      "add_two_doubles");
 
395
  Add(ExternalReference::double_fp_operation(Token::SUB, isolate).address(),
 
396
      UNCLASSIFIED,
 
397
      19,
 
398
      "sub_two_doubles");
 
399
  Add(ExternalReference::double_fp_operation(Token::MUL, isolate).address(),
 
400
      UNCLASSIFIED,
 
401
      20,
 
402
      "mul_two_doubles");
 
403
  Add(ExternalReference::double_fp_operation(Token::DIV, isolate).address(),
 
404
      UNCLASSIFIED,
 
405
      21,
 
406
      "div_two_doubles");
 
407
  Add(ExternalReference::double_fp_operation(Token::MOD, isolate).address(),
 
408
      UNCLASSIFIED,
 
409
      22,
 
410
      "mod_two_doubles");
 
411
  Add(ExternalReference::compare_doubles(isolate).address(),
 
412
      UNCLASSIFIED,
 
413
      23,
 
414
      "compare_doubles");
 
415
#ifndef V8_INTERPRETED_REGEXP
 
416
  Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
 
417
      UNCLASSIFIED,
 
418
      24,
 
419
      "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
 
420
  Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
 
421
      UNCLASSIFIED,
 
422
      25,
 
423
      "RegExpMacroAssembler*::CheckStackGuardState()");
 
424
  Add(ExternalReference::re_grow_stack(isolate).address(),
 
425
      UNCLASSIFIED,
 
426
      26,
 
427
      "NativeRegExpMacroAssembler::GrowStack()");
 
428
  Add(ExternalReference::re_word_character_map().address(),
 
429
      UNCLASSIFIED,
 
430
      27,
 
431
      "NativeRegExpMacroAssembler::word_character_map");
 
432
#endif  // V8_INTERPRETED_REGEXP
 
433
  // Keyed lookup cache.
 
434
  Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(),
 
435
      UNCLASSIFIED,
 
436
      28,
 
437
      "KeyedLookupCache::keys()");
 
438
  Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(),
 
439
      UNCLASSIFIED,
 
440
      29,
 
441
      "KeyedLookupCache::field_offsets()");
 
442
  Add(ExternalReference::transcendental_cache_array_address(isolate).address(),
 
443
      UNCLASSIFIED,
 
444
      30,
 
445
      "TranscendentalCache::caches()");
 
446
  Add(ExternalReference::handle_scope_next_address().address(),
 
447
      UNCLASSIFIED,
 
448
      31,
 
449
      "HandleScope::next");
 
450
  Add(ExternalReference::handle_scope_limit_address().address(),
 
451
      UNCLASSIFIED,
 
452
      32,
 
453
      "HandleScope::limit");
 
454
  Add(ExternalReference::handle_scope_level_address().address(),
 
455
      UNCLASSIFIED,
 
456
      33,
 
457
      "HandleScope::level");
 
458
  Add(ExternalReference::new_deoptimizer_function(isolate).address(),
 
459
      UNCLASSIFIED,
 
460
      34,
 
461
      "Deoptimizer::New()");
 
462
  Add(ExternalReference::compute_output_frames_function(isolate).address(),
 
463
      UNCLASSIFIED,
 
464
      35,
 
465
      "Deoptimizer::ComputeOutputFrames()");
 
466
  Add(ExternalReference::address_of_min_int().address(),
 
467
      UNCLASSIFIED,
 
468
      36,
 
469
      "LDoubleConstant::min_int");
 
470
  Add(ExternalReference::address_of_one_half().address(),
 
471
      UNCLASSIFIED,
 
472
      37,
 
473
      "LDoubleConstant::one_half");
 
474
  Add(ExternalReference::isolate_address().address(),
 
475
      UNCLASSIFIED,
 
476
      38,
 
477
      "isolate");
 
478
  Add(ExternalReference::address_of_minus_zero().address(),
 
479
      UNCLASSIFIED,
 
480
      39,
 
481
      "LDoubleConstant::minus_zero");
 
482
  Add(ExternalReference::address_of_negative_infinity().address(),
 
483
      UNCLASSIFIED,
 
484
      40,
 
485
      "LDoubleConstant::negative_infinity");
 
486
  Add(ExternalReference::power_double_double_function(isolate).address(),
 
487
      UNCLASSIFIED,
 
488
      41,
 
489
      "power_double_double_function");
 
490
  Add(ExternalReference::power_double_int_function(isolate).address(),
 
491
      UNCLASSIFIED,
 
492
      42,
 
493
      "power_double_int_function");
 
494
  Add(ExternalReference::store_buffer_top(isolate).address(),
 
495
      UNCLASSIFIED,
 
496
      43,
 
497
      "store_buffer_top");
 
498
  Add(ExternalReference::address_of_canonical_non_hole_nan().address(),
 
499
      UNCLASSIFIED,
 
500
      44,
 
501
      "canonical_nan");
 
502
  Add(ExternalReference::address_of_the_hole_nan().address(),
 
503
      UNCLASSIFIED,
 
504
      45,
 
505
      "the_hole_nan");
 
506
  Add(ExternalReference::get_date_field_function(isolate).address(),
 
507
      UNCLASSIFIED,
 
508
      46,
 
509
      "JSDate::GetField");
 
510
  Add(ExternalReference::date_cache_stamp(isolate).address(),
 
511
      UNCLASSIFIED,
 
512
      47,
 
513
      "date_cache_stamp");
 
514
  Add(ExternalReference::address_of_pending_message_obj(isolate).address(),
 
515
      UNCLASSIFIED,
 
516
      48,
 
517
      "address_of_pending_message_obj");
 
518
  Add(ExternalReference::address_of_has_pending_message(isolate).address(),
 
519
      UNCLASSIFIED,
 
520
      49,
 
521
      "address_of_has_pending_message");
 
522
  Add(ExternalReference::address_of_pending_message_script(isolate).address(),
 
523
      UNCLASSIFIED,
 
524
      50,
 
525
      "pending_message_script");
 
526
}
 
527
 
 
528
 
 
529
ExternalReferenceEncoder::ExternalReferenceEncoder()
 
530
    : encodings_(Match),
 
531
      isolate_(Isolate::Current()) {
 
532
  ExternalReferenceTable* external_references =
 
533
      ExternalReferenceTable::instance(isolate_);
 
534
  for (int i = 0; i < external_references->size(); ++i) {
 
535
    Put(external_references->address(i), i);
 
536
  }
 
537
}
 
538
 
 
539
 
 
540
uint32_t ExternalReferenceEncoder::Encode(Address key) const {
 
541
  int index = IndexOf(key);
 
542
  ASSERT(key == NULL || index >= 0);
 
543
  return index >=0 ?
 
544
         ExternalReferenceTable::instance(isolate_)->code(index) : 0;
 
545
}
 
546
 
 
547
 
 
548
const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
 
549
  int index = IndexOf(key);
 
550
  return index >= 0 ?
 
551
      ExternalReferenceTable::instance(isolate_)->name(index) : NULL;
 
552
}
 
553
 
 
554
 
 
555
int ExternalReferenceEncoder::IndexOf(Address key) const {
 
556
  if (key == NULL) return -1;
 
557
  HashMap::Entry* entry =
 
558
      const_cast<HashMap&>(encodings_).Lookup(key, Hash(key), false);
 
559
  return entry == NULL
 
560
      ? -1
 
561
      : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
 
562
}
 
563
 
 
564
 
 
565
void ExternalReferenceEncoder::Put(Address key, int index) {
 
566
  HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
 
567
  entry->value = reinterpret_cast<void*>(index);
 
568
}
 
569
 
 
570
 
 
571
ExternalReferenceDecoder::ExternalReferenceDecoder()
 
572
    : encodings_(NewArray<Address*>(kTypeCodeCount)),
 
573
      isolate_(Isolate::Current()) {
 
574
  ExternalReferenceTable* external_references =
 
575
      ExternalReferenceTable::instance(isolate_);
 
576
  for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
 
577
    int max = external_references->max_id(type) + 1;
 
578
    encodings_[type] = NewArray<Address>(max + 1);
 
579
  }
 
580
  for (int i = 0; i < external_references->size(); ++i) {
 
581
    Put(external_references->code(i), external_references->address(i));
 
582
  }
 
583
}
 
584
 
 
585
 
 
586
ExternalReferenceDecoder::~ExternalReferenceDecoder() {
 
587
  for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
 
588
    DeleteArray(encodings_[type]);
 
589
  }
 
590
  DeleteArray(encodings_);
 
591
}
 
592
 
 
593
 
 
594
bool Serializer::serialization_enabled_ = false;
 
595
bool Serializer::too_late_to_enable_now_ = false;
 
596
 
 
597
 
 
598
Deserializer::Deserializer(SnapshotByteSource* source)
 
599
    : isolate_(NULL),
 
600
      source_(source),
 
601
      external_reference_decoder_(NULL) {
 
602
}
 
603
 
 
604
 
 
605
// This routine both allocates a new object, and also keeps
 
606
// track of where objects have been allocated so that we can
 
607
// fix back references when deserializing.
 
608
Address Deserializer::Allocate(int space_index, Space* space, int size) {
 
609
  Address address;
 
610
  if (!SpaceIsLarge(space_index)) {
 
611
    ASSERT(!SpaceIsPaged(space_index) ||
 
612
           size <= Page::kPageSize - Page::kObjectStartOffset);
 
613
    MaybeObject* maybe_new_allocation;
 
614
    if (space_index == NEW_SPACE) {
 
615
      maybe_new_allocation =
 
616
          reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
 
617
    } else {
 
618
      maybe_new_allocation =
 
619
          reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
 
620
    }
 
621
    ASSERT(!maybe_new_allocation->IsFailure());
 
622
    Object* new_allocation = maybe_new_allocation->ToObjectUnchecked();
 
623
    HeapObject* new_object = HeapObject::cast(new_allocation);
 
624
    address = new_object->address();
 
625
    high_water_[space_index] = address + size;
 
626
  } else {
 
627
    ASSERT(SpaceIsLarge(space_index));
 
628
    LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
 
629
    Object* new_allocation;
 
630
    if (space_index == kLargeData || space_index == kLargeFixedArray) {
 
631
      new_allocation =
 
632
          lo_space->AllocateRaw(size, NOT_EXECUTABLE)->ToObjectUnchecked();
 
633
    } else {
 
634
      ASSERT_EQ(kLargeCode, space_index);
 
635
      new_allocation =
 
636
          lo_space->AllocateRaw(size, EXECUTABLE)->ToObjectUnchecked();
 
637
    }
 
638
    HeapObject* new_object = HeapObject::cast(new_allocation);
 
639
    // Record all large objects in the same space.
 
640
    address = new_object->address();
 
641
    pages_[LO_SPACE].Add(address);
 
642
  }
 
643
  last_object_address_ = address;
 
644
  return address;
 
645
}
 
646
 
 
647
 
 
648
// This returns the address of an object that has been described in the
 
649
// snapshot as being offset bytes back in a particular space.
 
650
HeapObject* Deserializer::GetAddressFromEnd(int space) {
 
651
  int offset = source_->GetInt();
 
652
  ASSERT(!SpaceIsLarge(space));
 
653
  offset <<= kObjectAlignmentBits;
 
654
  return HeapObject::FromAddress(high_water_[space] - offset);
 
655
}
 
656
 
 
657
 
 
658
// This returns the address of an object that has been described in the
 
659
// snapshot as being offset bytes into a particular space.
 
660
HeapObject* Deserializer::GetAddressFromStart(int space) {
 
661
  int offset = source_->GetInt();
 
662
  if (SpaceIsLarge(space)) {
 
663
    // Large spaces have one object per 'page'.
 
664
    return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
 
665
  }
 
666
  offset <<= kObjectAlignmentBits;
 
667
  if (space == NEW_SPACE) {
 
668
    // New space has only one space - numbered 0.
 
669
    return HeapObject::FromAddress(pages_[space][0] + offset);
 
670
  }
 
671
  ASSERT(SpaceIsPaged(space));
 
672
  int page_of_pointee = offset >> kPageSizeBits;
 
673
  Address object_address = pages_[space][page_of_pointee] +
 
674
                           (offset & Page::kPageAlignmentMask);
 
675
  return HeapObject::FromAddress(object_address);
 
676
}
 
677
 
 
678
 
 
679
void Deserializer::Deserialize() {
 
680
  isolate_ = Isolate::Current();
 
681
  ASSERT(isolate_ != NULL);
 
682
  // Don't GC while deserializing - just expand the heap.
 
683
  AlwaysAllocateScope always_allocate;
 
684
  // Don't use the free lists while deserializing.
 
685
  LinearAllocationScope allocate_linearly;
 
686
  // No active threads.
 
687
  ASSERT_EQ(NULL, isolate_->thread_manager()->FirstThreadStateInUse());
 
688
  // No active handles.
 
689
  ASSERT(isolate_->handle_scope_implementer()->blocks()->is_empty());
 
690
  ASSERT_EQ(NULL, external_reference_decoder_);
 
691
  external_reference_decoder_ = new ExternalReferenceDecoder();
 
692
  isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
 
693
  isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
 
694
 
 
695
  isolate_->heap()->set_global_contexts_list(
 
696
      isolate_->heap()->undefined_value());
 
697
 
 
698
  // Update data pointers to the external strings containing natives sources.
 
699
  for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
 
700
    Object* source = isolate_->heap()->natives_source_cache()->get(i);
 
701
    if (!source->IsUndefined()) {
 
702
      ExternalAsciiString::cast(source)->update_data_cache();
 
703
    }
 
704
  }
 
705
}
 
706
 
 
707
 
 
708
void Deserializer::DeserializePartial(Object** root) {
 
709
  isolate_ = Isolate::Current();
 
710
  // Don't GC while deserializing - just expand the heap.
 
711
  AlwaysAllocateScope always_allocate;
 
712
  // Don't use the free lists while deserializing.
 
713
  LinearAllocationScope allocate_linearly;
 
714
  if (external_reference_decoder_ == NULL) {
 
715
    external_reference_decoder_ = new ExternalReferenceDecoder();
 
716
  }
 
717
  VisitPointer(root);
 
718
}
 
719
 
 
720
 
 
721
Deserializer::~Deserializer() {
 
722
  ASSERT(source_->AtEOF());
 
723
  if (external_reference_decoder_) {
 
724
    delete external_reference_decoder_;
 
725
    external_reference_decoder_ = NULL;
 
726
  }
 
727
}
 
728
 
 
729
 
 
730
// This is called on the roots.  It is the driver of the deserialization
 
731
// process.  It is also called on the body of each function.
 
732
void Deserializer::VisitPointers(Object** start, Object** end) {
 
733
  // The space must be new space.  Any other space would cause ReadChunk to try
 
734
  // to update the remembered using NULL as the address.
 
735
  ReadChunk(start, end, NEW_SPACE, NULL);
 
736
}
 
737
 
 
738
 
 
739
// This routine writes the new object into the pointer provided and then
 
740
// returns true if the new object was in young space and false otherwise.
 
741
// The reason for this strange interface is that otherwise the object is
 
742
// written very late, which means the FreeSpace map is not set up by the
 
743
// time we need to use it to mark the space at the end of a page free.
 
744
void Deserializer::ReadObject(int space_number,
 
745
                              Space* space,
 
746
                              Object** write_back) {
 
747
  int size = source_->GetInt() << kObjectAlignmentBits;
 
748
  Address address = Allocate(space_number, space, size);
 
749
  *write_back = HeapObject::FromAddress(address);
 
750
  Object** current = reinterpret_cast<Object**>(address);
 
751
  Object** limit = current + (size >> kPointerSizeLog2);
 
752
  if (FLAG_log_snapshot_positions) {
 
753
    LOG(isolate_, SnapshotPositionEvent(address, source_->position()));
 
754
  }
 
755
  ReadChunk(current, limit, space_number, address);
 
756
#ifdef DEBUG
 
757
  bool is_codespace = (space == HEAP->code_space()) ||
 
758
      ((space == HEAP->lo_space()) && (space_number == kLargeCode));
 
759
  ASSERT(HeapObject::FromAddress(address)->IsCode() == is_codespace);
 
760
#endif
 
761
}
 
762
 
 
763
 
 
764
// This macro is always used with a constant argument so it should all fold
 
765
// away to almost nothing in the generated code.  It might be nicer to do this
 
766
// with the ternary operator but there are type issues with that.
 
767
#define ASSIGN_DEST_SPACE(space_number)                                        \
 
768
  Space* dest_space;                                                           \
 
769
  if (space_number == NEW_SPACE) {                                             \
 
770
    dest_space = isolate->heap()->new_space();                                \
 
771
  } else if (space_number == OLD_POINTER_SPACE) {                              \
 
772
    dest_space = isolate->heap()->old_pointer_space();                         \
 
773
  } else if (space_number == OLD_DATA_SPACE) {                                 \
 
774
    dest_space = isolate->heap()->old_data_space();                            \
 
775
  } else if (space_number == CODE_SPACE) {                                     \
 
776
    dest_space = isolate->heap()->code_space();                                \
 
777
  } else if (space_number == MAP_SPACE) {                                      \
 
778
    dest_space = isolate->heap()->map_space();                                 \
 
779
  } else if (space_number == CELL_SPACE) {                                     \
 
780
    dest_space = isolate->heap()->cell_space();                                \
 
781
  } else {                                                                     \
 
782
    ASSERT(space_number >= LO_SPACE);                                          \
 
783
    dest_space = isolate->heap()->lo_space();                                  \
 
784
  }
 
785
 
 
786
 
 
787
static const int kUnknownOffsetFromStart = -1;
 
788
 
 
789
 
 
790
void Deserializer::ReadChunk(Object** current,
 
791
                             Object** limit,
 
792
                             int source_space,
 
793
                             Address current_object_address) {
 
794
  Isolate* const isolate = isolate_;
 
795
  bool write_barrier_needed = (current_object_address != NULL &&
 
796
                               source_space != NEW_SPACE &&
 
797
                               source_space != CELL_SPACE &&
 
798
                               source_space != CODE_SPACE &&
 
799
                               source_space != OLD_DATA_SPACE);
 
800
  while (current < limit) {
 
801
    int data = source_->Get();
 
802
    switch (data) {
 
803
#define CASE_STATEMENT(where, how, within, space_number)                       \
 
804
      case where + how + within + space_number:                                \
 
805
      ASSERT((where & ~kPointedToMask) == 0);                                  \
 
806
      ASSERT((how & ~kHowToCodeMask) == 0);                                    \
 
807
      ASSERT((within & ~kWhereToPointMask) == 0);                              \
 
808
      ASSERT((space_number & ~kSpaceMask) == 0);
 
809
 
 
810
#define CASE_BODY(where, how, within, space_number_if_any, offset_from_start)  \
 
811
      {                                                                        \
 
812
        bool emit_write_barrier = false;                                       \
 
813
        bool current_was_incremented = false;                                  \
 
814
        int space_number =  space_number_if_any == kAnyOldSpace ?              \
 
815
                            (data & kSpaceMask) : space_number_if_any;         \
 
816
        if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
 
817
          ASSIGN_DEST_SPACE(space_number)                                      \
 
818
          ReadObject(space_number, dest_space, current);                       \
 
819
          emit_write_barrier = (space_number == NEW_SPACE);                    \
 
820
        } else {                                                               \
 
821
          Object* new_object = NULL;  /* May not be a real Object pointer. */  \
 
822
          if (where == kNewObject) {                                           \
 
823
            ASSIGN_DEST_SPACE(space_number)                                    \
 
824
            ReadObject(space_number, dest_space, &new_object);                 \
 
825
          } else if (where == kRootArray) {                                    \
 
826
            int root_id = source_->GetInt();                                   \
 
827
            new_object = isolate->heap()->roots_array_start()[root_id];        \
 
828
            emit_write_barrier = isolate->heap()->InNewSpace(new_object);      \
 
829
          } else if (where == kPartialSnapshotCache) {                         \
 
830
            int cache_index = source_->GetInt();                               \
 
831
            new_object = isolate->serialize_partial_snapshot_cache()           \
 
832
                [cache_index];                                                 \
 
833
            emit_write_barrier = isolate->heap()->InNewSpace(new_object);      \
 
834
          } else if (where == kExternalReference) {                            \
 
835
            int reference_id = source_->GetInt();                              \
 
836
            Address address = external_reference_decoder_->                    \
 
837
                Decode(reference_id);                                          \
 
838
            new_object = reinterpret_cast<Object*>(address);                   \
 
839
          } else if (where == kBackref) {                                      \
 
840
            emit_write_barrier = (space_number == NEW_SPACE);                  \
 
841
            new_object = GetAddressFromEnd(data & kSpaceMask);                 \
 
842
          } else {                                                             \
 
843
            ASSERT(where == kFromStart);                                       \
 
844
            if (offset_from_start == kUnknownOffsetFromStart) {                \
 
845
              emit_write_barrier = (space_number == NEW_SPACE);                \
 
846
              new_object = GetAddressFromStart(data & kSpaceMask);             \
 
847
            } else {                                                           \
 
848
              Address object_address = pages_[space_number][0] +               \
 
849
                  (offset_from_start << kObjectAlignmentBits);                 \
 
850
              new_object = HeapObject::FromAddress(object_address);            \
 
851
            }                                                                  \
 
852
          }                                                                    \
 
853
          if (within == kInnerPointer) {                                       \
 
854
            if (space_number != CODE_SPACE || new_object->IsCode()) {          \
 
855
              Code* new_code_object = reinterpret_cast<Code*>(new_object);     \
 
856
              new_object = reinterpret_cast<Object*>(                          \
 
857
                  new_code_object->instruction_start());                       \
 
858
            } else {                                                           \
 
859
              ASSERT(space_number == CODE_SPACE || space_number == kLargeCode);\
 
860
              JSGlobalPropertyCell* cell =                                     \
 
861
                  JSGlobalPropertyCell::cast(new_object);                      \
 
862
              new_object = reinterpret_cast<Object*>(                          \
 
863
                  cell->ValueAddress());                                       \
 
864
            }                                                                  \
 
865
          }                                                                    \
 
866
          if (how == kFromCode) {                                              \
 
867
            Address location_of_branch_data =                                  \
 
868
                reinterpret_cast<Address>(current);                            \
 
869
            Assembler::deserialization_set_special_target_at(                  \
 
870
                location_of_branch_data,                                       \
 
871
                reinterpret_cast<Address>(new_object));                        \
 
872
            location_of_branch_data += Assembler::kSpecialTargetSize;          \
 
873
            current = reinterpret_cast<Object**>(location_of_branch_data);     \
 
874
            current_was_incremented = true;                                    \
 
875
          } else {                                                             \
 
876
            *current = new_object;                                             \
 
877
          }                                                                    \
 
878
        }                                                                      \
 
879
        if (emit_write_barrier && write_barrier_needed) {                      \
 
880
          Address current_address = reinterpret_cast<Address>(current);        \
 
881
          isolate->heap()->RecordWrite(                                        \
 
882
              current_object_address,                                          \
 
883
              static_cast<int>(current_address - current_object_address));     \
 
884
        }                                                                      \
 
885
        if (!current_was_incremented) {                                        \
 
886
          current++;                                                           \
 
887
        }                                                                      \
 
888
        break;                                                                 \
 
889
      }                                                                        \
 
890
 
 
891
// This generates a case and a body for each space.  The large object spaces are
 
892
// very rare in snapshots so they are grouped in one body.
 
893
#define ONE_PER_SPACE(where, how, within)                                      \
 
894
  CASE_STATEMENT(where, how, within, NEW_SPACE)                                \
 
895
  CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart)            \
 
896
  CASE_STATEMENT(where, how, within, OLD_DATA_SPACE)                           \
 
897
  CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart)       \
 
898
  CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE)                        \
 
899
  CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart)    \
 
900
  CASE_STATEMENT(where, how, within, CODE_SPACE)                               \
 
901
  CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart)           \
 
902
  CASE_STATEMENT(where, how, within, CELL_SPACE)                               \
 
903
  CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart)           \
 
904
  CASE_STATEMENT(where, how, within, MAP_SPACE)                                \
 
905
  CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart)            \
 
906
  CASE_STATEMENT(where, how, within, kLargeData)                               \
 
907
  CASE_STATEMENT(where, how, within, kLargeCode)                               \
 
908
  CASE_STATEMENT(where, how, within, kLargeFixedArray)                         \
 
909
  CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
 
910
 
 
911
// This generates a case and a body for the new space (which has to do extra
 
912
// write barrier handling) and handles the other spaces with 8 fall-through
 
913
// cases and one body.
 
914
#define ALL_SPACES(where, how, within)                                         \
 
915
  CASE_STATEMENT(where, how, within, NEW_SPACE)                                \
 
916
  CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart)            \
 
917
  CASE_STATEMENT(where, how, within, OLD_DATA_SPACE)                           \
 
918
  CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE)                        \
 
919
  CASE_STATEMENT(where, how, within, CODE_SPACE)                               \
 
920
  CASE_STATEMENT(where, how, within, CELL_SPACE)                               \
 
921
  CASE_STATEMENT(where, how, within, MAP_SPACE)                                \
 
922
  CASE_STATEMENT(where, how, within, kLargeData)                               \
 
923
  CASE_STATEMENT(where, how, within, kLargeCode)                               \
 
924
  CASE_STATEMENT(where, how, within, kLargeFixedArray)                         \
 
925
  CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
 
926
 
 
927
#define ONE_PER_CODE_SPACE(where, how, within)                                 \
 
928
  CASE_STATEMENT(where, how, within, CODE_SPACE)                               \
 
929
  CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart)           \
 
930
  CASE_STATEMENT(where, how, within, kLargeCode)                               \
 
931
  CASE_BODY(where, how, within, kLargeCode, kUnknownOffsetFromStart)
 
932
 
 
933
#define FOUR_CASES(byte_code)             \
 
934
  case byte_code:                         \
 
935
  case byte_code + 1:                     \
 
936
  case byte_code + 2:                     \
 
937
  case byte_code + 3:
 
938
 
 
939
#define SIXTEEN_CASES(byte_code)          \
 
940
  FOUR_CASES(byte_code)                   \
 
941
  FOUR_CASES(byte_code + 4)               \
 
942
  FOUR_CASES(byte_code + 8)               \
 
943
  FOUR_CASES(byte_code + 12)
 
944
 
 
945
      // We generate 15 cases and bodies that process special tags that combine
 
946
      // the raw data tag and the length into one byte.
 
947
#define RAW_CASE(index, size)                                      \
 
948
      case kRawData + index: {                                     \
 
949
        byte* raw_data_out = reinterpret_cast<byte*>(current);     \
 
950
        source_->CopyRaw(raw_data_out, size);                      \
 
951
        current = reinterpret_cast<Object**>(raw_data_out + size); \
 
952
        break;                                                     \
 
953
      }
 
954
      COMMON_RAW_LENGTHS(RAW_CASE)
 
955
#undef RAW_CASE
 
956
 
 
957
      // Deserialize a chunk of raw data that doesn't have one of the popular
 
958
      // lengths.
 
959
      case kRawData: {
 
960
        int size = source_->GetInt();
 
961
        byte* raw_data_out = reinterpret_cast<byte*>(current);
 
962
        source_->CopyRaw(raw_data_out, size);
 
963
        current = reinterpret_cast<Object**>(raw_data_out + size);
 
964
        break;
 
965
      }
 
966
 
 
967
      SIXTEEN_CASES(kRootArrayLowConstants)
 
968
      SIXTEEN_CASES(kRootArrayHighConstants) {
 
969
        int root_id = RootArrayConstantFromByteCode(data);
 
970
        Object* object = isolate->heap()->roots_array_start()[root_id];
 
971
        ASSERT(!isolate->heap()->InNewSpace(object));
 
972
        *current++ = object;
 
973
        break;
 
974
      }
 
975
 
 
976
      case kRepeat: {
 
977
        int repeats = source_->GetInt();
 
978
        Object* object = current[-1];
 
979
        ASSERT(!isolate->heap()->InNewSpace(object));
 
980
        for (int i = 0; i < repeats; i++) current[i] = object;
 
981
        current += repeats;
 
982
        break;
 
983
      }
 
984
 
 
985
      STATIC_ASSERT(kRootArrayNumberOfConstantEncodings ==
 
986
                    Heap::kOldSpaceRoots);
 
987
      STATIC_ASSERT(kMaxRepeats == 12);
 
988
      FOUR_CASES(kConstantRepeat)
 
989
      FOUR_CASES(kConstantRepeat + 4)
 
990
      FOUR_CASES(kConstantRepeat + 8) {
 
991
        int repeats = RepeatsForCode(data);
 
992
        Object* object = current[-1];
 
993
        ASSERT(!isolate->heap()->InNewSpace(object));
 
994
        for (int i = 0; i < repeats; i++) current[i] = object;
 
995
        current += repeats;
 
996
        break;
 
997
      }
 
998
 
 
999
      // Deserialize a new object and write a pointer to it to the current
 
1000
      // object.
 
1001
      ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject)
 
1002
      // Support for direct instruction pointers in functions.  It's an inner
 
1003
      // pointer because it points at the entry point, not at the start of the
 
1004
      // code object.
 
1005
      ONE_PER_CODE_SPACE(kNewObject, kPlain, kInnerPointer)
 
1006
      // Deserialize a new code object and write a pointer to its first
 
1007
      // instruction to the current code object.
 
1008
      ONE_PER_SPACE(kNewObject, kFromCode, kInnerPointer)
 
1009
      // Find a recently deserialized object using its offset from the current
 
1010
      // allocation point and write a pointer to it to the current object.
 
1011
      ALL_SPACES(kBackref, kPlain, kStartOfObject)
 
1012
#if V8_TARGET_ARCH_MIPS
 
1013
      // Deserialize a new object from pointer found in code and write
 
1014
      // a pointer to it to the current object. Required only for MIPS, and
 
1015
      // omitted on the other architectures because it is fully unrolled and
 
1016
      // would cause bloat.
 
1017
      ONE_PER_SPACE(kNewObject, kFromCode, kStartOfObject)
 
1018
      // Find a recently deserialized code object using its offset from the
 
1019
      // current allocation point and write a pointer to it to the current
 
1020
      // object. Required only for MIPS.
 
1021
      ALL_SPACES(kBackref, kFromCode, kStartOfObject)
 
1022
      // Find an already deserialized code object using its offset from
 
1023
      // the start and write a pointer to it to the current object.
 
1024
      // Required only for MIPS.
 
1025
      ALL_SPACES(kFromStart, kFromCode, kStartOfObject)
 
1026
#endif
 
1027
      // Find a recently deserialized code object using its offset from the
 
1028
      // current allocation point and write a pointer to its first instruction
 
1029
      // to the current code object or the instruction pointer in a function
 
1030
      // object.
 
1031
      ALL_SPACES(kBackref, kFromCode, kInnerPointer)
 
1032
      ALL_SPACES(kBackref, kPlain, kInnerPointer)
 
1033
      // Find an already deserialized object using its offset from the start
 
1034
      // and write a pointer to it to the current object.
 
1035
      ALL_SPACES(kFromStart, kPlain, kStartOfObject)
 
1036
      ALL_SPACES(kFromStart, kPlain, kInnerPointer)
 
1037
      // Find an already deserialized code object using its offset from the
 
1038
      // start and write a pointer to its first instruction to the current code
 
1039
      // object.
 
1040
      ALL_SPACES(kFromStart, kFromCode, kInnerPointer)
 
1041
      // Find an object in the roots array and write a pointer to it to the
 
1042
      // current object.
 
1043
      CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
 
1044
      CASE_BODY(kRootArray, kPlain, kStartOfObject, 0, kUnknownOffsetFromStart)
 
1045
      // Find an object in the partial snapshots cache and write a pointer to it
 
1046
      // to the current object.
 
1047
      CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
 
1048
      CASE_BODY(kPartialSnapshotCache,
 
1049
                kPlain,
 
1050
                kStartOfObject,
 
1051
                0,
 
1052
                kUnknownOffsetFromStart)
 
1053
      // Find an code entry in the partial snapshots cache and
 
1054
      // write a pointer to it to the current object.
 
1055
      CASE_STATEMENT(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
 
1056
      CASE_BODY(kPartialSnapshotCache,
 
1057
                kPlain,
 
1058
                kInnerPointer,
 
1059
                0,
 
1060
                kUnknownOffsetFromStart)
 
1061
      // Find an external reference and write a pointer to it to the current
 
1062
      // object.
 
1063
      CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
 
1064
      CASE_BODY(kExternalReference,
 
1065
                kPlain,
 
1066
                kStartOfObject,
 
1067
                0,
 
1068
                kUnknownOffsetFromStart)
 
1069
      // Find an external reference and write a pointer to it in the current
 
1070
      // code object.
 
1071
      CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
 
1072
      CASE_BODY(kExternalReference,
 
1073
                kFromCode,
 
1074
                kStartOfObject,
 
1075
                0,
 
1076
                kUnknownOffsetFromStart)
 
1077
 
 
1078
#undef CASE_STATEMENT
 
1079
#undef CASE_BODY
 
1080
#undef ONE_PER_SPACE
 
1081
#undef ALL_SPACES
 
1082
#undef ASSIGN_DEST_SPACE
 
1083
 
 
1084
      case kNewPage: {
 
1085
        int space = source_->Get();
 
1086
        pages_[space].Add(last_object_address_);
 
1087
        if (space == CODE_SPACE) {
 
1088
          CPU::FlushICache(last_object_address_, Page::kPageSize);
 
1089
        }
 
1090
        break;
 
1091
      }
 
1092
 
 
1093
      case kSkip: {
 
1094
        current++;
 
1095
        break;
 
1096
      }
 
1097
 
 
1098
      case kNativesStringResource: {
 
1099
        int index = source_->Get();
 
1100
        Vector<const char> source_vector = Natives::GetRawScriptSource(index);
 
1101
        NativesExternalStringResource* resource =
 
1102
            new NativesExternalStringResource(isolate->bootstrapper(),
 
1103
                                              source_vector.start(),
 
1104
                                              source_vector.length());
 
1105
        *current++ = reinterpret_cast<Object*>(resource);
 
1106
        break;
 
1107
      }
 
1108
 
 
1109
      case kSynchronize: {
 
1110
        // If we get here then that indicates that you have a mismatch between
 
1111
        // the number of GC roots when serializing and deserializing.
 
1112
        UNREACHABLE();
 
1113
      }
 
1114
 
 
1115
      default:
 
1116
        UNREACHABLE();
 
1117
    }
 
1118
  }
 
1119
  ASSERT_EQ(current, limit);
 
1120
}
 
1121
 
 
1122
 
 
1123
void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
 
1124
  const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
 
1125
  for (int shift = max_shift; shift > 0; shift -= 7) {
 
1126
    if (integer >= static_cast<uintptr_t>(1u) << shift) {
 
1127
      Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart");
 
1128
    }
 
1129
  }
 
1130
  PutSection(static_cast<int>(integer & 0x7f), "IntLastPart");
 
1131
}
 
1132
 
 
1133
 
 
1134
Serializer::Serializer(SnapshotByteSink* sink)
 
1135
    : sink_(sink),
 
1136
      current_root_index_(0),
 
1137
      external_reference_encoder_(new ExternalReferenceEncoder),
 
1138
      large_object_total_(0),
 
1139
      root_index_wave_front_(0) {
 
1140
  isolate_ = Isolate::Current();
 
1141
  // The serializer is meant to be used only to generate initial heap images
 
1142
  // from a context in which there is only one isolate.
 
1143
  ASSERT(isolate_->IsDefaultIsolate());
 
1144
  for (int i = 0; i <= LAST_SPACE; i++) {
 
1145
    fullness_[i] = 0;
 
1146
  }
 
1147
}
 
1148
 
 
1149
 
 
1150
Serializer::~Serializer() {
 
1151
  delete external_reference_encoder_;
 
1152
}
 
1153
 
 
1154
 
 
1155
void StartupSerializer::SerializeStrongReferences() {
 
1156
  Isolate* isolate = Isolate::Current();
 
1157
  // No active threads.
 
1158
  CHECK_EQ(NULL, Isolate::Current()->thread_manager()->FirstThreadStateInUse());
 
1159
  // No active or weak handles.
 
1160
  CHECK(isolate->handle_scope_implementer()->blocks()->is_empty());
 
1161
  CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles());
 
1162
  // We don't support serializing installed extensions.
 
1163
  CHECK(!isolate->has_installed_extensions());
 
1164
 
 
1165
  HEAP->IterateStrongRoots(this, VISIT_ONLY_STRONG);
 
1166
}
 
1167
 
 
1168
 
 
1169
void PartialSerializer::Serialize(Object** object) {
 
1170
  this->VisitPointer(object);
 
1171
}
 
1172
 
 
1173
 
 
1174
void Serializer::VisitPointers(Object** start, Object** end) {
 
1175
  Isolate* isolate = Isolate::Current();
 
1176
 
 
1177
  for (Object** current = start; current < end; current++) {
 
1178
    if (start == isolate->heap()->roots_array_start()) {
 
1179
      root_index_wave_front_ =
 
1180
          Max(root_index_wave_front_, static_cast<intptr_t>(current - start));
 
1181
    }
 
1182
    if (reinterpret_cast<Address>(current) ==
 
1183
        isolate->heap()->store_buffer()->TopAddress()) {
 
1184
      sink_->Put(kSkip, "Skip");
 
1185
    } else if ((*current)->IsSmi()) {
 
1186
      sink_->Put(kRawData, "RawData");
 
1187
      sink_->PutInt(kPointerSize, "length");
 
1188
      for (int i = 0; i < kPointerSize; i++) {
 
1189
        sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
 
1190
      }
 
1191
    } else {
 
1192
      SerializeObject(*current, kPlain, kStartOfObject);
 
1193
    }
 
1194
  }
 
1195
}
 
1196
 
 
1197
 
 
1198
// This ensures that the partial snapshot cache keeps things alive during GC and
 
1199
// tracks their movement.  When it is called during serialization of the startup
 
1200
// snapshot nothing happens.  When the partial (context) snapshot is created,
 
1201
// this array is populated with the pointers that the partial snapshot will
 
1202
// need. As that happens we emit serialized objects to the startup snapshot
 
1203
// that correspond to the elements of this cache array.  On deserialization we
 
1204
// therefore need to visit the cache array.  This fills it up with pointers to
 
1205
// deserialized objects.
 
1206
void SerializerDeserializer::Iterate(ObjectVisitor* visitor) {
 
1207
  if (Serializer::enabled()) return;
 
1208
  Isolate* isolate = Isolate::Current();
 
1209
  for (int i = 0; ; i++) {
 
1210
    if (isolate->serialize_partial_snapshot_cache_length() <= i) {
 
1211
      // Extend the array ready to get a value from the visitor when
 
1212
      // deserializing.
 
1213
      isolate->PushToPartialSnapshotCache(Smi::FromInt(0));
 
1214
    }
 
1215
    Object** cache = isolate->serialize_partial_snapshot_cache();
 
1216
    visitor->VisitPointers(&cache[i], &cache[i + 1]);
 
1217
    // Sentinel is the undefined object, which is a root so it will not normally
 
1218
    // be found in the cache.
 
1219
    if (cache[i] == isolate->heap()->undefined_value()) {
 
1220
      break;
 
1221
    }
 
1222
  }
 
1223
}
 
1224
 
 
1225
 
 
1226
int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
 
1227
  Isolate* isolate = Isolate::Current();
 
1228
 
 
1229
  for (int i = 0;
 
1230
       i < isolate->serialize_partial_snapshot_cache_length();
 
1231
       i++) {
 
1232
    Object* entry = isolate->serialize_partial_snapshot_cache()[i];
 
1233
    if (entry == heap_object) return i;
 
1234
  }
 
1235
 
 
1236
  // We didn't find the object in the cache.  So we add it to the cache and
 
1237
  // then visit the pointer so that it becomes part of the startup snapshot
 
1238
  // and we can refer to it from the partial snapshot.
 
1239
  int length = isolate->serialize_partial_snapshot_cache_length();
 
1240
  isolate->PushToPartialSnapshotCache(heap_object);
 
1241
  startup_serializer_->VisitPointer(reinterpret_cast<Object**>(&heap_object));
 
1242
  // We don't recurse from the startup snapshot generator into the partial
 
1243
  // snapshot generator.
 
1244
  ASSERT(length == isolate->serialize_partial_snapshot_cache_length() - 1);
 
1245
  return length;
 
1246
}
 
1247
 
 
1248
 
 
1249
int Serializer::RootIndex(HeapObject* heap_object, HowToCode from) {
 
1250
  Heap* heap = HEAP;
 
1251
  if (heap->InNewSpace(heap_object)) return kInvalidRootIndex;
 
1252
  for (int i = 0; i < root_index_wave_front_; i++) {
 
1253
    Object* root = heap->roots_array_start()[i];
 
1254
    if (!root->IsSmi() && root == heap_object) {
 
1255
#if V8_TARGET_ARCH_MIPS
 
1256
      if (from == kFromCode) {
 
1257
        // In order to avoid code bloat in the deserializer we don't have
 
1258
        // support for the encoding that specifies a particular root should
 
1259
        // be written into the lui/ori instructions on MIPS.  Therefore we
 
1260
        // should not generate such serialization data for MIPS.
 
1261
        return kInvalidRootIndex;
 
1262
      }
 
1263
#endif
 
1264
      return i;
 
1265
    }
 
1266
  }
 
1267
  return kInvalidRootIndex;
 
1268
}
 
1269
 
 
1270
 
 
1271
// Encode the location of an already deserialized object in order to write its
 
1272
// location into a later object.  We can encode the location as an offset from
 
1273
// the start of the deserialized objects or as an offset backwards from the
 
1274
// current allocation pointer.
 
1275
void Serializer::SerializeReferenceToPreviousObject(
 
1276
    int space,
 
1277
    int address,
 
1278
    HowToCode how_to_code,
 
1279
    WhereToPoint where_to_point) {
 
1280
  int offset = CurrentAllocationAddress(space) - address;
 
1281
  bool from_start = true;
 
1282
  if (SpaceIsPaged(space)) {
 
1283
    // For paged space it is simple to encode back from current allocation if
 
1284
    // the object is on the same page as the current allocation pointer.
 
1285
    if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
 
1286
        (address >> kPageSizeBits)) {
 
1287
      from_start = false;
 
1288
      address = offset;
 
1289
    }
 
1290
  } else if (space == NEW_SPACE) {
 
1291
    // For new space it is always simple to encode back from current allocation.
 
1292
    if (offset < address) {
 
1293
      from_start = false;
 
1294
      address = offset;
 
1295
    }
 
1296
  }
 
1297
  // If we are actually dealing with real offsets (and not a numbering of
 
1298
  // all objects) then we should shift out the bits that are always 0.
 
1299
  if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
 
1300
  if (from_start) {
 
1301
    sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer");
 
1302
    sink_->PutInt(address, "address");
 
1303
  } else {
 
1304
    sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
 
1305
    sink_->PutInt(address, "address");
 
1306
  }
 
1307
}
 
1308
 
 
1309
 
 
1310
void StartupSerializer::SerializeObject(
 
1311
    Object* o,
 
1312
    HowToCode how_to_code,
 
1313
    WhereToPoint where_to_point) {
 
1314
  CHECK(o->IsHeapObject());
 
1315
  HeapObject* heap_object = HeapObject::cast(o);
 
1316
 
 
1317
  int root_index;
 
1318
  if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
 
1319
    PutRoot(root_index, heap_object, how_to_code, where_to_point);
 
1320
    return;
 
1321
  }
 
1322
 
 
1323
  if (address_mapper_.IsMapped(heap_object)) {
 
1324
    int space = SpaceOfAlreadySerializedObject(heap_object);
 
1325
    int address = address_mapper_.MappedTo(heap_object);
 
1326
    SerializeReferenceToPreviousObject(space,
 
1327
                                       address,
 
1328
                                       how_to_code,
 
1329
                                       where_to_point);
 
1330
  } else {
 
1331
    // Object has not yet been serialized.  Serialize it here.
 
1332
    ObjectSerializer object_serializer(this,
 
1333
                                       heap_object,
 
1334
                                       sink_,
 
1335
                                       how_to_code,
 
1336
                                       where_to_point);
 
1337
    object_serializer.Serialize();
 
1338
  }
 
1339
}
 
1340
 
 
1341
 
 
1342
void StartupSerializer::SerializeWeakReferences() {
 
1343
  // This phase comes right after the partial serialization (of the snapshot).
 
1344
  // After we have done the partial serialization the partial snapshot cache
 
1345
  // will contain some references needed to decode the partial snapshot.  We
 
1346
  // add one entry with 'undefined' which is the sentinel that the deserializer
 
1347
  // uses to know it is done deserializing the array.
 
1348
  Isolate* isolate = Isolate::Current();
 
1349
  Object* undefined = isolate->heap()->undefined_value();
 
1350
  VisitPointer(&undefined);
 
1351
  HEAP->IterateWeakRoots(this, VISIT_ALL);
 
1352
}
 
1353
 
 
1354
 
 
1355
void Serializer::PutRoot(int root_index,
 
1356
                         HeapObject* object,
 
1357
                         SerializerDeserializer::HowToCode how_to_code,
 
1358
                         SerializerDeserializer::WhereToPoint where_to_point) {
 
1359
  if (how_to_code == kPlain &&
 
1360
      where_to_point == kStartOfObject &&
 
1361
      root_index < kRootArrayNumberOfConstantEncodings &&
 
1362
      !HEAP->InNewSpace(object)) {
 
1363
    if (root_index < kRootArrayNumberOfLowConstantEncodings) {
 
1364
      sink_->Put(kRootArrayLowConstants + root_index, "RootLoConstant");
 
1365
    } else {
 
1366
      sink_->Put(kRootArrayHighConstants + root_index -
 
1367
                     kRootArrayNumberOfLowConstantEncodings,
 
1368
                 "RootHiConstant");
 
1369
    }
 
1370
  } else {
 
1371
    sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
 
1372
    sink_->PutInt(root_index, "root_index");
 
1373
  }
 
1374
}
 
1375
 
 
1376
 
 
1377
void PartialSerializer::SerializeObject(
 
1378
    Object* o,
 
1379
    HowToCode how_to_code,
 
1380
    WhereToPoint where_to_point) {
 
1381
  CHECK(o->IsHeapObject());
 
1382
  HeapObject* heap_object = HeapObject::cast(o);
 
1383
 
 
1384
  if (heap_object->IsMap()) {
 
1385
    // The code-caches link to context-specific code objects, which
 
1386
    // the startup and context serializes cannot currently handle.
 
1387
    ASSERT(Map::cast(heap_object)->code_cache() ==
 
1388
           heap_object->GetHeap()->raw_unchecked_empty_fixed_array());
 
1389
  }
 
1390
 
 
1391
  int root_index;
 
1392
  if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
 
1393
    PutRoot(root_index, heap_object, how_to_code, where_to_point);
 
1394
    return;
 
1395
  }
 
1396
 
 
1397
  if (ShouldBeInThePartialSnapshotCache(heap_object)) {
 
1398
    int cache_index = PartialSnapshotCacheIndex(heap_object);
 
1399
    sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
 
1400
               "PartialSnapshotCache");
 
1401
    sink_->PutInt(cache_index, "partial_snapshot_cache_index");
 
1402
    return;
 
1403
  }
 
1404
 
 
1405
  // Pointers from the partial snapshot to the objects in the startup snapshot
 
1406
  // should go through the root array or through the partial snapshot cache.
 
1407
  // If this is not the case you may have to add something to the root array.
 
1408
  ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
 
1409
  // All the symbols that the partial snapshot needs should be either in the
 
1410
  // root table or in the partial snapshot cache.
 
1411
  ASSERT(!heap_object->IsSymbol());
 
1412
 
 
1413
  if (address_mapper_.IsMapped(heap_object)) {
 
1414
    int space = SpaceOfAlreadySerializedObject(heap_object);
 
1415
    int address = address_mapper_.MappedTo(heap_object);
 
1416
    SerializeReferenceToPreviousObject(space,
 
1417
                                       address,
 
1418
                                       how_to_code,
 
1419
                                       where_to_point);
 
1420
  } else {
 
1421
    // Object has not yet been serialized.  Serialize it here.
 
1422
    ObjectSerializer serializer(this,
 
1423
                                heap_object,
 
1424
                                sink_,
 
1425
                                how_to_code,
 
1426
                                where_to_point);
 
1427
    serializer.Serialize();
 
1428
  }
 
1429
}
 
1430
 
 
1431
 
 
1432
void Serializer::ObjectSerializer::Serialize() {
 
1433
  int space = Serializer::SpaceOfObject(object_);
 
1434
  int size = object_->Size();
 
1435
 
 
1436
  sink_->Put(kNewObject + reference_representation_ + space,
 
1437
             "ObjectSerialization");
 
1438
  sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
 
1439
 
 
1440
  LOG(i::Isolate::Current(),
 
1441
      SnapshotPositionEvent(object_->address(), sink_->Position()));
 
1442
 
 
1443
  // Mark this object as already serialized.
 
1444
  bool start_new_page;
 
1445
  int offset = serializer_->Allocate(space, size, &start_new_page);
 
1446
  serializer_->address_mapper()->AddMapping(object_, offset);
 
1447
  if (start_new_page) {
 
1448
    sink_->Put(kNewPage, "NewPage");
 
1449
    sink_->PutSection(space, "NewPageSpace");
 
1450
  }
 
1451
 
 
1452
  // Serialize the map (first word of the object).
 
1453
  serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject);
 
1454
 
 
1455
  // Serialize the rest of the object.
 
1456
  CHECK_EQ(0, bytes_processed_so_far_);
 
1457
  bytes_processed_so_far_ = kPointerSize;
 
1458
  object_->IterateBody(object_->map()->instance_type(), size, this);
 
1459
  OutputRawData(object_->address() + size);
 
1460
}
 
1461
 
 
1462
 
 
1463
void Serializer::ObjectSerializer::VisitPointers(Object** start,
 
1464
                                                 Object** end) {
 
1465
  Object** current = start;
 
1466
  while (current < end) {
 
1467
    while (current < end && (*current)->IsSmi()) current++;
 
1468
    if (current < end) OutputRawData(reinterpret_cast<Address>(current));
 
1469
 
 
1470
    while (current < end && !(*current)->IsSmi()) {
 
1471
      HeapObject* current_contents = HeapObject::cast(*current);
 
1472
      int root_index = serializer_->RootIndex(current_contents, kPlain);
 
1473
      // Repeats are not subject to the write barrier so there are only some
 
1474
      // objects that can be used in a repeat encoding.  These are the early
 
1475
      // ones in the root array that are never in new space.
 
1476
      if (current != start &&
 
1477
          root_index != kInvalidRootIndex &&
 
1478
          root_index < kRootArrayNumberOfConstantEncodings &&
 
1479
          current_contents == current[-1]) {
 
1480
        ASSERT(!HEAP->InNewSpace(current_contents));
 
1481
        int repeat_count = 1;
 
1482
        while (current < end - 1 && current[repeat_count] == current_contents) {
 
1483
          repeat_count++;
 
1484
        }
 
1485
        current += repeat_count;
 
1486
        bytes_processed_so_far_ += repeat_count * kPointerSize;
 
1487
        if (repeat_count > kMaxRepeats) {
 
1488
          sink_->Put(kRepeat, "SerializeRepeats");
 
1489
          sink_->PutInt(repeat_count, "SerializeRepeats");
 
1490
        } else {
 
1491
          sink_->Put(CodeForRepeats(repeat_count), "SerializeRepeats");
 
1492
        }
 
1493
      } else {
 
1494
        serializer_->SerializeObject(current_contents, kPlain, kStartOfObject);
 
1495
        bytes_processed_so_far_ += kPointerSize;
 
1496
        current++;
 
1497
      }
 
1498
    }
 
1499
  }
 
1500
}
 
1501
 
 
1502
 
 
1503
void Serializer::ObjectSerializer::VisitEmbeddedPointer(RelocInfo* rinfo) {
 
1504
  Object** current = rinfo->target_object_address();
 
1505
 
 
1506
  OutputRawData(rinfo->target_address_address());
 
1507
  HowToCode representation = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
 
1508
  serializer_->SerializeObject(*current, representation, kStartOfObject);
 
1509
  bytes_processed_so_far_ += rinfo->target_address_size();
 
1510
}
 
1511
 
 
1512
 
 
1513
void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
 
1514
                                                           Address* end) {
 
1515
  Address references_start = reinterpret_cast<Address>(start);
 
1516
  OutputRawData(references_start);
 
1517
 
 
1518
  for (Address* current = start; current < end; current++) {
 
1519
    sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
 
1520
    int reference_id = serializer_->EncodeExternalReference(*current);
 
1521
    sink_->PutInt(reference_id, "reference id");
 
1522
  }
 
1523
  bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
 
1524
}
 
1525
 
 
1526
 
 
1527
void Serializer::ObjectSerializer::VisitExternalReference(RelocInfo* rinfo) {
 
1528
  Address references_start = rinfo->target_address_address();
 
1529
  OutputRawData(references_start);
 
1530
 
 
1531
  Address* current = rinfo->target_reference_address();
 
1532
  int representation = rinfo->IsCodedSpecially() ?
 
1533
                       kFromCode + kStartOfObject : kPlain + kStartOfObject;
 
1534
  sink_->Put(kExternalReference + representation, "ExternalRef");
 
1535
  int reference_id = serializer_->EncodeExternalReference(*current);
 
1536
  sink_->PutInt(reference_id, "reference id");
 
1537
  bytes_processed_so_far_ += rinfo->target_address_size();
 
1538
}
 
1539
 
 
1540
 
 
1541
void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
 
1542
  Address target_start = rinfo->target_address_address();
 
1543
  OutputRawData(target_start);
 
1544
  Address target = rinfo->target_address();
 
1545
  uint32_t encoding = serializer_->EncodeExternalReference(target);
 
1546
  CHECK(target == NULL ? encoding == 0 : encoding != 0);
 
1547
  int representation;
 
1548
  // Can't use a ternary operator because of gcc.
 
1549
  if (rinfo->IsCodedSpecially()) {
 
1550
    representation = kStartOfObject + kFromCode;
 
1551
  } else {
 
1552
    representation = kStartOfObject + kPlain;
 
1553
  }
 
1554
  sink_->Put(kExternalReference + representation, "ExternalReference");
 
1555
  sink_->PutInt(encoding, "reference id");
 
1556
  bytes_processed_so_far_ += rinfo->target_address_size();
 
1557
}
 
1558
 
 
1559
 
 
1560
void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
 
1561
  CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
 
1562
  Address target_start = rinfo->target_address_address();
 
1563
  OutputRawData(target_start);
 
1564
  Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
 
1565
  serializer_->SerializeObject(target, kFromCode, kInnerPointer);
 
1566
  bytes_processed_so_far_ += rinfo->target_address_size();
 
1567
}
 
1568
 
 
1569
 
 
1570
void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
 
1571
  Code* target = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
 
1572
  OutputRawData(entry_address);
 
1573
  serializer_->SerializeObject(target, kPlain, kInnerPointer);
 
1574
  bytes_processed_so_far_ += kPointerSize;
 
1575
}
 
1576
 
 
1577
 
 
1578
void Serializer::ObjectSerializer::VisitGlobalPropertyCell(RelocInfo* rinfo) {
 
1579
  ASSERT(rinfo->rmode() == RelocInfo::GLOBAL_PROPERTY_CELL);
 
1580
  JSGlobalPropertyCell* cell =
 
1581
      JSGlobalPropertyCell::cast(rinfo->target_cell());
 
1582
  OutputRawData(rinfo->pc());
 
1583
  serializer_->SerializeObject(cell, kPlain, kInnerPointer);
 
1584
}
 
1585
 
 
1586
 
 
1587
void Serializer::ObjectSerializer::VisitExternalAsciiString(
 
1588
    v8::String::ExternalAsciiStringResource** resource_pointer) {
 
1589
  Address references_start = reinterpret_cast<Address>(resource_pointer);
 
1590
  OutputRawData(references_start);
 
1591
  for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
 
1592
    Object* source = HEAP->natives_source_cache()->get(i);
 
1593
    if (!source->IsUndefined()) {
 
1594
      ExternalAsciiString* string = ExternalAsciiString::cast(source);
 
1595
      typedef v8::String::ExternalAsciiStringResource Resource;
 
1596
      const Resource* resource = string->resource();
 
1597
      if (resource == *resource_pointer) {
 
1598
        sink_->Put(kNativesStringResource, "NativesStringResource");
 
1599
        sink_->PutSection(i, "NativesStringResourceEnd");
 
1600
        bytes_processed_so_far_ += sizeof(resource);
 
1601
        return;
 
1602
      }
 
1603
    }
 
1604
  }
 
1605
  // One of the strings in the natives cache should match the resource.  We
 
1606
  // can't serialize any other kinds of external strings.
 
1607
  UNREACHABLE();
 
1608
}
 
1609
 
 
1610
 
 
1611
void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
 
1612
  Address object_start = object_->address();
 
1613
  int up_to_offset = static_cast<int>(up_to - object_start);
 
1614
  int skipped = up_to_offset - bytes_processed_so_far_;
 
1615
  // This assert will fail if the reloc info gives us the target_address_address
 
1616
  // locations in a non-ascending order.  Luckily that doesn't happen.
 
1617
  ASSERT(skipped >= 0);
 
1618
  if (skipped != 0) {
 
1619
    Address base = object_start + bytes_processed_so_far_;
 
1620
#define RAW_CASE(index, length)                                                \
 
1621
    if (skipped == length) {                                                   \
 
1622
      sink_->PutSection(kRawData + index, "RawDataFixed");                     \
 
1623
    } else  /* NOLINT */
 
1624
    COMMON_RAW_LENGTHS(RAW_CASE)
 
1625
#undef RAW_CASE
 
1626
    {  /* NOLINT */
 
1627
      sink_->Put(kRawData, "RawData");
 
1628
      sink_->PutInt(skipped, "length");
 
1629
    }
 
1630
    for (int i = 0; i < skipped; i++) {
 
1631
      unsigned int data = base[i];
 
1632
      sink_->PutSection(data, "Byte");
 
1633
    }
 
1634
    bytes_processed_so_far_ += skipped;
 
1635
  }
 
1636
}
 
1637
 
 
1638
 
 
1639
int Serializer::SpaceOfObject(HeapObject* object) {
 
1640
  for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
 
1641
    AllocationSpace s = static_cast<AllocationSpace>(i);
 
1642
    if (HEAP->InSpace(object, s)) {
 
1643
      if (i == LO_SPACE) {
 
1644
        if (object->IsCode()) {
 
1645
          return kLargeCode;
 
1646
        } else if (object->IsFixedArray()) {
 
1647
          return kLargeFixedArray;
 
1648
        } else {
 
1649
          return kLargeData;
 
1650
        }
 
1651
      }
 
1652
      return i;
 
1653
    }
 
1654
  }
 
1655
  UNREACHABLE();
 
1656
  return 0;
 
1657
}
 
1658
 
 
1659
 
 
1660
int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
 
1661
  for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
 
1662
    AllocationSpace s = static_cast<AllocationSpace>(i);
 
1663
    if (HEAP->InSpace(object, s)) {
 
1664
      return i;
 
1665
    }
 
1666
  }
 
1667
  UNREACHABLE();
 
1668
  return 0;
 
1669
}
 
1670
 
 
1671
 
 
1672
int Serializer::Allocate(int space, int size, bool* new_page) {
 
1673
  CHECK(space >= 0 && space < kNumberOfSpaces);
 
1674
  if (SpaceIsLarge(space)) {
 
1675
    // In large object space we merely number the objects instead of trying to
 
1676
    // determine some sort of address.
 
1677
    *new_page = true;
 
1678
    large_object_total_ += size;
 
1679
    return fullness_[LO_SPACE]++;
 
1680
  }
 
1681
  *new_page = false;
 
1682
  if (fullness_[space] == 0) {
 
1683
    *new_page = true;
 
1684
  }
 
1685
  if (SpaceIsPaged(space)) {
 
1686
    // Paged spaces are a little special.  We encode their addresses as if the
 
1687
    // pages were all contiguous and each page were filled up in the range
 
1688
    // 0 - Page::kObjectAreaSize.  In practice the pages may not be contiguous
 
1689
    // and allocation does not start at offset 0 in the page, but this scheme
 
1690
    // means the deserializer can get the page number quickly by shifting the
 
1691
    // serialized address.
 
1692
    CHECK(IsPowerOf2(Page::kPageSize));
 
1693
    int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
 
1694
    CHECK(size <= SpaceAreaSize(space));
 
1695
    if (used_in_this_page + size > SpaceAreaSize(space)) {
 
1696
      *new_page = true;
 
1697
      fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
 
1698
    }
 
1699
  }
 
1700
  int allocation_address = fullness_[space];
 
1701
  fullness_[space] = allocation_address + size;
 
1702
  return allocation_address;
 
1703
}
 
1704
 
 
1705
 
 
1706
int Serializer::SpaceAreaSize(int space) {
 
1707
  if (space == CODE_SPACE) {
 
1708
    return isolate_->memory_allocator()->CodePageAreaSize();
 
1709
  } else {
 
1710
    return Page::kPageSize - Page::kObjectStartOffset;
 
1711
  }
 
1712
}
 
1713
 
 
1714
 
 
1715
} }  // namespace v8::internal