blob: 19e65185645ba207f3a851ab2e3703e57614bc46 [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");
Ben Murdoch086aeea2011-05-13 15:57:08 +0100501 Add(ExternalReference::arguments_marker_location().address(),
502 UNCLASSIFIED,
503 40,
504 "Factory::arguments_marker().location()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000505}
506
507
508ExternalReferenceEncoder::ExternalReferenceEncoder()
509 : encodings_(Match) {
510 ExternalReferenceTable* external_references =
511 ExternalReferenceTable::instance();
512 for (int i = 0; i < external_references->size(); ++i) {
513 Put(external_references->address(i), i);
514 }
515}
516
517
518uint32_t ExternalReferenceEncoder::Encode(Address key) const {
519 int index = IndexOf(key);
Ben Murdochbb769b22010-08-11 14:56:33 +0100520 ASSERT(key == NULL || index >= 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000521 return index >=0 ? ExternalReferenceTable::instance()->code(index) : 0;
522}
523
524
525const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
526 int index = IndexOf(key);
527 return index >=0 ? ExternalReferenceTable::instance()->name(index) : NULL;
528}
529
530
531int ExternalReferenceEncoder::IndexOf(Address key) const {
532 if (key == NULL) return -1;
533 HashMap::Entry* entry =
534 const_cast<HashMap &>(encodings_).Lookup(key, Hash(key), false);
535 return entry == NULL
536 ? -1
537 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
538}
539
540
541void ExternalReferenceEncoder::Put(Address key, int index) {
542 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
Steve Block6ded16b2010-05-10 14:33:55 +0100543 entry->value = reinterpret_cast<void*>(index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000544}
545
546
547ExternalReferenceDecoder::ExternalReferenceDecoder()
Ben Murdochf87a2032010-10-22 12:50:53 +0100548 : encodings_(NewArray<Address*>(kTypeCodeCount)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000549 ExternalReferenceTable* external_references =
550 ExternalReferenceTable::instance();
551 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
552 int max = external_references->max_id(type) + 1;
553 encodings_[type] = NewArray<Address>(max + 1);
554 }
555 for (int i = 0; i < external_references->size(); ++i) {
556 Put(external_references->code(i), external_references->address(i));
557 }
558}
559
560
561ExternalReferenceDecoder::~ExternalReferenceDecoder() {
562 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
563 DeleteArray(encodings_[type]);
564 }
565 DeleteArray(encodings_);
566}
567
568
Steve Blocka7e24c12009-10-30 11:49:00 +0000569bool Serializer::serialization_enabled_ = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000570bool Serializer::too_late_to_enable_now_ = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000571ExternalReferenceDecoder* Deserializer::external_reference_decoder_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000572
573
Leon Clarkee46be812010-01-19 14:06:41 +0000574Deserializer::Deserializer(SnapshotByteSource* source) : source_(source) {
Steve Blockd0582a62009-12-15 09:54:21 +0000575}
576
577
578// This routine both allocates a new object, and also keeps
579// track of where objects have been allocated so that we can
580// fix back references when deserializing.
581Address Deserializer::Allocate(int space_index, Space* space, int size) {
582 Address address;
583 if (!SpaceIsLarge(space_index)) {
584 ASSERT(!SpaceIsPaged(space_index) ||
585 size <= Page::kPageSize - Page::kObjectStartOffset);
John Reck59135872010-11-02 12:39:01 -0700586 MaybeObject* maybe_new_allocation;
Steve Blockd0582a62009-12-15 09:54:21 +0000587 if (space_index == NEW_SPACE) {
John Reck59135872010-11-02 12:39:01 -0700588 maybe_new_allocation =
589 reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
Steve Blockd0582a62009-12-15 09:54:21 +0000590 } else {
John Reck59135872010-11-02 12:39:01 -0700591 maybe_new_allocation =
592 reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
Steve Blockd0582a62009-12-15 09:54:21 +0000593 }
John Reck59135872010-11-02 12:39:01 -0700594 Object* new_allocation = maybe_new_allocation->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000595 HeapObject* new_object = HeapObject::cast(new_allocation);
Steve Blockd0582a62009-12-15 09:54:21 +0000596 address = new_object->address();
597 high_water_[space_index] = address + size;
598 } else {
599 ASSERT(SpaceIsLarge(space_index));
600 ASSERT(size > Page::kPageSize - Page::kObjectStartOffset);
601 LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
602 Object* new_allocation;
603 if (space_index == kLargeData) {
John Reck59135872010-11-02 12:39:01 -0700604 new_allocation = lo_space->AllocateRaw(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000605 } else if (space_index == kLargeFixedArray) {
John Reck59135872010-11-02 12:39:01 -0700606 new_allocation =
607 lo_space->AllocateRawFixedArray(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000608 } else {
609 ASSERT_EQ(kLargeCode, space_index);
John Reck59135872010-11-02 12:39:01 -0700610 new_allocation = lo_space->AllocateRawCode(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000611 }
Steve Blockd0582a62009-12-15 09:54:21 +0000612 HeapObject* new_object = HeapObject::cast(new_allocation);
613 // Record all large objects in the same space.
614 address = new_object->address();
Andrei Popescu31002712010-02-23 13:46:05 +0000615 pages_[LO_SPACE].Add(address);
Steve Blockd0582a62009-12-15 09:54:21 +0000616 }
617 last_object_address_ = address;
618 return address;
619}
620
621
622// This returns the address of an object that has been described in the
623// snapshot as being offset bytes back in a particular space.
624HeapObject* Deserializer::GetAddressFromEnd(int space) {
625 int offset = source_->GetInt();
626 ASSERT(!SpaceIsLarge(space));
627 offset <<= kObjectAlignmentBits;
628 return HeapObject::FromAddress(high_water_[space] - offset);
629}
630
631
632// This returns the address of an object that has been described in the
633// snapshot as being offset bytes into a particular space.
634HeapObject* Deserializer::GetAddressFromStart(int space) {
635 int offset = source_->GetInt();
636 if (SpaceIsLarge(space)) {
637 // Large spaces have one object per 'page'.
638 return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
639 }
640 offset <<= kObjectAlignmentBits;
641 if (space == NEW_SPACE) {
642 // New space has only one space - numbered 0.
643 return HeapObject::FromAddress(pages_[space][0] + offset);
644 }
645 ASSERT(SpaceIsPaged(space));
Leon Clarkee46be812010-01-19 14:06:41 +0000646 int page_of_pointee = offset >> kPageSizeBits;
Steve Blockd0582a62009-12-15 09:54:21 +0000647 Address object_address = pages_[space][page_of_pointee] +
648 (offset & Page::kPageAlignmentMask);
649 return HeapObject::FromAddress(object_address);
650}
651
652
653void Deserializer::Deserialize() {
654 // Don't GC while deserializing - just expand the heap.
655 AlwaysAllocateScope always_allocate;
656 // Don't use the free lists while deserializing.
657 LinearAllocationScope allocate_linearly;
658 // No active threads.
659 ASSERT_EQ(NULL, ThreadState::FirstInUse());
660 // No active handles.
661 ASSERT(HandleScopeImplementer::instance()->blocks()->is_empty());
Leon Clarked91b9f72010-01-27 17:25:45 +0000662 // Make sure the entire partial snapshot cache is traversed, filling it with
663 // valid object pointers.
664 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000665 ASSERT_EQ(NULL, external_reference_decoder_);
666 external_reference_decoder_ = new ExternalReferenceDecoder();
Leon Clarked91b9f72010-01-27 17:25:45 +0000667 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
668 Heap::IterateWeakRoots(this, VISIT_ALL);
Ben Murdochf87a2032010-10-22 12:50:53 +0100669
670 Heap::set_global_contexts_list(Heap::undefined_value());
Leon Clarkee46be812010-01-19 14:06:41 +0000671}
672
673
674void Deserializer::DeserializePartial(Object** root) {
675 // Don't GC while deserializing - just expand the heap.
676 AlwaysAllocateScope always_allocate;
677 // Don't use the free lists while deserializing.
678 LinearAllocationScope allocate_linearly;
679 if (external_reference_decoder_ == NULL) {
680 external_reference_decoder_ = new ExternalReferenceDecoder();
681 }
682 VisitPointer(root);
683}
684
685
Leon Clarked91b9f72010-01-27 17:25:45 +0000686Deserializer::~Deserializer() {
687 ASSERT(source_->AtEOF());
Leon Clarkee46be812010-01-19 14:06:41 +0000688 if (external_reference_decoder_ != NULL) {
689 delete external_reference_decoder_;
690 external_reference_decoder_ = NULL;
691 }
Steve Blockd0582a62009-12-15 09:54:21 +0000692}
693
694
695// This is called on the roots. It is the driver of the deserialization
696// process. It is also called on the body of each function.
697void Deserializer::VisitPointers(Object** start, Object** end) {
698 // The space must be new space. Any other space would cause ReadChunk to try
699 // to update the remembered using NULL as the address.
700 ReadChunk(start, end, NEW_SPACE, NULL);
701}
702
703
704// This routine writes the new object into the pointer provided and then
705// returns true if the new object was in young space and false otherwise.
706// The reason for this strange interface is that otherwise the object is
707// written very late, which means the ByteArray map is not set up by the
708// time we need to use it to mark the space at the end of a page free (by
709// making it into a byte array).
710void Deserializer::ReadObject(int space_number,
711 Space* space,
712 Object** write_back) {
713 int size = source_->GetInt() << kObjectAlignmentBits;
714 Address address = Allocate(space_number, space, size);
715 *write_back = HeapObject::FromAddress(address);
716 Object** current = reinterpret_cast<Object**>(address);
717 Object** limit = current + (size >> kPointerSizeLog2);
Leon Clarkee46be812010-01-19 14:06:41 +0000718 if (FLAG_log_snapshot_positions) {
719 LOG(SnapshotPositionEvent(address, source_->position()));
720 }
Steve Blockd0582a62009-12-15 09:54:21 +0000721 ReadChunk(current, limit, space_number, address);
722}
723
724
Leon Clarkef7060e22010-06-03 12:02:55 +0100725// This macro is always used with a constant argument so it should all fold
726// away to almost nothing in the generated code. It might be nicer to do this
727// with the ternary operator but there are type issues with that.
728#define ASSIGN_DEST_SPACE(space_number) \
729 Space* dest_space; \
730 if (space_number == NEW_SPACE) { \
731 dest_space = Heap::new_space(); \
732 } else if (space_number == OLD_POINTER_SPACE) { \
733 dest_space = Heap::old_pointer_space(); \
734 } else if (space_number == OLD_DATA_SPACE) { \
735 dest_space = Heap::old_data_space(); \
736 } else if (space_number == CODE_SPACE) { \
737 dest_space = Heap::code_space(); \
738 } else if (space_number == MAP_SPACE) { \
739 dest_space = Heap::map_space(); \
740 } else if (space_number == CELL_SPACE) { \
741 dest_space = Heap::cell_space(); \
742 } else { \
743 ASSERT(space_number >= LO_SPACE); \
744 dest_space = Heap::lo_space(); \
745 }
746
747
748static const int kUnknownOffsetFromStart = -1;
Steve Blockd0582a62009-12-15 09:54:21 +0000749
750
751void Deserializer::ReadChunk(Object** current,
752 Object** limit,
Leon Clarkef7060e22010-06-03 12:02:55 +0100753 int source_space,
Steve Blockd0582a62009-12-15 09:54:21 +0000754 Address address) {
755 while (current < limit) {
756 int data = source_->Get();
757 switch (data) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100758#define CASE_STATEMENT(where, how, within, space_number) \
759 case where + how + within + space_number: \
760 ASSERT((where & ~kPointedToMask) == 0); \
761 ASSERT((how & ~kHowToCodeMask) == 0); \
762 ASSERT((within & ~kWhereToPointMask) == 0); \
763 ASSERT((space_number & ~kSpaceMask) == 0);
764
765#define CASE_BODY(where, how, within, space_number_if_any, offset_from_start) \
766 { \
767 bool emit_write_barrier = false; \
768 bool current_was_incremented = false; \
769 int space_number = space_number_if_any == kAnyOldSpace ? \
770 (data & kSpaceMask) : space_number_if_any; \
771 if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
772 ASSIGN_DEST_SPACE(space_number) \
773 ReadObject(space_number, dest_space, current); \
774 emit_write_barrier = \
775 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
776 } else { \
777 Object* new_object = NULL; /* May not be a real Object pointer. */ \
778 if (where == kNewObject) { \
779 ASSIGN_DEST_SPACE(space_number) \
780 ReadObject(space_number, dest_space, &new_object); \
781 } else if (where == kRootArray) { \
782 int root_id = source_->GetInt(); \
783 new_object = Heap::roots_address()[root_id]; \
784 } else if (where == kPartialSnapshotCache) { \
785 int cache_index = source_->GetInt(); \
786 new_object = partial_snapshot_cache_[cache_index]; \
787 } else if (where == kExternalReference) { \
788 int reference_id = source_->GetInt(); \
789 Address address = \
790 external_reference_decoder_->Decode(reference_id); \
791 new_object = reinterpret_cast<Object*>(address); \
792 } else if (where == kBackref) { \
793 emit_write_barrier = \
794 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
795 new_object = GetAddressFromEnd(data & kSpaceMask); \
796 } else { \
797 ASSERT(where == kFromStart); \
798 if (offset_from_start == kUnknownOffsetFromStart) { \
799 emit_write_barrier = \
800 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
801 new_object = GetAddressFromStart(data & kSpaceMask); \
802 } else { \
803 Address object_address = pages_[space_number][0] + \
804 (offset_from_start << kObjectAlignmentBits); \
805 new_object = HeapObject::FromAddress(object_address); \
806 } \
807 } \
808 if (within == kFirstInstruction) { \
809 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
810 new_object = reinterpret_cast<Object*>( \
811 new_code_object->instruction_start()); \
812 } \
813 if (how == kFromCode) { \
814 Address location_of_branch_data = \
815 reinterpret_cast<Address>(current); \
816 Assembler::set_target_at(location_of_branch_data, \
817 reinterpret_cast<Address>(new_object)); \
818 if (within == kFirstInstruction) { \
819 location_of_branch_data += Assembler::kCallTargetSize; \
820 current = reinterpret_cast<Object**>(location_of_branch_data); \
821 current_was_incremented = true; \
822 } \
823 } else { \
824 *current = new_object; \
825 } \
826 } \
827 if (emit_write_barrier) { \
828 Heap::RecordWrite(address, static_cast<int>( \
829 reinterpret_cast<Address>(current) - address)); \
830 } \
831 if (!current_was_incremented) { \
832 current++; /* Increment current if it wasn't done above. */ \
833 } \
834 break; \
835 } \
836
837// This generates a case and a body for each space. The large object spaces are
838// very rare in snapshots so they are grouped in one body.
839#define ONE_PER_SPACE(where, how, within) \
840 CASE_STATEMENT(where, how, within, NEW_SPACE) \
841 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
842 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
843 CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart) \
844 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
845 CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart) \
846 CASE_STATEMENT(where, how, within, CODE_SPACE) \
847 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
848 CASE_STATEMENT(where, how, within, CELL_SPACE) \
849 CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart) \
850 CASE_STATEMENT(where, how, within, MAP_SPACE) \
851 CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart) \
852 CASE_STATEMENT(where, how, within, kLargeData) \
853 CASE_STATEMENT(where, how, within, kLargeCode) \
854 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
855 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
856
857// This generates a case and a body for the new space (which has to do extra
858// write barrier handling) and handles the other spaces with 8 fall-through
859// cases and one body.
860#define ALL_SPACES(where, how, within) \
861 CASE_STATEMENT(where, how, within, NEW_SPACE) \
862 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
863 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
864 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
865 CASE_STATEMENT(where, how, within, CODE_SPACE) \
866 CASE_STATEMENT(where, how, within, CELL_SPACE) \
867 CASE_STATEMENT(where, how, within, MAP_SPACE) \
868 CASE_STATEMENT(where, how, within, kLargeData) \
869 CASE_STATEMENT(where, how, within, kLargeCode) \
870 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
871 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
872
Steve Block791712a2010-08-27 10:21:07 +0100873#define ONE_PER_CODE_SPACE(where, how, within) \
874 CASE_STATEMENT(where, how, within, CODE_SPACE) \
875 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
876 CASE_STATEMENT(where, how, within, kLargeCode) \
877 CASE_BODY(where, how, within, LO_SPACE, kUnknownOffsetFromStart)
878
Leon Clarkef7060e22010-06-03 12:02:55 +0100879#define EMIT_COMMON_REFERENCE_PATTERNS(pseudo_space_number, \
880 space_number, \
881 offset_from_start) \
882 CASE_STATEMENT(kFromStart, kPlain, kStartOfObject, pseudo_space_number) \
883 CASE_BODY(kFromStart, kPlain, kStartOfObject, space_number, offset_from_start)
884
885 // We generate 15 cases and bodies that process special tags that combine
886 // the raw data tag and the length into one byte.
Steve Blockd0582a62009-12-15 09:54:21 +0000887#define RAW_CASE(index, size) \
Leon Clarkef7060e22010-06-03 12:02:55 +0100888 case kRawData + index: { \
Steve Blockd0582a62009-12-15 09:54:21 +0000889 byte* raw_data_out = reinterpret_cast<byte*>(current); \
890 source_->CopyRaw(raw_data_out, size); \
891 current = reinterpret_cast<Object**>(raw_data_out + size); \
892 break; \
893 }
894 COMMON_RAW_LENGTHS(RAW_CASE)
895#undef RAW_CASE
Leon Clarkef7060e22010-06-03 12:02:55 +0100896
897 // Deserialize a chunk of raw data that doesn't have one of the popular
898 // lengths.
899 case kRawData: {
Steve Blockd0582a62009-12-15 09:54:21 +0000900 int size = source_->GetInt();
901 byte* raw_data_out = reinterpret_cast<byte*>(current);
902 source_->CopyRaw(raw_data_out, size);
903 current = reinterpret_cast<Object**>(raw_data_out + size);
904 break;
905 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100906
907 // Deserialize a new object and write a pointer to it to the current
908 // object.
909 ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100910 // Support for direct instruction pointers in functions
911 ONE_PER_CODE_SPACE(kNewObject, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100912 // Deserialize a new code object and write a pointer to its first
913 // instruction to the current code object.
914 ONE_PER_SPACE(kNewObject, kFromCode, kFirstInstruction)
915 // Find a recently deserialized object using its offset from the current
916 // allocation point and write a pointer to it to the current object.
917 ALL_SPACES(kBackref, kPlain, kStartOfObject)
918 // Find a recently deserialized code object using its offset from the
919 // current allocation point and write a pointer to its first instruction
Steve Block791712a2010-08-27 10:21:07 +0100920 // to the current code object or the instruction pointer in a function
921 // object.
Leon Clarkef7060e22010-06-03 12:02:55 +0100922 ALL_SPACES(kBackref, kFromCode, kFirstInstruction)
Steve Block791712a2010-08-27 10:21:07 +0100923 ALL_SPACES(kBackref, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100924 // Find an already deserialized object using its offset from the start
925 // and write a pointer to it to the current object.
926 ALL_SPACES(kFromStart, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100927 ALL_SPACES(kFromStart, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100928 // Find an already deserialized code object using its offset from the
929 // start and write a pointer to its first instruction to the current code
930 // object.
931 ALL_SPACES(kFromStart, kFromCode, kFirstInstruction)
932 // Find an already deserialized object at one of the predetermined popular
933 // offsets from the start and write a pointer to it in the current object.
934 COMMON_REFERENCE_PATTERNS(EMIT_COMMON_REFERENCE_PATTERNS)
935 // Find an object in the roots array and write a pointer to it to the
936 // current object.
937 CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
938 CASE_BODY(kRootArray, kPlain, kStartOfObject, 0, kUnknownOffsetFromStart)
939 // Find an object in the partial snapshots cache and write a pointer to it
940 // to the current object.
941 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
942 CASE_BODY(kPartialSnapshotCache,
943 kPlain,
944 kStartOfObject,
945 0,
946 kUnknownOffsetFromStart)
Steve Block791712a2010-08-27 10:21:07 +0100947 // Find an code entry in the partial snapshots cache and
948 // write a pointer to it to the current object.
949 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kFirstInstruction, 0)
950 CASE_BODY(kPartialSnapshotCache,
951 kPlain,
952 kFirstInstruction,
953 0,
954 kUnknownOffsetFromStart)
Leon Clarkef7060e22010-06-03 12:02:55 +0100955 // Find an external reference and write a pointer to it to the current
956 // object.
957 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
958 CASE_BODY(kExternalReference,
959 kPlain,
960 kStartOfObject,
961 0,
962 kUnknownOffsetFromStart)
963 // Find an external reference and write a pointer to it in the current
964 // code object.
965 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
966 CASE_BODY(kExternalReference,
967 kFromCode,
968 kStartOfObject,
969 0,
970 kUnknownOffsetFromStart)
971
972#undef CASE_STATEMENT
973#undef CASE_BODY
974#undef ONE_PER_SPACE
975#undef ALL_SPACES
976#undef EMIT_COMMON_REFERENCE_PATTERNS
977#undef ASSIGN_DEST_SPACE
978
979 case kNewPage: {
Steve Blockd0582a62009-12-15 09:54:21 +0000980 int space = source_->Get();
981 pages_[space].Add(last_object_address_);
Steve Block6ded16b2010-05-10 14:33:55 +0100982 if (space == CODE_SPACE) {
983 CPU::FlushICache(last_object_address_, Page::kPageSize);
984 }
Steve Blockd0582a62009-12-15 09:54:21 +0000985 break;
986 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100987
988 case kNativesStringResource: {
Steve Blockd0582a62009-12-15 09:54:21 +0000989 int index = source_->Get();
990 Vector<const char> source_vector = Natives::GetScriptSource(index);
991 NativesExternalStringResource* resource =
992 new NativesExternalStringResource(source_vector.start());
993 *current++ = reinterpret_cast<Object*>(resource);
994 break;
995 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100996
997 case kSynchronize: {
Leon Clarked91b9f72010-01-27 17:25:45 +0000998 // If we get here then that indicates that you have a mismatch between
999 // the number of GC roots when serializing and deserializing.
1000 UNREACHABLE();
1001 }
Leon Clarkef7060e22010-06-03 12:02:55 +01001002
Steve Blockd0582a62009-12-15 09:54:21 +00001003 default:
1004 UNREACHABLE();
1005 }
1006 }
1007 ASSERT_EQ(current, limit);
1008}
1009
1010
1011void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
1012 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
1013 for (int shift = max_shift; shift > 0; shift -= 7) {
1014 if (integer >= static_cast<uintptr_t>(1u) << shift) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001015 Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart");
Steve Blockd0582a62009-12-15 09:54:21 +00001016 }
1017 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001018 PutSection(static_cast<int>(integer & 0x7f), "IntLastPart");
Steve Blockd0582a62009-12-15 09:54:21 +00001019}
1020
Steve Blocka7e24c12009-10-30 11:49:00 +00001021#ifdef DEBUG
Steve Blockd0582a62009-12-15 09:54:21 +00001022
1023void Deserializer::Synchronize(const char* tag) {
1024 int data = source_->Get();
1025 // If this assert fails then that indicates that you have a mismatch between
1026 // the number of GC roots when serializing and deserializing.
Leon Clarkef7060e22010-06-03 12:02:55 +01001027 ASSERT_EQ(kSynchronize, data);
Steve Blockd0582a62009-12-15 09:54:21 +00001028 do {
1029 int character = source_->Get();
1030 if (character == 0) break;
1031 if (FLAG_debug_serialization) {
1032 PrintF("%c", character);
1033 }
1034 } while (true);
1035 if (FLAG_debug_serialization) {
1036 PrintF("\n");
1037 }
1038}
1039
Steve Blocka7e24c12009-10-30 11:49:00 +00001040
1041void Serializer::Synchronize(const char* tag) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001042 sink_->Put(kSynchronize, tag);
Steve Blockd0582a62009-12-15 09:54:21 +00001043 int character;
1044 do {
1045 character = *tag++;
1046 sink_->PutSection(character, "TagCharacter");
1047 } while (character != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001048}
Steve Blockd0582a62009-12-15 09:54:21 +00001049
Steve Blocka7e24c12009-10-30 11:49:00 +00001050#endif
1051
Steve Blockd0582a62009-12-15 09:54:21 +00001052Serializer::Serializer(SnapshotByteSink* sink)
1053 : sink_(sink),
1054 current_root_index_(0),
Andrei Popescu31002712010-02-23 13:46:05 +00001055 external_reference_encoder_(new ExternalReferenceEncoder),
Leon Clarkee46be812010-01-19 14:06:41 +00001056 large_object_total_(0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001057 for (int i = 0; i <= LAST_SPACE; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +00001058 fullness_[i] = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001059 }
1060}
1061
1062
Andrei Popescu31002712010-02-23 13:46:05 +00001063Serializer::~Serializer() {
1064 delete external_reference_encoder_;
1065}
1066
1067
Leon Clarked91b9f72010-01-27 17:25:45 +00001068void StartupSerializer::SerializeStrongReferences() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001069 // No active threads.
1070 CHECK_EQ(NULL, ThreadState::FirstInUse());
1071 // No active or weak handles.
1072 CHECK(HandleScopeImplementer::instance()->blocks()->is_empty());
1073 CHECK_EQ(0, GlobalHandles::NumberOfWeakHandles());
Steve Blockd0582a62009-12-15 09:54:21 +00001074 // We don't support serializing installed extensions.
1075 for (RegisteredExtension* ext = RegisteredExtension::first_extension();
1076 ext != NULL;
1077 ext = ext->next()) {
1078 CHECK_NE(v8::INSTALLED, ext->state());
1079 }
Leon Clarked91b9f72010-01-27 17:25:45 +00001080 Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG);
Steve Blocka7e24c12009-10-30 11:49:00 +00001081}
1082
1083
Leon Clarked91b9f72010-01-27 17:25:45 +00001084void PartialSerializer::Serialize(Object** object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001085 this->VisitPointer(object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001086
1087 // After we have done the partial serialization the partial snapshot cache
1088 // will contain some references needed to decode the partial snapshot. We
1089 // fill it up with undefineds so it has a predictable length so the
1090 // deserialization code doesn't need to know the length.
1091 for (int index = partial_snapshot_cache_length_;
1092 index < kPartialSnapshotCacheCapacity;
1093 index++) {
1094 partial_snapshot_cache_[index] = Heap::undefined_value();
1095 startup_serializer_->VisitPointer(&partial_snapshot_cache_[index]);
1096 }
1097 partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity;
Leon Clarkee46be812010-01-19 14:06:41 +00001098}
1099
1100
Steve Blocka7e24c12009-10-30 11:49:00 +00001101void Serializer::VisitPointers(Object** start, Object** end) {
Steve Blockd0582a62009-12-15 09:54:21 +00001102 for (Object** current = start; current < end; current++) {
1103 if ((*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001104 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001105 sink_->PutInt(kPointerSize, "length");
1106 for (int i = 0; i < kPointerSize; i++) {
1107 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1108 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001109 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001110 SerializeObject(*current, kPlain, kStartOfObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001111 }
1112 }
1113}
1114
1115
Leon Clarked91b9f72010-01-27 17:25:45 +00001116Object* SerializerDeserializer::partial_snapshot_cache_[
1117 kPartialSnapshotCacheCapacity];
1118int SerializerDeserializer::partial_snapshot_cache_length_ = 0;
1119
1120
1121// This ensures that the partial snapshot cache keeps things alive during GC and
1122// tracks their movement. When it is called during serialization of the startup
1123// snapshot the partial snapshot is empty, so nothing happens. When the partial
1124// (context) snapshot is created, this array is populated with the pointers that
1125// the partial snapshot will need. As that happens we emit serialized objects to
1126// the startup snapshot that correspond to the elements of this cache array. On
1127// deserialization we therefore need to visit the cache array. This fills it up
1128// with pointers to deserialized objects.
Steve Block6ded16b2010-05-10 14:33:55 +01001129void SerializerDeserializer::Iterate(ObjectVisitor* visitor) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001130 visitor->VisitPointers(
1131 &partial_snapshot_cache_[0],
1132 &partial_snapshot_cache_[partial_snapshot_cache_length_]);
1133}
1134
1135
1136// When deserializing we need to set the size of the snapshot cache. This means
1137// the root iteration code (above) will iterate over array elements, writing the
1138// references to deserialized objects in them.
1139void SerializerDeserializer::SetSnapshotCacheSize(int size) {
1140 partial_snapshot_cache_length_ = size;
1141}
1142
1143
1144int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1145 for (int i = 0; i < partial_snapshot_cache_length_; i++) {
1146 Object* entry = partial_snapshot_cache_[i];
1147 if (entry == heap_object) return i;
1148 }
Andrei Popescu31002712010-02-23 13:46:05 +00001149
Leon Clarked91b9f72010-01-27 17:25:45 +00001150 // We didn't find the object in the cache. So we add it to the cache and
1151 // then visit the pointer so that it becomes part of the startup snapshot
1152 // and we can refer to it from the partial snapshot.
1153 int length = partial_snapshot_cache_length_;
1154 CHECK(length < kPartialSnapshotCacheCapacity);
1155 partial_snapshot_cache_[length] = heap_object;
1156 startup_serializer_->VisitPointer(&partial_snapshot_cache_[length]);
1157 // We don't recurse from the startup snapshot generator into the partial
1158 // snapshot generator.
1159 ASSERT(length == partial_snapshot_cache_length_);
1160 return partial_snapshot_cache_length_++;
1161}
1162
1163
1164int PartialSerializer::RootIndex(HeapObject* heap_object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001165 for (int i = 0; i < Heap::kRootListLength; i++) {
1166 Object* root = Heap::roots_address()[i];
1167 if (root == heap_object) return i;
1168 }
1169 return kInvalidRootIndex;
1170}
1171
1172
Leon Clarked91b9f72010-01-27 17:25:45 +00001173// Encode the location of an already deserialized object in order to write its
1174// location into a later object. We can encode the location as an offset from
1175// the start of the deserialized objects or as an offset backwards from the
1176// current allocation pointer.
1177void Serializer::SerializeReferenceToPreviousObject(
1178 int space,
1179 int address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001180 HowToCode how_to_code,
1181 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001182 int offset = CurrentAllocationAddress(space) - address;
1183 bool from_start = true;
1184 if (SpaceIsPaged(space)) {
1185 // For paged space it is simple to encode back from current allocation if
1186 // the object is on the same page as the current allocation pointer.
1187 if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1188 (address >> kPageSizeBits)) {
1189 from_start = false;
1190 address = offset;
1191 }
1192 } else if (space == NEW_SPACE) {
1193 // For new space it is always simple to encode back from current allocation.
1194 if (offset < address) {
1195 from_start = false;
1196 address = offset;
1197 }
1198 }
1199 // If we are actually dealing with real offsets (and not a numbering of
1200 // all objects) then we should shift out the bits that are always 0.
1201 if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
Leon Clarkef7060e22010-06-03 12:02:55 +01001202 if (from_start) {
1203#define COMMON_REFS_CASE(pseudo_space, actual_space, offset) \
1204 if (space == actual_space && address == offset && \
1205 how_to_code == kPlain && where_to_point == kStartOfObject) { \
1206 sink_->Put(kFromStart + how_to_code + where_to_point + \
1207 pseudo_space, "RefSer"); \
1208 } else /* NOLINT */
1209 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
1210#undef COMMON_REFS_CASE
1211 { /* NOLINT */
1212 sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer");
Leon Clarked91b9f72010-01-27 17:25:45 +00001213 sink_->PutInt(address, "address");
1214 }
1215 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001216 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1217 sink_->PutInt(address, "address");
Leon Clarked91b9f72010-01-27 17:25:45 +00001218 }
1219}
1220
1221
1222void StartupSerializer::SerializeObject(
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001223 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001224 HowToCode how_to_code,
1225 WhereToPoint where_to_point) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001226 CHECK(o->IsHeapObject());
1227 HeapObject* heap_object = HeapObject::cast(o);
Leon Clarked91b9f72010-01-27 17:25:45 +00001228
1229 if (address_mapper_.IsMapped(heap_object)) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001230 int space = SpaceOfAlreadySerializedObject(heap_object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001231 int address = address_mapper_.MappedTo(heap_object);
1232 SerializeReferenceToPreviousObject(space,
1233 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001234 how_to_code,
1235 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001236 } else {
1237 // Object has not yet been serialized. Serialize it here.
1238 ObjectSerializer object_serializer(this,
1239 heap_object,
1240 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001241 how_to_code,
1242 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001243 object_serializer.Serialize();
1244 }
1245}
1246
1247
1248void StartupSerializer::SerializeWeakReferences() {
1249 for (int i = partial_snapshot_cache_length_;
1250 i < kPartialSnapshotCacheCapacity;
1251 i++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001252 sink_->Put(kRootArray + kPlain + kStartOfObject, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001253 sink_->PutInt(Heap::kUndefinedValueRootIndex, "root_index");
1254 }
1255 Heap::IterateWeakRoots(this, VISIT_ALL);
1256}
1257
1258
1259void PartialSerializer::SerializeObject(
1260 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001261 HowToCode how_to_code,
1262 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001263 CHECK(o->IsHeapObject());
1264 HeapObject* heap_object = HeapObject::cast(o);
1265
1266 int root_index;
1267 if ((root_index = RootIndex(heap_object)) != kInvalidRootIndex) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001268 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001269 sink_->PutInt(root_index, "root_index");
1270 return;
1271 }
1272
1273 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1274 int cache_index = PartialSnapshotCacheIndex(heap_object);
Leon Clarkef7060e22010-06-03 12:02:55 +01001275 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1276 "PartialSnapshotCache");
Leon Clarked91b9f72010-01-27 17:25:45 +00001277 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1278 return;
1279 }
1280
1281 // Pointers from the partial snapshot to the objects in the startup snapshot
1282 // should go through the root array or through the partial snapshot cache.
1283 // If this is not the case you may have to add something to the root array.
1284 ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1285 // All the symbols that the partial snapshot needs should be either in the
1286 // root table or in the partial snapshot cache.
1287 ASSERT(!heap_object->IsSymbol());
1288
1289 if (address_mapper_.IsMapped(heap_object)) {
1290 int space = SpaceOfAlreadySerializedObject(heap_object);
1291 int address = address_mapper_.MappedTo(heap_object);
1292 SerializeReferenceToPreviousObject(space,
1293 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001294 how_to_code,
1295 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001296 } else {
1297 // Object has not yet been serialized. Serialize it here.
1298 ObjectSerializer serializer(this,
1299 heap_object,
1300 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001301 how_to_code,
1302 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001303 serializer.Serialize();
1304 }
1305}
1306
1307
Steve Blockd0582a62009-12-15 09:54:21 +00001308void Serializer::ObjectSerializer::Serialize() {
1309 int space = Serializer::SpaceOfObject(object_);
1310 int size = object_->Size();
1311
Leon Clarkef7060e22010-06-03 12:02:55 +01001312 sink_->Put(kNewObject + reference_representation_ + space,
1313 "ObjectSerialization");
Steve Blockd0582a62009-12-15 09:54:21 +00001314 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1315
Leon Clarkee46be812010-01-19 14:06:41 +00001316 LOG(SnapshotPositionEvent(object_->address(), sink_->Position()));
1317
Steve Blockd0582a62009-12-15 09:54:21 +00001318 // Mark this object as already serialized.
1319 bool start_new_page;
Leon Clarked91b9f72010-01-27 17:25:45 +00001320 int offset = serializer_->Allocate(space, size, &start_new_page);
1321 serializer_->address_mapper()->AddMapping(object_, offset);
Steve Blockd0582a62009-12-15 09:54:21 +00001322 if (start_new_page) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001323 sink_->Put(kNewPage, "NewPage");
Steve Blockd0582a62009-12-15 09:54:21 +00001324 sink_->PutSection(space, "NewPageSpace");
1325 }
1326
1327 // Serialize the map (first word of the object).
Leon Clarkef7060e22010-06-03 12:02:55 +01001328 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001329
1330 // Serialize the rest of the object.
1331 CHECK_EQ(0, bytes_processed_so_far_);
1332 bytes_processed_so_far_ = kPointerSize;
1333 object_->IterateBody(object_->map()->instance_type(), size, this);
1334 OutputRawData(object_->address() + size);
1335}
1336
1337
1338void Serializer::ObjectSerializer::VisitPointers(Object** start,
1339 Object** end) {
1340 Object** current = start;
1341 while (current < end) {
1342 while (current < end && (*current)->IsSmi()) current++;
1343 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1344
1345 while (current < end && !(*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001346 serializer_->SerializeObject(*current, kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001347 bytes_processed_so_far_ += kPointerSize;
1348 current++;
1349 }
1350 }
1351}
1352
1353
1354void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1355 Address* end) {
1356 Address references_start = reinterpret_cast<Address>(start);
1357 OutputRawData(references_start);
1358
1359 for (Address* current = start; current < end; current++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001360 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
Steve Blockd0582a62009-12-15 09:54:21 +00001361 int reference_id = serializer_->EncodeExternalReference(*current);
1362 sink_->PutInt(reference_id, "reference id");
1363 }
1364 bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1365}
1366
1367
1368void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1369 Address target_start = rinfo->target_address_address();
1370 OutputRawData(target_start);
1371 Address target = rinfo->target_address();
1372 uint32_t encoding = serializer_->EncodeExternalReference(target);
1373 CHECK(target == NULL ? encoding == 0 : encoding != 0);
Leon Clarkef7060e22010-06-03 12:02:55 +01001374 int representation;
1375 // Can't use a ternary operator because of gcc.
1376 if (rinfo->IsCodedSpecially()) {
1377 representation = kStartOfObject + kFromCode;
1378 } else {
1379 representation = kStartOfObject + kPlain;
1380 }
1381 sink_->Put(kExternalReference + representation, "ExternalReference");
Steve Blockd0582a62009-12-15 09:54:21 +00001382 sink_->PutInt(encoding, "reference id");
Leon Clarkef7060e22010-06-03 12:02:55 +01001383 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001384}
1385
1386
1387void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1388 CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1389 Address target_start = rinfo->target_address_address();
1390 OutputRawData(target_start);
1391 Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
Leon Clarkef7060e22010-06-03 12:02:55 +01001392 serializer_->SerializeObject(target, kFromCode, kFirstInstruction);
1393 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001394}
1395
1396
Steve Block791712a2010-08-27 10:21:07 +01001397void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1398 Code* target = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1399 OutputRawData(entry_address);
1400 serializer_->SerializeObject(target, kPlain, kFirstInstruction);
1401 bytes_processed_so_far_ += kPointerSize;
1402}
1403
1404
Ben Murdochb0fe1622011-05-05 13:52:32 +01001405void Serializer::ObjectSerializer::VisitGlobalPropertyCell(RelocInfo* rinfo) {
1406 // We shouldn't have any global property cell references in code
1407 // objects in the snapshot.
1408 UNREACHABLE();
1409}
1410
1411
Steve Blockd0582a62009-12-15 09:54:21 +00001412void Serializer::ObjectSerializer::VisitExternalAsciiString(
1413 v8::String::ExternalAsciiStringResource** resource_pointer) {
1414 Address references_start = reinterpret_cast<Address>(resource_pointer);
1415 OutputRawData(references_start);
1416 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1417 Object* source = Heap::natives_source_cache()->get(i);
1418 if (!source->IsUndefined()) {
1419 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1420 typedef v8::String::ExternalAsciiStringResource Resource;
1421 Resource* resource = string->resource();
1422 if (resource == *resource_pointer) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001423 sink_->Put(kNativesStringResource, "NativesStringResource");
Steve Blockd0582a62009-12-15 09:54:21 +00001424 sink_->PutSection(i, "NativesStringResourceEnd");
1425 bytes_processed_so_far_ += sizeof(resource);
1426 return;
1427 }
1428 }
1429 }
1430 // One of the strings in the natives cache should match the resource. We
1431 // can't serialize any other kinds of external strings.
1432 UNREACHABLE();
1433}
1434
1435
1436void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1437 Address object_start = object_->address();
1438 int up_to_offset = static_cast<int>(up_to - object_start);
1439 int skipped = up_to_offset - bytes_processed_so_far_;
1440 // This assert will fail if the reloc info gives us the target_address_address
1441 // locations in a non-ascending order. Luckily that doesn't happen.
1442 ASSERT(skipped >= 0);
1443 if (skipped != 0) {
1444 Address base = object_start + bytes_processed_so_far_;
1445#define RAW_CASE(index, length) \
1446 if (skipped == length) { \
Leon Clarkef7060e22010-06-03 12:02:55 +01001447 sink_->PutSection(kRawData + index, "RawDataFixed"); \
Steve Blockd0582a62009-12-15 09:54:21 +00001448 } else /* NOLINT */
1449 COMMON_RAW_LENGTHS(RAW_CASE)
1450#undef RAW_CASE
1451 { /* NOLINT */
Leon Clarkef7060e22010-06-03 12:02:55 +01001452 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001453 sink_->PutInt(skipped, "length");
1454 }
1455 for (int i = 0; i < skipped; i++) {
1456 unsigned int data = base[i];
1457 sink_->PutSection(data, "Byte");
1458 }
1459 bytes_processed_so_far_ += skipped;
1460 }
1461}
1462
1463
1464int Serializer::SpaceOfObject(HeapObject* object) {
1465 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1466 AllocationSpace s = static_cast<AllocationSpace>(i);
1467 if (Heap::InSpace(object, s)) {
1468 if (i == LO_SPACE) {
1469 if (object->IsCode()) {
1470 return kLargeCode;
1471 } else if (object->IsFixedArray()) {
1472 return kLargeFixedArray;
1473 } else {
1474 return kLargeData;
1475 }
1476 }
1477 return i;
1478 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001479 }
1480 UNREACHABLE();
Steve Blockd0582a62009-12-15 09:54:21 +00001481 return 0;
1482}
1483
1484
1485int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1486 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1487 AllocationSpace s = static_cast<AllocationSpace>(i);
1488 if (Heap::InSpace(object, s)) {
1489 return i;
1490 }
1491 }
1492 UNREACHABLE();
1493 return 0;
1494}
1495
1496
1497int Serializer::Allocate(int space, int size, bool* new_page) {
1498 CHECK(space >= 0 && space < kNumberOfSpaces);
1499 if (SpaceIsLarge(space)) {
1500 // In large object space we merely number the objects instead of trying to
1501 // determine some sort of address.
1502 *new_page = true;
Leon Clarkee46be812010-01-19 14:06:41 +00001503 large_object_total_ += size;
Steve Blockd0582a62009-12-15 09:54:21 +00001504 return fullness_[LO_SPACE]++;
1505 }
1506 *new_page = false;
1507 if (fullness_[space] == 0) {
1508 *new_page = true;
1509 }
1510 if (SpaceIsPaged(space)) {
1511 // Paged spaces are a little special. We encode their addresses as if the
1512 // pages were all contiguous and each page were filled up in the range
1513 // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1514 // and allocation does not start at offset 0 in the page, but this scheme
1515 // means the deserializer can get the page number quickly by shifting the
1516 // serialized address.
1517 CHECK(IsPowerOf2(Page::kPageSize));
1518 int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1519 CHECK(size <= Page::kObjectAreaSize);
1520 if (used_in_this_page + size > Page::kObjectAreaSize) {
1521 *new_page = true;
1522 fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1523 }
1524 }
1525 int allocation_address = fullness_[space];
1526 fullness_[space] = allocation_address + size;
1527 return allocation_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001528}
1529
1530
1531} } // namespace v8::internal