OLD | NEW |
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" | |
6 #include "vm/object.h" | 5 #include "vm/object.h" |
7 #include "vm/object_store.h" | 6 #include "vm/object_store.h" |
8 #include "vm/snapshot.h" | 7 #include "vm/snapshot.h" |
9 #include "vm/stub_code.h" | 8 #include "vm/stub_code.h" |
10 #include "vm/symbols.h" | 9 #include "vm/symbols.h" |
11 #include "vm/visitor.h" | 10 #include "vm/visitor.h" |
12 | 11 |
13 namespace dart { | 12 namespace dart { |
14 | 13 |
15 #define NEW_OBJECT(type) \ | 14 #define NEW_OBJECT(type) \ |
16 ((kind == Snapshot::kFull) ? reader->New##type() : type::New()) | 15 ((kind == Snapshot::kFull) ? reader->New##type() : type::New()) |
17 | 16 |
18 #define NEW_OBJECT_WITH_LEN(type, len) \ | 17 #define NEW_OBJECT_WITH_LEN(type, len) \ |
19 ((kind == Snapshot::kFull) ? reader->New##type(len) : type::New(len)) | 18 ((kind == Snapshot::kFull) ? reader->New##type(len) : type::New(len)) |
20 | 19 |
21 #define NEW_OBJECT_WITH_LEN_SPACE(type, len, kind) \ | 20 #define NEW_OBJECT_WITH_LEN_SPACE(type, len, kind) \ |
22 ((kind == Snapshot::kFull) ? \ | 21 ((kind == Snapshot::kFull) ? \ |
23 reader->New##type(len) : type::New(len, HEAP_SPACE(kind))) | 22 reader->New##type(len) : type::New(len, HEAP_SPACE(kind))) |
24 | 23 |
25 | 24 |
26 static uword BigintAllocator(intptr_t size) { | |
27 Zone* zone = Isolate::Current()->current_zone(); | |
28 return zone->AllocUnsafe(size); | |
29 } | |
30 | |
31 | |
32 RawClass* Class::ReadFrom(SnapshotReader* reader, | 25 RawClass* Class::ReadFrom(SnapshotReader* reader, |
33 intptr_t object_id, | 26 intptr_t object_id, |
34 intptr_t tags, | 27 intptr_t tags, |
35 Snapshot::Kind kind) { | 28 Snapshot::Kind kind) { |
36 ASSERT(reader != NULL); | 29 ASSERT(reader != NULL); |
37 | 30 |
38 Class& cls = Class::ZoneHandle(reader->isolate(), Class::null()); | 31 Class& cls = Class::ZoneHandle(reader->isolate(), Class::null()); |
39 if ((kind == Snapshot::kFull) || | 32 if ((kind == Snapshot::kFull) || |
40 (kind == Snapshot::kScript && !RawObject::IsCreatedFromSnapshot(tags))) { | 33 (kind == Snapshot::kScript && !RawObject::IsCreatedFromSnapshot(tags))) { |
41 // Read in the base information. | 34 // Read in the base information. |
(...skipping 1663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1705 // Write out the class and tags information. | 1698 // Write out the class and tags information. |
1706 writer->WriteIndexedObject(kMintCid); | 1699 writer->WriteIndexedObject(kMintCid); |
1707 writer->WriteTags(writer->GetObjectTags(this)); | 1700 writer->WriteTags(writer->GetObjectTags(this)); |
1708 | 1701 |
1709 // Write out the 64 bit value. | 1702 // Write out the 64 bit value. |
1710 writer->Write<int64_t>(ptr()->value_); | 1703 writer->Write<int64_t>(ptr()->value_); |
1711 } | 1704 } |
1712 | 1705 |
1713 | 1706 |
1714 RawBigint* Bigint::ReadFrom(SnapshotReader* reader, | 1707 RawBigint* Bigint::ReadFrom(SnapshotReader* reader, |
1715 intptr_t object_id, | 1708 intptr_t object_id, |
1716 intptr_t tags, | 1709 intptr_t tags, |
1717 Snapshot::Kind kind) { | 1710 Snapshot::Kind kind) { |
1718 ASSERT(reader != NULL); | 1711 ASSERT(reader != NULL); |
1719 | 1712 |
1720 // Read in the HexCString representation of the bigint. | 1713 // Allocate bigint object. |
1721 intptr_t len = reader->ReadIntptrValue(); | 1714 Bigint& obj = Bigint::ZoneHandle(reader->isolate(), NEW_OBJECT(Bigint)); |
1722 char* str = reader->isolate()->current_zone()->Alloc<char>(len + 1); | 1715 reader->AddBackRef(object_id, &obj, kIsDeserialized); |
1723 str[len] = '\0'; | |
1724 reader->ReadBytes(reinterpret_cast<uint8_t*>(str), len); | |
1725 | 1716 |
1726 // Create a Bigint object from HexCString. | 1717 // Set all the object fields. |
1727 Bigint& obj = Bigint::ZoneHandle( | 1718 // TODO(5411462): Need to assert No GC can happen here, even though |
1728 reader->isolate(), | 1719 // allocations may happen. |
1729 ((kind == Snapshot::kFull) ? reader->NewBigint(str) : | 1720 intptr_t num_flds = (obj.raw()->to() - obj.raw()->from()); |
1730 BigintOperations::FromHexCString(str, HEAP_SPACE(kind)))); | 1721 for (intptr_t i = 0; i <= num_flds; i++) { |
| 1722 (*reader->PassiveObjectHandle()) = reader->ReadObjectRef(); |
| 1723 obj.StorePointer(obj.raw()->from() + i, |
| 1724 reader->PassiveObjectHandle()->raw()); |
| 1725 } |
1731 | 1726 |
1732 // If it is a canonical constant make it one. | 1727 // If it is a canonical constant make it one. |
1733 // When reading a full snapshot we don't need to canonicalize the object | 1728 // When reading a full snapshot we don't need to canonicalize the object |
1734 // as it would already be a canonical object. | 1729 // as it would already be a canonical object. |
1735 // When reading a script snapshot we need to canonicalize only those object | 1730 // When reading a script snapshot we need to canonicalize only those object |
1736 // references that are objects from the core library (loaded from a | 1731 // references that are objects from the core library (loaded from a |
1737 // full snapshot). Objects that are only in the script need not be | 1732 // full snapshot). Objects that are only in the script need not be |
1738 // canonicalized as they are already canonical. | 1733 // canonicalized as they are already canonical. |
1739 // When reading a message snapshot we always have to canonicalize the object. | 1734 // When reading a message snapshot we always have to canonicalize the object. |
1740 if ((kind != Snapshot::kFull) && RawObject::IsCanonical(tags) && | 1735 if ((kind != Snapshot::kFull) && RawObject::IsCanonical(tags) && |
1741 (RawObject::IsCreatedFromSnapshot(tags) || | 1736 (RawObject::IsCreatedFromSnapshot(tags) || |
1742 (kind == Snapshot::kMessage))) { | 1737 (kind == Snapshot::kMessage))) { |
1743 obj ^= obj.CheckAndCanonicalize(NULL); | 1738 obj ^= obj.CheckAndCanonicalize(NULL); |
1744 ASSERT(!obj.IsNull()); | 1739 ASSERT(!obj.IsNull()); |
1745 } | 1740 } |
1746 reader->AddBackRef(object_id, &obj, kIsDeserialized); | |
1747 | 1741 |
1748 // Set the object tags. | 1742 // Set the object tags. |
1749 obj.set_tags(tags); | 1743 obj.set_tags(tags); |
1750 | 1744 |
1751 return obj.raw(); | 1745 return obj.raw(); |
1752 } | 1746 } |
1753 | 1747 |
1754 | 1748 |
1755 void RawBigint::WriteTo(SnapshotWriter* writer, | 1749 void RawBigint::WriteTo(SnapshotWriter* writer, |
1756 intptr_t object_id, | 1750 intptr_t object_id, |
1757 Snapshot::Kind kind) { | 1751 Snapshot::Kind kind) { |
1758 ASSERT(writer != NULL); | 1752 ASSERT(writer != NULL); |
1759 | 1753 |
1760 // Write out the serialization header value for this object. | 1754 // Write out the serialization header value for this object. |
1761 writer->WriteInlinedObjectHeader(object_id); | 1755 writer->WriteInlinedObjectHeader(object_id); |
1762 | 1756 |
1763 // Write out the class and tags information. | 1757 // Write out the class and tags information. |
1764 writer->WriteIndexedObject(kBigintCid); | 1758 writer->WriteIndexedObject(kBigintCid); |
1765 writer->WriteTags(writer->GetObjectTags(this)); | 1759 writer->WriteTags(writer->GetObjectTags(this)); |
1766 | 1760 |
1767 // Write out the bigint value as a HEXCstring. | 1761 // Write out all the object pointer fields. |
1768 intptr_t length = ptr()->signed_length_; | 1762 SnapshotWriterVisitor visitor(writer); |
1769 bool is_negative = false; | 1763 visitor.VisitPointers(from(), to()); |
1770 if (length <= 0) { | |
1771 length = -length; | |
1772 is_negative = true; | |
1773 } | |
1774 uword data_start = reinterpret_cast<uword>(ptr()) + sizeof(RawBigint); | |
1775 const char* str = BigintOperations::ToHexCString( | |
1776 length, | |
1777 is_negative, | |
1778 reinterpret_cast<void*>(data_start), | |
1779 &BigintAllocator); | |
1780 bool neg = false; | |
1781 if (*str == '-') { | |
1782 neg = true; | |
1783 str++; | |
1784 } | |
1785 intptr_t len = strlen(str); | |
1786 ASSERT(len > 2 && str[0] == '0' && str[1] == 'x'); | |
1787 if (neg) { | |
1788 writer->WriteIntptrValue(len - 1); // Include '-' in length. | |
1789 writer->Write<uint8_t>('-'); | |
1790 } else { | |
1791 writer->WriteIntptrValue(len - 2); | |
1792 } | |
1793 writer->WriteBytes(reinterpret_cast<const uint8_t*>(&(str[2])), (len - 2)); | |
1794 } | 1764 } |
1795 | 1765 |
1796 | 1766 |
1797 RawDouble* Double::ReadFrom(SnapshotReader* reader, | 1767 RawDouble* Double::ReadFrom(SnapshotReader* reader, |
1798 intptr_t object_id, | 1768 intptr_t object_id, |
1799 intptr_t tags, | 1769 intptr_t tags, |
1800 Snapshot::Kind kind) { | 1770 Snapshot::Kind kind) { |
1801 ASSERT(reader != NULL); | 1771 ASSERT(reader != NULL); |
1802 ASSERT(kind != Snapshot::kMessage); | 1772 ASSERT(kind != Snapshot::kMessage); |
1803 // Read the double value for the object. | 1773 // Read the double value for the object. |
(...skipping 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2869 // We do not allow objects with native fields in an isolate message. | 2839 // We do not allow objects with native fields in an isolate message. |
2870 writer->SetWriteException(Exceptions::kArgument, | 2840 writer->SetWriteException(Exceptions::kArgument, |
2871 "Illegal argument in isolate message" | 2841 "Illegal argument in isolate message" |
2872 " : (object is a UserTag)"); | 2842 " : (object is a UserTag)"); |
2873 } else { | 2843 } else { |
2874 UNREACHABLE(); | 2844 UNREACHABLE(); |
2875 } | 2845 } |
2876 } | 2846 } |
2877 | 2847 |
2878 } // namespace dart | 2848 } // namespace dart |
OLD | NEW |