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_OBJECT_H_ | 5 #ifndef VM_OBJECT_H_ |
6 #define VM_OBJECT_H_ | 6 #define VM_OBJECT_H_ |
7 | 7 |
8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 #include "platform/utils.h" | 10 #include "platform/utils.h" |
(...skipping 2593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2604 private: | 2604 private: |
2605 void set_value(int64_t value) const; | 2605 void set_value(int64_t value) const; |
2606 | 2606 |
2607 HEAP_OBJECT_IMPLEMENTATION(Mint, Integer); | 2607 HEAP_OBJECT_IMPLEMENTATION(Mint, Integer); |
2608 friend class Class; | 2608 friend class Class; |
2609 }; | 2609 }; |
2610 | 2610 |
2611 | 2611 |
2612 class Bigint : public Integer { | 2612 class Bigint : public Integer { |
2613 public: | 2613 public: |
2614 virtual bool IsZero() const; | 2614 virtual bool IsZero() const { return raw_ptr()->signed_length_ == 0; } |
2615 virtual bool IsNegative() const; | 2615 virtual bool IsNegative() const { return raw_ptr()->signed_length_ < 0; } |
2616 | 2616 |
2617 virtual bool Equals(const Instance& other) const; | 2617 virtual bool Equals(const Instance& other) const; |
2618 | 2618 |
2619 virtual double AsDoubleValue() const; | 2619 virtual double AsDoubleValue() const; |
2620 virtual int64_t AsInt64Value() const; | 2620 virtual int64_t AsInt64Value() const; |
2621 | 2621 |
2622 virtual int CompareWith(const Integer& other) const; | 2622 virtual int CompareWith(const Integer& other) const; |
2623 | 2623 |
2624 static intptr_t InstanceSize(const BIGNUM* bn) { | 2624 static intptr_t InstanceSize(intptr_t length) { |
2625 // Danger Will Robinson! Use of OpenSSL internals! | 2625 ASSERT(length >= 0); |
2626 return RoundedAllocationSize(sizeof(RawBigint) | 2626 return RoundedAllocationSize(sizeof(RawBigint) + (length * sizeof(Chunk))); |
2627 + sizeof(BN_ULONG) * bn->top); | |
2628 } | 2627 } |
2629 static intptr_t InstanceSize() { | 2628 static intptr_t InstanceSize() { return 0; } |
2630 ASSERT(sizeof(RawBigint) == OFFSET_OF(RawBigint, data_)); | |
2631 return 0; | |
2632 } | |
2633 | |
2634 static RawBigint* New(const BIGNUM* bn, Heap::Space space = Heap::kNew); | |
2635 | 2629 |
2636 static RawBigint* New(const String& str, Heap::Space space = Heap::kNew); | 2630 static RawBigint* New(const String& str, Heap::Space space = Heap::kNew); |
2637 static RawBigint* New(int64_t value, Heap::Space space = Heap::kNew); | 2631 static RawBigint* New(int64_t value, Heap::Space space = Heap::kNew); |
2638 | 2632 |
2639 private: | 2633 private: |
2640 void SetSign(bool is_negative) const; | 2634 typedef uint32_t Chunk; |
| 2635 typedef uint64_t DoubleChunk; |
| 2636 static const int kChunkSize = sizeof(Chunk); |
| 2637 |
| 2638 Chunk GetChunkAt(intptr_t i) const { |
| 2639 return *ChunkAddr(i); |
| 2640 } |
| 2641 |
| 2642 void SetChunkAt(intptr_t i, Chunk newValue) const { |
| 2643 *ChunkAddr(i) = newValue; |
| 2644 } |
| 2645 |
| 2646 intptr_t Length() const { |
| 2647 intptr_t signed_length = raw_ptr()->signed_length_; |
| 2648 return Utils::Abs(signed_length); |
| 2649 } |
| 2650 |
| 2651 // SetLength does not change the sign. |
| 2652 void SetLength(intptr_t length) const { |
| 2653 ASSERT(length >= 0); |
| 2654 bool is_negative = IsNegative(); |
| 2655 raw_ptr()->signed_length_ = length; |
| 2656 if (is_negative) ToggleSign(); |
| 2657 } |
| 2658 |
| 2659 void SetSign(bool is_negative) const { |
| 2660 if (is_negative != IsNegative()) { |
| 2661 ToggleSign(); |
| 2662 } |
| 2663 } |
2641 | 2664 |
2642 void ToggleSign() const { | 2665 void ToggleSign() const { |
2643 BIGNUM* bn = MutableBNAddr(); | 2666 raw_ptr()->signed_length_ = -raw_ptr()->signed_length_; |
2644 // Danger Will Robinson! Use of OpenSSL internals! | |
2645 // FIXME(benl): can be changed to use BN_set_negative() on more | |
2646 // recent OpenSSL releases (> 1.0.0). | |
2647 SetSign(!bn->neg); | |
2648 } | 2667 } |
2649 | 2668 |
2650 BIGNUM* MutableBNAddr() const { | 2669 Chunk* ChunkAddr(intptr_t index) const { |
2651 // Fix up internals as we may have been moved. | 2670 ASSERT(0 <= index); |
2652 raw_ptr()->bn_.d = BNMemory(); | 2671 ASSERT(index < Length()); |
2653 | 2672 uword digits_start = reinterpret_cast<uword>(raw_ptr()) + sizeof(RawBigint); |
2654 return &raw_ptr()->bn_; | 2673 return &(reinterpret_cast<Chunk*>(digits_start)[index]); |
2655 } | |
2656 const BIGNUM* BNAddr() const { return MutableBNAddr(); } | |
2657 BN_ULONG* BNMemory() const { | |
2658 return &raw_ptr()->data_[0]; | |
2659 } | 2674 } |
2660 | 2675 |
2661 int NumberOfBits() const { return BN_num_bits(BNAddr()); } | 2676 static RawBigint* Allocate(intptr_t length, Heap::Space space = Heap::kNew); |
2662 bool IsBitSet(intptr_t bit) const { return Bit(bit) == 1; } | |
2663 int Bit(intptr_t bit) const { return BN_is_bit_set(BNAddr(), bit); } | |
2664 | 2677 |
2665 HEAP_OBJECT_IMPLEMENTATION(Bigint, Integer); | 2678 HEAP_OBJECT_IMPLEMENTATION(Bigint, Integer); |
2666 friend class Class; | 2679 friend class Class; |
2667 friend class BigintOperations; | 2680 friend class BigintOperations; |
2668 }; | 2681 }; |
2669 | 2682 |
2670 | 2683 |
2671 class Double : public Number { | 2684 class Double : public Number { |
2672 public: | 2685 public: |
2673 double value() const { | 2686 double value() const { |
(...skipping 1068 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3742 } | 3755 } |
3743 | 3756 |
3744 | 3757 |
3745 void Context::SetAt(intptr_t index, const Instance& value) const { | 3758 void Context::SetAt(intptr_t index, const Instance& value) const { |
3746 StorePointer(InstanceAddr(index), value.raw()); | 3759 StorePointer(InstanceAddr(index), value.raw()); |
3747 } | 3760 } |
3748 | 3761 |
3749 } // namespace dart | 3762 } // namespace dart |
3750 | 3763 |
3751 #endif // VM_OBJECT_H_ | 3764 #endif // VM_OBJECT_H_ |
OLD | NEW |