blob: 0e283f4618bba028ce22b33ae8949b352d63c411 [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()");
240 const char* debug_register_format = "Debug::register_address(%i)";
Steve Blockd0582a62009-12-15 09:54:21 +0000241 int dr_format_length = StrLength(debug_register_format);
Steve Blocka7e24c12009-10-30 11:49:00 +0000242 for (int i = 0; i < kNumJSCallerSaved; ++i) {
243 Vector<char> name = Vector<char>::New(dr_format_length + 1);
244 OS::SNPrintF(name, debug_register_format, i);
245 Add(Debug_Address(Debug::k_register_address, i).address(),
246 DEBUG_ADDRESS,
247 Debug::k_register_address << kDebugIdShift | i,
248 name.start());
249 }
250#endif
251
252 // Stat counters
253 struct StatsRefTableEntry {
254 StatsCounter* counter;
255 uint16_t id;
256 const char* name;
257 };
258
259 static const StatsRefTableEntry stats_ref_table[] = {
260#define COUNTER_ENTRY(name, caption) \
261 { &Counters::name, \
262 Counters::k_##name, \
263 "Counters::" #name },
264
265 STATS_COUNTER_LIST_1(COUNTER_ENTRY)
266 STATS_COUNTER_LIST_2(COUNTER_ENTRY)
267#undef COUNTER_ENTRY
268 }; // end of stats_ref_table[].
269
270 for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
271 Add(reinterpret_cast<Address>(
272 GetInternalPointer(stats_ref_table[i].counter)),
273 STATS_COUNTER,
274 stats_ref_table[i].id,
275 stats_ref_table[i].name);
276 }
277
278 // Top addresses
Steve Block3ce2e202009-11-05 08:53:23 +0000279 const char* top_address_format = "Top::%s";
280
281 const char* AddressNames[] = {
282#define C(name) #name,
283 TOP_ADDRESS_LIST(C)
284 TOP_ADDRESS_LIST_PROF(C)
285 NULL
286#undef C
287 };
288
Steve Blockd0582a62009-12-15 09:54:21 +0000289 int top_format_length = StrLength(top_address_format) - 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000290 for (uint16_t i = 0; i < Top::k_top_address_count; ++i) {
Steve Block3ce2e202009-11-05 08:53:23 +0000291 const char* address_name = AddressNames[i];
292 Vector<char> name =
Steve Blockd0582a62009-12-15 09:54:21 +0000293 Vector<char>::New(top_format_length + StrLength(address_name) + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000294 const char* chars = name.start();
Steve Block3ce2e202009-11-05 08:53:23 +0000295 OS::SNPrintF(name, top_address_format, address_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 Add(Top::get_address_from_id((Top::AddressId)i), TOP_ADDRESS, i, chars);
297 }
298
299 // Extensions
300 Add(FUNCTION_ADDR(GCExtension::GC), EXTENSION, 1,
301 "GCExtension::GC");
302
303 // Accessors
304#define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
305 Add((Address)&Accessors::name, \
306 ACCESSOR, \
307 Accessors::k##name, \
308 "Accessors::" #name);
309
310 ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
311#undef ACCESSOR_DESCRIPTOR_DECLARATION
312
313 // Stub cache tables
314 Add(SCTableReference::keyReference(StubCache::kPrimary).address(),
315 STUB_CACHE_TABLE,
316 1,
317 "StubCache::primary_->key");
318 Add(SCTableReference::valueReference(StubCache::kPrimary).address(),
319 STUB_CACHE_TABLE,
320 2,
321 "StubCache::primary_->value");
322 Add(SCTableReference::keyReference(StubCache::kSecondary).address(),
323 STUB_CACHE_TABLE,
324 3,
325 "StubCache::secondary_->key");
326 Add(SCTableReference::valueReference(StubCache::kSecondary).address(),
327 STUB_CACHE_TABLE,
328 4,
329 "StubCache::secondary_->value");
330
331 // Runtime entries
332 Add(ExternalReference::perform_gc_function().address(),
333 RUNTIME_ENTRY,
334 1,
335 "Runtime::PerformGC");
Steve Block6ded16b2010-05-10 14:33:55 +0100336 Add(ExternalReference::fill_heap_number_with_random_function().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 RUNTIME_ENTRY,
338 2,
Steve Block6ded16b2010-05-10 14:33:55 +0100339 "V8::FillHeapNumberWithRandom");
340
341 Add(ExternalReference::random_uint32_function().address(),
342 RUNTIME_ENTRY,
343 3,
344 "V8::Random");
Steve Blocka7e24c12009-10-30 11:49:00 +0000345
346 // Miscellaneous
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 Add(ExternalReference::the_hole_value_location().address(),
348 UNCLASSIFIED,
349 2,
350 "Factory::the_hole_value().location()");
351 Add(ExternalReference::roots_address().address(),
352 UNCLASSIFIED,
353 3,
354 "Heap::roots_address()");
Steve Blockd0582a62009-12-15 09:54:21 +0000355 Add(ExternalReference::address_of_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000356 UNCLASSIFIED,
357 4,
358 "StackGuard::address_of_jslimit()");
Steve Blockd0582a62009-12-15 09:54:21 +0000359 Add(ExternalReference::address_of_real_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000360 UNCLASSIFIED,
361 5,
Steve Blockd0582a62009-12-15 09:54:21 +0000362 "StackGuard::address_of_real_jslimit()");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100363#ifndef V8_INTERPRETED_REGEXP
Steve Blockd0582a62009-12-15 09:54:21 +0000364 Add(ExternalReference::address_of_regexp_stack_limit().address(),
365 UNCLASSIFIED,
366 6,
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 "RegExpStack::limit_address()");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100368 Add(ExternalReference::address_of_regexp_stack_memory_address().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000369 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000370 7,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100371 "RegExpStack::memory_address()");
372 Add(ExternalReference::address_of_regexp_stack_memory_size().address(),
373 UNCLASSIFIED,
374 8,
375 "RegExpStack::memory_size()");
376 Add(ExternalReference::address_of_static_offsets_vector().address(),
377 UNCLASSIFIED,
378 9,
379 "OffsetsVector::static_offsets_vector");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100380#endif // V8_INTERPRETED_REGEXP
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100381 Add(ExternalReference::new_space_start().address(),
382 UNCLASSIFIED,
383 10,
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 "Heap::NewSpaceStart()");
Andrei Popescu402d9372010-02-26 13:31:12 +0000385 Add(ExternalReference::new_space_mask().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000386 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100387 11,
Andrei Popescu402d9372010-02-26 13:31:12 +0000388 "Heap::NewSpaceMask()");
389 Add(ExternalReference::heap_always_allocate_scope_depth().address(),
390 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100391 12,
Steve Blocka7e24c12009-10-30 11:49:00 +0000392 "Heap::always_allocate_scope_depth()");
393 Add(ExternalReference::new_space_allocation_limit_address().address(),
394 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100395 13,
Steve Blocka7e24c12009-10-30 11:49:00 +0000396 "Heap::NewSpaceAllocationLimitAddress()");
397 Add(ExternalReference::new_space_allocation_top_address().address(),
398 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100399 14,
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 "Heap::NewSpaceAllocationTopAddress()");
401#ifdef ENABLE_DEBUGGER_SUPPORT
402 Add(ExternalReference::debug_break().address(),
403 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100404 15,
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 "Debug::Break()");
406 Add(ExternalReference::debug_step_in_fp_address().address(),
407 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100408 16,
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 "Debug::step_in_fp_addr()");
410#endif
411 Add(ExternalReference::double_fp_operation(Token::ADD).address(),
412 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100413 17,
Steve Blocka7e24c12009-10-30 11:49:00 +0000414 "add_two_doubles");
415 Add(ExternalReference::double_fp_operation(Token::SUB).address(),
416 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100417 18,
Steve Blocka7e24c12009-10-30 11:49:00 +0000418 "sub_two_doubles");
419 Add(ExternalReference::double_fp_operation(Token::MUL).address(),
420 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100421 19,
Steve Blocka7e24c12009-10-30 11:49:00 +0000422 "mul_two_doubles");
423 Add(ExternalReference::double_fp_operation(Token::DIV).address(),
424 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100425 20,
Steve Blocka7e24c12009-10-30 11:49:00 +0000426 "div_two_doubles");
427 Add(ExternalReference::double_fp_operation(Token::MOD).address(),
428 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100429 21,
Steve Blocka7e24c12009-10-30 11:49:00 +0000430 "mod_two_doubles");
431 Add(ExternalReference::compare_doubles().address(),
432 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100433 22,
Steve Blocka7e24c12009-10-30 11:49:00 +0000434 "compare_doubles");
Steve Block6ded16b2010-05-10 14:33:55 +0100435#ifndef V8_INTERPRETED_REGEXP
436 Add(ExternalReference::re_case_insensitive_compare_uc16().address(),
437 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100438 23,
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
440 Add(ExternalReference::re_check_stack_guard_state().address(),
441 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100442 24,
Steve Blocka7e24c12009-10-30 11:49:00 +0000443 "RegExpMacroAssembler*::CheckStackGuardState()");
444 Add(ExternalReference::re_grow_stack().address(),
445 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100446 25,
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 "NativeRegExpMacroAssembler::GrowStack()");
Leon Clarkee46be812010-01-19 14:06:41 +0000448 Add(ExternalReference::re_word_character_map().address(),
449 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100450 26,
Leon Clarkee46be812010-01-19 14:06:41 +0000451 "NativeRegExpMacroAssembler::word_character_map");
Steve Block6ded16b2010-05-10 14:33:55 +0100452#endif // V8_INTERPRETED_REGEXP
Leon Clarkee46be812010-01-19 14:06:41 +0000453 // Keyed lookup cache.
454 Add(ExternalReference::keyed_lookup_cache_keys().address(),
455 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100456 27,
Leon Clarkee46be812010-01-19 14:06:41 +0000457 "KeyedLookupCache::keys()");
458 Add(ExternalReference::keyed_lookup_cache_field_offsets().address(),
459 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100460 28,
Leon Clarkee46be812010-01-19 14:06:41 +0000461 "KeyedLookupCache::field_offsets()");
Andrei Popescu402d9372010-02-26 13:31:12 +0000462 Add(ExternalReference::transcendental_cache_array_address().address(),
463 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100464 29,
Andrei Popescu402d9372010-02-26 13:31:12 +0000465 "TranscendentalCache::caches()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000466}
467
468
469ExternalReferenceEncoder::ExternalReferenceEncoder()
470 : encodings_(Match) {
471 ExternalReferenceTable* external_references =
472 ExternalReferenceTable::instance();
473 for (int i = 0; i < external_references->size(); ++i) {
474 Put(external_references->address(i), i);
475 }
476}
477
478
479uint32_t ExternalReferenceEncoder::Encode(Address key) const {
480 int index = IndexOf(key);
481 return index >=0 ? ExternalReferenceTable::instance()->code(index) : 0;
482}
483
484
485const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
486 int index = IndexOf(key);
487 return index >=0 ? ExternalReferenceTable::instance()->name(index) : NULL;
488}
489
490
491int ExternalReferenceEncoder::IndexOf(Address key) const {
492 if (key == NULL) return -1;
493 HashMap::Entry* entry =
494 const_cast<HashMap &>(encodings_).Lookup(key, Hash(key), false);
495 return entry == NULL
496 ? -1
497 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
498}
499
500
501void ExternalReferenceEncoder::Put(Address key, int index) {
502 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
Steve Block6ded16b2010-05-10 14:33:55 +0100503 entry->value = reinterpret_cast<void*>(index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000504}
505
506
507ExternalReferenceDecoder::ExternalReferenceDecoder()
508 : encodings_(NewArray<Address*>(kTypeCodeCount)) {
509 ExternalReferenceTable* external_references =
510 ExternalReferenceTable::instance();
511 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
512 int max = external_references->max_id(type) + 1;
513 encodings_[type] = NewArray<Address>(max + 1);
514 }
515 for (int i = 0; i < external_references->size(); ++i) {
516 Put(external_references->code(i), external_references->address(i));
517 }
518}
519
520
521ExternalReferenceDecoder::~ExternalReferenceDecoder() {
522 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
523 DeleteArray(encodings_[type]);
524 }
525 DeleteArray(encodings_);
526}
527
528
Steve Blocka7e24c12009-10-30 11:49:00 +0000529bool Serializer::serialization_enabled_ = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000530bool Serializer::too_late_to_enable_now_ = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000531ExternalReferenceDecoder* Deserializer::external_reference_decoder_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000532
533
Leon Clarkee46be812010-01-19 14:06:41 +0000534Deserializer::Deserializer(SnapshotByteSource* source) : source_(source) {
Steve Blockd0582a62009-12-15 09:54:21 +0000535}
536
537
538// This routine both allocates a new object, and also keeps
539// track of where objects have been allocated so that we can
540// fix back references when deserializing.
541Address Deserializer::Allocate(int space_index, Space* space, int size) {
542 Address address;
543 if (!SpaceIsLarge(space_index)) {
544 ASSERT(!SpaceIsPaged(space_index) ||
545 size <= Page::kPageSize - Page::kObjectStartOffset);
546 Object* new_allocation;
547 if (space_index == NEW_SPACE) {
548 new_allocation = reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
549 } else {
550 new_allocation = reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
551 }
552 HeapObject* new_object = HeapObject::cast(new_allocation);
553 ASSERT(!new_object->IsFailure());
554 address = new_object->address();
555 high_water_[space_index] = address + size;
556 } else {
557 ASSERT(SpaceIsLarge(space_index));
558 ASSERT(size > Page::kPageSize - Page::kObjectStartOffset);
559 LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
560 Object* new_allocation;
561 if (space_index == kLargeData) {
562 new_allocation = lo_space->AllocateRaw(size);
563 } else if (space_index == kLargeFixedArray) {
564 new_allocation = lo_space->AllocateRawFixedArray(size);
565 } else {
566 ASSERT_EQ(kLargeCode, space_index);
567 new_allocation = lo_space->AllocateRawCode(size);
568 }
569 ASSERT(!new_allocation->IsFailure());
570 HeapObject* new_object = HeapObject::cast(new_allocation);
571 // Record all large objects in the same space.
572 address = new_object->address();
Andrei Popescu31002712010-02-23 13:46:05 +0000573 pages_[LO_SPACE].Add(address);
Steve Blockd0582a62009-12-15 09:54:21 +0000574 }
575 last_object_address_ = address;
576 return address;
577}
578
579
580// This returns the address of an object that has been described in the
581// snapshot as being offset bytes back in a particular space.
582HeapObject* Deserializer::GetAddressFromEnd(int space) {
583 int offset = source_->GetInt();
584 ASSERT(!SpaceIsLarge(space));
585 offset <<= kObjectAlignmentBits;
586 return HeapObject::FromAddress(high_water_[space] - offset);
587}
588
589
590// This returns the address of an object that has been described in the
591// snapshot as being offset bytes into a particular space.
592HeapObject* Deserializer::GetAddressFromStart(int space) {
593 int offset = source_->GetInt();
594 if (SpaceIsLarge(space)) {
595 // Large spaces have one object per 'page'.
596 return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
597 }
598 offset <<= kObjectAlignmentBits;
599 if (space == NEW_SPACE) {
600 // New space has only one space - numbered 0.
601 return HeapObject::FromAddress(pages_[space][0] + offset);
602 }
603 ASSERT(SpaceIsPaged(space));
Leon Clarkee46be812010-01-19 14:06:41 +0000604 int page_of_pointee = offset >> kPageSizeBits;
Steve Blockd0582a62009-12-15 09:54:21 +0000605 Address object_address = pages_[space][page_of_pointee] +
606 (offset & Page::kPageAlignmentMask);
607 return HeapObject::FromAddress(object_address);
608}
609
610
611void Deserializer::Deserialize() {
612 // Don't GC while deserializing - just expand the heap.
613 AlwaysAllocateScope always_allocate;
614 // Don't use the free lists while deserializing.
615 LinearAllocationScope allocate_linearly;
616 // No active threads.
617 ASSERT_EQ(NULL, ThreadState::FirstInUse());
618 // No active handles.
619 ASSERT(HandleScopeImplementer::instance()->blocks()->is_empty());
Leon Clarked91b9f72010-01-27 17:25:45 +0000620 // Make sure the entire partial snapshot cache is traversed, filling it with
621 // valid object pointers.
622 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000623 ASSERT_EQ(NULL, external_reference_decoder_);
624 external_reference_decoder_ = new ExternalReferenceDecoder();
Leon Clarked91b9f72010-01-27 17:25:45 +0000625 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
626 Heap::IterateWeakRoots(this, VISIT_ALL);
Leon Clarkee46be812010-01-19 14:06:41 +0000627}
628
629
630void Deserializer::DeserializePartial(Object** root) {
631 // Don't GC while deserializing - just expand the heap.
632 AlwaysAllocateScope always_allocate;
633 // Don't use the free lists while deserializing.
634 LinearAllocationScope allocate_linearly;
635 if (external_reference_decoder_ == NULL) {
636 external_reference_decoder_ = new ExternalReferenceDecoder();
637 }
638 VisitPointer(root);
639}
640
641
Leon Clarked91b9f72010-01-27 17:25:45 +0000642Deserializer::~Deserializer() {
643 ASSERT(source_->AtEOF());
Leon Clarkee46be812010-01-19 14:06:41 +0000644 if (external_reference_decoder_ != NULL) {
645 delete external_reference_decoder_;
646 external_reference_decoder_ = NULL;
647 }
Steve Blockd0582a62009-12-15 09:54:21 +0000648}
649
650
651// This is called on the roots. It is the driver of the deserialization
652// process. It is also called on the body of each function.
653void Deserializer::VisitPointers(Object** start, Object** end) {
654 // The space must be new space. Any other space would cause ReadChunk to try
655 // to update the remembered using NULL as the address.
656 ReadChunk(start, end, NEW_SPACE, NULL);
657}
658
659
660// This routine writes the new object into the pointer provided and then
661// returns true if the new object was in young space and false otherwise.
662// The reason for this strange interface is that otherwise the object is
663// written very late, which means the ByteArray map is not set up by the
664// time we need to use it to mark the space at the end of a page free (by
665// making it into a byte array).
666void Deserializer::ReadObject(int space_number,
667 Space* space,
668 Object** write_back) {
669 int size = source_->GetInt() << kObjectAlignmentBits;
670 Address address = Allocate(space_number, space, size);
671 *write_back = HeapObject::FromAddress(address);
672 Object** current = reinterpret_cast<Object**>(address);
673 Object** limit = current + (size >> kPointerSizeLog2);
Leon Clarkee46be812010-01-19 14:06:41 +0000674 if (FLAG_log_snapshot_positions) {
675 LOG(SnapshotPositionEvent(address, source_->position()));
676 }
Steve Blockd0582a62009-12-15 09:54:21 +0000677 ReadChunk(current, limit, space_number, address);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100678
679 if (space == Heap::map_space()) {
680 ASSERT(size == Map::kSize);
681 HeapObject* obj = HeapObject::FromAddress(address);
682 Map* map = reinterpret_cast<Map*>(obj);
683 map->set_scavenger(Heap::GetScavenger(map->instance_type(),
684 map->instance_size()));
685 }
Steve Blockd0582a62009-12-15 09:54:21 +0000686}
687
688
Leon Clarkef7060e22010-06-03 12:02:55 +0100689// This macro is always used with a constant argument so it should all fold
690// away to almost nothing in the generated code. It might be nicer to do this
691// with the ternary operator but there are type issues with that.
692#define ASSIGN_DEST_SPACE(space_number) \
693 Space* dest_space; \
694 if (space_number == NEW_SPACE) { \
695 dest_space = Heap::new_space(); \
696 } else if (space_number == OLD_POINTER_SPACE) { \
697 dest_space = Heap::old_pointer_space(); \
698 } else if (space_number == OLD_DATA_SPACE) { \
699 dest_space = Heap::old_data_space(); \
700 } else if (space_number == CODE_SPACE) { \
701 dest_space = Heap::code_space(); \
702 } else if (space_number == MAP_SPACE) { \
703 dest_space = Heap::map_space(); \
704 } else if (space_number == CELL_SPACE) { \
705 dest_space = Heap::cell_space(); \
706 } else { \
707 ASSERT(space_number >= LO_SPACE); \
708 dest_space = Heap::lo_space(); \
709 }
710
711
712static const int kUnknownOffsetFromStart = -1;
Steve Blockd0582a62009-12-15 09:54:21 +0000713
714
715void Deserializer::ReadChunk(Object** current,
716 Object** limit,
Leon Clarkef7060e22010-06-03 12:02:55 +0100717 int source_space,
Steve Blockd0582a62009-12-15 09:54:21 +0000718 Address address) {
719 while (current < limit) {
720 int data = source_->Get();
721 switch (data) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100722#define CASE_STATEMENT(where, how, within, space_number) \
723 case where + how + within + space_number: \
724 ASSERT((where & ~kPointedToMask) == 0); \
725 ASSERT((how & ~kHowToCodeMask) == 0); \
726 ASSERT((within & ~kWhereToPointMask) == 0); \
727 ASSERT((space_number & ~kSpaceMask) == 0);
728
729#define CASE_BODY(where, how, within, space_number_if_any, offset_from_start) \
730 { \
731 bool emit_write_barrier = false; \
732 bool current_was_incremented = false; \
733 int space_number = space_number_if_any == kAnyOldSpace ? \
734 (data & kSpaceMask) : space_number_if_any; \
735 if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
736 ASSIGN_DEST_SPACE(space_number) \
737 ReadObject(space_number, dest_space, current); \
738 emit_write_barrier = \
739 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
740 } else { \
741 Object* new_object = NULL; /* May not be a real Object pointer. */ \
742 if (where == kNewObject) { \
743 ASSIGN_DEST_SPACE(space_number) \
744 ReadObject(space_number, dest_space, &new_object); \
745 } else if (where == kRootArray) { \
746 int root_id = source_->GetInt(); \
747 new_object = Heap::roots_address()[root_id]; \
748 } else if (where == kPartialSnapshotCache) { \
749 int cache_index = source_->GetInt(); \
750 new_object = partial_snapshot_cache_[cache_index]; \
751 } else if (where == kExternalReference) { \
752 int reference_id = source_->GetInt(); \
753 Address address = \
754 external_reference_decoder_->Decode(reference_id); \
755 new_object = reinterpret_cast<Object*>(address); \
756 } else if (where == kBackref) { \
757 emit_write_barrier = \
758 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
759 new_object = GetAddressFromEnd(data & kSpaceMask); \
760 } else { \
761 ASSERT(where == kFromStart); \
762 if (offset_from_start == kUnknownOffsetFromStart) { \
763 emit_write_barrier = \
764 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
765 new_object = GetAddressFromStart(data & kSpaceMask); \
766 } else { \
767 Address object_address = pages_[space_number][0] + \
768 (offset_from_start << kObjectAlignmentBits); \
769 new_object = HeapObject::FromAddress(object_address); \
770 } \
771 } \
772 if (within == kFirstInstruction) { \
773 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
774 new_object = reinterpret_cast<Object*>( \
775 new_code_object->instruction_start()); \
776 } \
777 if (how == kFromCode) { \
778 Address location_of_branch_data = \
779 reinterpret_cast<Address>(current); \
780 Assembler::set_target_at(location_of_branch_data, \
781 reinterpret_cast<Address>(new_object)); \
782 if (within == kFirstInstruction) { \
783 location_of_branch_data += Assembler::kCallTargetSize; \
784 current = reinterpret_cast<Object**>(location_of_branch_data); \
785 current_was_incremented = true; \
786 } \
787 } else { \
788 *current = new_object; \
789 } \
790 } \
791 if (emit_write_barrier) { \
792 Heap::RecordWrite(address, static_cast<int>( \
793 reinterpret_cast<Address>(current) - address)); \
794 } \
795 if (!current_was_incremented) { \
796 current++; /* Increment current if it wasn't done above. */ \
797 } \
798 break; \
799 } \
800
801// This generates a case and a body for each space. The large object spaces are
802// very rare in snapshots so they are grouped in one body.
803#define ONE_PER_SPACE(where, how, within) \
804 CASE_STATEMENT(where, how, within, NEW_SPACE) \
805 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
806 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
807 CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart) \
808 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
809 CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart) \
810 CASE_STATEMENT(where, how, within, CODE_SPACE) \
811 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
812 CASE_STATEMENT(where, how, within, CELL_SPACE) \
813 CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart) \
814 CASE_STATEMENT(where, how, within, MAP_SPACE) \
815 CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart) \
816 CASE_STATEMENT(where, how, within, kLargeData) \
817 CASE_STATEMENT(where, how, within, kLargeCode) \
818 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
819 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
820
821// This generates a case and a body for the new space (which has to do extra
822// write barrier handling) and handles the other spaces with 8 fall-through
823// cases and one body.
824#define ALL_SPACES(where, how, within) \
825 CASE_STATEMENT(where, how, within, NEW_SPACE) \
826 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
827 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
828 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
829 CASE_STATEMENT(where, how, within, CODE_SPACE) \
830 CASE_STATEMENT(where, how, within, CELL_SPACE) \
831 CASE_STATEMENT(where, how, within, MAP_SPACE) \
832 CASE_STATEMENT(where, how, within, kLargeData) \
833 CASE_STATEMENT(where, how, within, kLargeCode) \
834 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
835 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
836
837#define EMIT_COMMON_REFERENCE_PATTERNS(pseudo_space_number, \
838 space_number, \
839 offset_from_start) \
840 CASE_STATEMENT(kFromStart, kPlain, kStartOfObject, pseudo_space_number) \
841 CASE_BODY(kFromStart, kPlain, kStartOfObject, space_number, offset_from_start)
842
843 // We generate 15 cases and bodies that process special tags that combine
844 // the raw data tag and the length into one byte.
Steve Blockd0582a62009-12-15 09:54:21 +0000845#define RAW_CASE(index, size) \
Leon Clarkef7060e22010-06-03 12:02:55 +0100846 case kRawData + index: { \
Steve Blockd0582a62009-12-15 09:54:21 +0000847 byte* raw_data_out = reinterpret_cast<byte*>(current); \
848 source_->CopyRaw(raw_data_out, size); \
849 current = reinterpret_cast<Object**>(raw_data_out + size); \
850 break; \
851 }
852 COMMON_RAW_LENGTHS(RAW_CASE)
853#undef RAW_CASE
Leon Clarkef7060e22010-06-03 12:02:55 +0100854
855 // Deserialize a chunk of raw data that doesn't have one of the popular
856 // lengths.
857 case kRawData: {
Steve Blockd0582a62009-12-15 09:54:21 +0000858 int size = source_->GetInt();
859 byte* raw_data_out = reinterpret_cast<byte*>(current);
860 source_->CopyRaw(raw_data_out, size);
861 current = reinterpret_cast<Object**>(raw_data_out + size);
862 break;
863 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100864
865 // Deserialize a new object and write a pointer to it to the current
866 // object.
867 ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject)
868 // Deserialize a new code object and write a pointer to its first
869 // instruction to the current code object.
870 ONE_PER_SPACE(kNewObject, kFromCode, kFirstInstruction)
871 // Find a recently deserialized object using its offset from the current
872 // allocation point and write a pointer to it to the current object.
873 ALL_SPACES(kBackref, kPlain, kStartOfObject)
874 // Find a recently deserialized code object using its offset from the
875 // current allocation point and write a pointer to its first instruction
876 // to the current code object.
877 ALL_SPACES(kBackref, kFromCode, kFirstInstruction)
878 // Find an already deserialized object using its offset from the start
879 // and write a pointer to it to the current object.
880 ALL_SPACES(kFromStart, kPlain, kStartOfObject)
881 // 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)
900 // Find an external reference and write a pointer to it to the current
901 // object.
902 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
903 CASE_BODY(kExternalReference,
904 kPlain,
905 kStartOfObject,
906 0,
907 kUnknownOffsetFromStart)
908 // Find an external reference and write a pointer to it in the current
909 // code object.
910 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
911 CASE_BODY(kExternalReference,
912 kFromCode,
913 kStartOfObject,
914 0,
915 kUnknownOffsetFromStart)
916
917#undef CASE_STATEMENT
918#undef CASE_BODY
919#undef ONE_PER_SPACE
920#undef ALL_SPACES
921#undef EMIT_COMMON_REFERENCE_PATTERNS
922#undef ASSIGN_DEST_SPACE
923
924 case kNewPage: {
Steve Blockd0582a62009-12-15 09:54:21 +0000925 int space = source_->Get();
926 pages_[space].Add(last_object_address_);
Steve Block6ded16b2010-05-10 14:33:55 +0100927 if (space == CODE_SPACE) {
928 CPU::FlushICache(last_object_address_, Page::kPageSize);
929 }
Steve Blockd0582a62009-12-15 09:54:21 +0000930 break;
931 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100932
933 case kNativesStringResource: {
Steve Blockd0582a62009-12-15 09:54:21 +0000934 int index = source_->Get();
935 Vector<const char> source_vector = Natives::GetScriptSource(index);
936 NativesExternalStringResource* resource =
937 new NativesExternalStringResource(source_vector.start());
938 *current++ = reinterpret_cast<Object*>(resource);
939 break;
940 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100941
942 case kSynchronize: {
Leon Clarked91b9f72010-01-27 17:25:45 +0000943 // If we get here then that indicates that you have a mismatch between
944 // the number of GC roots when serializing and deserializing.
945 UNREACHABLE();
946 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100947
Steve Blockd0582a62009-12-15 09:54:21 +0000948 default:
949 UNREACHABLE();
950 }
951 }
952 ASSERT_EQ(current, limit);
953}
954
955
956void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
957 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
958 for (int shift = max_shift; shift > 0; shift -= 7) {
959 if (integer >= static_cast<uintptr_t>(1u) << shift) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000960 Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart");
Steve Blockd0582a62009-12-15 09:54:21 +0000961 }
962 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000963 PutSection(static_cast<int>(integer & 0x7f), "IntLastPart");
Steve Blockd0582a62009-12-15 09:54:21 +0000964}
965
Steve Blocka7e24c12009-10-30 11:49:00 +0000966#ifdef DEBUG
Steve Blockd0582a62009-12-15 09:54:21 +0000967
968void Deserializer::Synchronize(const char* tag) {
969 int data = source_->Get();
970 // If this assert fails then that indicates that you have a mismatch between
971 // the number of GC roots when serializing and deserializing.
Leon Clarkef7060e22010-06-03 12:02:55 +0100972 ASSERT_EQ(kSynchronize, data);
Steve Blockd0582a62009-12-15 09:54:21 +0000973 do {
974 int character = source_->Get();
975 if (character == 0) break;
976 if (FLAG_debug_serialization) {
977 PrintF("%c", character);
978 }
979 } while (true);
980 if (FLAG_debug_serialization) {
981 PrintF("\n");
982 }
983}
984
Steve Blocka7e24c12009-10-30 11:49:00 +0000985
986void Serializer::Synchronize(const char* tag) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100987 sink_->Put(kSynchronize, tag);
Steve Blockd0582a62009-12-15 09:54:21 +0000988 int character;
989 do {
990 character = *tag++;
991 sink_->PutSection(character, "TagCharacter");
992 } while (character != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000993}
Steve Blockd0582a62009-12-15 09:54:21 +0000994
Steve Blocka7e24c12009-10-30 11:49:00 +0000995#endif
996
Steve Blockd0582a62009-12-15 09:54:21 +0000997Serializer::Serializer(SnapshotByteSink* sink)
998 : sink_(sink),
999 current_root_index_(0),
Andrei Popescu31002712010-02-23 13:46:05 +00001000 external_reference_encoder_(new ExternalReferenceEncoder),
Leon Clarkee46be812010-01-19 14:06:41 +00001001 large_object_total_(0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001002 for (int i = 0; i <= LAST_SPACE; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +00001003 fullness_[i] = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001004 }
1005}
1006
1007
Andrei Popescu31002712010-02-23 13:46:05 +00001008Serializer::~Serializer() {
1009 delete external_reference_encoder_;
1010}
1011
1012
Leon Clarked91b9f72010-01-27 17:25:45 +00001013void StartupSerializer::SerializeStrongReferences() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001014 // No active threads.
1015 CHECK_EQ(NULL, ThreadState::FirstInUse());
1016 // No active or weak handles.
1017 CHECK(HandleScopeImplementer::instance()->blocks()->is_empty());
1018 CHECK_EQ(0, GlobalHandles::NumberOfWeakHandles());
Steve Blockd0582a62009-12-15 09:54:21 +00001019 // We don't support serializing installed extensions.
1020 for (RegisteredExtension* ext = RegisteredExtension::first_extension();
1021 ext != NULL;
1022 ext = ext->next()) {
1023 CHECK_NE(v8::INSTALLED, ext->state());
1024 }
Leon Clarked91b9f72010-01-27 17:25:45 +00001025 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
Steve Blocka7e24c12009-10-30 11:49:00 +00001026}
1027
1028
Leon Clarked91b9f72010-01-27 17:25:45 +00001029void PartialSerializer::Serialize(Object** object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001030 this->VisitPointer(object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001031
1032 // After we have done the partial serialization the partial snapshot cache
1033 // will contain some references needed to decode the partial snapshot. We
1034 // fill it up with undefineds so it has a predictable length so the
1035 // deserialization code doesn't need to know the length.
1036 for (int index = partial_snapshot_cache_length_;
1037 index < kPartialSnapshotCacheCapacity;
1038 index++) {
1039 partial_snapshot_cache_[index] = Heap::undefined_value();
1040 startup_serializer_->VisitPointer(&partial_snapshot_cache_[index]);
1041 }
1042 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Leon Clarkee46be812010-01-19 14:06:41 +00001043}
1044
1045
Steve Blocka7e24c12009-10-30 11:49:00 +00001046void Serializer::VisitPointers(Object** start, Object** end) {
Steve Blockd0582a62009-12-15 09:54:21 +00001047 for (Object** current = start; current < end; current++) {
1048 if ((*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001049 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001050 sink_->PutInt(kPointerSize, "length");
1051 for (int i = 0; i < kPointerSize; i++) {
1052 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1053 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001054 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001055 SerializeObject(*current, kPlain, kStartOfObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001056 }
1057 }
1058}
1059
1060
Leon Clarked91b9f72010-01-27 17:25:45 +00001061Object* SerializerDeserializer::partial_snapshot_cache_[
1062 kPartialSnapshotCacheCapacity];
1063int SerializerDeserializer::partial_snapshot_cache_length_ = 0;
1064
1065
1066// This ensures that the partial snapshot cache keeps things alive during GC and
1067// tracks their movement. When it is called during serialization of the startup
1068// snapshot the partial snapshot is empty, so nothing happens. When the partial
1069// (context) snapshot is created, this array is populated with the pointers that
1070// the partial snapshot will need. As that happens we emit serialized objects to
1071// the startup snapshot that correspond to the elements of this cache array. On
1072// deserialization we therefore need to visit the cache array. This fills it up
1073// with pointers to deserialized objects.
Steve Block6ded16b2010-05-10 14:33:55 +01001074void SerializerDeserializer::Iterate(ObjectVisitor* visitor) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001075 visitor->VisitPointers(
1076 &partial_snapshot_cache_[0],
1077 &partial_snapshot_cache_[partial_snapshot_cache_length_]);
1078}
1079
1080
1081// When deserializing we need to set the size of the snapshot cache. This means
1082// the root iteration code (above) will iterate over array elements, writing the
1083// references to deserialized objects in them.
1084void SerializerDeserializer::SetSnapshotCacheSize(int size) {
1085 partial_snapshot_cache_length_ = size;
1086}
1087
1088
1089int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1090 for (int i = 0; i < partial_snapshot_cache_length_; i++) {
1091 Object* entry = partial_snapshot_cache_[i];
1092 if (entry == heap_object) return i;
1093 }
Andrei Popescu31002712010-02-23 13:46:05 +00001094
Leon Clarked91b9f72010-01-27 17:25:45 +00001095 // We didn't find the object in the cache. So we add it to the cache and
1096 // then visit the pointer so that it becomes part of the startup snapshot
1097 // and we can refer to it from the partial snapshot.
1098 int length = partial_snapshot_cache_length_;
1099 CHECK(length < kPartialSnapshotCacheCapacity);
1100 partial_snapshot_cache_[length] = heap_object;
1101 startup_serializer_->VisitPointer(&partial_snapshot_cache_[length]);
1102 // We don't recurse from the startup snapshot generator into the partial
1103 // snapshot generator.
1104 ASSERT(length == partial_snapshot_cache_length_);
1105 return partial_snapshot_cache_length_++;
1106}
1107
1108
1109int PartialSerializer::RootIndex(HeapObject* heap_object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001110 for (int i = 0; i < Heap::kRootListLength; i++) {
1111 Object* root = Heap::roots_address()[i];
1112 if (root == heap_object) return i;
1113 }
1114 return kInvalidRootIndex;
1115}
1116
1117
Leon Clarked91b9f72010-01-27 17:25:45 +00001118// Encode the location of an already deserialized object in order to write its
1119// location into a later object. We can encode the location as an offset from
1120// the start of the deserialized objects or as an offset backwards from the
1121// current allocation pointer.
1122void Serializer::SerializeReferenceToPreviousObject(
1123 int space,
1124 int address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001125 HowToCode how_to_code,
1126 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001127 int offset = CurrentAllocationAddress(space) - address;
1128 bool from_start = true;
1129 if (SpaceIsPaged(space)) {
1130 // For paged space it is simple to encode back from current allocation if
1131 // the object is on the same page as the current allocation pointer.
1132 if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1133 (address >> kPageSizeBits)) {
1134 from_start = false;
1135 address = offset;
1136 }
1137 } else if (space == NEW_SPACE) {
1138 // For new space it is always simple to encode back from current allocation.
1139 if (offset < address) {
1140 from_start = false;
1141 address = offset;
1142 }
1143 }
1144 // If we are actually dealing with real offsets (and not a numbering of
1145 // all objects) then we should shift out the bits that are always 0.
1146 if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
Leon Clarkef7060e22010-06-03 12:02:55 +01001147 if (from_start) {
1148#define COMMON_REFS_CASE(pseudo_space, actual_space, offset) \
1149 if (space == actual_space && address == offset && \
1150 how_to_code == kPlain && where_to_point == kStartOfObject) { \
1151 sink_->Put(kFromStart + how_to_code + where_to_point + \
1152 pseudo_space, "RefSer"); \
1153 } else /* NOLINT */
1154 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
1155#undef COMMON_REFS_CASE
1156 { /* NOLINT */
1157 sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer");
Leon Clarked91b9f72010-01-27 17:25:45 +00001158 sink_->PutInt(address, "address");
1159 }
1160 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001161 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1162 sink_->PutInt(address, "address");
Leon Clarked91b9f72010-01-27 17:25:45 +00001163 }
1164}
1165
1166
1167void StartupSerializer::SerializeObject(
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001168 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001169 HowToCode how_to_code,
1170 WhereToPoint where_to_point) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001171 CHECK(o->IsHeapObject());
1172 HeapObject* heap_object = HeapObject::cast(o);
Leon Clarked91b9f72010-01-27 17:25:45 +00001173
1174 if (address_mapper_.IsMapped(heap_object)) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001175 int space = SpaceOfAlreadySerializedObject(heap_object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001176 int address = address_mapper_.MappedTo(heap_object);
1177 SerializeReferenceToPreviousObject(space,
1178 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001179 how_to_code,
1180 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001181 } else {
1182 // Object has not yet been serialized. Serialize it here.
1183 ObjectSerializer object_serializer(this,
1184 heap_object,
1185 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001186 how_to_code,
1187 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001188 object_serializer.Serialize();
1189 }
1190}
1191
1192
1193void StartupSerializer::SerializeWeakReferences() {
1194 for (int i = partial_snapshot_cache_length_;
1195 i < kPartialSnapshotCacheCapacity;
1196 i++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001197 sink_->Put(kRootArray + kPlain + kStartOfObject, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001198 sink_->PutInt(Heap::kUndefinedValueRootIndex, "root_index");
1199 }
1200 Heap::IterateWeakRoots(this, VISIT_ALL);
1201}
1202
1203
1204void PartialSerializer::SerializeObject(
1205 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001206 HowToCode how_to_code,
1207 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001208 CHECK(o->IsHeapObject());
1209 HeapObject* heap_object = HeapObject::cast(o);
1210
1211 int root_index;
1212 if ((root_index = RootIndex(heap_object)) != kInvalidRootIndex) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001213 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001214 sink_->PutInt(root_index, "root_index");
1215 return;
1216 }
1217
1218 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1219 int cache_index = PartialSnapshotCacheIndex(heap_object);
Leon Clarkef7060e22010-06-03 12:02:55 +01001220 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1221 "PartialSnapshotCache");
Leon Clarked91b9f72010-01-27 17:25:45 +00001222 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1223 return;
1224 }
1225
1226 // Pointers from the partial snapshot to the objects in the startup snapshot
1227 // should go through the root array or through the partial snapshot cache.
1228 // If this is not the case you may have to add something to the root array.
1229 ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1230 // All the symbols that the partial snapshot needs should be either in the
1231 // root table or in the partial snapshot cache.
1232 ASSERT(!heap_object->IsSymbol());
1233
1234 if (address_mapper_.IsMapped(heap_object)) {
1235 int space = SpaceOfAlreadySerializedObject(heap_object);
1236 int address = address_mapper_.MappedTo(heap_object);
1237 SerializeReferenceToPreviousObject(space,
1238 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001239 how_to_code,
1240 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001241 } else {
1242 // Object has not yet been serialized. Serialize it here.
1243 ObjectSerializer serializer(this,
1244 heap_object,
1245 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001246 how_to_code,
1247 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001248 serializer.Serialize();
1249 }
1250}
1251
1252
Steve Blockd0582a62009-12-15 09:54:21 +00001253void Serializer::ObjectSerializer::Serialize() {
1254 int space = Serializer::SpaceOfObject(object_);
1255 int size = object_->Size();
1256
Leon Clarkef7060e22010-06-03 12:02:55 +01001257 sink_->Put(kNewObject + reference_representation_ + space,
1258 "ObjectSerialization");
Steve Blockd0582a62009-12-15 09:54:21 +00001259 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1260
Leon Clarkee46be812010-01-19 14:06:41 +00001261 LOG(SnapshotPositionEvent(object_->address(), sink_->Position()));
1262
Steve Blockd0582a62009-12-15 09:54:21 +00001263 // Mark this object as already serialized.
1264 bool start_new_page;
Leon Clarked91b9f72010-01-27 17:25:45 +00001265 int offset = serializer_->Allocate(space, size, &start_new_page);
1266 serializer_->address_mapper()->AddMapping(object_, offset);
Steve Blockd0582a62009-12-15 09:54:21 +00001267 if (start_new_page) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001268 sink_->Put(kNewPage, "NewPage");
Steve Blockd0582a62009-12-15 09:54:21 +00001269 sink_->PutSection(space, "NewPageSpace");
1270 }
1271
1272 // Serialize the map (first word of the object).
Leon Clarkef7060e22010-06-03 12:02:55 +01001273 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001274
1275 // Serialize the rest of the object.
1276 CHECK_EQ(0, bytes_processed_so_far_);
1277 bytes_processed_so_far_ = kPointerSize;
1278 object_->IterateBody(object_->map()->instance_type(), size, this);
1279 OutputRawData(object_->address() + size);
1280}
1281
1282
1283void Serializer::ObjectSerializer::VisitPointers(Object** start,
1284 Object** end) {
1285 Object** current = start;
1286 while (current < end) {
1287 while (current < end && (*current)->IsSmi()) current++;
1288 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1289
1290 while (current < end && !(*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001291 serializer_->SerializeObject(*current, kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001292 bytes_processed_so_far_ += kPointerSize;
1293 current++;
1294 }
1295 }
1296}
1297
1298
1299void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1300 Address* end) {
1301 Address references_start = reinterpret_cast<Address>(start);
1302 OutputRawData(references_start);
1303
1304 for (Address* current = start; current < end; current++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001305 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
Steve Blockd0582a62009-12-15 09:54:21 +00001306 int reference_id = serializer_->EncodeExternalReference(*current);
1307 sink_->PutInt(reference_id, "reference id");
1308 }
1309 bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1310}
1311
1312
1313void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1314 Address target_start = rinfo->target_address_address();
1315 OutputRawData(target_start);
1316 Address target = rinfo->target_address();
1317 uint32_t encoding = serializer_->EncodeExternalReference(target);
1318 CHECK(target == NULL ? encoding == 0 : encoding != 0);
Leon Clarkef7060e22010-06-03 12:02:55 +01001319 int representation;
1320 // Can't use a ternary operator because of gcc.
1321 if (rinfo->IsCodedSpecially()) {
1322 representation = kStartOfObject + kFromCode;
1323 } else {
1324 representation = kStartOfObject + kPlain;
1325 }
1326 sink_->Put(kExternalReference + representation, "ExternalReference");
Steve Blockd0582a62009-12-15 09:54:21 +00001327 sink_->PutInt(encoding, "reference id");
Leon Clarkef7060e22010-06-03 12:02:55 +01001328 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001329}
1330
1331
1332void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1333 CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1334 Address target_start = rinfo->target_address_address();
1335 OutputRawData(target_start);
1336 Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
Leon Clarkef7060e22010-06-03 12:02:55 +01001337 serializer_->SerializeObject(target, kFromCode, kFirstInstruction);
1338 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001339}
1340
1341
1342void Serializer::ObjectSerializer::VisitExternalAsciiString(
1343 v8::String::ExternalAsciiStringResource** resource_pointer) {
1344 Address references_start = reinterpret_cast<Address>(resource_pointer);
1345 OutputRawData(references_start);
1346 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1347 Object* source = Heap::natives_source_cache()->get(i);
1348 if (!source->IsUndefined()) {
1349 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1350 typedef v8::String::ExternalAsciiStringResource Resource;
1351 Resource* resource = string->resource();
1352 if (resource == *resource_pointer) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001353 sink_->Put(kNativesStringResource, "NativesStringResource");
Steve Blockd0582a62009-12-15 09:54:21 +00001354 sink_->PutSection(i, "NativesStringResourceEnd");
1355 bytes_processed_so_far_ += sizeof(resource);
1356 return;
1357 }
1358 }
1359 }
1360 // One of the strings in the natives cache should match the resource. We
1361 // can't serialize any other kinds of external strings.
1362 UNREACHABLE();
1363}
1364
1365
1366void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1367 Address object_start = object_->address();
1368 int up_to_offset = static_cast<int>(up_to - object_start);
1369 int skipped = up_to_offset - bytes_processed_so_far_;
1370 // This assert will fail if the reloc info gives us the target_address_address
1371 // locations in a non-ascending order. Luckily that doesn't happen.
1372 ASSERT(skipped >= 0);
1373 if (skipped != 0) {
1374 Address base = object_start + bytes_processed_so_far_;
1375#define RAW_CASE(index, length) \
1376 if (skipped == length) { \
Leon Clarkef7060e22010-06-03 12:02:55 +01001377 sink_->PutSection(kRawData + index, "RawDataFixed"); \
Steve Blockd0582a62009-12-15 09:54:21 +00001378 } else /* NOLINT */
1379 COMMON_RAW_LENGTHS(RAW_CASE)
1380#undef RAW_CASE
1381 { /* NOLINT */
Leon Clarkef7060e22010-06-03 12:02:55 +01001382 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001383 sink_->PutInt(skipped, "length");
1384 }
1385 for (int i = 0; i < skipped; i++) {
1386 unsigned int data = base[i];
1387 sink_->PutSection(data, "Byte");
1388 }
1389 bytes_processed_so_far_ += skipped;
1390 }
1391}
1392
1393
1394int Serializer::SpaceOfObject(HeapObject* object) {
1395 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1396 AllocationSpace s = static_cast<AllocationSpace>(i);
1397 if (Heap::InSpace(object, s)) {
1398 if (i == LO_SPACE) {
1399 if (object->IsCode()) {
1400 return kLargeCode;
1401 } else if (object->IsFixedArray()) {
1402 return kLargeFixedArray;
1403 } else {
1404 return kLargeData;
1405 }
1406 }
1407 return i;
1408 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001409 }
1410 UNREACHABLE();
Steve Blockd0582a62009-12-15 09:54:21 +00001411 return 0;
1412}
1413
1414
1415int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1416 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1417 AllocationSpace s = static_cast<AllocationSpace>(i);
1418 if (Heap::InSpace(object, s)) {
1419 return i;
1420 }
1421 }
1422 UNREACHABLE();
1423 return 0;
1424}
1425
1426
1427int Serializer::Allocate(int space, int size, bool* new_page) {
1428 CHECK(space >= 0 && space < kNumberOfSpaces);
1429 if (SpaceIsLarge(space)) {
1430 // In large object space we merely number the objects instead of trying to
1431 // determine some sort of address.
1432 *new_page = true;
Leon Clarkee46be812010-01-19 14:06:41 +00001433 large_object_total_ += size;
Steve Blockd0582a62009-12-15 09:54:21 +00001434 return fullness_[LO_SPACE]++;
1435 }
1436 *new_page = false;
1437 if (fullness_[space] == 0) {
1438 *new_page = true;
1439 }
1440 if (SpaceIsPaged(space)) {
1441 // Paged spaces are a little special. We encode their addresses as if the
1442 // pages were all contiguous and each page were filled up in the range
1443 // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1444 // and allocation does not start at offset 0 in the page, but this scheme
1445 // means the deserializer can get the page number quickly by shifting the
1446 // serialized address.
1447 CHECK(IsPowerOf2(Page::kPageSize));
1448 int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1449 CHECK(size <= Page::kObjectAreaSize);
1450 if (used_in_this_page + size > Page::kObjectAreaSize) {
1451 *new_page = true;
1452 fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1453 }
1454 }
1455 int allocation_address = fullness_[space];
1456 fullness_[space] = allocation_address + size;
1457 return allocation_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001458}
1459
1460
1461} } // namespace v8::internal