blob: ccba737d15908980fe9d1cdf7a8f425ae0b4be50 [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
293 // Extensions
294 Add(FUNCTION_ADDR(GCExtension::GC), EXTENSION, 1,
295 "GCExtension::GC");
296
297 // Accessors
298#define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
299 Add((Address)&Accessors::name, \
300 ACCESSOR, \
301 Accessors::k##name, \
302 "Accessors::" #name);
303
304 ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
305#undef ACCESSOR_DESCRIPTOR_DECLARATION
306
307 // Stub cache tables
308 Add(SCTableReference::keyReference(StubCache::kPrimary).address(),
309 STUB_CACHE_TABLE,
310 1,
311 "StubCache::primary_->key");
312 Add(SCTableReference::valueReference(StubCache::kPrimary).address(),
313 STUB_CACHE_TABLE,
314 2,
315 "StubCache::primary_->value");
316 Add(SCTableReference::keyReference(StubCache::kSecondary).address(),
317 STUB_CACHE_TABLE,
318 3,
319 "StubCache::secondary_->key");
320 Add(SCTableReference::valueReference(StubCache::kSecondary).address(),
321 STUB_CACHE_TABLE,
322 4,
323 "StubCache::secondary_->value");
324
325 // Runtime entries
326 Add(ExternalReference::perform_gc_function().address(),
327 RUNTIME_ENTRY,
328 1,
329 "Runtime::PerformGC");
Steve Block6ded16b2010-05-10 14:33:55 +0100330 Add(ExternalReference::fill_heap_number_with_random_function().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000331 RUNTIME_ENTRY,
332 2,
Steve Block6ded16b2010-05-10 14:33:55 +0100333 "V8::FillHeapNumberWithRandom");
334
335 Add(ExternalReference::random_uint32_function().address(),
336 RUNTIME_ENTRY,
337 3,
338 "V8::Random");
Steve Blocka7e24c12009-10-30 11:49:00 +0000339
340 // Miscellaneous
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 Add(ExternalReference::the_hole_value_location().address(),
342 UNCLASSIFIED,
343 2,
344 "Factory::the_hole_value().location()");
345 Add(ExternalReference::roots_address().address(),
346 UNCLASSIFIED,
347 3,
348 "Heap::roots_address()");
Steve Blockd0582a62009-12-15 09:54:21 +0000349 Add(ExternalReference::address_of_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 UNCLASSIFIED,
351 4,
352 "StackGuard::address_of_jslimit()");
Steve Blockd0582a62009-12-15 09:54:21 +0000353 Add(ExternalReference::address_of_real_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 UNCLASSIFIED,
355 5,
Steve Blockd0582a62009-12-15 09:54:21 +0000356 "StackGuard::address_of_real_jslimit()");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100357#ifndef V8_INTERPRETED_REGEXP
Steve Blockd0582a62009-12-15 09:54:21 +0000358 Add(ExternalReference::address_of_regexp_stack_limit().address(),
359 UNCLASSIFIED,
360 6,
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 "RegExpStack::limit_address()");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100362 Add(ExternalReference::address_of_regexp_stack_memory_address().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000363 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000364 7,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100365 "RegExpStack::memory_address()");
366 Add(ExternalReference::address_of_regexp_stack_memory_size().address(),
367 UNCLASSIFIED,
368 8,
369 "RegExpStack::memory_size()");
370 Add(ExternalReference::address_of_static_offsets_vector().address(),
371 UNCLASSIFIED,
372 9,
373 "OffsetsVector::static_offsets_vector");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100374#endif // V8_INTERPRETED_REGEXP
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100375 Add(ExternalReference::new_space_start().address(),
376 UNCLASSIFIED,
377 10,
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 "Heap::NewSpaceStart()");
Andrei Popescu402d9372010-02-26 13:31:12 +0000379 Add(ExternalReference::new_space_mask().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100381 11,
Andrei Popescu402d9372010-02-26 13:31:12 +0000382 "Heap::NewSpaceMask()");
383 Add(ExternalReference::heap_always_allocate_scope_depth().address(),
384 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100385 12,
Steve Blocka7e24c12009-10-30 11:49:00 +0000386 "Heap::always_allocate_scope_depth()");
387 Add(ExternalReference::new_space_allocation_limit_address().address(),
388 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100389 13,
Steve Blocka7e24c12009-10-30 11:49:00 +0000390 "Heap::NewSpaceAllocationLimitAddress()");
391 Add(ExternalReference::new_space_allocation_top_address().address(),
392 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100393 14,
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 "Heap::NewSpaceAllocationTopAddress()");
395#ifdef ENABLE_DEBUGGER_SUPPORT
396 Add(ExternalReference::debug_break().address(),
397 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100398 15,
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 "Debug::Break()");
400 Add(ExternalReference::debug_step_in_fp_address().address(),
401 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100402 16,
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 "Debug::step_in_fp_addr()");
404#endif
405 Add(ExternalReference::double_fp_operation(Token::ADD).address(),
406 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100407 17,
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 "add_two_doubles");
409 Add(ExternalReference::double_fp_operation(Token::SUB).address(),
410 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100411 18,
Steve Blocka7e24c12009-10-30 11:49:00 +0000412 "sub_two_doubles");
413 Add(ExternalReference::double_fp_operation(Token::MUL).address(),
414 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100415 19,
Steve Blocka7e24c12009-10-30 11:49:00 +0000416 "mul_two_doubles");
417 Add(ExternalReference::double_fp_operation(Token::DIV).address(),
418 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100419 20,
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 "div_two_doubles");
421 Add(ExternalReference::double_fp_operation(Token::MOD).address(),
422 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100423 21,
Steve Blocka7e24c12009-10-30 11:49:00 +0000424 "mod_two_doubles");
425 Add(ExternalReference::compare_doubles().address(),
426 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100427 22,
Steve Blocka7e24c12009-10-30 11:49:00 +0000428 "compare_doubles");
Steve Block6ded16b2010-05-10 14:33:55 +0100429#ifndef V8_INTERPRETED_REGEXP
430 Add(ExternalReference::re_case_insensitive_compare_uc16().address(),
431 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100432 23,
Steve Blocka7e24c12009-10-30 11:49:00 +0000433 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
434 Add(ExternalReference::re_check_stack_guard_state().address(),
435 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100436 24,
Steve Blocka7e24c12009-10-30 11:49:00 +0000437 "RegExpMacroAssembler*::CheckStackGuardState()");
438 Add(ExternalReference::re_grow_stack().address(),
439 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100440 25,
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 "NativeRegExpMacroAssembler::GrowStack()");
Leon Clarkee46be812010-01-19 14:06:41 +0000442 Add(ExternalReference::re_word_character_map().address(),
443 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100444 26,
Leon Clarkee46be812010-01-19 14:06:41 +0000445 "NativeRegExpMacroAssembler::word_character_map");
Steve Block6ded16b2010-05-10 14:33:55 +0100446#endif // V8_INTERPRETED_REGEXP
Leon Clarkee46be812010-01-19 14:06:41 +0000447 // Keyed lookup cache.
448 Add(ExternalReference::keyed_lookup_cache_keys().address(),
449 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100450 27,
Leon Clarkee46be812010-01-19 14:06:41 +0000451 "KeyedLookupCache::keys()");
452 Add(ExternalReference::keyed_lookup_cache_field_offsets().address(),
453 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100454 28,
Leon Clarkee46be812010-01-19 14:06:41 +0000455 "KeyedLookupCache::field_offsets()");
Andrei Popescu402d9372010-02-26 13:31:12 +0000456 Add(ExternalReference::transcendental_cache_array_address().address(),
457 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100458 29,
Andrei Popescu402d9372010-02-26 13:31:12 +0000459 "TranscendentalCache::caches()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000460}
461
462
463ExternalReferenceEncoder::ExternalReferenceEncoder()
464 : encodings_(Match) {
465 ExternalReferenceTable* external_references =
466 ExternalReferenceTable::instance();
467 for (int i = 0; i < external_references->size(); ++i) {
468 Put(external_references->address(i), i);
469 }
470}
471
472
473uint32_t ExternalReferenceEncoder::Encode(Address key) const {
474 int index = IndexOf(key);
Ben Murdochbb769b22010-08-11 14:56:33 +0100475 ASSERT(key == NULL || index >= 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000476 return index >=0 ? ExternalReferenceTable::instance()->code(index) : 0;
477}
478
479
480const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
481 int index = IndexOf(key);
482 return index >=0 ? ExternalReferenceTable::instance()->name(index) : NULL;
483}
484
485
486int ExternalReferenceEncoder::IndexOf(Address key) const {
487 if (key == NULL) return -1;
488 HashMap::Entry* entry =
489 const_cast<HashMap &>(encodings_).Lookup(key, Hash(key), false);
490 return entry == NULL
491 ? -1
492 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
493}
494
495
496void ExternalReferenceEncoder::Put(Address key, int index) {
497 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
Steve Block6ded16b2010-05-10 14:33:55 +0100498 entry->value = reinterpret_cast<void*>(index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000499}
500
501
502ExternalReferenceDecoder::ExternalReferenceDecoder()
Ben Murdochf87a2032010-10-22 12:50:53 +0100503 : encodings_(NewArray<Address*>(kTypeCodeCount)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000504 ExternalReferenceTable* external_references =
505 ExternalReferenceTable::instance();
506 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
507 int max = external_references->max_id(type) + 1;
508 encodings_[type] = NewArray<Address>(max + 1);
509 }
510 for (int i = 0; i < external_references->size(); ++i) {
511 Put(external_references->code(i), external_references->address(i));
512 }
513}
514
515
516ExternalReferenceDecoder::~ExternalReferenceDecoder() {
517 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
518 DeleteArray(encodings_[type]);
519 }
520 DeleteArray(encodings_);
521}
522
523
Steve Blocka7e24c12009-10-30 11:49:00 +0000524bool Serializer::serialization_enabled_ = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000525bool Serializer::too_late_to_enable_now_ = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000526ExternalReferenceDecoder* Deserializer::external_reference_decoder_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000527
528
Leon Clarkee46be812010-01-19 14:06:41 +0000529Deserializer::Deserializer(SnapshotByteSource* source) : source_(source) {
Steve Blockd0582a62009-12-15 09:54:21 +0000530}
531
532
533// This routine both allocates a new object, and also keeps
534// track of where objects have been allocated so that we can
535// fix back references when deserializing.
536Address Deserializer::Allocate(int space_index, Space* space, int size) {
537 Address address;
538 if (!SpaceIsLarge(space_index)) {
539 ASSERT(!SpaceIsPaged(space_index) ||
540 size <= Page::kPageSize - Page::kObjectStartOffset);
541 Object* new_allocation;
542 if (space_index == NEW_SPACE) {
543 new_allocation = reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
544 } else {
545 new_allocation = reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
546 }
547 HeapObject* new_object = HeapObject::cast(new_allocation);
548 ASSERT(!new_object->IsFailure());
549 address = new_object->address();
550 high_water_[space_index] = address + size;
551 } else {
552 ASSERT(SpaceIsLarge(space_index));
553 ASSERT(size > Page::kPageSize - Page::kObjectStartOffset);
554 LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
555 Object* new_allocation;
556 if (space_index == kLargeData) {
557 new_allocation = lo_space->AllocateRaw(size);
558 } else if (space_index == kLargeFixedArray) {
559 new_allocation = lo_space->AllocateRawFixedArray(size);
560 } else {
561 ASSERT_EQ(kLargeCode, space_index);
562 new_allocation = lo_space->AllocateRawCode(size);
563 }
564 ASSERT(!new_allocation->IsFailure());
565 HeapObject* new_object = HeapObject::cast(new_allocation);
566 // Record all large objects in the same space.
567 address = new_object->address();
Andrei Popescu31002712010-02-23 13:46:05 +0000568 pages_[LO_SPACE].Add(address);
Steve Blockd0582a62009-12-15 09:54:21 +0000569 }
570 last_object_address_ = address;
571 return address;
572}
573
574
575// This returns the address of an object that has been described in the
576// snapshot as being offset bytes back in a particular space.
577HeapObject* Deserializer::GetAddressFromEnd(int space) {
578 int offset = source_->GetInt();
579 ASSERT(!SpaceIsLarge(space));
580 offset <<= kObjectAlignmentBits;
581 return HeapObject::FromAddress(high_water_[space] - offset);
582}
583
584
585// This returns the address of an object that has been described in the
586// snapshot as being offset bytes into a particular space.
587HeapObject* Deserializer::GetAddressFromStart(int space) {
588 int offset = source_->GetInt();
589 if (SpaceIsLarge(space)) {
590 // Large spaces have one object per 'page'.
591 return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
592 }
593 offset <<= kObjectAlignmentBits;
594 if (space == NEW_SPACE) {
595 // New space has only one space - numbered 0.
596 return HeapObject::FromAddress(pages_[space][0] + offset);
597 }
598 ASSERT(SpaceIsPaged(space));
Leon Clarkee46be812010-01-19 14:06:41 +0000599 int page_of_pointee = offset >> kPageSizeBits;
Steve Blockd0582a62009-12-15 09:54:21 +0000600 Address object_address = pages_[space][page_of_pointee] +
601 (offset & Page::kPageAlignmentMask);
602 return HeapObject::FromAddress(object_address);
603}
604
605
606void Deserializer::Deserialize() {
607 // Don't GC while deserializing - just expand the heap.
608 AlwaysAllocateScope always_allocate;
609 // Don't use the free lists while deserializing.
610 LinearAllocationScope allocate_linearly;
611 // No active threads.
612 ASSERT_EQ(NULL, ThreadState::FirstInUse());
613 // No active handles.
614 ASSERT(HandleScopeImplementer::instance()->blocks()->is_empty());
Leon Clarked91b9f72010-01-27 17:25:45 +0000615 // Make sure the entire partial snapshot cache is traversed, filling it with
616 // valid object pointers.
617 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000618 ASSERT_EQ(NULL, external_reference_decoder_);
619 external_reference_decoder_ = new ExternalReferenceDecoder();
Leon Clarked91b9f72010-01-27 17:25:45 +0000620 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
621 Heap::IterateWeakRoots(this, VISIT_ALL);
Ben Murdochf87a2032010-10-22 12:50:53 +0100622
623 Heap::set_global_contexts_list(Heap::undefined_value());
Leon Clarkee46be812010-01-19 14:06:41 +0000624}
625
626
627void Deserializer::DeserializePartial(Object** root) {
628 // Don't GC while deserializing - just expand the heap.
629 AlwaysAllocateScope always_allocate;
630 // Don't use the free lists while deserializing.
631 LinearAllocationScope allocate_linearly;
632 if (external_reference_decoder_ == NULL) {
633 external_reference_decoder_ = new ExternalReferenceDecoder();
634 }
635 VisitPointer(root);
636}
637
638
Leon Clarked91b9f72010-01-27 17:25:45 +0000639Deserializer::~Deserializer() {
640 ASSERT(source_->AtEOF());
Leon Clarkee46be812010-01-19 14:06:41 +0000641 if (external_reference_decoder_ != NULL) {
642 delete external_reference_decoder_;
643 external_reference_decoder_ = NULL;
644 }
Steve Blockd0582a62009-12-15 09:54:21 +0000645}
646
647
648// This is called on the roots. It is the driver of the deserialization
649// process. It is also called on the body of each function.
650void Deserializer::VisitPointers(Object** start, Object** end) {
651 // The space must be new space. Any other space would cause ReadChunk to try
652 // to update the remembered using NULL as the address.
653 ReadChunk(start, end, NEW_SPACE, NULL);
654}
655
656
657// This routine writes the new object into the pointer provided and then
658// returns true if the new object was in young space and false otherwise.
659// The reason for this strange interface is that otherwise the object is
660// written very late, which means the ByteArray map is not set up by the
661// time we need to use it to mark the space at the end of a page free (by
662// making it into a byte array).
663void Deserializer::ReadObject(int space_number,
664 Space* space,
665 Object** write_back) {
666 int size = source_->GetInt() << kObjectAlignmentBits;
667 Address address = Allocate(space_number, space, size);
668 *write_back = HeapObject::FromAddress(address);
669 Object** current = reinterpret_cast<Object**>(address);
670 Object** limit = current + (size >> kPointerSizeLog2);
Leon Clarkee46be812010-01-19 14:06:41 +0000671 if (FLAG_log_snapshot_positions) {
672 LOG(SnapshotPositionEvent(address, source_->position()));
673 }
Steve Blockd0582a62009-12-15 09:54:21 +0000674 ReadChunk(current, limit, space_number, address);
675}
676
677
Leon Clarkef7060e22010-06-03 12:02:55 +0100678// This macro is always used with a constant argument so it should all fold
679// away to almost nothing in the generated code. It might be nicer to do this
680// with the ternary operator but there are type issues with that.
681#define ASSIGN_DEST_SPACE(space_number) \
682 Space* dest_space; \
683 if (space_number == NEW_SPACE) { \
684 dest_space = Heap::new_space(); \
685 } else if (space_number == OLD_POINTER_SPACE) { \
686 dest_space = Heap::old_pointer_space(); \
687 } else if (space_number == OLD_DATA_SPACE) { \
688 dest_space = Heap::old_data_space(); \
689 } else if (space_number == CODE_SPACE) { \
690 dest_space = Heap::code_space(); \
691 } else if (space_number == MAP_SPACE) { \
692 dest_space = Heap::map_space(); \
693 } else if (space_number == CELL_SPACE) { \
694 dest_space = Heap::cell_space(); \
695 } else { \
696 ASSERT(space_number >= LO_SPACE); \
697 dest_space = Heap::lo_space(); \
698 }
699
700
701static const int kUnknownOffsetFromStart = -1;
Steve Blockd0582a62009-12-15 09:54:21 +0000702
703
704void Deserializer::ReadChunk(Object** current,
705 Object** limit,
Leon Clarkef7060e22010-06-03 12:02:55 +0100706 int source_space,
Steve Blockd0582a62009-12-15 09:54:21 +0000707 Address address) {
708 while (current < limit) {
709 int data = source_->Get();
710 switch (data) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100711#define CASE_STATEMENT(where, how, within, space_number) \
712 case where + how + within + space_number: \
713 ASSERT((where & ~kPointedToMask) == 0); \
714 ASSERT((how & ~kHowToCodeMask) == 0); \
715 ASSERT((within & ~kWhereToPointMask) == 0); \
716 ASSERT((space_number & ~kSpaceMask) == 0);
717
718#define CASE_BODY(where, how, within, space_number_if_any, offset_from_start) \
719 { \
720 bool emit_write_barrier = false; \
721 bool current_was_incremented = false; \
722 int space_number = space_number_if_any == kAnyOldSpace ? \
723 (data & kSpaceMask) : space_number_if_any; \
724 if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
725 ASSIGN_DEST_SPACE(space_number) \
726 ReadObject(space_number, dest_space, current); \
727 emit_write_barrier = \
728 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
729 } else { \
730 Object* new_object = NULL; /* May not be a real Object pointer. */ \
731 if (where == kNewObject) { \
732 ASSIGN_DEST_SPACE(space_number) \
733 ReadObject(space_number, dest_space, &new_object); \
734 } else if (where == kRootArray) { \
735 int root_id = source_->GetInt(); \
736 new_object = Heap::roots_address()[root_id]; \
737 } else if (where == kPartialSnapshotCache) { \
738 int cache_index = source_->GetInt(); \
739 new_object = partial_snapshot_cache_[cache_index]; \
740 } else if (where == kExternalReference) { \
741 int reference_id = source_->GetInt(); \
742 Address address = \
743 external_reference_decoder_->Decode(reference_id); \
744 new_object = reinterpret_cast<Object*>(address); \
745 } else if (where == kBackref) { \
746 emit_write_barrier = \
747 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
748 new_object = GetAddressFromEnd(data & kSpaceMask); \
749 } else { \
750 ASSERT(where == kFromStart); \
751 if (offset_from_start == kUnknownOffsetFromStart) { \
752 emit_write_barrier = \
753 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
754 new_object = GetAddressFromStart(data & kSpaceMask); \
755 } else { \
756 Address object_address = pages_[space_number][0] + \
757 (offset_from_start << kObjectAlignmentBits); \
758 new_object = HeapObject::FromAddress(object_address); \
759 } \
760 } \
761 if (within == kFirstInstruction) { \
762 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
763 new_object = reinterpret_cast<Object*>( \
764 new_code_object->instruction_start()); \
765 } \
766 if (how == kFromCode) { \
767 Address location_of_branch_data = \
768 reinterpret_cast<Address>(current); \
769 Assembler::set_target_at(location_of_branch_data, \
770 reinterpret_cast<Address>(new_object)); \
771 if (within == kFirstInstruction) { \
772 location_of_branch_data += Assembler::kCallTargetSize; \
773 current = reinterpret_cast<Object**>(location_of_branch_data); \
774 current_was_incremented = true; \
775 } \
776 } else { \
777 *current = new_object; \
778 } \
779 } \
780 if (emit_write_barrier) { \
781 Heap::RecordWrite(address, static_cast<int>( \
782 reinterpret_cast<Address>(current) - address)); \
783 } \
784 if (!current_was_incremented) { \
785 current++; /* Increment current if it wasn't done above. */ \
786 } \
787 break; \
788 } \
789
790// This generates a case and a body for each space. The large object spaces are
791// very rare in snapshots so they are grouped in one body.
792#define ONE_PER_SPACE(where, how, within) \
793 CASE_STATEMENT(where, how, within, NEW_SPACE) \
794 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
795 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
796 CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart) \
797 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
798 CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart) \
799 CASE_STATEMENT(where, how, within, CODE_SPACE) \
800 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
801 CASE_STATEMENT(where, how, within, CELL_SPACE) \
802 CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart) \
803 CASE_STATEMENT(where, how, within, MAP_SPACE) \
804 CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart) \
805 CASE_STATEMENT(where, how, within, kLargeData) \
806 CASE_STATEMENT(where, how, within, kLargeCode) \
807 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
808 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
809
810// This generates a case and a body for the new space (which has to do extra
811// write barrier handling) and handles the other spaces with 8 fall-through
812// cases and one body.
813#define ALL_SPACES(where, how, within) \
814 CASE_STATEMENT(where, how, within, NEW_SPACE) \
815 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
816 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
817 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
818 CASE_STATEMENT(where, how, within, CODE_SPACE) \
819 CASE_STATEMENT(where, how, within, CELL_SPACE) \
820 CASE_STATEMENT(where, how, within, MAP_SPACE) \
821 CASE_STATEMENT(where, how, within, kLargeData) \
822 CASE_STATEMENT(where, how, within, kLargeCode) \
823 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
824 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
825
Steve Block791712a2010-08-27 10:21:07 +0100826#define ONE_PER_CODE_SPACE(where, how, within) \
827 CASE_STATEMENT(where, how, within, CODE_SPACE) \
828 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
829 CASE_STATEMENT(where, how, within, kLargeCode) \
830 CASE_BODY(where, how, within, LO_SPACE, kUnknownOffsetFromStart)
831
Leon Clarkef7060e22010-06-03 12:02:55 +0100832#define EMIT_COMMON_REFERENCE_PATTERNS(pseudo_space_number, \
833 space_number, \
834 offset_from_start) \
835 CASE_STATEMENT(kFromStart, kPlain, kStartOfObject, pseudo_space_number) \
836 CASE_BODY(kFromStart, kPlain, kStartOfObject, space_number, offset_from_start)
837
838 // We generate 15 cases and bodies that process special tags that combine
839 // the raw data tag and the length into one byte.
Steve Blockd0582a62009-12-15 09:54:21 +0000840#define RAW_CASE(index, size) \
Leon Clarkef7060e22010-06-03 12:02:55 +0100841 case kRawData + index: { \
Steve Blockd0582a62009-12-15 09:54:21 +0000842 byte* raw_data_out = reinterpret_cast<byte*>(current); \
843 source_->CopyRaw(raw_data_out, size); \
844 current = reinterpret_cast<Object**>(raw_data_out + size); \
845 break; \
846 }
847 COMMON_RAW_LENGTHS(RAW_CASE)
848#undef RAW_CASE
Leon Clarkef7060e22010-06-03 12:02:55 +0100849
850 // Deserialize a chunk of raw data that doesn't have one of the popular
851 // lengths.
852 case kRawData: {
Steve Blockd0582a62009-12-15 09:54:21 +0000853 int size = source_->GetInt();
854 byte* raw_data_out = reinterpret_cast<byte*>(current);
855 source_->CopyRaw(raw_data_out, size);
856 current = reinterpret_cast<Object**>(raw_data_out + size);
857 break;
858 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100859
860 // Deserialize a new object and write a pointer to it to the current
861 // object.
862 ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100863 // Support for direct instruction pointers in functions
864 ONE_PER_CODE_SPACE(kNewObject, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100865 // Deserialize a new code object and write a pointer to its first
866 // instruction to the current code object.
867 ONE_PER_SPACE(kNewObject, kFromCode, kFirstInstruction)
868 // Find a recently deserialized object using its offset from the current
869 // allocation point and write a pointer to it to the current object.
870 ALL_SPACES(kBackref, kPlain, kStartOfObject)
871 // Find a recently deserialized code object using its offset from the
872 // current allocation point and write a pointer to its first instruction
Steve Block791712a2010-08-27 10:21:07 +0100873 // to the current code object or the instruction pointer in a function
874 // object.
Leon Clarkef7060e22010-06-03 12:02:55 +0100875 ALL_SPACES(kBackref, kFromCode, kFirstInstruction)
Steve Block791712a2010-08-27 10:21:07 +0100876 ALL_SPACES(kBackref, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100877 // Find an already deserialized object using its offset from the start
878 // and write a pointer to it to the current object.
879 ALL_SPACES(kFromStart, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100880 ALL_SPACES(kFromStart, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100881 // Find an already deserialized code object using its offset from the
882 // start and write a pointer to its first instruction to the current code
883 // object.
884 ALL_SPACES(kFromStart, kFromCode, kFirstInstruction)
885 // Find an already deserialized object at one of the predetermined popular
886 // offsets from the start and write a pointer to it in the current object.
887 COMMON_REFERENCE_PATTERNS(EMIT_COMMON_REFERENCE_PATTERNS)
888 // Find an object in the roots array and write a pointer to it to the
889 // current object.
890 CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
891 CASE_BODY(kRootArray, kPlain, kStartOfObject, 0, kUnknownOffsetFromStart)
892 // Find an object in the partial snapshots cache and write a pointer to it
893 // to the current object.
894 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
895 CASE_BODY(kPartialSnapshotCache,
896 kPlain,
897 kStartOfObject,
898 0,
899 kUnknownOffsetFromStart)
Steve Block791712a2010-08-27 10:21:07 +0100900 // Find an code entry in the partial snapshots cache and
901 // write a pointer to it to the current object.
902 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kFirstInstruction, 0)
903 CASE_BODY(kPartialSnapshotCache,
904 kPlain,
905 kFirstInstruction,
906 0,
907 kUnknownOffsetFromStart)
Leon Clarkef7060e22010-06-03 12:02:55 +0100908 // Find an external reference and write a pointer to it to the current
909 // object.
910 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
911 CASE_BODY(kExternalReference,
912 kPlain,
913 kStartOfObject,
914 0,
915 kUnknownOffsetFromStart)
916 // Find an external reference and write a pointer to it in the current
917 // code object.
918 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
919 CASE_BODY(kExternalReference,
920 kFromCode,
921 kStartOfObject,
922 0,
923 kUnknownOffsetFromStart)
924
925#undef CASE_STATEMENT
926#undef CASE_BODY
927#undef ONE_PER_SPACE
928#undef ALL_SPACES
929#undef EMIT_COMMON_REFERENCE_PATTERNS
930#undef ASSIGN_DEST_SPACE
931
932 case kNewPage: {
Steve Blockd0582a62009-12-15 09:54:21 +0000933 int space = source_->Get();
934 pages_[space].Add(last_object_address_);
Steve Block6ded16b2010-05-10 14:33:55 +0100935 if (space == CODE_SPACE) {
936 CPU::FlushICache(last_object_address_, Page::kPageSize);
937 }
Steve Blockd0582a62009-12-15 09:54:21 +0000938 break;
939 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100940
941 case kNativesStringResource: {
Steve Blockd0582a62009-12-15 09:54:21 +0000942 int index = source_->Get();
943 Vector<const char> source_vector = Natives::GetScriptSource(index);
944 NativesExternalStringResource* resource =
945 new NativesExternalStringResource(source_vector.start());
946 *current++ = reinterpret_cast<Object*>(resource);
947 break;
948 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100949
950 case kSynchronize: {
Leon Clarked91b9f72010-01-27 17:25:45 +0000951 // If we get here then that indicates that you have a mismatch between
952 // the number of GC roots when serializing and deserializing.
953 UNREACHABLE();
954 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100955
Steve Blockd0582a62009-12-15 09:54:21 +0000956 default:
957 UNREACHABLE();
958 }
959 }
960 ASSERT_EQ(current, limit);
961}
962
963
964void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
965 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
966 for (int shift = max_shift; shift > 0; shift -= 7) {
967 if (integer >= static_cast<uintptr_t>(1u) << shift) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000968 Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart");
Steve Blockd0582a62009-12-15 09:54:21 +0000969 }
970 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000971 PutSection(static_cast<int>(integer & 0x7f), "IntLastPart");
Steve Blockd0582a62009-12-15 09:54:21 +0000972}
973
Steve Blocka7e24c12009-10-30 11:49:00 +0000974#ifdef DEBUG
Steve Blockd0582a62009-12-15 09:54:21 +0000975
976void Deserializer::Synchronize(const char* tag) {
977 int data = source_->Get();
978 // If this assert fails then that indicates that you have a mismatch between
979 // the number of GC roots when serializing and deserializing.
Leon Clarkef7060e22010-06-03 12:02:55 +0100980 ASSERT_EQ(kSynchronize, data);
Steve Blockd0582a62009-12-15 09:54:21 +0000981 do {
982 int character = source_->Get();
983 if (character == 0) break;
984 if (FLAG_debug_serialization) {
985 PrintF("%c", character);
986 }
987 } while (true);
988 if (FLAG_debug_serialization) {
989 PrintF("\n");
990 }
991}
992
Steve Blocka7e24c12009-10-30 11:49:00 +0000993
994void Serializer::Synchronize(const char* tag) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100995 sink_->Put(kSynchronize, tag);
Steve Blockd0582a62009-12-15 09:54:21 +0000996 int character;
997 do {
998 character = *tag++;
999 sink_->PutSection(character, "TagCharacter");
1000 } while (character != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001001}
Steve Blockd0582a62009-12-15 09:54:21 +00001002
Steve Blocka7e24c12009-10-30 11:49:00 +00001003#endif
1004
Steve Blockd0582a62009-12-15 09:54:21 +00001005Serializer::Serializer(SnapshotByteSink* sink)
1006 : sink_(sink),
1007 current_root_index_(0),
Andrei Popescu31002712010-02-23 13:46:05 +00001008 external_reference_encoder_(new ExternalReferenceEncoder),
Leon Clarkee46be812010-01-19 14:06:41 +00001009 large_object_total_(0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001010 for (int i = 0; i <= LAST_SPACE; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +00001011 fullness_[i] = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001012 }
1013}
1014
1015
Andrei Popescu31002712010-02-23 13:46:05 +00001016Serializer::~Serializer() {
1017 delete external_reference_encoder_;
1018}
1019
1020
Leon Clarked91b9f72010-01-27 17:25:45 +00001021void StartupSerializer::SerializeStrongReferences() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001022 // No active threads.
1023 CHECK_EQ(NULL, ThreadState::FirstInUse());
1024 // No active or weak handles.
1025 CHECK(HandleScopeImplementer::instance()->blocks()->is_empty());
1026 CHECK_EQ(0, GlobalHandles::NumberOfWeakHandles());
Steve Blockd0582a62009-12-15 09:54:21 +00001027 // We don't support serializing installed extensions.
1028 for (RegisteredExtension* ext = RegisteredExtension::first_extension();
1029 ext != NULL;
1030 ext = ext->next()) {
1031 CHECK_NE(v8::INSTALLED, ext->state());
1032 }
Leon Clarked91b9f72010-01-27 17:25:45 +00001033 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
Steve Blocka7e24c12009-10-30 11:49:00 +00001034}
1035
1036
Leon Clarked91b9f72010-01-27 17:25:45 +00001037void PartialSerializer::Serialize(Object** object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001038 this->VisitPointer(object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001039
1040 // After we have done the partial serialization the partial snapshot cache
1041 // will contain some references needed to decode the partial snapshot. We
1042 // fill it up with undefineds so it has a predictable length so the
1043 // deserialization code doesn't need to know the length.
1044 for (int index = partial_snapshot_cache_length_;
1045 index < kPartialSnapshotCacheCapacity;
1046 index++) {
1047 partial_snapshot_cache_[index] = Heap::undefined_value();
1048 startup_serializer_->VisitPointer(&partial_snapshot_cache_[index]);
1049 }
1050 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Leon Clarkee46be812010-01-19 14:06:41 +00001051}
1052
1053
Steve Blocka7e24c12009-10-30 11:49:00 +00001054void Serializer::VisitPointers(Object** start, Object** end) {
Steve Blockd0582a62009-12-15 09:54:21 +00001055 for (Object** current = start; current < end; current++) {
1056 if ((*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001057 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001058 sink_->PutInt(kPointerSize, "length");
1059 for (int i = 0; i < kPointerSize; i++) {
1060 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1061 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001062 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001063 SerializeObject(*current, kPlain, kStartOfObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001064 }
1065 }
1066}
1067
1068
Leon Clarked91b9f72010-01-27 17:25:45 +00001069Object* SerializerDeserializer::partial_snapshot_cache_[
1070 kPartialSnapshotCacheCapacity];
1071int SerializerDeserializer::partial_snapshot_cache_length_ = 0;
1072
1073
1074// This ensures that the partial snapshot cache keeps things alive during GC and
1075// tracks their movement. When it is called during serialization of the startup
1076// snapshot the partial snapshot is empty, so nothing happens. When the partial
1077// (context) snapshot is created, this array is populated with the pointers that
1078// the partial snapshot will need. As that happens we emit serialized objects to
1079// the startup snapshot that correspond to the elements of this cache array. On
1080// deserialization we therefore need to visit the cache array. This fills it up
1081// with pointers to deserialized objects.
Steve Block6ded16b2010-05-10 14:33:55 +01001082void SerializerDeserializer::Iterate(ObjectVisitor* visitor) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001083 visitor->VisitPointers(
1084 &partial_snapshot_cache_[0],
1085 &partial_snapshot_cache_[partial_snapshot_cache_length_]);
1086}
1087
1088
1089// When deserializing we need to set the size of the snapshot cache. This means
1090// the root iteration code (above) will iterate over array elements, writing the
1091// references to deserialized objects in them.
1092void SerializerDeserializer::SetSnapshotCacheSize(int size) {
1093 partial_snapshot_cache_length_ = size;
1094}
1095
1096
1097int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1098 for (int i = 0; i < partial_snapshot_cache_length_; i++) {
1099 Object* entry = partial_snapshot_cache_[i];
1100 if (entry == heap_object) return i;
1101 }
Andrei Popescu31002712010-02-23 13:46:05 +00001102
Leon Clarked91b9f72010-01-27 17:25:45 +00001103 // We didn't find the object in the cache. So we add it to the cache and
1104 // then visit the pointer so that it becomes part of the startup snapshot
1105 // and we can refer to it from the partial snapshot.
1106 int length = partial_snapshot_cache_length_;
1107 CHECK(length < kPartialSnapshotCacheCapacity);
1108 partial_snapshot_cache_[length] = heap_object;
1109 startup_serializer_->VisitPointer(&partial_snapshot_cache_[length]);
1110 // We don't recurse from the startup snapshot generator into the partial
1111 // snapshot generator.
1112 ASSERT(length == partial_snapshot_cache_length_);
1113 return partial_snapshot_cache_length_++;
1114}
1115
1116
1117int PartialSerializer::RootIndex(HeapObject* heap_object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001118 for (int i = 0; i < Heap::kRootListLength; i++) {
1119 Object* root = Heap::roots_address()[i];
1120 if (root == heap_object) return i;
1121 }
1122 return kInvalidRootIndex;
1123}
1124
1125
Leon Clarked91b9f72010-01-27 17:25:45 +00001126// Encode the location of an already deserialized object in order to write its
1127// location into a later object. We can encode the location as an offset from
1128// the start of the deserialized objects or as an offset backwards from the
1129// current allocation pointer.
1130void Serializer::SerializeReferenceToPreviousObject(
1131 int space,
1132 int address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001133 HowToCode how_to_code,
1134 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001135 int offset = CurrentAllocationAddress(space) - address;
1136 bool from_start = true;
1137 if (SpaceIsPaged(space)) {
1138 // For paged space it is simple to encode back from current allocation if
1139 // the object is on the same page as the current allocation pointer.
1140 if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1141 (address >> kPageSizeBits)) {
1142 from_start = false;
1143 address = offset;
1144 }
1145 } else if (space == NEW_SPACE) {
1146 // For new space it is always simple to encode back from current allocation.
1147 if (offset < address) {
1148 from_start = false;
1149 address = offset;
1150 }
1151 }
1152 // If we are actually dealing with real offsets (and not a numbering of
1153 // all objects) then we should shift out the bits that are always 0.
1154 if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
Leon Clarkef7060e22010-06-03 12:02:55 +01001155 if (from_start) {
1156#define COMMON_REFS_CASE(pseudo_space, actual_space, offset) \
1157 if (space == actual_space && address == offset && \
1158 how_to_code == kPlain && where_to_point == kStartOfObject) { \
1159 sink_->Put(kFromStart + how_to_code + where_to_point + \
1160 pseudo_space, "RefSer"); \
1161 } else /* NOLINT */
1162 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
1163#undef COMMON_REFS_CASE
1164 { /* NOLINT */
1165 sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer");
Leon Clarked91b9f72010-01-27 17:25:45 +00001166 sink_->PutInt(address, "address");
1167 }
1168 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001169 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1170 sink_->PutInt(address, "address");
Leon Clarked91b9f72010-01-27 17:25:45 +00001171 }
1172}
1173
1174
1175void StartupSerializer::SerializeObject(
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001176 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001177 HowToCode how_to_code,
1178 WhereToPoint where_to_point) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001179 CHECK(o->IsHeapObject());
1180 HeapObject* heap_object = HeapObject::cast(o);
Leon Clarked91b9f72010-01-27 17:25:45 +00001181
1182 if (address_mapper_.IsMapped(heap_object)) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001183 int space = SpaceOfAlreadySerializedObject(heap_object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001184 int address = address_mapper_.MappedTo(heap_object);
1185 SerializeReferenceToPreviousObject(space,
1186 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001187 how_to_code,
1188 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001189 } else {
1190 // Object has not yet been serialized. Serialize it here.
1191 ObjectSerializer object_serializer(this,
1192 heap_object,
1193 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001194 how_to_code,
1195 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001196 object_serializer.Serialize();
1197 }
1198}
1199
1200
1201void StartupSerializer::SerializeWeakReferences() {
1202 for (int i = partial_snapshot_cache_length_;
1203 i < kPartialSnapshotCacheCapacity;
1204 i++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001205 sink_->Put(kRootArray + kPlain + kStartOfObject, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001206 sink_->PutInt(Heap::kUndefinedValueRootIndex, "root_index");
1207 }
1208 Heap::IterateWeakRoots(this, VISIT_ALL);
1209}
1210
1211
1212void PartialSerializer::SerializeObject(
1213 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001214 HowToCode how_to_code,
1215 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001216 CHECK(o->IsHeapObject());
1217 HeapObject* heap_object = HeapObject::cast(o);
1218
1219 int root_index;
1220 if ((root_index = RootIndex(heap_object)) != kInvalidRootIndex) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001221 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001222 sink_->PutInt(root_index, "root_index");
1223 return;
1224 }
1225
1226 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1227 int cache_index = PartialSnapshotCacheIndex(heap_object);
Leon Clarkef7060e22010-06-03 12:02:55 +01001228 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1229 "PartialSnapshotCache");
Leon Clarked91b9f72010-01-27 17:25:45 +00001230 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1231 return;
1232 }
1233
1234 // Pointers from the partial snapshot to the objects in the startup snapshot
1235 // should go through the root array or through the partial snapshot cache.
1236 // If this is not the case you may have to add something to the root array.
1237 ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1238 // All the symbols that the partial snapshot needs should be either in the
1239 // root table or in the partial snapshot cache.
1240 ASSERT(!heap_object->IsSymbol());
1241
1242 if (address_mapper_.IsMapped(heap_object)) {
1243 int space = SpaceOfAlreadySerializedObject(heap_object);
1244 int address = address_mapper_.MappedTo(heap_object);
1245 SerializeReferenceToPreviousObject(space,
1246 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001247 how_to_code,
1248 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001249 } else {
1250 // Object has not yet been serialized. Serialize it here.
1251 ObjectSerializer serializer(this,
1252 heap_object,
1253 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001254 how_to_code,
1255 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001256 serializer.Serialize();
1257 }
1258}
1259
1260
Steve Blockd0582a62009-12-15 09:54:21 +00001261void Serializer::ObjectSerializer::Serialize() {
1262 int space = Serializer::SpaceOfObject(object_);
1263 int size = object_->Size();
1264
Leon Clarkef7060e22010-06-03 12:02:55 +01001265 sink_->Put(kNewObject + reference_representation_ + space,
1266 "ObjectSerialization");
Steve Blockd0582a62009-12-15 09:54:21 +00001267 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1268
Leon Clarkee46be812010-01-19 14:06:41 +00001269 LOG(SnapshotPositionEvent(object_->address(), sink_->Position()));
1270
Steve Blockd0582a62009-12-15 09:54:21 +00001271 // Mark this object as already serialized.
1272 bool start_new_page;
Leon Clarked91b9f72010-01-27 17:25:45 +00001273 int offset = serializer_->Allocate(space, size, &start_new_page);
1274 serializer_->address_mapper()->AddMapping(object_, offset);
Steve Blockd0582a62009-12-15 09:54:21 +00001275 if (start_new_page) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001276 sink_->Put(kNewPage, "NewPage");
Steve Blockd0582a62009-12-15 09:54:21 +00001277 sink_->PutSection(space, "NewPageSpace");
1278 }
1279
1280 // Serialize the map (first word of the object).
Leon Clarkef7060e22010-06-03 12:02:55 +01001281 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001282
1283 // Serialize the rest of the object.
1284 CHECK_EQ(0, bytes_processed_so_far_);
1285 bytes_processed_so_far_ = kPointerSize;
1286 object_->IterateBody(object_->map()->instance_type(), size, this);
1287 OutputRawData(object_->address() + size);
1288}
1289
1290
1291void Serializer::ObjectSerializer::VisitPointers(Object** start,
1292 Object** end) {
1293 Object** current = start;
1294 while (current < end) {
1295 while (current < end && (*current)->IsSmi()) current++;
1296 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1297
1298 while (current < end && !(*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001299 serializer_->SerializeObject(*current, kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001300 bytes_processed_so_far_ += kPointerSize;
1301 current++;
1302 }
1303 }
1304}
1305
1306
1307void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1308 Address* end) {
1309 Address references_start = reinterpret_cast<Address>(start);
1310 OutputRawData(references_start);
1311
1312 for (Address* current = start; current < end; current++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001313 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
Steve Blockd0582a62009-12-15 09:54:21 +00001314 int reference_id = serializer_->EncodeExternalReference(*current);
1315 sink_->PutInt(reference_id, "reference id");
1316 }
1317 bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1318}
1319
1320
1321void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1322 Address target_start = rinfo->target_address_address();
1323 OutputRawData(target_start);
1324 Address target = rinfo->target_address();
1325 uint32_t encoding = serializer_->EncodeExternalReference(target);
1326 CHECK(target == NULL ? encoding == 0 : encoding != 0);
Leon Clarkef7060e22010-06-03 12:02:55 +01001327 int representation;
1328 // Can't use a ternary operator because of gcc.
1329 if (rinfo->IsCodedSpecially()) {
1330 representation = kStartOfObject + kFromCode;
1331 } else {
1332 representation = kStartOfObject + kPlain;
1333 }
1334 sink_->Put(kExternalReference + representation, "ExternalReference");
Steve Blockd0582a62009-12-15 09:54:21 +00001335 sink_->PutInt(encoding, "reference id");
Leon Clarkef7060e22010-06-03 12:02:55 +01001336 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001337}
1338
1339
1340void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1341 CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1342 Address target_start = rinfo->target_address_address();
1343 OutputRawData(target_start);
1344 Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
Leon Clarkef7060e22010-06-03 12:02:55 +01001345 serializer_->SerializeObject(target, kFromCode, kFirstInstruction);
1346 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001347}
1348
1349
Steve Block791712a2010-08-27 10:21:07 +01001350void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1351 Code* target = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1352 OutputRawData(entry_address);
1353 serializer_->SerializeObject(target, kPlain, kFirstInstruction);
1354 bytes_processed_so_far_ += kPointerSize;
1355}
1356
1357
Steve Blockd0582a62009-12-15 09:54:21 +00001358void Serializer::ObjectSerializer::VisitExternalAsciiString(
1359 v8::String::ExternalAsciiStringResource** resource_pointer) {
1360 Address references_start = reinterpret_cast<Address>(resource_pointer);
1361 OutputRawData(references_start);
1362 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1363 Object* source = Heap::natives_source_cache()->get(i);
1364 if (!source->IsUndefined()) {
1365 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1366 typedef v8::String::ExternalAsciiStringResource Resource;
1367 Resource* resource = string->resource();
1368 if (resource == *resource_pointer) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001369 sink_->Put(kNativesStringResource, "NativesStringResource");
Steve Blockd0582a62009-12-15 09:54:21 +00001370 sink_->PutSection(i, "NativesStringResourceEnd");
1371 bytes_processed_so_far_ += sizeof(resource);
1372 return;
1373 }
1374 }
1375 }
1376 // One of the strings in the natives cache should match the resource. We
1377 // can't serialize any other kinds of external strings.
1378 UNREACHABLE();
1379}
1380
1381
1382void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1383 Address object_start = object_->address();
1384 int up_to_offset = static_cast<int>(up_to - object_start);
1385 int skipped = up_to_offset - bytes_processed_so_far_;
1386 // This assert will fail if the reloc info gives us the target_address_address
1387 // locations in a non-ascending order. Luckily that doesn't happen.
1388 ASSERT(skipped >= 0);
1389 if (skipped != 0) {
1390 Address base = object_start + bytes_processed_so_far_;
1391#define RAW_CASE(index, length) \
1392 if (skipped == length) { \
Leon Clarkef7060e22010-06-03 12:02:55 +01001393 sink_->PutSection(kRawData + index, "RawDataFixed"); \
Steve Blockd0582a62009-12-15 09:54:21 +00001394 } else /* NOLINT */
1395 COMMON_RAW_LENGTHS(RAW_CASE)
1396#undef RAW_CASE
1397 { /* NOLINT */
Leon Clarkef7060e22010-06-03 12:02:55 +01001398 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001399 sink_->PutInt(skipped, "length");
1400 }
1401 for (int i = 0; i < skipped; i++) {
1402 unsigned int data = base[i];
1403 sink_->PutSection(data, "Byte");
1404 }
1405 bytes_processed_so_far_ += skipped;
1406 }
1407}
1408
1409
1410int Serializer::SpaceOfObject(HeapObject* object) {
1411 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1412 AllocationSpace s = static_cast<AllocationSpace>(i);
1413 if (Heap::InSpace(object, s)) {
1414 if (i == LO_SPACE) {
1415 if (object->IsCode()) {
1416 return kLargeCode;
1417 } else if (object->IsFixedArray()) {
1418 return kLargeFixedArray;
1419 } else {
1420 return kLargeData;
1421 }
1422 }
1423 return i;
1424 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001425 }
1426 UNREACHABLE();
Steve Blockd0582a62009-12-15 09:54:21 +00001427 return 0;
1428}
1429
1430
1431int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1432 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1433 AllocationSpace s = static_cast<AllocationSpace>(i);
1434 if (Heap::InSpace(object, s)) {
1435 return i;
1436 }
1437 }
1438 UNREACHABLE();
1439 return 0;
1440}
1441
1442
1443int Serializer::Allocate(int space, int size, bool* new_page) {
1444 CHECK(space >= 0 && space < kNumberOfSpaces);
1445 if (SpaceIsLarge(space)) {
1446 // In large object space we merely number the objects instead of trying to
1447 // determine some sort of address.
1448 *new_page = true;
Leon Clarkee46be812010-01-19 14:06:41 +00001449 large_object_total_ += size;
Steve Blockd0582a62009-12-15 09:54:21 +00001450 return fullness_[LO_SPACE]++;
1451 }
1452 *new_page = false;
1453 if (fullness_[space] == 0) {
1454 *new_page = true;
1455 }
1456 if (SpaceIsPaged(space)) {
1457 // Paged spaces are a little special. We encode their addresses as if the
1458 // pages were all contiguous and each page were filled up in the range
1459 // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1460 // and allocation does not start at offset 0 in the page, but this scheme
1461 // means the deserializer can get the page number quickly by shifting the
1462 // serialized address.
1463 CHECK(IsPowerOf2(Page::kPageSize));
1464 int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1465 CHECK(size <= Page::kObjectAreaSize);
1466 if (used_in_this_page + size > Page::kObjectAreaSize) {
1467 *new_page = true;
1468 fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1469 }
1470 }
1471 int allocation_address = fullness_[space];
1472 fullness_[space] = allocation_address + size;
1473 return allocation_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001474}
1475
1476
1477} } // namespace v8::internal