Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: runtime/vm/object.cc

Issue 11348259: Canonicalize all integer constants taht we create by parsing a string. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/bigint_operations.h" 10 #include "vm/bigint_operations.h"
(...skipping 4479 matching lines...) Expand 10 before | Expand all | Expand 10 after
4490 Heap::kOld); 4490 Heap::kOld);
4491 return reinterpret_cast<RawLiteralToken*>(raw); 4491 return reinterpret_cast<RawLiteralToken*>(raw);
4492 } 4492 }
4493 4493
4494 4494
4495 RawLiteralToken* LiteralToken::New(Token::Kind kind, const String& literal) { 4495 RawLiteralToken* LiteralToken::New(Token::Kind kind, const String& literal) {
4496 const LiteralToken& result = LiteralToken::Handle(LiteralToken::New()); 4496 const LiteralToken& result = LiteralToken::Handle(LiteralToken::New());
4497 result.set_kind(kind); 4497 result.set_kind(kind);
4498 result.set_literal(literal); 4498 result.set_literal(literal);
4499 if (kind == Token::kINTEGER) { 4499 if (kind == Token::kINTEGER) {
4500 const Integer& value = Integer::Handle(Integer::New(literal, Heap::kOld)); 4500 const Integer& value = Integer::Handle(Integer::NewCanonical(literal));
4501 ASSERT(value.IsSmi() || value.IsOld()); 4501 ASSERT(value.IsSmi() || value.IsOld());
4502 result.set_value(value); 4502 result.set_value(value);
4503 } else if (kind == Token::kDOUBLE) { 4503 } else if (kind == Token::kDOUBLE) {
4504 const Double& value = Double::Handle(Double::NewCanonical(literal)); 4504 const Double& value = Double::Handle(Double::NewCanonical(literal));
4505 result.set_value(value); 4505 result.set_value(value);
4506 } else { 4506 } else {
4507 ASSERT(Token::NeedsLiteralToken(kind)); 4507 ASSERT(Token::NeedsLiteralToken(kind));
4508 result.set_value(literal); 4508 result.set_value(literal);
4509 } 4509 }
4510 return result.raw(); 4510 return result.raw();
(...skipping 4724 matching lines...) Expand 10 before | Expand all | Expand 10 after
9235 } 9235 }
9236 9236
9237 9237
9238 const char* Integer::ToCString() const { 9238 const char* Integer::ToCString() const {
9239 // Integer is an interface. No instances of Integer should exist. 9239 // Integer is an interface. No instances of Integer should exist.
9240 UNREACHABLE(); 9240 UNREACHABLE();
9241 return "Integer"; 9241 return "Integer";
9242 } 9242 }
9243 9243
9244 9244
9245 RawInteger* Integer::New(const String& str, Heap::Space space) { 9245 RawInteger* Integer::NewCanonical(const String& str) {
9246 // We are not supposed to have integers represented as two byte or 9246 // We are not supposed to have integers represented as two byte strings.
9247 // four byte strings.
9248 ASSERT(str.IsOneByteString()); 9247 ASSERT(str.IsOneByteString());
9249 int64_t value; 9248 int64_t value;
9250 if (!OS::StringToInt64(str.ToCString(), &value)) { 9249 if (!OS::StringToInt64(str.ToCString(), &value)) {
9251 const Bigint& big = Bigint::Handle(Bigint::New(str, space)); 9250 const Bigint& big = Bigint::Handle(Bigint::NewCanonical(str));
9252 ASSERT(!BigintOperations::FitsIntoSmi(big)); 9251 ASSERT(!BigintOperations::FitsIntoSmi(big));
9253 ASSERT(!BigintOperations::FitsIntoMint(big)); 9252 ASSERT(!BigintOperations::FitsIntoMint(big));
9254 return big.raw(); 9253 return big.raw();
9255 } 9254 }
9256 return Integer::New(value, space); 9255 if ((value <= Smi::kMaxValue) && (value >= Smi::kMinValue)) {
9256 return Smi::New(value);
9257 }
9258 return Mint::NewCanonical(value);
9257 } 9259 }
9258 9260
9259 9261
9260 RawInteger* Integer::New(int64_t value, Heap::Space space) { 9262 RawInteger* Integer::New(int64_t value, Heap::Space space) {
9261 if ((value <= Smi::kMaxValue) && (value >= Smi::kMinValue)) { 9263 if ((value <= Smi::kMaxValue) && (value >= Smi::kMinValue)) {
9262 return Smi::New(value); 9264 return Smi::New(value);
9263 } 9265 }
9264 return Mint::New(value, space); 9266 return Mint::New(value, space);
9265 } 9267 }
9266 9268
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after
9851 // Both handles point to the same raw instance. 9853 // Both handles point to the same raw instance.
9852 return true; 9854 return true;
9853 } 9855 }
9854 9856
9855 if (!other.IsBigint() || other.IsNull()) { 9857 if (!other.IsBigint() || other.IsNull()) {
9856 return false; 9858 return false;
9857 } 9859 }
9858 9860
9859 const Bigint& other_bgi = Bigint::Cast(other); 9861 const Bigint& other_bgi = Bigint::Cast(other);
9860 9862
9863 if (this->IsNegative() != other_bgi.IsNegative()) {
9864 return false;
9865 }
9866
9861 intptr_t len = this->Length(); 9867 intptr_t len = this->Length();
9862 if (len != other_bgi.Length()) { 9868 if (len != other_bgi.Length()) {
9863 return false; 9869 return false;
9864 } 9870 }
9865 9871
9866 for (intptr_t i = 0; i < len; i++) { 9872 for (intptr_t i = 0; i < len; i++) {
9867 if (this->GetChunkAt(i) != other_bgi.GetChunkAt(i)) { 9873 if (this->GetChunkAt(i) != other_bgi.GetChunkAt(i)) {
9868 return false; 9874 return false;
9869 } 9875 }
9870 } 9876 }
9871 return true; 9877 return true;
9872 } 9878 }
9873 9879
9874 9880
9875 RawBigint* Bigint::New(const String& str, Heap::Space space) { 9881 RawBigint* Bigint::NewCanonical(const String& str) {
9876 const Bigint& result = Bigint::Handle( 9882 const Bigint& value = Bigint::Handle(
9877 BigintOperations::NewFromCString(str.ToCString(), space)); 9883 BigintOperations::NewFromCString(str.ToCString(), Heap::kOld));
9878 ASSERT(!BigintOperations::FitsIntoMint(result)); 9884 ASSERT(!BigintOperations::FitsIntoMint(value));
9879 return result.raw(); 9885 const Class& cls =
9886 Class::Handle(Isolate::Current()->object_store()->bigint_class());
9887 const Array& constants = Array::Handle(cls.constants());
9888 const intptr_t constants_len = constants.Length();
9889 // Linear search to see whether this value is already present in the
9890 // list of canonicalized constants.
9891 Bigint& canonical_value = Bigint::Handle();
9892 intptr_t index = 0;
9893 while (index < constants_len) {
9894 canonical_value ^= constants.At(index);
9895 if (canonical_value.IsNull()) {
9896 break;
9897 }
9898 if (canonical_value.Equals(value)) {
9899 return canonical_value.raw();
9900 }
9901 index++;
9902 }
9903 // The value needs to be added to the constants list. Grow the list if
9904 // it is full.
9905 cls.InsertCanonicalConstant(index, value);
9906 value.SetCanonical();
9907 return value.raw();
9880 } 9908 }
9881 9909
9882 9910
9883 double Bigint::AsDoubleValue() const { 9911 double Bigint::AsDoubleValue() const {
9884 return Double::Handle(BigintOperations::ToDouble(*this)).value(); 9912 return Double::Handle(BigintOperations::ToDouble(*this)).value();
9885 } 9913 }
9886 9914
9887 9915
9888 int64_t Bigint::AsInt64Value() const { 9916 int64_t Bigint::AsInt64Value() const {
9889 if (!BigintOperations::FitsIntoMint(*this)) { 9917 if (!BigintOperations::FitsIntoMint(*this)) {
(...skipping 2304 matching lines...) Expand 10 before | Expand all | Expand 10 after
12194 } 12222 }
12195 return result.raw(); 12223 return result.raw();
12196 } 12224 }
12197 12225
12198 12226
12199 const char* WeakProperty::ToCString() const { 12227 const char* WeakProperty::ToCString() const {
12200 return "_WeakProperty"; 12228 return "_WeakProperty";
12201 } 12229 }
12202 12230
12203 } // namespace dart 12231 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698