Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: runtime/vm/kernel_binary.h

Issue 2972343002: [kernel] Insert kernel bodies into VM heap (Closed)
Patch Set: Rebased Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, the Dart 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 file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #ifndef RUNTIME_VM_KERNEL_BINARY_H_ 5 #ifndef RUNTIME_VM_KERNEL_BINARY_H_
6 #define RUNTIME_VM_KERNEL_BINARY_H_ 6 #define RUNTIME_VM_KERNEL_BINARY_H_
7 7
8 #if !defined(DART_PRECOMPILED_RUNTIME) 8 #if !defined(DART_PRECOMPILED_RUNTIME)
9 9
10 #include <map> 10 #include <map>
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 kSpecializedVariableGet = 128, 130 kSpecializedVariableGet = 128,
131 kSpecializedVariableSet = 136, 131 kSpecializedVariableSet = 136,
132 kSpecialIntLiteral = 144, 132 kSpecialIntLiteral = 144,
133 }; 133 };
134 134
135 static const int SpecializedIntLiteralBias = 3; 135 static const int SpecializedIntLiteralBias = 3;
136 136
137 class Reader { 137 class Reader {
138 public: 138 public:
139 Reader(const uint8_t* buffer, intptr_t size) 139 Reader(const uint8_t* buffer, intptr_t size)
140 : buffer_(buffer), size_(size), offset_(0) {} 140 : raw_buffer_(buffer), typed_data_(NULL), size_(size), offset_(0) {}
141
142 explicit Reader(const TypedData& typed_data)
143 : raw_buffer_(NULL),
144 typed_data_(&typed_data),
145 size_(typed_data.IsNull() ? 0 : typed_data.Length()),
146 offset_(0) {}
141 147
142 uint32_t ReadUInt32() { 148 uint32_t ReadUInt32() {
143 ASSERT(offset_ + 4 <= size_); 149 ASSERT(offset_ + 4 <= size_);
144 150
151 const uint8_t* buffer_ = buffer();
Kevin Millikin (Google) 2017/08/09 12:08:51 The underscore (_) suffix is used for member varia
jensj 2017/08/10 07:37:34 Done.
145 uint32_t value = (buffer_[offset_ + 0] << 24) | 152 uint32_t value = (buffer_[offset_ + 0] << 24) |
146 (buffer_[offset_ + 1] << 16) | 153 (buffer_[offset_ + 1] << 16) |
147 (buffer_[offset_ + 2] << 8) | (buffer_[offset_ + 3] << 0); 154 (buffer_[offset_ + 2] << 8) | (buffer_[offset_ + 3] << 0);
148 offset_ += 4; 155 offset_ += 4;
149 return value; 156 return value;
150 } 157 }
151 158
152 uint32_t ReadUInt() { 159 uint32_t ReadUInt() {
153 ASSERT(offset_ + 1 <= size_); 160 ASSERT(offset_ + 1 <= size_);
161
162 const uint8_t* buffer_ = buffer();
Kevin Millikin (Google) 2017/08/09 12:08:51 Same here.
jensj 2017/08/10 07:37:34 Done.
154 uint8_t byte0 = buffer_[offset_]; 163 uint8_t byte0 = buffer_[offset_];
155 if ((byte0 & 0x80) == 0) { 164 if ((byte0 & 0x80) == 0) {
156 // 0... 165 // 0...
157 offset_++; 166 offset_++;
158 return byte0; 167 return byte0;
159 } else if ((byte0 & 0xc0) == 0x80) { 168 } else if ((byte0 & 0xc0) == 0x80) {
160 // 10... 169 // 10...
161 ASSERT(offset_ + 2 <= size_); 170 ASSERT(offset_ + 2 <= size_);
162 uint32_t value = ((byte0 & ~0x80) << 8) | (buffer_[offset_ + 1]); 171 uint32_t value = ((byte0 & ~0x80) << 8) | (buffer_[offset_ + 1]);
163 offset_ += 2; 172 offset_ += 2;
164 return value; 173 return value;
165 } else { 174 } else {
166 // 11... 175 // 11...
167 ASSERT(offset_ + 4 <= size_); 176 ASSERT(offset_ + 4 <= size_);
168 uint32_t value = ((byte0 & ~0xc0) << 24) | (buffer_[offset_ + 1] << 16) | 177 uint32_t value = ((byte0 & ~0xc0) << 24) | (buffer_[offset_ + 1] << 16) |
169 (buffer_[offset_ + 2] << 8) | 178 (buffer_[offset_ + 2] << 8) |
170 (buffer_[offset_ + 3] << 0); 179 (buffer_[offset_ + 3] << 0);
171 offset_ += 4; 180 offset_ += 4;
172 return value; 181 return value;
173 } 182 }
174 } 183 }
175 184
176 /** 185 /**
177 * Read and return a TokenPosition from this reader. 186 * Read and return a TokenPosition from this reader.
178 * @param record specifies whether or not the read position is saved as a
179 * valid token position in the current script.
180 * If not be sure to record it later by calling record_token_position (after
181 * setting the correct current_script_id).
182 */ 187 */
183 TokenPosition ReadPosition() { 188 TokenPosition ReadPosition() {
184 // Position is saved as unsigned, 189 // Position is saved as unsigned,
185 // but actually ranges from -1 and up (thus the -1) 190 // but actually ranges from -1 and up (thus the -1)
186 intptr_t value = ReadUInt() - 1; 191 intptr_t value = ReadUInt() - 1;
187 TokenPosition result = TokenPosition(value); 192 TokenPosition result = TokenPosition(value);
188 max_position_ = Utils::Maximum(max_position_, result); 193 max_position_ = Utils::Maximum(max_position_, result);
189 if (min_position_.IsNoSource()) { 194 if (min_position_.IsNoSource()) {
190 min_position_ = result; 195 min_position_ = result;
191 } else if (result.IsReal()) { 196 } else if (result.IsReal()) {
192 min_position_ = Utils::Minimum(min_position_, result); 197 min_position_ = Utils::Minimum(min_position_, result);
193 } 198 }
194 199
195 return result; 200 return result;
196 } 201 }
197 202
198 intptr_t ReadListLength() { return ReadUInt(); } 203 intptr_t ReadListLength() { return ReadUInt(); }
199 204
200 uint8_t ReadByte() { return buffer_[offset_++]; } 205 uint8_t ReadByte() { return buffer()[offset_++]; }
201 206
202 uint8_t PeekByte() { return buffer_[offset_]; } 207 uint8_t PeekByte() { return buffer()[offset_]; }
203 208
204 bool ReadBool() { return (ReadByte() & 1) == 1; } 209 bool ReadBool() { return (ReadByte() & 1) == 1; }
205 210
206 word ReadFlags() { return ReadByte(); } 211 word ReadFlags() { return ReadByte(); }
207 212
208 Tag ReadTag(uint8_t* payload = NULL) { 213 Tag ReadTag(uint8_t* payload = NULL) {
209 uint8_t byte = ReadByte(); 214 uint8_t byte = ReadByte();
210 bool has_payload = (byte & kSpecializedTagHighBit) != 0; 215 bool has_payload = (byte & kSpecializedTagHighBit) != 0;
211 if (has_payload) { 216 if (has_payload) {
212 if (payload != NULL) { 217 if (payload != NULL) {
(...skipping 11 matching lines...) Expand all
224 if (has_payload) { 229 if (has_payload) {
225 if (payload != NULL) { 230 if (payload != NULL) {
226 *payload = byte & kSpecializedPayloadMask; 231 *payload = byte & kSpecializedPayloadMask;
227 } 232 }
228 return static_cast<Tag>(byte & kSpecializedTagMask); 233 return static_cast<Tag>(byte & kSpecializedTagMask);
229 } else { 234 } else {
230 return static_cast<Tag>(byte); 235 return static_cast<Tag>(byte);
231 } 236 }
232 } 237 }
233 238
234 const uint8_t* Consume(int count) {
235 ASSERT(offset_ + count <= size_);
236 const uint8_t* old = buffer_ + offset_;
237 offset_ += count;
238 return old;
239 }
240
241 void EnsureEnd() { 239 void EnsureEnd() {
242 if (offset_ != size_) { 240 if (offset_ != size_) {
243 FATAL2( 241 FATAL2(
244 "Reading Kernel file: Expected to be at EOF " 242 "Reading Kernel file: Expected to be at EOF "
245 "(offset: %" Pd ", size: %" Pd ")", 243 "(offset: %" Pd ", size: %" Pd ")",
246 offset_, size_); 244 offset_, size_);
247 } 245 }
248 } 246 }
249 247
250 // The largest position read yet (since last reset). 248 // The largest position read yet (since last reset).
251 // This is automatically updated when calling ReadPosition, 249 // This is automatically updated when calling ReadPosition,
252 // but can be overwritten (e.g. via the PositionScope class). 250 // but can be overwritten (e.g. via the PositionScope class).
253 TokenPosition max_position() { return max_position_; } 251 TokenPosition max_position() { return max_position_; }
254 // The smallest position read yet (since last reset). 252 // The smallest position read yet (since last reset).
255 // This is automatically updated when calling ReadPosition, 253 // This is automatically updated when calling ReadPosition,
256 // but can be overwritten (e.g. via the PositionScope class). 254 // but can be overwritten (e.g. via the PositionScope class).
257 TokenPosition min_position() { return min_position_; } 255 TokenPosition min_position() { return min_position_; }
258 256
259 template <typename T, typename RT>
260 T* ReadOptional() {
261 Tag tag = ReadTag();
262 if (tag == kNothing) {
263 return NULL;
264 }
265 ASSERT(tag == kSomething);
266 return RT::ReadFrom(this);
267 }
268
269 template <typename T>
270 T* ReadOptional() {
271 return ReadOptional<T, T>();
272 }
273
274 // A canonical name reference of -1 indicates none (for optional names), not 257 // A canonical name reference of -1 indicates none (for optional names), not
275 // the root name as in the canonical name table. 258 // the root name as in the canonical name table.
276 NameIndex ReadCanonicalNameReference() { return NameIndex(ReadUInt() - 1); } 259 NameIndex ReadCanonicalNameReference() { return NameIndex(ReadUInt() - 1); }
277 260
278 intptr_t offset() { return offset_; } 261 intptr_t offset() { return offset_; }
279 void set_offset(intptr_t offset) { offset_ = offset; } 262 void set_offset(intptr_t offset) { offset_ = offset; }
263
280 intptr_t size() { return size_; } 264 intptr_t size() { return size_; }
265 void set_size(intptr_t size) { size_ = size; }
281 266
282 const uint8_t* buffer() { return buffer_; } 267 const TypedData* typed_data() { return typed_data_; }
268 void set_typed_data(const TypedData* typed_data) { typed_data_ = typed_data; }
269
270 const uint8_t* raw_buffer() { return raw_buffer_; }
271 void set_raw_buffer(const uint8_t* raw_buffer) { raw_buffer_ = raw_buffer; }
272
273 TypedData& CopyDataToVMHeap(Zone* zone,
274 intptr_t from_byte,
275 intptr_t to_byte) {
276 intptr_t size = to_byte - from_byte;
277 TypedData& data = TypedData::Handle(
278 zone, TypedData::New(kTypedDataUint8ArrayCid, size, Heap::kOld));
279 {
280 NoSafepointScope no_safepoint;
281 memmove(data.DataAddr(0), buffer() + from_byte, size);
282 }
283 return data;
284 }
285
286 uint8_t* CopyDataIntoZone(Zone* zone, intptr_t offset, intptr_t length) {
287 uint8_t* buffer_ = zone->Alloc<uint8_t>(length);
288 {
289 NoSafepointScope no_safepoint;
290 memmove(buffer_, buffer() + offset, length);
291 }
292 return buffer_;
293 }
283 294
284 private: 295 private:
285 const uint8_t* buffer_; 296 const uint8_t* buffer() {
297 if (raw_buffer_ != NULL) {
298 return raw_buffer_;
299 }
300 NoSafepointScope no_safepoint;
301 return reinterpret_cast<uint8_t*>(typed_data_->DataAddr(0));
302 }
303
304 const uint8_t* raw_buffer_;
305 const TypedData* typed_data_;
286 intptr_t size_; 306 intptr_t size_;
287 intptr_t offset_; 307 intptr_t offset_;
288 TokenPosition max_position_; 308 TokenPosition max_position_;
289 TokenPosition min_position_; 309 TokenPosition min_position_;
290 intptr_t current_script_id_; 310 intptr_t current_script_id_;
291 311
292 friend class PositionScope; 312 friend class PositionScope;
293 friend class Program; 313 friend class Program;
294 }; 314 };
295 315
(...skipping 24 matching lines...) Expand all
320 Reader* reader_; 340 Reader* reader_;
321 TokenPosition min_; 341 TokenPosition min_;
322 TokenPosition max_; 342 TokenPosition max_;
323 }; 343 };
324 344
325 } // namespace kernel 345 } // namespace kernel
326 } // namespace dart 346 } // namespace dart
327 347
328 #endif // !defined(DART_PRECOMPILED_RUNTIME) 348 #endif // !defined(DART_PRECOMPILED_RUNTIME)
329 #endif // RUNTIME_VM_KERNEL_BINARY_H_ 349 #endif // RUNTIME_VM_KERNEL_BINARY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698