blob: 12e9613e3e1f7ffa3ecd57009ee6faef9f59779a [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 Blockd0582a62009-12-15 09:54:21 +000041#include "bootstrapper.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000042
43namespace v8 {
44namespace internal {
45
Steve Blocka7e24c12009-10-30 11:49:00 +000046
47// -----------------------------------------------------------------------------
48// Coding of external references.
49
50// The encoding of an external reference. The type is in the high word.
51// The id is in the low word.
52static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
53 return static_cast<uint32_t>(type) << 16 | id;
54}
55
56
57static int* GetInternalPointer(StatsCounter* counter) {
58 // All counters refer to dummy_counter, if deserializing happens without
59 // setting up counters.
60 static int dummy_counter = 0;
61 return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
62}
63
64
65// ExternalReferenceTable is a helper class that defines the relationship
66// between external references and their encodings. It is used to build
67// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
68class ExternalReferenceTable {
69 public:
Steve Block44f0eee2011-05-26 01:26:41 +010070 static ExternalReferenceTable* instance(Isolate* isolate) {
71 ExternalReferenceTable* external_reference_table =
72 isolate->external_reference_table();
73 if (external_reference_table == NULL) {
74 external_reference_table = new ExternalReferenceTable(isolate);
75 isolate->set_external_reference_table(external_reference_table);
76 }
77 return external_reference_table;
Steve Blocka7e24c12009-10-30 11:49:00 +000078 }
79
80 int size() const { return refs_.length(); }
81
82 Address address(int i) { return refs_[i].address; }
83
84 uint32_t code(int i) { return refs_[i].code; }
85
86 const char* name(int i) { return refs_[i].name; }
87
88 int max_id(int code) { return max_id_[code]; }
89
90 private:
Steve Block44f0eee2011-05-26 01:26:41 +010091 explicit ExternalReferenceTable(Isolate* isolate) : refs_(64) {
92 PopulateTable(isolate);
93 }
Steve Blocka7e24c12009-10-30 11:49:00 +000094 ~ExternalReferenceTable() { }
95
96 struct ExternalReferenceEntry {
97 Address address;
98 uint32_t code;
99 const char* name;
100 };
101
Steve Block44f0eee2011-05-26 01:26:41 +0100102 void PopulateTable(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000103
104 // For a few types of references, we can get their address from their id.
Steve Block44f0eee2011-05-26 01:26:41 +0100105 void AddFromId(TypeCode type,
106 uint16_t id,
107 const char* name,
108 Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000109
110 // For other types of references, the caller will figure out the address.
111 void Add(Address address, TypeCode type, uint16_t id, const char* name);
112
113 List<ExternalReferenceEntry> refs_;
114 int max_id_[kTypeCodeCount];
115};
116
117
Steve Blocka7e24c12009-10-30 11:49:00 +0000118void ExternalReferenceTable::AddFromId(TypeCode type,
119 uint16_t id,
Steve Block44f0eee2011-05-26 01:26:41 +0100120 const char* name,
121 Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 Address address;
123 switch (type) {
124 case C_BUILTIN: {
Steve Block44f0eee2011-05-26 01:26:41 +0100125 ExternalReference ref(static_cast<Builtins::CFunctionId>(id), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000126 address = ref.address();
127 break;
128 }
129 case BUILTIN: {
Steve Block44f0eee2011-05-26 01:26:41 +0100130 ExternalReference ref(static_cast<Builtins::Name>(id), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 address = ref.address();
132 break;
133 }
134 case RUNTIME_FUNCTION: {
Steve Block44f0eee2011-05-26 01:26:41 +0100135 ExternalReference ref(static_cast<Runtime::FunctionId>(id), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 address = ref.address();
137 break;
138 }
139 case IC_UTILITY: {
Steve Block44f0eee2011-05-26 01:26:41 +0100140 ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)),
141 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000142 address = ref.address();
143 break;
144 }
145 default:
146 UNREACHABLE();
147 return;
148 }
149 Add(address, type, id, name);
150}
151
152
153void ExternalReferenceTable::Add(Address address,
154 TypeCode type,
155 uint16_t id,
156 const char* name) {
Steve Blockd0582a62009-12-15 09:54:21 +0000157 ASSERT_NE(NULL, address);
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 ExternalReferenceEntry entry;
159 entry.address = address;
160 entry.code = EncodeExternal(type, id);
161 entry.name = name;
Steve Blockd0582a62009-12-15 09:54:21 +0000162 ASSERT_NE(0, entry.code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000163 refs_.Add(entry);
164 if (id > max_id_[type]) max_id_[type] = id;
165}
166
167
Steve Block44f0eee2011-05-26 01:26:41 +0100168void ExternalReferenceTable::PopulateTable(Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000169 for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
170 max_id_[type_code] = 0;
171 }
172
173 // The following populates all of the different type of external references
174 // into the ExternalReferenceTable.
175 //
176 // NOTE: This function was originally 100k of code. It has since been
177 // rewritten to be mostly table driven, as the callback macro style tends to
178 // very easily cause code bloat. Please be careful in the future when adding
179 // new references.
180
181 struct RefTableEntry {
182 TypeCode type;
183 uint16_t id;
184 const char* name;
185 };
186
187 static const RefTableEntry ref_table[] = {
188 // Builtins
Leon Clarkee46be812010-01-19 14:06:41 +0000189#define DEF_ENTRY_C(name, ignored) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000190 { C_BUILTIN, \
191 Builtins::c_##name, \
192 "Builtins::" #name },
193
194 BUILTIN_LIST_C(DEF_ENTRY_C)
195#undef DEF_ENTRY_C
196
Leon Clarkee46be812010-01-19 14:06:41 +0000197#define DEF_ENTRY_C(name, ignored) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 { BUILTIN, \
Steve Block44f0eee2011-05-26 01:26:41 +0100199 Builtins::k##name, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000200 "Builtins::" #name },
Steve Block1e0659c2011-05-24 12:43:12 +0100201#define DEF_ENTRY_A(name, kind, state, extra) DEF_ENTRY_C(name, ignored)
Steve Blocka7e24c12009-10-30 11:49:00 +0000202
203 BUILTIN_LIST_C(DEF_ENTRY_C)
204 BUILTIN_LIST_A(DEF_ENTRY_A)
205 BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
206#undef DEF_ENTRY_C
207#undef DEF_ENTRY_A
208
209 // Runtime functions
210#define RUNTIME_ENTRY(name, nargs, ressize) \
211 { RUNTIME_FUNCTION, \
212 Runtime::k##name, \
213 "Runtime::" #name },
214
215 RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
216#undef RUNTIME_ENTRY
217
218 // IC utilities
219#define IC_ENTRY(name) \
220 { IC_UTILITY, \
221 IC::k##name, \
222 "IC::" #name },
223
224 IC_UTIL_LIST(IC_ENTRY)
225#undef IC_ENTRY
226 }; // end of ref_table[].
227
228 for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
Steve Block44f0eee2011-05-26 01:26:41 +0100229 AddFromId(ref_table[i].type,
230 ref_table[i].id,
231 ref_table[i].name,
232 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 }
234
235#ifdef ENABLE_DEBUGGER_SUPPORT
236 // Debug addresses
Steve Block44f0eee2011-05-26 01:26:41 +0100237 Add(Debug_Address(Debug::k_after_break_target_address).address(isolate),
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 DEBUG_ADDRESS,
239 Debug::k_after_break_target_address << kDebugIdShift,
240 "Debug::after_break_target_address()");
Steve Block44f0eee2011-05-26 01:26:41 +0100241 Add(Debug_Address(Debug::k_debug_break_slot_address).address(isolate),
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100242 DEBUG_ADDRESS,
243 Debug::k_debug_break_slot_address << kDebugIdShift,
244 "Debug::debug_break_slot_address()");
Steve Block44f0eee2011-05-26 01:26:41 +0100245 Add(Debug_Address(Debug::k_debug_break_return_address).address(isolate),
Steve Blocka7e24c12009-10-30 11:49:00 +0000246 DEBUG_ADDRESS,
247 Debug::k_debug_break_return_address << kDebugIdShift,
248 "Debug::debug_break_return_address()");
Steve Block44f0eee2011-05-26 01:26:41 +0100249 Add(Debug_Address(Debug::k_restarter_frame_function_pointer).address(isolate),
Ben Murdochbb769b22010-08-11 14:56:33 +0100250 DEBUG_ADDRESS,
251 Debug::k_restarter_frame_function_pointer << kDebugIdShift,
252 "Debug::restarter_frame_function_pointer_address()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000253#endif
254
255 // Stat counters
256 struct StatsRefTableEntry {
Steve Block44f0eee2011-05-26 01:26:41 +0100257 StatsCounter* (Counters::*counter)();
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 uint16_t id;
259 const char* name;
260 };
261
Steve Block44f0eee2011-05-26 01:26:41 +0100262 const StatsRefTableEntry stats_ref_table[] = {
Steve Blocka7e24c12009-10-30 11:49:00 +0000263#define COUNTER_ENTRY(name, caption) \
Steve Block44f0eee2011-05-26 01:26:41 +0100264 { &Counters::name, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 Counters::k_##name, \
266 "Counters::" #name },
267
268 STATS_COUNTER_LIST_1(COUNTER_ENTRY)
269 STATS_COUNTER_LIST_2(COUNTER_ENTRY)
270#undef COUNTER_ENTRY
271 }; // end of stats_ref_table[].
272
Steve Block44f0eee2011-05-26 01:26:41 +0100273 Counters* counters = isolate->counters();
Steve Blocka7e24c12009-10-30 11:49:00 +0000274 for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
Steve Block44f0eee2011-05-26 01:26:41 +0100275 Add(reinterpret_cast<Address>(GetInternalPointer(
276 (counters->*(stats_ref_table[i].counter))())),
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 STATS_COUNTER,
278 stats_ref_table[i].id,
279 stats_ref_table[i].name);
280 }
281
282 // Top addresses
Steve Block3ce2e202009-11-05 08:53:23 +0000283
284 const char* AddressNames[] = {
Steve Block44f0eee2011-05-26 01:26:41 +0100285#define C(name) "Isolate::" #name,
286 ISOLATE_ADDRESS_LIST(C)
287 ISOLATE_ADDRESS_LIST_PROF(C)
Steve Block3ce2e202009-11-05 08:53:23 +0000288 NULL
289#undef C
290 };
291
Steve Block44f0eee2011-05-26 01:26:41 +0100292 for (uint16_t i = 0; i < Isolate::k_isolate_address_count; ++i) {
293 Add(isolate->get_address_from_id((Isolate::AddressId)i),
294 TOP_ADDRESS, i, AddressNames[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000295 }
296
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 // Accessors
298#define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
299 Add((Address)&Accessors::name, \
300 ACCESSOR, \
301 Accessors::k##name, \
302 "Accessors::" #name);
303
304 ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
305#undef ACCESSOR_DESCRIPTOR_DECLARATION
306
Steve Block44f0eee2011-05-26 01:26:41 +0100307 StubCache* stub_cache = isolate->stub_cache();
308
Steve Blocka7e24c12009-10-30 11:49:00 +0000309 // Stub cache tables
Steve Block44f0eee2011-05-26 01:26:41 +0100310 Add(stub_cache->key_reference(StubCache::kPrimary).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 STUB_CACHE_TABLE,
312 1,
313 "StubCache::primary_->key");
Steve Block44f0eee2011-05-26 01:26:41 +0100314 Add(stub_cache->value_reference(StubCache::kPrimary).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 STUB_CACHE_TABLE,
316 2,
317 "StubCache::primary_->value");
Steve Block44f0eee2011-05-26 01:26:41 +0100318 Add(stub_cache->key_reference(StubCache::kSecondary).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 STUB_CACHE_TABLE,
320 3,
321 "StubCache::secondary_->key");
Steve Block44f0eee2011-05-26 01:26:41 +0100322 Add(stub_cache->value_reference(StubCache::kSecondary).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 STUB_CACHE_TABLE,
324 4,
325 "StubCache::secondary_->value");
326
327 // Runtime entries
Steve Block44f0eee2011-05-26 01:26:41 +0100328 Add(ExternalReference::perform_gc_function(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 RUNTIME_ENTRY,
330 1,
331 "Runtime::PerformGC");
Steve Block44f0eee2011-05-26 01:26:41 +0100332 Add(ExternalReference::fill_heap_number_with_random_function(
333 isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 RUNTIME_ENTRY,
335 2,
Steve Block6ded16b2010-05-10 14:33:55 +0100336 "V8::FillHeapNumberWithRandom");
Steve Block44f0eee2011-05-26 01:26:41 +0100337 Add(ExternalReference::random_uint32_function(isolate).address(),
Steve Block6ded16b2010-05-10 14:33:55 +0100338 RUNTIME_ENTRY,
339 3,
340 "V8::Random");
Steve Block44f0eee2011-05-26 01:26:41 +0100341 Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
John Reck59135872010-11-02 12:39:01 -0700342 RUNTIME_ENTRY,
Steve Block1e0659c2011-05-24 12:43:12 +0100343 4,
John Reck59135872010-11-02 12:39:01 -0700344 "HandleScope::DeleteExtensions");
345
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 // Miscellaneous
Steve Block44f0eee2011-05-26 01:26:41 +0100347 Add(ExternalReference::the_hole_value_location(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 UNCLASSIFIED,
349 2,
350 "Factory::the_hole_value().location()");
Steve Block44f0eee2011-05-26 01:26:41 +0100351 Add(ExternalReference::roots_address(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000352 UNCLASSIFIED,
353 3,
354 "Heap::roots_address()");
Steve Block44f0eee2011-05-26 01:26:41 +0100355 Add(ExternalReference::address_of_stack_limit(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000356 UNCLASSIFIED,
357 4,
358 "StackGuard::address_of_jslimit()");
Steve Block44f0eee2011-05-26 01:26:41 +0100359 Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000360 UNCLASSIFIED,
361 5,
Steve Blockd0582a62009-12-15 09:54:21 +0000362 "StackGuard::address_of_real_jslimit()");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100363#ifndef V8_INTERPRETED_REGEXP
Steve Block44f0eee2011-05-26 01:26:41 +0100364 Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
Steve Blockd0582a62009-12-15 09:54:21 +0000365 UNCLASSIFIED,
366 6,
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 "RegExpStack::limit_address()");
Steve Block44f0eee2011-05-26 01:26:41 +0100368 Add(ExternalReference::address_of_regexp_stack_memory_address(
369 isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000370 UNCLASSIFIED,
Steve Blockd0582a62009-12-15 09:54:21 +0000371 7,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100372 "RegExpStack::memory_address()");
Steve Block44f0eee2011-05-26 01:26:41 +0100373 Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100374 UNCLASSIFIED,
375 8,
376 "RegExpStack::memory_size()");
Steve Block44f0eee2011-05-26 01:26:41 +0100377 Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100378 UNCLASSIFIED,
379 9,
380 "OffsetsVector::static_offsets_vector");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100381#endif // V8_INTERPRETED_REGEXP
Steve Block44f0eee2011-05-26 01:26:41 +0100382 Add(ExternalReference::new_space_start(isolate).address(),
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100383 UNCLASSIFIED,
384 10,
Steve Blocka7e24c12009-10-30 11:49:00 +0000385 "Heap::NewSpaceStart()");
Steve Block44f0eee2011-05-26 01:26:41 +0100386 Add(ExternalReference::new_space_mask(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100388 11,
Andrei Popescu402d9372010-02-26 13:31:12 +0000389 "Heap::NewSpaceMask()");
Steve Block44f0eee2011-05-26 01:26:41 +0100390 Add(ExternalReference::heap_always_allocate_scope_depth(isolate).address(),
Andrei Popescu402d9372010-02-26 13:31:12 +0000391 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100392 12,
Steve Blocka7e24c12009-10-30 11:49:00 +0000393 "Heap::always_allocate_scope_depth()");
Steve Block44f0eee2011-05-26 01:26:41 +0100394 Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100396 13,
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 "Heap::NewSpaceAllocationLimitAddress()");
Steve Block44f0eee2011-05-26 01:26:41 +0100398 Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100400 14,
Steve Blocka7e24c12009-10-30 11:49:00 +0000401 "Heap::NewSpaceAllocationTopAddress()");
402#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100403 Add(ExternalReference::debug_break(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100405 15,
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 "Debug::Break()");
Steve Block44f0eee2011-05-26 01:26:41 +0100407 Add(ExternalReference::debug_step_in_fp_address(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100409 16,
Steve Blocka7e24c12009-10-30 11:49:00 +0000410 "Debug::step_in_fp_addr()");
411#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100412 Add(ExternalReference::double_fp_operation(Token::ADD, isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100414 17,
Steve Blocka7e24c12009-10-30 11:49:00 +0000415 "add_two_doubles");
Steve Block44f0eee2011-05-26 01:26:41 +0100416 Add(ExternalReference::double_fp_operation(Token::SUB, isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100418 18,
Steve Blocka7e24c12009-10-30 11:49:00 +0000419 "sub_two_doubles");
Steve Block44f0eee2011-05-26 01:26:41 +0100420 Add(ExternalReference::double_fp_operation(Token::MUL, isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000421 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100422 19,
Steve Blocka7e24c12009-10-30 11:49:00 +0000423 "mul_two_doubles");
Steve Block44f0eee2011-05-26 01:26:41 +0100424 Add(ExternalReference::double_fp_operation(Token::DIV, isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100426 20,
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 "div_two_doubles");
Steve Block44f0eee2011-05-26 01:26:41 +0100428 Add(ExternalReference::double_fp_operation(Token::MOD, isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100430 21,
Steve Blocka7e24c12009-10-30 11:49:00 +0000431 "mod_two_doubles");
Steve Block44f0eee2011-05-26 01:26:41 +0100432 Add(ExternalReference::compare_doubles(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000433 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100434 22,
Steve Blocka7e24c12009-10-30 11:49:00 +0000435 "compare_doubles");
Steve Block6ded16b2010-05-10 14:33:55 +0100436#ifndef V8_INTERPRETED_REGEXP
Steve Block44f0eee2011-05-26 01:26:41 +0100437 Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
Steve Block6ded16b2010-05-10 14:33:55 +0100438 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100439 23,
Steve Blocka7e24c12009-10-30 11:49:00 +0000440 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
Steve Block44f0eee2011-05-26 01:26:41 +0100441 Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000442 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100443 24,
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 "RegExpMacroAssembler*::CheckStackGuardState()");
Steve Block44f0eee2011-05-26 01:26:41 +0100445 Add(ExternalReference::re_grow_stack(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000446 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100447 25,
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 "NativeRegExpMacroAssembler::GrowStack()");
Leon Clarkee46be812010-01-19 14:06:41 +0000449 Add(ExternalReference::re_word_character_map().address(),
450 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100451 26,
Leon Clarkee46be812010-01-19 14:06:41 +0000452 "NativeRegExpMacroAssembler::word_character_map");
Steve Block6ded16b2010-05-10 14:33:55 +0100453#endif // V8_INTERPRETED_REGEXP
Leon Clarkee46be812010-01-19 14:06:41 +0000454 // Keyed lookup cache.
Steve Block44f0eee2011-05-26 01:26:41 +0100455 Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(),
Leon Clarkee46be812010-01-19 14:06:41 +0000456 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100457 27,
Leon Clarkee46be812010-01-19 14:06:41 +0000458 "KeyedLookupCache::keys()");
Steve Block44f0eee2011-05-26 01:26:41 +0100459 Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(),
Leon Clarkee46be812010-01-19 14:06:41 +0000460 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100461 28,
Leon Clarkee46be812010-01-19 14:06:41 +0000462 "KeyedLookupCache::field_offsets()");
Steve Block44f0eee2011-05-26 01:26:41 +0100463 Add(ExternalReference::transcendental_cache_array_address(isolate).address(),
Andrei Popescu402d9372010-02-26 13:31:12 +0000464 UNCLASSIFIED,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100465 29,
Andrei Popescu402d9372010-02-26 13:31:12 +0000466 "TranscendentalCache::caches()");
John Reck59135872010-11-02 12:39:01 -0700467 Add(ExternalReference::handle_scope_next_address().address(),
468 UNCLASSIFIED,
469 30,
470 "HandleScope::next");
471 Add(ExternalReference::handle_scope_limit_address().address(),
472 UNCLASSIFIED,
473 31,
474 "HandleScope::limit");
475 Add(ExternalReference::handle_scope_level_address().address(),
476 UNCLASSIFIED,
477 32,
478 "HandleScope::level");
Steve Block44f0eee2011-05-26 01:26:41 +0100479 Add(ExternalReference::new_deoptimizer_function(isolate).address(),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100480 UNCLASSIFIED,
481 33,
482 "Deoptimizer::New()");
Steve Block44f0eee2011-05-26 01:26:41 +0100483 Add(ExternalReference::compute_output_frames_function(isolate).address(),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100484 UNCLASSIFIED,
485 34,
486 "Deoptimizer::ComputeOutputFrames()");
487 Add(ExternalReference::address_of_min_int().address(),
488 UNCLASSIFIED,
489 35,
490 "LDoubleConstant::min_int");
491 Add(ExternalReference::address_of_one_half().address(),
492 UNCLASSIFIED,
493 36,
494 "LDoubleConstant::one_half");
Steve Block44f0eee2011-05-26 01:26:41 +0100495 Add(ExternalReference::isolate_address().address(),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100496 UNCLASSIFIED,
497 37,
Steve Block44f0eee2011-05-26 01:26:41 +0100498 "isolate");
499 Add(ExternalReference::address_of_minus_zero().address(),
500 UNCLASSIFIED,
501 38,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100502 "LDoubleConstant::minus_zero");
503 Add(ExternalReference::address_of_negative_infinity().address(),
504 UNCLASSIFIED,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100505 39,
Steve Block44f0eee2011-05-26 01:26:41 +0100506 "LDoubleConstant::negative_infinity");
507 Add(ExternalReference::power_double_double_function(isolate).address(),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100508 UNCLASSIFIED,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100509 40,
Steve Block44f0eee2011-05-26 01:26:41 +0100510 "power_double_double_function");
511 Add(ExternalReference::power_double_int_function(isolate).address(),
Ben Murdoch086aeea2011-05-13 15:57:08 +0100512 UNCLASSIFIED,
Steve Block1e0659c2011-05-24 12:43:12 +0100513 41,
Steve Block44f0eee2011-05-26 01:26:41 +0100514 "power_double_int_function");
515 Add(ExternalReference::arguments_marker_location(isolate).address(),
516 UNCLASSIFIED,
517 42,
Ben Murdoch086aeea2011-05-13 15:57:08 +0100518 "Factory::arguments_marker().location()");
Steve Blocka7e24c12009-10-30 11:49:00 +0000519}
520
521
522ExternalReferenceEncoder::ExternalReferenceEncoder()
Steve Block44f0eee2011-05-26 01:26:41 +0100523 : encodings_(Match),
524 isolate_(Isolate::Current()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000525 ExternalReferenceTable* external_references =
Steve Block44f0eee2011-05-26 01:26:41 +0100526 ExternalReferenceTable::instance(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 for (int i = 0; i < external_references->size(); ++i) {
528 Put(external_references->address(i), i);
529 }
530}
531
532
533uint32_t ExternalReferenceEncoder::Encode(Address key) const {
534 int index = IndexOf(key);
Ben Murdochbb769b22010-08-11 14:56:33 +0100535 ASSERT(key == NULL || index >= 0);
Steve Block44f0eee2011-05-26 01:26:41 +0100536 return index >=0 ?
537 ExternalReferenceTable::instance(isolate_)->code(index) : 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000538}
539
540
541const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
542 int index = IndexOf(key);
Steve Block44f0eee2011-05-26 01:26:41 +0100543 return index >= 0 ?
544 ExternalReferenceTable::instance(isolate_)->name(index) : NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000545}
546
547
548int ExternalReferenceEncoder::IndexOf(Address key) const {
549 if (key == NULL) return -1;
550 HashMap::Entry* entry =
Steve Block44f0eee2011-05-26 01:26:41 +0100551 const_cast<HashMap&>(encodings_).Lookup(key, Hash(key), false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000552 return entry == NULL
553 ? -1
554 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
555}
556
557
558void ExternalReferenceEncoder::Put(Address key, int index) {
559 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
Steve Block6ded16b2010-05-10 14:33:55 +0100560 entry->value = reinterpret_cast<void*>(index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000561}
562
563
564ExternalReferenceDecoder::ExternalReferenceDecoder()
Steve Block44f0eee2011-05-26 01:26:41 +0100565 : encodings_(NewArray<Address*>(kTypeCodeCount)),
566 isolate_(Isolate::Current()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000567 ExternalReferenceTable* external_references =
Steve Block44f0eee2011-05-26 01:26:41 +0100568 ExternalReferenceTable::instance(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000569 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
570 int max = external_references->max_id(type) + 1;
571 encodings_[type] = NewArray<Address>(max + 1);
572 }
573 for (int i = 0; i < external_references->size(); ++i) {
574 Put(external_references->code(i), external_references->address(i));
575 }
576}
577
578
579ExternalReferenceDecoder::~ExternalReferenceDecoder() {
580 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
581 DeleteArray(encodings_[type]);
582 }
583 DeleteArray(encodings_);
584}
585
586
Steve Blocka7e24c12009-10-30 11:49:00 +0000587bool Serializer::serialization_enabled_ = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000588bool Serializer::too_late_to_enable_now_ = false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000589
590
Steve Block44f0eee2011-05-26 01:26:41 +0100591Deserializer::Deserializer(SnapshotByteSource* source)
592 : isolate_(NULL),
593 source_(source),
594 external_reference_decoder_(NULL) {
Steve Blockd0582a62009-12-15 09:54:21 +0000595}
596
597
598// This routine both allocates a new object, and also keeps
599// track of where objects have been allocated so that we can
600// fix back references when deserializing.
601Address Deserializer::Allocate(int space_index, Space* space, int size) {
602 Address address;
603 if (!SpaceIsLarge(space_index)) {
604 ASSERT(!SpaceIsPaged(space_index) ||
605 size <= Page::kPageSize - Page::kObjectStartOffset);
John Reck59135872010-11-02 12:39:01 -0700606 MaybeObject* maybe_new_allocation;
Steve Blockd0582a62009-12-15 09:54:21 +0000607 if (space_index == NEW_SPACE) {
John Reck59135872010-11-02 12:39:01 -0700608 maybe_new_allocation =
609 reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
Steve Blockd0582a62009-12-15 09:54:21 +0000610 } else {
John Reck59135872010-11-02 12:39:01 -0700611 maybe_new_allocation =
612 reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
Steve Blockd0582a62009-12-15 09:54:21 +0000613 }
John Reck59135872010-11-02 12:39:01 -0700614 Object* new_allocation = maybe_new_allocation->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000615 HeapObject* new_object = HeapObject::cast(new_allocation);
Steve Blockd0582a62009-12-15 09:54:21 +0000616 address = new_object->address();
617 high_water_[space_index] = address + size;
618 } else {
619 ASSERT(SpaceIsLarge(space_index));
Steve Blockd0582a62009-12-15 09:54:21 +0000620 LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
621 Object* new_allocation;
622 if (space_index == kLargeData) {
John Reck59135872010-11-02 12:39:01 -0700623 new_allocation = lo_space->AllocateRaw(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000624 } else if (space_index == kLargeFixedArray) {
John Reck59135872010-11-02 12:39:01 -0700625 new_allocation =
626 lo_space->AllocateRawFixedArray(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000627 } else {
628 ASSERT_EQ(kLargeCode, space_index);
John Reck59135872010-11-02 12:39:01 -0700629 new_allocation = lo_space->AllocateRawCode(size)->ToObjectUnchecked();
Steve Blockd0582a62009-12-15 09:54:21 +0000630 }
Steve Blockd0582a62009-12-15 09:54:21 +0000631 HeapObject* new_object = HeapObject::cast(new_allocation);
632 // Record all large objects in the same space.
633 address = new_object->address();
Andrei Popescu31002712010-02-23 13:46:05 +0000634 pages_[LO_SPACE].Add(address);
Steve Blockd0582a62009-12-15 09:54:21 +0000635 }
636 last_object_address_ = address;
637 return address;
638}
639
640
641// This returns the address of an object that has been described in the
642// snapshot as being offset bytes back in a particular space.
643HeapObject* Deserializer::GetAddressFromEnd(int space) {
644 int offset = source_->GetInt();
645 ASSERT(!SpaceIsLarge(space));
646 offset <<= kObjectAlignmentBits;
647 return HeapObject::FromAddress(high_water_[space] - offset);
648}
649
650
651// This returns the address of an object that has been described in the
652// snapshot as being offset bytes into a particular space.
653HeapObject* Deserializer::GetAddressFromStart(int space) {
654 int offset = source_->GetInt();
655 if (SpaceIsLarge(space)) {
656 // Large spaces have one object per 'page'.
657 return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
658 }
659 offset <<= kObjectAlignmentBits;
660 if (space == NEW_SPACE) {
661 // New space has only one space - numbered 0.
662 return HeapObject::FromAddress(pages_[space][0] + offset);
663 }
664 ASSERT(SpaceIsPaged(space));
Leon Clarkee46be812010-01-19 14:06:41 +0000665 int page_of_pointee = offset >> kPageSizeBits;
Steve Blockd0582a62009-12-15 09:54:21 +0000666 Address object_address = pages_[space][page_of_pointee] +
667 (offset & Page::kPageAlignmentMask);
668 return HeapObject::FromAddress(object_address);
669}
670
671
672void Deserializer::Deserialize() {
Steve Block44f0eee2011-05-26 01:26:41 +0100673 isolate_ = Isolate::Current();
Steve Blockd0582a62009-12-15 09:54:21 +0000674 // Don't GC while deserializing - just expand the heap.
675 AlwaysAllocateScope always_allocate;
676 // Don't use the free lists while deserializing.
677 LinearAllocationScope allocate_linearly;
678 // No active threads.
Steve Block44f0eee2011-05-26 01:26:41 +0100679 ASSERT_EQ(NULL, isolate_->thread_manager()->FirstThreadStateInUse());
Steve Blockd0582a62009-12-15 09:54:21 +0000680 // No active handles.
Steve Block44f0eee2011-05-26 01:26:41 +0100681 ASSERT(isolate_->handle_scope_implementer()->blocks()->is_empty());
Leon Clarked91b9f72010-01-27 17:25:45 +0000682 // Make sure the entire partial snapshot cache is traversed, filling it with
683 // valid object pointers.
Steve Block44f0eee2011-05-26 01:26:41 +0100684 isolate_->set_serialize_partial_snapshot_cache_length(
685 Isolate::kPartialSnapshotCacheCapacity);
Steve Blockd0582a62009-12-15 09:54:21 +0000686 ASSERT_EQ(NULL, external_reference_decoder_);
687 external_reference_decoder_ = new ExternalReferenceDecoder();
Steve Block44f0eee2011-05-26 01:26:41 +0100688 isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
689 isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
Ben Murdochf87a2032010-10-22 12:50:53 +0100690
Steve Block44f0eee2011-05-26 01:26:41 +0100691 isolate_->heap()->set_global_contexts_list(
692 isolate_->heap()->undefined_value());
Leon Clarkee46be812010-01-19 14:06:41 +0000693}
694
695
696void Deserializer::DeserializePartial(Object** root) {
Steve Block44f0eee2011-05-26 01:26:41 +0100697 isolate_ = Isolate::Current();
Leon Clarkee46be812010-01-19 14:06:41 +0000698 // Don't GC while deserializing - just expand the heap.
699 AlwaysAllocateScope always_allocate;
700 // Don't use the free lists while deserializing.
701 LinearAllocationScope allocate_linearly;
702 if (external_reference_decoder_ == NULL) {
703 external_reference_decoder_ = new ExternalReferenceDecoder();
704 }
705 VisitPointer(root);
706}
707
708
Leon Clarked91b9f72010-01-27 17:25:45 +0000709Deserializer::~Deserializer() {
710 ASSERT(source_->AtEOF());
Steve Block44f0eee2011-05-26 01:26:41 +0100711 if (external_reference_decoder_) {
Leon Clarkee46be812010-01-19 14:06:41 +0000712 delete external_reference_decoder_;
713 external_reference_decoder_ = NULL;
714 }
Steve Blockd0582a62009-12-15 09:54:21 +0000715}
716
717
718// This is called on the roots. It is the driver of the deserialization
719// process. It is also called on the body of each function.
720void Deserializer::VisitPointers(Object** start, Object** end) {
721 // The space must be new space. Any other space would cause ReadChunk to try
722 // to update the remembered using NULL as the address.
723 ReadChunk(start, end, NEW_SPACE, NULL);
724}
725
726
727// This routine writes the new object into the pointer provided and then
728// returns true if the new object was in young space and false otherwise.
729// The reason for this strange interface is that otherwise the object is
730// written very late, which means the ByteArray map is not set up by the
731// time we need to use it to mark the space at the end of a page free (by
732// making it into a byte array).
733void Deserializer::ReadObject(int space_number,
734 Space* space,
735 Object** write_back) {
736 int size = source_->GetInt() << kObjectAlignmentBits;
737 Address address = Allocate(space_number, space, size);
738 *write_back = HeapObject::FromAddress(address);
739 Object** current = reinterpret_cast<Object**>(address);
740 Object** limit = current + (size >> kPointerSizeLog2);
Leon Clarkee46be812010-01-19 14:06:41 +0000741 if (FLAG_log_snapshot_positions) {
Steve Block44f0eee2011-05-26 01:26:41 +0100742 LOG(isolate_, SnapshotPositionEvent(address, source_->position()));
Leon Clarkee46be812010-01-19 14:06:41 +0000743 }
Steve Blockd0582a62009-12-15 09:54:21 +0000744 ReadChunk(current, limit, space_number, address);
Steve Block44f0eee2011-05-26 01:26:41 +0100745#ifdef DEBUG
746 bool is_codespace = (space == HEAP->code_space()) ||
747 ((space == HEAP->lo_space()) && (space_number == kLargeCode));
748 ASSERT(HeapObject::FromAddress(address)->IsCode() == is_codespace);
749#endif
Steve Blockd0582a62009-12-15 09:54:21 +0000750}
751
752
Leon Clarkef7060e22010-06-03 12:02:55 +0100753// This macro is always used with a constant argument so it should all fold
754// away to almost nothing in the generated code. It might be nicer to do this
755// with the ternary operator but there are type issues with that.
756#define ASSIGN_DEST_SPACE(space_number) \
757 Space* dest_space; \
758 if (space_number == NEW_SPACE) { \
Steve Block44f0eee2011-05-26 01:26:41 +0100759 dest_space = isolate->heap()->new_space(); \
Leon Clarkef7060e22010-06-03 12:02:55 +0100760 } else if (space_number == OLD_POINTER_SPACE) { \
Steve Block44f0eee2011-05-26 01:26:41 +0100761 dest_space = isolate->heap()->old_pointer_space(); \
Leon Clarkef7060e22010-06-03 12:02:55 +0100762 } else if (space_number == OLD_DATA_SPACE) { \
Steve Block44f0eee2011-05-26 01:26:41 +0100763 dest_space = isolate->heap()->old_data_space(); \
Leon Clarkef7060e22010-06-03 12:02:55 +0100764 } else if (space_number == CODE_SPACE) { \
Steve Block44f0eee2011-05-26 01:26:41 +0100765 dest_space = isolate->heap()->code_space(); \
Leon Clarkef7060e22010-06-03 12:02:55 +0100766 } else if (space_number == MAP_SPACE) { \
Steve Block44f0eee2011-05-26 01:26:41 +0100767 dest_space = isolate->heap()->map_space(); \
Leon Clarkef7060e22010-06-03 12:02:55 +0100768 } else if (space_number == CELL_SPACE) { \
Steve Block44f0eee2011-05-26 01:26:41 +0100769 dest_space = isolate->heap()->cell_space(); \
Leon Clarkef7060e22010-06-03 12:02:55 +0100770 } else { \
771 ASSERT(space_number >= LO_SPACE); \
Steve Block44f0eee2011-05-26 01:26:41 +0100772 dest_space = isolate->heap()->lo_space(); \
Leon Clarkef7060e22010-06-03 12:02:55 +0100773 }
774
775
776static const int kUnknownOffsetFromStart = -1;
Steve Blockd0582a62009-12-15 09:54:21 +0000777
778
779void Deserializer::ReadChunk(Object** current,
780 Object** limit,
Leon Clarkef7060e22010-06-03 12:02:55 +0100781 int source_space,
Steve Blockd0582a62009-12-15 09:54:21 +0000782 Address address) {
Steve Block44f0eee2011-05-26 01:26:41 +0100783 Isolate* const isolate = isolate_;
Steve Blockd0582a62009-12-15 09:54:21 +0000784 while (current < limit) {
785 int data = source_->Get();
786 switch (data) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100787#define CASE_STATEMENT(where, how, within, space_number) \
788 case where + how + within + space_number: \
789 ASSERT((where & ~kPointedToMask) == 0); \
790 ASSERT((how & ~kHowToCodeMask) == 0); \
791 ASSERT((within & ~kWhereToPointMask) == 0); \
792 ASSERT((space_number & ~kSpaceMask) == 0);
793
794#define CASE_BODY(where, how, within, space_number_if_any, offset_from_start) \
795 { \
796 bool emit_write_barrier = false; \
797 bool current_was_incremented = false; \
798 int space_number = space_number_if_any == kAnyOldSpace ? \
799 (data & kSpaceMask) : space_number_if_any; \
800 if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
801 ASSIGN_DEST_SPACE(space_number) \
802 ReadObject(space_number, dest_space, current); \
803 emit_write_barrier = \
804 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
805 } else { \
806 Object* new_object = NULL; /* May not be a real Object pointer. */ \
807 if (where == kNewObject) { \
808 ASSIGN_DEST_SPACE(space_number) \
809 ReadObject(space_number, dest_space, &new_object); \
810 } else if (where == kRootArray) { \
811 int root_id = source_->GetInt(); \
Steve Block44f0eee2011-05-26 01:26:41 +0100812 new_object = isolate->heap()->roots_address()[root_id]; \
Leon Clarkef7060e22010-06-03 12:02:55 +0100813 } else if (where == kPartialSnapshotCache) { \
814 int cache_index = source_->GetInt(); \
Steve Block44f0eee2011-05-26 01:26:41 +0100815 new_object = isolate->serialize_partial_snapshot_cache() \
816 [cache_index]; \
Leon Clarkef7060e22010-06-03 12:02:55 +0100817 } else if (where == kExternalReference) { \
818 int reference_id = source_->GetInt(); \
Steve Block44f0eee2011-05-26 01:26:41 +0100819 Address address = external_reference_decoder_-> \
820 Decode(reference_id); \
Leon Clarkef7060e22010-06-03 12:02:55 +0100821 new_object = reinterpret_cast<Object*>(address); \
822 } else if (where == kBackref) { \
823 emit_write_barrier = \
824 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
825 new_object = GetAddressFromEnd(data & kSpaceMask); \
826 } else { \
827 ASSERT(where == kFromStart); \
828 if (offset_from_start == kUnknownOffsetFromStart) { \
829 emit_write_barrier = \
830 (space_number == NEW_SPACE && source_space != NEW_SPACE); \
831 new_object = GetAddressFromStart(data & kSpaceMask); \
832 } else { \
833 Address object_address = pages_[space_number][0] + \
834 (offset_from_start << kObjectAlignmentBits); \
835 new_object = HeapObject::FromAddress(object_address); \
836 } \
837 } \
838 if (within == kFirstInstruction) { \
839 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
840 new_object = reinterpret_cast<Object*>( \
841 new_code_object->instruction_start()); \
842 } \
843 if (how == kFromCode) { \
844 Address location_of_branch_data = \
845 reinterpret_cast<Address>(current); \
846 Assembler::set_target_at(location_of_branch_data, \
847 reinterpret_cast<Address>(new_object)); \
848 if (within == kFirstInstruction) { \
849 location_of_branch_data += Assembler::kCallTargetSize; \
850 current = reinterpret_cast<Object**>(location_of_branch_data); \
851 current_was_incremented = true; \
852 } \
853 } else { \
854 *current = new_object; \
855 } \
856 } \
857 if (emit_write_barrier) { \
Steve Block44f0eee2011-05-26 01:26:41 +0100858 isolate->heap()->RecordWrite(address, static_cast<int>( \
Leon Clarkef7060e22010-06-03 12:02:55 +0100859 reinterpret_cast<Address>(current) - address)); \
860 } \
861 if (!current_was_incremented) { \
862 current++; /* Increment current if it wasn't done above. */ \
863 } \
864 break; \
865 } \
866
867// This generates a case and a body for each space. The large object spaces are
868// very rare in snapshots so they are grouped in one body.
869#define ONE_PER_SPACE(where, how, within) \
870 CASE_STATEMENT(where, how, within, NEW_SPACE) \
871 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
872 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
873 CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart) \
874 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
875 CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart) \
876 CASE_STATEMENT(where, how, within, CODE_SPACE) \
877 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
878 CASE_STATEMENT(where, how, within, CELL_SPACE) \
879 CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart) \
880 CASE_STATEMENT(where, how, within, MAP_SPACE) \
881 CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart) \
882 CASE_STATEMENT(where, how, within, kLargeData) \
883 CASE_STATEMENT(where, how, within, kLargeCode) \
884 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
885 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
886
887// This generates a case and a body for the new space (which has to do extra
888// write barrier handling) and handles the other spaces with 8 fall-through
889// cases and one body.
890#define ALL_SPACES(where, how, within) \
891 CASE_STATEMENT(where, how, within, NEW_SPACE) \
892 CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
893 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
894 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
895 CASE_STATEMENT(where, how, within, CODE_SPACE) \
896 CASE_STATEMENT(where, how, within, CELL_SPACE) \
897 CASE_STATEMENT(where, how, within, MAP_SPACE) \
898 CASE_STATEMENT(where, how, within, kLargeData) \
899 CASE_STATEMENT(where, how, within, kLargeCode) \
900 CASE_STATEMENT(where, how, within, kLargeFixedArray) \
901 CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
902
Steve Block791712a2010-08-27 10:21:07 +0100903#define ONE_PER_CODE_SPACE(where, how, within) \
904 CASE_STATEMENT(where, how, within, CODE_SPACE) \
905 CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
906 CASE_STATEMENT(where, how, within, kLargeCode) \
Steve Block44f0eee2011-05-26 01:26:41 +0100907 CASE_BODY(where, how, within, kLargeCode, kUnknownOffsetFromStart)
Steve Block791712a2010-08-27 10:21:07 +0100908
Leon Clarkef7060e22010-06-03 12:02:55 +0100909#define EMIT_COMMON_REFERENCE_PATTERNS(pseudo_space_number, \
910 space_number, \
911 offset_from_start) \
912 CASE_STATEMENT(kFromStart, kPlain, kStartOfObject, pseudo_space_number) \
913 CASE_BODY(kFromStart, kPlain, kStartOfObject, space_number, offset_from_start)
914
915 // We generate 15 cases and bodies that process special tags that combine
916 // the raw data tag and the length into one byte.
Steve Blockd0582a62009-12-15 09:54:21 +0000917#define RAW_CASE(index, size) \
Leon Clarkef7060e22010-06-03 12:02:55 +0100918 case kRawData + index: { \
Steve Blockd0582a62009-12-15 09:54:21 +0000919 byte* raw_data_out = reinterpret_cast<byte*>(current); \
920 source_->CopyRaw(raw_data_out, size); \
921 current = reinterpret_cast<Object**>(raw_data_out + size); \
922 break; \
923 }
924 COMMON_RAW_LENGTHS(RAW_CASE)
925#undef RAW_CASE
Leon Clarkef7060e22010-06-03 12:02:55 +0100926
927 // Deserialize a chunk of raw data that doesn't have one of the popular
928 // lengths.
929 case kRawData: {
Steve Blockd0582a62009-12-15 09:54:21 +0000930 int size = source_->GetInt();
931 byte* raw_data_out = reinterpret_cast<byte*>(current);
932 source_->CopyRaw(raw_data_out, size);
933 current = reinterpret_cast<Object**>(raw_data_out + size);
934 break;
935 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100936
937 // Deserialize a new object and write a pointer to it to the current
938 // object.
939 ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100940 // Support for direct instruction pointers in functions
941 ONE_PER_CODE_SPACE(kNewObject, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100942 // Deserialize a new code object and write a pointer to its first
943 // instruction to the current code object.
944 ONE_PER_SPACE(kNewObject, kFromCode, kFirstInstruction)
945 // Find a recently deserialized object using its offset from the current
946 // allocation point and write a pointer to it to the current object.
947 ALL_SPACES(kBackref, kPlain, kStartOfObject)
948 // Find a recently deserialized code object using its offset from the
949 // current allocation point and write a pointer to its first instruction
Steve Block791712a2010-08-27 10:21:07 +0100950 // to the current code object or the instruction pointer in a function
951 // object.
Leon Clarkef7060e22010-06-03 12:02:55 +0100952 ALL_SPACES(kBackref, kFromCode, kFirstInstruction)
Steve Block791712a2010-08-27 10:21:07 +0100953 ALL_SPACES(kBackref, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100954 // Find an already deserialized object using its offset from the start
955 // and write a pointer to it to the current object.
956 ALL_SPACES(kFromStart, kPlain, kStartOfObject)
Steve Block791712a2010-08-27 10:21:07 +0100957 ALL_SPACES(kFromStart, kPlain, kFirstInstruction)
Leon Clarkef7060e22010-06-03 12:02:55 +0100958 // Find an already deserialized code object using its offset from the
959 // start and write a pointer to its first instruction to the current code
960 // object.
961 ALL_SPACES(kFromStart, kFromCode, kFirstInstruction)
962 // Find an already deserialized object at one of the predetermined popular
963 // offsets from the start and write a pointer to it in the current object.
964 COMMON_REFERENCE_PATTERNS(EMIT_COMMON_REFERENCE_PATTERNS)
965 // Find an object in the roots array and write a pointer to it to the
966 // current object.
967 CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
968 CASE_BODY(kRootArray, kPlain, kStartOfObject, 0, kUnknownOffsetFromStart)
969 // Find an object in the partial snapshots cache and write a pointer to it
970 // to the current object.
971 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
972 CASE_BODY(kPartialSnapshotCache,
973 kPlain,
974 kStartOfObject,
975 0,
976 kUnknownOffsetFromStart)
Steve Block791712a2010-08-27 10:21:07 +0100977 // Find an code entry in the partial snapshots cache and
978 // write a pointer to it to the current object.
979 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kFirstInstruction, 0)
980 CASE_BODY(kPartialSnapshotCache,
981 kPlain,
982 kFirstInstruction,
983 0,
984 kUnknownOffsetFromStart)
Leon Clarkef7060e22010-06-03 12:02:55 +0100985 // Find an external reference and write a pointer to it to the current
986 // object.
987 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
988 CASE_BODY(kExternalReference,
989 kPlain,
990 kStartOfObject,
991 0,
992 kUnknownOffsetFromStart)
993 // Find an external reference and write a pointer to it in the current
994 // code object.
995 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
996 CASE_BODY(kExternalReference,
997 kFromCode,
998 kStartOfObject,
999 0,
1000 kUnknownOffsetFromStart)
1001
1002#undef CASE_STATEMENT
1003#undef CASE_BODY
1004#undef ONE_PER_SPACE
1005#undef ALL_SPACES
1006#undef EMIT_COMMON_REFERENCE_PATTERNS
1007#undef ASSIGN_DEST_SPACE
1008
1009 case kNewPage: {
Steve Blockd0582a62009-12-15 09:54:21 +00001010 int space = source_->Get();
1011 pages_[space].Add(last_object_address_);
Steve Block6ded16b2010-05-10 14:33:55 +01001012 if (space == CODE_SPACE) {
1013 CPU::FlushICache(last_object_address_, Page::kPageSize);
1014 }
Steve Blockd0582a62009-12-15 09:54:21 +00001015 break;
1016 }
Leon Clarkef7060e22010-06-03 12:02:55 +01001017
1018 case kNativesStringResource: {
Steve Blockd0582a62009-12-15 09:54:21 +00001019 int index = source_->Get();
1020 Vector<const char> source_vector = Natives::GetScriptSource(index);
1021 NativesExternalStringResource* resource =
Steve Block44f0eee2011-05-26 01:26:41 +01001022 new NativesExternalStringResource(
1023 isolate->bootstrapper(), source_vector.start());
Steve Blockd0582a62009-12-15 09:54:21 +00001024 *current++ = reinterpret_cast<Object*>(resource);
1025 break;
1026 }
Leon Clarkef7060e22010-06-03 12:02:55 +01001027
1028 case kSynchronize: {
Leon Clarked91b9f72010-01-27 17:25:45 +00001029 // If we get here then that indicates that you have a mismatch between
1030 // the number of GC roots when serializing and deserializing.
1031 UNREACHABLE();
1032 }
Leon Clarkef7060e22010-06-03 12:02:55 +01001033
Steve Blockd0582a62009-12-15 09:54:21 +00001034 default:
1035 UNREACHABLE();
1036 }
1037 }
1038 ASSERT_EQ(current, limit);
1039}
1040
1041
1042void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
1043 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
1044 for (int shift = max_shift; shift > 0; shift -= 7) {
1045 if (integer >= static_cast<uintptr_t>(1u) << shift) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001046 Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart");
Steve Blockd0582a62009-12-15 09:54:21 +00001047 }
1048 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001049 PutSection(static_cast<int>(integer & 0x7f), "IntLastPart");
Steve Blockd0582a62009-12-15 09:54:21 +00001050}
1051
Steve Blocka7e24c12009-10-30 11:49:00 +00001052#ifdef DEBUG
Steve Blockd0582a62009-12-15 09:54:21 +00001053
1054void Deserializer::Synchronize(const char* tag) {
1055 int data = source_->Get();
1056 // If this assert fails then that indicates that you have a mismatch between
1057 // the number of GC roots when serializing and deserializing.
Leon Clarkef7060e22010-06-03 12:02:55 +01001058 ASSERT_EQ(kSynchronize, data);
Steve Blockd0582a62009-12-15 09:54:21 +00001059 do {
1060 int character = source_->Get();
1061 if (character == 0) break;
1062 if (FLAG_debug_serialization) {
1063 PrintF("%c", character);
1064 }
1065 } while (true);
1066 if (FLAG_debug_serialization) {
1067 PrintF("\n");
1068 }
1069}
1070
Steve Blocka7e24c12009-10-30 11:49:00 +00001071
1072void Serializer::Synchronize(const char* tag) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001073 sink_->Put(kSynchronize, tag);
Steve Blockd0582a62009-12-15 09:54:21 +00001074 int character;
1075 do {
1076 character = *tag++;
1077 sink_->PutSection(character, "TagCharacter");
1078 } while (character != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001079}
Steve Blockd0582a62009-12-15 09:54:21 +00001080
Steve Blocka7e24c12009-10-30 11:49:00 +00001081#endif
1082
Steve Blockd0582a62009-12-15 09:54:21 +00001083Serializer::Serializer(SnapshotByteSink* sink)
1084 : sink_(sink),
1085 current_root_index_(0),
Andrei Popescu31002712010-02-23 13:46:05 +00001086 external_reference_encoder_(new ExternalReferenceEncoder),
Leon Clarkee46be812010-01-19 14:06:41 +00001087 large_object_total_(0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001088 // The serializer is meant to be used only to generate initial heap images
1089 // from a context in which there is only one isolate.
1090 ASSERT(Isolate::Current()->IsDefaultIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001091 for (int i = 0; i <= LAST_SPACE; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +00001092 fullness_[i] = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001093 }
1094}
1095
1096
Andrei Popescu31002712010-02-23 13:46:05 +00001097Serializer::~Serializer() {
1098 delete external_reference_encoder_;
1099}
1100
1101
Leon Clarked91b9f72010-01-27 17:25:45 +00001102void StartupSerializer::SerializeStrongReferences() {
Steve Block44f0eee2011-05-26 01:26:41 +01001103 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001104 // No active threads.
Steve Block44f0eee2011-05-26 01:26:41 +01001105 CHECK_EQ(NULL, Isolate::Current()->thread_manager()->FirstThreadStateInUse());
Steve Blocka7e24c12009-10-30 11:49:00 +00001106 // No active or weak handles.
Steve Block44f0eee2011-05-26 01:26:41 +01001107 CHECK(isolate->handle_scope_implementer()->blocks()->is_empty());
1108 CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles());
Steve Blockd0582a62009-12-15 09:54:21 +00001109 // We don't support serializing installed extensions.
Steve Block44f0eee2011-05-26 01:26:41 +01001110 for (RegisteredExtension* ext = v8::RegisteredExtension::first_extension();
Steve Blockd0582a62009-12-15 09:54:21 +00001111 ext != NULL;
1112 ext = ext->next()) {
1113 CHECK_NE(v8::INSTALLED, ext->state());
1114 }
Steve Block44f0eee2011-05-26 01:26:41 +01001115 HEAP->IterateStrongRoots(this, VISIT_ONLY_STRONG);
Steve Blocka7e24c12009-10-30 11:49:00 +00001116}
1117
1118
Leon Clarked91b9f72010-01-27 17:25:45 +00001119void PartialSerializer::Serialize(Object** object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001120 this->VisitPointer(object);
Steve Block44f0eee2011-05-26 01:26:41 +01001121 Isolate* isolate = Isolate::Current();
Leon Clarked91b9f72010-01-27 17:25:45 +00001122
1123 // After we have done the partial serialization the partial snapshot cache
1124 // will contain some references needed to decode the partial snapshot. We
1125 // fill it up with undefineds so it has a predictable length so the
1126 // deserialization code doesn't need to know the length.
Steve Block44f0eee2011-05-26 01:26:41 +01001127 for (int index = isolate->serialize_partial_snapshot_cache_length();
1128 index < Isolate::kPartialSnapshotCacheCapacity;
Leon Clarked91b9f72010-01-27 17:25:45 +00001129 index++) {
Steve Block44f0eee2011-05-26 01:26:41 +01001130 isolate->serialize_partial_snapshot_cache()[index] =
1131 isolate->heap()->undefined_value();
1132 startup_serializer_->VisitPointer(
1133 &isolate->serialize_partial_snapshot_cache()[index]);
Leon Clarked91b9f72010-01-27 17:25:45 +00001134 }
Steve Block44f0eee2011-05-26 01:26:41 +01001135 isolate->set_serialize_partial_snapshot_cache_length(
1136 Isolate::kPartialSnapshotCacheCapacity);
Leon Clarkee46be812010-01-19 14:06:41 +00001137}
1138
1139
Steve Blocka7e24c12009-10-30 11:49:00 +00001140void Serializer::VisitPointers(Object** start, Object** end) {
Steve Blockd0582a62009-12-15 09:54:21 +00001141 for (Object** current = start; current < end; current++) {
1142 if ((*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001143 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001144 sink_->PutInt(kPointerSize, "length");
1145 for (int i = 0; i < kPointerSize; i++) {
1146 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1147 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001148 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001149 SerializeObject(*current, kPlain, kStartOfObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001150 }
1151 }
1152}
1153
1154
Leon Clarked91b9f72010-01-27 17:25:45 +00001155// This ensures that the partial snapshot cache keeps things alive during GC and
1156// tracks their movement. When it is called during serialization of the startup
1157// snapshot the partial snapshot is empty, so nothing happens. When the partial
1158// (context) snapshot is created, this array is populated with the pointers that
1159// the partial snapshot will need. As that happens we emit serialized objects to
1160// the startup snapshot that correspond to the elements of this cache array. On
1161// deserialization we therefore need to visit the cache array. This fills it up
1162// with pointers to deserialized objects.
Steve Block6ded16b2010-05-10 14:33:55 +01001163void SerializerDeserializer::Iterate(ObjectVisitor* visitor) {
Steve Block44f0eee2011-05-26 01:26:41 +01001164 Isolate* isolate = Isolate::Current();
Leon Clarked91b9f72010-01-27 17:25:45 +00001165 visitor->VisitPointers(
Steve Block44f0eee2011-05-26 01:26:41 +01001166 isolate->serialize_partial_snapshot_cache(),
1167 &isolate->serialize_partial_snapshot_cache()[
1168 isolate->serialize_partial_snapshot_cache_length()]);
Leon Clarked91b9f72010-01-27 17:25:45 +00001169}
1170
1171
1172// When deserializing we need to set the size of the snapshot cache. This means
1173// the root iteration code (above) will iterate over array elements, writing the
1174// references to deserialized objects in them.
1175void SerializerDeserializer::SetSnapshotCacheSize(int size) {
Steve Block44f0eee2011-05-26 01:26:41 +01001176 Isolate::Current()->set_serialize_partial_snapshot_cache_length(size);
Leon Clarked91b9f72010-01-27 17:25:45 +00001177}
1178
1179
1180int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
Steve Block44f0eee2011-05-26 01:26:41 +01001181 Isolate* isolate = Isolate::Current();
1182
1183 for (int i = 0;
1184 i < isolate->serialize_partial_snapshot_cache_length();
1185 i++) {
1186 Object* entry = isolate->serialize_partial_snapshot_cache()[i];
Leon Clarked91b9f72010-01-27 17:25:45 +00001187 if (entry == heap_object) return i;
1188 }
Andrei Popescu31002712010-02-23 13:46:05 +00001189
Leon Clarked91b9f72010-01-27 17:25:45 +00001190 // We didn't find the object in the cache. So we add it to the cache and
1191 // then visit the pointer so that it becomes part of the startup snapshot
1192 // and we can refer to it from the partial snapshot.
Steve Block44f0eee2011-05-26 01:26:41 +01001193 int length = isolate->serialize_partial_snapshot_cache_length();
1194 CHECK(length < Isolate::kPartialSnapshotCacheCapacity);
1195 isolate->serialize_partial_snapshot_cache()[length] = heap_object;
1196 startup_serializer_->VisitPointer(
1197 &isolate->serialize_partial_snapshot_cache()[length]);
Leon Clarked91b9f72010-01-27 17:25:45 +00001198 // We don't recurse from the startup snapshot generator into the partial
1199 // snapshot generator.
Steve Block44f0eee2011-05-26 01:26:41 +01001200 ASSERT(length == isolate->serialize_partial_snapshot_cache_length());
1201 isolate->set_serialize_partial_snapshot_cache_length(length + 1);
1202 return length;
Leon Clarked91b9f72010-01-27 17:25:45 +00001203}
1204
1205
1206int PartialSerializer::RootIndex(HeapObject* heap_object) {
Leon Clarkee46be812010-01-19 14:06:41 +00001207 for (int i = 0; i < Heap::kRootListLength; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +01001208 Object* root = HEAP->roots_address()[i];
Leon Clarkee46be812010-01-19 14:06:41 +00001209 if (root == heap_object) return i;
1210 }
1211 return kInvalidRootIndex;
1212}
1213
1214
Leon Clarked91b9f72010-01-27 17:25:45 +00001215// Encode the location of an already deserialized object in order to write its
1216// location into a later object. We can encode the location as an offset from
1217// the start of the deserialized objects or as an offset backwards from the
1218// current allocation pointer.
1219void Serializer::SerializeReferenceToPreviousObject(
1220 int space,
1221 int address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001222 HowToCode how_to_code,
1223 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001224 int offset = CurrentAllocationAddress(space) - address;
1225 bool from_start = true;
1226 if (SpaceIsPaged(space)) {
1227 // For paged space it is simple to encode back from current allocation if
1228 // the object is on the same page as the current allocation pointer.
1229 if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1230 (address >> kPageSizeBits)) {
1231 from_start = false;
1232 address = offset;
1233 }
1234 } else if (space == NEW_SPACE) {
1235 // For new space it is always simple to encode back from current allocation.
1236 if (offset < address) {
1237 from_start = false;
1238 address = offset;
1239 }
1240 }
1241 // If we are actually dealing with real offsets (and not a numbering of
1242 // all objects) then we should shift out the bits that are always 0.
1243 if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
Leon Clarkef7060e22010-06-03 12:02:55 +01001244 if (from_start) {
1245#define COMMON_REFS_CASE(pseudo_space, actual_space, offset) \
1246 if (space == actual_space && address == offset && \
1247 how_to_code == kPlain && where_to_point == kStartOfObject) { \
1248 sink_->Put(kFromStart + how_to_code + where_to_point + \
1249 pseudo_space, "RefSer"); \
1250 } else /* NOLINT */
1251 COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE)
1252#undef COMMON_REFS_CASE
1253 { /* NOLINT */
1254 sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer");
Leon Clarked91b9f72010-01-27 17:25:45 +00001255 sink_->PutInt(address, "address");
1256 }
1257 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01001258 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1259 sink_->PutInt(address, "address");
Leon Clarked91b9f72010-01-27 17:25:45 +00001260 }
1261}
1262
1263
1264void StartupSerializer::SerializeObject(
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001265 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001266 HowToCode how_to_code,
1267 WhereToPoint where_to_point) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001268 CHECK(o->IsHeapObject());
1269 HeapObject* heap_object = HeapObject::cast(o);
Leon Clarked91b9f72010-01-27 17:25:45 +00001270
1271 if (address_mapper_.IsMapped(heap_object)) {
Leon Clarkeeab96aa2010-01-27 16:31:12 +00001272 int space = SpaceOfAlreadySerializedObject(heap_object);
Leon Clarked91b9f72010-01-27 17:25:45 +00001273 int address = address_mapper_.MappedTo(heap_object);
1274 SerializeReferenceToPreviousObject(space,
1275 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001276 how_to_code,
1277 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001278 } else {
1279 // Object has not yet been serialized. Serialize it here.
1280 ObjectSerializer object_serializer(this,
1281 heap_object,
1282 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001283 how_to_code,
1284 where_to_point);
Leon Clarked91b9f72010-01-27 17:25:45 +00001285 object_serializer.Serialize();
1286 }
1287}
1288
1289
1290void StartupSerializer::SerializeWeakReferences() {
Steve Block44f0eee2011-05-26 01:26:41 +01001291 for (int i = Isolate::Current()->serialize_partial_snapshot_cache_length();
1292 i < Isolate::kPartialSnapshotCacheCapacity;
Leon Clarked91b9f72010-01-27 17:25:45 +00001293 i++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001294 sink_->Put(kRootArray + kPlain + kStartOfObject, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001295 sink_->PutInt(Heap::kUndefinedValueRootIndex, "root_index");
1296 }
Steve Block44f0eee2011-05-26 01:26:41 +01001297 HEAP->IterateWeakRoots(this, VISIT_ALL);
Leon Clarked91b9f72010-01-27 17:25:45 +00001298}
1299
1300
1301void PartialSerializer::SerializeObject(
1302 Object* o,
Leon Clarkef7060e22010-06-03 12:02:55 +01001303 HowToCode how_to_code,
1304 WhereToPoint where_to_point) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001305 CHECK(o->IsHeapObject());
1306 HeapObject* heap_object = HeapObject::cast(o);
1307
1308 int root_index;
1309 if ((root_index = RootIndex(heap_object)) != kInvalidRootIndex) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001310 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
Leon Clarked91b9f72010-01-27 17:25:45 +00001311 sink_->PutInt(root_index, "root_index");
1312 return;
1313 }
1314
1315 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1316 int cache_index = PartialSnapshotCacheIndex(heap_object);
Leon Clarkef7060e22010-06-03 12:02:55 +01001317 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1318 "PartialSnapshotCache");
Leon Clarked91b9f72010-01-27 17:25:45 +00001319 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1320 return;
1321 }
1322
1323 // Pointers from the partial snapshot to the objects in the startup snapshot
1324 // should go through the root array or through the partial snapshot cache.
1325 // If this is not the case you may have to add something to the root array.
1326 ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1327 // All the symbols that the partial snapshot needs should be either in the
1328 // root table or in the partial snapshot cache.
1329 ASSERT(!heap_object->IsSymbol());
1330
1331 if (address_mapper_.IsMapped(heap_object)) {
1332 int space = SpaceOfAlreadySerializedObject(heap_object);
1333 int address = address_mapper_.MappedTo(heap_object);
1334 SerializeReferenceToPreviousObject(space,
1335 address,
Leon Clarkef7060e22010-06-03 12:02:55 +01001336 how_to_code,
1337 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001338 } else {
1339 // Object has not yet been serialized. Serialize it here.
1340 ObjectSerializer serializer(this,
1341 heap_object,
1342 sink_,
Leon Clarkef7060e22010-06-03 12:02:55 +01001343 how_to_code,
1344 where_to_point);
Steve Blockd0582a62009-12-15 09:54:21 +00001345 serializer.Serialize();
1346 }
1347}
1348
1349
Steve Blockd0582a62009-12-15 09:54:21 +00001350void Serializer::ObjectSerializer::Serialize() {
1351 int space = Serializer::SpaceOfObject(object_);
1352 int size = object_->Size();
1353
Leon Clarkef7060e22010-06-03 12:02:55 +01001354 sink_->Put(kNewObject + reference_representation_ + space,
1355 "ObjectSerialization");
Steve Blockd0582a62009-12-15 09:54:21 +00001356 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1357
Steve Block44f0eee2011-05-26 01:26:41 +01001358 LOG(i::Isolate::Current(),
1359 SnapshotPositionEvent(object_->address(), sink_->Position()));
Leon Clarkee46be812010-01-19 14:06:41 +00001360
Steve Blockd0582a62009-12-15 09:54:21 +00001361 // Mark this object as already serialized.
1362 bool start_new_page;
Leon Clarked91b9f72010-01-27 17:25:45 +00001363 int offset = serializer_->Allocate(space, size, &start_new_page);
1364 serializer_->address_mapper()->AddMapping(object_, offset);
Steve Blockd0582a62009-12-15 09:54:21 +00001365 if (start_new_page) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001366 sink_->Put(kNewPage, "NewPage");
Steve Blockd0582a62009-12-15 09:54:21 +00001367 sink_->PutSection(space, "NewPageSpace");
1368 }
1369
1370 // Serialize the map (first word of the object).
Leon Clarkef7060e22010-06-03 12:02:55 +01001371 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001372
1373 // Serialize the rest of the object.
1374 CHECK_EQ(0, bytes_processed_so_far_);
1375 bytes_processed_so_far_ = kPointerSize;
1376 object_->IterateBody(object_->map()->instance_type(), size, this);
1377 OutputRawData(object_->address() + size);
1378}
1379
1380
1381void Serializer::ObjectSerializer::VisitPointers(Object** start,
1382 Object** end) {
1383 Object** current = start;
1384 while (current < end) {
1385 while (current < end && (*current)->IsSmi()) current++;
1386 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1387
1388 while (current < end && !(*current)->IsSmi()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001389 serializer_->SerializeObject(*current, kPlain, kStartOfObject);
Steve Blockd0582a62009-12-15 09:54:21 +00001390 bytes_processed_so_far_ += kPointerSize;
1391 current++;
1392 }
1393 }
1394}
1395
1396
1397void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1398 Address* end) {
1399 Address references_start = reinterpret_cast<Address>(start);
1400 OutputRawData(references_start);
1401
1402 for (Address* current = start; current < end; current++) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001403 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
Steve Blockd0582a62009-12-15 09:54:21 +00001404 int reference_id = serializer_->EncodeExternalReference(*current);
1405 sink_->PutInt(reference_id, "reference id");
1406 }
1407 bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1408}
1409
1410
1411void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1412 Address target_start = rinfo->target_address_address();
1413 OutputRawData(target_start);
1414 Address target = rinfo->target_address();
1415 uint32_t encoding = serializer_->EncodeExternalReference(target);
1416 CHECK(target == NULL ? encoding == 0 : encoding != 0);
Leon Clarkef7060e22010-06-03 12:02:55 +01001417 int representation;
1418 // Can't use a ternary operator because of gcc.
1419 if (rinfo->IsCodedSpecially()) {
1420 representation = kStartOfObject + kFromCode;
1421 } else {
1422 representation = kStartOfObject + kPlain;
1423 }
1424 sink_->Put(kExternalReference + representation, "ExternalReference");
Steve Blockd0582a62009-12-15 09:54:21 +00001425 sink_->PutInt(encoding, "reference id");
Leon Clarkef7060e22010-06-03 12:02:55 +01001426 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001427}
1428
1429
1430void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1431 CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1432 Address target_start = rinfo->target_address_address();
1433 OutputRawData(target_start);
1434 Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
Leon Clarkef7060e22010-06-03 12:02:55 +01001435 serializer_->SerializeObject(target, kFromCode, kFirstInstruction);
1436 bytes_processed_so_far_ += rinfo->target_address_size();
Steve Blockd0582a62009-12-15 09:54:21 +00001437}
1438
1439
Steve Block791712a2010-08-27 10:21:07 +01001440void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1441 Code* target = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1442 OutputRawData(entry_address);
1443 serializer_->SerializeObject(target, kPlain, kFirstInstruction);
1444 bytes_processed_so_far_ += kPointerSize;
1445}
1446
1447
Ben Murdochb0fe1622011-05-05 13:52:32 +01001448void Serializer::ObjectSerializer::VisitGlobalPropertyCell(RelocInfo* rinfo) {
1449 // We shouldn't have any global property cell references in code
1450 // objects in the snapshot.
1451 UNREACHABLE();
1452}
1453
1454
Steve Blockd0582a62009-12-15 09:54:21 +00001455void Serializer::ObjectSerializer::VisitExternalAsciiString(
1456 v8::String::ExternalAsciiStringResource** resource_pointer) {
1457 Address references_start = reinterpret_cast<Address>(resource_pointer);
1458 OutputRawData(references_start);
1459 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
Steve Block44f0eee2011-05-26 01:26:41 +01001460 Object* source = HEAP->natives_source_cache()->get(i);
Steve Blockd0582a62009-12-15 09:54:21 +00001461 if (!source->IsUndefined()) {
1462 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1463 typedef v8::String::ExternalAsciiStringResource Resource;
1464 Resource* resource = string->resource();
1465 if (resource == *resource_pointer) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001466 sink_->Put(kNativesStringResource, "NativesStringResource");
Steve Blockd0582a62009-12-15 09:54:21 +00001467 sink_->PutSection(i, "NativesStringResourceEnd");
1468 bytes_processed_so_far_ += sizeof(resource);
1469 return;
1470 }
1471 }
1472 }
1473 // One of the strings in the natives cache should match the resource. We
1474 // can't serialize any other kinds of external strings.
1475 UNREACHABLE();
1476}
1477
1478
1479void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1480 Address object_start = object_->address();
1481 int up_to_offset = static_cast<int>(up_to - object_start);
1482 int skipped = up_to_offset - bytes_processed_so_far_;
1483 // This assert will fail if the reloc info gives us the target_address_address
1484 // locations in a non-ascending order. Luckily that doesn't happen.
1485 ASSERT(skipped >= 0);
1486 if (skipped != 0) {
1487 Address base = object_start + bytes_processed_so_far_;
1488#define RAW_CASE(index, length) \
1489 if (skipped == length) { \
Leon Clarkef7060e22010-06-03 12:02:55 +01001490 sink_->PutSection(kRawData + index, "RawDataFixed"); \
Steve Blockd0582a62009-12-15 09:54:21 +00001491 } else /* NOLINT */
1492 COMMON_RAW_LENGTHS(RAW_CASE)
1493#undef RAW_CASE
1494 { /* NOLINT */
Leon Clarkef7060e22010-06-03 12:02:55 +01001495 sink_->Put(kRawData, "RawData");
Steve Blockd0582a62009-12-15 09:54:21 +00001496 sink_->PutInt(skipped, "length");
1497 }
1498 for (int i = 0; i < skipped; i++) {
1499 unsigned int data = base[i];
1500 sink_->PutSection(data, "Byte");
1501 }
1502 bytes_processed_so_far_ += skipped;
1503 }
1504}
1505
1506
1507int Serializer::SpaceOfObject(HeapObject* object) {
1508 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1509 AllocationSpace s = static_cast<AllocationSpace>(i);
Steve Block44f0eee2011-05-26 01:26:41 +01001510 if (HEAP->InSpace(object, s)) {
Steve Blockd0582a62009-12-15 09:54:21 +00001511 if (i == LO_SPACE) {
1512 if (object->IsCode()) {
1513 return kLargeCode;
1514 } else if (object->IsFixedArray()) {
1515 return kLargeFixedArray;
1516 } else {
1517 return kLargeData;
1518 }
1519 }
1520 return i;
1521 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001522 }
1523 UNREACHABLE();
Steve Blockd0582a62009-12-15 09:54:21 +00001524 return 0;
1525}
1526
1527
1528int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1529 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1530 AllocationSpace s = static_cast<AllocationSpace>(i);
Steve Block44f0eee2011-05-26 01:26:41 +01001531 if (HEAP->InSpace(object, s)) {
Steve Blockd0582a62009-12-15 09:54:21 +00001532 return i;
1533 }
1534 }
1535 UNREACHABLE();
1536 return 0;
1537}
1538
1539
1540int Serializer::Allocate(int space, int size, bool* new_page) {
1541 CHECK(space >= 0 && space < kNumberOfSpaces);
1542 if (SpaceIsLarge(space)) {
1543 // In large object space we merely number the objects instead of trying to
1544 // determine some sort of address.
1545 *new_page = true;
Leon Clarkee46be812010-01-19 14:06:41 +00001546 large_object_total_ += size;
Steve Blockd0582a62009-12-15 09:54:21 +00001547 return fullness_[LO_SPACE]++;
1548 }
1549 *new_page = false;
1550 if (fullness_[space] == 0) {
1551 *new_page = true;
1552 }
1553 if (SpaceIsPaged(space)) {
1554 // Paged spaces are a little special. We encode their addresses as if the
1555 // pages were all contiguous and each page were filled up in the range
1556 // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1557 // and allocation does not start at offset 0 in the page, but this scheme
1558 // means the deserializer can get the page number quickly by shifting the
1559 // serialized address.
1560 CHECK(IsPowerOf2(Page::kPageSize));
1561 int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1562 CHECK(size <= Page::kObjectAreaSize);
1563 if (used_in_this_page + size > Page::kObjectAreaSize) {
1564 *new_page = true;
1565 fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1566 }
1567 }
1568 int allocation_address = fullness_[space];
1569 fullness_[space] = allocation_address + size;
1570 return allocation_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001571}
1572
1573
1574} } // namespace v8::internal