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