| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 41 | #include "top.h" |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 42 | #include "bootstrapper.h" |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | |
| 44 | namespace v8 { |
| 45 | namespace internal { |
| 46 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | |
| 48 | // ----------------------------------------------------------------------------- |
| 49 | // Coding of external references. |
| 50 | |
| 51 | // The encoding of an external reference. The type is in the high word. |
| 52 | // The id is in the low word. |
| 53 | static uint32_t EncodeExternal(TypeCode type, uint16_t id) { |
| 54 | return static_cast<uint32_t>(type) << 16 | id; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static int* GetInternalPointer(StatsCounter* counter) { |
| 59 | // All counters refer to dummy_counter, if deserializing happens without |
| 60 | // setting up counters. |
| 61 | static int dummy_counter = 0; |
| 62 | return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | // ExternalReferenceTable is a helper class that defines the relationship |
| 67 | // between external references and their encodings. It is used to build |
| 68 | // hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder. |
| 69 | class ExternalReferenceTable { |
| 70 | public: |
| 71 | static ExternalReferenceTable* instance() { |
| 72 | if (!instance_) instance_ = new ExternalReferenceTable(); |
| 73 | return instance_; |
| 74 | } |
| 75 | |
| 76 | int size() const { return refs_.length(); } |
| 77 | |
| 78 | Address address(int i) { return refs_[i].address; } |
| 79 | |
| 80 | uint32_t code(int i) { return refs_[i].code; } |
| 81 | |
| 82 | const char* name(int i) { return refs_[i].name; } |
| 83 | |
| 84 | int max_id(int code) { return max_id_[code]; } |
| 85 | |
| 86 | private: |
| 87 | static ExternalReferenceTable* instance_; |
| 88 | |
| 89 | ExternalReferenceTable() : refs_(64) { PopulateTable(); } |
| 90 | ~ExternalReferenceTable() { } |
| 91 | |
| 92 | struct ExternalReferenceEntry { |
| 93 | Address address; |
| 94 | uint32_t code; |
| 95 | const char* name; |
| 96 | }; |
| 97 | |
| 98 | void PopulateTable(); |
| 99 | |
| 100 | // For a few types of references, we can get their address from their id. |
| 101 | void AddFromId(TypeCode type, uint16_t id, const char* name); |
| 102 | |
| 103 | // For other types of references, the caller will figure out the address. |
| 104 | void Add(Address address, TypeCode type, uint16_t id, const char* name); |
| 105 | |
| 106 | List<ExternalReferenceEntry> refs_; |
| 107 | int max_id_[kTypeCodeCount]; |
| 108 | }; |
| 109 | |
| 110 | |
| 111 | ExternalReferenceTable* ExternalReferenceTable::instance_ = NULL; |
| 112 | |
| 113 | |
| 114 | void ExternalReferenceTable::AddFromId(TypeCode type, |
| 115 | uint16_t id, |
| 116 | const char* name) { |
| 117 | Address address; |
| 118 | switch (type) { |
| 119 | case C_BUILTIN: { |
| 120 | ExternalReference ref(static_cast<Builtins::CFunctionId>(id)); |
| 121 | address = ref.address(); |
| 122 | break; |
| 123 | } |
| 124 | case BUILTIN: { |
| 125 | ExternalReference ref(static_cast<Builtins::Name>(id)); |
| 126 | address = ref.address(); |
| 127 | break; |
| 128 | } |
| 129 | case RUNTIME_FUNCTION: { |
| 130 | ExternalReference ref(static_cast<Runtime::FunctionId>(id)); |
| 131 | address = ref.address(); |
| 132 | break; |
| 133 | } |
| 134 | case IC_UTILITY: { |
| 135 | ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id))); |
| 136 | address = ref.address(); |
| 137 | break; |
| 138 | } |
| 139 | default: |
| 140 | UNREACHABLE(); |
| 141 | return; |
| 142 | } |
| 143 | Add(address, type, id, name); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void ExternalReferenceTable::Add(Address address, |
| 148 | TypeCode type, |
| 149 | uint16_t id, |
| 150 | const char* name) { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 151 | ASSERT_NE(NULL, address); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | ExternalReferenceEntry entry; |
| 153 | entry.address = address; |
| 154 | entry.code = EncodeExternal(type, id); |
| 155 | entry.name = name; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 156 | ASSERT_NE(0, entry.code); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 157 | refs_.Add(entry); |
| 158 | if (id > max_id_[type]) max_id_[type] = id; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | void ExternalReferenceTable::PopulateTable() { |
| 163 | for (int type_code = 0; type_code < kTypeCodeCount; type_code++) { |
| 164 | max_id_[type_code] = 0; |
| 165 | } |
| 166 | |
| 167 | // The following populates all of the different type of external references |
| 168 | // into the ExternalReferenceTable. |
| 169 | // |
| 170 | // NOTE: This function was originally 100k of code. It has since been |
| 171 | // rewritten to be mostly table driven, as the callback macro style tends to |
| 172 | // very easily cause code bloat. Please be careful in the future when adding |
| 173 | // new references. |
| 174 | |
| 175 | struct RefTableEntry { |
| 176 | TypeCode type; |
| 177 | uint16_t id; |
| 178 | const char* name; |
| 179 | }; |
| 180 | |
| 181 | static const RefTableEntry ref_table[] = { |
| 182 | // Builtins |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 183 | #define DEF_ENTRY_C(name, ignored) \ |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | { C_BUILTIN, \ |
| 185 | Builtins::c_##name, \ |
| 186 | "Builtins::" #name }, |
| 187 | |
| 188 | BUILTIN_LIST_C(DEF_ENTRY_C) |
| 189 | #undef DEF_ENTRY_C |
| 190 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 191 | #define DEF_ENTRY_C(name, ignored) \ |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | { BUILTIN, \ |
| 193 | Builtins::name, \ |
| 194 | "Builtins::" #name }, |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 195 | #define DEF_ENTRY_A(name, kind, state) DEF_ENTRY_C(name, ignored) |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 196 | |
| 197 | BUILTIN_LIST_C(DEF_ENTRY_C) |
| 198 | BUILTIN_LIST_A(DEF_ENTRY_A) |
| 199 | BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A) |
| 200 | #undef DEF_ENTRY_C |
| 201 | #undef DEF_ENTRY_A |
| 202 | |
| 203 | // Runtime functions |
| 204 | #define RUNTIME_ENTRY(name, nargs, ressize) \ |
| 205 | { RUNTIME_FUNCTION, \ |
| 206 | Runtime::k##name, \ |
| 207 | "Runtime::" #name }, |
| 208 | |
| 209 | RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY) |
| 210 | #undef RUNTIME_ENTRY |
| 211 | |
| 212 | // IC utilities |
| 213 | #define IC_ENTRY(name) \ |
| 214 | { IC_UTILITY, \ |
| 215 | IC::k##name, \ |
| 216 | "IC::" #name }, |
| 217 | |
| 218 | IC_UTIL_LIST(IC_ENTRY) |
| 219 | #undef IC_ENTRY |
| 220 | }; // end of ref_table[]. |
| 221 | |
| 222 | for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) { |
| 223 | AddFromId(ref_table[i].type, ref_table[i].id, ref_table[i].name); |
| 224 | } |
| 225 | |
| 226 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 227 | // Debug addresses |
| 228 | Add(Debug_Address(Debug::k_after_break_target_address).address(), |
| 229 | DEBUG_ADDRESS, |
| 230 | Debug::k_after_break_target_address << kDebugIdShift, |
| 231 | "Debug::after_break_target_address()"); |
| Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 232 | Add(Debug_Address(Debug::k_debug_break_slot_address).address(), |
| 233 | DEBUG_ADDRESS, |
| 234 | Debug::k_debug_break_slot_address << kDebugIdShift, |
| 235 | "Debug::debug_break_slot_address()"); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | Add(Debug_Address(Debug::k_debug_break_return_address).address(), |
| 237 | DEBUG_ADDRESS, |
| 238 | Debug::k_debug_break_return_address << kDebugIdShift, |
| 239 | "Debug::debug_break_return_address()"); |
| 240 | const char* debug_register_format = "Debug::register_address(%i)"; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 241 | int dr_format_length = StrLength(debug_register_format); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 242 | for (int i = 0; i < kNumJSCallerSaved; ++i) { |
| 243 | Vector<char> name = Vector<char>::New(dr_format_length + 1); |
| 244 | OS::SNPrintF(name, debug_register_format, i); |
| 245 | Add(Debug_Address(Debug::k_register_address, i).address(), |
| 246 | DEBUG_ADDRESS, |
| 247 | Debug::k_register_address << kDebugIdShift | i, |
| 248 | name.start()); |
| 249 | } |
| 250 | #endif |
| 251 | |
| 252 | // Stat counters |
| 253 | struct StatsRefTableEntry { |
| 254 | StatsCounter* counter; |
| 255 | uint16_t id; |
| 256 | const char* name; |
| 257 | }; |
| 258 | |
| 259 | static const StatsRefTableEntry stats_ref_table[] = { |
| 260 | #define COUNTER_ENTRY(name, caption) \ |
| 261 | { &Counters::name, \ |
| 262 | Counters::k_##name, \ |
| 263 | "Counters::" #name }, |
| 264 | |
| 265 | STATS_COUNTER_LIST_1(COUNTER_ENTRY) |
| 266 | STATS_COUNTER_LIST_2(COUNTER_ENTRY) |
| 267 | #undef COUNTER_ENTRY |
| 268 | }; // end of stats_ref_table[]. |
| 269 | |
| 270 | for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) { |
| 271 | Add(reinterpret_cast<Address>( |
| 272 | GetInternalPointer(stats_ref_table[i].counter)), |
| 273 | STATS_COUNTER, |
| 274 | stats_ref_table[i].id, |
| 275 | stats_ref_table[i].name); |
| 276 | } |
| 277 | |
| 278 | // Top addresses |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 279 | const char* top_address_format = "Top::%s"; |
| 280 | |
| 281 | const char* AddressNames[] = { |
| 282 | #define C(name) #name, |
| 283 | TOP_ADDRESS_LIST(C) |
| 284 | TOP_ADDRESS_LIST_PROF(C) |
| 285 | NULL |
| 286 | #undef C |
| 287 | }; |
| 288 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 289 | int top_format_length = StrLength(top_address_format) - 2; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 290 | for (uint16_t i = 0; i < Top::k_top_address_count; ++i) { |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 291 | const char* address_name = AddressNames[i]; |
| 292 | Vector<char> name = |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 293 | Vector<char>::New(top_format_length + StrLength(address_name) + 1); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 294 | const char* chars = name.start(); |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 295 | OS::SNPrintF(name, top_address_format, address_name); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | Add(Top::get_address_from_id((Top::AddressId)i), TOP_ADDRESS, i, chars); |
| 297 | } |
| 298 | |
| 299 | // Extensions |
| 300 | Add(FUNCTION_ADDR(GCExtension::GC), EXTENSION, 1, |
| 301 | "GCExtension::GC"); |
| 302 | |
| 303 | // Accessors |
| 304 | #define ACCESSOR_DESCRIPTOR_DECLARATION(name) \ |
| 305 | Add((Address)&Accessors::name, \ |
| 306 | ACCESSOR, \ |
| 307 | Accessors::k##name, \ |
| 308 | "Accessors::" #name); |
| 309 | |
| 310 | ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION) |
| 311 | #undef ACCESSOR_DESCRIPTOR_DECLARATION |
| 312 | |
| 313 | // Stub cache tables |
| 314 | Add(SCTableReference::keyReference(StubCache::kPrimary).address(), |
| 315 | STUB_CACHE_TABLE, |
| 316 | 1, |
| 317 | "StubCache::primary_->key"); |
| 318 | Add(SCTableReference::valueReference(StubCache::kPrimary).address(), |
| 319 | STUB_CACHE_TABLE, |
| 320 | 2, |
| 321 | "StubCache::primary_->value"); |
| 322 | Add(SCTableReference::keyReference(StubCache::kSecondary).address(), |
| 323 | STUB_CACHE_TABLE, |
| 324 | 3, |
| 325 | "StubCache::secondary_->key"); |
| 326 | Add(SCTableReference::valueReference(StubCache::kSecondary).address(), |
| 327 | STUB_CACHE_TABLE, |
| 328 | 4, |
| 329 | "StubCache::secondary_->value"); |
| 330 | |
| 331 | // Runtime entries |
| 332 | Add(ExternalReference::perform_gc_function().address(), |
| 333 | RUNTIME_ENTRY, |
| 334 | 1, |
| 335 | "Runtime::PerformGC"); |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 336 | Add(ExternalReference::fill_heap_number_with_random_function().address(), |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 337 | RUNTIME_ENTRY, |
| 338 | 2, |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 339 | "V8::FillHeapNumberWithRandom"); |
| 340 | |
| 341 | Add(ExternalReference::random_uint32_function().address(), |
| 342 | RUNTIME_ENTRY, |
| 343 | 3, |
| 344 | "V8::Random"); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | |
| 346 | // Miscellaneous |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 347 | Add(ExternalReference::the_hole_value_location().address(), |
| 348 | UNCLASSIFIED, |
| 349 | 2, |
| 350 | "Factory::the_hole_value().location()"); |
| 351 | Add(ExternalReference::roots_address().address(), |
| 352 | UNCLASSIFIED, |
| 353 | 3, |
| 354 | "Heap::roots_address()"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 355 | Add(ExternalReference::address_of_stack_limit().address(), |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 356 | UNCLASSIFIED, |
| 357 | 4, |
| 358 | "StackGuard::address_of_jslimit()"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 359 | Add(ExternalReference::address_of_real_stack_limit().address(), |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | UNCLASSIFIED, |
| 361 | 5, |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 362 | "StackGuard::address_of_real_jslimit()"); |
| 363 | Add(ExternalReference::address_of_regexp_stack_limit().address(), |
| 364 | UNCLASSIFIED, |
| 365 | 6, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 366 | "RegExpStack::limit_address()"); |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 367 | Add(ExternalReference::address_of_regexp_stack_memory_address().address(), |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 368 | UNCLASSIFIED, |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 369 | 7, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 370 | "RegExpStack::memory_address()"); |
| 371 | Add(ExternalReference::address_of_regexp_stack_memory_size().address(), |
| 372 | UNCLASSIFIED, |
| 373 | 8, |
| 374 | "RegExpStack::memory_size()"); |
| 375 | Add(ExternalReference::address_of_static_offsets_vector().address(), |
| 376 | UNCLASSIFIED, |
| 377 | 9, |
| 378 | "OffsetsVector::static_offsets_vector"); |
| 379 | Add(ExternalReference::new_space_start().address(), |
| 380 | UNCLASSIFIED, |
| 381 | 10, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 382 | "Heap::NewSpaceStart()"); |
| Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 383 | Add(ExternalReference::new_space_mask().address(), |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 384 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 385 | 11, |
| Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 386 | "Heap::NewSpaceMask()"); |
| 387 | Add(ExternalReference::heap_always_allocate_scope_depth().address(), |
| 388 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 389 | 12, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 390 | "Heap::always_allocate_scope_depth()"); |
| 391 | Add(ExternalReference::new_space_allocation_limit_address().address(), |
| 392 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 393 | 13, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 394 | "Heap::NewSpaceAllocationLimitAddress()"); |
| 395 | Add(ExternalReference::new_space_allocation_top_address().address(), |
| 396 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 397 | 14, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 398 | "Heap::NewSpaceAllocationTopAddress()"); |
| 399 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 400 | Add(ExternalReference::debug_break().address(), |
| 401 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 402 | 15, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 403 | "Debug::Break()"); |
| 404 | Add(ExternalReference::debug_step_in_fp_address().address(), |
| 405 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 406 | 16, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 407 | "Debug::step_in_fp_addr()"); |
| 408 | #endif |
| 409 | Add(ExternalReference::double_fp_operation(Token::ADD).address(), |
| 410 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 411 | 17, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 412 | "add_two_doubles"); |
| 413 | Add(ExternalReference::double_fp_operation(Token::SUB).address(), |
| 414 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 415 | 18, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 416 | "sub_two_doubles"); |
| 417 | Add(ExternalReference::double_fp_operation(Token::MUL).address(), |
| 418 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 419 | 19, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 420 | "mul_two_doubles"); |
| 421 | Add(ExternalReference::double_fp_operation(Token::DIV).address(), |
| 422 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 423 | 20, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 424 | "div_two_doubles"); |
| 425 | Add(ExternalReference::double_fp_operation(Token::MOD).address(), |
| 426 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 427 | 21, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 428 | "mod_two_doubles"); |
| 429 | Add(ExternalReference::compare_doubles().address(), |
| 430 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 431 | 22, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 432 | "compare_doubles"); |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 433 | #ifndef V8_INTERPRETED_REGEXP |
| 434 | Add(ExternalReference::re_case_insensitive_compare_uc16().address(), |
| 435 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 436 | 23, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 437 | "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()"); |
| 438 | Add(ExternalReference::re_check_stack_guard_state().address(), |
| 439 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 440 | 24, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 441 | "RegExpMacroAssembler*::CheckStackGuardState()"); |
| 442 | Add(ExternalReference::re_grow_stack().address(), |
| 443 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 444 | 25, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 445 | "NativeRegExpMacroAssembler::GrowStack()"); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 446 | Add(ExternalReference::re_word_character_map().address(), |
| 447 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 448 | 26, |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 449 | "NativeRegExpMacroAssembler::word_character_map"); |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 450 | #endif // V8_INTERPRETED_REGEXP |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 451 | // Keyed lookup cache. |
| 452 | Add(ExternalReference::keyed_lookup_cache_keys().address(), |
| 453 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 454 | 27, |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 455 | "KeyedLookupCache::keys()"); |
| 456 | Add(ExternalReference::keyed_lookup_cache_field_offsets().address(), |
| 457 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 458 | 28, |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 459 | "KeyedLookupCache::field_offsets()"); |
| Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 460 | Add(ExternalReference::transcendental_cache_array_address().address(), |
| 461 | UNCLASSIFIED, |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 462 | 29, |
| Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 463 | "TranscendentalCache::caches()"); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
| 467 | ExternalReferenceEncoder::ExternalReferenceEncoder() |
| 468 | : encodings_(Match) { |
| 469 | ExternalReferenceTable* external_references = |
| 470 | ExternalReferenceTable::instance(); |
| 471 | for (int i = 0; i < external_references->size(); ++i) { |
| 472 | Put(external_references->address(i), i); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | |
| 477 | uint32_t ExternalReferenceEncoder::Encode(Address key) const { |
| 478 | int index = IndexOf(key); |
| 479 | return index >=0 ? ExternalReferenceTable::instance()->code(index) : 0; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | const char* ExternalReferenceEncoder::NameOfAddress(Address key) const { |
| 484 | int index = IndexOf(key); |
| 485 | return index >=0 ? ExternalReferenceTable::instance()->name(index) : NULL; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | int ExternalReferenceEncoder::IndexOf(Address key) const { |
| 490 | if (key == NULL) return -1; |
| 491 | HashMap::Entry* entry = |
| 492 | const_cast<HashMap &>(encodings_).Lookup(key, Hash(key), false); |
| 493 | return entry == NULL |
| 494 | ? -1 |
| 495 | : static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | void ExternalReferenceEncoder::Put(Address key, int index) { |
| 500 | HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true); |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 501 | entry->value = reinterpret_cast<void*>(index); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | |
| 505 | ExternalReferenceDecoder::ExternalReferenceDecoder() |
| 506 | : encodings_(NewArray<Address*>(kTypeCodeCount)) { |
| 507 | ExternalReferenceTable* external_references = |
| 508 | ExternalReferenceTable::instance(); |
| 509 | for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) { |
| 510 | int max = external_references->max_id(type) + 1; |
| 511 | encodings_[type] = NewArray<Address>(max + 1); |
| 512 | } |
| 513 | for (int i = 0; i < external_references->size(); ++i) { |
| 514 | Put(external_references->code(i), external_references->address(i)); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | |
| 519 | ExternalReferenceDecoder::~ExternalReferenceDecoder() { |
| 520 | for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) { |
| 521 | DeleteArray(encodings_[type]); |
| 522 | } |
| 523 | DeleteArray(encodings_); |
| 524 | } |
| 525 | |
| 526 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 527 | bool Serializer::serialization_enabled_ = false; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 528 | bool Serializer::too_late_to_enable_now_ = false; |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 529 | ExternalReferenceDecoder* Deserializer::external_reference_decoder_ = NULL; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 530 | |
| 531 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 532 | Deserializer::Deserializer(SnapshotByteSource* source) : source_(source) { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | |
| 536 | // This routine both allocates a new object, and also keeps |
| 537 | // track of where objects have been allocated so that we can |
| 538 | // fix back references when deserializing. |
| 539 | Address Deserializer::Allocate(int space_index, Space* space, int size) { |
| 540 | Address address; |
| 541 | if (!SpaceIsLarge(space_index)) { |
| 542 | ASSERT(!SpaceIsPaged(space_index) || |
| 543 | size <= Page::kPageSize - Page::kObjectStartOffset); |
| 544 | Object* new_allocation; |
| 545 | if (space_index == NEW_SPACE) { |
| 546 | new_allocation = reinterpret_cast<NewSpace*>(space)->AllocateRaw(size); |
| 547 | } else { |
| 548 | new_allocation = reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size); |
| 549 | } |
| 550 | HeapObject* new_object = HeapObject::cast(new_allocation); |
| 551 | ASSERT(!new_object->IsFailure()); |
| 552 | address = new_object->address(); |
| 553 | high_water_[space_index] = address + size; |
| 554 | } else { |
| 555 | ASSERT(SpaceIsLarge(space_index)); |
| 556 | ASSERT(size > Page::kPageSize - Page::kObjectStartOffset); |
| 557 | LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space); |
| 558 | Object* new_allocation; |
| 559 | if (space_index == kLargeData) { |
| 560 | new_allocation = lo_space->AllocateRaw(size); |
| 561 | } else if (space_index == kLargeFixedArray) { |
| 562 | new_allocation = lo_space->AllocateRawFixedArray(size); |
| 563 | } else { |
| 564 | ASSERT_EQ(kLargeCode, space_index); |
| 565 | new_allocation = lo_space->AllocateRawCode(size); |
| 566 | } |
| 567 | ASSERT(!new_allocation->IsFailure()); |
| 568 | HeapObject* new_object = HeapObject::cast(new_allocation); |
| 569 | // Record all large objects in the same space. |
| 570 | address = new_object->address(); |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 571 | pages_[LO_SPACE].Add(address); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 572 | } |
| 573 | last_object_address_ = address; |
| 574 | return address; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | // This returns the address of an object that has been described in the |
| 579 | // snapshot as being offset bytes back in a particular space. |
| 580 | HeapObject* Deserializer::GetAddressFromEnd(int space) { |
| 581 | int offset = source_->GetInt(); |
| 582 | ASSERT(!SpaceIsLarge(space)); |
| 583 | offset <<= kObjectAlignmentBits; |
| 584 | return HeapObject::FromAddress(high_water_[space] - offset); |
| 585 | } |
| 586 | |
| 587 | |
| 588 | // This returns the address of an object that has been described in the |
| 589 | // snapshot as being offset bytes into a particular space. |
| 590 | HeapObject* Deserializer::GetAddressFromStart(int space) { |
| 591 | int offset = source_->GetInt(); |
| 592 | if (SpaceIsLarge(space)) { |
| 593 | // Large spaces have one object per 'page'. |
| 594 | return HeapObject::FromAddress(pages_[LO_SPACE][offset]); |
| 595 | } |
| 596 | offset <<= kObjectAlignmentBits; |
| 597 | if (space == NEW_SPACE) { |
| 598 | // New space has only one space - numbered 0. |
| 599 | return HeapObject::FromAddress(pages_[space][0] + offset); |
| 600 | } |
| 601 | ASSERT(SpaceIsPaged(space)); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 602 | int page_of_pointee = offset >> kPageSizeBits; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 603 | Address object_address = pages_[space][page_of_pointee] + |
| 604 | (offset & Page::kPageAlignmentMask); |
| 605 | return HeapObject::FromAddress(object_address); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | void Deserializer::Deserialize() { |
| 610 | // Don't GC while deserializing - just expand the heap. |
| 611 | AlwaysAllocateScope always_allocate; |
| 612 | // Don't use the free lists while deserializing. |
| 613 | LinearAllocationScope allocate_linearly; |
| 614 | // No active threads. |
| 615 | ASSERT_EQ(NULL, ThreadState::FirstInUse()); |
| 616 | // No active handles. |
| 617 | ASSERT(HandleScopeImplementer::instance()->blocks()->is_empty()); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 618 | // Make sure the entire partial snapshot cache is traversed, filling it with |
| 619 | // valid object pointers. |
| 620 | partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 621 | ASSERT_EQ(NULL, external_reference_decoder_); |
| 622 | external_reference_decoder_ = new ExternalReferenceDecoder(); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 623 | Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG); |
| 624 | Heap::IterateWeakRoots(this, VISIT_ALL); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
| 628 | void Deserializer::DeserializePartial(Object** root) { |
| 629 | // Don't GC while deserializing - just expand the heap. |
| 630 | AlwaysAllocateScope always_allocate; |
| 631 | // Don't use the free lists while deserializing. |
| 632 | LinearAllocationScope allocate_linearly; |
| 633 | if (external_reference_decoder_ == NULL) { |
| 634 | external_reference_decoder_ = new ExternalReferenceDecoder(); |
| 635 | } |
| 636 | VisitPointer(root); |
| 637 | } |
| 638 | |
| 639 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 640 | Deserializer::~Deserializer() { |
| 641 | ASSERT(source_->AtEOF()); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 642 | if (external_reference_decoder_ != NULL) { |
| 643 | delete external_reference_decoder_; |
| 644 | external_reference_decoder_ = NULL; |
| 645 | } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | |
| 649 | // This is called on the roots. It is the driver of the deserialization |
| 650 | // process. It is also called on the body of each function. |
| 651 | void Deserializer::VisitPointers(Object** start, Object** end) { |
| 652 | // The space must be new space. Any other space would cause ReadChunk to try |
| 653 | // to update the remembered using NULL as the address. |
| 654 | ReadChunk(start, end, NEW_SPACE, NULL); |
| 655 | } |
| 656 | |
| 657 | |
| 658 | // This routine writes the new object into the pointer provided and then |
| 659 | // returns true if the new object was in young space and false otherwise. |
| 660 | // The reason for this strange interface is that otherwise the object is |
| 661 | // written very late, which means the ByteArray map is not set up by the |
| 662 | // time we need to use it to mark the space at the end of a page free (by |
| 663 | // making it into a byte array). |
| 664 | void Deserializer::ReadObject(int space_number, |
| 665 | Space* space, |
| 666 | Object** write_back) { |
| 667 | int size = source_->GetInt() << kObjectAlignmentBits; |
| 668 | Address address = Allocate(space_number, space, size); |
| 669 | *write_back = HeapObject::FromAddress(address); |
| 670 | Object** current = reinterpret_cast<Object**>(address); |
| 671 | Object** limit = current + (size >> kPointerSizeLog2); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 672 | if (FLAG_log_snapshot_positions) { |
| 673 | LOG(SnapshotPositionEvent(address, source_->position())); |
| 674 | } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 675 | ReadChunk(current, limit, space_number, address); |
| 676 | } |
| 677 | |
| 678 | |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 679 | // This macro is always used with a constant argument so it should all fold |
| 680 | // away to almost nothing in the generated code. It might be nicer to do this |
| 681 | // with the ternary operator but there are type issues with that. |
| 682 | #define ASSIGN_DEST_SPACE(space_number) \ |
| 683 | Space* dest_space; \ |
| 684 | if (space_number == NEW_SPACE) { \ |
| 685 | dest_space = Heap::new_space(); \ |
| 686 | } else if (space_number == OLD_POINTER_SPACE) { \ |
| 687 | dest_space = Heap::old_pointer_space(); \ |
| 688 | } else if (space_number == OLD_DATA_SPACE) { \ |
| 689 | dest_space = Heap::old_data_space(); \ |
| 690 | } else if (space_number == CODE_SPACE) { \ |
| 691 | dest_space = Heap::code_space(); \ |
| 692 | } else if (space_number == MAP_SPACE) { \ |
| 693 | dest_space = Heap::map_space(); \ |
| 694 | } else if (space_number == CELL_SPACE) { \ |
| 695 | dest_space = Heap::cell_space(); \ |
| 696 | } else { \ |
| 697 | ASSERT(space_number >= LO_SPACE); \ |
| 698 | dest_space = Heap::lo_space(); \ |
| 699 | } |
| 700 | |
| 701 | |
| 702 | static const int kUnknownOffsetFromStart = -1; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 703 | |
| 704 | |
| 705 | void Deserializer::ReadChunk(Object** current, |
| 706 | Object** limit, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 707 | int source_space, |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 708 | Address address) { |
| 709 | while (current < limit) { |
| 710 | int data = source_->Get(); |
| 711 | switch (data) { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 712 | #define CASE_STATEMENT(where, how, within, space_number) \ |
| 713 | case where + how + within + space_number: \ |
| 714 | ASSERT((where & ~kPointedToMask) == 0); \ |
| 715 | ASSERT((how & ~kHowToCodeMask) == 0); \ |
| 716 | ASSERT((within & ~kWhereToPointMask) == 0); \ |
| 717 | ASSERT((space_number & ~kSpaceMask) == 0); |
| 718 | |
| 719 | #define CASE_BODY(where, how, within, space_number_if_any, offset_from_start) \ |
| 720 | { \ |
| 721 | bool emit_write_barrier = false; \ |
| 722 | bool current_was_incremented = false; \ |
| 723 | int space_number = space_number_if_any == kAnyOldSpace ? \ |
| 724 | (data & kSpaceMask) : space_number_if_any; \ |
| 725 | if (where == kNewObject && how == kPlain && within == kStartOfObject) {\ |
| 726 | ASSIGN_DEST_SPACE(space_number) \ |
| 727 | ReadObject(space_number, dest_space, current); \ |
| 728 | emit_write_barrier = \ |
| 729 | (space_number == NEW_SPACE && source_space != NEW_SPACE); \ |
| 730 | } else { \ |
| 731 | Object* new_object = NULL; /* May not be a real Object pointer. */ \ |
| 732 | if (where == kNewObject) { \ |
| 733 | ASSIGN_DEST_SPACE(space_number) \ |
| 734 | ReadObject(space_number, dest_space, &new_object); \ |
| 735 | } else if (where == kRootArray) { \ |
| 736 | int root_id = source_->GetInt(); \ |
| 737 | new_object = Heap::roots_address()[root_id]; \ |
| 738 | } else if (where == kPartialSnapshotCache) { \ |
| 739 | int cache_index = source_->GetInt(); \ |
| 740 | new_object = partial_snapshot_cache_[cache_index]; \ |
| 741 | } else if (where == kExternalReference) { \ |
| 742 | int reference_id = source_->GetInt(); \ |
| 743 | Address address = \ |
| 744 | external_reference_decoder_->Decode(reference_id); \ |
| 745 | new_object = reinterpret_cast<Object*>(address); \ |
| 746 | } else if (where == kBackref) { \ |
| 747 | emit_write_barrier = \ |
| 748 | (space_number == NEW_SPACE && source_space != NEW_SPACE); \ |
| 749 | new_object = GetAddressFromEnd(data & kSpaceMask); \ |
| 750 | } else { \ |
| 751 | ASSERT(where == kFromStart); \ |
| 752 | if (offset_from_start == kUnknownOffsetFromStart) { \ |
| 753 | emit_write_barrier = \ |
| 754 | (space_number == NEW_SPACE && source_space != NEW_SPACE); \ |
| 755 | new_object = GetAddressFromStart(data & kSpaceMask); \ |
| 756 | } else { \ |
| 757 | Address object_address = pages_[space_number][0] + \ |
| 758 | (offset_from_start << kObjectAlignmentBits); \ |
| 759 | new_object = HeapObject::FromAddress(object_address); \ |
| 760 | } \ |
| 761 | } \ |
| 762 | if (within == kFirstInstruction) { \ |
| 763 | Code* new_code_object = reinterpret_cast<Code*>(new_object); \ |
| 764 | new_object = reinterpret_cast<Object*>( \ |
| 765 | new_code_object->instruction_start()); \ |
| 766 | } \ |
| 767 | if (how == kFromCode) { \ |
| 768 | Address location_of_branch_data = \ |
| 769 | reinterpret_cast<Address>(current); \ |
| 770 | Assembler::set_target_at(location_of_branch_data, \ |
| 771 | reinterpret_cast<Address>(new_object)); \ |
| 772 | if (within == kFirstInstruction) { \ |
| 773 | location_of_branch_data += Assembler::kCallTargetSize; \ |
| 774 | current = reinterpret_cast<Object**>(location_of_branch_data); \ |
| 775 | current_was_incremented = true; \ |
| 776 | } \ |
| 777 | } else { \ |
| 778 | *current = new_object; \ |
| 779 | } \ |
| 780 | } \ |
| 781 | if (emit_write_barrier) { \ |
| 782 | Heap::RecordWrite(address, static_cast<int>( \ |
| 783 | reinterpret_cast<Address>(current) - address)); \ |
| 784 | } \ |
| 785 | if (!current_was_incremented) { \ |
| 786 | current++; /* Increment current if it wasn't done above. */ \ |
| 787 | } \ |
| 788 | break; \ |
| 789 | } \ |
| 790 | |
| 791 | // This generates a case and a body for each space. The large object spaces are |
| 792 | // very rare in snapshots so they are grouped in one body. |
| 793 | #define ONE_PER_SPACE(where, how, within) \ |
| 794 | CASE_STATEMENT(where, how, within, NEW_SPACE) \ |
| 795 | CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \ |
| 796 | CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \ |
| 797 | CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart) \ |
| 798 | CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \ |
| 799 | CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart) \ |
| 800 | CASE_STATEMENT(where, how, within, CODE_SPACE) \ |
| 801 | CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \ |
| 802 | CASE_STATEMENT(where, how, within, CELL_SPACE) \ |
| 803 | CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart) \ |
| 804 | CASE_STATEMENT(where, how, within, MAP_SPACE) \ |
| 805 | CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart) \ |
| 806 | CASE_STATEMENT(where, how, within, kLargeData) \ |
| 807 | CASE_STATEMENT(where, how, within, kLargeCode) \ |
| 808 | CASE_STATEMENT(where, how, within, kLargeFixedArray) \ |
| 809 | CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart) |
| 810 | |
| 811 | // This generates a case and a body for the new space (which has to do extra |
| 812 | // write barrier handling) and handles the other spaces with 8 fall-through |
| 813 | // cases and one body. |
| 814 | #define ALL_SPACES(where, how, within) \ |
| 815 | CASE_STATEMENT(where, how, within, NEW_SPACE) \ |
| 816 | CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \ |
| 817 | CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \ |
| 818 | CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \ |
| 819 | CASE_STATEMENT(where, how, within, CODE_SPACE) \ |
| 820 | CASE_STATEMENT(where, how, within, CELL_SPACE) \ |
| 821 | CASE_STATEMENT(where, how, within, MAP_SPACE) \ |
| 822 | CASE_STATEMENT(where, how, within, kLargeData) \ |
| 823 | CASE_STATEMENT(where, how, within, kLargeCode) \ |
| 824 | CASE_STATEMENT(where, how, within, kLargeFixedArray) \ |
| 825 | CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart) |
| 826 | |
| 827 | #define EMIT_COMMON_REFERENCE_PATTERNS(pseudo_space_number, \ |
| 828 | space_number, \ |
| 829 | offset_from_start) \ |
| 830 | CASE_STATEMENT(kFromStart, kPlain, kStartOfObject, pseudo_space_number) \ |
| 831 | CASE_BODY(kFromStart, kPlain, kStartOfObject, space_number, offset_from_start) |
| 832 | |
| 833 | // We generate 15 cases and bodies that process special tags that combine |
| 834 | // the raw data tag and the length into one byte. |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 835 | #define RAW_CASE(index, size) \ |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 836 | case kRawData + index: { \ |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 837 | byte* raw_data_out = reinterpret_cast<byte*>(current); \ |
| 838 | source_->CopyRaw(raw_data_out, size); \ |
| 839 | current = reinterpret_cast<Object**>(raw_data_out + size); \ |
| 840 | break; \ |
| 841 | } |
| 842 | COMMON_RAW_LENGTHS(RAW_CASE) |
| 843 | #undef RAW_CASE |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 844 | |
| 845 | // Deserialize a chunk of raw data that doesn't have one of the popular |
| 846 | // lengths. |
| 847 | case kRawData: { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 848 | int size = source_->GetInt(); |
| 849 | byte* raw_data_out = reinterpret_cast<byte*>(current); |
| 850 | source_->CopyRaw(raw_data_out, size); |
| 851 | current = reinterpret_cast<Object**>(raw_data_out + size); |
| 852 | break; |
| 853 | } |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 854 | |
| 855 | // Deserialize a new object and write a pointer to it to the current |
| 856 | // object. |
| 857 | ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject) |
| 858 | // Deserialize a new code object and write a pointer to its first |
| 859 | // instruction to the current code object. |
| 860 | ONE_PER_SPACE(kNewObject, kFromCode, kFirstInstruction) |
| 861 | // Find a recently deserialized object using its offset from the current |
| 862 | // allocation point and write a pointer to it to the current object. |
| 863 | ALL_SPACES(kBackref, kPlain, kStartOfObject) |
| 864 | // Find a recently deserialized code object using its offset from the |
| 865 | // current allocation point and write a pointer to its first instruction |
| 866 | // to the current code object. |
| 867 | ALL_SPACES(kBackref, kFromCode, kFirstInstruction) |
| 868 | // Find an already deserialized object using its offset from the start |
| 869 | // and write a pointer to it to the current object. |
| 870 | ALL_SPACES(kFromStart, kPlain, kStartOfObject) |
| 871 | // Find an already deserialized code object using its offset from the |
| 872 | // start and write a pointer to its first instruction to the current code |
| 873 | // object. |
| 874 | ALL_SPACES(kFromStart, kFromCode, kFirstInstruction) |
| 875 | // Find an already deserialized object at one of the predetermined popular |
| 876 | // offsets from the start and write a pointer to it in the current object. |
| 877 | COMMON_REFERENCE_PATTERNS(EMIT_COMMON_REFERENCE_PATTERNS) |
| 878 | // Find an object in the roots array and write a pointer to it to the |
| 879 | // current object. |
| 880 | CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0) |
| 881 | CASE_BODY(kRootArray, kPlain, kStartOfObject, 0, kUnknownOffsetFromStart) |
| 882 | // Find an object in the partial snapshots cache and write a pointer to it |
| 883 | // to the current object. |
| 884 | CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0) |
| 885 | CASE_BODY(kPartialSnapshotCache, |
| 886 | kPlain, |
| 887 | kStartOfObject, |
| 888 | 0, |
| 889 | kUnknownOffsetFromStart) |
| 890 | // Find an external reference and write a pointer to it to the current |
| 891 | // object. |
| 892 | CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0) |
| 893 | CASE_BODY(kExternalReference, |
| 894 | kPlain, |
| 895 | kStartOfObject, |
| 896 | 0, |
| 897 | kUnknownOffsetFromStart) |
| 898 | // Find an external reference and write a pointer to it in the current |
| 899 | // code object. |
| 900 | CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0) |
| 901 | CASE_BODY(kExternalReference, |
| 902 | kFromCode, |
| 903 | kStartOfObject, |
| 904 | 0, |
| 905 | kUnknownOffsetFromStart) |
| 906 | |
| 907 | #undef CASE_STATEMENT |
| 908 | #undef CASE_BODY |
| 909 | #undef ONE_PER_SPACE |
| 910 | #undef ALL_SPACES |
| 911 | #undef EMIT_COMMON_REFERENCE_PATTERNS |
| 912 | #undef ASSIGN_DEST_SPACE |
| 913 | |
| 914 | case kNewPage: { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 915 | int space = source_->Get(); |
| 916 | pages_[space].Add(last_object_address_); |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 917 | if (space == CODE_SPACE) { |
| 918 | CPU::FlushICache(last_object_address_, Page::kPageSize); |
| 919 | } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 920 | break; |
| 921 | } |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 922 | |
| 923 | case kNativesStringResource: { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 924 | int index = source_->Get(); |
| 925 | Vector<const char> source_vector = Natives::GetScriptSource(index); |
| 926 | NativesExternalStringResource* resource = |
| 927 | new NativesExternalStringResource(source_vector.start()); |
| 928 | *current++ = reinterpret_cast<Object*>(resource); |
| 929 | break; |
| 930 | } |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 931 | |
| 932 | case kSynchronize: { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 933 | // If we get here then that indicates that you have a mismatch between |
| 934 | // the number of GC roots when serializing and deserializing. |
| 935 | UNREACHABLE(); |
| 936 | } |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 937 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 938 | default: |
| 939 | UNREACHABLE(); |
| 940 | } |
| 941 | } |
| 942 | ASSERT_EQ(current, limit); |
| 943 | } |
| 944 | |
| 945 | |
| 946 | void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) { |
| 947 | const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7; |
| 948 | for (int shift = max_shift; shift > 0; shift -= 7) { |
| 949 | if (integer >= static_cast<uintptr_t>(1u) << shift) { |
| Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 950 | Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 953 | PutSection(static_cast<int>(integer & 0x7f), "IntLastPart"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 956 | #ifdef DEBUG |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 957 | |
| 958 | void Deserializer::Synchronize(const char* tag) { |
| 959 | int data = source_->Get(); |
| 960 | // If this assert fails then that indicates that you have a mismatch between |
| 961 | // the number of GC roots when serializing and deserializing. |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 962 | ASSERT_EQ(kSynchronize, data); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 963 | do { |
| 964 | int character = source_->Get(); |
| 965 | if (character == 0) break; |
| 966 | if (FLAG_debug_serialization) { |
| 967 | PrintF("%c", character); |
| 968 | } |
| 969 | } while (true); |
| 970 | if (FLAG_debug_serialization) { |
| 971 | PrintF("\n"); |
| 972 | } |
| 973 | } |
| 974 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 975 | |
| 976 | void Serializer::Synchronize(const char* tag) { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 977 | sink_->Put(kSynchronize, tag); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 978 | int character; |
| 979 | do { |
| 980 | character = *tag++; |
| 981 | sink_->PutSection(character, "TagCharacter"); |
| 982 | } while (character != 0); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 983 | } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 984 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 985 | #endif |
| 986 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 987 | Serializer::Serializer(SnapshotByteSink* sink) |
| 988 | : sink_(sink), |
| 989 | current_root_index_(0), |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 990 | external_reference_encoder_(new ExternalReferenceEncoder), |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 991 | large_object_total_(0) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 992 | for (int i = 0; i <= LAST_SPACE; i++) { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 993 | fullness_[i] = 0; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
| 997 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 998 | Serializer::~Serializer() { |
| 999 | delete external_reference_encoder_; |
| 1000 | } |
| 1001 | |
| 1002 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1003 | void StartupSerializer::SerializeStrongReferences() { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1004 | // No active threads. |
| 1005 | CHECK_EQ(NULL, ThreadState::FirstInUse()); |
| 1006 | // No active or weak handles. |
| 1007 | CHECK(HandleScopeImplementer::instance()->blocks()->is_empty()); |
| 1008 | CHECK_EQ(0, GlobalHandles::NumberOfWeakHandles()); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1009 | // We don't support serializing installed extensions. |
| 1010 | for (RegisteredExtension* ext = RegisteredExtension::first_extension(); |
| 1011 | ext != NULL; |
| 1012 | ext = ext->next()) { |
| 1013 | CHECK_NE(v8::INSTALLED, ext->state()); |
| 1014 | } |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1015 | Heap::IterateStrongRoots(this, VISIT_ONLY_STRONG); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1019 | void PartialSerializer::Serialize(Object** object) { |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1020 | this->VisitPointer(object); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1021 | |
| 1022 | // After we have done the partial serialization the partial snapshot cache |
| 1023 | // will contain some references needed to decode the partial snapshot. We |
| 1024 | // fill it up with undefineds so it has a predictable length so the |
| 1025 | // deserialization code doesn't need to know the length. |
| 1026 | for (int index = partial_snapshot_cache_length_; |
| 1027 | index < kPartialSnapshotCacheCapacity; |
| 1028 | index++) { |
| 1029 | partial_snapshot_cache_[index] = Heap::undefined_value(); |
| 1030 | startup_serializer_->VisitPointer(&partial_snapshot_cache_[index]); |
| 1031 | } |
| 1032 | partial_snapshot_cache_length_ = kPartialSnapshotCacheCapacity; |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1036 | void Serializer::VisitPointers(Object** start, Object** end) { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1037 | for (Object** current = start; current < end; current++) { |
| 1038 | if ((*current)->IsSmi()) { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1039 | sink_->Put(kRawData, "RawData"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1040 | sink_->PutInt(kPointerSize, "length"); |
| 1041 | for (int i = 0; i < kPointerSize; i++) { |
| 1042 | sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte"); |
| 1043 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1044 | } else { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1045 | SerializeObject(*current, kPlain, kStartOfObject); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1051 | Object* SerializerDeserializer::partial_snapshot_cache_[ |
| 1052 | kPartialSnapshotCacheCapacity]; |
| 1053 | int SerializerDeserializer::partial_snapshot_cache_length_ = 0; |
| 1054 | |
| 1055 | |
| 1056 | // This ensures that the partial snapshot cache keeps things alive during GC and |
| 1057 | // tracks their movement. When it is called during serialization of the startup |
| 1058 | // snapshot the partial snapshot is empty, so nothing happens. When the partial |
| 1059 | // (context) snapshot is created, this array is populated with the pointers that |
| 1060 | // the partial snapshot will need. As that happens we emit serialized objects to |
| 1061 | // the startup snapshot that correspond to the elements of this cache array. On |
| 1062 | // deserialization we therefore need to visit the cache array. This fills it up |
| 1063 | // with pointers to deserialized objects. |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1064 | void SerializerDeserializer::Iterate(ObjectVisitor* visitor) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1065 | visitor->VisitPointers( |
| 1066 | &partial_snapshot_cache_[0], |
| 1067 | &partial_snapshot_cache_[partial_snapshot_cache_length_]); |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | // When deserializing we need to set the size of the snapshot cache. This means |
| 1072 | // the root iteration code (above) will iterate over array elements, writing the |
| 1073 | // references to deserialized objects in them. |
| 1074 | void SerializerDeserializer::SetSnapshotCacheSize(int size) { |
| 1075 | partial_snapshot_cache_length_ = size; |
| 1076 | } |
| 1077 | |
| 1078 | |
| 1079 | int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) { |
| 1080 | for (int i = 0; i < partial_snapshot_cache_length_; i++) { |
| 1081 | Object* entry = partial_snapshot_cache_[i]; |
| 1082 | if (entry == heap_object) return i; |
| 1083 | } |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1084 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1085 | // We didn't find the object in the cache. So we add it to the cache and |
| 1086 | // then visit the pointer so that it becomes part of the startup snapshot |
| 1087 | // and we can refer to it from the partial snapshot. |
| 1088 | int length = partial_snapshot_cache_length_; |
| 1089 | CHECK(length < kPartialSnapshotCacheCapacity); |
| 1090 | partial_snapshot_cache_[length] = heap_object; |
| 1091 | startup_serializer_->VisitPointer(&partial_snapshot_cache_[length]); |
| 1092 | // We don't recurse from the startup snapshot generator into the partial |
| 1093 | // snapshot generator. |
| 1094 | ASSERT(length == partial_snapshot_cache_length_); |
| 1095 | return partial_snapshot_cache_length_++; |
| 1096 | } |
| 1097 | |
| 1098 | |
| 1099 | int PartialSerializer::RootIndex(HeapObject* heap_object) { |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1100 | for (int i = 0; i < Heap::kRootListLength; i++) { |
| 1101 | Object* root = Heap::roots_address()[i]; |
| 1102 | if (root == heap_object) return i; |
| 1103 | } |
| 1104 | return kInvalidRootIndex; |
| 1105 | } |
| 1106 | |
| 1107 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1108 | // Encode the location of an already deserialized object in order to write its |
| 1109 | // location into a later object. We can encode the location as an offset from |
| 1110 | // the start of the deserialized objects or as an offset backwards from the |
| 1111 | // current allocation pointer. |
| 1112 | void Serializer::SerializeReferenceToPreviousObject( |
| 1113 | int space, |
| 1114 | int address, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1115 | HowToCode how_to_code, |
| 1116 | WhereToPoint where_to_point) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1117 | int offset = CurrentAllocationAddress(space) - address; |
| 1118 | bool from_start = true; |
| 1119 | if (SpaceIsPaged(space)) { |
| 1120 | // For paged space it is simple to encode back from current allocation if |
| 1121 | // the object is on the same page as the current allocation pointer. |
| 1122 | if ((CurrentAllocationAddress(space) >> kPageSizeBits) == |
| 1123 | (address >> kPageSizeBits)) { |
| 1124 | from_start = false; |
| 1125 | address = offset; |
| 1126 | } |
| 1127 | } else if (space == NEW_SPACE) { |
| 1128 | // For new space it is always simple to encode back from current allocation. |
| 1129 | if (offset < address) { |
| 1130 | from_start = false; |
| 1131 | address = offset; |
| 1132 | } |
| 1133 | } |
| 1134 | // If we are actually dealing with real offsets (and not a numbering of |
| 1135 | // all objects) then we should shift out the bits that are always 0. |
| 1136 | if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits; |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1137 | if (from_start) { |
| 1138 | #define COMMON_REFS_CASE(pseudo_space, actual_space, offset) \ |
| 1139 | if (space == actual_space && address == offset && \ |
| 1140 | how_to_code == kPlain && where_to_point == kStartOfObject) { \ |
| 1141 | sink_->Put(kFromStart + how_to_code + where_to_point + \ |
| 1142 | pseudo_space, "RefSer"); \ |
| 1143 | } else /* NOLINT */ |
| 1144 | COMMON_REFERENCE_PATTERNS(COMMON_REFS_CASE) |
| 1145 | #undef COMMON_REFS_CASE |
| 1146 | { /* NOLINT */ |
| 1147 | sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer"); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1148 | sink_->PutInt(address, "address"); |
| 1149 | } |
| 1150 | } else { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1151 | sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer"); |
| 1152 | sink_->PutInt(address, "address"); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | void StartupSerializer::SerializeObject( |
| Leon Clarke | eab96aa | 2010-01-27 16:31:12 +0000 | [diff] [blame] | 1158 | Object* o, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1159 | HowToCode how_to_code, |
| 1160 | WhereToPoint where_to_point) { |
| Leon Clarke | eab96aa | 2010-01-27 16:31:12 +0000 | [diff] [blame] | 1161 | CHECK(o->IsHeapObject()); |
| 1162 | HeapObject* heap_object = HeapObject::cast(o); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1163 | |
| 1164 | if (address_mapper_.IsMapped(heap_object)) { |
| Leon Clarke | eab96aa | 2010-01-27 16:31:12 +0000 | [diff] [blame] | 1165 | int space = SpaceOfAlreadySerializedObject(heap_object); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1166 | int address = address_mapper_.MappedTo(heap_object); |
| 1167 | SerializeReferenceToPreviousObject(space, |
| 1168 | address, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1169 | how_to_code, |
| 1170 | where_to_point); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1171 | } else { |
| 1172 | // Object has not yet been serialized. Serialize it here. |
| 1173 | ObjectSerializer object_serializer(this, |
| 1174 | heap_object, |
| 1175 | sink_, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1176 | how_to_code, |
| 1177 | where_to_point); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1178 | object_serializer.Serialize(); |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | void StartupSerializer::SerializeWeakReferences() { |
| 1184 | for (int i = partial_snapshot_cache_length_; |
| 1185 | i < kPartialSnapshotCacheCapacity; |
| 1186 | i++) { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1187 | sink_->Put(kRootArray + kPlain + kStartOfObject, "RootSerialization"); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1188 | sink_->PutInt(Heap::kUndefinedValueRootIndex, "root_index"); |
| 1189 | } |
| 1190 | Heap::IterateWeakRoots(this, VISIT_ALL); |
| 1191 | } |
| 1192 | |
| 1193 | |
| 1194 | void PartialSerializer::SerializeObject( |
| 1195 | Object* o, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1196 | HowToCode how_to_code, |
| 1197 | WhereToPoint where_to_point) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1198 | CHECK(o->IsHeapObject()); |
| 1199 | HeapObject* heap_object = HeapObject::cast(o); |
| 1200 | |
| 1201 | int root_index; |
| 1202 | if ((root_index = RootIndex(heap_object)) != kInvalidRootIndex) { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1203 | sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization"); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1204 | sink_->PutInt(root_index, "root_index"); |
| 1205 | return; |
| 1206 | } |
| 1207 | |
| 1208 | if (ShouldBeInThePartialSnapshotCache(heap_object)) { |
| 1209 | int cache_index = PartialSnapshotCacheIndex(heap_object); |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1210 | sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point, |
| 1211 | "PartialSnapshotCache"); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1212 | sink_->PutInt(cache_index, "partial_snapshot_cache_index"); |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | // Pointers from the partial snapshot to the objects in the startup snapshot |
| 1217 | // should go through the root array or through the partial snapshot cache. |
| 1218 | // If this is not the case you may have to add something to the root array. |
| 1219 | ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object)); |
| 1220 | // All the symbols that the partial snapshot needs should be either in the |
| 1221 | // root table or in the partial snapshot cache. |
| 1222 | ASSERT(!heap_object->IsSymbol()); |
| 1223 | |
| 1224 | if (address_mapper_.IsMapped(heap_object)) { |
| 1225 | int space = SpaceOfAlreadySerializedObject(heap_object); |
| 1226 | int address = address_mapper_.MappedTo(heap_object); |
| 1227 | SerializeReferenceToPreviousObject(space, |
| 1228 | address, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1229 | how_to_code, |
| 1230 | where_to_point); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1231 | } else { |
| 1232 | // Object has not yet been serialized. Serialize it here. |
| 1233 | ObjectSerializer serializer(this, |
| 1234 | heap_object, |
| 1235 | sink_, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1236 | how_to_code, |
| 1237 | where_to_point); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1238 | serializer.Serialize(); |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1243 | void Serializer::ObjectSerializer::Serialize() { |
| 1244 | int space = Serializer::SpaceOfObject(object_); |
| 1245 | int size = object_->Size(); |
| 1246 | |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1247 | sink_->Put(kNewObject + reference_representation_ + space, |
| 1248 | "ObjectSerialization"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1249 | sink_->PutInt(size >> kObjectAlignmentBits, "Size in words"); |
| 1250 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1251 | LOG(SnapshotPositionEvent(object_->address(), sink_->Position())); |
| 1252 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1253 | // Mark this object as already serialized. |
| 1254 | bool start_new_page; |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1255 | int offset = serializer_->Allocate(space, size, &start_new_page); |
| 1256 | serializer_->address_mapper()->AddMapping(object_, offset); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1257 | if (start_new_page) { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1258 | sink_->Put(kNewPage, "NewPage"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1259 | sink_->PutSection(space, "NewPageSpace"); |
| 1260 | } |
| 1261 | |
| 1262 | // Serialize the map (first word of the object). |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1263 | serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1264 | |
| 1265 | // Serialize the rest of the object. |
| 1266 | CHECK_EQ(0, bytes_processed_so_far_); |
| 1267 | bytes_processed_so_far_ = kPointerSize; |
| 1268 | object_->IterateBody(object_->map()->instance_type(), size, this); |
| 1269 | OutputRawData(object_->address() + size); |
| 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | void Serializer::ObjectSerializer::VisitPointers(Object** start, |
| 1274 | Object** end) { |
| 1275 | Object** current = start; |
| 1276 | while (current < end) { |
| 1277 | while (current < end && (*current)->IsSmi()) current++; |
| 1278 | if (current < end) OutputRawData(reinterpret_cast<Address>(current)); |
| 1279 | |
| 1280 | while (current < end && !(*current)->IsSmi()) { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1281 | serializer_->SerializeObject(*current, kPlain, kStartOfObject); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1282 | bytes_processed_so_far_ += kPointerSize; |
| 1283 | current++; |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | void Serializer::ObjectSerializer::VisitExternalReferences(Address* start, |
| 1290 | Address* end) { |
| 1291 | Address references_start = reinterpret_cast<Address>(start); |
| 1292 | OutputRawData(references_start); |
| 1293 | |
| 1294 | for (Address* current = start; current < end; current++) { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1295 | sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1296 | int reference_id = serializer_->EncodeExternalReference(*current); |
| 1297 | sink_->PutInt(reference_id, "reference id"); |
| 1298 | } |
| 1299 | bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize); |
| 1300 | } |
| 1301 | |
| 1302 | |
| 1303 | void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) { |
| 1304 | Address target_start = rinfo->target_address_address(); |
| 1305 | OutputRawData(target_start); |
| 1306 | Address target = rinfo->target_address(); |
| 1307 | uint32_t encoding = serializer_->EncodeExternalReference(target); |
| 1308 | CHECK(target == NULL ? encoding == 0 : encoding != 0); |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1309 | int representation; |
| 1310 | // Can't use a ternary operator because of gcc. |
| 1311 | if (rinfo->IsCodedSpecially()) { |
| 1312 | representation = kStartOfObject + kFromCode; |
| 1313 | } else { |
| 1314 | representation = kStartOfObject + kPlain; |
| 1315 | } |
| 1316 | sink_->Put(kExternalReference + representation, "ExternalReference"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1317 | sink_->PutInt(encoding, "reference id"); |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1318 | bytes_processed_so_far_ += rinfo->target_address_size(); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | |
| 1322 | void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) { |
| 1323 | CHECK(RelocInfo::IsCodeTarget(rinfo->rmode())); |
| 1324 | Address target_start = rinfo->target_address_address(); |
| 1325 | OutputRawData(target_start); |
| 1326 | Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address()); |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1327 | serializer_->SerializeObject(target, kFromCode, kFirstInstruction); |
| 1328 | bytes_processed_so_far_ += rinfo->target_address_size(); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | |
| 1332 | void Serializer::ObjectSerializer::VisitExternalAsciiString( |
| 1333 | v8::String::ExternalAsciiStringResource** resource_pointer) { |
| 1334 | Address references_start = reinterpret_cast<Address>(resource_pointer); |
| 1335 | OutputRawData(references_start); |
| 1336 | for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { |
| 1337 | Object* source = Heap::natives_source_cache()->get(i); |
| 1338 | if (!source->IsUndefined()) { |
| 1339 | ExternalAsciiString* string = ExternalAsciiString::cast(source); |
| 1340 | typedef v8::String::ExternalAsciiStringResource Resource; |
| 1341 | Resource* resource = string->resource(); |
| 1342 | if (resource == *resource_pointer) { |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1343 | sink_->Put(kNativesStringResource, "NativesStringResource"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1344 | sink_->PutSection(i, "NativesStringResourceEnd"); |
| 1345 | bytes_processed_so_far_ += sizeof(resource); |
| 1346 | return; |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | // One of the strings in the natives cache should match the resource. We |
| 1351 | // can't serialize any other kinds of external strings. |
| 1352 | UNREACHABLE(); |
| 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | void Serializer::ObjectSerializer::OutputRawData(Address up_to) { |
| 1357 | Address object_start = object_->address(); |
| 1358 | int up_to_offset = static_cast<int>(up_to - object_start); |
| 1359 | int skipped = up_to_offset - bytes_processed_so_far_; |
| 1360 | // This assert will fail if the reloc info gives us the target_address_address |
| 1361 | // locations in a non-ascending order. Luckily that doesn't happen. |
| 1362 | ASSERT(skipped >= 0); |
| 1363 | if (skipped != 0) { |
| 1364 | Address base = object_start + bytes_processed_so_far_; |
| 1365 | #define RAW_CASE(index, length) \ |
| 1366 | if (skipped == length) { \ |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1367 | sink_->PutSection(kRawData + index, "RawDataFixed"); \ |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1368 | } else /* NOLINT */ |
| 1369 | COMMON_RAW_LENGTHS(RAW_CASE) |
| 1370 | #undef RAW_CASE |
| 1371 | { /* NOLINT */ |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1372 | sink_->Put(kRawData, "RawData"); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1373 | sink_->PutInt(skipped, "length"); |
| 1374 | } |
| 1375 | for (int i = 0; i < skipped; i++) { |
| 1376 | unsigned int data = base[i]; |
| 1377 | sink_->PutSection(data, "Byte"); |
| 1378 | } |
| 1379 | bytes_processed_so_far_ += skipped; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | |
| 1384 | int Serializer::SpaceOfObject(HeapObject* object) { |
| 1385 | for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) { |
| 1386 | AllocationSpace s = static_cast<AllocationSpace>(i); |
| 1387 | if (Heap::InSpace(object, s)) { |
| 1388 | if (i == LO_SPACE) { |
| 1389 | if (object->IsCode()) { |
| 1390 | return kLargeCode; |
| 1391 | } else if (object->IsFixedArray()) { |
| 1392 | return kLargeFixedArray; |
| 1393 | } else { |
| 1394 | return kLargeData; |
| 1395 | } |
| 1396 | } |
| 1397 | return i; |
| 1398 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1399 | } |
| 1400 | UNREACHABLE(); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) { |
| 1406 | for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) { |
| 1407 | AllocationSpace s = static_cast<AllocationSpace>(i); |
| 1408 | if (Heap::InSpace(object, s)) { |
| 1409 | return i; |
| 1410 | } |
| 1411 | } |
| 1412 | UNREACHABLE(); |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | |
| 1417 | int Serializer::Allocate(int space, int size, bool* new_page) { |
| 1418 | CHECK(space >= 0 && space < kNumberOfSpaces); |
| 1419 | if (SpaceIsLarge(space)) { |
| 1420 | // In large object space we merely number the objects instead of trying to |
| 1421 | // determine some sort of address. |
| 1422 | *new_page = true; |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1423 | large_object_total_ += size; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1424 | return fullness_[LO_SPACE]++; |
| 1425 | } |
| 1426 | *new_page = false; |
| 1427 | if (fullness_[space] == 0) { |
| 1428 | *new_page = true; |
| 1429 | } |
| 1430 | if (SpaceIsPaged(space)) { |
| 1431 | // Paged spaces are a little special. We encode their addresses as if the |
| 1432 | // pages were all contiguous and each page were filled up in the range |
| 1433 | // 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous |
| 1434 | // and allocation does not start at offset 0 in the page, but this scheme |
| 1435 | // means the deserializer can get the page number quickly by shifting the |
| 1436 | // serialized address. |
| 1437 | CHECK(IsPowerOf2(Page::kPageSize)); |
| 1438 | int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1)); |
| 1439 | CHECK(size <= Page::kObjectAreaSize); |
| 1440 | if (used_in_this_page + size > Page::kObjectAreaSize) { |
| 1441 | *new_page = true; |
| 1442 | fullness_[space] = RoundUp(fullness_[space], Page::kPageSize); |
| 1443 | } |
| 1444 | } |
| 1445 | int allocation_address = fullness_[space]; |
| 1446 | fullness_[space] = allocation_address + size; |
| 1447 | return allocation_address; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | |
| 1451 | } } // namespace v8::internal |