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

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
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/object_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 4725 matching lines...) Expand 10 before | Expand all | Expand 10 after
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::New(const String& str, Heap::Space space) {
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::New(str, space));
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 return Integer::New(value, space);
9257 } 9256 }
9258 9257
9259 9258
9259 RawInteger* Integer::NewCanonical(const String& str) {
9260 // We are not supposed to have integers represented as two byte strings.
9261 ASSERT(str.IsOneByteString());
9262 int64_t value;
9263 if (!OS::StringToInt64(str.ToCString(), &value)) {
9264 const Bigint& big = Bigint::Handle(Bigint::NewCanonical(str));
9265 ASSERT(!BigintOperations::FitsIntoSmi(big));
9266 ASSERT(!BigintOperations::FitsIntoMint(big));
9267 return big.raw();
9268 }
9269 if ((value <= Smi::kMaxValue) && (value >= Smi::kMinValue)) {
9270 return Smi::New(value);
9271 }
9272 return Mint::NewCanonical(value);
9273 }
9274
9275
9260 RawInteger* Integer::New(int64_t value, Heap::Space space) { 9276 RawInteger* Integer::New(int64_t value, Heap::Space space) {
9261 if ((value <= Smi::kMaxValue) && (value >= Smi::kMinValue)) { 9277 if ((value <= Smi::kMaxValue) && (value >= Smi::kMinValue)) {
9262 return Smi::New(value); 9278 return Smi::New(value);
9263 } 9279 }
9264 return Mint::New(value, space); 9280 return Mint::New(value, space);
9265 } 9281 }
9266 9282
9267 9283
9268 double Integer::AsDoubleValue() const { 9284 double Integer::AsDoubleValue() const {
9269 UNIMPLEMENTED(); 9285 UNIMPLEMENTED();
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after
9851 // Both handles point to the same raw instance. 9867 // Both handles point to the same raw instance.
9852 return true; 9868 return true;
9853 } 9869 }
9854 9870
9855 if (!other.IsBigint() || other.IsNull()) { 9871 if (!other.IsBigint() || other.IsNull()) {
9856 return false; 9872 return false;
9857 } 9873 }
9858 9874
9859 const Bigint& other_bgi = Bigint::Cast(other); 9875 const Bigint& other_bgi = Bigint::Cast(other);
9860 9876
9877 if (this->IsNegative() != other_bgi.IsNegative()) {
9878 return false;
9879 }
9880
9861 intptr_t len = this->Length(); 9881 intptr_t len = this->Length();
9862 if (len != other_bgi.Length()) { 9882 if (len != other_bgi.Length()) {
9863 return false; 9883 return false;
9864 } 9884 }
9865 9885
9866 for (intptr_t i = 0; i < len; i++) { 9886 for (intptr_t i = 0; i < len; i++) {
9867 if (this->GetChunkAt(i) != other_bgi.GetChunkAt(i)) { 9887 if (this->GetChunkAt(i) != other_bgi.GetChunkAt(i)) {
9868 return false; 9888 return false;
9869 } 9889 }
9870 } 9890 }
9871 return true; 9891 return true;
9872 } 9892 }
9873 9893
9874 9894
9875 RawBigint* Bigint::New(const String& str, Heap::Space space) { 9895 RawBigint* Bigint::New(const String& str, Heap::Space space) {
9876 const Bigint& result = Bigint::Handle( 9896 const Bigint& result = Bigint::Handle(
9877 BigintOperations::NewFromCString(str.ToCString(), space)); 9897 BigintOperations::NewFromCString(str.ToCString(), space));
9878 ASSERT(!BigintOperations::FitsIntoMint(result)); 9898 ASSERT(!BigintOperations::FitsIntoMint(result));
9879 return result.raw(); 9899 return result.raw();
9880 } 9900 }
9881 9901
9882 9902
9903 RawBigint* Bigint::NewCanonical(const String& str) {
9904 const Bigint& value = Bigint::Handle(
9905 BigintOperations::NewFromCString(str.ToCString(), Heap::kOld));
9906 ASSERT(!BigintOperations::FitsIntoMint(value));
9907 const Class& cls =
9908 Class::Handle(Isolate::Current()->object_store()->bigint_class());
9909 const Array& constants = Array::Handle(cls.constants());
9910 const intptr_t constants_len = constants.Length();
9911 // Linear search to see whether this value is already present in the
9912 // list of canonicalized constants.
9913 Bigint& canonical_value = Bigint::Handle();
9914 intptr_t index = 0;
9915 while (index < constants_len) {
9916 canonical_value ^= constants.At(index);
9917 if (canonical_value.IsNull()) {
9918 break;
9919 }
9920 if (canonical_value.Equals(value)) {
9921 return canonical_value.raw();
9922 }
9923 index++;
9924 }
9925 // The value needs to be added to the constants list. Grow the list if
9926 // it is full.
9927 cls.InsertCanonicalConstant(index, value);
9928 value.SetCanonical();
9929 return value.raw();
9930 }
9931
9932
9883 double Bigint::AsDoubleValue() const { 9933 double Bigint::AsDoubleValue() const {
9884 return Double::Handle(BigintOperations::ToDouble(*this)).value(); 9934 return Double::Handle(BigintOperations::ToDouble(*this)).value();
9885 } 9935 }
9886 9936
9887 9937
9888 int64_t Bigint::AsInt64Value() const { 9938 int64_t Bigint::AsInt64Value() const {
9889 if (!BigintOperations::FitsIntoMint(*this)) { 9939 if (!BigintOperations::FitsIntoMint(*this)) {
9890 UNREACHABLE(); 9940 UNREACHABLE();
9891 } 9941 }
9892 return BigintOperations::ToMint(*this); 9942 return BigintOperations::ToMint(*this);
(...skipping 2301 matching lines...) Expand 10 before | Expand all | Expand 10 after
12194 } 12244 }
12195 return result.raw(); 12245 return result.raw();
12196 } 12246 }
12197 12247
12198 12248
12199 const char* WeakProperty::ToCString() const { 12249 const char* WeakProperty::ToCString() const {
12200 return "_WeakProperty"; 12250 return "_WeakProperty";
12201 } 12251 }
12202 12252
12203 } // namespace dart 12253 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/object_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698