blob: 15fed442b952aeb953e413d11132bb0e417492f9 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "execution.h"
33#include "global-handles.h"
34#include "ic-inl.h"
35#include "natives.h"
36#include "platform.h"
37#include "runtime.h"
38#include "serialize.h"
39#include "stub-cache.h"
40#include "v8threads.h"
Steve Block3ce2e202009-11-05 08:53:23 +000041#include "top.h"
Steve Blockd0582a62009-12-15 09:54:21 +000042#include "bootstrapper.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44namespace v8 {
45namespace internal {
46
Steve Blocka7e24c12009-10-30 11:49:00 +000047
48// -----------------------------------------------------------------------------
49// Coding of external references.
50
51// The encoding of an external reference. The type is in the high word.
52// The id is in the low word.
53static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
54 return static_cast<uint32_t>(type) << 16 | id;
55}
56
57
58static int* GetInternalPointer(StatsCounter* counter) {
59 // All counters refer to dummy_counter, if deserializing happens without
60 // setting up counters.
61 static int dummy_counter = 0;
62 return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
63}
64
65
66// ExternalReferenceTable is a helper class that defines the relationship
67// between external references and their encodings. It is used to build
68// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
69class ExternalReferenceTable {
70 public:
71 static ExternalReferenceTable* instance() {
72 if (!instance_) instance_ = new ExternalReferenceTable();
73 return instance_;
74 }
75
76 int size() const { return refs_.length(); }
77
78 Address address(int i) { return refs_[i].address; }
79
80 uint32_t code(int i) { return refs_[i].code; }
81
82 const char* name(int i) { return refs_[i].name; }
83
84 int max_id(int code) { return max_id_[code]; }
85
86 private:
87 static ExternalReferenceTable* instance_;
88
89 ExternalReferenceTable() : refs_(64) { PopulateTable(); }
90 ~ExternalReferenceTable() { }
91
92 struct ExternalReferenceEntry {
93 Address address;
94 uint32_t code;
95 const char* name;
96 };
97
98 void PopulateTable();
99
100 // For a few types of references, we can get their address from their id.
101 void AddFromId(TypeCode type, uint16_t id, const char* name);
102
103 // For other types of references, the caller will figure out the address.
104 void Add(Address address, TypeCode type, uint16_t id, const char* name);
105
106 List<ExternalReferenceEntry> refs_;
107 int max_id_[kTypeCodeCount];
108};
109
110
111ExternalReferenceTable* ExternalReferenceTable::instance_ = NULL;
112
113
114void ExternalReferenceTable::AddFromId(TypeCode type,
115 uint16_t id,
116 const char* name) {
117 Address address;
118 switch (type) {
119 case C_BUILTIN: {
120 ExternalReference ref(static_cast<Builtins::CFunctionId>(id));
121 address = ref.address();
122 break;
123 }
124 case BUILTIN: {
125 ExternalReference ref(static_cast<Builtins::Name>(id));
126 address = ref.address();
127 break;
128 }
129 case RUNTIME_FUNCTION: {
130 ExternalReference ref(static_cast<Runtime::FunctionId>(id));
131 address = ref.address();
132 break;
133 }
134 case IC_UTILITY: {
135 ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)));
136 address = ref.address();
137 break;
138 }
139 default:
140 UNREACHABLE();
141 return;
142 }
143 Add(address, type, id, name);
144}
145
146
147void ExternalReferenceTable::Add(Address address,
148 TypeCode type,
149 uint16_t id,
150 const char* name) {
Steve Blockd0582a62009-12-15 09:54:21 +0000151 ASSERT_NE(NULL, address);
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 ExternalReferenceEntry entry;
153 entry.address = address;
154 entry.code = EncodeExternal(type, id);
155 entry.name = name;
Steve Blockd0582a62009-12-15 09:54:21 +0000156 ASSERT_NE(0, entry.code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 refs_.Add(entry);
158 if (id > max_id_[type]) max_id_[type] = id;
159}
160
161
162void ExternalReferenceTable::PopulateTable() {
163 for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
164 max_id_[type_code] = 0;
165 }
166
167 // The following populates all of the different type of external references
168 // into the ExternalReferenceTable.
169 //
170 // NOTE: This function was originally 100k of code. It has since been
171 // rewritten to be mostly table driven, as the callback macro style tends to
172 // very easily cause code bloat. Please be careful in the future when adding
173 // new references.
174
175 struct RefTableEntry {
176 TypeCode type;
177 uint16_t id;
178 const char* name;
179 };
180
181 static const RefTableEntry ref_table[] = {
182 // Builtins
Leon Clarkee46be812010-01-19 14:06:41 +0000183#define DEF_ENTRY_C(name, ignored) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000184 { C_BUILTIN, \
185 Builtins::c_##name, \
186 "Builtins::" #name },
187
188 BUILTIN_LIST_C(DEF_ENTRY_C)
189#undef DEF_ENTRY_C
190
Leon Clarkee46be812010-01-19 14:06:41 +0000191#define DEF_ENTRY_C(name, ignored) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 { BUILTIN, \
193 Builtins::name, \
194 "Builtins::" #name },
Leon Clarkee46be812010-01-19 14:06:41 +0000195#define DEF_ENTRY_A(name, kind, state) DEF_ENTRY_C(name, ignored)
Steve Blocka7e24c12009-10-30 11:49:00 +0000196
197 BUILTIN_LIST_C(DEF_ENTRY_C)
198 BUILTIN_LIST_A(DEF_ENTRY_A)
199 BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
200#undef DEF_ENTRY_C
201#undef DEF_ENTRY_A
202
203 // Runtime functions
204#define RUNTIME_ENTRY(name, nargs, ressize) \
205 { RUNTIME_FUNCTION, \
206 Runtime::k##name, \
207 "Runtime::" #name },
208
209 RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
210#undef RUNTIME_ENTRY
211
212 // IC utilities
213#define IC_ENTRY(name) \
214 { IC_UTILITY, \
215 IC::k##name, \
216 "IC::" #name },
217
218 IC_UTIL_LIST(IC_ENTRY)
219#undef IC_ENTRY
220 }; // end of ref_table[].
221
222 for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
223 AddFromId(ref_table[i].type, ref_table[i].id, ref_table[i].name);
224 }
225
226#ifdef ENABLE_DEBUGGER_SUPPORT
227 // Debug addresses
228 Add(Debug_Address(Debug::k_after_break_target_address).address(),
229 DEBUG_ADDRESS,
230 Debug::k_after_break_target_address << kDebugIdShift,
231 "Debug::after_break_target_address()");
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100232 Add(Debug_Address(Debug::k_debug_break_slot_address).address(),
233 DEBUG_ADDRESS,
234 Debug::k_debug_break_slot_address << kDebugIdShift,
235 "Debug::debug_break_slot_address()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 Add(Debug_Address(Debug::k_debug_break_return_address).address(),
237 DEBUG_ADDRESS,
238 Debug::k_debug_break_return_address << kDebugIdShift,
239 "Debug::debug_break_return_address()");
Ben Murdochbb769b22010-08-11 14:56:33 +0100240 Add(Debug_Address(Debug::k_restarter_frame_function_pointer).address(),
241 DEBUG_ADDRESS,
242 Debug::k_restarter_frame_function_pointer << kDebugIdShift,
243 "Debug::restarter_frame_function_pointer_address()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000244#endif
245
246 // Stat counters
247 struct StatsRefTableEntry {
248 StatsCounter* counter;
249 uint16_t id;
250 const char* name;
251 };
252
253 static const StatsRefTableEntry stats_ref_table[] = {
254#define COUNTER_ENTRY(name, caption) \
255 { &Counters::name, \
256 Counters::k_##name, \
257 "Counters::" #name },
258
259 STATS_COUNTER_LIST_1(COUNTER_ENTRY)
260 STATS_COUNTER_LIST_2(COUNTER_ENTRY)
261#undef COUNTER_ENTRY
262 }; // end of stats_ref_table[].
263
264 for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
265 Add(reinterpret_cast<Address>(
266 GetInternalPointer(stats_ref_table[i].counter)),
267 STATS_COUNTER,
268 stats_ref_table[i].id,
269 stats_ref_table[i].name);
270 }
271
272 // Top addresses
Steve Block3ce2e202009-11-05 08:53:23 +0000273 const char* top_address_format = "Top::%s";
274
275 const char* AddressNames[] = {
276#define C(name) #name,
277 TOP_ADDRESS_LIST(C)
278 TOP_ADDRESS_LIST_PROF(C)
279 NULL
280#undef C
281 };
282
Steve Blockd0582a62009-12-15 09:54:21 +0000283 int top_format_length = StrLength(top_address_format) - 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000284 for (uint16_t i = 0; i < Top::k_top_address_count; ++i) {
Steve Block3ce2e202009-11-05 08:53:23 +0000285 const char* address_name = AddressNames[i];
286 Vector<char> name =
Steve Blockd0582a62009-12-15 09:54:21 +0000287 Vector<char>::New(top_format_length + StrLength(address_name) + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000288 const char* chars = name.start();
Steve Block3ce2e202009-11-05 08:53:23 +0000289 OS::SNPrintF(name, top_address_format, address_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000290 Add(Top::get_address_from_id((Top::AddressId)i), TOP_ADDRESS, i, chars);
291 }
292
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 // Accessors
294#define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
295 Add((Address)&Accessors::name, \
296 ACCESSOR, \
297 Accessors::k##name, \
298 "Accessors::" #name);
299
300 ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
301#undef ACCESSOR_DESCRIPTOR_DECLARATION
302
303 // Stub cache tables
304 Add(SCTableReference::keyReference(StubCache::kPrimary).address(),
305 STUB_CACHE_TABLE,
306 1,
307 "StubCache::primary_->key");
308 Add(SCTableReference::valueReference(StubCache::kPrimary).address(),
309 STUB_CACHE_TABLE,
310 2,
311 "StubCache::primary_->value");
312 Add(SCTableReference::keyReference(StubCache::kSecondary).address(),
313 STUB_CACHE_TABLE,
314 3,
315 "StubCache::secondary_->key");
316 Add(SCTableReference::valueReference(StubCache::kSecondary).address(),
317 STUB_CACHE_TABLE,
318 4,
319 "StubCache::secondary_->value");
320
321 // Runtime entries
322 Add(ExternalReference::perform_gc_function().address(),
323 RUNTIME_ENTRY,
324 1,
325 "Runtime::PerformGC");
Steve Block6ded16b2010-05-10 14:33:55 +0100326 Add(ExternalReference::fill_heap_number_with_random_function().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 RUNTIME_ENTRY,
328 2,
Steve Block6ded16b2010-05-10 14:33:55 +0100329 "V8::FillHeapNumberWithRandom");
330
331 Add(ExternalReference::random_uint32_function().address(),
332 RUNTIME_ENTRY,
333 3,
334 "V8::Random");
Steve Blocka7e24c12009-10-30 11:49:00 +0000335
John Reck59135872010-11-02 12:39:01 -0700336 Add(ExternalReference::delete_handle_scope_extensions().address(),
337 RUNTIME_ENTRY,
338 3,
339 "HandleScope::DeleteExtensions");
340
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 // Miscellaneous
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 Add(ExternalReference::the_hole_value_location().address(),
343 UNCLASSIFIED,
344 2,
345 "Factory::the_hole_value().location()");
346 Add(ExternalReference::roots_address().address(),
347 UNCLASSIFIED,
348 3,
349 "Heap::roots_address()");
Steve Blockd0582a62009-12-15 09:54:21 +0000350 Add(ExternalReference::address_of_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 UNCLASSIFIED,
352 4,
353 "StackGuard::address_of_jslimit()");
Steve Blockd0582a62009-12-15 09:54:21 +0000354 Add(ExternalReference::address_of_real_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 UNCLASSIFIED,
356 5,
Steve Blockd0582a62009-12-15 09:54:21 +0000357 "StackGuard::address_of_real_jslimit()");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100358#ifndef V8_INTERPRETED_REGEXP
Steve Blockd0582a62009-12-15 09:54:21 +0000359 Add(ExternalReference::address_of_regexp_stack_limit().address(),
360 UNCLASSIFIED,
361 6,
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 "RegExpStack::limit_address()");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100363 Add(ExternalReference::address_of_regexp_stack_memory_address().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000365 7,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100366 "RegExpStack::memory_address()");
367 Add(ExternalReference::address_of_regexp_stack_memory_size().address(),
368 UNCLASSIFIED,
369 8,
370 "RegExpStack::memory_size()");
371 Add(ExternalReference::address_of_static_offsets_vector().address(),
372 UNCLASSIFIED,
373 9,
374 "OffsetsVector::static_offsets_vector");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100375#endif // V8_INTERPRETED_REGEXP
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100376 Add(ExternalReference::new_space_start().address(),
377 UNCLASSIFIED,
378 10,
Steve Blocka7e24c12009-10-30 11:49:00 +0000379 "Heap::NewSpaceStart()");
Andrei Popescu402d9372010-02-26 13:31:12 +0000380 Add(ExternalReference::new_space_mask().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000381 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100382 11,
Andrei Popescu402d9372010-02-26 13:31:12 +0000383 "Heap::NewSpaceMask()");
384 Add(ExternalReference::heap_always_allocate_scope_depth().address(),
385 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100386 12,
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 "Heap::always_allocate_scope_depth()");
388 Add(ExternalReference::new_space_allocation_limit_address().address(),
389 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100390 13,
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 "Heap::NewSpaceAllocationLimitAddress()");
392 Add(ExternalReference::new_space_allocation_top_address().address(),
393 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100394 14,
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 "Heap::NewSpaceAllocationTopAddress()");
396#ifdef ENABLE_DEBUGGER_SUPPORT
397 Add(ExternalReference::debug_break().address(),
398 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100399 15,
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 "Debug::Break()");
401 Add(ExternalReference::debug_step_in_fp_address().address(),
402 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100403 16,
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 "Debug::step_in_fp_addr()");
405#endif
406 Add(ExternalReference::double_fp_operation(Token::ADD).address(),
407 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100408 17,
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 "add_two_doubles");
410 Add(ExternalReference::double_fp_operation(Token::SUB).address(),
411 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100412 18,
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 "sub_two_doubles");
414 Add(ExternalReference::double_fp_operation(Token::MUL).address(),
415 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100416 19,
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 "mul_two_doubles");
418 Add(ExternalReference::double_fp_operation(Token::DIV).address(),
419 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100420 20,
Steve Blocka7e24c12009-10-30 11:49:00 +0000421 "div_two_doubles");
422 Add(ExternalReference::double_fp_operation(Token::MOD).address(),
423 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100424 21,
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 "mod_two_doubles");
426 Add(ExternalReference::compare_doubles().address(),
427 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100428 22,
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 "compare_doubles");
Steve Block6ded16b2010-05-10 14:33:55 +0100430#ifndef V8_INTERPRETED_REGEXP
431 Add(ExternalReference::re_case_insensitive_compare_uc16().address(),
432 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100433 23,
Steve Blocka7e24c12009-10-30 11:49:00 +0000434 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
435 Add(ExternalReference::re_check_stack_guard_state().address(),
436 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100437 24,
Steve Blocka7e24c12009-10-30 11:49:00 +0000438 "RegExpMacroAssembler*::CheckStackGuardState()");
439 Add(ExternalReference::re_grow_stack().address(),
440 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100441 25,
Steve Blocka7e24c12009-10-30 11:49:00 +0000442 "NativeRegExpMacroAssembler::GrowStack()");
Leon Clarkee46be812010-01-19 14:06:41 +0000443 Add(ExternalReference::re_word_character_map().address(),
444 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100445 26,
Leon Clarkee46be812010-01-19 14:06:41 +0000446 "NativeRegExpMacroAssembler::word_character_map");
Steve Block6ded16b2010-05-10 14:33:55 +0100447#endif // V8_INTERPRETED_REGEXP
Leon Clarkee46be812010-01-19 14:06:41 +0000448 // Keyed lookup cache.
449 Add(ExternalReference::keyed_lookup_cache_keys().address(),
450 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100451 27,
Leon Clarkee46be812010-01-19 14:06:41 +0000452 "KeyedLookupCache::keys()");
453 Add(ExternalReference::keyed_lookup_cache_field_offsets().address(),
454 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100455 28,
Leon Clarkee46be812010-01-19 14:06:41 +0000456 "KeyedLookupCache::field_offsets()");
Andrei Popescu402d9372010-02-26 13:31:12 +0000457 Add(ExternalReference::transcendental_cache_array_address().address(),
458 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100459 29,
Andrei Popescu402d9372010-02-26 13:31:12 +0000460 "TranscendentalCache::caches()");
John Reck59135872010-11-02 12:39:01 -0700461 Add(ExternalReference::handle_scope_next_address().address(),
462 UNCLASSIFIED,
463 30,
464 "HandleScope::next");
465 Add(ExternalReference::handle_scope_limit_address().address(),
466 UNCLASSIFIED,
467 31,
468 "HandleScope::limit");
469 Add(ExternalReference::handle_scope_level_address().address(),
470 UNCLASSIFIED,
471 32,
472 "HandleScope::level");
Steve Blocka7e24c12009-10-30 11:49:00 +0000473}
474
475
476ExternalReferenceEncoder::ExternalReferenceEncoder()
477 : encodings_(Match) {
478 ExternalReferenceTable* external_references =
479 ExternalReferenceTable::instance();
480 for (int i = 0; i < external_references->size(); ++i) {
481 Put(external_references->address(i), i);
482 }
483}
484
485
486uint32_t ExternalReferenceEncoder::Encode(Address key) const {
487 int index = IndexOf(key);
Ben Murdochbb769b22010-08-11 14:56:33 +0100488 ASSERT(key == NULL || index >= 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000489 return index >=0 ? ExternalReferenceTable::instance()->code(index) : 0;
490}
491
492
493const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
494 int index = IndexOf(key);
495 return index >=0 ? ExternalReferenceTable::instance()->name(index) : NULL;
496}
497
498
499int ExternalReferenceEncoder::IndexOf(Address key) const {
500 if (key == NULL) return -1;
501 HashMap::Entry* entry =
502 const_cast<HashMap &>(encodings_).Lookup(key, Hash(key), false);
503 return entry == NULL
504 ? -1
505 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
506}
507
508
509void ExternalReferenceEncoder::Put(Address key, int index) {
510 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
Steve Block6ded16b2010-05-10 14:33:55 +0100511 entry->value = reinterpret_cast<void*>(index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000512}
513
514
515ExternalReferenceDecoder::ExternalReferenceDecoder()
Ben Murdochf87a2032010-10-22 12:50:53 +0100516 : encodings_(NewArray<Address*>(kTypeCodeCount)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000517 ExternalReferenceTable* external_references =
518 ExternalReferenceTable::instance();
519 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
520 int max = external_references->max_id(type) + 1;
521 encodings_[type] = NewArray<Address>(max + 1);
522 }
523 for (int i = 0; i < external_references->size(); ++i) {
524 Put(external_references->code(i), external_references->address(i));
525 }
526}
527
528
529ExternalReferenceDecoder::~ExternalReferenceDecoder() {
530 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
531 DeleteArray(encodings_[type]);
532 }
533 DeleteArray(encodings_);
534}
535
536
Steve Blocka7e24c12009-10-30 11:49:00 +0000537bool Serializer::serialization_enabled_ = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000538bool Serializer::too_late_to_enable_now_ = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000539ExternalReferenceDecoder* Deserializer::external_reference_decoder_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000540
541
Leon Clarkee46be812010-01-19 14:06:41 +0000542Deserializer::Deserializer(SnapshotByteSource* source) : source_(source) {
Steve Blockd0582a62009-12-15 09:54:21 +0000543}
544
545
546// This routine both allocates a new object, and also keeps
547// track of where objects have been allocated so that we can
548// fix back references when deserializing.
549Address Deserializer::Allocate(int space_index, Space* space, int size) {
550 Address address;
551 if (!SpaceIsLarge(space_index)) {
552 ASSERT(!SpaceIsPaged(space_index) ||
553 size <= Page::kPageSize - Page::kObjectStartOffset);
John Reck59135872010-11-02 12:39:01 -0700554 MaybeObject* maybe_new_allocation;
Steve Blockd0582a62009-12-15 09:54:21 +0000555 if (space_index == NEW_SPACE) {
John Reck59135872010-11-02 12:39:01 -0700556 maybe_new_allocation =
557 reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
Steve Blockd0582a62009-12-15 09:54:21 +0000558 } else {
John Reck59135872010-11-02 12:39:01 -0700559 maybe_new_allocation =
560 reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
Steve Blockd0582a62009-12-15 09:54:21 +0000561 }
John Reck59135872010-11-02 12:39:01 -0700562 Object* new_allocation = maybe_new_allocation->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000563 HeapObject* new_object = HeapObject::cast(new_allocation);
Steve Blockd0582a62009-12-15 09:54:21 +0000564 address = new_object->address();
565 high_water_[space_index] = address + size;
566 } else {
567 ASSERT(SpaceIsLarge(space_index));
568 ASSERT(size > Page::kPageSize - Page::kObjectStartOffset);
569 LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
570 Object* new_allocation;
571 if (space_index == kLargeData) {
John Reck59135872010-11-02 12:39:01 -0700572 new_allocation = lo_space->AllocateRaw(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000573 } else if (space_index == kLargeFixedArray) {
John Reck59135872010-11-02 12:39:01 -0700574 new_allocation =
575 lo_space->AllocateRawFixedArray(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000576 } else {
577 ASSERT_EQ(kLargeCode, space_index);
John Reck59135872010-11-02 12:39:01 -0700578 new_allocation = lo_space->AllocateRawCode(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000579 }
Steve Blockd0582a62009-12-15 09:54:21 +0000580 HeapObject* new_object = HeapObject::cast(new_allocation);
581 // Record all large objects in the same space.
582 address = new_object->address();
Andrei Popescu31002712010-02-23 13:46:05 +0000583 pages_[LO_SPACE].Add(address);
Steve Blockd0582a62009-12-15 09:54:21 +0000584 }
585 last_object_address_ = address;
586 return address;
587}
588
589
590// This returns the address of an object that has been described in the
591// snapshot as being offset bytes back in a particular space.
592HeapObject* Deserializer::GetAddressFromEnd(int space) {
593 int offset = source_->GetInt();
594 ASSERT(!SpaceIsLarge(space));
595 offset <<= kObjectAlignmentBits;
596 return HeapObject::FromAddress(high_water_[space] - offset);
597}
598
599
600// This returns the address of an object that has been described in the
601// snapshot as being offset bytes into a particular space.
602HeapObject* Deserializer::GetAddressFromStart(int space) {
603 int offset = source_->GetInt();
604 if (SpaceIsLarge(space)) {
605 // Large spaces have one object per 'page'.
606 return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
607 }
608 offset <<= kObjectAlignmentBits;
609 if (space == NEW_SPACE) {
610 // New space has only one space - numbered 0.
611 return HeapObject::FromAddress(pages_[space][0] + offset);
612 }
613 ASSERT(SpaceIsPaged(space));
Leon Clarkee46be812010-01-19 14:06:41 +0000614 int page_of_pointee = offset >> kPageSizeBits;
Steve Blockd0582a62009-12-15 09:54:21 +0000615 Address object_address = pages_[space][page_of_pointee] +
616 (offset & Page::kPageAlignmentMask);
617 return HeapObject::FromAddress(object_address);
618}
619
620
621void Deserializer::Deserialize() {
622 // Don't GC while deserializing - just expand the heap.
623 AlwaysAllocateScope always_allocate;
624 // Don't use the free lists while deserializing.
625 LinearAllocationScope allocate_linearly;
626 // No active threads.
627 ASSERT_EQ(NULL, ThreadState::FirstInUse());
628 // No active handles.
629 ASSERT(HandleScopeImplementer::instance()->blocks()->is_empty());
Leon Clarked91b9f72010-01-27 17:25:45 +0000630 // Make sure the entire partial snapshot cache is traversed, filling it with
631 // valid object pointers.
632 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000633 ASSERT_EQ(NULL, external_reference_decoder_);
634 external_reference_decoder_ = new ExternalReferenceDecoder();
Leon Clarked91b9f72010-01-27 17:25:45 +0000635 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
636 Heap::IterateWeakRoots(this, VISIT_ALL);
Ben Murdochf87a2032010-10-22 12:50:53 +0100637
638 Heap::set_global_contexts_list(Heap::undefined_value());
Leon Clarkee46be812010-01-19 14:06:41 +0000639}
640
641
642void Deserializer::DeserializePartial(Object** root) {
643 // Don't GC while deserializing - just expand the heap.
644 AlwaysAllocateScope always_allocate;
645 // Don't use the free lists while deserializing.
646 LinearAllocationScope allocate_linearly;
647 if (external_reference_decoder_ == NULL) {
648 external_reference_decoder_ = new ExternalReferenceDecoder();
649 }
650 VisitPointer(root);
651}
652
653
Leon Clarked91b9f72010-01-27 17:25:45 +0000654Deserializer::~Deserializer() {
655 ASSERT(source_->AtEOF());
Leon Clarkee46be812010-01-19 14:06:41 +0000656 if (external_reference_decoder_ != NULL) {
657 delete external_reference_decoder_;
658 external_reference_decoder_ = NULL;
659 }
Steve Blockd0582a62009-12-15 09:54:21 +0000660}
661
662
663// This is called on the roots. It is the driver of the deserialization
664// process. It is also called on the body of each function.
665void Deserializer::VisitPointers(Object** start, Object** end) {
666 // The space must be new space. Any other space would cause ReadChunk to try
667 // to update the remembered using NULL as the address.
668 ReadChunk(start, end, NEW_SPACE, NULL);
669}
670
671
672// This routine writes the new object into the pointer provided and then
673// returns true if the new object was in young space and false otherwise.
674// The reason for this strange interface is that otherwise the object is
675// written very late, which means the ByteArray map is not set up by the
676// time we need to use it to mark the space at the end of a page free (by
677// making it into a byte array).
678void Deserializer::ReadObject(int space_number,
679 Space* space,
680 Object** write_back) {
681 int size = source_->GetInt() << kObjectAlignmentBits;
682 Address address = Allocate(space_number, space, size);
683 *write_back = HeapObject::FromAddress(address);
684 Object** current = reinterpret_cast<Object**>(address);
685 Object** limit = current + (size >> kPointerSizeLog2);
Leon Clarkee46be812010-01-19 14:06:41 +0000686 if (FLAG_log_snapshot_positions) {
687 LOG(SnapshotPositionEvent(address, source_->position()));
688 }
Steve Blockd0582a62009-12-15 09:54:21 +0000689 ReadChunk(current, limit, space_number, address);
690}
691
692
Leon Clarkef7060e22010-06-03 12:02:55 +0100693// This macro is always used with a constant argument so it should all fold
694// away to almost nothing in the generated code. It might be nicer to do this
695// with the ternary operator but there are type issues with that.
696#define ASSIGN_DEST_SPACE(space_number) \
697 Space* dest_space; \
698 if (space_number == NEW_SPACE) { \
699 dest_space = Heap::new_space(); \
700 } else if (space_number == OLD_POINTER_SPACE) { \
701 dest_space = Heap::old_pointer_space(); \
702 } else if (space_number == OLD_DATA_SPACE) { \
703 dest_space = Heap::old_data_space(); \
704 } else if (space_number == CODE_SPACE) { \
705 dest_space = Heap::code_space(); \
706 } else if (space_number == MAP_SPACE) { \
707 dest_space = Heap::map_space(); \
708 } else if (space_number == CELL_SPACE) { \
709 dest_space = Heap::cell_space(); \
710 } else { \
711 ASSERT(space_number >= LO_SPACE); \
712 dest_space = Heap::lo_space(); \
713 }
714
715
716static const int kUnknownOffsetFromStart = -1;
Steve Blockd0582a62009-12-15 09:54:21 +0000717
718
719void Deserializer::ReadChunk(Object** current,
720 Object** limit,
Leon Clarkef7060e22010-06-03 12:02:55 +0100721 int source_space,
Steve Blockd0582a62009-12-15 09:54:21 +0000722 Address address) {
723 while (current < limit) {
724 int data = source_->Get();
725 switch (data) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100726#define CASE_STATEMENT(where, how, within, space_number) \
727 case where + how + within + space_number: \
728 ASSERT((where & ~kPointedToMask) == 0); \
729 ASSERT((how & ~kHowToCodeMask) == 0); \
730 ASSERT((within & ~kWhereToPointMask) == 0); \
731 ASSERT((space_number & ~kSpaceMask) == 0);
732
733#define CASE_BODY(where, how, within, space_number_if_any, offset_from_start) \
734 { \
735 bool emit_write_barrier = false; \
736 bool current_was_incremented = false; \
737 int space_number = space_number_if_any == kAnyOldSpace ? \
738 (data & kSpaceMask) : space_number_if_any; \
739 if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
740 ASSIGN_DEST_SPACE(space_number) \
741 ReadObject(space_number, dest_space, current); \
742 emit_write_barrier = \
743 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
744 } else { \
745 Object* new_object = NULL; /* May not be a real Object pointer. */ \
746 if (where == kNewObject) { \
747 ASSIGN_DEST_SPACE(space_number) \
748 ReadObject(space_number, dest_space, &new_object); \
749 } else if (where == kRootArray) { \
750 int root_id = source_->GetInt(); \
751 new_object = Heap::roots_address()[root_id]; \
752 } else if (where == kPartialSnapshotCache) { \
753 int cache_index = source_->GetInt(); \
754 new_object = partial_snapshot_cache_[cache_index]; \
755 } else if (where == kExternalReference) { \
756 int reference_id = source_->GetInt(); \
757 Address address = \
758 external_reference_decoder_->Decode(reference_id); \
759 new_object = reinterpret_cast<Object*>(address); \
760 } else if (where == kBackref) { \
761 emit_write_barrier = \
762 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
763 new_object = GetAddressFromEnd(data & kSpaceMask); \
764 } else { \
765 ASSERT(where == kFromStart); \
766 if (offset_from_start == kUnknownOffsetFromStart) { \
767 emit_write_barrier = \
768 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
769 new_object = GetAddressFromStart(data & kSpaceMask); \
770 } else { \
771 Address object_address = pages_[space_number][0] + \
772 (offset_from_start << kObjectAlignmentBits); \
773 new_object = HeapObject::FromAddress(object_address); \
774 } \
775 } \
776 if (within == kFirstInstruction) { \
777 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
778 new_object = reinterpret_cast<Object*>( \
779 new_code_object->instruction_start()); \
780 } \
781 if (how == kFromCode) { \
782 Address location_of_branch_data = \
783 reinterpret_cast<Address>(current); \
784 Assembler::set_target_at(location_of_branch_data, \
785 reinterpret_cast<Address>(new_object)); \
786 if (within == kFirstInstruction) { \
787 location_of_branch_data += Assembler::kCallTargetSize; \
788 current = reinterpret_cast<Object**>(location_of_branch_data); \
789 current_was_incremented = true; \
790 } \
791 } else { \
792 *current = new_object; \
793 } \
794 } \
795 if (emit_write_barrier) { \
796 Heap::RecordWrite(address, static_cast<int>( \
797 reinterpret_cast<Address>(current) - address)); \
798 } \
799 if (!current_was_incremented) { \
800 current++; /* Increment current if it wasn't done above. */ \
801 } \
802 break; \
803 } \
804
805// This generates a case and a body for each space. The large object spaces are
806// very rare in snapshots so they are grouped in one body.
807#define ONE_PER_SPACE(where, how, within) \
808 CASE_STATEMENT(where, how, within, NEW_SPACE) \
809 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
810 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
811 CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart) \
812 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
813 CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart) \
814 CASE_STATEMENT(where, how, within, CODE_SPACE) \
815 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
816 CASE_STATEMENT(where, how, within, CELL_SPACE) \
817 CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart) \
818 CASE_STATEMENT(where, how, within, MAP_SPACE) \
819 CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart) \
820 CASE_STATEMENT(where, how, within, kLargeData) \
821 CASE_STATEMENT(where, how, within, kLargeCode) \
822 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
823 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
824
825// This generates a case and a body for the new space (which has to do extra
826// write barrier handling) and handles the other spaces with 8 fall-through
827// cases and one body.
828#define ALL_SPACES(where, how, within) \
829 CASE_STATEMENT(where, how, within, NEW_SPACE) \
830 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
831 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
832 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
833 CASE_STATEMENT(where, how, within, CODE_SPACE) \
834 CASE_STATEMENT(where, how, within, CELL_SPACE) \
835 CASE_STATEMENT(where, how, within, MAP_SPACE) \
836 CASE_STATEMENT(where, how, within, kLargeData) \
837 CASE_STATEMENT(where, how, within, kLargeCode) \
838 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
839 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
840
Steve Block791712a2010-08-27 10:21:07 +0100841#define ONE_PER_CODE_SPACE(where, how, within) \
842 CASE_STATEMENT(where, how, within, CODE_SPACE) \
843 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
844 CASE_STATEMENT(where, how, within, kLargeCode) \
845 CASE_BODY(where, how, within, LO_SPACE, kUnknownOffsetFromStart)
846
Leon Clarkef7060e22010-06-03 12:02:55 +0100847#define EMIT_COMMON_REFERENCE_PATTERNS(pseudo_space_number, \
848 space_number, \
849 offset_from_start) \
850 CASE_STATEMENT(kFromStart, kPlain, kStartOfObject, pseudo_space_number) \
851 CASE_BODY(kFromStart, kPlain, kStartOfObject, space_number, offset_from_start)
852
853 // We generate 15 cases and bodies that process special tags that combine
854 // the raw data tag and the length into one byte.
Steve Blockd0582a62009-12-15 09:54:21 +0000855#define RAW_CASE(index, size) \
Leon Clarkef7060e22010-06-03 12:02:55 +0100856 case kRawData + index: { \
Steve Blockd0582a62009-12-15 09:54:21 +0000857 byte* raw_data_out = reinterpret_cast<byte*>(current); \
858 source_->CopyRaw(raw_data_out, size); \
859 current = reinterpret_cast<Object**>(raw_data_out + size); \
860 break; \
861 }
862 COMMON_RAW_LENGTHS(RAW_CASE)
863#undef RAW_CASE
Leon Clarkef7060e22010-06-03 12:02:55 +0100864
865 // Deserialize a chunk of raw data that doesn't have one of the popular
866 // lengths.
867 case kRawData: {
Steve Blockd0582a62009-12-15 09:54:21 +0000868 int size = source_->GetInt();
869 byte* raw_data_out = reinterpret_cast<byte*>(current);
870 source_->CopyRaw(raw_data_out, size);
871 current = reinterpret_cast<Object**>(raw_data_out + size);
872 break;
873 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100874
875 // Deserialize a new object and write a pointer to it to the current
876 // object.
877 ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100878 // Support for direct instruction pointers in functions
879 ONE_PER_CODE_SPACE(kNewObject, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100880 // Deserialize a new code object and write a pointer to its first
881 // instruction to the current code object.
882 ONE_PER_SPACE(kNewObject, kFromCode, kFirstInstruction)
883 // Find a recently deserialized object using its offset from the current
884 // allocation point and write a pointer to it to the current object.
885 ALL_SPACES(kBackref, kPlain, kStartOfObject)
886 // Find a recently deserialized code object using its offset from the
887 // current allocation point and write a pointer to its first instruction
Steve Block791712a2010-08-27 10:21:07 +0100888 // to the current code object or the instruction pointer in a function
889 // object.
Leon Clarkef7060e22010-06-03 12:02:55 +0100890 ALL_SPACES(kBackref, kFromCode, kFirstInstruction)
Steve Block791712a2010-08-27 10:21:07 +0100891 ALL_SPACES(kBackref, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100892 // Find an already deserialized object using its offset from the start
893 // and write a pointer to it to the current object.
894 ALL_SPACES(kFromStart, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100895 ALL_SPACES(kFromStart, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100896 // Find an already deserialized code object using its offset from the
897 // start and write a pointer to its first instruction to the current code
898 // object.
899 ALL_SPACES(kFromStart, kFromCode, kFirstInstruction)
900 // Find an already deserialized object at one of the predetermined popular
901 // offsets from the start and write a pointer to it in the current object.
902 COMMON_REFERENCE_PATTERNS(EMIT_COMMON_REFERENCE_PATTERNS)
903 // Find an object in the roots array and write a pointer to it to the
904 // current object.
905 CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
906 CASE_BODY(kRootArray, kPlain, kStartOfObject, 0, kUnknownOffsetFromStart)
907 // Find an object in the partial snapshots cache and write a pointer to it
908 // to the current object.
909 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
910 CASE_BODY(kPartialSnapshotCache,
911 kPlain,
912 kStartOfObject,
913 0,
914 kUnknownOffsetFromStart)
Steve Block791712a2010-08-27 10:21:07 +0100915 // Find an code entry in the partial snapshots cache and
916 // write a pointer to it to the current object.
917 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kFirstInstruction, 0)
918 CASE_BODY(kPartialSnapshotCache,
919 kPlain,
920 kFirstInstruction,
921 0,
922 kUnknownOffsetFromStart)
Leon Clarkef7060e22010-06-03 12:02:55 +0100923 // Find an external reference and write a pointer to it to the current
924 // object.
925 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
926 CASE_BODY(kExternalReference,
927 kPlain,
928 kStartOfObject,
929 0,
930 kUnknownOffsetFromStart)
931 // Find an external reference and write a pointer to it in the current
932 // code object.
933 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
934 CASE_BODY(kExternalReference,
935 kFromCode,
936 kStartOfObject,
937 0,
938 kUnknownOffsetFromStart)
939
940#undef CASE_STATEMENT
941#undef CASE_BODY
942#undef ONE_PER_SPACE
943#undef ALL_SPACES
944#undef EMIT_COMMON_REFERENCE_PATTERNS
945#undef ASSIGN_DEST_SPACE
946
947 case kNewPage: {
Steve Blockd0582a62009-12-15 09:54:21 +0000948 int space = source_->Get();
949 pages_[space].Add(last_object_address_);
Steve Block6ded16b2010-05-10 14:33:55 +0100950 if (space == CODE_SPACE) {
951 CPU::FlushICache(last_object_address_, Page::kPageSize);
952 }
Steve Blockd0582a62009-12-15 09:54:21 +0000953 break;
954 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100955
956 case kNativesStringResource: {
Steve Blockd0582a62009-12-15 09:54:21 +0000957 int index = source_->Get();
958 Vector<const char> source_vector = Natives::GetScriptSource(index);
959 NativesExternalStringResource* resource =
960 new NativesExternalStringResource(source_vector.start());
961 *current++ = reinterpret_cast<Object*>(resource);
962 break;
963 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100964
965 case kSynchronize: {
Leon Clarked91b9f72010-01-27 17:25:45 +0000966 // If we get here then that indicates that you have a mismatch between
967 // the number of GC roots when serializing and deserializing.
968 UNREACHABLE();
969 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100970
Steve Blockd0582a62009-12-15 09:54:21 +0000971 default:
972 UNREACHABLE();
973 }
974 }
975 ASSERT_EQ(current, limit);
976}
977
978
979void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
980 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
981 for (int shift = max_shift; shift > 0; shift -= 7) {
982 if (integer >= static_cast<uintptr_t>(1u) << shift) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000983 Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart");
Steve Blockd0582a62009-12-15 09:54:21 +0000984 }
985 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000986 PutSection(static_cast<int>(integer & 0x7f), "IntLastPart");
Steve Blockd0582a62009-12-15 09:54:21 +0000987}
988
Steve Blocka7e24c12009-10-30 11:49:00 +0000989#ifdef DEBUG
Steve Blockd0582a62009-12-15 09:54:21 +0000990
991void Deserializer::Synchronize(const char* tag) {
992 int data = source_->Get();
993 // If this assert fails then that indicates that you have a mismatch between
994 // the number of GC roots when serializing and deserializing.
Leon Clarkef7060e22010-06-03 12:02:55 +0100995 ASSERT_EQ(kSynchronize, data);
Steve Blockd0582a62009-12-15 09:54:21 +0000996 do {
997 int character = source_->Get();
998 if (character == 0) break;
999 if (FLAG_debug_serialization) {
1000 PrintF("%c", character);
1001 }
1002 } while (true);
1003 if (FLAG_debug_serialization) {
1004 PrintF("\n");
1005 }
1006}
1007
Steve Blocka7e24c12009-10-30 11:49:00 +00001008
1009void Serializer::Synchronize(const char* tag) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001010 sink_->Put(kSynchronize, tag);
Steve Blockd0582a62009-12-15 09:54:21 +00001011 int character;
1012 do {
1013 character = *tag++;
1014 sink_->PutSection(character, "TagCharacter");
1015 } while (character != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001016}
Steve Blockd0582a62009-12-15 09:54:21 +00001017
Steve Blocka7e24c12009-10-30 11:49:00 +00001018#endif
1019
Steve Blockd0582a62009-12-15 09:54:21 +00001020Serializer::Serializer(SnapshotByteSink* sink)
1021 : sink_(sink),
1022 current_root_index_(0),
Andrei Popescu31002712010-02-23 13:46:05 +00001023 external_reference_encoder_(new ExternalReferenceEncoder),
Leon Clarkee46be812010-01-19 14:06:41 +00001024 large_object_total_(0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001025 for (int i = 0; i <= LAST_SPACE; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +00001026 fullness_[i] = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001027 }
1028}
1029
1030
Andrei Popescu31002712010-02-23 13:46:05 +00001031Serializer::~Serializer() {
1032 delete external_reference_encoder_;
1033}
1034
1035
Leon Clarked91b9f72010-01-27 17:25:45 +00001036void StartupSerializer::SerializeStrongReferences() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001037 // No active threads.
1038 CHECK_EQ(NULL, ThreadState::FirstInUse());
1039 // No active or weak handles.
1040 CHECK(HandleScopeImplementer::instance()->blocks()->is_empty());
1041 CHECK_EQ(0, GlobalHandles::NumberOfWeakHandles());
Steve Blockd0582a62009-12-15 09:54:21 +00001042 // We don't support serializing installed extensions.
1043 for (RegisteredExtension* ext = RegisteredExtension::first_extension();
1044 ext != NULL;
1045 ext = ext->next()) {
1046 CHECK_NE(v8::INSTALLED, ext->state());
1047 }
Leon Clarked91b9f72010-01-27 17:25:45 +00001048 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
Steve Blocka7e24c12009-10-30 11:49:00 +00001049}
1050
1051
Leon Clarked91b9f72010-01-27 17:25:45 +00001052void PartialSerializer::Serialize(Object** object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001053 this->VisitPointer(object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001054
1055 // After we have done the partial serialization the partial snapshot cache
1056 // will contain some references needed to decode the partial snapshot. We
1057 // fill it up with undefineds so it has a predictable length so the
1058 // deserialization code doesn't need to know the length.
1059 for (int index = partial_snapshot_cache_length_;
1060 index < kPartialSnapshotCacheCapacity;
1061 index++) {
1062 partial_snapshot_cache_[index] = Heap::undefined_value();
1063 startup_serializer_->VisitPointer(&partial_snapshot_cache_[index]);
1064 }
1065 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Leon Clarkee46be812010-01-19 14:06:41 +00001066}
1067
1068
Steve Blocka7e24c12009-10-30 11:49:00 +00001069void Serializer::VisitPointers(Object** start, Object** end) {
Steve Blockd0582a62009-12-15 09:54:21 +00001070 for (Object** current = start; current < end; current++) {
1071 if ((*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001072 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001073 sink_->PutInt(kPointerSize, "length");
1074 for (int i = 0; i < kPointerSize; i++) {
1075 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1076 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001077 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001078 SerializeObject(*current, kPlain, kStartOfObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001079 }
1080 }
1081}
1082
1083
Leon Clarked91b9f72010-01-27 17:25:45 +00001084Object* SerializerDeserializer::partial_snapshot_cache_[
1085 kPartialSnapshotCacheCapacity];
1086int SerializerDeserializer::partial_snapshot_cache_length_ = 0;
1087
1088
1089// This ensures that the partial snapshot cache keeps things alive during GC and
1090// tracks their movement. When it is called during serialization of the startup
1091// snapshot the partial snapshot is empty, so nothing happens. When the partial
1092// (context) snapshot is created, this array is populated with the pointers that
1093// the partial snapshot will need. As that happens we emit serialized objects to
1094// the startup snapshot that correspond to the elements of this cache array. On
1095// deserialization we therefore need to visit the cache array. This fills it up
1096// with pointers to deserialized objects.
Steve Block6ded16b2010-05-10 14:33:55 +01001097void SerializerDeserializer::Iterate(ObjectVisitor* visitor) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001098 visitor->VisitPointers(
1099 &partial_snapshot_cache_[0],
1100 &partial_snapshot_cache_[partial_snapshot_cache_length_]);
1101}
1102
1103
1104// When deserializing we need to set the size of the snapshot cache. This means
1105// the root iteration code (above) will iterate over array elements, writing the
1106// references to deserialized objects in them.
1107void SerializerDeserializer::SetSnapshotCacheSize(int size) {
1108 partial_snapshot_cache_length_ = size;
1109}
1110
1111
1112int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1113 for (int i = 0; i < partial_snapshot_cache_length_; i++) {
1114 Object* entry = partial_snapshot_cache_[i];
1115 if (entry == heap_object) return i;
1116 }
Andrei Popescu31002712010-02-23 13:46:05 +00001117
Leon Clarked91b9f72010-01-27 17:25:45 +00001118 // We didn't find the object in the cache. So we add it to the cache and
1119 // then visit the pointer so that it becomes part of the startup snapshot
1120 // and we can refer to it from the partial snapshot.
1121 int length = partial_snapshot_cache_length_;
1122 CHECK(length < kPartialSnapshotCacheCapacity);
1123 partial_snapshot_cache_[length] = heap_object;
1124 startup_serializer_->VisitPointer(&partial_snapshot_cache_[length]);
1125 // We don't recurse from the startup snapshot generator into the partial
1126 // snapshot generator.
1127 ASSERT(length == partial_snapshot_cache_length_);
1128 return partial_snapshot_cache_length_++;
1129}
1130
1131
1132int PartialSerializer::RootIndex(HeapObject* heap_object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001133 for (int i = 0; i < Heap::kRootListLength; i++) {
1134 Object* root = Heap::roots_address()[i];
1135 if (root == heap_object) return i;
1136 }
1137 return kInvalidRootIndex;
1138}
1139
1140
Leon Clarked91b9f72010-01-27 17:25:45 +00001141// Encode the location of an already deserialized object in order to write its
1142// location into a later object. We can encode the location as an offset from
1143// the start of the deserialized objects or as an offset backwards from the
1144// current allocation pointer.
1145void Serializer::SerializeReferenceToPreviousObject(
1146 int space,
1147 int address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001148 HowToCode how_to_code,
1149 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001150 int offset = CurrentAllocationAddress(space) - address;
1151 bool from_start = true;
1152 if (SpaceIsPaged(space)) {
1153 // For paged space it is simple to encode back from current allocation if
1154 // the object is on the same page as the current allocation pointer.
1155 if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1156 (address >> kPageSizeBits)) {
1157 from_start = false;
1158 address = offset;
1159 }
1160 } else if (space == NEW_SPACE) {
1161 // For new space it is always simple to encode back from current allocation.
1162 if (offset < address) {
1163 from_start = false;
1164 address = offset;
1165 }
1166 }
1167 // If we are actually dealing with real offsets (and not a numbering of
1168 // all objects) then we should shift out the bits that are always 0.
1169 if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
Leon Clarkef7060e22010-06-03 12:02:55 +01001170 if (from_start) {
1171#define COMMON_REFS_CASE(pseudo_space, actual_space, offset) \
1172 if (space == actual_space && address == offset && \
1173 how_to_code == kPlain && where_to_point == kStartOfObject) { \
1174 sink_->Put(kFromStart + how_to_code + where_to_point + \
1175 pseudo_space, "RefSer"); \
1176 } else /* NOLINT */
1177 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
1178#undef COMMON_REFS_CASE
1179 { /* NOLINT */
1180 sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer");
Leon Clarked91b9f72010-01-27 17:25:45 +00001181 sink_->PutInt(address, "address");
1182 }
1183 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001184 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1185 sink_->PutInt(address, "address");
Leon Clarked91b9f72010-01-27 17:25:45 +00001186 }
1187}
1188
1189
1190void StartupSerializer::SerializeObject(
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001191 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001192 HowToCode how_to_code,
1193 WhereToPoint where_to_point) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001194 CHECK(o->IsHeapObject());
1195 HeapObject* heap_object = HeapObject::cast(o);
Leon Clarked91b9f72010-01-27 17:25:45 +00001196
1197 if (address_mapper_.IsMapped(heap_object)) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001198 int space = SpaceOfAlreadySerializedObject(heap_object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001199 int address = address_mapper_.MappedTo(heap_object);
1200 SerializeReferenceToPreviousObject(space,
1201 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001202 how_to_code,
1203 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001204 } else {
1205 // Object has not yet been serialized. Serialize it here.
1206 ObjectSerializer object_serializer(this,
1207 heap_object,
1208 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001209 how_to_code,
1210 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001211 object_serializer.Serialize();
1212 }
1213}
1214
1215
1216void StartupSerializer::SerializeWeakReferences() {
1217 for (int i = partial_snapshot_cache_length_;
1218 i < kPartialSnapshotCacheCapacity;
1219 i++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001220 sink_->Put(kRootArray + kPlain + kStartOfObject, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001221 sink_->PutInt(Heap::kUndefinedValueRootIndex, "root_index");
1222 }
1223 Heap::IterateWeakRoots(this, VISIT_ALL);
1224}
1225
1226
1227void PartialSerializer::SerializeObject(
1228 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001229 HowToCode how_to_code,
1230 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001231 CHECK(o->IsHeapObject());
1232 HeapObject* heap_object = HeapObject::cast(o);
1233
1234 int root_index;
1235 if ((root_index = RootIndex(heap_object)) != kInvalidRootIndex) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001236 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001237 sink_->PutInt(root_index, "root_index");
1238 return;
1239 }
1240
1241 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1242 int cache_index = PartialSnapshotCacheIndex(heap_object);
Leon Clarkef7060e22010-06-03 12:02:55 +01001243 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1244 "PartialSnapshotCache");
Leon Clarked91b9f72010-01-27 17:25:45 +00001245 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1246 return;
1247 }
1248
1249 // Pointers from the partial snapshot to the objects in the startup snapshot
1250 // should go through the root array or through the partial snapshot cache.
1251 // If this is not the case you may have to add something to the root array.
1252 ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1253 // All the symbols that the partial snapshot needs should be either in the
1254 // root table or in the partial snapshot cache.
1255 ASSERT(!heap_object->IsSymbol());
1256
1257 if (address_mapper_.IsMapped(heap_object)) {
1258 int space = SpaceOfAlreadySerializedObject(heap_object);
1259 int address = address_mapper_.MappedTo(heap_object);
1260 SerializeReferenceToPreviousObject(space,
1261 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001262 how_to_code,
1263 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001264 } else {
1265 // Object has not yet been serialized. Serialize it here.
1266 ObjectSerializer serializer(this,
1267 heap_object,
1268 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001269 how_to_code,
1270 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001271 serializer.Serialize();
1272 }
1273}
1274
1275
Steve Blockd0582a62009-12-15 09:54:21 +00001276void Serializer::ObjectSerializer::Serialize() {
1277 int space = Serializer::SpaceOfObject(object_);
1278 int size = object_->Size();
1279
Leon Clarkef7060e22010-06-03 12:02:55 +01001280 sink_->Put(kNewObject + reference_representation_ + space,
1281 "ObjectSerialization");
Steve Blockd0582a62009-12-15 09:54:21 +00001282 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1283
Leon Clarkee46be812010-01-19 14:06:41 +00001284 LOG(SnapshotPositionEvent(object_->address(), sink_->Position()));
1285
Steve Blockd0582a62009-12-15 09:54:21 +00001286 // Mark this object as already serialized.
1287 bool start_new_page;
Leon Clarked91b9f72010-01-27 17:25:45 +00001288 int offset = serializer_->Allocate(space, size, &start_new_page);
1289 serializer_->address_mapper()->AddMapping(object_, offset);
Steve Blockd0582a62009-12-15 09:54:21 +00001290 if (start_new_page) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001291 sink_->Put(kNewPage, "NewPage");
Steve Blockd0582a62009-12-15 09:54:21 +00001292 sink_->PutSection(space, "NewPageSpace");
1293 }
1294
1295 // Serialize the map (first word of the object).
Leon Clarkef7060e22010-06-03 12:02:55 +01001296 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001297
1298 // Serialize the rest of the object.
1299 CHECK_EQ(0, bytes_processed_so_far_);
1300 bytes_processed_so_far_ = kPointerSize;
1301 object_->IterateBody(object_->map()->instance_type(), size, this);
1302 OutputRawData(object_->address() + size);
1303}
1304
1305
1306void Serializer::ObjectSerializer::VisitPointers(Object** start,
1307 Object** end) {
1308 Object** current = start;
1309 while (current < end) {
1310 while (current < end && (*current)->IsSmi()) current++;
1311 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1312
1313 while (current < end && !(*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001314 serializer_->SerializeObject(*current, kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001315 bytes_processed_so_far_ += kPointerSize;
1316 current++;
1317 }
1318 }
1319}
1320
1321
1322void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1323 Address* end) {
1324 Address references_start = reinterpret_cast<Address>(start);
1325 OutputRawData(references_start);
1326
1327 for (Address* current = start; current < end; current++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001328 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
Steve Blockd0582a62009-12-15 09:54:21 +00001329 int reference_id = serializer_->EncodeExternalReference(*current);
1330 sink_->PutInt(reference_id, "reference id");
1331 }
1332 bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1333}
1334
1335
1336void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1337 Address target_start = rinfo->target_address_address();
1338 OutputRawData(target_start);
1339 Address target = rinfo->target_address();
1340 uint32_t encoding = serializer_->EncodeExternalReference(target);
1341 CHECK(target == NULL ? encoding == 0 : encoding != 0);
Leon Clarkef7060e22010-06-03 12:02:55 +01001342 int representation;
1343 // Can't use a ternary operator because of gcc.
1344 if (rinfo->IsCodedSpecially()) {
1345 representation = kStartOfObject + kFromCode;
1346 } else {
1347 representation = kStartOfObject + kPlain;
1348 }
1349 sink_->Put(kExternalReference + representation, "ExternalReference");
Steve Blockd0582a62009-12-15 09:54:21 +00001350 sink_->PutInt(encoding, "reference id");
Leon Clarkef7060e22010-06-03 12:02:55 +01001351 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001352}
1353
1354
1355void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1356 CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1357 Address target_start = rinfo->target_address_address();
1358 OutputRawData(target_start);
1359 Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
Leon Clarkef7060e22010-06-03 12:02:55 +01001360 serializer_->SerializeObject(target, kFromCode, kFirstInstruction);
1361 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001362}
1363
1364
Steve Block791712a2010-08-27 10:21:07 +01001365void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1366 Code* target = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1367 OutputRawData(entry_address);
1368 serializer_->SerializeObject(target, kPlain, kFirstInstruction);
1369 bytes_processed_so_far_ += kPointerSize;
1370}
1371
1372
Steve Blockd0582a62009-12-15 09:54:21 +00001373void Serializer::ObjectSerializer::VisitExternalAsciiString(
1374 v8::String::ExternalAsciiStringResource** resource_pointer) {
1375 Address references_start = reinterpret_cast<Address>(resource_pointer);
1376 OutputRawData(references_start);
1377 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1378 Object* source = Heap::natives_source_cache()->get(i);
1379 if (!source->IsUndefined()) {
1380 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1381 typedef v8::String::ExternalAsciiStringResource Resource;
1382 Resource* resource = string->resource();
1383 if (resource == *resource_pointer) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001384 sink_->Put(kNativesStringResource, "NativesStringResource");
Steve Blockd0582a62009-12-15 09:54:21 +00001385 sink_->PutSection(i, "NativesStringResourceEnd");
1386 bytes_processed_so_far_ += sizeof(resource);
1387 return;
1388 }
1389 }
1390 }
1391 // One of the strings in the natives cache should match the resource. We
1392 // can't serialize any other kinds of external strings.
1393 UNREACHABLE();
1394}
1395
1396
1397void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1398 Address object_start = object_->address();
1399 int up_to_offset = static_cast<int>(up_to - object_start);
1400 int skipped = up_to_offset - bytes_processed_so_far_;
1401 // This assert will fail if the reloc info gives us the target_address_address
1402 // locations in a non-ascending order. Luckily that doesn't happen.
1403 ASSERT(skipped >= 0);
1404 if (skipped != 0) {
1405 Address base = object_start + bytes_processed_so_far_;
1406#define RAW_CASE(index, length) \
1407 if (skipped == length) { \
Leon Clarkef7060e22010-06-03 12:02:55 +01001408 sink_->PutSection(kRawData + index, "RawDataFixed"); \
Steve Blockd0582a62009-12-15 09:54:21 +00001409 } else /* NOLINT */
1410 COMMON_RAW_LENGTHS(RAW_CASE)
1411#undef RAW_CASE
1412 { /* NOLINT */
Leon Clarkef7060e22010-06-03 12:02:55 +01001413 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001414 sink_->PutInt(skipped, "length");
1415 }
1416 for (int i = 0; i < skipped; i++) {
1417 unsigned int data = base[i];
1418 sink_->PutSection(data, "Byte");
1419 }
1420 bytes_processed_so_far_ += skipped;
1421 }
1422}
1423
1424
1425int Serializer::SpaceOfObject(HeapObject* object) {
1426 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1427 AllocationSpace s = static_cast<AllocationSpace>(i);
1428 if (Heap::InSpace(object, s)) {
1429 if (i == LO_SPACE) {
1430 if (object->IsCode()) {
1431 return kLargeCode;
1432 } else if (object->IsFixedArray()) {
1433 return kLargeFixedArray;
1434 } else {
1435 return kLargeData;
1436 }
1437 }
1438 return i;
1439 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001440 }
1441 UNREACHABLE();
Steve Blockd0582a62009-12-15 09:54:21 +00001442 return 0;
1443}
1444
1445
1446int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1447 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1448 AllocationSpace s = static_cast<AllocationSpace>(i);
1449 if (Heap::InSpace(object, s)) {
1450 return i;
1451 }
1452 }
1453 UNREACHABLE();
1454 return 0;
1455}
1456
1457
1458int Serializer::Allocate(int space, int size, bool* new_page) {
1459 CHECK(space >= 0 && space < kNumberOfSpaces);
1460 if (SpaceIsLarge(space)) {
1461 // In large object space we merely number the objects instead of trying to
1462 // determine some sort of address.
1463 *new_page = true;
Leon Clarkee46be812010-01-19 14:06:41 +00001464 large_object_total_ += size;
Steve Blockd0582a62009-12-15 09:54:21 +00001465 return fullness_[LO_SPACE]++;
1466 }
1467 *new_page = false;
1468 if (fullness_[space] == 0) {
1469 *new_page = true;
1470 }
1471 if (SpaceIsPaged(space)) {
1472 // Paged spaces are a little special. We encode their addresses as if the
1473 // pages were all contiguous and each page were filled up in the range
1474 // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1475 // and allocation does not start at offset 0 in the page, but this scheme
1476 // means the deserializer can get the page number quickly by shifting the
1477 // serialized address.
1478 CHECK(IsPowerOf2(Page::kPageSize));
1479 int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1480 CHECK(size <= Page::kObjectAreaSize);
1481 if (used_in_this_page + size > Page::kObjectAreaSize) {
1482 *new_page = true;
1483 fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1484 }
1485 }
1486 int allocation_address = fullness_[space];
1487 fullness_[space] = allocation_address + size;
1488 return allocation_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001489}
1490
1491
1492} } // namespace v8::internal