blob: ec3a967ba80b3543c7eefe5dbadb466e91387a0c [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 Blockd0582a62009-12-15 09:54:21 +000047// Mapping objects to their location after deserialization.
48// This is used during building, but not at runtime by V8.
49class SerializationAddressMapper {
Steve Blocka7e24c12009-10-30 11:49:00 +000050 public:
Steve Blockd0582a62009-12-15 09:54:21 +000051 static bool IsMapped(HeapObject* obj) {
52 EnsureMapExists();
53 return serialization_map_->Lookup(Key(obj), Hash(obj), false) != NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000054 }
55
Steve Blockd0582a62009-12-15 09:54:21 +000056 static int MappedTo(HeapObject* obj) {
57 ASSERT(IsMapped(obj));
Leon Clarkee46be812010-01-19 14:06:41 +000058 return static_cast<int>(reinterpret_cast<intptr_t>(
59 serialization_map_->Lookup(Key(obj), Hash(obj), false)->value));
Steve Blocka7e24c12009-10-30 11:49:00 +000060 }
61
Steve Blockd0582a62009-12-15 09:54:21 +000062 static void Map(HeapObject* obj, int to) {
63 EnsureMapExists();
64 ASSERT(!IsMapped(obj));
65 HashMap::Entry* entry =
66 serialization_map_->Lookup(Key(obj), Hash(obj), true);
67 entry->value = Value(to);
Steve Blocka7e24c12009-10-30 11:49:00 +000068 }
69
Steve Blockd0582a62009-12-15 09:54:21 +000070 static void Zap() {
71 if (serialization_map_ != NULL) {
72 delete serialization_map_;
73 }
74 serialization_map_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000075 }
Steve Blocka7e24c12009-10-30 11:49:00 +000076
77 private:
Steve Blockd0582a62009-12-15 09:54:21 +000078 static bool SerializationMatchFun(void* key1, void* key2) {
79 return key1 == key2;
Steve Blocka7e24c12009-10-30 11:49:00 +000080 }
Steve Blocka7e24c12009-10-30 11:49:00 +000081
Steve Blockd0582a62009-12-15 09:54:21 +000082 static uint32_t Hash(HeapObject* obj) {
Leon Clarkee46be812010-01-19 14:06:41 +000083 return static_cast<int32_t>(reinterpret_cast<intptr_t>(obj->address()));
Steve Blocka7e24c12009-10-30 11:49:00 +000084 }
Steve Blocka7e24c12009-10-30 11:49:00 +000085
Steve Blockd0582a62009-12-15 09:54:21 +000086 static void* Key(HeapObject* obj) {
87 return reinterpret_cast<void*>(obj->address());
Steve Blocka7e24c12009-10-30 11:49:00 +000088 }
Steve Blocka7e24c12009-10-30 11:49:00 +000089
Steve Blockd0582a62009-12-15 09:54:21 +000090 static void* Value(int v) {
91 return reinterpret_cast<void*>(v);
92 }
Steve Blocka7e24c12009-10-30 11:49:00 +000093
Steve Blockd0582a62009-12-15 09:54:21 +000094 static void EnsureMapExists() {
95 if (serialization_map_ == NULL) {
96 serialization_map_ = new HashMap(&SerializationMatchFun);
Steve Blocka7e24c12009-10-30 11:49:00 +000097 }
Steve Blocka7e24c12009-10-30 11:49:00 +000098 }
Steve Blockd0582a62009-12-15 09:54:21 +000099
100 static HashMap* serialization_map_;
101};
Steve Blocka7e24c12009-10-30 11:49:00 +0000102
103
Steve Blockd0582a62009-12-15 09:54:21 +0000104HashMap* SerializationAddressMapper::serialization_map_ = NULL;
105
106
107
Steve Blocka7e24c12009-10-30 11:49:00 +0000108
109// -----------------------------------------------------------------------------
110// Coding of external references.
111
112// The encoding of an external reference. The type is in the high word.
113// The id is in the low word.
114static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
115 return static_cast<uint32_t>(type) << 16 | id;
116}
117
118
119static int* GetInternalPointer(StatsCounter* counter) {
120 // All counters refer to dummy_counter, if deserializing happens without
121 // setting up counters.
122 static int dummy_counter = 0;
123 return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
124}
125
126
127// ExternalReferenceTable is a helper class that defines the relationship
128// between external references and their encodings. It is used to build
129// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
130class ExternalReferenceTable {
131 public:
132 static ExternalReferenceTable* instance() {
133 if (!instance_) instance_ = new ExternalReferenceTable();
134 return instance_;
135 }
136
137 int size() const { return refs_.length(); }
138
139 Address address(int i) { return refs_[i].address; }
140
141 uint32_t code(int i) { return refs_[i].code; }
142
143 const char* name(int i) { return refs_[i].name; }
144
145 int max_id(int code) { return max_id_[code]; }
146
147 private:
148 static ExternalReferenceTable* instance_;
149
150 ExternalReferenceTable() : refs_(64) { PopulateTable(); }
151 ~ExternalReferenceTable() { }
152
153 struct ExternalReferenceEntry {
154 Address address;
155 uint32_t code;
156 const char* name;
157 };
158
159 void PopulateTable();
160
161 // For a few types of references, we can get their address from their id.
162 void AddFromId(TypeCode type, uint16_t id, const char* name);
163
164 // For other types of references, the caller will figure out the address.
165 void Add(Address address, TypeCode type, uint16_t id, const char* name);
166
167 List<ExternalReferenceEntry> refs_;
168 int max_id_[kTypeCodeCount];
169};
170
171
172ExternalReferenceTable* ExternalReferenceTable::instance_ = NULL;
173
174
175void ExternalReferenceTable::AddFromId(TypeCode type,
176 uint16_t id,
177 const char* name) {
178 Address address;
179 switch (type) {
180 case C_BUILTIN: {
181 ExternalReference ref(static_cast<Builtins::CFunctionId>(id));
182 address = ref.address();
183 break;
184 }
185 case BUILTIN: {
186 ExternalReference ref(static_cast<Builtins::Name>(id));
187 address = ref.address();
188 break;
189 }
190 case RUNTIME_FUNCTION: {
191 ExternalReference ref(static_cast<Runtime::FunctionId>(id));
192 address = ref.address();
193 break;
194 }
195 case IC_UTILITY: {
196 ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)));
197 address = ref.address();
198 break;
199 }
200 default:
201 UNREACHABLE();
202 return;
203 }
204 Add(address, type, id, name);
205}
206
207
208void ExternalReferenceTable::Add(Address address,
209 TypeCode type,
210 uint16_t id,
211 const char* name) {
Steve Blockd0582a62009-12-15 09:54:21 +0000212 ASSERT_NE(NULL, address);
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 ExternalReferenceEntry entry;
214 entry.address = address;
215 entry.code = EncodeExternal(type, id);
216 entry.name = name;
Steve Blockd0582a62009-12-15 09:54:21 +0000217 ASSERT_NE(0, entry.code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 refs_.Add(entry);
219 if (id > max_id_[type]) max_id_[type] = id;
220}
221
222
223void ExternalReferenceTable::PopulateTable() {
224 for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
225 max_id_[type_code] = 0;
226 }
227
228 // The following populates all of the different type of external references
229 // into the ExternalReferenceTable.
230 //
231 // NOTE: This function was originally 100k of code. It has since been
232 // rewritten to be mostly table driven, as the callback macro style tends to
233 // very easily cause code bloat. Please be careful in the future when adding
234 // new references.
235
236 struct RefTableEntry {
237 TypeCode type;
238 uint16_t id;
239 const char* name;
240 };
241
242 static const RefTableEntry ref_table[] = {
243 // Builtins
Leon Clarkee46be812010-01-19 14:06:41 +0000244#define DEF_ENTRY_C(name, ignored) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000245 { C_BUILTIN, \
246 Builtins::c_##name, \
247 "Builtins::" #name },
248
249 BUILTIN_LIST_C(DEF_ENTRY_C)
250#undef DEF_ENTRY_C
251
Leon Clarkee46be812010-01-19 14:06:41 +0000252#define DEF_ENTRY_C(name, ignored) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 { BUILTIN, \
254 Builtins::name, \
255 "Builtins::" #name },
Leon Clarkee46be812010-01-19 14:06:41 +0000256#define DEF_ENTRY_A(name, kind, state) DEF_ENTRY_C(name, ignored)
Steve Blocka7e24c12009-10-30 11:49:00 +0000257
258 BUILTIN_LIST_C(DEF_ENTRY_C)
259 BUILTIN_LIST_A(DEF_ENTRY_A)
260 BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
261#undef DEF_ENTRY_C
262#undef DEF_ENTRY_A
263
264 // Runtime functions
265#define RUNTIME_ENTRY(name, nargs, ressize) \
266 { RUNTIME_FUNCTION, \
267 Runtime::k##name, \
268 "Runtime::" #name },
269
270 RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
271#undef RUNTIME_ENTRY
272
273 // IC utilities
274#define IC_ENTRY(name) \
275 { IC_UTILITY, \
276 IC::k##name, \
277 "IC::" #name },
278
279 IC_UTIL_LIST(IC_ENTRY)
280#undef IC_ENTRY
281 }; // end of ref_table[].
282
283 for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
284 AddFromId(ref_table[i].type, ref_table[i].id, ref_table[i].name);
285 }
286
287#ifdef ENABLE_DEBUGGER_SUPPORT
288 // Debug addresses
289 Add(Debug_Address(Debug::k_after_break_target_address).address(),
290 DEBUG_ADDRESS,
291 Debug::k_after_break_target_address << kDebugIdShift,
292 "Debug::after_break_target_address()");
293 Add(Debug_Address(Debug::k_debug_break_return_address).address(),
294 DEBUG_ADDRESS,
295 Debug::k_debug_break_return_address << kDebugIdShift,
296 "Debug::debug_break_return_address()");
297 const char* debug_register_format = "Debug::register_address(%i)";
Steve Blockd0582a62009-12-15 09:54:21 +0000298 int dr_format_length = StrLength(debug_register_format);
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 for (int i = 0; i < kNumJSCallerSaved; ++i) {
300 Vector<char> name = Vector<char>::New(dr_format_length + 1);
301 OS::SNPrintF(name, debug_register_format, i);
302 Add(Debug_Address(Debug::k_register_address, i).address(),
303 DEBUG_ADDRESS,
304 Debug::k_register_address << kDebugIdShift | i,
305 name.start());
306 }
307#endif
308
309 // Stat counters
310 struct StatsRefTableEntry {
311 StatsCounter* counter;
312 uint16_t id;
313 const char* name;
314 };
315
316 static const StatsRefTableEntry stats_ref_table[] = {
317#define COUNTER_ENTRY(name, caption) \
318 { &Counters::name, \
319 Counters::k_##name, \
320 "Counters::" #name },
321
322 STATS_COUNTER_LIST_1(COUNTER_ENTRY)
323 STATS_COUNTER_LIST_2(COUNTER_ENTRY)
324#undef COUNTER_ENTRY
325 }; // end of stats_ref_table[].
326
327 for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
328 Add(reinterpret_cast<Address>(
329 GetInternalPointer(stats_ref_table[i].counter)),
330 STATS_COUNTER,
331 stats_ref_table[i].id,
332 stats_ref_table[i].name);
333 }
334
335 // Top addresses
Steve Block3ce2e202009-11-05 08:53:23 +0000336 const char* top_address_format = "Top::%s";
337
338 const char* AddressNames[] = {
339#define C(name) #name,
340 TOP_ADDRESS_LIST(C)
341 TOP_ADDRESS_LIST_PROF(C)
342 NULL
343#undef C
344 };
345
Steve Blockd0582a62009-12-15 09:54:21 +0000346 int top_format_length = StrLength(top_address_format) - 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 for (uint16_t i = 0; i < Top::k_top_address_count; ++i) {
Steve Block3ce2e202009-11-05 08:53:23 +0000348 const char* address_name = AddressNames[i];
349 Vector<char> name =
Steve Blockd0582a62009-12-15 09:54:21 +0000350 Vector<char>::New(top_format_length + StrLength(address_name) + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 const char* chars = name.start();
Steve Block3ce2e202009-11-05 08:53:23 +0000352 OS::SNPrintF(name, top_address_format, address_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 Add(Top::get_address_from_id((Top::AddressId)i), TOP_ADDRESS, i, chars);
354 }
355
356 // Extensions
357 Add(FUNCTION_ADDR(GCExtension::GC), EXTENSION, 1,
358 "GCExtension::GC");
359
360 // Accessors
361#define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
362 Add((Address)&Accessors::name, \
363 ACCESSOR, \
364 Accessors::k##name, \
365 "Accessors::" #name);
366
367 ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
368#undef ACCESSOR_DESCRIPTOR_DECLARATION
369
370 // Stub cache tables
371 Add(SCTableReference::keyReference(StubCache::kPrimary).address(),
372 STUB_CACHE_TABLE,
373 1,
374 "StubCache::primary_->key");
375 Add(SCTableReference::valueReference(StubCache::kPrimary).address(),
376 STUB_CACHE_TABLE,
377 2,
378 "StubCache::primary_->value");
379 Add(SCTableReference::keyReference(StubCache::kSecondary).address(),
380 STUB_CACHE_TABLE,
381 3,
382 "StubCache::secondary_->key");
383 Add(SCTableReference::valueReference(StubCache::kSecondary).address(),
384 STUB_CACHE_TABLE,
385 4,
386 "StubCache::secondary_->value");
387
388 // Runtime entries
389 Add(ExternalReference::perform_gc_function().address(),
390 RUNTIME_ENTRY,
391 1,
392 "Runtime::PerformGC");
393 Add(ExternalReference::random_positive_smi_function().address(),
394 RUNTIME_ENTRY,
395 2,
396 "V8::RandomPositiveSmi");
397
398 // Miscellaneous
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 Add(ExternalReference::the_hole_value_location().address(),
400 UNCLASSIFIED,
401 2,
402 "Factory::the_hole_value().location()");
403 Add(ExternalReference::roots_address().address(),
404 UNCLASSIFIED,
405 3,
406 "Heap::roots_address()");
Steve Blockd0582a62009-12-15 09:54:21 +0000407 Add(ExternalReference::address_of_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 UNCLASSIFIED,
409 4,
410 "StackGuard::address_of_jslimit()");
Steve Blockd0582a62009-12-15 09:54:21 +0000411 Add(ExternalReference::address_of_real_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000412 UNCLASSIFIED,
413 5,
Steve Blockd0582a62009-12-15 09:54:21 +0000414 "StackGuard::address_of_real_jslimit()");
415 Add(ExternalReference::address_of_regexp_stack_limit().address(),
416 UNCLASSIFIED,
417 6,
Steve Blocka7e24c12009-10-30 11:49:00 +0000418 "RegExpStack::limit_address()");
419 Add(ExternalReference::new_space_start().address(),
420 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000421 7,
Steve Blocka7e24c12009-10-30 11:49:00 +0000422 "Heap::NewSpaceStart()");
423 Add(ExternalReference::heap_always_allocate_scope_depth().address(),
424 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000425 8,
Steve Blocka7e24c12009-10-30 11:49:00 +0000426 "Heap::always_allocate_scope_depth()");
427 Add(ExternalReference::new_space_allocation_limit_address().address(),
428 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000429 9,
Steve Blocka7e24c12009-10-30 11:49:00 +0000430 "Heap::NewSpaceAllocationLimitAddress()");
431 Add(ExternalReference::new_space_allocation_top_address().address(),
432 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000433 10,
Steve Blocka7e24c12009-10-30 11:49:00 +0000434 "Heap::NewSpaceAllocationTopAddress()");
435#ifdef ENABLE_DEBUGGER_SUPPORT
436 Add(ExternalReference::debug_break().address(),
437 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000438 11,
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 "Debug::Break()");
440 Add(ExternalReference::debug_step_in_fp_address().address(),
441 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000442 12,
Steve Blocka7e24c12009-10-30 11:49:00 +0000443 "Debug::step_in_fp_addr()");
444#endif
445 Add(ExternalReference::double_fp_operation(Token::ADD).address(),
446 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000447 13,
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 "add_two_doubles");
449 Add(ExternalReference::double_fp_operation(Token::SUB).address(),
450 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000451 14,
Steve Blocka7e24c12009-10-30 11:49:00 +0000452 "sub_two_doubles");
453 Add(ExternalReference::double_fp_operation(Token::MUL).address(),
454 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000455 15,
Steve Blocka7e24c12009-10-30 11:49:00 +0000456 "mul_two_doubles");
457 Add(ExternalReference::double_fp_operation(Token::DIV).address(),
458 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000459 16,
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 "div_two_doubles");
461 Add(ExternalReference::double_fp_operation(Token::MOD).address(),
462 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000463 17,
Steve Blocka7e24c12009-10-30 11:49:00 +0000464 "mod_two_doubles");
465 Add(ExternalReference::compare_doubles().address(),
466 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000467 18,
Steve Blocka7e24c12009-10-30 11:49:00 +0000468 "compare_doubles");
469#ifdef V8_NATIVE_REGEXP
470 Add(ExternalReference::re_case_insensitive_compare_uc16().address(),
471 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000472 19,
Steve Blocka7e24c12009-10-30 11:49:00 +0000473 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
474 Add(ExternalReference::re_check_stack_guard_state().address(),
475 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000476 20,
Steve Blocka7e24c12009-10-30 11:49:00 +0000477 "RegExpMacroAssembler*::CheckStackGuardState()");
478 Add(ExternalReference::re_grow_stack().address(),
479 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000480 21,
Steve Blocka7e24c12009-10-30 11:49:00 +0000481 "NativeRegExpMacroAssembler::GrowStack()");
Leon Clarkee46be812010-01-19 14:06:41 +0000482 Add(ExternalReference::re_word_character_map().address(),
483 UNCLASSIFIED,
484 22,
485 "NativeRegExpMacroAssembler::word_character_map");
Steve Blocka7e24c12009-10-30 11:49:00 +0000486#endif
Leon Clarkee46be812010-01-19 14:06:41 +0000487 // Keyed lookup cache.
488 Add(ExternalReference::keyed_lookup_cache_keys().address(),
489 UNCLASSIFIED,
490 23,
491 "KeyedLookupCache::keys()");
492 Add(ExternalReference::keyed_lookup_cache_field_offsets().address(),
493 UNCLASSIFIED,
494 24,
495 "KeyedLookupCache::field_offsets()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000496}
497
498
499ExternalReferenceEncoder::ExternalReferenceEncoder()
500 : encodings_(Match) {
501 ExternalReferenceTable* external_references =
502 ExternalReferenceTable::instance();
503 for (int i = 0; i < external_references->size(); ++i) {
504 Put(external_references->address(i), i);
505 }
506}
507
508
509uint32_t ExternalReferenceEncoder::Encode(Address key) const {
510 int index = IndexOf(key);
511 return index >=0 ? ExternalReferenceTable::instance()->code(index) : 0;
512}
513
514
515const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
516 int index = IndexOf(key);
517 return index >=0 ? ExternalReferenceTable::instance()->name(index) : NULL;
518}
519
520
521int ExternalReferenceEncoder::IndexOf(Address key) const {
522 if (key == NULL) return -1;
523 HashMap::Entry* entry =
524 const_cast<HashMap &>(encodings_).Lookup(key, Hash(key), false);
525 return entry == NULL
526 ? -1
527 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
528}
529
530
531void ExternalReferenceEncoder::Put(Address key, int index) {
532 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
533 entry->value = reinterpret_cast<void *>(index);
534}
535
536
537ExternalReferenceDecoder::ExternalReferenceDecoder()
538 : encodings_(NewArray<Address*>(kTypeCodeCount)) {
539 ExternalReferenceTable* external_references =
540 ExternalReferenceTable::instance();
541 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
542 int max = external_references->max_id(type) + 1;
543 encodings_[type] = NewArray<Address>(max + 1);
544 }
545 for (int i = 0; i < external_references->size(); ++i) {
546 Put(external_references->code(i), external_references->address(i));
547 }
548}
549
550
551ExternalReferenceDecoder::~ExternalReferenceDecoder() {
552 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
553 DeleteArray(encodings_[type]);
554 }
555 DeleteArray(encodings_);
556}
557
558
Steve Blocka7e24c12009-10-30 11:49:00 +0000559bool Serializer::serialization_enabled_ = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000560bool Serializer::too_late_to_enable_now_ = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000561ExternalReferenceDecoder* Deserializer::external_reference_decoder_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000562
563
Leon Clarkee46be812010-01-19 14:06:41 +0000564Deserializer::Deserializer(SnapshotByteSource* source) : source_(source) {
Steve Blockd0582a62009-12-15 09:54:21 +0000565}
566
567
568// This routine both allocates a new object, and also keeps
569// track of where objects have been allocated so that we can
570// fix back references when deserializing.
571Address Deserializer::Allocate(int space_index, Space* space, int size) {
572 Address address;
573 if (!SpaceIsLarge(space_index)) {
574 ASSERT(!SpaceIsPaged(space_index) ||
575 size <= Page::kPageSize - Page::kObjectStartOffset);
576 Object* new_allocation;
577 if (space_index == NEW_SPACE) {
578 new_allocation = reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
579 } else {
580 new_allocation = reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
581 }
582 HeapObject* new_object = HeapObject::cast(new_allocation);
583 ASSERT(!new_object->IsFailure());
584 address = new_object->address();
585 high_water_[space_index] = address + size;
586 } else {
587 ASSERT(SpaceIsLarge(space_index));
588 ASSERT(size > Page::kPageSize - Page::kObjectStartOffset);
589 LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
590 Object* new_allocation;
591 if (space_index == kLargeData) {
592 new_allocation = lo_space->AllocateRaw(size);
593 } else if (space_index == kLargeFixedArray) {
594 new_allocation = lo_space->AllocateRawFixedArray(size);
595 } else {
596 ASSERT_EQ(kLargeCode, space_index);
597 new_allocation = lo_space->AllocateRawCode(size);
598 }
599 ASSERT(!new_allocation->IsFailure());
600 HeapObject* new_object = HeapObject::cast(new_allocation);
601 // Record all large objects in the same space.
602 address = new_object->address();
603 high_water_[LO_SPACE] = address + size;
604 }
605 last_object_address_ = address;
606 return address;
607}
608
609
610// This returns the address of an object that has been described in the
611// snapshot as being offset bytes back in a particular space.
612HeapObject* Deserializer::GetAddressFromEnd(int space) {
613 int offset = source_->GetInt();
614 ASSERT(!SpaceIsLarge(space));
615 offset <<= kObjectAlignmentBits;
616 return HeapObject::FromAddress(high_water_[space] - offset);
617}
618
619
620// This returns the address of an object that has been described in the
621// snapshot as being offset bytes into a particular space.
622HeapObject* Deserializer::GetAddressFromStart(int space) {
623 int offset = source_->GetInt();
624 if (SpaceIsLarge(space)) {
625 // Large spaces have one object per 'page'.
626 return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
627 }
628 offset <<= kObjectAlignmentBits;
629 if (space == NEW_SPACE) {
630 // New space has only one space - numbered 0.
631 return HeapObject::FromAddress(pages_[space][0] + offset);
632 }
633 ASSERT(SpaceIsPaged(space));
Leon Clarkee46be812010-01-19 14:06:41 +0000634 int page_of_pointee = offset >> kPageSizeBits;
Steve Blockd0582a62009-12-15 09:54:21 +0000635 Address object_address = pages_[space][page_of_pointee] +
636 (offset & Page::kPageAlignmentMask);
637 return HeapObject::FromAddress(object_address);
638}
639
640
641void Deserializer::Deserialize() {
642 // Don't GC while deserializing - just expand the heap.
643 AlwaysAllocateScope always_allocate;
644 // Don't use the free lists while deserializing.
645 LinearAllocationScope allocate_linearly;
646 // No active threads.
647 ASSERT_EQ(NULL, ThreadState::FirstInUse());
648 // No active handles.
649 ASSERT(HandleScopeImplementer::instance()->blocks()->is_empty());
650 ASSERT_EQ(NULL, external_reference_decoder_);
651 external_reference_decoder_ = new ExternalReferenceDecoder();
652 Heap::IterateRoots(this, VISIT_ONLY_STRONG);
653 ASSERT(source_->AtEOF());
Leon Clarkee46be812010-01-19 14:06:41 +0000654}
655
656
657void Deserializer::DeserializePartial(Object** root) {
658 // Don't GC while deserializing - just expand the heap.
659 AlwaysAllocateScope always_allocate;
660 // Don't use the free lists while deserializing.
661 LinearAllocationScope allocate_linearly;
662 if (external_reference_decoder_ == NULL) {
663 external_reference_decoder_ = new ExternalReferenceDecoder();
664 }
665 VisitPointer(root);
666}
667
668
669void Deserializer::TearDown() {
670 if (external_reference_decoder_ != NULL) {
671 delete external_reference_decoder_;
672 external_reference_decoder_ = NULL;
673 }
Steve Blockd0582a62009-12-15 09:54:21 +0000674}
675
676
677// This is called on the roots. It is the driver of the deserialization
678// process. It is also called on the body of each function.
679void Deserializer::VisitPointers(Object** start, Object** end) {
680 // The space must be new space. Any other space would cause ReadChunk to try
681 // to update the remembered using NULL as the address.
682 ReadChunk(start, end, NEW_SPACE, NULL);
683}
684
685
686// This routine writes the new object into the pointer provided and then
687// returns true if the new object was in young space and false otherwise.
688// The reason for this strange interface is that otherwise the object is
689// written very late, which means the ByteArray map is not set up by the
690// time we need to use it to mark the space at the end of a page free (by
691// making it into a byte array).
692void Deserializer::ReadObject(int space_number,
693 Space* space,
694 Object** write_back) {
695 int size = source_->GetInt() << kObjectAlignmentBits;
696 Address address = Allocate(space_number, space, size);
697 *write_back = HeapObject::FromAddress(address);
698 Object** current = reinterpret_cast<Object**>(address);
699 Object** limit = current + (size >> kPointerSizeLog2);
Leon Clarkee46be812010-01-19 14:06:41 +0000700 if (FLAG_log_snapshot_positions) {
701 LOG(SnapshotPositionEvent(address, source_->position()));
702 }
Steve Blockd0582a62009-12-15 09:54:21 +0000703 ReadChunk(current, limit, space_number, address);
704}
705
706
707#define ONE_CASE_PER_SPACE(base_tag) \
708 case (base_tag) + NEW_SPACE: /* NOLINT */ \
709 case (base_tag) + OLD_POINTER_SPACE: /* NOLINT */ \
710 case (base_tag) + OLD_DATA_SPACE: /* NOLINT */ \
711 case (base_tag) + CODE_SPACE: /* NOLINT */ \
712 case (base_tag) + MAP_SPACE: /* NOLINT */ \
713 case (base_tag) + CELL_SPACE: /* NOLINT */ \
714 case (base_tag) + kLargeData: /* NOLINT */ \
715 case (base_tag) + kLargeCode: /* NOLINT */ \
716 case (base_tag) + kLargeFixedArray: /* NOLINT */
717
718
719void Deserializer::ReadChunk(Object** current,
720 Object** limit,
721 int space,
722 Address address) {
723 while (current < limit) {
724 int data = source_->Get();
725 switch (data) {
726#define RAW_CASE(index, size) \
727 case RAW_DATA_SERIALIZATION + index: { \
728 byte* raw_data_out = reinterpret_cast<byte*>(current); \
729 source_->CopyRaw(raw_data_out, size); \
730 current = reinterpret_cast<Object**>(raw_data_out + size); \
731 break; \
732 }
733 COMMON_RAW_LENGTHS(RAW_CASE)
734#undef RAW_CASE
735 case RAW_DATA_SERIALIZATION: {
736 int size = source_->GetInt();
737 byte* raw_data_out = reinterpret_cast<byte*>(current);
738 source_->CopyRaw(raw_data_out, size);
739 current = reinterpret_cast<Object**>(raw_data_out + size);
740 break;
741 }
742 case OBJECT_SERIALIZATION + NEW_SPACE: {
743 ReadObject(NEW_SPACE, Heap::new_space(), current);
744 if (space != NEW_SPACE) {
745 Heap::RecordWrite(address, static_cast<int>(
746 reinterpret_cast<Address>(current) - address));
747 }
748 current++;
749 break;
750 }
751 case OBJECT_SERIALIZATION + OLD_DATA_SPACE:
752 ReadObject(OLD_DATA_SPACE, Heap::old_data_space(), current++);
753 break;
754 case OBJECT_SERIALIZATION + OLD_POINTER_SPACE:
755 ReadObject(OLD_POINTER_SPACE, Heap::old_pointer_space(), current++);
756 break;
757 case OBJECT_SERIALIZATION + MAP_SPACE:
758 ReadObject(MAP_SPACE, Heap::map_space(), current++);
759 break;
760 case OBJECT_SERIALIZATION + CODE_SPACE:
761 ReadObject(CODE_SPACE, Heap::code_space(), current++);
762 LOG(LogCodeObject(current[-1]));
763 break;
764 case OBJECT_SERIALIZATION + CELL_SPACE:
765 ReadObject(CELL_SPACE, Heap::cell_space(), current++);
766 break;
767 case OBJECT_SERIALIZATION + kLargeData:
768 ReadObject(kLargeData, Heap::lo_space(), current++);
769 break;
770 case OBJECT_SERIALIZATION + kLargeCode:
771 ReadObject(kLargeCode, Heap::lo_space(), current++);
772 LOG(LogCodeObject(current[-1]));
773 break;
774 case OBJECT_SERIALIZATION + kLargeFixedArray:
775 ReadObject(kLargeFixedArray, Heap::lo_space(), current++);
776 break;
777 case CODE_OBJECT_SERIALIZATION + kLargeCode: {
778 Object* new_code_object = NULL;
779 ReadObject(kLargeCode, Heap::lo_space(), &new_code_object);
780 Code* code_object = reinterpret_cast<Code*>(new_code_object);
781 LOG(LogCodeObject(code_object));
782 // Setting a branch/call to another code object from code.
783 Address location_of_branch_data = reinterpret_cast<Address>(current);
784 Assembler::set_target_at(location_of_branch_data,
785 code_object->instruction_start());
786 location_of_branch_data += Assembler::kCallTargetSize;
787 current = reinterpret_cast<Object**>(location_of_branch_data);
788 break;
789 }
790 case CODE_OBJECT_SERIALIZATION + CODE_SPACE: {
791 Object* new_code_object = NULL;
792 ReadObject(CODE_SPACE, Heap::code_space(), &new_code_object);
793 Code* code_object = reinterpret_cast<Code*>(new_code_object);
794 LOG(LogCodeObject(code_object));
795 // Setting a branch/call to another code object from code.
796 Address location_of_branch_data = reinterpret_cast<Address>(current);
797 Assembler::set_target_at(location_of_branch_data,
798 code_object->instruction_start());
799 location_of_branch_data += Assembler::kCallTargetSize;
800 current = reinterpret_cast<Object**>(location_of_branch_data);
801 break;
802 }
803 ONE_CASE_PER_SPACE(BACKREF_SERIALIZATION) {
804 // Write a backreference to an object we unpacked earlier.
805 int backref_space = (data & kSpaceMask);
806 if (backref_space == NEW_SPACE && space != NEW_SPACE) {
807 Heap::RecordWrite(address, static_cast<int>(
808 reinterpret_cast<Address>(current) - address));
809 }
810 *current++ = GetAddressFromEnd(backref_space);
811 break;
812 }
813 ONE_CASE_PER_SPACE(REFERENCE_SERIALIZATION) {
814 // Write a reference to an object we unpacked earlier.
815 int reference_space = (data & kSpaceMask);
816 if (reference_space == NEW_SPACE && space != NEW_SPACE) {
817 Heap::RecordWrite(address, static_cast<int>(
818 reinterpret_cast<Address>(current) - address));
819 }
820 *current++ = GetAddressFromStart(reference_space);
821 break;
822 }
823#define COMMON_REFS_CASE(index, reference_space, address) \
824 case REFERENCE_SERIALIZATION + index: { \
825 ASSERT(SpaceIsPaged(reference_space)); \
826 Address object_address = \
827 pages_[reference_space][0] + (address << kObjectAlignmentBits); \
828 *current++ = HeapObject::FromAddress(object_address); \
829 break; \
830 }
831 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
832#undef COMMON_REFS_CASE
833 ONE_CASE_PER_SPACE(CODE_BACKREF_SERIALIZATION) {
834 int backref_space = (data & kSpaceMask);
835 // Can't use Code::cast because heap is not set up yet and assertions
836 // will fail.
837 Code* code_object =
838 reinterpret_cast<Code*>(GetAddressFromEnd(backref_space));
839 // Setting a branch/call to previously decoded code object from code.
840 Address location_of_branch_data = reinterpret_cast<Address>(current);
841 Assembler::set_target_at(location_of_branch_data,
842 code_object->instruction_start());
843 location_of_branch_data += Assembler::kCallTargetSize;
844 current = reinterpret_cast<Object**>(location_of_branch_data);
845 break;
846 }
847 ONE_CASE_PER_SPACE(CODE_REFERENCE_SERIALIZATION) {
848 int backref_space = (data & kSpaceMask);
849 // Can't use Code::cast because heap is not set up yet and assertions
850 // will fail.
851 Code* code_object =
852 reinterpret_cast<Code*>(GetAddressFromStart(backref_space));
853 // Setting a branch/call to previously decoded code object from code.
854 Address location_of_branch_data = reinterpret_cast<Address>(current);
855 Assembler::set_target_at(location_of_branch_data,
856 code_object->instruction_start());
857 location_of_branch_data += Assembler::kCallTargetSize;
858 current = reinterpret_cast<Object**>(location_of_branch_data);
859 break;
860 }
861 case EXTERNAL_REFERENCE_SERIALIZATION: {
862 int reference_id = source_->GetInt();
863 Address address = external_reference_decoder_->Decode(reference_id);
864 *current++ = reinterpret_cast<Object*>(address);
865 break;
866 }
867 case EXTERNAL_BRANCH_TARGET_SERIALIZATION: {
868 int reference_id = source_->GetInt();
869 Address address = external_reference_decoder_->Decode(reference_id);
870 Address location_of_branch_data = reinterpret_cast<Address>(current);
871 Assembler::set_external_target_at(location_of_branch_data, address);
872 location_of_branch_data += Assembler::kExternalTargetSize;
873 current = reinterpret_cast<Object**>(location_of_branch_data);
874 break;
875 }
876 case START_NEW_PAGE_SERIALIZATION: {
877 int space = source_->Get();
878 pages_[space].Add(last_object_address_);
879 break;
880 }
881 case NATIVES_STRING_RESOURCE: {
882 int index = source_->Get();
883 Vector<const char> source_vector = Natives::GetScriptSource(index);
884 NativesExternalStringResource* resource =
885 new NativesExternalStringResource(source_vector.start());
886 *current++ = reinterpret_cast<Object*>(resource);
887 break;
888 }
Leon Clarkee46be812010-01-19 14:06:41 +0000889 case ROOT_SERIALIZATION: {
890 int root_id = source_->GetInt();
891 *current++ = Heap::roots_address()[root_id];
892 break;
893 }
Steve Blockd0582a62009-12-15 09:54:21 +0000894 default:
895 UNREACHABLE();
896 }
897 }
898 ASSERT_EQ(current, limit);
899}
900
901
902void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
903 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
904 for (int shift = max_shift; shift > 0; shift -= 7) {
905 if (integer >= static_cast<uintptr_t>(1u) << shift) {
906 Put(((integer >> shift) & 0x7f) | 0x80, "IntPart");
907 }
908 }
909 PutSection(integer & 0x7f, "IntLastPart");
910}
911
Steve Blocka7e24c12009-10-30 11:49:00 +0000912#ifdef DEBUG
Steve Blockd0582a62009-12-15 09:54:21 +0000913
914void Deserializer::Synchronize(const char* tag) {
915 int data = source_->Get();
916 // If this assert fails then that indicates that you have a mismatch between
917 // the number of GC roots when serializing and deserializing.
918 ASSERT_EQ(SYNCHRONIZE, data);
919 do {
920 int character = source_->Get();
921 if (character == 0) break;
922 if (FLAG_debug_serialization) {
923 PrintF("%c", character);
924 }
925 } while (true);
926 if (FLAG_debug_serialization) {
927 PrintF("\n");
928 }
929}
930
Steve Blocka7e24c12009-10-30 11:49:00 +0000931
932void Serializer::Synchronize(const char* tag) {
Steve Blockd0582a62009-12-15 09:54:21 +0000933 sink_->Put(SYNCHRONIZE, tag);
934 int character;
935 do {
936 character = *tag++;
937 sink_->PutSection(character, "TagCharacter");
938 } while (character != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000939}
Steve Blockd0582a62009-12-15 09:54:21 +0000940
Steve Blocka7e24c12009-10-30 11:49:00 +0000941#endif
942
Steve Blockd0582a62009-12-15 09:54:21 +0000943Serializer::Serializer(SnapshotByteSink* sink)
944 : sink_(sink),
945 current_root_index_(0),
Leon Clarkee46be812010-01-19 14:06:41 +0000946 external_reference_encoder_(NULL),
947 partial_(false),
948 large_object_total_(0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000949 for (int i = 0; i <= LAST_SPACE; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +0000950 fullness_[i] = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000951 }
952}
953
954
Steve Blocka7e24c12009-10-30 11:49:00 +0000955void Serializer::Serialize() {
956 // No active threads.
957 CHECK_EQ(NULL, ThreadState::FirstInUse());
958 // No active or weak handles.
959 CHECK(HandleScopeImplementer::instance()->blocks()->is_empty());
960 CHECK_EQ(0, GlobalHandles::NumberOfWeakHandles());
Steve Blockd0582a62009-12-15 09:54:21 +0000961 CHECK_EQ(NULL, external_reference_encoder_);
962 // We don't support serializing installed extensions.
963 for (RegisteredExtension* ext = RegisteredExtension::first_extension();
964 ext != NULL;
965 ext = ext->next()) {
966 CHECK_NE(v8::INSTALLED, ext->state());
967 }
968 external_reference_encoder_ = new ExternalReferenceEncoder();
969 Heap::IterateRoots(this, VISIT_ONLY_STRONG);
970 delete external_reference_encoder_;
971 external_reference_encoder_ = NULL;
972 SerializationAddressMapper::Zap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000973}
974
975
Leon Clarkee46be812010-01-19 14:06:41 +0000976void Serializer::SerializePartial(Object** object) {
977 partial_ = true;
978 external_reference_encoder_ = new ExternalReferenceEncoder();
979 this->VisitPointer(object);
980 delete external_reference_encoder_;
981 external_reference_encoder_ = NULL;
982 SerializationAddressMapper::Zap();
983}
984
985
Steve Blocka7e24c12009-10-30 11:49:00 +0000986void Serializer::VisitPointers(Object** start, Object** end) {
Steve Blockd0582a62009-12-15 09:54:21 +0000987 for (Object** current = start; current < end; current++) {
988 if ((*current)->IsSmi()) {
989 sink_->Put(RAW_DATA_SERIALIZATION, "RawData");
990 sink_->PutInt(kPointerSize, "length");
991 for (int i = 0; i < kPointerSize; i++) {
992 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
993 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000994 } else {
Steve Blockd0582a62009-12-15 09:54:21 +0000995 SerializeObject(*current, TAGGED_REPRESENTATION);
Steve Blocka7e24c12009-10-30 11:49:00 +0000996 }
997 }
998}
999
1000
Leon Clarkee46be812010-01-19 14:06:41 +00001001int Serializer::RootIndex(HeapObject* heap_object) {
1002 for (int i = 0; i < Heap::kRootListLength; i++) {
1003 Object* root = Heap::roots_address()[i];
1004 if (root == heap_object) return i;
1005 }
1006 return kInvalidRootIndex;
1007}
1008
1009
Steve Blockd0582a62009-12-15 09:54:21 +00001010void Serializer::SerializeObject(
1011 Object* o,
1012 ReferenceRepresentation reference_representation) {
1013 CHECK(o->IsHeapObject());
1014 HeapObject* heap_object = HeapObject::cast(o);
Leon Clarkee46be812010-01-19 14:06:41 +00001015 if (partial_) {
1016 int root_index = RootIndex(heap_object);
1017 if (root_index != kInvalidRootIndex) {
1018 sink_->Put(ROOT_SERIALIZATION, "RootSerialization");
1019 sink_->PutInt(root_index, "root_index");
1020 return;
1021 }
1022 // All the symbols that the snapshot needs should be in the root table.
1023 ASSERT(!heap_object->IsSymbol());
1024 }
Steve Blockd0582a62009-12-15 09:54:21 +00001025 if (SerializationAddressMapper::IsMapped(heap_object)) {
1026 int space = SpaceOfAlreadySerializedObject(heap_object);
1027 int address = SerializationAddressMapper::MappedTo(heap_object);
1028 int offset = CurrentAllocationAddress(space) - address;
1029 bool from_start = true;
1030 if (SpaceIsPaged(space)) {
Leon Clarkee46be812010-01-19 14:06:41 +00001031 if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1032 (address >> kPageSizeBits)) {
Steve Blockd0582a62009-12-15 09:54:21 +00001033 from_start = false;
1034 address = offset;
1035 }
1036 } else if (space == NEW_SPACE) {
1037 if (offset < address) {
1038 from_start = false;
1039 address = offset;
1040 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001041 }
Steve Blockd0582a62009-12-15 09:54:21 +00001042 // If we are actually dealing with real offsets (and not a numbering of
1043 // all objects) then we should shift out the bits that are always 0.
1044 if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
1045 if (reference_representation == CODE_TARGET_REPRESENTATION) {
1046 if (from_start) {
1047 sink_->Put(CODE_REFERENCE_SERIALIZATION + space, "RefCodeSer");
1048 sink_->PutInt(address, "address");
Steve Blocka7e24c12009-10-30 11:49:00 +00001049 } else {
Steve Blockd0582a62009-12-15 09:54:21 +00001050 sink_->Put(CODE_BACKREF_SERIALIZATION + space, "BackRefCodeSer");
1051 sink_->PutInt(address, "address");
Steve Blocka7e24c12009-10-30 11:49:00 +00001052 }
1053 } else {
Steve Blockd0582a62009-12-15 09:54:21 +00001054 CHECK_EQ(TAGGED_REPRESENTATION, reference_representation);
1055 if (from_start) {
1056#define COMMON_REFS_CASE(tag, common_space, common_offset) \
1057 if (space == common_space && address == common_offset) { \
1058 sink_->PutSection(tag + REFERENCE_SERIALIZATION, "RefSer"); \
1059 } else /* NOLINT */
1060 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
1061#undef COMMON_REFS_CASE
1062 { /* NOLINT */
1063 sink_->Put(REFERENCE_SERIALIZATION + space, "RefSer");
1064 sink_->PutInt(address, "address");
Steve Blocka7e24c12009-10-30 11:49:00 +00001065 }
Steve Blockd0582a62009-12-15 09:54:21 +00001066 } else {
1067 sink_->Put(BACKREF_SERIALIZATION + space, "BackRefSer");
1068 sink_->PutInt(address, "address");
Steve Blocka7e24c12009-10-30 11:49:00 +00001069 }
Steve Blockd0582a62009-12-15 09:54:21 +00001070 }
1071 } else {
1072 // Object has not yet been serialized. Serialize it here.
1073 ObjectSerializer serializer(this,
1074 heap_object,
1075 sink_,
1076 reference_representation);
1077 serializer.Serialize();
1078 }
1079}
1080
1081
1082
1083void Serializer::ObjectSerializer::Serialize() {
1084 int space = Serializer::SpaceOfObject(object_);
1085 int size = object_->Size();
1086
1087 if (reference_representation_ == TAGGED_REPRESENTATION) {
1088 sink_->Put(OBJECT_SERIALIZATION + space, "ObjectSerialization");
1089 } else {
1090 CHECK_EQ(CODE_TARGET_REPRESENTATION, reference_representation_);
1091 sink_->Put(CODE_OBJECT_SERIALIZATION + space, "ObjectSerialization");
1092 }
1093 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1094
Leon Clarkee46be812010-01-19 14:06:41 +00001095 LOG(SnapshotPositionEvent(object_->address(), sink_->Position()));
1096
Steve Blockd0582a62009-12-15 09:54:21 +00001097 // Mark this object as already serialized.
1098 bool start_new_page;
1099 SerializationAddressMapper::Map(
1100 object_,
1101 serializer_->Allocate(space, size, &start_new_page));
1102 if (start_new_page) {
1103 sink_->Put(START_NEW_PAGE_SERIALIZATION, "NewPage");
1104 sink_->PutSection(space, "NewPageSpace");
1105 }
1106
1107 // Serialize the map (first word of the object).
1108 serializer_->SerializeObject(object_->map(), TAGGED_REPRESENTATION);
1109
1110 // Serialize the rest of the object.
1111 CHECK_EQ(0, bytes_processed_so_far_);
1112 bytes_processed_so_far_ = kPointerSize;
1113 object_->IterateBody(object_->map()->instance_type(), size, this);
1114 OutputRawData(object_->address() + size);
1115}
1116
1117
1118void Serializer::ObjectSerializer::VisitPointers(Object** start,
1119 Object** end) {
1120 Object** current = start;
1121 while (current < end) {
1122 while (current < end && (*current)->IsSmi()) current++;
1123 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1124
1125 while (current < end && !(*current)->IsSmi()) {
1126 serializer_->SerializeObject(*current, TAGGED_REPRESENTATION);
1127 bytes_processed_so_far_ += kPointerSize;
1128 current++;
1129 }
1130 }
1131}
1132
1133
1134void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1135 Address* end) {
1136 Address references_start = reinterpret_cast<Address>(start);
1137 OutputRawData(references_start);
1138
1139 for (Address* current = start; current < end; current++) {
1140 sink_->Put(EXTERNAL_REFERENCE_SERIALIZATION, "ExternalReference");
1141 int reference_id = serializer_->EncodeExternalReference(*current);
1142 sink_->PutInt(reference_id, "reference id");
1143 }
1144 bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1145}
1146
1147
1148void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1149 Address target_start = rinfo->target_address_address();
1150 OutputRawData(target_start);
1151 Address target = rinfo->target_address();
1152 uint32_t encoding = serializer_->EncodeExternalReference(target);
1153 CHECK(target == NULL ? encoding == 0 : encoding != 0);
1154 sink_->Put(EXTERNAL_BRANCH_TARGET_SERIALIZATION, "ExternalReference");
1155 sink_->PutInt(encoding, "reference id");
1156 bytes_processed_so_far_ += Assembler::kExternalTargetSize;
1157}
1158
1159
1160void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1161 CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1162 Address target_start = rinfo->target_address_address();
1163 OutputRawData(target_start);
1164 Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
1165 serializer_->SerializeObject(target, CODE_TARGET_REPRESENTATION);
1166 bytes_processed_so_far_ += Assembler::kCallTargetSize;
1167}
1168
1169
1170void Serializer::ObjectSerializer::VisitExternalAsciiString(
1171 v8::String::ExternalAsciiStringResource** resource_pointer) {
1172 Address references_start = reinterpret_cast<Address>(resource_pointer);
1173 OutputRawData(references_start);
1174 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1175 Object* source = Heap::natives_source_cache()->get(i);
1176 if (!source->IsUndefined()) {
1177 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1178 typedef v8::String::ExternalAsciiStringResource Resource;
1179 Resource* resource = string->resource();
1180 if (resource == *resource_pointer) {
1181 sink_->Put(NATIVES_STRING_RESOURCE, "NativesStringResource");
1182 sink_->PutSection(i, "NativesStringResourceEnd");
1183 bytes_processed_so_far_ += sizeof(resource);
1184 return;
1185 }
1186 }
1187 }
1188 // One of the strings in the natives cache should match the resource. We
1189 // can't serialize any other kinds of external strings.
1190 UNREACHABLE();
1191}
1192
1193
1194void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1195 Address object_start = object_->address();
1196 int up_to_offset = static_cast<int>(up_to - object_start);
1197 int skipped = up_to_offset - bytes_processed_so_far_;
1198 // This assert will fail if the reloc info gives us the target_address_address
1199 // locations in a non-ascending order. Luckily that doesn't happen.
1200 ASSERT(skipped >= 0);
1201 if (skipped != 0) {
1202 Address base = object_start + bytes_processed_so_far_;
1203#define RAW_CASE(index, length) \
1204 if (skipped == length) { \
1205 sink_->PutSection(RAW_DATA_SERIALIZATION + index, "RawDataFixed"); \
1206 } else /* NOLINT */
1207 COMMON_RAW_LENGTHS(RAW_CASE)
1208#undef RAW_CASE
1209 { /* NOLINT */
1210 sink_->Put(RAW_DATA_SERIALIZATION, "RawData");
1211 sink_->PutInt(skipped, "length");
1212 }
1213 for (int i = 0; i < skipped; i++) {
1214 unsigned int data = base[i];
1215 sink_->PutSection(data, "Byte");
1216 }
1217 bytes_processed_so_far_ += skipped;
1218 }
1219}
1220
1221
1222int Serializer::SpaceOfObject(HeapObject* object) {
1223 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1224 AllocationSpace s = static_cast<AllocationSpace>(i);
1225 if (Heap::InSpace(object, s)) {
1226 if (i == LO_SPACE) {
1227 if (object->IsCode()) {
1228 return kLargeCode;
1229 } else if (object->IsFixedArray()) {
1230 return kLargeFixedArray;
1231 } else {
1232 return kLargeData;
1233 }
1234 }
1235 return i;
1236 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001237 }
1238 UNREACHABLE();
Steve Blockd0582a62009-12-15 09:54:21 +00001239 return 0;
1240}
1241
1242
1243int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1244 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1245 AllocationSpace s = static_cast<AllocationSpace>(i);
1246 if (Heap::InSpace(object, s)) {
1247 return i;
1248 }
1249 }
1250 UNREACHABLE();
1251 return 0;
1252}
1253
1254
1255int Serializer::Allocate(int space, int size, bool* new_page) {
1256 CHECK(space >= 0 && space < kNumberOfSpaces);
1257 if (SpaceIsLarge(space)) {
1258 // In large object space we merely number the objects instead of trying to
1259 // determine some sort of address.
1260 *new_page = true;
Leon Clarkee46be812010-01-19 14:06:41 +00001261 large_object_total_ += size;
Steve Blockd0582a62009-12-15 09:54:21 +00001262 return fullness_[LO_SPACE]++;
1263 }
1264 *new_page = false;
1265 if (fullness_[space] == 0) {
1266 *new_page = true;
1267 }
1268 if (SpaceIsPaged(space)) {
1269 // Paged spaces are a little special. We encode their addresses as if the
1270 // pages were all contiguous and each page were filled up in the range
1271 // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1272 // and allocation does not start at offset 0 in the page, but this scheme
1273 // means the deserializer can get the page number quickly by shifting the
1274 // serialized address.
1275 CHECK(IsPowerOf2(Page::kPageSize));
1276 int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1277 CHECK(size <= Page::kObjectAreaSize);
1278 if (used_in_this_page + size > Page::kObjectAreaSize) {
1279 *new_page = true;
1280 fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1281 }
1282 }
1283 int allocation_address = fullness_[space];
1284 fullness_[space] = allocation_address + size;
1285 return allocation_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001286}
1287
1288
1289} } // namespace v8::internal