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/snapshot.h" | 5 #include "vm/snapshot.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "vm/bootstrap.h" | 8 #include "vm/bootstrap.h" |
9 #include "vm/exceptions.h" | 9 #include "vm/exceptions.h" |
10 #include "vm/heap.h" | 10 #include "vm/heap.h" |
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 #undef SNAPSHOT_READ | 430 #undef SNAPSHOT_READ |
431 default: UNREACHABLE(); break; | 431 default: UNREACHABLE(); break; |
432 } | 432 } |
433 if (kind_ == Snapshot::kFull) { | 433 if (kind_ == Snapshot::kFull) { |
434 obj_.SetCreatedFromSnapshot(); | 434 obj_.SetCreatedFromSnapshot(); |
435 } | 435 } |
436 return obj_.raw(); | 436 return obj_.raw(); |
437 } | 437 } |
438 | 438 |
439 | 439 |
440 CMessageReader::CMessageReader(const uint8_t* buffer, | |
441 intptr_t length, | |
442 ReAlloc alloc) | |
443 : BaseReader(buffer, length), | |
444 alloc_(alloc), | |
445 backward_references_(kNumInitialReferences) { | |
446 // Initialize marker objects used to handle Lists. | |
447 // TODO(sjesse): Remove this when message serialization format is | |
448 // updated. | |
449 memset(&type_arguments_marker, 0, sizeof(type_arguments_marker)); | |
450 memset(&dynamic_type_marker, 0, sizeof(dynamic_type_marker)); | |
451 type_arguments_marker.type = | |
452 static_cast<Dart_CObject::Type>(Dart_CObject_Internal::kTypeArguments); | |
453 dynamic_type_marker.type = | |
454 static_cast<Dart_CObject::Type>(Dart_CObject_Internal::kDynamicType); | |
455 } | |
456 | |
457 | |
458 Dart_CObject* CMessageReader::ReadMessage() { | |
459 // Read the object out of the message. | |
460 return ReadObject(); | |
461 } | |
462 | |
463 intptr_t CMessageReader::LookupInternalClass(intptr_t class_header) { | |
464 SerializedHeaderType header_type = SerializedHeaderTag::decode(class_header); | |
465 ASSERT(header_type == kObjectId); | |
466 intptr_t header_value = SerializedHeaderData::decode(class_header); | |
467 return header_value; | |
468 } | |
469 | |
470 | |
471 Dart_CObject* CMessageReader::AllocateDartCObject(Dart_CObject::Type type) { | |
472 Dart_CObject* value = | |
473 reinterpret_cast<Dart_CObject*>( | |
474 alloc_(NULL, 0, sizeof(Dart_CObject))); | |
475 value->type = type; | |
476 return value; | |
477 } | |
478 | |
479 | |
480 Dart_CObject* CMessageReader::AllocateDartCObjectNull() { | |
481 return AllocateDartCObject(Dart_CObject::kNull); | |
482 } | |
483 | |
484 | |
485 Dart_CObject* CMessageReader::AllocateDartCObjectBool(bool val) { | |
486 Dart_CObject* value = AllocateDartCObject(Dart_CObject::kBool); | |
487 value->value.as_bool = val; | |
488 return value; | |
489 } | |
490 | |
491 | |
492 Dart_CObject* CMessageReader::AllocateDartCObjectInt32(int32_t val) { | |
493 Dart_CObject* value = AllocateDartCObject(Dart_CObject::kInt32); | |
494 value->value.as_int32 = val; | |
495 return value; | |
496 } | |
497 | |
498 | |
499 Dart_CObject* CMessageReader::AllocateDartCObjectDouble(double val) { | |
500 Dart_CObject* value = AllocateDartCObject(Dart_CObject::kDouble); | |
501 value->value.as_double = val; | |
502 return value; | |
503 } | |
504 | |
505 | |
506 Dart_CObject* CMessageReader::AllocateDartCObjectString(intptr_t length) { | |
507 // Allocate a Dart_CObject structure followed by an array of chars | |
508 // for the string content. The pointer to the string content is set | |
509 // up to this area. | |
510 Dart_CObject* value = | |
511 reinterpret_cast<Dart_CObject*>( | |
512 alloc_(NULL, 0, sizeof(Dart_CObject) + length + 1)); | |
513 value->value.as_string = reinterpret_cast<char*>(value) + sizeof(*value); | |
514 value->type = Dart_CObject::kString; | |
515 return value; | |
516 } | |
517 | |
518 | |
519 Dart_CObject* CMessageReader::AllocateDartCObjectArray(intptr_t length) { | |
520 // Allocate a Dart_CObject structure followed by an array of | |
521 // pointers to Dart_CObject structures. The pointer to the array | |
522 // content is set up to this area. | |
523 Dart_CObject* value = | |
524 reinterpret_cast<Dart_CObject*>( | |
525 alloc_(NULL, 0, sizeof(Dart_CObject) + length * sizeof(value))); | |
526 value->type = Dart_CObject::kArray; | |
527 value->value.as_array.length = length; | |
528 if (length > 0) { | |
529 value->value.as_array.values = reinterpret_cast<Dart_CObject**>(value + 1); | |
530 } else { | |
531 value->value.as_array.values = NULL; | |
532 } | |
533 return value; | |
534 } | |
535 | |
536 | |
537 Dart_CObject* CMessageReader::ReadInlinedObject(intptr_t object_id) { | |
538 // Read the class header information and lookup the class. | |
539 intptr_t class_header = ReadIntptrValue(); | |
540 intptr_t tags = ReadIntptrValue(); | |
541 USE(tags); | |
542 intptr_t class_id; | |
543 | |
544 // Reading of regular dart instances is not supported. | |
545 if (SerializedHeaderData::decode(class_header) == kInstanceId) { | |
546 return NULL; | |
547 } | |
548 | |
549 ASSERT((class_header & kSmiTagMask) != 0); | |
550 class_id = LookupInternalClass(class_header); | |
551 switch (class_id) { | |
552 case Object::kClassClass: { | |
553 return NULL; | |
554 } | |
555 case Object::kTypeArgumentsClass: { | |
556 // TODO(sjesse): Remove this when message serialization format is | |
557 // updated (currently length is leaked). | |
558 AddBackwardReference(object_id, NULL); | |
559 Dart_CObject* length = ReadObject(); | |
560 ASSERT(length->type == Dart_CObject::kInt32); | |
561 for (int i = 0; i < length->value.as_int32; i++) { | |
562 Dart_CObject* type = ReadObject(); | |
563 if (type != &dynamic_type_marker) return NULL; | |
564 } | |
565 return &type_arguments_marker; | |
566 break; | |
567 } | |
568 case ObjectStore::kArrayClass: { | |
569 intptr_t len = ReadSmiValue(); | |
570 Dart_CObject* value = AllocateDartCObjectArray(len); | |
571 AddBackwardReference(object_id, value); | |
572 // Skip type arguments. | |
573 // TODO(sjesse): Remove this when message serialization format is | |
574 // updated (currently type_arguments is leaked). | |
575 Dart_CObject* type_arguments = ReadObject(); | |
576 if (type_arguments != &type_arguments_marker && | |
577 type_arguments->type != Dart_CObject::kNull) { | |
578 return NULL; | |
579 } | |
580 for (int i = 0; i < len; i++) { | |
581 value->value.as_array.values[i] = ReadObject(); | |
582 } | |
583 return value; | |
584 break; | |
585 } | |
586 case ObjectStore::kDoubleClass: { | |
587 // Read the double value for the object. | |
588 Dart_CObject* object = AllocateDartCObjectDouble(Read<double>()); | |
589 AddBackwardReference(object_id, object); | |
590 return object; | |
591 break; | |
592 } | |
593 case ObjectStore::kOneByteStringClass: { | |
594 intptr_t len = ReadSmiValue(); | |
595 intptr_t hash = ReadSmiValue(); | |
596 USE(hash); | |
597 Dart_CObject* object = AllocateDartCObjectString(len); | |
598 AddBackwardReference(object_id, object); | |
599 char* p = object->value.as_string; | |
600 for (intptr_t i = 0; i < len; i++) { | |
601 *p = Read<uint8_t>(); | |
602 p++; | |
603 } | |
604 *p = '\0'; | |
605 return object; | |
606 break; | |
607 } | |
608 case ObjectStore::kTwoByteStringClass: | |
609 // Two byte strings not supported. | |
610 return NULL; | |
611 break; | |
612 case ObjectStore::kFourByteStringClass: | |
613 // Four byte strings not supported. | |
614 return NULL; | |
615 break; | |
616 default: | |
617 // Everything else not supported. | |
618 return NULL; | |
619 } | |
620 } | |
621 | |
622 | |
623 Dart_CObject* CMessageReader::ReadIndexedObject(intptr_t object_id) { | |
624 if (object_id == Object::kNullObject) { | |
625 return AllocateDartCObjectNull(); | |
626 } else if (object_id == ObjectStore::kTrueValue) { | |
627 return AllocateDartCObjectBool(true); | |
628 } else if (object_id == ObjectStore::kFalseValue) { | |
629 return AllocateDartCObjectBool(false); | |
630 } else if (object_id == ObjectStore::kDynamicType || | |
631 object_id == ObjectStore::kDoubleInterface || | |
632 object_id == ObjectStore::kIntInterface || | |
633 object_id == ObjectStore::kBoolInterface || | |
634 object_id == ObjectStore::kStringInterface) { | |
635 // Always return dynamic type (this is only a marker). | |
636 return &dynamic_type_marker; | |
637 } else { | |
638 intptr_t index = object_id - kMaxPredefinedObjectIds; | |
639 ASSERT(index < backward_references_.length()); | |
640 ASSERT(backward_references_[index] != NULL); | |
641 return backward_references_[index]; | |
642 } | |
643 return NULL; | |
644 } | |
645 | |
646 | |
647 Dart_CObject* CMessageReader::ReadObjectImpl(intptr_t header) { | |
648 SerializedHeaderType header_type = SerializedHeaderTag::decode(header); | |
649 intptr_t header_value = SerializedHeaderData::decode(header); | |
650 | |
651 if (header_type == kObjectId) { | |
652 return ReadIndexedObject(header_value); | |
653 } | |
654 ASSERT(header_type == kInlined); | |
655 return ReadInlinedObject(header_value); | |
656 } | |
657 | |
658 | |
659 Dart_CObject* CMessageReader::ReadObject() { | |
660 int64_t value = Read<int64_t>(); | |
661 if ((value & kSmiTagMask) == 0) { | |
662 Dart_CObject* dart_value = AllocateDartCObjectInt32(value >> kSmiTagShift); | |
663 return dart_value; | |
664 } | |
665 ASSERT((value <= kIntptrMax) && (value >= kIntptrMin)); | |
666 return ReadObjectImpl(value); | |
667 } | |
668 | |
669 | |
670 void CMessageReader::AddBackwardReference(intptr_t id, Dart_CObject* obj) { | |
671 ASSERT((id - kMaxPredefinedObjectIds) == backward_references_.length()); | |
672 backward_references_.Add(obj); | |
673 } | |
674 | |
675 | |
676 void MessageWriter::WriteMessage(intptr_t field_count, intptr_t *data) { | 440 void MessageWriter::WriteMessage(intptr_t field_count, intptr_t *data) { |
677 // Write out the serialization header value for this object. | 441 // Write out the serialization header value for this object. |
678 WriteSerializationMarker(kInlined, kMaxPredefinedObjectIds); | 442 WriteSerializationMarker(kInlined, kMaxPredefinedObjectIds); |
679 | 443 |
680 // Write out the class and tags information. | 444 // Write out the class and tags information. |
681 WriteObjectHeader(ObjectStore::kArrayClass, 0); | 445 WriteObjectHeader(ObjectStore::kArrayClass, 0); |
682 | 446 |
683 // Write out the length field. | 447 // Write out the length field. |
684 Write<RawObject*>(Smi::New(field_count)); | 448 Write<RawObject*>(Smi::New(field_count)); |
685 | 449 |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1028 | 792 |
1029 | 793 |
1030 void SnapshotWriterVisitor::VisitPointers(RawObject** first, RawObject** last) { | 794 void SnapshotWriterVisitor::VisitPointers(RawObject** first, RawObject** last) { |
1031 for (RawObject** current = first; current <= last; current++) { | 795 for (RawObject** current = first; current <= last; current++) { |
1032 RawObject* raw_obj = *current; | 796 RawObject* raw_obj = *current; |
1033 writer_->WriteObject(raw_obj); | 797 writer_->WriteObject(raw_obj); |
1034 } | 798 } |
1035 } | 799 } |
1036 | 800 |
1037 } // namespace dart | 801 } // namespace dart |
OLD | NEW |