Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1 | // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/snapshot/partial-serializer.h" |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 6 | #include "src/snapshot/startup-serializer.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 7 | |
| 8 | #include "src/objects-inl.h" |
| 9 | |
| 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | |
| 13 | PartialSerializer::PartialSerializer(Isolate* isolate, |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 14 | StartupSerializer* startup_serializer) |
| 15 | : Serializer(isolate), startup_serializer_(startup_serializer) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 16 | InitializeCodeAddressMap(); |
| 17 | } |
| 18 | |
| 19 | PartialSerializer::~PartialSerializer() { |
| 20 | OutputStatistics("PartialSerializer"); |
| 21 | } |
| 22 | |
| 23 | void PartialSerializer::Serialize(Object** o) { |
| 24 | if ((*o)->IsContext()) { |
| 25 | Context* context = Context::cast(*o); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 26 | reference_map()->AddAttachedReference(context->global_proxy()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 27 | // The bootstrap snapshot has a code-stub context. When serializing the |
| 28 | // partial snapshot, it is chained into the weak context list on the isolate |
| 29 | // and it's next context pointer may point to the code-stub context. Clear |
| 30 | // it before serializing, it will get re-added to the context list |
| 31 | // explicitly when it's loaded. |
| 32 | if (context->IsNativeContext()) { |
| 33 | context->set(Context::NEXT_CONTEXT_LINK, |
| 34 | isolate_->heap()->undefined_value()); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 35 | DCHECK(!context->global_object()->IsUndefined(context->GetIsolate())); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | VisitPointer(o); |
| 39 | SerializeDeferredObjects(); |
| 40 | Pad(); |
| 41 | } |
| 42 | |
| 43 | void PartialSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code, |
| 44 | WhereToPoint where_to_point, int skip) { |
| 45 | if (obj->IsMap()) { |
| 46 | // The code-caches link to context-specific code objects, which |
| 47 | // the startup and context serializes cannot currently handle. |
| 48 | DCHECK(Map::cast(obj)->code_cache() == obj->GetHeap()->empty_fixed_array()); |
| 49 | } |
| 50 | |
| 51 | // Replace typed arrays by undefined. |
| 52 | if (obj->IsJSTypedArray()) obj = isolate_->heap()->undefined_value(); |
| 53 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 54 | if (SerializeHotObject(obj, how_to_code, where_to_point, skip)) return; |
| 55 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 56 | int root_index = root_index_map_.Lookup(obj); |
| 57 | if (root_index != RootIndexMap::kInvalidRootIndex) { |
| 58 | PutRoot(root_index, obj, how_to_code, where_to_point, skip); |
| 59 | return; |
| 60 | } |
| 61 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 62 | if (SerializeBackReference(obj, how_to_code, where_to_point, skip)) return; |
| 63 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 64 | if (ShouldBeInThePartialSnapshotCache(obj)) { |
| 65 | FlushSkip(skip); |
| 66 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 67 | int cache_index = startup_serializer_->PartialSnapshotCacheIndex(obj); |
| 68 | sink_.Put(kPartialSnapshotCache + how_to_code + where_to_point, |
| 69 | "PartialSnapshotCache"); |
| 70 | sink_.PutInt(cache_index, "partial_snapshot_cache_index"); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Pointers from the partial snapshot to the objects in the startup snapshot |
| 75 | // should go through the root array or through the partial snapshot cache. |
| 76 | // If this is not the case you may have to add something to the root array. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 77 | DCHECK(!startup_serializer_->reference_map()->Lookup(obj).is_valid()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 78 | // All the internalized strings that the partial snapshot needs should be |
| 79 | // either in the root table or in the partial snapshot cache. |
| 80 | DCHECK(!obj->IsInternalizedString()); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 81 | // Function and object templates are not context specific. |
| 82 | DCHECK(!obj->IsTemplateInfo()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 83 | |
| 84 | FlushSkip(skip); |
| 85 | |
| 86 | // Clear literal boilerplates. |
| 87 | if (obj->IsJSFunction()) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 88 | JSFunction* function = JSFunction::cast(obj); |
| 89 | LiteralsArray* literals = function->literals(); |
| 90 | for (int i = 0; i < literals->literals_count(); i++) { |
| 91 | literals->set_literal_undefined(i); |
| 92 | } |
| 93 | function->ClearTypeFeedbackInfo(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Object has not yet been serialized. Serialize it here. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 97 | ObjectSerializer serializer(this, obj, &sink_, how_to_code, where_to_point); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 98 | serializer.Serialize(); |
| 99 | } |
| 100 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 101 | bool PartialSerializer::ShouldBeInThePartialSnapshotCache(HeapObject* o) { |
| 102 | // Scripts should be referred only through shared function infos. We can't |
| 103 | // allow them to be part of the partial snapshot because they contain a |
| 104 | // unique ID, and deserializing several partial snapshots containing script |
| 105 | // would cause dupes. |
| 106 | DCHECK(!o->IsScript()); |
| 107 | return o->IsName() || o->IsSharedFunctionInfo() || o->IsHeapNumber() || |
| 108 | o->IsCode() || o->IsScopeInfo() || o->IsAccessorInfo() || |
| 109 | o->map() == |
| 110 | startup_serializer_->isolate()->heap()->fixed_cow_array_map(); |
| 111 | } |
| 112 | |
| 113 | } // namespace internal |
| 114 | } // namespace v8 |