blob: b217fff52b84f109a977317331373f0cc8d35ed3 [file] [log] [blame]
Ben Murdochda12d292016-06-02 14:46:10 +01001// 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_CODE_SERIALIZER_H_
6#define V8_SNAPSHOT_CODE_SERIALIZER_H_
7
8#include "src/parsing/preparse-data.h"
9#include "src/snapshot/serializer.h"
10
11namespace v8 {
12namespace internal {
13
14class CodeSerializer : public Serializer {
15 public:
16 static ScriptData* Serialize(Isolate* isolate,
17 Handle<SharedFunctionInfo> info,
18 Handle<String> source);
19
20 MUST_USE_RESULT static MaybeHandle<SharedFunctionInfo> Deserialize(
21 Isolate* isolate, ScriptData* cached_data, Handle<String> source);
22
23 static const int kSourceObjectIndex = 0;
24 STATIC_ASSERT(kSourceObjectReference == kSourceObjectIndex);
25
26 static const int kCodeStubsBaseIndex = 1;
27
28 String* source() const {
29 DCHECK(!AllowHeapAllocation::IsAllowed());
30 return source_;
31 }
32
33 const List<uint32_t>* stub_keys() const { return &stub_keys_; }
34
35 private:
36 CodeSerializer(Isolate* isolate, SnapshotByteSink* sink, String* source)
37 : Serializer(isolate, sink), source_(source) {
38 back_reference_map_.AddSourceString(source);
39 }
40
41 ~CodeSerializer() override { OutputStatistics("CodeSerializer"); }
42
43 void SerializeObject(HeapObject* o, HowToCode how_to_code,
44 WhereToPoint where_to_point, int skip) override;
45
46 void SerializeBuiltin(int builtin_index, HowToCode how_to_code,
47 WhereToPoint where_to_point);
48 void SerializeIC(Code* ic, HowToCode how_to_code,
49 WhereToPoint where_to_point);
50 void SerializeCodeStub(uint32_t stub_key, HowToCode how_to_code,
51 WhereToPoint where_to_point);
52 void SerializeGeneric(HeapObject* heap_object, HowToCode how_to_code,
53 WhereToPoint where_to_point);
54 int AddCodeStubKey(uint32_t stub_key);
55
56 DisallowHeapAllocation no_gc_;
57 String* source_;
58 List<uint32_t> stub_keys_;
59 DISALLOW_COPY_AND_ASSIGN(CodeSerializer);
60};
61
62// Wrapper around ScriptData to provide code-serializer-specific functionality.
63class SerializedCodeData : public SerializedData {
64 public:
65 // Used when consuming.
66 static SerializedCodeData* FromCachedData(Isolate* isolate,
67 ScriptData* cached_data,
68 String* source);
69
70 // Used when producing.
71 SerializedCodeData(const List<byte>& payload, const CodeSerializer& cs);
72
73 // Return ScriptData object and relinquish ownership over it to the caller.
74 ScriptData* GetScriptData();
75
76 Vector<const Reservation> Reservations() const;
77 Vector<const byte> Payload() const;
78
79 Vector<const uint32_t> CodeStubKeys() const;
80
81 private:
82 explicit SerializedCodeData(ScriptData* data);
83
84 enum SanityCheckResult {
85 CHECK_SUCCESS = 0,
86 MAGIC_NUMBER_MISMATCH = 1,
87 VERSION_MISMATCH = 2,
88 SOURCE_MISMATCH = 3,
89 CPU_FEATURES_MISMATCH = 4,
90 FLAGS_MISMATCH = 5,
91 CHECKSUM_MISMATCH = 6
92 };
93
94 SanityCheckResult SanityCheck(Isolate* isolate, String* source) const;
95
96 uint32_t SourceHash(String* source) const;
97
98 // The data header consists of uint32_t-sized entries:
99 // [0] magic number and external reference count
100 // [1] version hash
101 // [2] source hash
102 // [3] cpu features
103 // [4] flag hash
104 // [5] number of code stub keys
105 // [6] number of reservation size entries
106 // [7] payload length
107 // [8] payload checksum part 1
108 // [9] payload checksum part 2
109 // ... reservations
110 // ... code stub keys
111 // ... serialized payload
112 static const int kVersionHashOffset = kMagicNumberOffset + kInt32Size;
113 static const int kSourceHashOffset = kVersionHashOffset + kInt32Size;
114 static const int kCpuFeaturesOffset = kSourceHashOffset + kInt32Size;
115 static const int kFlagHashOffset = kCpuFeaturesOffset + kInt32Size;
116 static const int kNumReservationsOffset = kFlagHashOffset + kInt32Size;
117 static const int kNumCodeStubKeysOffset = kNumReservationsOffset + kInt32Size;
118 static const int kPayloadLengthOffset = kNumCodeStubKeysOffset + kInt32Size;
119 static const int kChecksum1Offset = kPayloadLengthOffset + kInt32Size;
120 static const int kChecksum2Offset = kChecksum1Offset + kInt32Size;
121 static const int kHeaderSize = kChecksum2Offset + kInt32Size;
122};
123
124} // namespace internal
125} // namespace v8
126
127#endif // V8_SNAPSHOT_CODE_SERIALIZER_H_