| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_SERIALIZE_H_ |
| 6 | #define V8_SERIALIZE_H_ |
| 7 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/compiler.h" |
| 9 | #include "src/hashmap.h" |
| 10 | #include "src/heap-profiler.h" |
| 11 | #include "src/isolate.h" |
| 12 | #include "src/snapshot-source-sink.h" |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
| 17 | // A TypeCode is used to distinguish different kinds of external reference. |
| 18 | // It is a single bit to make testing for types easy. |
| 19 | enum TypeCode { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | UNCLASSIFIED, // One-of-a-kind references. |
| 21 | C_BUILTIN, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 22 | BUILTIN, |
| 23 | RUNTIME_FUNCTION, |
| 24 | IC_UTILITY, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 25 | STATS_COUNTER, |
| 26 | TOP_ADDRESS, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 27 | ACCESSOR, |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | STUB_CACHE_TABLE, |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 29 | RUNTIME_ENTRY, |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | LAZY_DEOPTIMIZATION |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | const int kTypeCodeCount = LAZY_DEOPTIMIZATION + 1; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | const int kFirstTypeCode = UNCLASSIFIED; |
| 35 | |
| 36 | const int kReferenceIdBits = 16; |
| 37 | const int kReferenceIdMask = (1 << kReferenceIdBits) - 1; |
| 38 | const int kReferenceTypeShift = kReferenceIdBits; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | const int kDeoptTableSerializeEntryCount = 64; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | |
| Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 42 | // ExternalReferenceTable is a helper class that defines the relationship |
| 43 | // between external references and their encodings. It is used to build |
| 44 | // hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder. |
| 45 | class ExternalReferenceTable { |
| 46 | public: |
| 47 | static ExternalReferenceTable* instance(Isolate* isolate); |
| 48 | |
| 49 | ~ExternalReferenceTable() { } |
| 50 | |
| 51 | int size() const { return refs_.length(); } |
| 52 | |
| 53 | Address address(int i) { return refs_[i].address; } |
| 54 | |
| 55 | uint32_t code(int i) { return refs_[i].code; } |
| 56 | |
| 57 | const char* name(int i) { return refs_[i].name; } |
| 58 | |
| 59 | int max_id(int code) { return max_id_[code]; } |
| 60 | |
| 61 | private: |
| 62 | explicit ExternalReferenceTable(Isolate* isolate) : refs_(64) { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 63 | PopulateTable(isolate); |
| Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | struct ExternalReferenceEntry { |
| 67 | Address address; |
| 68 | uint32_t code; |
| 69 | const char* name; |
| 70 | }; |
| 71 | |
| 72 | void PopulateTable(Isolate* isolate); |
| 73 | |
| 74 | // For a few types of references, we can get their address from their id. |
| 75 | void AddFromId(TypeCode type, |
| 76 | uint16_t id, |
| 77 | const char* name, |
| 78 | Isolate* isolate); |
| 79 | |
| 80 | // For other types of references, the caller will figure out the address. |
| 81 | void Add(Address address, TypeCode type, uint16_t id, const char* name); |
| 82 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 83 | void Add(Address address, const char* name) { |
| 84 | Add(address, UNCLASSIFIED, ++max_id_[UNCLASSIFIED], name); |
| 85 | } |
| 86 | |
| Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 87 | List<ExternalReferenceEntry> refs_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | uint16_t max_id_[kTypeCodeCount]; |
| Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | class ExternalReferenceEncoder { |
| 93 | public: |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 94 | explicit ExternalReferenceEncoder(Isolate* isolate); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | |
| 96 | uint32_t Encode(Address key) const; |
| 97 | |
| 98 | const char* NameOfAddress(Address key) const; |
| 99 | |
| 100 | private: |
| 101 | HashMap encodings_; |
| 102 | static uint32_t Hash(Address key) { |
| 103 | return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >> 2); |
| 104 | } |
| 105 | |
| 106 | int IndexOf(Address key) const; |
| 107 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | void Put(Address key, int index); |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 109 | |
| 110 | Isolate* isolate_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | |
| 114 | class ExternalReferenceDecoder { |
| 115 | public: |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 116 | explicit ExternalReferenceDecoder(Isolate* isolate); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | ~ExternalReferenceDecoder(); |
| 118 | |
| 119 | Address Decode(uint32_t key) const { |
| 120 | if (key == 0) return NULL; |
| 121 | return *Lookup(key); |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | Address** encodings_; |
| 126 | |
| 127 | Address* Lookup(uint32_t key) const { |
| 128 | int type = key >> kReferenceTypeShift; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | DCHECK(kFirstTypeCode <= type && type < kTypeCodeCount); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | int id = key & kReferenceIdMask; |
| 131 | return &encodings_[type][id]; |
| 132 | } |
| 133 | |
| 134 | void Put(uint32_t key, Address value) { |
| 135 | *Lookup(key) = value; |
| 136 | } |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 137 | |
| 138 | Isolate* isolate_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 142 | // The Serializer/Deserializer class is a common superclass for Serializer and |
| 143 | // Deserializer which is used to store common constants and methods used by |
| 144 | // both. |
| 145 | class SerializerDeserializer: public ObjectVisitor { |
| 146 | public: |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 147 | static void Iterate(Isolate* isolate, ObjectVisitor* visitor); |
| 148 | |
| 149 | static int nop() { return kNop; } |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 150 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 151 | protected: |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 152 | // Where the pointed-to object can be found: |
| 153 | enum Where { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 154 | kNewObject = 0, // Object is next in snapshot. |
| 155 | // 1-6 One per space. |
| 156 | kRootArray = 0x9, // Object is found in root array. |
| 157 | kPartialSnapshotCache = 0xa, // Object is in the cache. |
| 158 | kExternalReference = 0xb, // Pointer to an external reference. |
| 159 | kSkip = 0xc, // Skip n bytes. |
| 160 | kBuiltin = 0xd, // Builtin code object. |
| 161 | kAttachedReference = 0xe, // Object is described in an attached list. |
| 162 | kNop = 0xf, // Does nothing, used to pad. |
| 163 | kBackref = 0x10, // Object is described relative to end. |
| 164 | // 0x11-0x16 One per space. |
| 165 | kBackrefWithSkip = 0x18, // Object is described relative to end. |
| 166 | // 0x19-0x1e One per space. |
| 167 | // 0x20-0x3f Used by misc. tags below. |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 168 | kPointedToMask = 0x3f |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 169 | }; |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 170 | |
| 171 | // How to code the pointer to the object. |
| 172 | enum HowToCode { |
| 173 | kPlain = 0, // Straight pointer. |
| 174 | // What this means depends on the architecture: |
| 175 | kFromCode = 0x40, // A pointer inlined in code. |
| 176 | kHowToCodeMask = 0x40 |
| 177 | }; |
| 178 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 179 | // For kRootArrayConstants |
| 180 | enum WithSkip { |
| 181 | kNoSkipDistance = 0, |
| 182 | kHasSkipDistance = 0x40, |
| 183 | kWithSkipMask = 0x40 |
| 184 | }; |
| 185 | |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 186 | // Where to point within the object. |
| 187 | enum WhereToPoint { |
| 188 | kStartOfObject = 0, |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 189 | kInnerPointer = 0x80, // First insn in code object or payload of cell. |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 190 | kWhereToPointMask = 0x80 |
| 191 | }; |
| 192 | |
| 193 | // Misc. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 194 | // Raw data to be copied from the snapshot. This byte code does not advance |
| 195 | // the current pointer, which is used for code objects, where we write the |
| 196 | // entire code in one memcpy, then fix up stuff with kSkip and other byte |
| 197 | // codes that overwrite data. |
| 198 | static const int kRawData = 0x20; |
| 199 | // Some common raw lengths: 0x21-0x3f. These autoadvance the current pointer. |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 200 | // A tag emitted at strategic points in the snapshot to delineate sections. |
| 201 | // If the deserializer does not find these at the expected moments then it |
| 202 | // is an indication that the snapshot and the VM do not fit together. |
| 203 | // Examine the build process for architecture, version or configuration |
| 204 | // mismatches. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 205 | static const int kSynchronize = 0x70; |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 206 | // Used for the source code of the natives, which is in the executable, but |
| 207 | // is referred to from external strings in the snapshot. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 208 | static const int kNativesStringResource = 0x71; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 209 | static const int kRepeat = 0x72; |
| 210 | static const int kConstantRepeat = 0x73; |
| 211 | // 0x73-0x7f Repeat last word (subtract 0x72 to get the count). |
| 212 | static const int kMaxRepeats = 0x7f - 0x72; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 213 | static int CodeForRepeats(int repeats) { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 214 | DCHECK(repeats >= 1 && repeats <= kMaxRepeats); |
| 215 | return 0x72 + repeats; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 216 | } |
| 217 | static int RepeatsForCode(int byte_code) { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 218 | DCHECK(byte_code >= kConstantRepeat && byte_code <= 0x7f); |
| 219 | return byte_code - 0x72; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 220 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 221 | static const int kRootArrayConstants = 0xa0; |
| 222 | // 0xa0-0xbf Things from the first 32 elements of the root array. |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 223 | static const int kRootArrayNumberOfConstantEncodings = 0x20; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 224 | static int RootArrayConstantFromByteCode(int byte_code) { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 225 | return byte_code & 0x1f; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 226 | } |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 227 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 228 | static const int kNumberOfSpaces = LO_SPACE; |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 229 | static const int kAnyOldSpace = -1; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 230 | |
| 231 | // A bitmask for getting the space out of an instruction. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 232 | static const int kSpaceMask = 7; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | // A Deserializer reads a snapshot and reconstructs the Object graph it defines. |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 237 | class Deserializer: public SerializerDeserializer { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 238 | public: |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 239 | // Create a deserializer from a snapshot byte source. |
| 240 | explicit Deserializer(SnapshotByteSource* source); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 242 | virtual ~Deserializer(); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | |
| 244 | // Deserialize the snapshot into an empty heap. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 245 | void Deserialize(Isolate* isolate); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 246 | |
| 247 | // Deserialize a single object and the objects reachable from it. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 248 | void DeserializePartial(Isolate* isolate, Object** root); |
| 249 | |
| 250 | void set_reservation(int space_number, int reservation) { |
| 251 | DCHECK(space_number >= 0); |
| 252 | DCHECK(space_number <= LAST_SPACE); |
| 253 | reservations_[space_number] = reservation; |
| 254 | } |
| 255 | |
| 256 | void FlushICacheForNewCodeObjects(); |
| 257 | |
| 258 | // Serialized user code reference certain objects that are provided in a list |
| 259 | // By calling this method, we assume that we are deserializing user code. |
| 260 | void SetAttachedObjects(Vector<Handle<Object> >* attached_objects) { |
| 261 | attached_objects_ = attached_objects; |
| 262 | } |
| 263 | |
| 264 | bool deserializing_user_code() { return attached_objects_ != NULL; } |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 265 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 266 | private: |
| 267 | virtual void VisitPointers(Object** start, Object** end); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 268 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 269 | virtual void VisitRuntimeEntry(RelocInfo* rinfo) { |
| 270 | UNREACHABLE(); |
| 271 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 272 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 273 | // Allocation sites are present in the snapshot, and must be linked into |
| 274 | // a list at deserialization time. |
| 275 | void RelinkAllocationSite(AllocationSite* site); |
| 276 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 277 | // Fills in some heap data in an area from start to end (non-inclusive). The |
| 278 | // space id is used for the write barrier. The object_address is the address |
| 279 | // of the object we are writing into, or NULL if we are not writing into an |
| 280 | // object, i.e. if we are writing a series of tagged values that are not on |
| 281 | // the heap. |
| 282 | void ReadChunk( |
| 283 | Object** start, Object** end, int space, Address object_address); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 284 | void ReadObject(int space_number, Object** write_back); |
| 285 | |
| 286 | // Special handling for serialized code like hooking up internalized strings. |
| 287 | HeapObject* ProcessNewObjectFromSerializedCode(HeapObject* obj); |
| 288 | Object* ProcessBackRefInSerializedCode(Object* obj); |
| 289 | |
| 290 | // This routine both allocates a new object, and also keeps |
| 291 | // track of where objects have been allocated so that we can |
| 292 | // fix back references when deserializing. |
| 293 | Address Allocate(int space_index, int size) { |
| 294 | Address address = high_water_[space_index]; |
| 295 | high_water_[space_index] = address + size; |
| 296 | return address; |
| 297 | } |
| 298 | |
| 299 | // This returns the address of an object that has been described in the |
| 300 | // snapshot as being offset bytes back in a particular space. |
| 301 | HeapObject* GetAddressFromEnd(int space) { |
| 302 | int offset = source_->GetInt(); |
| 303 | offset <<= kObjectAlignmentBits; |
| 304 | return HeapObject::FromAddress(high_water_[space] - offset); |
| 305 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 306 | |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 307 | // Cached current isolate. |
| 308 | Isolate* isolate_; |
| 309 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 310 | // Objects from the attached object descriptions in the serialized user code. |
| 311 | Vector<Handle<Object> >* attached_objects_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 312 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 313 | SnapshotByteSource* source_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 314 | // This is the address of the next object that will be allocated in each |
| 315 | // space. It is used to calculate the addresses of back-references. |
| 316 | Address high_water_[LAST_SPACE + 1]; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 317 | |
| 318 | int reservations_[LAST_SPACE + 1]; |
| 319 | static const intptr_t kUninitializedReservation = -1; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 320 | |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 321 | ExternalReferenceDecoder* external_reference_decoder_; |
| 322 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | DISALLOW_COPY_AND_ASSIGN(Deserializer); |
| 324 | }; |
| 325 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 326 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 327 | // Mapping objects to their location after deserialization. |
| 328 | // This is used during building, but not at runtime by V8. |
| 329 | class SerializationAddressMapper { |
| 330 | public: |
| 331 | SerializationAddressMapper() |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 332 | : no_allocation_(), |
| 333 | serialization_map_(new HashMap(HashMap::PointersMatch)) { } |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 334 | |
| 335 | ~SerializationAddressMapper() { |
| 336 | delete serialization_map_; |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | bool IsMapped(HeapObject* obj) { |
| 340 | return serialization_map_->Lookup(Key(obj), Hash(obj), false) != NULL; |
| 341 | } |
| 342 | |
| 343 | int MappedTo(HeapObject* obj) { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 344 | DCHECK(IsMapped(obj)); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 345 | return static_cast<int>(reinterpret_cast<intptr_t>( |
| 346 | serialization_map_->Lookup(Key(obj), Hash(obj), false)->value)); |
| 347 | } |
| 348 | |
| 349 | void AddMapping(HeapObject* obj, int to) { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 350 | DCHECK(!IsMapped(obj)); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 351 | HashMap::Entry* entry = |
| 352 | serialization_map_->Lookup(Key(obj), Hash(obj), true); |
| 353 | entry->value = Value(to); |
| 354 | } |
| 355 | |
| 356 | private: |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 357 | static uint32_t Hash(HeapObject* obj) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 358 | return static_cast<int32_t>(reinterpret_cast<intptr_t>(obj->address())); |
| 359 | } |
| 360 | |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 361 | static void* Key(HeapObject* obj) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 362 | return reinterpret_cast<void*>(obj->address()); |
| 363 | } |
| 364 | |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 365 | static void* Value(int v) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 366 | return reinterpret_cast<void*>(v); |
| 367 | } |
| 368 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 369 | DisallowHeapAllocation no_allocation_; |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 370 | HashMap* serialization_map_; |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 371 | DISALLOW_COPY_AND_ASSIGN(SerializationAddressMapper); |
| 372 | }; |
| 373 | |
| 374 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 375 | class CodeAddressMap; |
| 376 | |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 377 | // There can be only one serializer per V8 process. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 378 | class Serializer : public SerializerDeserializer { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 379 | public: |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 380 | Serializer(Isolate* isolate, SnapshotByteSink* sink); |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 381 | ~Serializer(); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 382 | void VisitPointers(Object** start, Object** end); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 383 | // You can call this after serialization to find out how much space was used |
| 384 | // in each space. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 385 | int CurrentAllocationAddress(int space) const { |
| 386 | DCHECK(space < kNumberOfSpaces); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 387 | return fullness_[space]; |
| 388 | } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 389 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 390 | Isolate* isolate() const { return isolate_; } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 391 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 392 | SerializationAddressMapper* address_mapper() { return &address_mapper_; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 393 | void PutRoot(int index, |
| 394 | HeapObject* object, |
| 395 | HowToCode how, |
| 396 | WhereToPoint where, |
| 397 | int skip); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 398 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 399 | protected: |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 400 | static const int kInvalidRootIndex = -1; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 401 | |
| 402 | int RootIndex(HeapObject* heap_object, HowToCode from); |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 403 | intptr_t root_index_wave_front() { return root_index_wave_front_; } |
| 404 | void set_root_index_wave_front(intptr_t value) { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 405 | DCHECK(value >= root_index_wave_front_); |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 406 | root_index_wave_front_ = value; |
| 407 | } |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 408 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 409 | class ObjectSerializer : public ObjectVisitor { |
| 410 | public: |
| 411 | ObjectSerializer(Serializer* serializer, |
| 412 | Object* o, |
| 413 | SnapshotByteSink* sink, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 414 | HowToCode how_to_code, |
| 415 | WhereToPoint where_to_point) |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 416 | : serializer_(serializer), |
| 417 | object_(HeapObject::cast(o)), |
| 418 | sink_(sink), |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 419 | reference_representation_(how_to_code + where_to_point), |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 420 | bytes_processed_so_far_(0), |
| 421 | code_object_(o->IsCode()), |
| 422 | code_has_been_output_(false) { } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 423 | void Serialize(); |
| 424 | void VisitPointers(Object** start, Object** end); |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 425 | void VisitEmbeddedPointer(RelocInfo* target); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 426 | void VisitExternalReference(Address* p); |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 427 | void VisitExternalReference(RelocInfo* rinfo); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 428 | void VisitCodeTarget(RelocInfo* target); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 429 | void VisitCodeEntry(Address entry_address); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 430 | void VisitCell(RelocInfo* rinfo); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 431 | void VisitRuntimeEntry(RelocInfo* reloc); |
| 432 | // Used for seralizing the external strings that hold the natives source. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 433 | void VisitExternalOneByteString( |
| 434 | v8::String::ExternalOneByteStringResource** resource); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 435 | // We can't serialize a heap with external two byte strings. |
| 436 | void VisitExternalTwoByteString( |
| 437 | v8::String::ExternalStringResource** resource) { |
| 438 | UNREACHABLE(); |
| 439 | } |
| 440 | |
| 441 | private: |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 442 | enum ReturnSkip { kCanReturnSkipInsteadOfSkipping, kIgnoringReturn }; |
| 443 | // This function outputs or skips the raw data between the last pointer and |
| 444 | // up to the current position. It optionally can just return the number of |
| 445 | // bytes to skip instead of performing a skip instruction, in case the skip |
| 446 | // can be merged into the next instruction. |
| 447 | int OutputRawData(Address up_to, ReturnSkip return_skip = kIgnoringReturn); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 448 | |
| 449 | Serializer* serializer_; |
| 450 | HeapObject* object_; |
| 451 | SnapshotByteSink* sink_; |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 452 | int reference_representation_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 453 | int bytes_processed_so_far_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 454 | bool code_object_; |
| 455 | bool code_has_been_output_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 456 | }; |
| 457 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 458 | virtual void SerializeObject(Object* o, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 459 | HowToCode how_to_code, |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 460 | WhereToPoint where_to_point, |
| 461 | int skip) = 0; |
| 462 | void SerializeReferenceToPreviousObject(HeapObject* heap_object, |
| 463 | HowToCode how_to_code, |
| 464 | WhereToPoint where_to_point, |
| 465 | int skip); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 466 | void InitializeAllocators(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 467 | // This will return the space for an object. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 468 | static int SpaceOfObject(HeapObject* object); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 469 | int Allocate(int space, int size); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 470 | int EncodeExternalReference(Address addr) { |
| 471 | return external_reference_encoder_->Encode(addr); |
| 472 | } |
| 473 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 474 | int SpaceAreaSize(int space); |
| 475 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 476 | // Some roots should not be serialized, because their actual value depends on |
| 477 | // absolute addresses and they are reset after deserialization, anyway. |
| 478 | bool ShouldBeSkipped(Object** current); |
| 479 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 480 | Isolate* isolate_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 481 | // Keep track of the fullness of each space in order to generate |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 482 | // relative addresses for back references. |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 483 | int fullness_[LAST_SPACE + 1]; |
| 484 | SnapshotByteSink* sink_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 485 | ExternalReferenceEncoder* external_reference_encoder_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 486 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 487 | SerializationAddressMapper address_mapper_; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 488 | intptr_t root_index_wave_front_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 489 | void Pad(); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 490 | |
| 491 | friend class ObjectSerializer; |
| 492 | friend class Deserializer; |
| 493 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 494 | // We may not need the code address map for logging for every instance |
| 495 | // of the serializer. Initialize it on demand. |
| 496 | void InitializeCodeAddressMap(); |
| 497 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 498 | private: |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 499 | CodeAddressMap* code_address_map_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 500 | DISALLOW_COPY_AND_ASSIGN(Serializer); |
| 501 | }; |
| 502 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 503 | |
| 504 | class PartialSerializer : public Serializer { |
| 505 | public: |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 506 | PartialSerializer(Isolate* isolate, |
| 507 | Serializer* startup_snapshot_serializer, |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 508 | SnapshotByteSink* sink) |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 509 | : Serializer(isolate, sink), |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 510 | startup_serializer_(startup_snapshot_serializer) { |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 511 | set_root_index_wave_front(Heap::kStrongRootListLength); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 512 | InitializeCodeAddressMap(); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | // Serialize the objects reachable from a single object pointer. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 516 | void Serialize(Object** o); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 517 | virtual void SerializeObject(Object* o, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 518 | HowToCode how_to_code, |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 519 | WhereToPoint where_to_point, |
| 520 | int skip); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 521 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 522 | private: |
| 523 | int PartialSnapshotCacheIndex(HeapObject* o); |
| 524 | bool ShouldBeInThePartialSnapshotCache(HeapObject* o) { |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 525 | // Scripts should be referred only through shared function infos. We can't |
| 526 | // allow them to be part of the partial snapshot because they contain a |
| 527 | // unique ID, and deserializing several partial snapshots containing script |
| 528 | // would cause dupes. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 529 | DCHECK(!o->IsScript()); |
| 530 | return o->IsName() || o->IsSharedFunctionInfo() || |
| Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 531 | o->IsHeapNumber() || o->IsCode() || |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 532 | o->IsScopeInfo() || |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 533 | o->map() == |
| 534 | startup_serializer_->isolate()->heap()->fixed_cow_array_map(); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 537 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 538 | Serializer* startup_serializer_; |
| 539 | DISALLOW_COPY_AND_ASSIGN(PartialSerializer); |
| 540 | }; |
| 541 | |
| 542 | |
| 543 | class StartupSerializer : public Serializer { |
| 544 | public: |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 545 | StartupSerializer(Isolate* isolate, SnapshotByteSink* sink) |
| 546 | : Serializer(isolate, sink) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 547 | // Clear the cache of objects used by the partial snapshot. After the |
| 548 | // strong roots have been serialized we can create a partial snapshot |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 549 | // which will repopulate the cache with objects needed by that partial |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 550 | // snapshot. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 551 | isolate->set_serialize_partial_snapshot_cache_length(0); |
| 552 | InitializeCodeAddressMap(); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 553 | } |
| 554 | // Serialize the current state of the heap. The order is: |
| 555 | // 1) Strong references. |
| 556 | // 2) Partial snapshot cache. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 557 | // 3) Weak references (e.g. the string table). |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 558 | virtual void SerializeStrongReferences(); |
| 559 | virtual void SerializeObject(Object* o, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 560 | HowToCode how_to_code, |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 561 | WhereToPoint where_to_point, |
| 562 | int skip); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 563 | void SerializeWeakReferences(); |
| 564 | void Serialize() { |
| 565 | SerializeStrongReferences(); |
| 566 | SerializeWeakReferences(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 567 | Pad(); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | private: |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 571 | DISALLOW_COPY_AND_ASSIGN(StartupSerializer); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 572 | }; |
| 573 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 574 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 575 | class CodeSerializer : public Serializer { |
| 576 | public: |
| 577 | CodeSerializer(Isolate* isolate, SnapshotByteSink* sink, String* source) |
| 578 | : Serializer(isolate, sink), source_(source) { |
| 579 | set_root_index_wave_front(Heap::kStrongRootListLength); |
| 580 | InitializeCodeAddressMap(); |
| 581 | } |
| 582 | |
| 583 | static ScriptData* Serialize(Isolate* isolate, |
| 584 | Handle<SharedFunctionInfo> info, |
| 585 | Handle<String> source); |
| 586 | |
| 587 | virtual void SerializeObject(Object* o, HowToCode how_to_code, |
| 588 | WhereToPoint where_to_point, int skip); |
| 589 | |
| 590 | static Handle<SharedFunctionInfo> Deserialize(Isolate* isolate, |
| 591 | ScriptData* data, |
| 592 | Handle<String> source); |
| 593 | |
| 594 | static const int kSourceObjectIndex = 0; |
| 595 | static const int kCodeStubsBaseIndex = 1; |
| 596 | |
| 597 | String* source() { |
| 598 | DCHECK(!AllowHeapAllocation::IsAllowed()); |
| 599 | return source_; |
| 600 | } |
| 601 | |
| 602 | List<uint32_t>* stub_keys() { return &stub_keys_; } |
| 603 | |
| 604 | private: |
| 605 | void SerializeBuiltin(Code* builtin, HowToCode how_to_code, |
| 606 | WhereToPoint where_to_point, int skip); |
| 607 | void SerializeCodeStub(Code* code, HowToCode how_to_code, |
| 608 | WhereToPoint where_to_point, int skip); |
| 609 | void SerializeSourceObject(HowToCode how_to_code, WhereToPoint where_to_point, |
| 610 | int skip); |
| 611 | void SerializeHeapObject(HeapObject* heap_object, HowToCode how_to_code, |
| 612 | WhereToPoint where_to_point, int skip); |
| 613 | int AddCodeStubKey(uint32_t stub_key); |
| 614 | |
| 615 | DisallowHeapAllocation no_gc_; |
| 616 | String* source_; |
| 617 | List<uint32_t> stub_keys_; |
| 618 | DISALLOW_COPY_AND_ASSIGN(CodeSerializer); |
| 619 | }; |
| 620 | |
| 621 | |
| 622 | // Wrapper around ScriptData to provide code-serializer-specific functionality. |
| 623 | class SerializedCodeData { |
| 624 | public: |
| 625 | // Used by when consuming. |
| 626 | explicit SerializedCodeData(ScriptData* data, String* source) |
| 627 | : script_data_(data), owns_script_data_(false) { |
| 628 | DisallowHeapAllocation no_gc; |
| 629 | CHECK(IsSane(source)); |
| 630 | } |
| 631 | |
| 632 | // Used when producing. |
| 633 | SerializedCodeData(List<byte>* payload, CodeSerializer* cs); |
| 634 | |
| 635 | ~SerializedCodeData() { |
| 636 | if (owns_script_data_) delete script_data_; |
| 637 | } |
| 638 | |
| 639 | // Return ScriptData object and relinquish ownership over it to the caller. |
| 640 | ScriptData* GetScriptData() { |
| 641 | ScriptData* result = script_data_; |
| 642 | script_data_ = NULL; |
| 643 | DCHECK(owns_script_data_); |
| 644 | owns_script_data_ = false; |
| 645 | return result; |
| 646 | } |
| 647 | |
| 648 | Vector<const uint32_t> CodeStubKeys() const { |
| 649 | return Vector<const uint32_t>( |
| 650 | reinterpret_cast<const uint32_t*>(script_data_->data() + kHeaderSize), |
| 651 | GetHeaderValue(kNumCodeStubKeysOffset)); |
| 652 | } |
| 653 | |
| 654 | const byte* Payload() const { |
| 655 | int code_stubs_size = GetHeaderValue(kNumCodeStubKeysOffset) * kInt32Size; |
| 656 | return script_data_->data() + kHeaderSize + code_stubs_size; |
| 657 | } |
| 658 | |
| 659 | int PayloadLength() const { |
| 660 | int payload_length = GetHeaderValue(kPayloadLengthOffset); |
| 661 | DCHECK_EQ(script_data_->data() + script_data_->length(), |
| 662 | Payload() + payload_length); |
| 663 | return payload_length; |
| 664 | } |
| 665 | |
| 666 | int GetReservation(int space) const { |
| 667 | return GetHeaderValue(kReservationsOffset + space); |
| 668 | } |
| 669 | |
| 670 | private: |
| 671 | void SetHeaderValue(int offset, int value) { |
| 672 | reinterpret_cast<int*>(const_cast<byte*>(script_data_->data()))[offset] = |
| 673 | value; |
| 674 | } |
| 675 | |
| 676 | int GetHeaderValue(int offset) const { |
| 677 | return reinterpret_cast<const int*>(script_data_->data())[offset]; |
| 678 | } |
| 679 | |
| 680 | bool IsSane(String* source); |
| 681 | |
| 682 | int CheckSum(String* source); |
| 683 | |
| 684 | // The data header consists of int-sized entries: |
| 685 | // [0] version hash |
| 686 | // [1] number of code stub keys |
| 687 | // [2] payload length |
| 688 | // [3..9] reservation sizes for spaces from NEW_SPACE to PROPERTY_CELL_SPACE. |
| 689 | static const int kCheckSumOffset = 0; |
| 690 | static const int kNumCodeStubKeysOffset = 1; |
| 691 | static const int kPayloadLengthOffset = 2; |
| 692 | static const int kReservationsOffset = 3; |
| 693 | |
| 694 | static const int kNumSpaces = PROPERTY_CELL_SPACE - NEW_SPACE + 1; |
| 695 | static const int kHeaderEntries = kReservationsOffset + kNumSpaces; |
| 696 | static const int kHeaderSize = kHeaderEntries * kIntSize; |
| 697 | |
| 698 | // Following the header, we store, in sequential order |
| 699 | // - code stub keys |
| 700 | // - serialization payload |
| 701 | |
| 702 | ScriptData* script_data_; |
| 703 | bool owns_script_data_; |
| 704 | }; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 705 | } } // namespace v8::internal |
| 706 | |
| 707 | #endif // V8_SERIALIZE_H_ |