blob: 00a601ffd95ff15f0131d56e555fca6cd4b0144c [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "execution.h"
33#include "global-handles.h"
34#include "ic-inl.h"
35#include "natives.h"
36#include "platform.h"
37#include "runtime.h"
38#include "serialize.h"
39#include "stub-cache.h"
40#include "v8threads.h"
Steve Block3ce2e202009-11-05 08:53:23 +000041#include "top.h"
Steve Blockd0582a62009-12-15 09:54:21 +000042#include "bootstrapper.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44namespace v8 {
45namespace internal {
46
Steve Blocka7e24c12009-10-30 11:49:00 +000047
48// -----------------------------------------------------------------------------
49// Coding of external references.
50
51// The encoding of an external reference. The type is in the high word.
52// The id is in the low word.
53static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
54 return static_cast<uint32_t>(type) << 16 | id;
55}
56
57
58static int* GetInternalPointer(StatsCounter* counter) {
59 // All counters refer to dummy_counter, if deserializing happens without
60 // setting up counters.
61 static int dummy_counter = 0;
62 return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
63}
64
65
66// ExternalReferenceTable is a helper class that defines the relationship
67// between external references and their encodings. It is used to build
68// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
69class ExternalReferenceTable {
70 public:
71 static ExternalReferenceTable* instance() {
72 if (!instance_) instance_ = new ExternalReferenceTable();
73 return instance_;
74 }
75
76 int size() const { return refs_.length(); }
77
78 Address address(int i) { return refs_[i].address; }
79
80 uint32_t code(int i) { return refs_[i].code; }
81
82 const char* name(int i) { return refs_[i].name; }
83
84 int max_id(int code) { return max_id_[code]; }
85
86 private:
87 static ExternalReferenceTable* instance_;
88
89 ExternalReferenceTable() : refs_(64) { PopulateTable(); }
90 ~ExternalReferenceTable() { }
91
92 struct ExternalReferenceEntry {
93 Address address;
94 uint32_t code;
95 const char* name;
96 };
97
98 void PopulateTable();
99
100 // For a few types of references, we can get their address from their id.
101 void AddFromId(TypeCode type, uint16_t id, const char* name);
102
103 // For other types of references, the caller will figure out the address.
104 void Add(Address address, TypeCode type, uint16_t id, const char* name);
105
106 List<ExternalReferenceEntry> refs_;
107 int max_id_[kTypeCodeCount];
108};
109
110
111ExternalReferenceTable* ExternalReferenceTable::instance_ = NULL;
112
113
114void ExternalReferenceTable::AddFromId(TypeCode type,
115 uint16_t id,
116 const char* name) {
117 Address address;
118 switch (type) {
119 case C_BUILTIN: {
120 ExternalReference ref(static_cast<Builtins::CFunctionId>(id));
121 address = ref.address();
122 break;
123 }
124 case BUILTIN: {
125 ExternalReference ref(static_cast<Builtins::Name>(id));
126 address = ref.address();
127 break;
128 }
129 case RUNTIME_FUNCTION: {
130 ExternalReference ref(static_cast<Runtime::FunctionId>(id));
131 address = ref.address();
132 break;
133 }
134 case IC_UTILITY: {
135 ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)));
136 address = ref.address();
137 break;
138 }
139 default:
140 UNREACHABLE();
141 return;
142 }
143 Add(address, type, id, name);
144}
145
146
147void ExternalReferenceTable::Add(Address address,
148 TypeCode type,
149 uint16_t id,
150 const char* name) {
Steve Blockd0582a62009-12-15 09:54:21 +0000151 ASSERT_NE(NULL, address);
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 ExternalReferenceEntry entry;
153 entry.address = address;
154 entry.code = EncodeExternal(type, id);
155 entry.name = name;
Steve Blockd0582a62009-12-15 09:54:21 +0000156 ASSERT_NE(0, entry.code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 refs_.Add(entry);
158 if (id > max_id_[type]) max_id_[type] = id;
159}
160
161
162void ExternalReferenceTable::PopulateTable() {
163 for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
164 max_id_[type_code] = 0;
165 }
166
167 // The following populates all of the different type of external references
168 // into the ExternalReferenceTable.
169 //
170 // NOTE: This function was originally 100k of code. It has since been
171 // rewritten to be mostly table driven, as the callback macro style tends to
172 // very easily cause code bloat. Please be careful in the future when adding
173 // new references.
174
175 struct RefTableEntry {
176 TypeCode type;
177 uint16_t id;
178 const char* name;
179 };
180
181 static const RefTableEntry ref_table[] = {
182 // Builtins
Leon Clarkee46be812010-01-19 14:06:41 +0000183#define DEF_ENTRY_C(name, ignored) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000184 { C_BUILTIN, \
185 Builtins::c_##name, \
186 "Builtins::" #name },
187
188 BUILTIN_LIST_C(DEF_ENTRY_C)
189#undef DEF_ENTRY_C
190
Leon Clarkee46be812010-01-19 14:06:41 +0000191#define DEF_ENTRY_C(name, ignored) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 { BUILTIN, \
193 Builtins::name, \
194 "Builtins::" #name },
Leon Clarkee46be812010-01-19 14:06:41 +0000195#define DEF_ENTRY_A(name, kind, state) DEF_ENTRY_C(name, ignored)
Steve Blocka7e24c12009-10-30 11:49:00 +0000196
197 BUILTIN_LIST_C(DEF_ENTRY_C)
198 BUILTIN_LIST_A(DEF_ENTRY_A)
199 BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
200#undef DEF_ENTRY_C
201#undef DEF_ENTRY_A
202
203 // Runtime functions
204#define RUNTIME_ENTRY(name, nargs, ressize) \
205 { RUNTIME_FUNCTION, \
206 Runtime::k##name, \
207 "Runtime::" #name },
208
209 RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
210#undef RUNTIME_ENTRY
211
212 // IC utilities
213#define IC_ENTRY(name) \
214 { IC_UTILITY, \
215 IC::k##name, \
216 "IC::" #name },
217
218 IC_UTIL_LIST(IC_ENTRY)
219#undef IC_ENTRY
220 }; // end of ref_table[].
221
222 for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
223 AddFromId(ref_table[i].type, ref_table[i].id, ref_table[i].name);
224 }
225
226#ifdef ENABLE_DEBUGGER_SUPPORT
227 // Debug addresses
228 Add(Debug_Address(Debug::k_after_break_target_address).address(),
229 DEBUG_ADDRESS,
230 Debug::k_after_break_target_address << kDebugIdShift,
231 "Debug::after_break_target_address()");
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100232 Add(Debug_Address(Debug::k_debug_break_slot_address).address(),
233 DEBUG_ADDRESS,
234 Debug::k_debug_break_slot_address << kDebugIdShift,
235 "Debug::debug_break_slot_address()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 Add(Debug_Address(Debug::k_debug_break_return_address).address(),
237 DEBUG_ADDRESS,
238 Debug::k_debug_break_return_address << kDebugIdShift,
239 "Debug::debug_break_return_address()");
Ben Murdochbb769b22010-08-11 14:56:33 +0100240 Add(Debug_Address(Debug::k_restarter_frame_function_pointer).address(),
241 DEBUG_ADDRESS,
242 Debug::k_restarter_frame_function_pointer << kDebugIdShift,
243 "Debug::restarter_frame_function_pointer_address()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000244#endif
245
246 // Stat counters
247 struct StatsRefTableEntry {
248 StatsCounter* counter;
249 uint16_t id;
250 const char* name;
251 };
252
253 static const StatsRefTableEntry stats_ref_table[] = {
254#define COUNTER_ENTRY(name, caption) \
255 { &Counters::name, \
256 Counters::k_##name, \
257 "Counters::" #name },
258
259 STATS_COUNTER_LIST_1(COUNTER_ENTRY)
260 STATS_COUNTER_LIST_2(COUNTER_ENTRY)
261#undef COUNTER_ENTRY
262 }; // end of stats_ref_table[].
263
264 for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
265 Add(reinterpret_cast<Address>(
266 GetInternalPointer(stats_ref_table[i].counter)),
267 STATS_COUNTER,
268 stats_ref_table[i].id,
269 stats_ref_table[i].name);
270 }
271
272 // Top addresses
Steve Block3ce2e202009-11-05 08:53:23 +0000273 const char* top_address_format = "Top::%s";
274
275 const char* AddressNames[] = {
276#define C(name) #name,
277 TOP_ADDRESS_LIST(C)
278 TOP_ADDRESS_LIST_PROF(C)
279 NULL
280#undef C
281 };
282
Steve Blockd0582a62009-12-15 09:54:21 +0000283 int top_format_length = StrLength(top_address_format) - 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000284 for (uint16_t i = 0; i < Top::k_top_address_count; ++i) {
Steve Block3ce2e202009-11-05 08:53:23 +0000285 const char* address_name = AddressNames[i];
286 Vector<char> name =
Steve Blockd0582a62009-12-15 09:54:21 +0000287 Vector<char>::New(top_format_length + StrLength(address_name) + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000288 const char* chars = name.start();
Steve Block3ce2e202009-11-05 08:53:23 +0000289 OS::SNPrintF(name, top_address_format, address_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000290 Add(Top::get_address_from_id((Top::AddressId)i), TOP_ADDRESS, i, chars);
291 }
292
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 // Accessors
294#define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
295 Add((Address)&Accessors::name, \
296 ACCESSOR, \
297 Accessors::k##name, \
298 "Accessors::" #name);
299
300 ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
301#undef ACCESSOR_DESCRIPTOR_DECLARATION
302
303 // Stub cache tables
304 Add(SCTableReference::keyReference(StubCache::kPrimary).address(),
305 STUB_CACHE_TABLE,
306 1,
307 "StubCache::primary_->key");
308 Add(SCTableReference::valueReference(StubCache::kPrimary).address(),
309 STUB_CACHE_TABLE,
310 2,
311 "StubCache::primary_->value");
312 Add(SCTableReference::keyReference(StubCache::kSecondary).address(),
313 STUB_CACHE_TABLE,
314 3,
315 "StubCache::secondary_->key");
316 Add(SCTableReference::valueReference(StubCache::kSecondary).address(),
317 STUB_CACHE_TABLE,
318 4,
319 "StubCache::secondary_->value");
320
321 // Runtime entries
322 Add(ExternalReference::perform_gc_function().address(),
323 RUNTIME_ENTRY,
324 1,
325 "Runtime::PerformGC");
Steve Block6ded16b2010-05-10 14:33:55 +0100326 Add(ExternalReference::fill_heap_number_with_random_function().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 RUNTIME_ENTRY,
328 2,
Steve Block6ded16b2010-05-10 14:33:55 +0100329 "V8::FillHeapNumberWithRandom");
330
331 Add(ExternalReference::random_uint32_function().address(),
332 RUNTIME_ENTRY,
333 3,
334 "V8::Random");
Steve Blocka7e24c12009-10-30 11:49:00 +0000335
John Reck59135872010-11-02 12:39:01 -0700336 Add(ExternalReference::delete_handle_scope_extensions().address(),
337 RUNTIME_ENTRY,
338 3,
339 "HandleScope::DeleteExtensions");
340
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 // Miscellaneous
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 Add(ExternalReference::the_hole_value_location().address(),
343 UNCLASSIFIED,
344 2,
345 "Factory::the_hole_value().location()");
346 Add(ExternalReference::roots_address().address(),
347 UNCLASSIFIED,
348 3,
349 "Heap::roots_address()");
Steve Blockd0582a62009-12-15 09:54:21 +0000350 Add(ExternalReference::address_of_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 UNCLASSIFIED,
352 4,
353 "StackGuard::address_of_jslimit()");
Steve Blockd0582a62009-12-15 09:54:21 +0000354 Add(ExternalReference::address_of_real_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 UNCLASSIFIED,
356 5,
Steve Blockd0582a62009-12-15 09:54:21 +0000357 "StackGuard::address_of_real_jslimit()");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100358#ifndef V8_INTERPRETED_REGEXP
Steve Blockd0582a62009-12-15 09:54:21 +0000359 Add(ExternalReference::address_of_regexp_stack_limit().address(),
360 UNCLASSIFIED,
361 6,
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 "RegExpStack::limit_address()");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100363 Add(ExternalReference::address_of_regexp_stack_memory_address().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000365 7,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100366 "RegExpStack::memory_address()");
367 Add(ExternalReference::address_of_regexp_stack_memory_size().address(),
368 UNCLASSIFIED,
369 8,
370 "RegExpStack::memory_size()");
371 Add(ExternalReference::address_of_static_offsets_vector().address(),
372 UNCLASSIFIED,
373 9,
374 "OffsetsVector::static_offsets_vector");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100375#endif // V8_INTERPRETED_REGEXP
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100376 Add(ExternalReference::new_space_start().address(),
377 UNCLASSIFIED,
378 10,
Steve Blocka7e24c12009-10-30 11:49:00 +0000379 "Heap::NewSpaceStart()");
Andrei Popescu402d9372010-02-26 13:31:12 +0000380 Add(ExternalReference::new_space_mask().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000381 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100382 11,
Andrei Popescu402d9372010-02-26 13:31:12 +0000383 "Heap::NewSpaceMask()");
384 Add(ExternalReference::heap_always_allocate_scope_depth().address(),
385 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100386 12,
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 "Heap::always_allocate_scope_depth()");
388 Add(ExternalReference::new_space_allocation_limit_address().address(),
389 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100390 13,
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 "Heap::NewSpaceAllocationLimitAddress()");
392 Add(ExternalReference::new_space_allocation_top_address().address(),
393 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100394 14,
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 "Heap::NewSpaceAllocationTopAddress()");
396#ifdef ENABLE_DEBUGGER_SUPPORT
397 Add(ExternalReference::debug_break().address(),
398 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100399 15,
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 "Debug::Break()");
401 Add(ExternalReference::debug_step_in_fp_address().address(),
402 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100403 16,
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 "Debug::step_in_fp_addr()");
405#endif
406 Add(ExternalReference::double_fp_operation(Token::ADD).address(),
407 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100408 17,
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 "add_two_doubles");
410 Add(ExternalReference::double_fp_operation(Token::SUB).address(),
411 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100412 18,
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 "sub_two_doubles");
414 Add(ExternalReference::double_fp_operation(Token::MUL).address(),
415 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100416 19,
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 "mul_two_doubles");
418 Add(ExternalReference::double_fp_operation(Token::DIV).address(),
419 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100420 20,
Steve Blocka7e24c12009-10-30 11:49:00 +0000421 "div_two_doubles");
422 Add(ExternalReference::double_fp_operation(Token::MOD).address(),
423 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100424 21,
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 "mod_two_doubles");
426 Add(ExternalReference::compare_doubles().address(),
427 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100428 22,
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 "compare_doubles");
Steve Block6ded16b2010-05-10 14:33:55 +0100430#ifndef V8_INTERPRETED_REGEXP
431 Add(ExternalReference::re_case_insensitive_compare_uc16().address(),
432 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100433 23,
Steve Blocka7e24c12009-10-30 11:49:00 +0000434 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
435 Add(ExternalReference::re_check_stack_guard_state().address(),
436 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100437 24,
Steve Blocka7e24c12009-10-30 11:49:00 +0000438 "RegExpMacroAssembler*::CheckStackGuardState()");
439 Add(ExternalReference::re_grow_stack().address(),
440 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100441 25,
Steve Blocka7e24c12009-10-30 11:49:00 +0000442 "NativeRegExpMacroAssembler::GrowStack()");
Leon Clarkee46be812010-01-19 14:06:41 +0000443 Add(ExternalReference::re_word_character_map().address(),
444 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100445 26,
Leon Clarkee46be812010-01-19 14:06:41 +0000446 "NativeRegExpMacroAssembler::word_character_map");
Steve Block6ded16b2010-05-10 14:33:55 +0100447#endif // V8_INTERPRETED_REGEXP
Leon Clarkee46be812010-01-19 14:06:41 +0000448 // Keyed lookup cache.
449 Add(ExternalReference::keyed_lookup_cache_keys().address(),
450 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100451 27,
Leon Clarkee46be812010-01-19 14:06:41 +0000452 "KeyedLookupCache::keys()");
453 Add(ExternalReference::keyed_lookup_cache_field_offsets().address(),
454 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100455 28,
Leon Clarkee46be812010-01-19 14:06:41 +0000456 "KeyedLookupCache::field_offsets()");
Andrei Popescu402d9372010-02-26 13:31:12 +0000457 Add(ExternalReference::transcendental_cache_array_address().address(),
458 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100459 29,
Andrei Popescu402d9372010-02-26 13:31:12 +0000460 "TranscendentalCache::caches()");
John Reck59135872010-11-02 12:39:01 -0700461 Add(ExternalReference::handle_scope_next_address().address(),
462 UNCLASSIFIED,
463 30,
464 "HandleScope::next");
465 Add(ExternalReference::handle_scope_limit_address().address(),
466 UNCLASSIFIED,
467 31,
468 "HandleScope::limit");
469 Add(ExternalReference::handle_scope_level_address().address(),
470 UNCLASSIFIED,
471 32,
472 "HandleScope::level");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100473 Add(ExternalReference::new_deoptimizer_function().address(),
474 UNCLASSIFIED,
475 33,
476 "Deoptimizer::New()");
477 Add(ExternalReference::compute_output_frames_function().address(),
478 UNCLASSIFIED,
479 34,
480 "Deoptimizer::ComputeOutputFrames()");
481 Add(ExternalReference::address_of_min_int().address(),
482 UNCLASSIFIED,
483 35,
484 "LDoubleConstant::min_int");
485 Add(ExternalReference::address_of_one_half().address(),
486 UNCLASSIFIED,
487 36,
488 "LDoubleConstant::one_half");
489 Add(ExternalReference::address_of_negative_infinity().address(),
490 UNCLASSIFIED,
491 37,
492 "LDoubleConstant::negative_infinity");
493 Add(ExternalReference::power_double_double_function().address(),
494 UNCLASSIFIED,
495 38,
496 "power_double_double_function");
497 Add(ExternalReference::power_double_int_function().address(),
498 UNCLASSIFIED,
499 39,
500 "power_double_int_function");
Steve Blocka7e24c12009-10-30 11:49:00 +0000501}
502
503
504ExternalReferenceEncoder::ExternalReferenceEncoder()
505 : encodings_(Match) {
506 ExternalReferenceTable* external_references =
507 ExternalReferenceTable::instance();
508 for (int i = 0; i < external_references->size(); ++i) {
509 Put(external_references->address(i), i);
510 }
511}
512
513
514uint32_t ExternalReferenceEncoder::Encode(Address key) const {
515 int index = IndexOf(key);
Ben Murdochbb769b22010-08-11 14:56:33 +0100516 ASSERT(key == NULL || index >= 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000517 return index >=0 ? ExternalReferenceTable::instance()->code(index) : 0;
518}
519
520
521const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
522 int index = IndexOf(key);
523 return index >=0 ? ExternalReferenceTable::instance()->name(index) : NULL;
524}
525
526
527int ExternalReferenceEncoder::IndexOf(Address key) const {
528 if (key == NULL) return -1;
529 HashMap::Entry* entry =
530 const_cast<HashMap &>(encodings_).Lookup(key, Hash(key), false);
531 return entry == NULL
532 ? -1
533 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
534}
535
536
537void ExternalReferenceEncoder::Put(Address key, int index) {
538 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
Steve Block6ded16b2010-05-10 14:33:55 +0100539 entry->value = reinterpret_cast<void*>(index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000540}
541
542
543ExternalReferenceDecoder::ExternalReferenceDecoder()
Ben Murdochf87a2032010-10-22 12:50:53 +0100544 : encodings_(NewArray<Address*>(kTypeCodeCount)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000545 ExternalReferenceTable* external_references =
546 ExternalReferenceTable::instance();
547 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
548 int max = external_references->max_id(type) + 1;
549 encodings_[type] = NewArray<Address>(max + 1);
550 }
551 for (int i = 0; i < external_references->size(); ++i) {
552 Put(external_references->code(i), external_references->address(i));
553 }
554}
555
556
557ExternalReferenceDecoder::~ExternalReferenceDecoder() {
558 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
559 DeleteArray(encodings_[type]);
560 }
561 DeleteArray(encodings_);
562}
563
564
Steve Blocka7e24c12009-10-30 11:49:00 +0000565bool Serializer::serialization_enabled_ = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000566bool Serializer::too_late_to_enable_now_ = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000567ExternalReferenceDecoder* Deserializer::external_reference_decoder_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000568
569
Leon Clarkee46be812010-01-19 14:06:41 +0000570Deserializer::Deserializer(SnapshotByteSource* source) : source_(source) {
Steve Blockd0582a62009-12-15 09:54:21 +0000571}
572
573
574// This routine both allocates a new object, and also keeps
575// track of where objects have been allocated so that we can
576// fix back references when deserializing.
577Address Deserializer::Allocate(int space_index, Space* space, int size) {
578 Address address;
579 if (!SpaceIsLarge(space_index)) {
580 ASSERT(!SpaceIsPaged(space_index) ||
581 size <= Page::kPageSize - Page::kObjectStartOffset);
John Reck59135872010-11-02 12:39:01 -0700582 MaybeObject* maybe_new_allocation;
Steve Blockd0582a62009-12-15 09:54:21 +0000583 if (space_index == NEW_SPACE) {
John Reck59135872010-11-02 12:39:01 -0700584 maybe_new_allocation =
585 reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
Steve Blockd0582a62009-12-15 09:54:21 +0000586 } else {
John Reck59135872010-11-02 12:39:01 -0700587 maybe_new_allocation =
588 reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
Steve Blockd0582a62009-12-15 09:54:21 +0000589 }
John Reck59135872010-11-02 12:39:01 -0700590 Object* new_allocation = maybe_new_allocation->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000591 HeapObject* new_object = HeapObject::cast(new_allocation);
Steve Blockd0582a62009-12-15 09:54:21 +0000592 address = new_object->address();
593 high_water_[space_index] = address + size;
594 } else {
595 ASSERT(SpaceIsLarge(space_index));
596 ASSERT(size > Page::kPageSize - Page::kObjectStartOffset);
597 LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
598 Object* new_allocation;
599 if (space_index == kLargeData) {
John Reck59135872010-11-02 12:39:01 -0700600 new_allocation = lo_space->AllocateRaw(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000601 } else if (space_index == kLargeFixedArray) {
John Reck59135872010-11-02 12:39:01 -0700602 new_allocation =
603 lo_space->AllocateRawFixedArray(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000604 } else {
605 ASSERT_EQ(kLargeCode, space_index);
John Reck59135872010-11-02 12:39:01 -0700606 new_allocation = lo_space->AllocateRawCode(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000607 }
Steve Blockd0582a62009-12-15 09:54:21 +0000608 HeapObject* new_object = HeapObject::cast(new_allocation);
609 // Record all large objects in the same space.
610 address = new_object->address();
Andrei Popescu31002712010-02-23 13:46:05 +0000611 pages_[LO_SPACE].Add(address);
Steve Blockd0582a62009-12-15 09:54:21 +0000612 }
613 last_object_address_ = address;
614 return address;
615}
616
617
618// This returns the address of an object that has been described in the
619// snapshot as being offset bytes back in a particular space.
620HeapObject* Deserializer::GetAddressFromEnd(int space) {
621 int offset = source_->GetInt();
622 ASSERT(!SpaceIsLarge(space));
623 offset <<= kObjectAlignmentBits;
624 return HeapObject::FromAddress(high_water_[space] - offset);
625}
626
627
628// This returns the address of an object that has been described in the
629// snapshot as being offset bytes into a particular space.
630HeapObject* Deserializer::GetAddressFromStart(int space) {
631 int offset = source_->GetInt();
632 if (SpaceIsLarge(space)) {
633 // Large spaces have one object per 'page'.
634 return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
635 }
636 offset <<= kObjectAlignmentBits;
637 if (space == NEW_SPACE) {
638 // New space has only one space - numbered 0.
639 return HeapObject::FromAddress(pages_[space][0] + offset);
640 }
641 ASSERT(SpaceIsPaged(space));
Leon Clarkee46be812010-01-19 14:06:41 +0000642 int page_of_pointee = offset >> kPageSizeBits;
Steve Blockd0582a62009-12-15 09:54:21 +0000643 Address object_address = pages_[space][page_of_pointee] +
644 (offset & Page::kPageAlignmentMask);
645 return HeapObject::FromAddress(object_address);
646}
647
648
649void Deserializer::Deserialize() {
650 // Don't GC while deserializing - just expand the heap.
651 AlwaysAllocateScope always_allocate;
652 // Don't use the free lists while deserializing.
653 LinearAllocationScope allocate_linearly;
654 // No active threads.
655 ASSERT_EQ(NULL, ThreadState::FirstInUse());
656 // No active handles.
657 ASSERT(HandleScopeImplementer::instance()->blocks()->is_empty());
Leon Clarked91b9f72010-01-27 17:25:45 +0000658 // Make sure the entire partial snapshot cache is traversed, filling it with
659 // valid object pointers.
660 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000661 ASSERT_EQ(NULL, external_reference_decoder_);
662 external_reference_decoder_ = new ExternalReferenceDecoder();
Leon Clarked91b9f72010-01-27 17:25:45 +0000663 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
664 Heap::IterateWeakRoots(this, VISIT_ALL);
Ben Murdochf87a2032010-10-22 12:50:53 +0100665
666 Heap::set_global_contexts_list(Heap::undefined_value());
Leon Clarkee46be812010-01-19 14:06:41 +0000667}
668
669
670void Deserializer::DeserializePartial(Object** root) {
671 // Don't GC while deserializing - just expand the heap.
672 AlwaysAllocateScope always_allocate;
673 // Don't use the free lists while deserializing.
674 LinearAllocationScope allocate_linearly;
675 if (external_reference_decoder_ == NULL) {
676 external_reference_decoder_ = new ExternalReferenceDecoder();
677 }
678 VisitPointer(root);
679}
680
681
Leon Clarked91b9f72010-01-27 17:25:45 +0000682Deserializer::~Deserializer() {
683 ASSERT(source_->AtEOF());
Leon Clarkee46be812010-01-19 14:06:41 +0000684 if (external_reference_decoder_ != NULL) {
685 delete external_reference_decoder_;
686 external_reference_decoder_ = NULL;
687 }
Steve Blockd0582a62009-12-15 09:54:21 +0000688}
689
690
691// This is called on the roots. It is the driver of the deserialization
692// process. It is also called on the body of each function.
693void Deserializer::VisitPointers(Object** start, Object** end) {
694 // The space must be new space. Any other space would cause ReadChunk to try
695 // to update the remembered using NULL as the address.
696 ReadChunk(start, end, NEW_SPACE, NULL);
697}
698
699
700// This routine writes the new object into the pointer provided and then
701// returns true if the new object was in young space and false otherwise.
702// The reason for this strange interface is that otherwise the object is
703// written very late, which means the ByteArray map is not set up by the
704// time we need to use it to mark the space at the end of a page free (by
705// making it into a byte array).
706void Deserializer::ReadObject(int space_number,
707 Space* space,
708 Object** write_back) {
709 int size = source_->GetInt() << kObjectAlignmentBits;
710 Address address = Allocate(space_number, space, size);
711 *write_back = HeapObject::FromAddress(address);
712 Object** current = reinterpret_cast<Object**>(address);
713 Object** limit = current + (size >> kPointerSizeLog2);
Leon Clarkee46be812010-01-19 14:06:41 +0000714 if (FLAG_log_snapshot_positions) {
715 LOG(SnapshotPositionEvent(address, source_->position()));
716 }
Steve Blockd0582a62009-12-15 09:54:21 +0000717 ReadChunk(current, limit, space_number, address);
718}
719
720
Leon Clarkef7060e22010-06-03 12:02:55 +0100721// This macro is always used with a constant argument so it should all fold
722// away to almost nothing in the generated code. It might be nicer to do this
723// with the ternary operator but there are type issues with that.
724#define ASSIGN_DEST_SPACE(space_number) \
725 Space* dest_space; \
726 if (space_number == NEW_SPACE) { \
727 dest_space = Heap::new_space(); \
728 } else if (space_number == OLD_POINTER_SPACE) { \
729 dest_space = Heap::old_pointer_space(); \
730 } else if (space_number == OLD_DATA_SPACE) { \
731 dest_space = Heap::old_data_space(); \
732 } else if (space_number == CODE_SPACE) { \
733 dest_space = Heap::code_space(); \
734 } else if (space_number == MAP_SPACE) { \
735 dest_space = Heap::map_space(); \
736 } else if (space_number == CELL_SPACE) { \
737 dest_space = Heap::cell_space(); \
738 } else { \
739 ASSERT(space_number >= LO_SPACE); \
740 dest_space = Heap::lo_space(); \
741 }
742
743
744static const int kUnknownOffsetFromStart = -1;
Steve Blockd0582a62009-12-15 09:54:21 +0000745
746
747void Deserializer::ReadChunk(Object** current,
748 Object** limit,
Leon Clarkef7060e22010-06-03 12:02:55 +0100749 int source_space,
Steve Blockd0582a62009-12-15 09:54:21 +0000750 Address address) {
751 while (current < limit) {
752 int data = source_->Get();
753 switch (data) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100754#define CASE_STATEMENT(where, how, within, space_number) \
755 case where + how + within + space_number: \
756 ASSERT((where & ~kPointedToMask) == 0); \
757 ASSERT((how & ~kHowToCodeMask) == 0); \
758 ASSERT((within & ~kWhereToPointMask) == 0); \
759 ASSERT((space_number & ~kSpaceMask) == 0);
760
761#define CASE_BODY(where, how, within, space_number_if_any, offset_from_start) \
762 { \
763 bool emit_write_barrier = false; \
764 bool current_was_incremented = false; \
765 int space_number = space_number_if_any == kAnyOldSpace ? \
766 (data & kSpaceMask) : space_number_if_any; \
767 if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
768 ASSIGN_DEST_SPACE(space_number) \
769 ReadObject(space_number, dest_space, current); \
770 emit_write_barrier = \
771 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
772 } else { \
773 Object* new_object = NULL; /* May not be a real Object pointer. */ \
774 if (where == kNewObject) { \
775 ASSIGN_DEST_SPACE(space_number) \
776 ReadObject(space_number, dest_space, &new_object); \
777 } else if (where == kRootArray) { \
778 int root_id = source_->GetInt(); \
779 new_object = Heap::roots_address()[root_id]; \
780 } else if (where == kPartialSnapshotCache) { \
781 int cache_index = source_->GetInt(); \
782 new_object = partial_snapshot_cache_[cache_index]; \
783 } else if (where == kExternalReference) { \
784 int reference_id = source_->GetInt(); \
785 Address address = \
786 external_reference_decoder_->Decode(reference_id); \
787 new_object = reinterpret_cast<Object*>(address); \
788 } else if (where == kBackref) { \
789 emit_write_barrier = \
790 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
791 new_object = GetAddressFromEnd(data & kSpaceMask); \
792 } else { \
793 ASSERT(where == kFromStart); \
794 if (offset_from_start == kUnknownOffsetFromStart) { \
795 emit_write_barrier = \
796 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
797 new_object = GetAddressFromStart(data & kSpaceMask); \
798 } else { \
799 Address object_address = pages_[space_number][0] + \
800 (offset_from_start << kObjectAlignmentBits); \
801 new_object = HeapObject::FromAddress(object_address); \
802 } \
803 } \
804 if (within == kFirstInstruction) { \
805 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
806 new_object = reinterpret_cast<Object*>( \
807 new_code_object->instruction_start()); \
808 } \
809 if (how == kFromCode) { \
810 Address location_of_branch_data = \
811 reinterpret_cast<Address>(current); \
812 Assembler::set_target_at(location_of_branch_data, \
813 reinterpret_cast<Address>(new_object)); \
814 if (within == kFirstInstruction) { \
815 location_of_branch_data += Assembler::kCallTargetSize; \
816 current = reinterpret_cast<Object**>(location_of_branch_data); \
817 current_was_incremented = true; \
818 } \
819 } else { \
820 *current = new_object; \
821 } \
822 } \
823 if (emit_write_barrier) { \
824 Heap::RecordWrite(address, static_cast<int>( \
825 reinterpret_cast<Address>(current) - address)); \
826 } \
827 if (!current_was_incremented) { \
828 current++; /* Increment current if it wasn't done above. */ \
829 } \
830 break; \
831 } \
832
833// This generates a case and a body for each space. The large object spaces are
834// very rare in snapshots so they are grouped in one body.
835#define ONE_PER_SPACE(where, how, within) \
836 CASE_STATEMENT(where, how, within, NEW_SPACE) \
837 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
838 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
839 CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart) \
840 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
841 CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart) \
842 CASE_STATEMENT(where, how, within, CODE_SPACE) \
843 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
844 CASE_STATEMENT(where, how, within, CELL_SPACE) \
845 CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart) \
846 CASE_STATEMENT(where, how, within, MAP_SPACE) \
847 CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart) \
848 CASE_STATEMENT(where, how, within, kLargeData) \
849 CASE_STATEMENT(where, how, within, kLargeCode) \
850 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
851 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
852
853// This generates a case and a body for the new space (which has to do extra
854// write barrier handling) and handles the other spaces with 8 fall-through
855// cases and one body.
856#define ALL_SPACES(where, how, within) \
857 CASE_STATEMENT(where, how, within, NEW_SPACE) \
858 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
859 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
860 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
861 CASE_STATEMENT(where, how, within, CODE_SPACE) \
862 CASE_STATEMENT(where, how, within, CELL_SPACE) \
863 CASE_STATEMENT(where, how, within, MAP_SPACE) \
864 CASE_STATEMENT(where, how, within, kLargeData) \
865 CASE_STATEMENT(where, how, within, kLargeCode) \
866 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
867 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
868
Steve Block791712a2010-08-27 10:21:07 +0100869#define ONE_PER_CODE_SPACE(where, how, within) \
870 CASE_STATEMENT(where, how, within, CODE_SPACE) \
871 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
872 CASE_STATEMENT(where, how, within, kLargeCode) \
873 CASE_BODY(where, how, within, LO_SPACE, kUnknownOffsetFromStart)
874
Leon Clarkef7060e22010-06-03 12:02:55 +0100875#define EMIT_COMMON_REFERENCE_PATTERNS(pseudo_space_number, \
876 space_number, \
877 offset_from_start) \
878 CASE_STATEMENT(kFromStart, kPlain, kStartOfObject, pseudo_space_number) \
879 CASE_BODY(kFromStart, kPlain, kStartOfObject, space_number, offset_from_start)
880
881 // We generate 15 cases and bodies that process special tags that combine
882 // the raw data tag and the length into one byte.
Steve Blockd0582a62009-12-15 09:54:21 +0000883#define RAW_CASE(index, size) \
Leon Clarkef7060e22010-06-03 12:02:55 +0100884 case kRawData + index: { \
Steve Blockd0582a62009-12-15 09:54:21 +0000885 byte* raw_data_out = reinterpret_cast<byte*>(current); \
886 source_->CopyRaw(raw_data_out, size); \
887 current = reinterpret_cast<Object**>(raw_data_out + size); \
888 break; \
889 }
890 COMMON_RAW_LENGTHS(RAW_CASE)
891#undef RAW_CASE
Leon Clarkef7060e22010-06-03 12:02:55 +0100892
893 // Deserialize a chunk of raw data that doesn't have one of the popular
894 // lengths.
895 case kRawData: {
Steve Blockd0582a62009-12-15 09:54:21 +0000896 int size = source_->GetInt();
897 byte* raw_data_out = reinterpret_cast<byte*>(current);
898 source_->CopyRaw(raw_data_out, size);
899 current = reinterpret_cast<Object**>(raw_data_out + size);
900 break;
901 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100902
903 // Deserialize a new object and write a pointer to it to the current
904 // object.
905 ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100906 // Support for direct instruction pointers in functions
907 ONE_PER_CODE_SPACE(kNewObject, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100908 // Deserialize a new code object and write a pointer to its first
909 // instruction to the current code object.
910 ONE_PER_SPACE(kNewObject, kFromCode, kFirstInstruction)
911 // Find a recently deserialized object using its offset from the current
912 // allocation point and write a pointer to it to the current object.
913 ALL_SPACES(kBackref, kPlain, kStartOfObject)
914 // Find a recently deserialized code object using its offset from the
915 // current allocation point and write a pointer to its first instruction
Steve Block791712a2010-08-27 10:21:07 +0100916 // to the current code object or the instruction pointer in a function
917 // object.
Leon Clarkef7060e22010-06-03 12:02:55 +0100918 ALL_SPACES(kBackref, kFromCode, kFirstInstruction)
Steve Block791712a2010-08-27 10:21:07 +0100919 ALL_SPACES(kBackref, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100920 // Find an already deserialized object using its offset from the start
921 // and write a pointer to it to the current object.
922 ALL_SPACES(kFromStart, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100923 ALL_SPACES(kFromStart, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100924 // Find an already deserialized code object using its offset from the
925 // start and write a pointer to its first instruction to the current code
926 // object.
927 ALL_SPACES(kFromStart, kFromCode, kFirstInstruction)
928 // Find an already deserialized object at one of the predetermined popular
929 // offsets from the start and write a pointer to it in the current object.
930 COMMON_REFERENCE_PATTERNS(EMIT_COMMON_REFERENCE_PATTERNS)
931 // Find an object in the roots array and write a pointer to it to the
932 // current object.
933 CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
934 CASE_BODY(kRootArray, kPlain, kStartOfObject, 0, kUnknownOffsetFromStart)
935 // Find an object in the partial snapshots cache and write a pointer to it
936 // to the current object.
937 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
938 CASE_BODY(kPartialSnapshotCache,
939 kPlain,
940 kStartOfObject,
941 0,
942 kUnknownOffsetFromStart)
Steve Block791712a2010-08-27 10:21:07 +0100943 // Find an code entry in the partial snapshots cache and
944 // write a pointer to it to the current object.
945 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kFirstInstruction, 0)
946 CASE_BODY(kPartialSnapshotCache,
947 kPlain,
948 kFirstInstruction,
949 0,
950 kUnknownOffsetFromStart)
Leon Clarkef7060e22010-06-03 12:02:55 +0100951 // Find an external reference and write a pointer to it to the current
952 // object.
953 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
954 CASE_BODY(kExternalReference,
955 kPlain,
956 kStartOfObject,
957 0,
958 kUnknownOffsetFromStart)
959 // Find an external reference and write a pointer to it in the current
960 // code object.
961 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
962 CASE_BODY(kExternalReference,
963 kFromCode,
964 kStartOfObject,
965 0,
966 kUnknownOffsetFromStart)
967
968#undef CASE_STATEMENT
969#undef CASE_BODY
970#undef ONE_PER_SPACE
971#undef ALL_SPACES
972#undef EMIT_COMMON_REFERENCE_PATTERNS
973#undef ASSIGN_DEST_SPACE
974
975 case kNewPage: {
Steve Blockd0582a62009-12-15 09:54:21 +0000976 int space = source_->Get();
977 pages_[space].Add(last_object_address_);
Steve Block6ded16b2010-05-10 14:33:55 +0100978 if (space == CODE_SPACE) {
979 CPU::FlushICache(last_object_address_, Page::kPageSize);
980 }
Steve Blockd0582a62009-12-15 09:54:21 +0000981 break;
982 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100983
984 case kNativesStringResource: {
Steve Blockd0582a62009-12-15 09:54:21 +0000985 int index = source_->Get();
986 Vector<const char> source_vector = Natives::GetScriptSource(index);
987 NativesExternalStringResource* resource =
988 new NativesExternalStringResource(source_vector.start());
989 *current++ = reinterpret_cast<Object*>(resource);
990 break;
991 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100992
993 case kSynchronize: {
Leon Clarked91b9f72010-01-27 17:25:45 +0000994 // If we get here then that indicates that you have a mismatch between
995 // the number of GC roots when serializing and deserializing.
996 UNREACHABLE();
997 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100998
Steve Blockd0582a62009-12-15 09:54:21 +0000999 default:
1000 UNREACHABLE();
1001 }
1002 }
1003 ASSERT_EQ(current, limit);
1004}
1005
1006
1007void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
1008 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
1009 for (int shift = max_shift; shift > 0; shift -= 7) {
1010 if (integer >= static_cast<uintptr_t>(1u) << shift) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001011 Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart");
Steve Blockd0582a62009-12-15 09:54:21 +00001012 }
1013 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001014 PutSection(static_cast<int>(integer & 0x7f), "IntLastPart");
Steve Blockd0582a62009-12-15 09:54:21 +00001015}
1016
Steve Blocka7e24c12009-10-30 11:49:00 +00001017#ifdef DEBUG
Steve Blockd0582a62009-12-15 09:54:21 +00001018
1019void Deserializer::Synchronize(const char* tag) {
1020 int data = source_->Get();
1021 // If this assert fails then that indicates that you have a mismatch between
1022 // the number of GC roots when serializing and deserializing.
Leon Clarkef7060e22010-06-03 12:02:55 +01001023 ASSERT_EQ(kSynchronize, data);
Steve Blockd0582a62009-12-15 09:54:21 +00001024 do {
1025 int character = source_->Get();
1026 if (character == 0) break;
1027 if (FLAG_debug_serialization) {
1028 PrintF("%c", character);
1029 }
1030 } while (true);
1031 if (FLAG_debug_serialization) {
1032 PrintF("\n");
1033 }
1034}
1035
Steve Blocka7e24c12009-10-30 11:49:00 +00001036
1037void Serializer::Synchronize(const char* tag) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001038 sink_->Put(kSynchronize, tag);
Steve Blockd0582a62009-12-15 09:54:21 +00001039 int character;
1040 do {
1041 character = *tag++;
1042 sink_->PutSection(character, "TagCharacter");
1043 } while (character != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001044}
Steve Blockd0582a62009-12-15 09:54:21 +00001045
Steve Blocka7e24c12009-10-30 11:49:00 +00001046#endif
1047
Steve Blockd0582a62009-12-15 09:54:21 +00001048Serializer::Serializer(SnapshotByteSink* sink)
1049 : sink_(sink),
1050 current_root_index_(0),
Andrei Popescu31002712010-02-23 13:46:05 +00001051 external_reference_encoder_(new ExternalReferenceEncoder),
Leon Clarkee46be812010-01-19 14:06:41 +00001052 large_object_total_(0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001053 for (int i = 0; i <= LAST_SPACE; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +00001054 fullness_[i] = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001055 }
1056}
1057
1058
Andrei Popescu31002712010-02-23 13:46:05 +00001059Serializer::~Serializer() {
1060 delete external_reference_encoder_;
1061}
1062
1063
Leon Clarked91b9f72010-01-27 17:25:45 +00001064void StartupSerializer::SerializeStrongReferences() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001065 // No active threads.
1066 CHECK_EQ(NULL, ThreadState::FirstInUse());
1067 // No active or weak handles.
1068 CHECK(HandleScopeImplementer::instance()->blocks()->is_empty());
1069 CHECK_EQ(0, GlobalHandles::NumberOfWeakHandles());
Steve Blockd0582a62009-12-15 09:54:21 +00001070 // We don't support serializing installed extensions.
1071 for (RegisteredExtension* ext = RegisteredExtension::first_extension();
1072 ext != NULL;
1073 ext = ext->next()) {
1074 CHECK_NE(v8::INSTALLED, ext->state());
1075 }
Leon Clarked91b9f72010-01-27 17:25:45 +00001076 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
Steve Blocka7e24c12009-10-30 11:49:00 +00001077}
1078
1079
Leon Clarked91b9f72010-01-27 17:25:45 +00001080void PartialSerializer::Serialize(Object** object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001081 this->VisitPointer(object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001082
1083 // After we have done the partial serialization the partial snapshot cache
1084 // will contain some references needed to decode the partial snapshot. We
1085 // fill it up with undefineds so it has a predictable length so the
1086 // deserialization code doesn't need to know the length.
1087 for (int index = partial_snapshot_cache_length_;
1088 index < kPartialSnapshotCacheCapacity;
1089 index++) {
1090 partial_snapshot_cache_[index] = Heap::undefined_value();
1091 startup_serializer_->VisitPointer(&partial_snapshot_cache_[index]);
1092 }
1093 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Leon Clarkee46be812010-01-19 14:06:41 +00001094}
1095
1096
Steve Blocka7e24c12009-10-30 11:49:00 +00001097void Serializer::VisitPointers(Object** start, Object** end) {
Steve Blockd0582a62009-12-15 09:54:21 +00001098 for (Object** current = start; current < end; current++) {
1099 if ((*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001100 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001101 sink_->PutInt(kPointerSize, "length");
1102 for (int i = 0; i < kPointerSize; i++) {
1103 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1104 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001105 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001106 SerializeObject(*current, kPlain, kStartOfObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001107 }
1108 }
1109}
1110
1111
Leon Clarked91b9f72010-01-27 17:25:45 +00001112Object* SerializerDeserializer::partial_snapshot_cache_[
1113 kPartialSnapshotCacheCapacity];
1114int SerializerDeserializer::partial_snapshot_cache_length_ = 0;
1115
1116
1117// This ensures that the partial snapshot cache keeps things alive during GC and
1118// tracks their movement. When it is called during serialization of the startup
1119// snapshot the partial snapshot is empty, so nothing happens. When the partial
1120// (context) snapshot is created, this array is populated with the pointers that
1121// the partial snapshot will need. As that happens we emit serialized objects to
1122// the startup snapshot that correspond to the elements of this cache array. On
1123// deserialization we therefore need to visit the cache array. This fills it up
1124// with pointers to deserialized objects.
Steve Block6ded16b2010-05-10 14:33:55 +01001125void SerializerDeserializer::Iterate(ObjectVisitor* visitor) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001126 visitor->VisitPointers(
1127 &partial_snapshot_cache_[0],
1128 &partial_snapshot_cache_[partial_snapshot_cache_length_]);
1129}
1130
1131
1132// When deserializing we need to set the size of the snapshot cache. This means
1133// the root iteration code (above) will iterate over array elements, writing the
1134// references to deserialized objects in them.
1135void SerializerDeserializer::SetSnapshotCacheSize(int size) {
1136 partial_snapshot_cache_length_ = size;
1137}
1138
1139
1140int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1141 for (int i = 0; i < partial_snapshot_cache_length_; i++) {
1142 Object* entry = partial_snapshot_cache_[i];
1143 if (entry == heap_object) return i;
1144 }
Andrei Popescu31002712010-02-23 13:46:05 +00001145
Leon Clarked91b9f72010-01-27 17:25:45 +00001146 // We didn't find the object in the cache. So we add it to the cache and
1147 // then visit the pointer so that it becomes part of the startup snapshot
1148 // and we can refer to it from the partial snapshot.
1149 int length = partial_snapshot_cache_length_;
1150 CHECK(length < kPartialSnapshotCacheCapacity);
1151 partial_snapshot_cache_[length] = heap_object;
1152 startup_serializer_->VisitPointer(&partial_snapshot_cache_[length]);
1153 // We don't recurse from the startup snapshot generator into the partial
1154 // snapshot generator.
1155 ASSERT(length == partial_snapshot_cache_length_);
1156 return partial_snapshot_cache_length_++;
1157}
1158
1159
1160int PartialSerializer::RootIndex(HeapObject* heap_object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001161 for (int i = 0; i < Heap::kRootListLength; i++) {
1162 Object* root = Heap::roots_address()[i];
1163 if (root == heap_object) return i;
1164 }
1165 return kInvalidRootIndex;
1166}
1167
1168
Leon Clarked91b9f72010-01-27 17:25:45 +00001169// Encode the location of an already deserialized object in order to write its
1170// location into a later object. We can encode the location as an offset from
1171// the start of the deserialized objects or as an offset backwards from the
1172// current allocation pointer.
1173void Serializer::SerializeReferenceToPreviousObject(
1174 int space,
1175 int address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001176 HowToCode how_to_code,
1177 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001178 int offset = CurrentAllocationAddress(space) - address;
1179 bool from_start = true;
1180 if (SpaceIsPaged(space)) {
1181 // For paged space it is simple to encode back from current allocation if
1182 // the object is on the same page as the current allocation pointer.
1183 if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1184 (address >> kPageSizeBits)) {
1185 from_start = false;
1186 address = offset;
1187 }
1188 } else if (space == NEW_SPACE) {
1189 // For new space it is always simple to encode back from current allocation.
1190 if (offset < address) {
1191 from_start = false;
1192 address = offset;
1193 }
1194 }
1195 // If we are actually dealing with real offsets (and not a numbering of
1196 // all objects) then we should shift out the bits that are always 0.
1197 if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
Leon Clarkef7060e22010-06-03 12:02:55 +01001198 if (from_start) {
1199#define COMMON_REFS_CASE(pseudo_space, actual_space, offset) \
1200 if (space == actual_space && address == offset && \
1201 how_to_code == kPlain && where_to_point == kStartOfObject) { \
1202 sink_->Put(kFromStart + how_to_code + where_to_point + \
1203 pseudo_space, "RefSer"); \
1204 } else /* NOLINT */
1205 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
1206#undef COMMON_REFS_CASE
1207 { /* NOLINT */
1208 sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer");
Leon Clarked91b9f72010-01-27 17:25:45 +00001209 sink_->PutInt(address, "address");
1210 }
1211 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001212 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1213 sink_->PutInt(address, "address");
Leon Clarked91b9f72010-01-27 17:25:45 +00001214 }
1215}
1216
1217
1218void StartupSerializer::SerializeObject(
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001219 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001220 HowToCode how_to_code,
1221 WhereToPoint where_to_point) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001222 CHECK(o->IsHeapObject());
1223 HeapObject* heap_object = HeapObject::cast(o);
Leon Clarked91b9f72010-01-27 17:25:45 +00001224
1225 if (address_mapper_.IsMapped(heap_object)) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001226 int space = SpaceOfAlreadySerializedObject(heap_object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001227 int address = address_mapper_.MappedTo(heap_object);
1228 SerializeReferenceToPreviousObject(space,
1229 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001230 how_to_code,
1231 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001232 } else {
1233 // Object has not yet been serialized. Serialize it here.
1234 ObjectSerializer object_serializer(this,
1235 heap_object,
1236 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001237 how_to_code,
1238 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001239 object_serializer.Serialize();
1240 }
1241}
1242
1243
1244void StartupSerializer::SerializeWeakReferences() {
1245 for (int i = partial_snapshot_cache_length_;
1246 i < kPartialSnapshotCacheCapacity;
1247 i++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001248 sink_->Put(kRootArray + kPlain + kStartOfObject, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001249 sink_->PutInt(Heap::kUndefinedValueRootIndex, "root_index");
1250 }
1251 Heap::IterateWeakRoots(this, VISIT_ALL);
1252}
1253
1254
1255void PartialSerializer::SerializeObject(
1256 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001257 HowToCode how_to_code,
1258 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001259 CHECK(o->IsHeapObject());
1260 HeapObject* heap_object = HeapObject::cast(o);
1261
1262 int root_index;
1263 if ((root_index = RootIndex(heap_object)) != kInvalidRootIndex) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001264 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001265 sink_->PutInt(root_index, "root_index");
1266 return;
1267 }
1268
1269 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1270 int cache_index = PartialSnapshotCacheIndex(heap_object);
Leon Clarkef7060e22010-06-03 12:02:55 +01001271 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1272 "PartialSnapshotCache");
Leon Clarked91b9f72010-01-27 17:25:45 +00001273 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1274 return;
1275 }
1276
1277 // Pointers from the partial snapshot to the objects in the startup snapshot
1278 // should go through the root array or through the partial snapshot cache.
1279 // If this is not the case you may have to add something to the root array.
1280 ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1281 // All the symbols that the partial snapshot needs should be either in the
1282 // root table or in the partial snapshot cache.
1283 ASSERT(!heap_object->IsSymbol());
1284
1285 if (address_mapper_.IsMapped(heap_object)) {
1286 int space = SpaceOfAlreadySerializedObject(heap_object);
1287 int address = address_mapper_.MappedTo(heap_object);
1288 SerializeReferenceToPreviousObject(space,
1289 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001290 how_to_code,
1291 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001292 } else {
1293 // Object has not yet been serialized. Serialize it here.
1294 ObjectSerializer serializer(this,
1295 heap_object,
1296 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001297 how_to_code,
1298 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001299 serializer.Serialize();
1300 }
1301}
1302
1303
Steve Blockd0582a62009-12-15 09:54:21 +00001304void Serializer::ObjectSerializer::Serialize() {
1305 int space = Serializer::SpaceOfObject(object_);
1306 int size = object_->Size();
1307
Leon Clarkef7060e22010-06-03 12:02:55 +01001308 sink_->Put(kNewObject + reference_representation_ + space,
1309 "ObjectSerialization");
Steve Blockd0582a62009-12-15 09:54:21 +00001310 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1311
Leon Clarkee46be812010-01-19 14:06:41 +00001312 LOG(SnapshotPositionEvent(object_->address(), sink_->Position()));
1313
Steve Blockd0582a62009-12-15 09:54:21 +00001314 // Mark this object as already serialized.
1315 bool start_new_page;
Leon Clarked91b9f72010-01-27 17:25:45 +00001316 int offset = serializer_->Allocate(space, size, &start_new_page);
1317 serializer_->address_mapper()->AddMapping(object_, offset);
Steve Blockd0582a62009-12-15 09:54:21 +00001318 if (start_new_page) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001319 sink_->Put(kNewPage, "NewPage");
Steve Blockd0582a62009-12-15 09:54:21 +00001320 sink_->PutSection(space, "NewPageSpace");
1321 }
1322
1323 // Serialize the map (first word of the object).
Leon Clarkef7060e22010-06-03 12:02:55 +01001324 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001325
1326 // Serialize the rest of the object.
1327 CHECK_EQ(0, bytes_processed_so_far_);
1328 bytes_processed_so_far_ = kPointerSize;
1329 object_->IterateBody(object_->map()->instance_type(), size, this);
1330 OutputRawData(object_->address() + size);
1331}
1332
1333
1334void Serializer::ObjectSerializer::VisitPointers(Object** start,
1335 Object** end) {
1336 Object** current = start;
1337 while (current < end) {
1338 while (current < end && (*current)->IsSmi()) current++;
1339 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1340
1341 while (current < end && !(*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001342 serializer_->SerializeObject(*current, kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001343 bytes_processed_so_far_ += kPointerSize;
1344 current++;
1345 }
1346 }
1347}
1348
1349
1350void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1351 Address* end) {
1352 Address references_start = reinterpret_cast<Address>(start);
1353 OutputRawData(references_start);
1354
1355 for (Address* current = start; current < end; current++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001356 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
Steve Blockd0582a62009-12-15 09:54:21 +00001357 int reference_id = serializer_->EncodeExternalReference(*current);
1358 sink_->PutInt(reference_id, "reference id");
1359 }
1360 bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1361}
1362
1363
1364void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1365 Address target_start = rinfo->target_address_address();
1366 OutputRawData(target_start);
1367 Address target = rinfo->target_address();
1368 uint32_t encoding = serializer_->EncodeExternalReference(target);
1369 CHECK(target == NULL ? encoding == 0 : encoding != 0);
Leon Clarkef7060e22010-06-03 12:02:55 +01001370 int representation;
1371 // Can't use a ternary operator because of gcc.
1372 if (rinfo->IsCodedSpecially()) {
1373 representation = kStartOfObject + kFromCode;
1374 } else {
1375 representation = kStartOfObject + kPlain;
1376 }
1377 sink_->Put(kExternalReference + representation, "ExternalReference");
Steve Blockd0582a62009-12-15 09:54:21 +00001378 sink_->PutInt(encoding, "reference id");
Leon Clarkef7060e22010-06-03 12:02:55 +01001379 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001380}
1381
1382
1383void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1384 CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1385 Address target_start = rinfo->target_address_address();
1386 OutputRawData(target_start);
1387 Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
Leon Clarkef7060e22010-06-03 12:02:55 +01001388 serializer_->SerializeObject(target, kFromCode, kFirstInstruction);
1389 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001390}
1391
1392
Steve Block791712a2010-08-27 10:21:07 +01001393void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1394 Code* target = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1395 OutputRawData(entry_address);
1396 serializer_->SerializeObject(target, kPlain, kFirstInstruction);
1397 bytes_processed_so_far_ += kPointerSize;
1398}
1399
1400
Ben Murdochb0fe1622011-05-05 13:52:32 +01001401void Serializer::ObjectSerializer::VisitGlobalPropertyCell(RelocInfo* rinfo) {
1402 // We shouldn't have any global property cell references in code
1403 // objects in the snapshot.
1404 UNREACHABLE();
1405}
1406
1407
Steve Blockd0582a62009-12-15 09:54:21 +00001408void Serializer::ObjectSerializer::VisitExternalAsciiString(
1409 v8::String::ExternalAsciiStringResource** resource_pointer) {
1410 Address references_start = reinterpret_cast<Address>(resource_pointer);
1411 OutputRawData(references_start);
1412 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1413 Object* source = Heap::natives_source_cache()->get(i);
1414 if (!source->IsUndefined()) {
1415 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1416 typedef v8::String::ExternalAsciiStringResource Resource;
1417 Resource* resource = string->resource();
1418 if (resource == *resource_pointer) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001419 sink_->Put(kNativesStringResource, "NativesStringResource");
Steve Blockd0582a62009-12-15 09:54:21 +00001420 sink_->PutSection(i, "NativesStringResourceEnd");
1421 bytes_processed_so_far_ += sizeof(resource);
1422 return;
1423 }
1424 }
1425 }
1426 // One of the strings in the natives cache should match the resource. We
1427 // can't serialize any other kinds of external strings.
1428 UNREACHABLE();
1429}
1430
1431
1432void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1433 Address object_start = object_->address();
1434 int up_to_offset = static_cast<int>(up_to - object_start);
1435 int skipped = up_to_offset - bytes_processed_so_far_;
1436 // This assert will fail if the reloc info gives us the target_address_address
1437 // locations in a non-ascending order. Luckily that doesn't happen.
1438 ASSERT(skipped >= 0);
1439 if (skipped != 0) {
1440 Address base = object_start + bytes_processed_so_far_;
1441#define RAW_CASE(index, length) \
1442 if (skipped == length) { \
Leon Clarkef7060e22010-06-03 12:02:55 +01001443 sink_->PutSection(kRawData + index, "RawDataFixed"); \
Steve Blockd0582a62009-12-15 09:54:21 +00001444 } else /* NOLINT */
1445 COMMON_RAW_LENGTHS(RAW_CASE)
1446#undef RAW_CASE
1447 { /* NOLINT */
Leon Clarkef7060e22010-06-03 12:02:55 +01001448 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001449 sink_->PutInt(skipped, "length");
1450 }
1451 for (int i = 0; i < skipped; i++) {
1452 unsigned int data = base[i];
1453 sink_->PutSection(data, "Byte");
1454 }
1455 bytes_processed_so_far_ += skipped;
1456 }
1457}
1458
1459
1460int Serializer::SpaceOfObject(HeapObject* object) {
1461 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1462 AllocationSpace s = static_cast<AllocationSpace>(i);
1463 if (Heap::InSpace(object, s)) {
1464 if (i == LO_SPACE) {
1465 if (object->IsCode()) {
1466 return kLargeCode;
1467 } else if (object->IsFixedArray()) {
1468 return kLargeFixedArray;
1469 } else {
1470 return kLargeData;
1471 }
1472 }
1473 return i;
1474 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001475 }
1476 UNREACHABLE();
Steve Blockd0582a62009-12-15 09:54:21 +00001477 return 0;
1478}
1479
1480
1481int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1482 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1483 AllocationSpace s = static_cast<AllocationSpace>(i);
1484 if (Heap::InSpace(object, s)) {
1485 return i;
1486 }
1487 }
1488 UNREACHABLE();
1489 return 0;
1490}
1491
1492
1493int Serializer::Allocate(int space, int size, bool* new_page) {
1494 CHECK(space >= 0 && space < kNumberOfSpaces);
1495 if (SpaceIsLarge(space)) {
1496 // In large object space we merely number the objects instead of trying to
1497 // determine some sort of address.
1498 *new_page = true;
Leon Clarkee46be812010-01-19 14:06:41 +00001499 large_object_total_ += size;
Steve Blockd0582a62009-12-15 09:54:21 +00001500 return fullness_[LO_SPACE]++;
1501 }
1502 *new_page = false;
1503 if (fullness_[space] == 0) {
1504 *new_page = true;
1505 }
1506 if (SpaceIsPaged(space)) {
1507 // Paged spaces are a little special. We encode their addresses as if the
1508 // pages were all contiguous and each page were filled up in the range
1509 // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1510 // and allocation does not start at offset 0 in the page, but this scheme
1511 // means the deserializer can get the page number quickly by shifting the
1512 // serialized address.
1513 CHECK(IsPowerOf2(Page::kPageSize));
1514 int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1515 CHECK(size <= Page::kObjectAreaSize);
1516 if (used_in_this_page + size > Page::kObjectAreaSize) {
1517 *new_page = true;
1518 fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1519 }
1520 }
1521 int allocation_address = fullness_[space];
1522 fullness_[space] = allocation_address + size;
1523 return allocation_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001524}
1525
1526
1527} } // namespace v8::internal