| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | #ifndef V8_SERIALIZE_H_ |
| 29 | #define V8_SERIALIZE_H_ |
| 30 | |
| 31 | #include "hashmap.h" |
| 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | // A TypeCode is used to distinguish different kinds of external reference. |
| 37 | // It is a single bit to make testing for types easy. |
| 38 | enum TypeCode { |
| 39 | UNCLASSIFIED, // One-of-a-kind references. |
| 40 | BUILTIN, |
| 41 | RUNTIME_FUNCTION, |
| 42 | IC_UTILITY, |
| 43 | DEBUG_ADDRESS, |
| 44 | STATS_COUNTER, |
| 45 | TOP_ADDRESS, |
| 46 | C_BUILTIN, |
| 47 | EXTENSION, |
| 48 | ACCESSOR, |
| 49 | RUNTIME_ENTRY, |
| 50 | STUB_CACHE_TABLE |
| 51 | }; |
| 52 | |
| 53 | const int kTypeCodeCount = STUB_CACHE_TABLE + 1; |
| 54 | const int kFirstTypeCode = UNCLASSIFIED; |
| 55 | |
| 56 | const int kReferenceIdBits = 16; |
| 57 | const int kReferenceIdMask = (1 << kReferenceIdBits) - 1; |
| 58 | const int kReferenceTypeShift = kReferenceIdBits; |
| 59 | const int kDebugRegisterBits = 4; |
| 60 | const int kDebugIdShift = kDebugRegisterBits; |
| 61 | |
| 62 | |
| Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 63 | // ExternalReferenceTable is a helper class that defines the relationship |
| 64 | // between external references and their encodings. It is used to build |
| 65 | // hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder. |
| 66 | class ExternalReferenceTable { |
| 67 | public: |
| 68 | static ExternalReferenceTable* instance(Isolate* isolate); |
| 69 | |
| 70 | ~ExternalReferenceTable() { } |
| 71 | |
| 72 | int size() const { return refs_.length(); } |
| 73 | |
| 74 | Address address(int i) { return refs_[i].address; } |
| 75 | |
| 76 | uint32_t code(int i) { return refs_[i].code; } |
| 77 | |
| 78 | const char* name(int i) { return refs_[i].name; } |
| 79 | |
| 80 | int max_id(int code) { return max_id_[code]; } |
| 81 | |
| 82 | private: |
| 83 | explicit ExternalReferenceTable(Isolate* isolate) : refs_(64) { |
| 84 | PopulateTable(isolate); |
| 85 | } |
| 86 | |
| 87 | struct ExternalReferenceEntry { |
| 88 | Address address; |
| 89 | uint32_t code; |
| 90 | const char* name; |
| 91 | }; |
| 92 | |
| 93 | void PopulateTable(Isolate* isolate); |
| 94 | |
| 95 | // For a few types of references, we can get their address from their id. |
| 96 | void AddFromId(TypeCode type, |
| 97 | uint16_t id, |
| 98 | const char* name, |
| 99 | Isolate* isolate); |
| 100 | |
| 101 | // For other types of references, the caller will figure out the address. |
| 102 | void Add(Address address, TypeCode type, uint16_t id, const char* name); |
| 103 | |
| 104 | List<ExternalReferenceEntry> refs_; |
| 105 | int max_id_[kTypeCodeCount]; |
| 106 | }; |
| 107 | |
| 108 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | class ExternalReferenceEncoder { |
| 110 | public: |
| 111 | ExternalReferenceEncoder(); |
| 112 | |
| 113 | uint32_t Encode(Address key) const; |
| 114 | |
| 115 | const char* NameOfAddress(Address key) const; |
| 116 | |
| 117 | private: |
| 118 | HashMap encodings_; |
| 119 | static uint32_t Hash(Address key) { |
| 120 | return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >> 2); |
| 121 | } |
| 122 | |
| 123 | int IndexOf(Address key) const; |
| 124 | |
| 125 | static bool Match(void* key1, void* key2) { return key1 == key2; } |
| 126 | |
| 127 | void Put(Address key, int index); |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 128 | |
| 129 | Isolate* isolate_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | |
| 133 | class ExternalReferenceDecoder { |
| 134 | public: |
| 135 | ExternalReferenceDecoder(); |
| 136 | ~ExternalReferenceDecoder(); |
| 137 | |
| 138 | Address Decode(uint32_t key) const { |
| 139 | if (key == 0) return NULL; |
| 140 | return *Lookup(key); |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | Address** encodings_; |
| 145 | |
| 146 | Address* Lookup(uint32_t key) const { |
| 147 | int type = key >> kReferenceTypeShift; |
| 148 | ASSERT(kFirstTypeCode <= type && type < kTypeCodeCount); |
| 149 | int id = key & kReferenceIdMask; |
| 150 | return &encodings_[type][id]; |
| 151 | } |
| 152 | |
| 153 | void Put(uint32_t key, Address value) { |
| 154 | *Lookup(key) = value; |
| 155 | } |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 156 | |
| 157 | Isolate* isolate_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 161 | class SnapshotByteSource { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | public: |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 163 | SnapshotByteSource(const byte* array, int length) |
| 164 | : data_(array), length_(length), position_(0) { } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 166 | bool HasMore() { return position_ < length_; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 168 | int Get() { |
| 169 | ASSERT(position_ < length_); |
| 170 | return data_[position_++]; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 173 | inline void CopyRaw(byte* to, int number_of_bytes); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 175 | inline int GetInt(); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 177 | bool AtEOF() { |
| 178 | return position_ == length_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 181 | int position() { return position_; } |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 182 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | private: |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 184 | const byte* data_; |
| 185 | int length_; |
| 186 | int position_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 190 | #define COMMON_RAW_LENGTHS(f) \ |
| 191 | f(1, 1) \ |
| 192 | f(2, 2) \ |
| 193 | f(3, 3) \ |
| 194 | f(4, 4) \ |
| 195 | f(5, 5) \ |
| 196 | f(6, 6) \ |
| 197 | f(7, 7) \ |
| 198 | f(8, 8) \ |
| 199 | f(9, 12) \ |
| 200 | f(10, 16) \ |
| 201 | f(11, 20) \ |
| 202 | f(12, 24) \ |
| 203 | f(13, 28) \ |
| 204 | f(14, 32) \ |
| 205 | f(15, 36) |
| 206 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 207 | // The Serializer/Deserializer class is a common superclass for Serializer and |
| 208 | // Deserializer which is used to store common constants and methods used by |
| 209 | // both. |
| 210 | class SerializerDeserializer: public ObjectVisitor { |
| 211 | public: |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 212 | static void Iterate(ObjectVisitor* visitor); |
| 213 | static void SetSnapshotCacheSize(int size); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 214 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 215 | protected: |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 216 | // Where the pointed-to object can be found: |
| 217 | enum Where { |
| 218 | kNewObject = 0, // Object is next in snapshot. |
| 219 | // 1-8 One per space. |
| 220 | kRootArray = 0x9, // Object is found in root array. |
| 221 | kPartialSnapshotCache = 0xa, // Object is in the cache. |
| 222 | kExternalReference = 0xb, // Pointer to an external reference. |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 223 | kSkip = 0xc, // Skip a pointer sized cell. |
| 224 | // 0xd-0xf Free. |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 225 | kBackref = 0x10, // Object is described relative to end. |
| 226 | // 0x11-0x18 One per space. |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 227 | // 0x19-0x1f Free. |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 228 | kFromStart = 0x20, // Object is described relative to start. |
| 229 | // 0x21-0x28 One per space. |
| 230 | // 0x29-0x2f Free. |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 231 | // 0x30-0x3f Used by misc. tags below. |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 232 | kPointedToMask = 0x3f |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 233 | }; |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 234 | |
| 235 | // How to code the pointer to the object. |
| 236 | enum HowToCode { |
| 237 | kPlain = 0, // Straight pointer. |
| 238 | // What this means depends on the architecture: |
| 239 | kFromCode = 0x40, // A pointer inlined in code. |
| 240 | kHowToCodeMask = 0x40 |
| 241 | }; |
| 242 | |
| 243 | // Where to point within the object. |
| 244 | enum WhereToPoint { |
| 245 | kStartOfObject = 0, |
| 246 | kFirstInstruction = 0x80, |
| 247 | kWhereToPointMask = 0x80 |
| 248 | }; |
| 249 | |
| 250 | // Misc. |
| 251 | // Raw data to be copied from the snapshot. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 252 | static const int kRawData = 0x30; |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 253 | // Some common raw lengths: 0x31-0x3f |
| 254 | // A tag emitted at strategic points in the snapshot to delineate sections. |
| 255 | // If the deserializer does not find these at the expected moments then it |
| 256 | // is an indication that the snapshot and the VM do not fit together. |
| 257 | // Examine the build process for architecture, version or configuration |
| 258 | // mismatches. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 259 | static const int kSynchronize = 0x70; |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 260 | // Used for the source code of the natives, which is in the executable, but |
| 261 | // is referred to from external strings in the snapshot. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 262 | static const int kNativesStringResource = 0x71; |
| 263 | static const int kNewPage = 0x72; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 264 | static const int kRepeat = 0x73; |
| 265 | static const int kConstantRepeat = 0x74; |
| 266 | // 0x74-0x7f Repeat last word (subtract 0x73 to get the count). |
| 267 | static const int kMaxRepeats = 0x7f - 0x73; |
| 268 | static int CodeForRepeats(int repeats) { |
| 269 | ASSERT(repeats >= 1 && repeats <= kMaxRepeats); |
| 270 | return 0x73 + repeats; |
| 271 | } |
| 272 | static int RepeatsForCode(int byte_code) { |
| 273 | ASSERT(byte_code >= kConstantRepeat && byte_code <= 0x7f); |
| 274 | return byte_code - 0x73; |
| 275 | } |
| 276 | static const int kRootArrayLowConstants = 0xb0; |
| 277 | // 0xb0-0xbf Things from the first 16 elements of the root array. |
| 278 | static const int kRootArrayHighConstants = 0xf0; |
| 279 | // 0xf0-0xff Things from the next 16 elements of the root array. |
| 280 | static const int kRootArrayNumberOfConstantEncodings = 0x20; |
| 281 | static const int kRootArrayNumberOfLowConstantEncodings = 0x10; |
| 282 | static int RootArrayConstantFromByteCode(int byte_code) { |
| 283 | int constant = (byte_code & 0xf) | ((byte_code & 0x40) >> 2); |
| 284 | ASSERT(constant >= 0 && constant < kRootArrayNumberOfConstantEncodings); |
| 285 | return constant; |
| 286 | } |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 287 | |
| 288 | |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 289 | static const int kLargeData = LAST_SPACE; |
| 290 | static const int kLargeCode = kLargeData + 1; |
| 291 | static const int kLargeFixedArray = kLargeCode + 1; |
| 292 | static const int kNumberOfSpaces = kLargeFixedArray + 1; |
| 293 | static const int kAnyOldSpace = -1; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 294 | |
| 295 | // A bitmask for getting the space out of an instruction. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 296 | static const int kSpaceMask = 15; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 297 | |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 298 | static inline bool SpaceIsLarge(int space) { return space >= kLargeData; } |
| 299 | static inline bool SpaceIsPaged(int space) { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 300 | return space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE; |
| 301 | } |
| 302 | }; |
| 303 | |
| 304 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 305 | int SnapshotByteSource::GetInt() { |
| 306 | // A little unwind to catch the really small ints. |
| 307 | int snapshot_byte = Get(); |
| 308 | if ((snapshot_byte & 0x80) == 0) { |
| 309 | return snapshot_byte; |
| 310 | } |
| 311 | int accumulator = (snapshot_byte & 0x7f) << 7; |
| 312 | while (true) { |
| 313 | snapshot_byte = Get(); |
| 314 | if ((snapshot_byte & 0x80) == 0) { |
| 315 | return accumulator | snapshot_byte; |
| 316 | } |
| 317 | accumulator = (accumulator | (snapshot_byte & 0x7f)) << 7; |
| 318 | } |
| 319 | UNREACHABLE(); |
| 320 | return accumulator; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | void SnapshotByteSource::CopyRaw(byte* to, int number_of_bytes) { |
| 325 | memcpy(to, data_ + position_, number_of_bytes); |
| 326 | position_ += number_of_bytes; |
| 327 | } |
| 328 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 329 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 330 | // A Deserializer reads a snapshot and reconstructs the Object graph it defines. |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 331 | class Deserializer: public SerializerDeserializer { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | public: |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 333 | // Create a deserializer from a snapshot byte source. |
| 334 | explicit Deserializer(SnapshotByteSource* source); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 336 | virtual ~Deserializer(); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 337 | |
| 338 | // Deserialize the snapshot into an empty heap. |
| 339 | void Deserialize(); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 340 | |
| 341 | // Deserialize a single object and the objects reachable from it. |
| 342 | void DeserializePartial(Object** root); |
| 343 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 344 | private: |
| 345 | virtual void VisitPointers(Object** start, Object** end); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 346 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 347 | virtual void VisitExternalReferences(Address* start, Address* end) { |
| 348 | UNREACHABLE(); |
| 349 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 350 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 351 | virtual void VisitRuntimeEntry(RelocInfo* rinfo) { |
| 352 | UNREACHABLE(); |
| 353 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 354 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 355 | // Fills in some heap data in an area from start to end (non-inclusive). The |
| 356 | // space id is used for the write barrier. The object_address is the address |
| 357 | // of the object we are writing into, or NULL if we are not writing into an |
| 358 | // object, i.e. if we are writing a series of tagged values that are not on |
| 359 | // the heap. |
| 360 | void ReadChunk( |
| 361 | Object** start, Object** end, int space, Address object_address); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 362 | HeapObject* GetAddressFromStart(int space); |
| 363 | inline HeapObject* GetAddressFromEnd(int space); |
| 364 | Address Allocate(int space_number, Space* space, int size); |
| 365 | void ReadObject(int space_number, Space* space, Object** write_back); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 366 | |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 367 | // Cached current isolate. |
| 368 | Isolate* isolate_; |
| 369 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 370 | // Keep track of the pages in the paged spaces. |
| 371 | // (In large object space we are keeping track of individual objects |
| 372 | // rather than pages.) In new space we just need the address of the |
| 373 | // first object and the others will flow from that. |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 374 | List<Address> pages_[SerializerDeserializer::kNumberOfSpaces]; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 375 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 376 | SnapshotByteSource* source_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 377 | // This is the address of the next object that will be allocated in each |
| 378 | // space. It is used to calculate the addresses of back-references. |
| 379 | Address high_water_[LAST_SPACE + 1]; |
| 380 | // This is the address of the most recent object that was allocated. It |
| 381 | // is used to set the location of the new page when we encounter a |
| 382 | // START_NEW_PAGE_SERIALIZATION tag. |
| 383 | Address last_object_address_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 384 | |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 385 | ExternalReferenceDecoder* external_reference_decoder_; |
| 386 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 387 | DISALLOW_COPY_AND_ASSIGN(Deserializer); |
| 388 | }; |
| 389 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 390 | |
| 391 | class SnapshotByteSink { |
| 392 | public: |
| 393 | virtual ~SnapshotByteSink() { } |
| 394 | virtual void Put(int byte, const char* description) = 0; |
| 395 | virtual void PutSection(int byte, const char* description) { |
| 396 | Put(byte, description); |
| 397 | } |
| 398 | void PutInt(uintptr_t integer, const char* description); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 399 | virtual int Position() = 0; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 403 | // Mapping objects to their location after deserialization. |
| 404 | // This is used during building, but not at runtime by V8. |
| 405 | class SerializationAddressMapper { |
| 406 | public: |
| 407 | SerializationAddressMapper() |
| 408 | : serialization_map_(new HashMap(&SerializationMatchFun)), |
| 409 | no_allocation_(new AssertNoAllocation()) { } |
| 410 | |
| 411 | ~SerializationAddressMapper() { |
| 412 | delete serialization_map_; |
| 413 | delete no_allocation_; |
| 414 | } |
| 415 | |
| 416 | bool IsMapped(HeapObject* obj) { |
| 417 | return serialization_map_->Lookup(Key(obj), Hash(obj), false) != NULL; |
| 418 | } |
| 419 | |
| 420 | int MappedTo(HeapObject* obj) { |
| 421 | ASSERT(IsMapped(obj)); |
| 422 | return static_cast<int>(reinterpret_cast<intptr_t>( |
| 423 | serialization_map_->Lookup(Key(obj), Hash(obj), false)->value)); |
| 424 | } |
| 425 | |
| 426 | void AddMapping(HeapObject* obj, int to) { |
| 427 | ASSERT(!IsMapped(obj)); |
| 428 | HashMap::Entry* entry = |
| 429 | serialization_map_->Lookup(Key(obj), Hash(obj), true); |
| 430 | entry->value = Value(to); |
| 431 | } |
| 432 | |
| 433 | private: |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 434 | static bool SerializationMatchFun(void* key1, void* key2) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 435 | return key1 == key2; |
| 436 | } |
| 437 | |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 438 | static uint32_t Hash(HeapObject* obj) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 439 | return static_cast<int32_t>(reinterpret_cast<intptr_t>(obj->address())); |
| 440 | } |
| 441 | |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 442 | static void* Key(HeapObject* obj) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 443 | return reinterpret_cast<void*>(obj->address()); |
| 444 | } |
| 445 | |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 446 | static void* Value(int v) { |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 447 | return reinterpret_cast<void*>(v); |
| 448 | } |
| 449 | |
| 450 | HashMap* serialization_map_; |
| 451 | AssertNoAllocation* no_allocation_; |
| 452 | DISALLOW_COPY_AND_ASSIGN(SerializationAddressMapper); |
| 453 | }; |
| 454 | |
| 455 | |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 456 | // There can be only one serializer per V8 process. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 457 | class Serializer : public SerializerDeserializer { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 458 | public: |
| 459 | explicit Serializer(SnapshotByteSink* sink); |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 460 | ~Serializer(); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 461 | void VisitPointers(Object** start, Object** end); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 462 | // You can call this after serialization to find out how much space was used |
| 463 | // in each space. |
| 464 | int CurrentAllocationAddress(int space) { |
| 465 | if (SpaceIsLarge(space)) return large_object_total_; |
| 466 | return fullness_[space]; |
| 467 | } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 468 | |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 469 | static void Enable() { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 470 | if (!serialization_enabled_) { |
| 471 | ASSERT(!too_late_to_enable_now_); |
| 472 | } |
| 473 | serialization_enabled_ = true; |
| 474 | } |
| 475 | |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 476 | static void Disable() { serialization_enabled_ = false; } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 477 | // Call this when you have made use of the fact that there is no serialization |
| 478 | // going on. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 479 | static void TooLateToEnableNow() { too_late_to_enable_now_ = true; } |
| 480 | static bool enabled() { return serialization_enabled_; } |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 481 | SerializationAddressMapper* address_mapper() { return &address_mapper_; } |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 482 | void PutRoot( |
| 483 | int index, HeapObject* object, HowToCode how, WhereToPoint where); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 484 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 485 | protected: |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 486 | static const int kInvalidRootIndex = -1; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 487 | |
| 488 | int RootIndex(HeapObject* heap_object, HowToCode from); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 489 | virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) = 0; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 490 | intptr_t root_index_wave_front() { return root_index_wave_front_; } |
| 491 | void set_root_index_wave_front(intptr_t value) { |
| 492 | ASSERT(value >= root_index_wave_front_); |
| 493 | root_index_wave_front_ = value; |
| 494 | } |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 495 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 496 | class ObjectSerializer : public ObjectVisitor { |
| 497 | public: |
| 498 | ObjectSerializer(Serializer* serializer, |
| 499 | Object* o, |
| 500 | SnapshotByteSink* sink, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 501 | HowToCode how_to_code, |
| 502 | WhereToPoint where_to_point) |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 503 | : serializer_(serializer), |
| 504 | object_(HeapObject::cast(o)), |
| 505 | sink_(sink), |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 506 | reference_representation_(how_to_code + where_to_point), |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 507 | bytes_processed_so_far_(0) { } |
| 508 | void Serialize(); |
| 509 | void VisitPointers(Object** start, Object** end); |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 510 | void VisitEmbeddedPointer(RelocInfo* target); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 511 | void VisitExternalReferences(Address* start, Address* end); |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 512 | void VisitExternalReference(RelocInfo* rinfo); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 513 | void VisitCodeTarget(RelocInfo* target); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 514 | void VisitCodeEntry(Address entry_address); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 515 | void VisitGlobalPropertyCell(RelocInfo* rinfo); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 516 | void VisitRuntimeEntry(RelocInfo* reloc); |
| 517 | // Used for seralizing the external strings that hold the natives source. |
| 518 | void VisitExternalAsciiString( |
| 519 | v8::String::ExternalAsciiStringResource** resource); |
| 520 | // We can't serialize a heap with external two byte strings. |
| 521 | void VisitExternalTwoByteString( |
| 522 | v8::String::ExternalStringResource** resource) { |
| 523 | UNREACHABLE(); |
| 524 | } |
| 525 | |
| 526 | private: |
| 527 | void OutputRawData(Address up_to); |
| 528 | |
| 529 | Serializer* serializer_; |
| 530 | HeapObject* object_; |
| 531 | SnapshotByteSink* sink_; |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 532 | int reference_representation_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 533 | int bytes_processed_so_far_; |
| 534 | }; |
| 535 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 536 | virtual void SerializeObject(Object* o, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 537 | HowToCode how_to_code, |
| 538 | WhereToPoint where_to_point) = 0; |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 539 | void SerializeReferenceToPreviousObject( |
| 540 | int space, |
| 541 | int address, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 542 | HowToCode how_to_code, |
| 543 | WhereToPoint where_to_point); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 544 | void InitializeAllocators(); |
| 545 | // This will return the space for an object. If the object is in large |
| 546 | // object space it may return kLargeCode or kLargeFixedArray in order |
| 547 | // to indicate to the deserializer what kind of large object allocation |
| 548 | // to make. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 549 | static int SpaceOfObject(HeapObject* object); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 550 | // This just returns the space of the object. It will return LO_SPACE |
| 551 | // for all large objects since you can't check the type of the object |
| 552 | // once the map has been used for the serialization address. |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 553 | static int SpaceOfAlreadySerializedObject(HeapObject* object); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 554 | int Allocate(int space, int size, bool* new_page_started); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 555 | int EncodeExternalReference(Address addr) { |
| 556 | return external_reference_encoder_->Encode(addr); |
| 557 | } |
| 558 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 559 | int SpaceAreaSize(int space); |
| 560 | |
| 561 | Isolate* isolate_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 562 | // Keep track of the fullness of each space in order to generate |
| 563 | // relative addresses for back references. Large objects are |
| 564 | // just numbered sequentially since relative addresses make no |
| 565 | // sense in large object space. |
| 566 | int fullness_[LAST_SPACE + 1]; |
| 567 | SnapshotByteSink* sink_; |
| 568 | int current_root_index_; |
| 569 | ExternalReferenceEncoder* external_reference_encoder_; |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 570 | static bool serialization_enabled_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 571 | // Did we already make use of the fact that serialization was not enabled? |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 572 | static bool too_late_to_enable_now_; |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 573 | int large_object_total_; |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 574 | SerializationAddressMapper address_mapper_; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 575 | intptr_t root_index_wave_front_; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 576 | |
| 577 | friend class ObjectSerializer; |
| 578 | friend class Deserializer; |
| 579 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 580 | private: |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 581 | DISALLOW_COPY_AND_ASSIGN(Serializer); |
| 582 | }; |
| 583 | |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 584 | |
| 585 | class PartialSerializer : public Serializer { |
| 586 | public: |
| 587 | PartialSerializer(Serializer* startup_snapshot_serializer, |
| 588 | SnapshotByteSink* sink) |
| 589 | : Serializer(sink), |
| 590 | startup_serializer_(startup_snapshot_serializer) { |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 591 | set_root_index_wave_front(Heap::kStrongRootListLength); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // Serialize the objects reachable from a single object pointer. |
| 595 | virtual void Serialize(Object** o); |
| 596 | virtual void SerializeObject(Object* o, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 597 | HowToCode how_to_code, |
| 598 | WhereToPoint where_to_point); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 599 | |
| 600 | protected: |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 601 | virtual int PartialSnapshotCacheIndex(HeapObject* o); |
| 602 | virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) { |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 603 | // Scripts should be referred only through shared function infos. We can't |
| 604 | // allow them to be part of the partial snapshot because they contain a |
| 605 | // unique ID, and deserializing several partial snapshots containing script |
| 606 | // would cause dupes. |
| 607 | ASSERT(!o->IsScript()); |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 608 | return o->IsString() || o->IsSharedFunctionInfo() || |
| Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 609 | o->IsHeapNumber() || o->IsCode() || |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 610 | o->IsScopeInfo() || |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 611 | o->map() == HEAP->fixed_cow_array_map(); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | private: |
| 615 | Serializer* startup_serializer_; |
| 616 | DISALLOW_COPY_AND_ASSIGN(PartialSerializer); |
| 617 | }; |
| 618 | |
| 619 | |
| 620 | class StartupSerializer : public Serializer { |
| 621 | public: |
| 622 | explicit StartupSerializer(SnapshotByteSink* sink) : Serializer(sink) { |
| 623 | // Clear the cache of objects used by the partial snapshot. After the |
| 624 | // strong roots have been serialized we can create a partial snapshot |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 625 | // which will repopulate the cache with objects needed by that partial |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 626 | // snapshot. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 627 | Isolate::Current()->set_serialize_partial_snapshot_cache_length(0); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 628 | } |
| 629 | // Serialize the current state of the heap. The order is: |
| 630 | // 1) Strong references. |
| 631 | // 2) Partial snapshot cache. |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame^] | 632 | // 3) Weak references (e.g. the symbol table). |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 633 | virtual void SerializeStrongReferences(); |
| 634 | virtual void SerializeObject(Object* o, |
| Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 635 | HowToCode how_to_code, |
| 636 | WhereToPoint where_to_point); |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 637 | void SerializeWeakReferences(); |
| 638 | void Serialize() { |
| 639 | SerializeStrongReferences(); |
| 640 | SerializeWeakReferences(); |
| 641 | } |
| 642 | |
| 643 | private: |
| Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 644 | virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) { |
| 645 | return false; |
| 646 | } |
| 647 | }; |
| 648 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 649 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 650 | } } // namespace v8::internal |
| 651 | |
| 652 | #endif // V8_SERIALIZE_H_ |