| 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 #ifndef VM_SNAPSHOT_H_ | 5 #ifndef VM_SNAPSHOT_H_ |
| 6 #define VM_SNAPSHOT_H_ | 6 #define VM_SNAPSHOT_H_ |
| 7 | 7 |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "vm/allocation.h" | 9 #include "vm/allocation.h" |
| 10 #include "vm/bitfield.h" | 10 #include "vm/bitfield.h" |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 | 217 |
| 218 static const intptr_t kHeaderSize = OS::kMaxPreferredCodeAlignment; | 218 static const intptr_t kHeaderSize = OS::kMaxPreferredCodeAlignment; |
| 219 | 219 |
| 220 private: | 220 private: |
| 221 const void* raw_memory_; // The symbol kInstructionsSnapshot. | 221 const void* raw_memory_; // The symbol kInstructionsSnapshot. |
| 222 | 222 |
| 223 DISALLOW_COPY_AND_ASSIGN(InstructionsSnapshot); | 223 DISALLOW_COPY_AND_ASSIGN(InstructionsSnapshot); |
| 224 }; | 224 }; |
| 225 | 225 |
| 226 | 226 |
| 227 class DataSnapshot : ValueObject { |
| 228 public: |
| 229 explicit DataSnapshot(const void* raw_memory) |
| 230 : raw_memory_(raw_memory) { |
| 231 ASSERT(Utils::IsAligned(raw_memory, 2 * kWordSize)); // kObjectAlignment |
| 232 } |
| 233 |
| 234 void* data_start() { |
| 235 return reinterpret_cast<void*>( |
| 236 reinterpret_cast<uword>(raw_memory_) + kHeaderSize); |
| 237 } |
| 238 |
| 239 uword data_size() { |
| 240 uword snapshot_size = *reinterpret_cast<const uword*>(raw_memory_); |
| 241 return snapshot_size - kHeaderSize; |
| 242 } |
| 243 |
| 244 // Header: data length and padding for alignment. We use the same alignment |
| 245 // as for code for now. |
| 246 static const intptr_t kHeaderSize = OS::kMaxPreferredCodeAlignment; |
| 247 |
| 248 private: |
| 249 const void* raw_memory_; // The symbol kDataSnapshot. |
| 250 |
| 251 DISALLOW_COPY_AND_ASSIGN(DataSnapshot); |
| 252 }; |
| 253 |
| 254 |
| 227 class BaseReader { | 255 class BaseReader { |
| 228 public: | 256 public: |
| 229 BaseReader(const uint8_t* buffer, intptr_t size) : stream_(buffer, size) {} | 257 BaseReader(const uint8_t* buffer, intptr_t size) : stream_(buffer, size) {} |
| 230 // Reads raw data (for basic types). | 258 // Reads raw data (for basic types). |
| 231 // sizeof(T) must be in {1,2,4,8}. | 259 // sizeof(T) must be in {1,2,4,8}. |
| 232 template <typename T> | 260 template <typename T> |
| 233 T Read() { | 261 T Read() { |
| 234 return ReadStream::Raw<sizeof(T), T>::Read(&stream_); | 262 return ReadStream::Raw<sizeof(T), T>::Read(&stream_); |
| 235 } | 263 } |
| 236 | 264 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 private: | 354 private: |
| 327 Object* reference_; | 355 Object* reference_; |
| 328 DeserializeState state_; | 356 DeserializeState state_; |
| 329 bool defer_canonicalization_; | 357 bool defer_canonicalization_; |
| 330 ZoneGrowableArray<intptr_t>* patch_records_; | 358 ZoneGrowableArray<intptr_t>* patch_records_; |
| 331 }; | 359 }; |
| 332 | 360 |
| 333 | 361 |
| 334 class InstructionsReader : public ZoneAllocated { | 362 class InstructionsReader : public ZoneAllocated { |
| 335 public: | 363 public: |
| 336 explicit InstructionsReader(const uint8_t* buffer) | 364 InstructionsReader(const uint8_t* instructions_buffer, |
| 337 : buffer_(buffer) { | 365 const uint8_t* data_buffer) |
| 338 ASSERT(buffer != NULL); | 366 : instructions_buffer_(instructions_buffer), |
| 339 ASSERT(Utils::IsAligned(reinterpret_cast<uword>(buffer), | 367 data_buffer_(data_buffer) { |
| 368 ASSERT(instructions_buffer != NULL); |
| 369 ASSERT(data_buffer != NULL); |
| 370 ASSERT(Utils::IsAligned(reinterpret_cast<uword>(instructions_buffer), |
| 340 OS::PreferredCodeAlignment())); | 371 OS::PreferredCodeAlignment())); |
| 341 } | 372 } |
| 342 | 373 |
| 343 RawInstructions* GetInstructionsAt(int32_t offset, uword expected_tags); | 374 RawInstructions* GetInstructionsAt(int32_t offset, uword expected_tags); |
| 375 RawObject* GetObjectAt(int32_t offset); |
| 344 | 376 |
| 345 private: | 377 private: |
| 346 const uint8_t* buffer_; | 378 const uint8_t* instructions_buffer_; |
| 379 const uint8_t* data_buffer_; |
| 347 | 380 |
| 348 DISALLOW_COPY_AND_ASSIGN(InstructionsReader); | 381 DISALLOW_COPY_AND_ASSIGN(InstructionsReader); |
| 349 }; | 382 }; |
| 350 | 383 |
| 351 | 384 |
| 352 // Reads a snapshot into objects. | 385 // Reads a snapshot into objects. |
| 353 class SnapshotReader : public BaseReader { | 386 class SnapshotReader : public BaseReader { |
| 354 public: | 387 public: |
| 355 Thread* thread() const { return thread_; } | 388 Thread* thread() const { return thread_; } |
| 356 Zone* zone() const { return zone_; } | 389 Zone* zone() const { return zone_; } |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 RawUnhandledException* NewUnhandledException(); | 482 RawUnhandledException* NewUnhandledException(); |
| 450 RawObject* NewInteger(int64_t value); | 483 RawObject* NewInteger(int64_t value); |
| 451 RawStacktrace* NewStacktrace(); | 484 RawStacktrace* NewStacktrace(); |
| 452 RawWeakProperty* NewWeakProperty(); | 485 RawWeakProperty* NewWeakProperty(); |
| 453 RawJSRegExp* NewJSRegExp(); | 486 RawJSRegExp* NewJSRegExp(); |
| 454 | 487 |
| 455 RawInstructions* GetInstructionsAt(int32_t offset, uword expected_tags) { | 488 RawInstructions* GetInstructionsAt(int32_t offset, uword expected_tags) { |
| 456 return instructions_reader_->GetInstructionsAt(offset, expected_tags); | 489 return instructions_reader_->GetInstructionsAt(offset, expected_tags); |
| 457 } | 490 } |
| 458 | 491 |
| 492 RawObject* GetObjectAt(int32_t offset) { |
| 493 return instructions_reader_->GetObjectAt(offset); |
| 494 } |
| 495 |
| 459 const uint8_t* instructions_buffer_; | 496 const uint8_t* instructions_buffer_; |
| 497 const uint8_t* data_buffer_; |
| 460 | 498 |
| 461 protected: | 499 protected: |
| 462 SnapshotReader(const uint8_t* buffer, | 500 SnapshotReader(const uint8_t* buffer, |
| 463 intptr_t size, | 501 intptr_t size, |
| 464 const uint8_t* instructions_buffer, | 502 const uint8_t* instructions_buffer, |
| 503 const uint8_t* data_buffer, |
| 465 Snapshot::Kind kind, | 504 Snapshot::Kind kind, |
| 466 ZoneGrowableArray<BackRefNode>* backward_references, | 505 ZoneGrowableArray<BackRefNode>* backward_references, |
| 467 Thread* thread); | 506 Thread* thread); |
| 468 ~SnapshotReader() { } | 507 ~SnapshotReader() { } |
| 469 | 508 |
| 470 ZoneGrowableArray<BackRefNode>* GetBackwardReferenceTable() const { | 509 ZoneGrowableArray<BackRefNode>* GetBackwardReferenceTable() const { |
| 471 return backward_references_; | 510 return backward_references_; |
| 472 } | 511 } |
| 473 void ResetBackwardReferenceTable() { backward_references_ = NULL; } | 512 void ResetBackwardReferenceTable() { backward_references_ = NULL; } |
| 474 PageSpace* old_space() const { return old_space_; } | 513 PageSpace* old_space() const { return old_space_; } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 friend class WeakProperty; | 640 friend class WeakProperty; |
| 602 DISALLOW_COPY_AND_ASSIGN(SnapshotReader); | 641 DISALLOW_COPY_AND_ASSIGN(SnapshotReader); |
| 603 }; | 642 }; |
| 604 | 643 |
| 605 | 644 |
| 606 class VmIsolateSnapshotReader : public SnapshotReader { | 645 class VmIsolateSnapshotReader : public SnapshotReader { |
| 607 public: | 646 public: |
| 608 VmIsolateSnapshotReader(const uint8_t* buffer, | 647 VmIsolateSnapshotReader(const uint8_t* buffer, |
| 609 intptr_t size, | 648 intptr_t size, |
| 610 const uint8_t* instructions_buffer, | 649 const uint8_t* instructions_buffer, |
| 650 const uint8_t* data_buffer, |
| 611 Thread* thread); | 651 Thread* thread); |
| 612 ~VmIsolateSnapshotReader(); | 652 ~VmIsolateSnapshotReader(); |
| 613 | 653 |
| 614 RawApiError* ReadVmIsolateSnapshot(); | 654 RawApiError* ReadVmIsolateSnapshot(); |
| 615 | 655 |
| 616 private: | 656 private: |
| 617 DISALLOW_COPY_AND_ASSIGN(VmIsolateSnapshotReader); | 657 DISALLOW_COPY_AND_ASSIGN(VmIsolateSnapshotReader); |
| 618 }; | 658 }; |
| 619 | 659 |
| 620 | 660 |
| 621 class IsolateSnapshotReader : public SnapshotReader { | 661 class IsolateSnapshotReader : public SnapshotReader { |
| 622 public: | 662 public: |
| 623 IsolateSnapshotReader(const uint8_t* buffer, | 663 IsolateSnapshotReader(const uint8_t* buffer, |
| 624 intptr_t size, | 664 intptr_t size, |
| 625 const uint8_t* instructions_buffer, | 665 const uint8_t* instructions_buffer, |
| 666 const uint8_t* data_buffer, |
| 626 Thread* thread); | 667 Thread* thread); |
| 627 ~IsolateSnapshotReader(); | 668 ~IsolateSnapshotReader(); |
| 628 | 669 |
| 629 private: | 670 private: |
| 630 DISALLOW_COPY_AND_ASSIGN(IsolateSnapshotReader); | 671 DISALLOW_COPY_AND_ASSIGN(IsolateSnapshotReader); |
| 631 }; | 672 }; |
| 632 | 673 |
| 633 | 674 |
| 634 class ScriptSnapshotReader : public SnapshotReader { | 675 class ScriptSnapshotReader : public SnapshotReader { |
| 635 public: | 676 public: |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 }; | 845 }; |
| 805 | 846 |
| 806 | 847 |
| 807 class InstructionsWriter : public ZoneAllocated { | 848 class InstructionsWriter : public ZoneAllocated { |
| 808 public: | 849 public: |
| 809 InstructionsWriter(uint8_t** buffer, | 850 InstructionsWriter(uint8_t** buffer, |
| 810 ReAlloc alloc, | 851 ReAlloc alloc, |
| 811 intptr_t initial_size) | 852 intptr_t initial_size) |
| 812 : stream_(buffer, alloc, initial_size), | 853 : stream_(buffer, alloc, initial_size), |
| 813 next_offset_(InstructionsSnapshot::kHeaderSize), | 854 next_offset_(InstructionsSnapshot::kHeaderSize), |
| 855 next_object_offset_(DataSnapshot::kHeaderSize), |
| 814 binary_size_(0), | 856 binary_size_(0), |
| 815 instructions_() { | 857 instructions_(), |
| 858 objects_() { |
| 816 ASSERT(buffer != NULL); | 859 ASSERT(buffer != NULL); |
| 817 ASSERT(alloc != NULL); | 860 ASSERT(alloc != NULL); |
| 818 } | 861 } |
| 819 | 862 |
| 820 // Size of the snapshot (assembly code). | 863 // Size of the snapshot (assembly code). |
| 821 intptr_t BytesWritten() const { return stream_.bytes_written(); } | 864 intptr_t BytesWritten() const { return stream_.bytes_written(); } |
| 822 | 865 |
| 823 intptr_t binary_size() { return binary_size_; } | 866 intptr_t binary_size() { return binary_size_; } |
| 824 | 867 |
| 825 int32_t GetOffsetFor(RawInstructions* instructions); | 868 int32_t GetOffsetFor(RawInstructions* instructions); |
| 826 | 869 |
| 870 int32_t GetObjectOffsetFor(RawObject* raw_object); |
| 871 |
| 827 void SetInstructionsCode(RawInstructions* insns, RawCode* code) { | 872 void SetInstructionsCode(RawInstructions* insns, RawCode* code) { |
| 828 for (intptr_t i = 0; i < instructions_.length(); i++) { | 873 for (intptr_t i = 0; i < instructions_.length(); i++) { |
| 829 if (instructions_[i].raw_insns_ == insns) { | 874 if (instructions_[i].raw_insns_ == insns) { |
| 830 instructions_[i].raw_code_ = code; | 875 instructions_[i].raw_code_ = code; |
| 831 return; | 876 return; |
| 832 } | 877 } |
| 833 } | 878 } |
| 834 UNREACHABLE(); | 879 UNREACHABLE(); |
| 835 } | 880 } |
| 836 | 881 |
| 837 void WriteAssembly(); | 882 void WriteAssembly(); |
| 838 | 883 |
| 839 private: | 884 private: |
| 840 struct InstructionsData { | 885 struct InstructionsData { |
| 841 explicit InstructionsData(RawInstructions* insns) | 886 explicit InstructionsData(RawInstructions* insns) |
| 842 : raw_insns_(insns), raw_code_(NULL) { } | 887 : raw_insns_(insns), raw_code_(NULL) { } |
| 843 | 888 |
| 844 union { | 889 union { |
| 845 RawInstructions* raw_insns_; | 890 RawInstructions* raw_insns_; |
| 846 const Instructions* insns_; | 891 const Instructions* insns_; |
| 847 }; | 892 }; |
| 848 union { | 893 union { |
| 849 RawCode* raw_code_; | 894 RawCode* raw_code_; |
| 850 const Code* code_; | 895 const Code* code_; |
| 851 }; | 896 }; |
| 852 }; | 897 }; |
| 853 | 898 |
| 899 struct ObjectData { |
| 900 explicit ObjectData(RawObject* raw_obj) |
| 901 : raw_obj_(raw_obj) { } |
| 902 |
| 903 union { |
| 904 RawObject* raw_obj_; |
| 905 const Object* obj_; |
| 906 }; |
| 907 }; |
| 908 |
| 854 void WriteWordLiteral(uword value) { | 909 void WriteWordLiteral(uword value) { |
| 855 // Padding is helpful for comparing the .S with --disassemble. | 910 // Padding is helpful for comparing the .S with --disassemble. |
| 856 #if defined(ARCH_IS_64_BIT) | 911 #if defined(ARCH_IS_64_BIT) |
| 857 stream_.Print(".quad 0x%0.16" Px "\n", value); | 912 stream_.Print(".quad 0x%0.16" Px "\n", value); |
| 858 #else | 913 #else |
| 859 stream_.Print(".long 0x%0.8" Px "\n", value); | 914 stream_.Print(".long 0x%0.8" Px "\n", value); |
| 860 #endif | 915 #endif |
| 861 binary_size_ += sizeof(value); | 916 binary_size_ += sizeof(value); |
| 862 } | 917 } |
| 863 | 918 |
| 864 WriteStream stream_; | 919 WriteStream stream_; |
| 865 intptr_t next_offset_; | 920 intptr_t next_offset_; |
| 921 intptr_t next_object_offset_; |
| 866 intptr_t binary_size_; | 922 intptr_t binary_size_; |
| 867 GrowableArray<InstructionsData> instructions_; | 923 GrowableArray<InstructionsData> instructions_; |
| 924 GrowableArray<ObjectData> objects_; |
| 868 | 925 |
| 869 DISALLOW_COPY_AND_ASSIGN(InstructionsWriter); | 926 DISALLOW_COPY_AND_ASSIGN(InstructionsWriter); |
| 870 }; | 927 }; |
| 871 | 928 |
| 872 | 929 |
| 873 class SnapshotWriter : public BaseWriter { | 930 class SnapshotWriter : public BaseWriter { |
| 874 protected: | 931 protected: |
| 875 SnapshotWriter(Snapshot::Kind kind, | 932 SnapshotWriter(Snapshot::Kind kind, |
| 876 Thread* thread, | 933 Thread* thread, |
| 877 uint8_t** buffer, | 934 uint8_t** buffer, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 913 | 970 |
| 914 // Write a version string for the snapshot. | 971 // Write a version string for the snapshot. |
| 915 void WriteVersion(); | 972 void WriteVersion(); |
| 916 | 973 |
| 917 static intptr_t FirstObjectId(); | 974 static intptr_t FirstObjectId(); |
| 918 | 975 |
| 919 int32_t GetInstructionsId(RawInstructions* instructions) { | 976 int32_t GetInstructionsId(RawInstructions* instructions) { |
| 920 return instructions_writer_->GetOffsetFor(instructions); | 977 return instructions_writer_->GetOffsetFor(instructions); |
| 921 } | 978 } |
| 922 | 979 |
| 980 int32_t GetObjectId(RawObject* raw) { |
| 981 return instructions_writer_->GetObjectOffsetFor(raw); |
| 982 } |
| 983 |
| 923 void SetInstructionsCode(RawInstructions* instructions, RawCode* code) { | 984 void SetInstructionsCode(RawInstructions* instructions, RawCode* code) { |
| 924 return instructions_writer_->SetInstructionsCode(instructions, code); | 985 return instructions_writer_->SetInstructionsCode(instructions, code); |
| 925 } | 986 } |
| 926 | 987 |
| 927 void WriteFunctionId(RawFunction* func, bool owner_is_class); | 988 void WriteFunctionId(RawFunction* func, bool owner_is_class); |
| 928 | 989 |
| 929 RawFunction* IsSerializableClosure(RawClosure* closure); | 990 RawFunction* IsSerializableClosure(RawClosure* closure); |
| 930 | 991 |
| 931 void WriteStaticImplicitClosure(intptr_t object_id, | 992 void WriteStaticImplicitClosure(intptr_t object_id, |
| 932 RawFunction* func, | 993 RawFunction* func, |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1135 private: | 1196 private: |
| 1136 SnapshotWriter* writer_; | 1197 SnapshotWriter* writer_; |
| 1137 bool as_references_; | 1198 bool as_references_; |
| 1138 | 1199 |
| 1139 DISALLOW_COPY_AND_ASSIGN(SnapshotWriterVisitor); | 1200 DISALLOW_COPY_AND_ASSIGN(SnapshotWriterVisitor); |
| 1140 }; | 1201 }; |
| 1141 | 1202 |
| 1142 } // namespace dart | 1203 } // namespace dart |
| 1143 | 1204 |
| 1144 #endif // VM_SNAPSHOT_H_ | 1205 #endif // VM_SNAPSHOT_H_ |
| OLD | NEW |