OLD | NEW |
1 // Copyright (c) 2016, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dartino project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 #ifndef SRC_VM_SNAPSHOT_H_ | 5 #ifndef SRC_VM_SNAPSHOT_H_ |
6 #define SRC_VM_SNAPSHOT_H_ | 6 #define SRC_VM_SNAPSHOT_H_ |
7 | 7 |
8 #include "src/shared/globals.h" | 8 #include "src/shared/globals.h" |
9 #include "src/shared/list.h" | 9 #include "src/shared/list.h" |
10 #include "src/shared/platform.h" | 10 #include "src/shared/platform.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 103 |
104 // The byte codes used to encode the snapshot. Not to be confused with | 104 // The byte codes used to encode the snapshot. Not to be confused with |
105 // the byte codes that encode the actual Dart program. Each byte code | 105 // the byte codes that encode the actual Dart program. Each byte code |
106 // is followed by 0, 1, 2 or 4 bytes of argument. | 106 // is followed by 0, 1, 2 or 4 bytes of argument. |
107 enum kSnapshotOpcodes { | 107 enum kSnapshotOpcodes { |
108 // 00xxxxxx are the popular objects. These are single-byte instructions. | 108 // 00xxxxxx are the popular objects. These are single-byte instructions. |
109 kSnapshotPopular = 0, | 109 kSnapshotPopular = 0, |
110 kSnapshotPopularMask = 0xc0, | 110 kSnapshotPopularMask = 0xc0, |
111 kSnapshotNumberOfPopularObjects = 0x40, | 111 kSnapshotNumberOfPopularObjects = 0x40, |
112 | 112 |
113 // ooo ww xxx are the other opcodes. Opcodes 0 and 1 are taken by the | 113 // ooo w x are the other opcodes. Opcodes 0 and 1 are taken by the popular |
114 // popular objects. ww gives the number of extra bytes that help make up the | 114 // objects. The number of extra bytes that help make up the value is named |
115 // value, 0, 1, 2 or 4. xxx are is the biased starting point from -1 to 6, | 115 // 'w' and can be between 0 and 4. The encoding of w is unary as follows: |
116 // and each extra byte specified by w is combined with the previous values by | 116 // ooo 0 xxxx /* w == 0 */ |
117 // x = (x << 8) | next_byte; | 117 // ooo 10 xxx /* w == 1 */ |
| 118 // ooo 110 xx /* w == 2 */ |
| 119 // ooo 1110 x /* w == 3 */ |
| 120 // ooo 1111 x /* w == 4 */ |
| 121 // xxx is the biased starting point (-1 based), and each extra byte specified |
| 122 // by w is combined with the previous values by x = (x << 8) | next_byte; |
118 // There are several places where ReadWord() relies on the order and bit | 123 // There are several places where ReadWord() relies on the order and bit |
119 // patterns of these opcodes. | 124 // patterns of these opcodes. |
120 kSnapshotRecentPointer = 2, | 125 kSnapshotRecentPointer = 2, |
121 kSnapshotRecentPointer0 = 2, | 126 kSnapshotRecentPointer0 = 2, |
122 kSnapshotRecentPointer1 = 3, | 127 kSnapshotRecentPointer1 = 3, |
123 kSnapshotRecentSmi = 4, // Relative to previous Smi. | 128 kSnapshotRecentSmi = 4, // Relative to previous Smi. |
124 kSnapshotSmi = 5, | 129 kSnapshotSmi = 5, |
125 kSnapshotExternal = 6, // Pointer into C++ heap. | 130 kSnapshotExternal = 6, // Pointer into C++ heap. |
126 kSnapshotRaw = 7, // Raw number of bytes follow. | 131 kSnapshotRaw = 7, // Raw number of bytes follow. |
127 | 132 |
128 // Section boundaries are marked as 111 00 000, which corresponds to a | 133 // Section boundaries are marked as 111 00 000, which corresponds to a |
129 // a run of raw bytes, length -1, which is otherwise illegal. | 134 // a run of raw bytes, length -1, which is otherwise illegal. |
130 }; | 135 }; |
131 | 136 |
132 static const int kSnapshotBias = -1; | 137 static const int kSnapshotBias = -1; |
133 | 138 |
134 // All addresses in the snapshot are based on these idealized word and floating | 139 // All addresses in the snapshot are based on these idealized word and floating |
135 // point sizes. | 140 // point sizes. |
136 static const word kIdealWordSize = 4; | 141 static const word kIdealWordSize = 4; |
137 static const word kIdealFloatSize = 4; | 142 static const word kIdealFloatSize = 4; |
138 // One class pointer plus one float is a boxed float. | 143 // One class pointer plus one float is a boxed float. |
139 static const word kIdealBoxedFloatSize = kIdealWordSize + kIdealFloatSize; | 144 static const word kIdealBoxedFloatSize = kIdealWordSize + kIdealFloatSize; |
140 | 145 |
141 inline int OpCode(uint8 byte_code) { return byte_code >> 5; } | 146 inline int OpCode(uint8 byte_code) { return byte_code >> 5; } |
142 | 147 |
143 inline bool IsOneBytePopularByteCode(uint8 byte_code) { | 148 inline bool IsOneBytePopularByteCode(uint8 byte_code) { |
144 return (byte_code & kSnapshotPopularMask) == kSnapshotPopular; | 149 return (byte_code & kSnapshotPopularMask) == kSnapshotPopular; |
145 } | 150 } |
146 | 151 |
| 152 static const uint8 kNumberOfArgumentBytes[32] = { |
| 153 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 154 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4}; |
| 155 |
147 inline int ArgumentBytes(uint8 byte_code) { | 156 inline int ArgumentBytes(uint8 byte_code) { |
148 ASSERT(!IsOneBytePopularByteCode(byte_code)); | 157 ASSERT(!IsOneBytePopularByteCode(byte_code)); |
149 int w = (byte_code >> 3) & 3; | 158 return kNumberOfArgumentBytes[byte_code & 0x1f]; |
150 // This is a branchless way to say if (w == 3) w = 4; | 159 } |
151 w += w & (w >> 1); | 160 |
152 return w; | 161 inline word ArgumentStart(uint8 byte_code, int w) { |
| 162 if (w == 4) return (byte_code & 1) + kSnapshotBias; |
| 163 word x = byte_code & ((1 << (4 - w)) - 1); |
| 164 return x + kSnapshotBias; |
153 } | 165 } |
154 | 166 |
155 inline bool IsSectionBoundary(uint8 b) { | 167 inline bool IsSectionBoundary(uint8 b) { |
156 if (OpCode(b) != kSnapshotRaw) return false; | 168 if (OpCode(b) != kSnapshotRaw) return false; |
157 return (b & 31) == 0; | 169 return (b & 31) == 0; |
158 } | 170 } |
159 | 171 |
160 class SnapshotReader { | 172 class SnapshotReader { |
161 public: | 173 public: |
162 explicit SnapshotReader(List<uint8> snapshot) | 174 explicit SnapshotReader(List<uint8> snapshot) |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 } | 330 } |
319 | 331 |
320 void GrowCapacity(int extra); | 332 void GrowCapacity(int extra); |
321 }; | 333 }; |
322 | 334 |
323 uint32_t ComputeSnapshotHash(List<uint8> snapshot); | 335 uint32_t ComputeSnapshotHash(List<uint8> snapshot); |
324 | 336 |
325 } // namespace dartino | 337 } // namespace dartino |
326 | 338 |
327 #endif // SRC_VM_SNAPSHOT_H_ | 339 #endif // SRC_VM_SNAPSHOT_H_ |
OLD | NEW |