OLD | NEW |
(Empty) | |
| 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 #ifndef V8_SNAPSHOT_SERIALIZER_COMMON_H_ |
| 6 #define V8_SNAPSHOT_SERIALIZER_COMMON_H_ |
| 7 |
| 8 #include "src/address-map.h" |
| 9 #include "src/globals.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 |
| 14 class Isolate; |
| 15 |
| 16 // ExternalReferenceTable is a helper class that defines the relationship |
| 17 // between external references and their encodings. It is used to build |
| 18 // hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder. |
| 19 class ExternalReferenceTable { |
| 20 public: |
| 21 static ExternalReferenceTable* instance(Isolate* isolate); |
| 22 |
| 23 int size() const { return refs_.length(); } |
| 24 Address address(int i) { return refs_[i].address; } |
| 25 const char* name(int i) { return refs_[i].name; } |
| 26 |
| 27 inline static Address NotAvailable() { return NULL; } |
| 28 |
| 29 static const int kDeoptTableSerializeEntryCount = 64; |
| 30 |
| 31 private: |
| 32 struct ExternalReferenceEntry { |
| 33 Address address; |
| 34 const char* name; |
| 35 }; |
| 36 |
| 37 explicit ExternalReferenceTable(Isolate* isolate); |
| 38 |
| 39 void Add(Address address, const char* name) { |
| 40 ExternalReferenceEntry entry = {address, name}; |
| 41 refs_.Add(entry); |
| 42 } |
| 43 |
| 44 List<ExternalReferenceEntry> refs_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(ExternalReferenceTable); |
| 47 }; |
| 48 |
| 49 class ExternalReferenceEncoder { |
| 50 public: |
| 51 explicit ExternalReferenceEncoder(Isolate* isolate); |
| 52 |
| 53 uint32_t Encode(Address key) const; |
| 54 |
| 55 const char* NameOfAddress(Isolate* isolate, Address address) const; |
| 56 |
| 57 private: |
| 58 static uint32_t Hash(Address key) { |
| 59 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >> |
| 60 kPointerSizeLog2); |
| 61 } |
| 62 |
| 63 HashMap* map_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(ExternalReferenceEncoder); |
| 66 }; |
| 67 |
| 68 class HotObjectsList { |
| 69 public: |
| 70 HotObjectsList() : index_(0) { |
| 71 for (int i = 0; i < kSize; i++) circular_queue_[i] = NULL; |
| 72 } |
| 73 |
| 74 void Add(HeapObject* object) { |
| 75 circular_queue_[index_] = object; |
| 76 index_ = (index_ + 1) & kSizeMask; |
| 77 } |
| 78 |
| 79 HeapObject* Get(int index) { |
| 80 DCHECK_NOT_NULL(circular_queue_[index]); |
| 81 return circular_queue_[index]; |
| 82 } |
| 83 |
| 84 static const int kNotFound = -1; |
| 85 |
| 86 int Find(HeapObject* object) { |
| 87 for (int i = 0; i < kSize; i++) { |
| 88 if (circular_queue_[i] == object) return i; |
| 89 } |
| 90 return kNotFound; |
| 91 } |
| 92 |
| 93 static const int kSize = 8; |
| 94 |
| 95 private: |
| 96 STATIC_ASSERT(IS_POWER_OF_TWO(kSize)); |
| 97 static const int kSizeMask = kSize - 1; |
| 98 HeapObject* circular_queue_[kSize]; |
| 99 int index_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(HotObjectsList); |
| 102 }; |
| 103 |
| 104 // The Serializer/Deserializer class is a common superclass for Serializer and |
| 105 // Deserializer which is used to store common constants and methods used by |
| 106 // both. |
| 107 class SerializerDeserializer : public ObjectVisitor { |
| 108 public: |
| 109 static void Iterate(Isolate* isolate, ObjectVisitor* visitor); |
| 110 |
| 111 // No reservation for large object space necessary. |
| 112 static const int kNumberOfPreallocatedSpaces = LAST_PAGED_SPACE + 1; |
| 113 static const int kNumberOfSpaces = LAST_SPACE + 1; |
| 114 |
| 115 protected: |
| 116 static bool CanBeDeferred(HeapObject* o); |
| 117 |
| 118 // ---------- byte code range 0x00..0x7f ---------- |
| 119 // Byte codes in this range represent Where, HowToCode and WhereToPoint. |
| 120 // Where the pointed-to object can be found: |
| 121 // The static assert below will trigger when the number of preallocated spaces |
| 122 // changed. If that happens, update the bytecode ranges in the comments below. |
| 123 STATIC_ASSERT(5 == kNumberOfSpaces); |
| 124 enum Where { |
| 125 // 0x00..0x04 Allocate new object, in specified space. |
| 126 kNewObject = 0, |
| 127 // 0x05 Unused (including 0x25, 0x45, 0x65). |
| 128 // 0x06 Unused (including 0x26, 0x46, 0x66). |
| 129 // 0x07 Unused (including 0x27, 0x47, 0x67). |
| 130 // 0x08..0x0c Reference to previous object from space. |
| 131 kBackref = 0x08, |
| 132 // 0x0d Unused (including 0x2d, 0x4d, 0x6d). |
| 133 // 0x0e Unused (including 0x2e, 0x4e, 0x6e). |
| 134 // 0x0f Unused (including 0x2f, 0x4f, 0x6f). |
| 135 // 0x10..0x14 Reference to previous object from space after skip. |
| 136 kBackrefWithSkip = 0x10, |
| 137 // 0x15 Unused (including 0x35, 0x55, 0x75). |
| 138 // 0x16 Unused (including 0x36, 0x56, 0x76). |
| 139 // 0x17 Misc (including 0x37, 0x57, 0x77). |
| 140 // 0x18 Root array item. |
| 141 kRootArray = 0x18, |
| 142 // 0x19 Object in the partial snapshot cache. |
| 143 kPartialSnapshotCache = 0x19, |
| 144 // 0x1a External reference referenced by id. |
| 145 kExternalReference = 0x1a, |
| 146 // 0x1b Object provided in the attached list. |
| 147 kAttachedReference = 0x1b, |
| 148 // 0x1c Builtin code referenced by index. |
| 149 kBuiltin = 0x1c |
| 150 // 0x1d..0x1f Misc (including 0x3d..0x3f, 0x5d..0x5f, 0x7d..0x7f) |
| 151 }; |
| 152 |
| 153 static const int kWhereMask = 0x1f; |
| 154 static const int kSpaceMask = 7; |
| 155 STATIC_ASSERT(kNumberOfSpaces <= kSpaceMask + 1); |
| 156 |
| 157 // How to code the pointer to the object. |
| 158 enum HowToCode { |
| 159 // Straight pointer. |
| 160 kPlain = 0, |
| 161 // A pointer inlined in code. What this means depends on the architecture. |
| 162 kFromCode = 0x20 |
| 163 }; |
| 164 |
| 165 static const int kHowToCodeMask = 0x20; |
| 166 |
| 167 // Where to point within the object. |
| 168 enum WhereToPoint { |
| 169 // Points to start of object |
| 170 kStartOfObject = 0, |
| 171 // Points to instruction in code object or payload of cell. |
| 172 kInnerPointer = 0x40 |
| 173 }; |
| 174 |
| 175 static const int kWhereToPointMask = 0x40; |
| 176 |
| 177 // ---------- Misc ---------- |
| 178 // Skip. |
| 179 static const int kSkip = 0x1d; |
| 180 // Internal reference encoded as offsets of pc and target from code entry. |
| 181 static const int kInternalReference = 0x1e; |
| 182 static const int kInternalReferenceEncoded = 0x1f; |
| 183 // Do nothing, used for padding. |
| 184 static const int kNop = 0x3d; |
| 185 // Move to next reserved chunk. |
| 186 static const int kNextChunk = 0x3e; |
| 187 // Deferring object content. |
| 188 static const int kDeferred = 0x3f; |
| 189 // Used for the source code of the natives, which is in the executable, but |
| 190 // is referred to from external strings in the snapshot. |
| 191 static const int kNativesStringResource = 0x5d; |
| 192 // Used for the source code for compiled stubs, which is in the executable, |
| 193 // but is referred to from external strings in the snapshot. |
| 194 static const int kExtraNativesStringResource = 0x5e; |
| 195 // A tag emitted at strategic points in the snapshot to delineate sections. |
| 196 // If the deserializer does not find these at the expected moments then it |
| 197 // is an indication that the snapshot and the VM do not fit together. |
| 198 // Examine the build process for architecture, version or configuration |
| 199 // mismatches. |
| 200 static const int kSynchronize = 0x17; |
| 201 // Repeats of variable length. |
| 202 static const int kVariableRepeat = 0x37; |
| 203 // Raw data of variable length. |
| 204 static const int kVariableRawData = 0x57; |
| 205 // Alignment prefixes 0x7d..0x7f |
| 206 static const int kAlignmentPrefix = 0x7d; |
| 207 |
| 208 // 0x77 unused |
| 209 |
| 210 // ---------- byte code range 0x80..0xff ---------- |
| 211 // First 32 root array items. |
| 212 static const int kNumberOfRootArrayConstants = 0x20; |
| 213 // 0x80..0x9f |
| 214 static const int kRootArrayConstants = 0x80; |
| 215 // 0xa0..0xbf |
| 216 static const int kRootArrayConstantsWithSkip = 0xa0; |
| 217 static const int kRootArrayConstantsMask = 0x1f; |
| 218 |
| 219 // 8 hot (recently seen or back-referenced) objects with optional skip. |
| 220 static const int kNumberOfHotObjects = 0x08; |
| 221 // 0xc0..0xc7 |
| 222 static const int kHotObject = 0xc0; |
| 223 // 0xc8..0xcf |
| 224 static const int kHotObjectWithSkip = 0xc8; |
| 225 static const int kHotObjectMask = 0x07; |
| 226 |
| 227 // 32 common raw data lengths. |
| 228 static const int kNumberOfFixedRawData = 0x20; |
| 229 // 0xd0..0xef |
| 230 static const int kFixedRawData = 0xd0; |
| 231 static const int kOnePointerRawData = kFixedRawData; |
| 232 static const int kFixedRawDataStart = kFixedRawData - 1; |
| 233 |
| 234 // 16 repeats lengths. |
| 235 static const int kNumberOfFixedRepeat = 0x10; |
| 236 // 0xf0..0xff |
| 237 static const int kFixedRepeat = 0xf0; |
| 238 static const int kFixedRepeatStart = kFixedRepeat - 1; |
| 239 |
| 240 // ---------- special values ---------- |
| 241 static const int kAnyOldSpace = -1; |
| 242 |
| 243 // Sentinel after a new object to indicate that double alignment is needed. |
| 244 static const int kDoubleAlignmentSentinel = 0; |
| 245 |
| 246 // Used as index for the attached reference representing the source object. |
| 247 static const int kSourceObjectReference = 0; |
| 248 |
| 249 // Used as index for the attached reference representing the global proxy. |
| 250 static const int kGlobalProxyReference = 0; |
| 251 |
| 252 // ---------- member variable ---------- |
| 253 HotObjectsList hot_objects_; |
| 254 }; |
| 255 |
| 256 class SerializedData { |
| 257 public: |
| 258 class Reservation { |
| 259 public: |
| 260 explicit Reservation(uint32_t size) |
| 261 : reservation_(ChunkSizeBits::encode(size)) {} |
| 262 |
| 263 uint32_t chunk_size() const { return ChunkSizeBits::decode(reservation_); } |
| 264 bool is_last() const { return IsLastChunkBits::decode(reservation_); } |
| 265 |
| 266 void mark_as_last() { reservation_ |= IsLastChunkBits::encode(true); } |
| 267 |
| 268 private: |
| 269 uint32_t reservation_; |
| 270 }; |
| 271 |
| 272 SerializedData(byte* data, int size) |
| 273 : data_(data), size_(size), owns_data_(false) {} |
| 274 SerializedData() : data_(NULL), size_(0), owns_data_(false) {} |
| 275 |
| 276 ~SerializedData() { |
| 277 if (owns_data_) DeleteArray<byte>(data_); |
| 278 } |
| 279 |
| 280 uint32_t GetMagicNumber() const { return GetHeaderValue(kMagicNumberOffset); } |
| 281 |
| 282 class ChunkSizeBits : public BitField<uint32_t, 0, 31> {}; |
| 283 class IsLastChunkBits : public BitField<bool, 31, 1> {}; |
| 284 |
| 285 static uint32_t ComputeMagicNumber(ExternalReferenceTable* table) { |
| 286 uint32_t external_refs = table->size(); |
| 287 return 0xC0DE0000 ^ external_refs; |
| 288 } |
| 289 |
| 290 protected: |
| 291 void SetHeaderValue(int offset, uint32_t value) { |
| 292 uint32_t* address = reinterpret_cast<uint32_t*>(data_ + offset); |
| 293 memcpy(reinterpret_cast<uint32_t*>(address), &value, sizeof(value)); |
| 294 } |
| 295 |
| 296 uint32_t GetHeaderValue(int offset) const { |
| 297 uint32_t value; |
| 298 memcpy(&value, reinterpret_cast<int*>(data_ + offset), sizeof(value)); |
| 299 return value; |
| 300 } |
| 301 |
| 302 void AllocateData(int size); |
| 303 |
| 304 static uint32_t ComputeMagicNumber(Isolate* isolate) { |
| 305 return ComputeMagicNumber(ExternalReferenceTable::instance(isolate)); |
| 306 } |
| 307 |
| 308 void SetMagicNumber(Isolate* isolate) { |
| 309 SetHeaderValue(kMagicNumberOffset, ComputeMagicNumber(isolate)); |
| 310 } |
| 311 |
| 312 static const int kMagicNumberOffset = 0; |
| 313 |
| 314 byte* data_; |
| 315 int size_; |
| 316 bool owns_data_; |
| 317 }; |
| 318 |
| 319 } // namespace internal |
| 320 } // namespace v8 |
| 321 |
| 322 #endif // V8_SNAPSHOT_SERIALIZER_COMMON_H_ |
OLD | NEW |