blob: cde7577c7a542811ee61a7b3a15f9a32cd5699fe [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()
503 : encodings_(NewArray<Address*>(kTypeCodeCount)) {
504 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);
Leon Clarkee46be812010-01-19 14:06:41 +0000622}
623
624
625void Deserializer::DeserializePartial(Object** root) {
626 // Don't GC while deserializing - just expand the heap.
627 AlwaysAllocateScope always_allocate;
628 // Don't use the free lists while deserializing.
629 LinearAllocationScope allocate_linearly;
630 if (external_reference_decoder_ == NULL) {
631 external_reference_decoder_ = new ExternalReferenceDecoder();
632 }
633 VisitPointer(root);
634}
635
636
Leon Clarked91b9f72010-01-27 17:25:45 +0000637Deserializer::~Deserializer() {
638 ASSERT(source_->AtEOF());
Leon Clarkee46be812010-01-19 14:06:41 +0000639 if (external_reference_decoder_ != NULL) {
640 delete external_reference_decoder_;
641 external_reference_decoder_ = NULL;
642 }
Steve Blockd0582a62009-12-15 09:54:21 +0000643}
644
645
646// This is called on the roots. It is the driver of the deserialization
647// process. It is also called on the body of each function.
648void Deserializer::VisitPointers(Object** start, Object** end) {
649 // The space must be new space. Any other space would cause ReadChunk to try
650 // to update the remembered using NULL as the address.
651 ReadChunk(start, end, NEW_SPACE, NULL);
652}
653
654
655// This routine writes the new object into the pointer provided and then
656// returns true if the new object was in young space and false otherwise.
657// The reason for this strange interface is that otherwise the object is
658// written very late, which means the ByteArray map is not set up by the
659// time we need to use it to mark the space at the end of a page free (by
660// making it into a byte array).
661void Deserializer::ReadObject(int space_number,
662 Space* space,
663 Object** write_back) {
664 int size = source_->GetInt() << kObjectAlignmentBits;
665 Address address = Allocate(space_number, space, size);
666 *write_back = HeapObject::FromAddress(address);
667 Object** current = reinterpret_cast<Object**>(address);
668 Object** limit = current + (size >> kPointerSizeLog2);
Leon Clarkee46be812010-01-19 14:06:41 +0000669 if (FLAG_log_snapshot_positions) {
670 LOG(SnapshotPositionEvent(address, source_->position()));
671 }
Steve Blockd0582a62009-12-15 09:54:21 +0000672 ReadChunk(current, limit, space_number, address);
673}
674
675
Leon Clarkef7060e22010-06-03 12:02:55 +0100676// This macro is always used with a constant argument so it should all fold
677// away to almost nothing in the generated code. It might be nicer to do this
678// with the ternary operator but there are type issues with that.
679#define ASSIGN_DEST_SPACE(space_number) \
680 Space* dest_space; \
681 if (space_number == NEW_SPACE) { \
682 dest_space = Heap::new_space(); \
683 } else if (space_number == OLD_POINTER_SPACE) { \
684 dest_space = Heap::old_pointer_space(); \
685 } else if (space_number == OLD_DATA_SPACE) { \
686 dest_space = Heap::old_data_space(); \
687 } else if (space_number == CODE_SPACE) { \
688 dest_space = Heap::code_space(); \
689 } else if (space_number == MAP_SPACE) { \
690 dest_space = Heap::map_space(); \
691 } else if (space_number == CELL_SPACE) { \
692 dest_space = Heap::cell_space(); \
693 } else { \
694 ASSERT(space_number >= LO_SPACE); \
695 dest_space = Heap::lo_space(); \
696 }
697
698
699static const int kUnknownOffsetFromStart = -1;
Steve Blockd0582a62009-12-15 09:54:21 +0000700
701
702void Deserializer::ReadChunk(Object** current,
703 Object** limit,
Leon Clarkef7060e22010-06-03 12:02:55 +0100704 int source_space,
Steve Blockd0582a62009-12-15 09:54:21 +0000705 Address address) {
706 while (current < limit) {
707 int data = source_->Get();
708 switch (data) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100709#define CASE_STATEMENT(where, how, within, space_number) \
710 case where + how + within + space_number: \
711 ASSERT((where & ~kPointedToMask) == 0); \
712 ASSERT((how & ~kHowToCodeMask) == 0); \
713 ASSERT((within & ~kWhereToPointMask) == 0); \
714 ASSERT((space_number & ~kSpaceMask) == 0);
715
716#define CASE_BODY(where, how, within, space_number_if_any, offset_from_start) \
717 { \
718 bool emit_write_barrier = false; \
719 bool current_was_incremented = false; \
720 int space_number = space_number_if_any == kAnyOldSpace ? \
721 (data & kSpaceMask) : space_number_if_any; \
722 if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
723 ASSIGN_DEST_SPACE(space_number) \
724 ReadObject(space_number, dest_space, current); \
725 emit_write_barrier = \
726 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
727 } else { \
728 Object* new_object = NULL; /* May not be a real Object pointer. */ \
729 if (where == kNewObject) { \
730 ASSIGN_DEST_SPACE(space_number) \
731 ReadObject(space_number, dest_space, &new_object); \
732 } else if (where == kRootArray) { \
733 int root_id = source_->GetInt(); \
734 new_object = Heap::roots_address()[root_id]; \
735 } else if (where == kPartialSnapshotCache) { \
736 int cache_index = source_->GetInt(); \
737 new_object = partial_snapshot_cache_[cache_index]; \
738 } else if (where == kExternalReference) { \
739 int reference_id = source_->GetInt(); \
740 Address address = \
741 external_reference_decoder_->Decode(reference_id); \
742 new_object = reinterpret_cast<Object*>(address); \
743 } else if (where == kBackref) { \
744 emit_write_barrier = \
745 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
746 new_object = GetAddressFromEnd(data & kSpaceMask); \
747 } else { \
748 ASSERT(where == kFromStart); \
749 if (offset_from_start == kUnknownOffsetFromStart) { \
750 emit_write_barrier = \
751 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
752 new_object = GetAddressFromStart(data & kSpaceMask); \
753 } else { \
754 Address object_address = pages_[space_number][0] + \
755 (offset_from_start << kObjectAlignmentBits); \
756 new_object = HeapObject::FromAddress(object_address); \
757 } \
758 } \
759 if (within == kFirstInstruction) { \
760 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
761 new_object = reinterpret_cast<Object*>( \
762 new_code_object->instruction_start()); \
763 } \
764 if (how == kFromCode) { \
765 Address location_of_branch_data = \
766 reinterpret_cast<Address>(current); \
767 Assembler::set_target_at(location_of_branch_data, \
768 reinterpret_cast<Address>(new_object)); \
769 if (within == kFirstInstruction) { \
770 location_of_branch_data += Assembler::kCallTargetSize; \
771 current = reinterpret_cast<Object**>(location_of_branch_data); \
772 current_was_incremented = true; \
773 } \
774 } else { \
775 *current = new_object; \
776 } \
777 } \
778 if (emit_write_barrier) { \
779 Heap::RecordWrite(address, static_cast<int>( \
780 reinterpret_cast<Address>(current) - address)); \
781 } \
782 if (!current_was_incremented) { \
783 current++; /* Increment current if it wasn't done above. */ \
784 } \
785 break; \
786 } \
787
788// This generates a case and a body for each space. The large object spaces are
789// very rare in snapshots so they are grouped in one body.
790#define ONE_PER_SPACE(where, how, within) \
791 CASE_STATEMENT(where, how, within, NEW_SPACE) \
792 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
793 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
794 CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart) \
795 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
796 CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart) \
797 CASE_STATEMENT(where, how, within, CODE_SPACE) \
798 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
799 CASE_STATEMENT(where, how, within, CELL_SPACE) \
800 CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart) \
801 CASE_STATEMENT(where, how, within, MAP_SPACE) \
802 CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart) \
803 CASE_STATEMENT(where, how, within, kLargeData) \
804 CASE_STATEMENT(where, how, within, kLargeCode) \
805 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
806 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
807
808// This generates a case and a body for the new space (which has to do extra
809// write barrier handling) and handles the other spaces with 8 fall-through
810// cases and one body.
811#define ALL_SPACES(where, how, within) \
812 CASE_STATEMENT(where, how, within, NEW_SPACE) \
813 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
814 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
815 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
816 CASE_STATEMENT(where, how, within, CODE_SPACE) \
817 CASE_STATEMENT(where, how, within, CELL_SPACE) \
818 CASE_STATEMENT(where, how, within, MAP_SPACE) \
819 CASE_STATEMENT(where, how, within, kLargeData) \
820 CASE_STATEMENT(where, how, within, kLargeCode) \
821 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
822 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
823
Steve Block791712a2010-08-27 10:21:07 +0100824#define ONE_PER_CODE_SPACE(where, how, within) \
825 CASE_STATEMENT(where, how, within, CODE_SPACE) \
826 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
827 CASE_STATEMENT(where, how, within, kLargeCode) \
828 CASE_BODY(where, how, within, LO_SPACE, kUnknownOffsetFromStart)
829
Leon Clarkef7060e22010-06-03 12:02:55 +0100830#define EMIT_COMMON_REFERENCE_PATTERNS(pseudo_space_number, \
831 space_number, \
832 offset_from_start) \
833 CASE_STATEMENT(kFromStart, kPlain, kStartOfObject, pseudo_space_number) \
834 CASE_BODY(kFromStart, kPlain, kStartOfObject, space_number, offset_from_start)
835
836 // We generate 15 cases and bodies that process special tags that combine
837 // the raw data tag and the length into one byte.
Steve Blockd0582a62009-12-15 09:54:21 +0000838#define RAW_CASE(index, size) \
Leon Clarkef7060e22010-06-03 12:02:55 +0100839 case kRawData + index: { \
Steve Blockd0582a62009-12-15 09:54:21 +0000840 byte* raw_data_out = reinterpret_cast<byte*>(current); \
841 source_->CopyRaw(raw_data_out, size); \
842 current = reinterpret_cast<Object**>(raw_data_out + size); \
843 break; \
844 }
845 COMMON_RAW_LENGTHS(RAW_CASE)
846#undef RAW_CASE
Leon Clarkef7060e22010-06-03 12:02:55 +0100847
848 // Deserialize a chunk of raw data that doesn't have one of the popular
849 // lengths.
850 case kRawData: {
Steve Blockd0582a62009-12-15 09:54:21 +0000851 int size = source_->GetInt();
852 byte* raw_data_out = reinterpret_cast<byte*>(current);
853 source_->CopyRaw(raw_data_out, size);
854 current = reinterpret_cast<Object**>(raw_data_out + size);
855 break;
856 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100857
858 // Deserialize a new object and write a pointer to it to the current
859 // object.
860 ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100861 // Support for direct instruction pointers in functions
862 ONE_PER_CODE_SPACE(kNewObject, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100863 // Deserialize a new code object and write a pointer to its first
864 // instruction to the current code object.
865 ONE_PER_SPACE(kNewObject, kFromCode, kFirstInstruction)
866 // Find a recently deserialized object using its offset from the current
867 // allocation point and write a pointer to it to the current object.
868 ALL_SPACES(kBackref, kPlain, kStartOfObject)
869 // Find a recently deserialized code object using its offset from the
870 // current allocation point and write a pointer to its first instruction
Steve Block791712a2010-08-27 10:21:07 +0100871 // to the current code object or the instruction pointer in a function
872 // object.
Leon Clarkef7060e22010-06-03 12:02:55 +0100873 ALL_SPACES(kBackref, kFromCode, kFirstInstruction)
Steve Block791712a2010-08-27 10:21:07 +0100874 ALL_SPACES(kBackref, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100875 // Find an already deserialized object using its offset from the start
876 // and write a pointer to it to the current object.
877 ALL_SPACES(kFromStart, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100878 ALL_SPACES(kFromStart, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100879 // Find an already deserialized code object using its offset from the
880 // start and write a pointer to its first instruction to the current code
881 // object.
882 ALL_SPACES(kFromStart, kFromCode, kFirstInstruction)
883 // Find an already deserialized object at one of the predetermined popular
884 // offsets from the start and write a pointer to it in the current object.
885 COMMON_REFERENCE_PATTERNS(EMIT_COMMON_REFERENCE_PATTERNS)
886 // Find an object in the roots array and write a pointer to it to the
887 // current object.
888 CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
889 CASE_BODY(kRootArray, kPlain, kStartOfObject, 0, kUnknownOffsetFromStart)
890 // Find an object in the partial snapshots cache and write a pointer to it
891 // to the current object.
892 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
893 CASE_BODY(kPartialSnapshotCache,
894 kPlain,
895 kStartOfObject,
896 0,
897 kUnknownOffsetFromStart)
Steve Block791712a2010-08-27 10:21:07 +0100898 // Find an code entry in the partial snapshots cache and
899 // write a pointer to it to the current object.
900 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kFirstInstruction, 0)
901 CASE_BODY(kPartialSnapshotCache,
902 kPlain,
903 kFirstInstruction,
904 0,
905 kUnknownOffsetFromStart)
Leon Clarkef7060e22010-06-03 12:02:55 +0100906 // Find an external reference and write a pointer to it to the current
907 // object.
908 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
909 CASE_BODY(kExternalReference,
910 kPlain,
911 kStartOfObject,
912 0,
913 kUnknownOffsetFromStart)
914 // Find an external reference and write a pointer to it in the current
915 // code object.
916 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
917 CASE_BODY(kExternalReference,
918 kFromCode,
919 kStartOfObject,
920 0,
921 kUnknownOffsetFromStart)
922
923#undef CASE_STATEMENT
924#undef CASE_BODY
925#undef ONE_PER_SPACE
926#undef ALL_SPACES
927#undef EMIT_COMMON_REFERENCE_PATTERNS
928#undef ASSIGN_DEST_SPACE
929
930 case kNewPage: {
Steve Blockd0582a62009-12-15 09:54:21 +0000931 int space = source_->Get();
932 pages_[space].Add(last_object_address_);
Steve Block6ded16b2010-05-10 14:33:55 +0100933 if (space == CODE_SPACE) {
934 CPU::FlushICache(last_object_address_, Page::kPageSize);
935 }
Steve Blockd0582a62009-12-15 09:54:21 +0000936 break;
937 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100938
939 case kNativesStringResource: {
Steve Blockd0582a62009-12-15 09:54:21 +0000940 int index = source_->Get();
941 Vector<const char> source_vector = Natives::GetScriptSource(index);
942 NativesExternalStringResource* resource =
943 new NativesExternalStringResource(source_vector.start());
944 *current++ = reinterpret_cast<Object*>(resource);
945 break;
946 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100947
948 case kSynchronize: {
Leon Clarked91b9f72010-01-27 17:25:45 +0000949 // If we get here then that indicates that you have a mismatch between
950 // the number of GC roots when serializing and deserializing.
951 UNREACHABLE();
952 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100953
Steve Blockd0582a62009-12-15 09:54:21 +0000954 default:
955 UNREACHABLE();
956 }
957 }
958 ASSERT_EQ(current, limit);
959}
960
961
962void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
963 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
964 for (int shift = max_shift; shift > 0; shift -= 7) {
965 if (integer >= static_cast<uintptr_t>(1u) << shift) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000966 Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart");
Steve Blockd0582a62009-12-15 09:54:21 +0000967 }
968 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000969 PutSection(static_cast<int>(integer & 0x7f), "IntLastPart");
Steve Blockd0582a62009-12-15 09:54:21 +0000970}
971
Steve Blocka7e24c12009-10-30 11:49:00 +0000972#ifdef DEBUG
Steve Blockd0582a62009-12-15 09:54:21 +0000973
974void Deserializer::Synchronize(const char* tag) {
975 int data = source_->Get();
976 // If this assert fails then that indicates that you have a mismatch between
977 // the number of GC roots when serializing and deserializing.
Leon Clarkef7060e22010-06-03 12:02:55 +0100978 ASSERT_EQ(kSynchronize, data);
Steve Blockd0582a62009-12-15 09:54:21 +0000979 do {
980 int character = source_->Get();
981 if (character == 0) break;
982 if (FLAG_debug_serialization) {
983 PrintF("%c", character);
984 }
985 } while (true);
986 if (FLAG_debug_serialization) {
987 PrintF("\n");
988 }
989}
990
Steve Blocka7e24c12009-10-30 11:49:00 +0000991
992void Serializer::Synchronize(const char* tag) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100993 sink_->Put(kSynchronize, tag);
Steve Blockd0582a62009-12-15 09:54:21 +0000994 int character;
995 do {
996 character = *tag++;
997 sink_->PutSection(character, "TagCharacter");
998 } while (character != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000999}
Steve Blockd0582a62009-12-15 09:54:21 +00001000
Steve Blocka7e24c12009-10-30 11:49:00 +00001001#endif
1002
Steve Blockd0582a62009-12-15 09:54:21 +00001003Serializer::Serializer(SnapshotByteSink* sink)
1004 : sink_(sink),
1005 current_root_index_(0),
Andrei Popescu31002712010-02-23 13:46:05 +00001006 external_reference_encoder_(new ExternalReferenceEncoder),
Leon Clarkee46be812010-01-19 14:06:41 +00001007 large_object_total_(0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 for (int i = 0; i <= LAST_SPACE; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +00001009 fullness_[i] = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001010 }
1011}
1012
1013
Andrei Popescu31002712010-02-23 13:46:05 +00001014Serializer::~Serializer() {
1015 delete external_reference_encoder_;
1016}
1017
1018
Leon Clarked91b9f72010-01-27 17:25:45 +00001019void StartupSerializer::SerializeStrongReferences() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001020 // No active threads.
1021 CHECK_EQ(NULL, ThreadState::FirstInUse());
1022 // No active or weak handles.
1023 CHECK(HandleScopeImplementer::instance()->blocks()->is_empty());
1024 CHECK_EQ(0, GlobalHandles::NumberOfWeakHandles());
Steve Blockd0582a62009-12-15 09:54:21 +00001025 // We don't support serializing installed extensions.
1026 for (RegisteredExtension* ext = RegisteredExtension::first_extension();
1027 ext != NULL;
1028 ext = ext->next()) {
1029 CHECK_NE(v8::INSTALLED, ext->state());
1030 }
Leon Clarked91b9f72010-01-27 17:25:45 +00001031 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
Steve Blocka7e24c12009-10-30 11:49:00 +00001032}
1033
1034
Leon Clarked91b9f72010-01-27 17:25:45 +00001035void PartialSerializer::Serialize(Object** object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001036 this->VisitPointer(object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001037
1038 // After we have done the partial serialization the partial snapshot cache
1039 // will contain some references needed to decode the partial snapshot. We
1040 // fill it up with undefineds so it has a predictable length so the
1041 // deserialization code doesn't need to know the length.
1042 for (int index = partial_snapshot_cache_length_;
1043 index < kPartialSnapshotCacheCapacity;
1044 index++) {
1045 partial_snapshot_cache_[index] = Heap::undefined_value();
1046 startup_serializer_->VisitPointer(&partial_snapshot_cache_[index]);
1047 }
1048 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Leon Clarkee46be812010-01-19 14:06:41 +00001049}
1050
1051
Steve Blocka7e24c12009-10-30 11:49:00 +00001052void Serializer::VisitPointers(Object** start, Object** end) {
Steve Blockd0582a62009-12-15 09:54:21 +00001053 for (Object** current = start; current < end; current++) {
1054 if ((*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001055 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001056 sink_->PutInt(kPointerSize, "length");
1057 for (int i = 0; i < kPointerSize; i++) {
1058 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1059 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001060 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001061 SerializeObject(*current, kPlain, kStartOfObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001062 }
1063 }
1064}
1065
1066
Leon Clarked91b9f72010-01-27 17:25:45 +00001067Object* SerializerDeserializer::partial_snapshot_cache_[
1068 kPartialSnapshotCacheCapacity];
1069int SerializerDeserializer::partial_snapshot_cache_length_ = 0;
1070
1071
1072// This ensures that the partial snapshot cache keeps things alive during GC and
1073// tracks their movement. When it is called during serialization of the startup
1074// snapshot the partial snapshot is empty, so nothing happens. When the partial
1075// (context) snapshot is created, this array is populated with the pointers that
1076// the partial snapshot will need. As that happens we emit serialized objects to
1077// the startup snapshot that correspond to the elements of this cache array. On
1078// deserialization we therefore need to visit the cache array. This fills it up
1079// with pointers to deserialized objects.
Steve Block6ded16b2010-05-10 14:33:55 +01001080void SerializerDeserializer::Iterate(ObjectVisitor* visitor) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001081 visitor->VisitPointers(
1082 &partial_snapshot_cache_[0],
1083 &partial_snapshot_cache_[partial_snapshot_cache_length_]);
1084}
1085
1086
1087// When deserializing we need to set the size of the snapshot cache. This means
1088// the root iteration code (above) will iterate over array elements, writing the
1089// references to deserialized objects in them.
1090void SerializerDeserializer::SetSnapshotCacheSize(int size) {
1091 partial_snapshot_cache_length_ = size;
1092}
1093
1094
1095int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1096 for (int i = 0; i < partial_snapshot_cache_length_; i++) {
1097 Object* entry = partial_snapshot_cache_[i];
1098 if (entry == heap_object) return i;
1099 }
Andrei Popescu31002712010-02-23 13:46:05 +00001100
Leon Clarked91b9f72010-01-27 17:25:45 +00001101 // We didn't find the object in the cache. So we add it to the cache and
1102 // then visit the pointer so that it becomes part of the startup snapshot
1103 // and we can refer to it from the partial snapshot.
1104 int length = partial_snapshot_cache_length_;
1105 CHECK(length < kPartialSnapshotCacheCapacity);
1106 partial_snapshot_cache_[length] = heap_object;
1107 startup_serializer_->VisitPointer(&partial_snapshot_cache_[length]);
1108 // We don't recurse from the startup snapshot generator into the partial
1109 // snapshot generator.
1110 ASSERT(length == partial_snapshot_cache_length_);
1111 return partial_snapshot_cache_length_++;
1112}
1113
1114
1115int PartialSerializer::RootIndex(HeapObject* heap_object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001116 for (int i = 0; i < Heap::kRootListLength; i++) {
1117 Object* root = Heap::roots_address()[i];
1118 if (root == heap_object) return i;
1119 }
1120 return kInvalidRootIndex;
1121}
1122
1123
Leon Clarked91b9f72010-01-27 17:25:45 +00001124// Encode the location of an already deserialized object in order to write its
1125// location into a later object. We can encode the location as an offset from
1126// the start of the deserialized objects or as an offset backwards from the
1127// current allocation pointer.
1128void Serializer::SerializeReferenceToPreviousObject(
1129 int space,
1130 int address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001131 HowToCode how_to_code,
1132 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001133 int offset = CurrentAllocationAddress(space) - address;
1134 bool from_start = true;
1135 if (SpaceIsPaged(space)) {
1136 // For paged space it is simple to encode back from current allocation if
1137 // the object is on the same page as the current allocation pointer.
1138 if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1139 (address >> kPageSizeBits)) {
1140 from_start = false;
1141 address = offset;
1142 }
1143 } else if (space == NEW_SPACE) {
1144 // For new space it is always simple to encode back from current allocation.
1145 if (offset < address) {
1146 from_start = false;
1147 address = offset;
1148 }
1149 }
1150 // If we are actually dealing with real offsets (and not a numbering of
1151 // all objects) then we should shift out the bits that are always 0.
1152 if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
Leon Clarkef7060e22010-06-03 12:02:55 +01001153 if (from_start) {
1154#define COMMON_REFS_CASE(pseudo_space, actual_space, offset) \
1155 if (space == actual_space && address == offset && \
1156 how_to_code == kPlain && where_to_point == kStartOfObject) { \
1157 sink_->Put(kFromStart + how_to_code + where_to_point + \
1158 pseudo_space, "RefSer"); \
1159 } else /* NOLINT */
1160 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
1161#undef COMMON_REFS_CASE
1162 { /* NOLINT */
1163 sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer");
Leon Clarked91b9f72010-01-27 17:25:45 +00001164 sink_->PutInt(address, "address");
1165 }
1166 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001167 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1168 sink_->PutInt(address, "address");
Leon Clarked91b9f72010-01-27 17:25:45 +00001169 }
1170}
1171
1172
1173void StartupSerializer::SerializeObject(
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001174 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001175 HowToCode how_to_code,
1176 WhereToPoint where_to_point) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001177 CHECK(o->IsHeapObject());
1178 HeapObject* heap_object = HeapObject::cast(o);
Leon Clarked91b9f72010-01-27 17:25:45 +00001179
1180 if (address_mapper_.IsMapped(heap_object)) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001181 int space = SpaceOfAlreadySerializedObject(heap_object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001182 int address = address_mapper_.MappedTo(heap_object);
1183 SerializeReferenceToPreviousObject(space,
1184 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001185 how_to_code,
1186 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001187 } else {
1188 // Object has not yet been serialized. Serialize it here.
1189 ObjectSerializer object_serializer(this,
1190 heap_object,
1191 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001192 how_to_code,
1193 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001194 object_serializer.Serialize();
1195 }
1196}
1197
1198
1199void StartupSerializer::SerializeWeakReferences() {
1200 for (int i = partial_snapshot_cache_length_;
1201 i < kPartialSnapshotCacheCapacity;
1202 i++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001203 sink_->Put(kRootArray + kPlain + kStartOfObject, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001204 sink_->PutInt(Heap::kUndefinedValueRootIndex, "root_index");
1205 }
1206 Heap::IterateWeakRoots(this, VISIT_ALL);
1207}
1208
1209
1210void PartialSerializer::SerializeObject(
1211 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001212 HowToCode how_to_code,
1213 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001214 CHECK(o->IsHeapObject());
1215 HeapObject* heap_object = HeapObject::cast(o);
1216
1217 int root_index;
1218 if ((root_index = RootIndex(heap_object)) != kInvalidRootIndex) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001219 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001220 sink_->PutInt(root_index, "root_index");
1221 return;
1222 }
1223
1224 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1225 int cache_index = PartialSnapshotCacheIndex(heap_object);
Leon Clarkef7060e22010-06-03 12:02:55 +01001226 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1227 "PartialSnapshotCache");
Leon Clarked91b9f72010-01-27 17:25:45 +00001228 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1229 return;
1230 }
1231
1232 // Pointers from the partial snapshot to the objects in the startup snapshot
1233 // should go through the root array or through the partial snapshot cache.
1234 // If this is not the case you may have to add something to the root array.
1235 ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1236 // All the symbols that the partial snapshot needs should be either in the
1237 // root table or in the partial snapshot cache.
1238 ASSERT(!heap_object->IsSymbol());
1239
1240 if (address_mapper_.IsMapped(heap_object)) {
1241 int space = SpaceOfAlreadySerializedObject(heap_object);
1242 int address = address_mapper_.MappedTo(heap_object);
1243 SerializeReferenceToPreviousObject(space,
1244 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001245 how_to_code,
1246 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001247 } else {
1248 // Object has not yet been serialized. Serialize it here.
1249 ObjectSerializer serializer(this,
1250 heap_object,
1251 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001252 how_to_code,
1253 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001254 serializer.Serialize();
1255 }
1256}
1257
1258
Steve Blockd0582a62009-12-15 09:54:21 +00001259void Serializer::ObjectSerializer::Serialize() {
1260 int space = Serializer::SpaceOfObject(object_);
1261 int size = object_->Size();
1262
Leon Clarkef7060e22010-06-03 12:02:55 +01001263 sink_->Put(kNewObject + reference_representation_ + space,
1264 "ObjectSerialization");
Steve Blockd0582a62009-12-15 09:54:21 +00001265 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1266
Leon Clarkee46be812010-01-19 14:06:41 +00001267 LOG(SnapshotPositionEvent(object_->address(), sink_->Position()));
1268
Steve Blockd0582a62009-12-15 09:54:21 +00001269 // Mark this object as already serialized.
1270 bool start_new_page;
Leon Clarked91b9f72010-01-27 17:25:45 +00001271 int offset = serializer_->Allocate(space, size, &start_new_page);
1272 serializer_->address_mapper()->AddMapping(object_, offset);
Steve Blockd0582a62009-12-15 09:54:21 +00001273 if (start_new_page) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001274 sink_->Put(kNewPage, "NewPage");
Steve Blockd0582a62009-12-15 09:54:21 +00001275 sink_->PutSection(space, "NewPageSpace");
1276 }
1277
1278 // Serialize the map (first word of the object).
Leon Clarkef7060e22010-06-03 12:02:55 +01001279 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001280
1281 // Serialize the rest of the object.
1282 CHECK_EQ(0, bytes_processed_so_far_);
1283 bytes_processed_so_far_ = kPointerSize;
1284 object_->IterateBody(object_->map()->instance_type(), size, this);
1285 OutputRawData(object_->address() + size);
1286}
1287
1288
1289void Serializer::ObjectSerializer::VisitPointers(Object** start,
1290 Object** end) {
1291 Object** current = start;
1292 while (current < end) {
1293 while (current < end && (*current)->IsSmi()) current++;
1294 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1295
1296 while (current < end && !(*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001297 serializer_->SerializeObject(*current, kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001298 bytes_processed_so_far_ += kPointerSize;
1299 current++;
1300 }
1301 }
1302}
1303
1304
1305void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1306 Address* end) {
1307 Address references_start = reinterpret_cast<Address>(start);
1308 OutputRawData(references_start);
1309
1310 for (Address* current = start; current < end; current++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001311 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
Steve Blockd0582a62009-12-15 09:54:21 +00001312 int reference_id = serializer_->EncodeExternalReference(*current);
1313 sink_->PutInt(reference_id, "reference id");
1314 }
1315 bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1316}
1317
1318
1319void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1320 Address target_start = rinfo->target_address_address();
1321 OutputRawData(target_start);
1322 Address target = rinfo->target_address();
1323 uint32_t encoding = serializer_->EncodeExternalReference(target);
1324 CHECK(target == NULL ? encoding == 0 : encoding != 0);
Leon Clarkef7060e22010-06-03 12:02:55 +01001325 int representation;
1326 // Can't use a ternary operator because of gcc.
1327 if (rinfo->IsCodedSpecially()) {
1328 representation = kStartOfObject + kFromCode;
1329 } else {
1330 representation = kStartOfObject + kPlain;
1331 }
1332 sink_->Put(kExternalReference + representation, "ExternalReference");
Steve Blockd0582a62009-12-15 09:54:21 +00001333 sink_->PutInt(encoding, "reference id");
Leon Clarkef7060e22010-06-03 12:02:55 +01001334 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001335}
1336
1337
1338void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1339 CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1340 Address target_start = rinfo->target_address_address();
1341 OutputRawData(target_start);
1342 Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
Leon Clarkef7060e22010-06-03 12:02:55 +01001343 serializer_->SerializeObject(target, kFromCode, kFirstInstruction);
1344 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001345}
1346
1347
Steve Block791712a2010-08-27 10:21:07 +01001348void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1349 Code* target = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1350 OutputRawData(entry_address);
1351 serializer_->SerializeObject(target, kPlain, kFirstInstruction);
1352 bytes_processed_so_far_ += kPointerSize;
1353}
1354
1355
Steve Blockd0582a62009-12-15 09:54:21 +00001356void Serializer::ObjectSerializer::VisitExternalAsciiString(
1357 v8::String::ExternalAsciiStringResource** resource_pointer) {
1358 Address references_start = reinterpret_cast<Address>(resource_pointer);
1359 OutputRawData(references_start);
1360 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1361 Object* source = Heap::natives_source_cache()->get(i);
1362 if (!source->IsUndefined()) {
1363 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1364 typedef v8::String::ExternalAsciiStringResource Resource;
1365 Resource* resource = string->resource();
1366 if (resource == *resource_pointer) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001367 sink_->Put(kNativesStringResource, "NativesStringResource");
Steve Blockd0582a62009-12-15 09:54:21 +00001368 sink_->PutSection(i, "NativesStringResourceEnd");
1369 bytes_processed_so_far_ += sizeof(resource);
1370 return;
1371 }
1372 }
1373 }
1374 // One of the strings in the natives cache should match the resource. We
1375 // can't serialize any other kinds of external strings.
1376 UNREACHABLE();
1377}
1378
1379
1380void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1381 Address object_start = object_->address();
1382 int up_to_offset = static_cast<int>(up_to - object_start);
1383 int skipped = up_to_offset - bytes_processed_so_far_;
1384 // This assert will fail if the reloc info gives us the target_address_address
1385 // locations in a non-ascending order. Luckily that doesn't happen.
1386 ASSERT(skipped >= 0);
1387 if (skipped != 0) {
1388 Address base = object_start + bytes_processed_so_far_;
1389#define RAW_CASE(index, length) \
1390 if (skipped == length) { \
Leon Clarkef7060e22010-06-03 12:02:55 +01001391 sink_->PutSection(kRawData + index, "RawDataFixed"); \
Steve Blockd0582a62009-12-15 09:54:21 +00001392 } else /* NOLINT */
1393 COMMON_RAW_LENGTHS(RAW_CASE)
1394#undef RAW_CASE
1395 { /* NOLINT */
Leon Clarkef7060e22010-06-03 12:02:55 +01001396 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001397 sink_->PutInt(skipped, "length");
1398 }
1399 for (int i = 0; i < skipped; i++) {
1400 unsigned int data = base[i];
1401 sink_->PutSection(data, "Byte");
1402 }
1403 bytes_processed_so_far_ += skipped;
1404 }
1405}
1406
1407
1408int Serializer::SpaceOfObject(HeapObject* object) {
1409 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1410 AllocationSpace s = static_cast<AllocationSpace>(i);
1411 if (Heap::InSpace(object, s)) {
1412 if (i == LO_SPACE) {
1413 if (object->IsCode()) {
1414 return kLargeCode;
1415 } else if (object->IsFixedArray()) {
1416 return kLargeFixedArray;
1417 } else {
1418 return kLargeData;
1419 }
1420 }
1421 return i;
1422 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001423 }
1424 UNREACHABLE();
Steve Blockd0582a62009-12-15 09:54:21 +00001425 return 0;
1426}
1427
1428
1429int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1430 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1431 AllocationSpace s = static_cast<AllocationSpace>(i);
1432 if (Heap::InSpace(object, s)) {
1433 return i;
1434 }
1435 }
1436 UNREACHABLE();
1437 return 0;
1438}
1439
1440
1441int Serializer::Allocate(int space, int size, bool* new_page) {
1442 CHECK(space >= 0 && space < kNumberOfSpaces);
1443 if (SpaceIsLarge(space)) {
1444 // In large object space we merely number the objects instead of trying to
1445 // determine some sort of address.
1446 *new_page = true;
Leon Clarkee46be812010-01-19 14:06:41 +00001447 large_object_total_ += size;
Steve Blockd0582a62009-12-15 09:54:21 +00001448 return fullness_[LO_SPACE]++;
1449 }
1450 *new_page = false;
1451 if (fullness_[space] == 0) {
1452 *new_page = true;
1453 }
1454 if (SpaceIsPaged(space)) {
1455 // Paged spaces are a little special. We encode their addresses as if the
1456 // pages were all contiguous and each page were filled up in the range
1457 // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1458 // and allocation does not start at offset 0 in the page, but this scheme
1459 // means the deserializer can get the page number quickly by shifting the
1460 // serialized address.
1461 CHECK(IsPowerOf2(Page::kPageSize));
1462 int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1463 CHECK(size <= Page::kObjectAreaSize);
1464 if (used_in_this_page + size > Page::kObjectAreaSize) {
1465 *new_page = true;
1466 fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1467 }
1468 }
1469 int allocation_address = fullness_[space];
1470 fullness_[space] = allocation_address + size;
1471 return allocation_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001472}
1473
1474
1475} } // namespace v8::internal