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

Side by Side Diff: runtime/vm/raw_object_snapshot.cc

Issue 9235063: Implementation of message reader for converting a message snapshot into a C structure (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 #include "vm/bigint_operations.h" 5 #include "vm/bigint_operations.h"
6 #include "vm/object.h" 6 #include "vm/object.h"
7 #include "vm/object_store.h" 7 #include "vm/object_store.h"
8 #include "vm/snapshot.h" 8 #include "vm/snapshot.h"
9 #include "vm/visitor.h" 9 #include "vm/visitor.h"
10 10
11 namespace dart { 11 namespace dart {
12 12
13 #define NEW_OBJECT(type) \ 13 #define NEW_OBJECT(type) \
14 ((kind == Snapshot::kFull) ? reader->New##type() : type::New()) 14 ((kind == Snapshot::kFull) ? reader->New##type() : type::New())
15 15
16 #define NEW_OBJECT_WITH_LEN(type, len) \ 16 #define NEW_OBJECT_WITH_LEN(type, len) \
17 ((kind == Snapshot::kFull) ? reader->New##type(len) : type::New(len)) 17 ((kind == Snapshot::kFull) ? reader->New##type(len) : type::New(len))
18 18
19 19
20 static RawSmi* AsSmi(intptr_t value) {
21 ASSERT((value & kSmiTagMask) == 0);
22 return reinterpret_cast<RawSmi*>(value);
23 }
24
25
26 static intptr_t GetSmiValue(intptr_t value) {
27 return Smi::Value(AsSmi(value));
28 }
29
30
31 static uword ZoneAllocator(intptr_t size) { 20 static uword ZoneAllocator(intptr_t size) {
32 Zone* zone = Isolate::Current()->current_zone(); 21 Zone* zone = Isolate::Current()->current_zone();
33 return zone->Allocate(size); 22 return zone->Allocate(size);
34 } 23 }
35 24
36 25
37 RawClass* Class::ReadFrom(SnapshotReader* reader, 26 RawClass* Class::ReadFrom(SnapshotReader* reader,
38 intptr_t object_id, 27 intptr_t object_id,
39 intptr_t tags, 28 intptr_t tags,
40 Snapshot::Kind kind) { 29 Snapshot::Kind kind) {
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 } 339 }
351 340
352 341
353 RawTypeArguments* TypeArguments::ReadFrom(SnapshotReader* reader, 342 RawTypeArguments* TypeArguments::ReadFrom(SnapshotReader* reader,
354 intptr_t object_id, 343 intptr_t object_id,
355 intptr_t tags, 344 intptr_t tags,
356 Snapshot::Kind kind) { 345 Snapshot::Kind kind) {
357 ASSERT(reader != NULL); 346 ASSERT(reader != NULL);
358 347
359 // Read the length so that we can determine instance size to allocate. 348 // Read the length so that we can determine instance size to allocate.
360 intptr_t len = GetSmiValue(reader->ReadIntptrValue()); 349 intptr_t len = reader->ReadSmiValue();
361 350
362 TypeArguments& type_arguments = TypeArguments::ZoneHandle( 351 TypeArguments& type_arguments = TypeArguments::ZoneHandle(
363 reader->isolate(), NEW_OBJECT_WITH_LEN(TypeArguments, len)); 352 reader->isolate(), NEW_OBJECT_WITH_LEN(TypeArguments, len));
364 reader->AddBackwardReference(object_id, &type_arguments); 353 reader->AddBackwardReference(object_id, &type_arguments);
365 354
366 // Now set all the object fields. 355 // Now set all the object fields.
367 for (intptr_t i = 0; i < len; i++) { 356 for (intptr_t i = 0; i < len; i++) {
368 *reader->TypeHandle() ^= reader->ReadObject(); 357 *reader->TypeHandle() ^= reader->ReadObject();
369 type_arguments.SetTypeAt(i, *reader->TypeHandle()); 358 type_arguments.SetTypeAt(i, *reader->TypeHandle());
370 } 359 }
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 567
579 568
580 RawTokenStream* TokenStream::ReadFrom(SnapshotReader* reader, 569 RawTokenStream* TokenStream::ReadFrom(SnapshotReader* reader,
581 intptr_t object_id, 570 intptr_t object_id,
582 intptr_t tags, 571 intptr_t tags,
583 Snapshot::Kind kind) { 572 Snapshot::Kind kind) {
584 ASSERT(reader != NULL); 573 ASSERT(reader != NULL);
585 ASSERT(kind != Snapshot::kMessage && !RawObject::IsCreatedFromSnapshot(tags)); 574 ASSERT(kind != Snapshot::kMessage && !RawObject::IsCreatedFromSnapshot(tags));
586 575
587 // Read the length so that we can determine number of tokens to read. 576 // Read the length so that we can determine number of tokens to read.
588 intptr_t len = GetSmiValue(reader->ReadIntptrValue()); 577 intptr_t len = reader->ReadSmiValue();
589 578
590 // Create the token stream object. 579 // Create the token stream object.
591 TokenStream& token_stream = TokenStream::ZoneHandle( 580 TokenStream& token_stream = TokenStream::ZoneHandle(
592 reader->isolate(), NEW_OBJECT_WITH_LEN(TokenStream, len)); 581 reader->isolate(), NEW_OBJECT_WITH_LEN(TokenStream, len));
593 reader->AddBackwardReference(object_id, &token_stream); 582 reader->AddBackwardReference(object_id, &token_stream);
594 583
595 // Set the object tags. 584 // Set the object tags.
596 token_stream.set_tags(tags); 585 token_stream.set_tags(tags);
597 586
598 // Read the token stream into the TokenStream. 587 // Read the token stream into the TokenStream.
599 for (intptr_t i = 0; i < len; i++) { 588 for (intptr_t i = 0; i < len; i++) {
600 Token::Kind kind = static_cast<Token::Kind>( 589 Token::Kind kind = static_cast<Token::Kind>(reader->ReadSmiValue());
601 GetSmiValue(reader->ReadIntptrValue()));
602 *reader->StringHandle() ^= reader->ReadObject(); 590 *reader->StringHandle() ^= reader->ReadObject();
603 token_stream.SetTokenAt(i, kind, *reader->StringHandle()); 591 token_stream.SetTokenAt(i, kind, *reader->StringHandle());
604 } 592 }
605 return token_stream.raw(); 593 return token_stream.raw();
606 } 594 }
607 595
608 596
609 void RawTokenStream::WriteTo(SnapshotWriter* writer, 597 void RawTokenStream::WriteTo(SnapshotWriter* writer,
610 intptr_t object_id, 598 intptr_t object_id,
611 Snapshot::Kind kind) { 599 Snapshot::Kind kind) {
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after
1268 } 1256 }
1269 } 1257 }
1270 1258
1271 1259
1272 RawOneByteString* OneByteString::ReadFrom(SnapshotReader* reader, 1260 RawOneByteString* OneByteString::ReadFrom(SnapshotReader* reader,
1273 intptr_t object_id, 1261 intptr_t object_id,
1274 intptr_t tags, 1262 intptr_t tags,
1275 Snapshot::Kind kind) { 1263 Snapshot::Kind kind) {
1276 // Read the length so that we can determine instance size to allocate. 1264 // Read the length so that we can determine instance size to allocate.
1277 ASSERT(reader != NULL); 1265 ASSERT(reader != NULL);
1278 intptr_t len = GetSmiValue(reader->ReadIntptrValue()); 1266 intptr_t len = reader->ReadSmiValue();
1279 intptr_t hash = GetSmiValue(reader->ReadIntptrValue()); 1267 intptr_t hash = reader->ReadSmiValue();
1280 OneByteString& str_obj = OneByteString::ZoneHandle(reader->isolate(), 1268 OneByteString& str_obj = OneByteString::ZoneHandle(reader->isolate(),
1281 OneByteString::null()); 1269 OneByteString::null());
1282 1270
1283 if (kind == Snapshot::kFull) { 1271 if (kind == Snapshot::kFull) {
1284 ASSERT(reader->isolate()->no_gc_scope_depth() != 0); 1272 ASSERT(reader->isolate()->no_gc_scope_depth() != 0);
1285 RawOneByteString* obj = reader->NewOneByteString(len); 1273 RawOneByteString* obj = reader->NewOneByteString(len);
1286 str_obj = obj; 1274 str_obj = obj;
1287 str_obj.set_tags(tags); 1275 str_obj.set_tags(tags);
1288 obj->ptr()->hash_ = Smi::New(hash); 1276 obj->ptr()->hash_ = Smi::New(hash);
1289 uint8_t* raw_ptr = (len > 0) ? str_obj.CharAddr(0) : NULL; 1277 uint8_t* raw_ptr = (len > 0) ? str_obj.CharAddr(0) : NULL;
(...skipping 10 matching lines...) Expand all
1300 return str_obj.raw(); 1288 return str_obj.raw();
1301 } 1289 }
1302 1290
1303 1291
1304 RawTwoByteString* TwoByteString::ReadFrom(SnapshotReader* reader, 1292 RawTwoByteString* TwoByteString::ReadFrom(SnapshotReader* reader,
1305 intptr_t object_id, 1293 intptr_t object_id,
1306 intptr_t tags, 1294 intptr_t tags,
1307 Snapshot::Kind kind) { 1295 Snapshot::Kind kind) {
1308 // Read the length so that we can determine instance size to allocate. 1296 // Read the length so that we can determine instance size to allocate.
1309 ASSERT(reader != NULL); 1297 ASSERT(reader != NULL);
1310 intptr_t len = GetSmiValue(reader->ReadIntptrValue()); 1298 intptr_t len = reader->ReadSmiValue();
1311 intptr_t hash = GetSmiValue(reader->ReadIntptrValue()); 1299 intptr_t hash = reader->ReadSmiValue();
1312 TwoByteString& str_obj = TwoByteString::ZoneHandle(reader->isolate(), 1300 TwoByteString& str_obj = TwoByteString::ZoneHandle(reader->isolate(),
1313 TwoByteString::null()); 1301 TwoByteString::null());
1314 1302
1315 if (kind == Snapshot::kFull) { 1303 if (kind == Snapshot::kFull) {
1316 RawTwoByteString* obj = reader->NewTwoByteString(len); 1304 RawTwoByteString* obj = reader->NewTwoByteString(len);
1317 str_obj = obj; 1305 str_obj = obj;
1318 str_obj.set_tags(tags); 1306 str_obj.set_tags(tags);
1319 obj->ptr()->hash_ = Smi::New(hash); 1307 obj->ptr()->hash_ = Smi::New(hash);
1320 uint16_t* raw_ptr = (len > 0)? str_obj.CharAddr(0) : NULL; 1308 uint16_t* raw_ptr = (len > 0)? str_obj.CharAddr(0) : NULL;
1321 for (intptr_t i = 0; i < len; i++) { 1309 for (intptr_t i = 0; i < len; i++) {
1322 ASSERT(str_obj.CharAddr(i) == raw_ptr); // Will trigger assertions. 1310 ASSERT(str_obj.CharAddr(i) == raw_ptr); // Will trigger assertions.
1323 *raw_ptr = reader->Read<uint16_t>(); 1311 *raw_ptr = reader->Read<uint16_t>();
1324 raw_ptr += 1; 1312 raw_ptr += 1;
1325 } 1313 }
1326 ASSERT(String::Hash(str_obj, 0, str_obj.Length()) == hash); 1314 ASSERT(String::Hash(str_obj, 0, str_obj.Length()) == hash);
1327 } else { 1315 } else {
1328 ReadFromImpl<TwoByteString, uint16_t>(reader, &str_obj, len, tags); 1316 ReadFromImpl<TwoByteString, uint16_t>(reader, &str_obj, len, tags);
1329 } 1317 }
1330 reader->AddBackwardReference(object_id, &str_obj); 1318 reader->AddBackwardReference(object_id, &str_obj);
1331 return str_obj.raw(); 1319 return str_obj.raw();
1332 } 1320 }
1333 1321
1334 1322
1335 RawFourByteString* FourByteString::ReadFrom(SnapshotReader* reader, 1323 RawFourByteString* FourByteString::ReadFrom(SnapshotReader* reader,
1336 intptr_t object_id, 1324 intptr_t object_id,
1337 intptr_t tags, 1325 intptr_t tags,
1338 Snapshot::Kind kind) { 1326 Snapshot::Kind kind) {
1339 // Read the length so that we can determine instance size to allocate. 1327 // Read the length so that we can determine instance size to allocate.
1340 ASSERT(reader != NULL); 1328 ASSERT(reader != NULL);
1341 intptr_t len = GetSmiValue(reader->ReadIntptrValue()); 1329 intptr_t len = reader->ReadSmiValue();
1342 intptr_t hash = GetSmiValue(reader->ReadIntptrValue()); 1330 intptr_t hash = reader->ReadSmiValue();
1343 FourByteString& str_obj = FourByteString::ZoneHandle(reader->isolate(), 1331 FourByteString& str_obj = FourByteString::ZoneHandle(reader->isolate(),
1344 FourByteString::null()); 1332 FourByteString::null());
1345 1333
1346 if (kind == Snapshot::kFull) { 1334 if (kind == Snapshot::kFull) {
1347 RawFourByteString* obj = reader->NewFourByteString(len); 1335 RawFourByteString* obj = reader->NewFourByteString(len);
1348 str_obj = obj; 1336 str_obj = obj;
1349 str_obj.set_tags(tags); 1337 str_obj.set_tags(tags);
1350 obj->ptr()->hash_ = Smi::New(hash); 1338 obj->ptr()->hash_ = Smi::New(hash);
1351 uint32_t* raw_ptr = (len > 0)? str_obj.CharAddr(0) : NULL; 1339 uint32_t* raw_ptr = (len > 0)? str_obj.CharAddr(0) : NULL;
1352 for (intptr_t i = 0; i < len; i++) { 1340 for (intptr_t i = 0; i < len; i++) {
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1547 } 1535 }
1548 1536
1549 1537
1550 RawArray* Array::ReadFrom(SnapshotReader* reader, 1538 RawArray* Array::ReadFrom(SnapshotReader* reader,
1551 intptr_t object_id, 1539 intptr_t object_id,
1552 intptr_t tags, 1540 intptr_t tags,
1553 Snapshot::Kind kind) { 1541 Snapshot::Kind kind) {
1554 ASSERT(reader != NULL); 1542 ASSERT(reader != NULL);
1555 1543
1556 // Read the length so that we can determine instance size to allocate. 1544 // Read the length so that we can determine instance size to allocate.
1557 intptr_t len = GetSmiValue(reader->ReadIntptrValue()); 1545 intptr_t len = reader->ReadSmiValue();
1558 Array& array = Array::ZoneHandle(reader->isolate(), 1546 Array& array = Array::ZoneHandle(reader->isolate(),
1559 NEW_OBJECT_WITH_LEN(Array, len)); 1547 NEW_OBJECT_WITH_LEN(Array, len));
1560 reader->AddBackwardReference(object_id, &array); 1548 reader->AddBackwardReference(object_id, &array);
1561 ArrayReadFrom(reader, array, len, tags); 1549 ArrayReadFrom(reader, array, len, tags);
1562 return array.raw(); 1550 return array.raw();
1563 } 1551 }
1564 1552
1565 1553
1566 RawImmutableArray* ImmutableArray::ReadFrom(SnapshotReader* reader, 1554 RawImmutableArray* ImmutableArray::ReadFrom(SnapshotReader* reader,
1567 intptr_t object_id, 1555 intptr_t object_id,
1568 intptr_t tags, 1556 intptr_t tags,
1569 Snapshot::Kind kind) { 1557 Snapshot::Kind kind) {
1570 ASSERT(reader != NULL); 1558 ASSERT(reader != NULL);
1571 1559
1572 // Read the length so that we can determine instance size to allocate. 1560 // Read the length so that we can determine instance size to allocate.
1573 intptr_t len = GetSmiValue(reader->ReadIntptrValue()); 1561 intptr_t len = reader->ReadSmiValue();
1574 ImmutableArray& array = ImmutableArray::ZoneHandle( 1562 ImmutableArray& array = ImmutableArray::ZoneHandle(
1575 reader->isolate(), NEW_OBJECT_WITH_LEN(ImmutableArray, len)); 1563 reader->isolate(), NEW_OBJECT_WITH_LEN(ImmutableArray, len));
1576 reader->AddBackwardReference(object_id, &array); 1564 reader->AddBackwardReference(object_id, &array);
1577 ArrayReadFrom(reader, array, len, tags); 1565 ArrayReadFrom(reader, array, len, tags);
1578 return array.raw(); 1566 return array.raw();
1579 } 1567 }
1580 1568
1581 1569
1582 static void ArrayWriteTo(SnapshotWriter* writer, 1570 static void ArrayWriteTo(SnapshotWriter* writer,
1583 intptr_t object_id, 1571 intptr_t object_id,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
1646 } 1634 }
1647 1635
1648 1636
1649 RawInternalByteArray* InternalByteArray::ReadFrom(SnapshotReader* reader, 1637 RawInternalByteArray* InternalByteArray::ReadFrom(SnapshotReader* reader,
1650 intptr_t object_id, 1638 intptr_t object_id,
1651 intptr_t tags, 1639 intptr_t tags,
1652 Snapshot::Kind kind) { 1640 Snapshot::Kind kind) {
1653 ASSERT(reader != NULL); 1641 ASSERT(reader != NULL);
1654 1642
1655 // Read the length so that we can determine instance size to allocate. 1643 // Read the length so that we can determine instance size to allocate.
1656 intptr_t len = GetSmiValue(reader->ReadIntptrValue()); 1644 intptr_t len = reader->ReadSmiValue();
1657 Heap::Space space = (kind == Snapshot::kFull) ? Heap::kOld : Heap::kNew; 1645 Heap::Space space = (kind == Snapshot::kFull) ? Heap::kOld : Heap::kNew;
1658 InternalByteArray& result = 1646 InternalByteArray& result =
1659 InternalByteArray::ZoneHandle(reader->isolate(), 1647 InternalByteArray::ZoneHandle(reader->isolate(),
1660 InternalByteArray::New(len, space)); 1648 InternalByteArray::New(len, space));
1661 reader->AddBackwardReference(object_id, &result); 1649 reader->AddBackwardReference(object_id, &result);
1662 1650
1663 // Set the object tags. 1651 // Set the object tags.
1664 result.set_tags(tags); 1652 result.set_tags(tags);
1665 1653
1666 // Setup the array elements. 1654 // Setup the array elements.
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1773 1761
1774 1762
1775 RawJSRegExp* JSRegExp::ReadFrom(SnapshotReader* reader, 1763 RawJSRegExp* JSRegExp::ReadFrom(SnapshotReader* reader,
1776 intptr_t object_id, 1764 intptr_t object_id,
1777 intptr_t tags, 1765 intptr_t tags,
1778 Snapshot::Kind kind) { 1766 Snapshot::Kind kind) {
1779 ASSERT(reader != NULL); 1767 ASSERT(reader != NULL);
1780 ASSERT(kind == Snapshot::kMessage); 1768 ASSERT(kind == Snapshot::kMessage);
1781 1769
1782 // Read the length so that we can determine instance size to allocate. 1770 // Read the length so that we can determine instance size to allocate.
1783 intptr_t len = GetSmiValue(reader->ReadIntptrValue()); 1771 intptr_t len = reader->ReadSmiValue();
1784 1772
1785 // Allocate JSRegExp object. 1773 // Allocate JSRegExp object.
1786 JSRegExp& regex = JSRegExp::ZoneHandle( 1774 JSRegExp& regex = JSRegExp::ZoneHandle(
1787 reader->isolate(), 1775 reader->isolate(),
1788 JSRegExp::New(len, (kind == Snapshot::kFull) ? Heap::kOld : Heap::kNew)); 1776 JSRegExp::New(len, (kind == Snapshot::kFull) ? Heap::kOld : Heap::kNew));
1789 reader->AddBackwardReference(object_id, &regex); 1777 reader->AddBackwardReference(object_id, &regex);
1790 1778
1791 // Set the object tags. 1779 // Set the object tags.
1792 regex.set_tags(tags); 1780 regex.set_tags(tags);
1793 1781
1794 // Read and Set all the other fields. 1782 // Read and Set all the other fields.
1795 regex.raw_ptr()->num_bracket_expressions_ = AsSmi(reader->ReadIntptrValue()); 1783 regex.raw_ptr()->num_bracket_expressions_ = reader->ReadAsSmi();
1796 *reader->StringHandle() ^= reader->ReadObject(); 1784 *reader->StringHandle() ^= reader->ReadObject();
1797 regex.raw_ptr()->pattern_ = (*reader->StringHandle()).raw(); 1785 regex.raw_ptr()->pattern_ = (*reader->StringHandle()).raw();
1798 regex.raw_ptr()->type_ = reader->ReadIntptrValue(); 1786 regex.raw_ptr()->type_ = reader->ReadIntptrValue();
1799 regex.raw_ptr()->flags_ = reader->ReadIntptrValue(); 1787 regex.raw_ptr()->flags_ = reader->ReadIntptrValue();
1800 1788
1801 // TODO(5411462): Need to implement a way of recompiling the regex. 1789 // TODO(5411462): Need to implement a way of recompiling the regex.
1802 1790
1803 return regex.raw(); 1791 return regex.raw();
1804 } 1792 }
1805 1793
(...skipping 16 matching lines...) Expand all
1822 // Write out all the other fields. 1810 // Write out all the other fields.
1823 writer->Write<RawObject*>(ptr()->num_bracket_expressions_); 1811 writer->Write<RawObject*>(ptr()->num_bracket_expressions_);
1824 writer->WriteObject(ptr()->pattern_); 1812 writer->WriteObject(ptr()->pattern_);
1825 writer->WriteIntptrValue(ptr()->type_); 1813 writer->WriteIntptrValue(ptr()->type_);
1826 writer->WriteIntptrValue(ptr()->flags_); 1814 writer->WriteIntptrValue(ptr()->flags_);
1827 1815
1828 // Do not write out the data part which is native. 1816 // Do not write out the data part which is native.
1829 } 1817 }
1830 1818
1831 } // namespace dart 1819 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698