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

Side by Side Diff: src/objects-inl.h

Issue 7477045: Tentative implementation of string slices (hidden under the flag --string-slices). (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Implemented suggested changes. Created 9 years, 4 months 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 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 // Because the symbol tag is non-zero and no non-string types have the 170 // Because the symbol tag is non-zero and no non-string types have the
171 // symbol bit set we can test for symbols with a very simple test 171 // symbol bit set we can test for symbols with a very simple test
172 // operation. 172 // operation.
173 ASSERT(kSymbolTag != 0); 173 ASSERT(kSymbolTag != 0);
174 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); 174 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
175 return (type & kIsSymbolMask) != 0; 175 return (type & kIsSymbolMask) != 0;
176 } 176 }
177 177
178 178
179 bool Object::IsConsString() { 179 bool Object::IsConsString() {
180 if (!this->IsHeapObject()) return false; 180 if (!IsString()) return false;
181 uint32_t type = HeapObject::cast(this)->map()->instance_type(); 181 return StringShape(String::cast(this)).IsCons();
182 return (type & (kIsNotStringMask | kStringRepresentationMask)) ==
183 (kStringTag | kConsStringTag);
184 } 182 }
185 183
186 184
185 bool Object::IsSlicedString() {
186 if (!IsString()) return false;
187 return StringShape(String::cast(this)).IsSliced();
188 }
189
190
187 bool Object::IsSeqString() { 191 bool Object::IsSeqString() {
188 if (!IsString()) return false; 192 if (!IsString()) return false;
189 return StringShape(String::cast(this)).IsSequential(); 193 return StringShape(String::cast(this)).IsSequential();
190 } 194 }
191 195
192 196
193 bool Object::IsSeqAsciiString() { 197 bool Object::IsSeqAsciiString() {
194 if (!IsString()) return false; 198 if (!IsString()) return false;
195 return StringShape(String::cast(this)).IsSequential() && 199 return StringShape(String::cast(this)).IsSequential() &&
196 String::cast(this)->IsAsciiRepresentation(); 200 String::cast(this)->IsAsciiRepresentation();
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 return (type & kStringEncodingMask) == kAsciiStringTag; 265 return (type & kStringEncodingMask) == kAsciiStringTag;
262 } 266 }
263 267
264 268
265 bool String::IsTwoByteRepresentation() { 269 bool String::IsTwoByteRepresentation() {
266 uint32_t type = map()->instance_type(); 270 uint32_t type = map()->instance_type();
267 return (type & kStringEncodingMask) == kTwoByteStringTag; 271 return (type & kStringEncodingMask) == kTwoByteStringTag;
268 } 272 }
269 273
270 274
275 bool String::IsAsciiRepresentationUnderneath() {
276 uint32_t type = map()->instance_type();
277 STATIC_ASSERT(kIsIndirectStringTag != 0);
278 STATIC_ASSERT(kIsIndirectStringMask != kStringEncodingMask);
Vitaly Repeshko 2011/08/19 16:27:40 Maybe (kIsIndirectStringMask & kStringEncodingMask
279 ASSERT(IsFlat());
280 switch (type & (kIsIndirectStringMask | kStringEncodingMask)) {
281 case kAsciiStringTag:
282 return true;
283 case kTwoByteStringTag:
284 return false;
285 default: // Cons or sliced string. Need to go deeper.
286 return GetUnderlying()->IsAsciiRepresentation();
287 }
288 }
289
290
291 bool String::IsTwoByteRepresentationUnderneath() {
292 uint32_t type = map()->instance_type();
293 STATIC_ASSERT(kIsIndirectStringTag != 0);
294 STATIC_ASSERT(kIsIndirectStringMask != kStringEncodingMask);
Vitaly Repeshko 2011/08/19 16:27:40 Ditto.
295 ASSERT(IsFlat());
296 switch (type & (kIsIndirectStringMask | kStringEncodingMask)) {
297 case kAsciiStringTag:
298 return false;
299 case kTwoByteStringTag:
300 return true;
301 default: // Cons or sliced string. Need to go deeper.
302 return GetUnderlying()->IsTwoByteRepresentation();
303 }
304 }
305
306
271 bool String::HasOnlyAsciiChars() { 307 bool String::HasOnlyAsciiChars() {
272 uint32_t type = map()->instance_type(); 308 uint32_t type = map()->instance_type();
273 return (type & kStringEncodingMask) == kAsciiStringTag || 309 return (type & kStringEncodingMask) == kAsciiStringTag ||
274 (type & kAsciiDataHintMask) == kAsciiDataHintTag; 310 (type & kAsciiDataHintMask) == kAsciiDataHintTag;
275 } 311 }
276 312
277 313
278 bool StringShape::IsCons() { 314 bool StringShape::IsCons() {
279 return (type_ & kStringRepresentationMask) == kConsStringTag; 315 return (type_ & kStringRepresentationMask) == kConsStringTag;
280 } 316 }
281 317
282 318
319 bool StringShape::IsSliced() {
320 return (type_ & kStringRepresentationMask) == kSlicedStringTag;
321 }
322
323
324 bool StringShape::IsIndirect() {
325 return (type_ & kIsIndirectStringMask) == kIsIndirectStringTag;
326 }
327
328
283 bool StringShape::IsExternal() { 329 bool StringShape::IsExternal() {
284 return (type_ & kStringRepresentationMask) == kExternalStringTag; 330 return (type_ & kStringRepresentationMask) == kExternalStringTag;
285 } 331 }
286 332
287 333
288 bool StringShape::IsSequential() { 334 bool StringShape::IsSequential() {
289 return (type_ & kStringRepresentationMask) == kSeqStringTag; 335 return (type_ & kStringRepresentationMask) == kSeqStringTag;
290 } 336 }
291 337
292 338
(...skipping 1741 matching lines...) Expand 10 before | Expand all | Expand 10 after
2034 CAST_ACCESSOR(JSFunctionResultCache) 2080 CAST_ACCESSOR(JSFunctionResultCache)
2035 CAST_ACCESSOR(NormalizedMapCache) 2081 CAST_ACCESSOR(NormalizedMapCache)
2036 CAST_ACCESSOR(CompilationCacheTable) 2082 CAST_ACCESSOR(CompilationCacheTable)
2037 CAST_ACCESSOR(CodeCacheHashTable) 2083 CAST_ACCESSOR(CodeCacheHashTable)
2038 CAST_ACCESSOR(PolymorphicCodeCacheHashTable) 2084 CAST_ACCESSOR(PolymorphicCodeCacheHashTable)
2039 CAST_ACCESSOR(MapCache) 2085 CAST_ACCESSOR(MapCache)
2040 CAST_ACCESSOR(String) 2086 CAST_ACCESSOR(String)
2041 CAST_ACCESSOR(SeqString) 2087 CAST_ACCESSOR(SeqString)
2042 CAST_ACCESSOR(SeqAsciiString) 2088 CAST_ACCESSOR(SeqAsciiString)
2043 CAST_ACCESSOR(SeqTwoByteString) 2089 CAST_ACCESSOR(SeqTwoByteString)
2090 CAST_ACCESSOR(SlicedString)
2044 CAST_ACCESSOR(ConsString) 2091 CAST_ACCESSOR(ConsString)
2045 CAST_ACCESSOR(ExternalString) 2092 CAST_ACCESSOR(ExternalString)
2046 CAST_ACCESSOR(ExternalAsciiString) 2093 CAST_ACCESSOR(ExternalAsciiString)
2047 CAST_ACCESSOR(ExternalTwoByteString) 2094 CAST_ACCESSOR(ExternalTwoByteString)
2048 CAST_ACCESSOR(JSReceiver) 2095 CAST_ACCESSOR(JSReceiver)
2049 CAST_ACCESSOR(JSObject) 2096 CAST_ACCESSOR(JSObject)
2050 CAST_ACCESSOR(Smi) 2097 CAST_ACCESSOR(Smi)
2051 CAST_ACCESSOR(HeapObject) 2098 CAST_ACCESSOR(HeapObject)
2052 CAST_ACCESSOR(HeapNumber) 2099 CAST_ACCESSOR(HeapNumber)
2053 CAST_ACCESSOR(Oddball) 2100 CAST_ACCESSOR(Oddball)
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
2120 if (StringShape(this).IsSymbol() && StringShape(other).IsSymbol()) { 2167 if (StringShape(this).IsSymbol() && StringShape(other).IsSymbol()) {
2121 return false; 2168 return false;
2122 } 2169 }
2123 return SlowEquals(other); 2170 return SlowEquals(other);
2124 } 2171 }
2125 2172
2126 2173
2127 MaybeObject* String::TryFlatten(PretenureFlag pretenure) { 2174 MaybeObject* String::TryFlatten(PretenureFlag pretenure) {
2128 if (!StringShape(this).IsCons()) return this; 2175 if (!StringShape(this).IsCons()) return this;
2129 ConsString* cons = ConsString::cast(this); 2176 ConsString* cons = ConsString::cast(this);
2130 if (cons->second()->length() == 0) return cons->first(); 2177 if (cons->IsFlat()) return cons->first();
2131 return SlowTryFlatten(pretenure); 2178 return SlowTryFlatten(pretenure);
2132 } 2179 }
2133 2180
2134 2181
2135 String* String::TryFlattenGetString(PretenureFlag pretenure) { 2182 String* String::TryFlattenGetString(PretenureFlag pretenure) {
2136 MaybeObject* flat = TryFlatten(pretenure); 2183 MaybeObject* flat = TryFlatten(pretenure);
2137 Object* successfully_flattened; 2184 Object* successfully_flattened;
2138 if (flat->ToObject(&successfully_flattened)) { 2185 if (!flat->ToObject(&successfully_flattened)) return this;
2139 return String::cast(successfully_flattened); 2186 return String::cast(successfully_flattened);
2140 }
2141 return this;
2142 } 2187 }
2143 2188
2144 2189
2145 uint16_t String::Get(int index) { 2190 uint16_t String::Get(int index) {
2146 ASSERT(index >= 0 && index < length()); 2191 ASSERT(index >= 0 && index < length());
2147 switch (StringShape(this).full_representation_tag()) { 2192 switch (StringShape(this).full_representation_tag()) {
2148 case kSeqStringTag | kAsciiStringTag: 2193 case kSeqStringTag | kAsciiStringTag:
2149 return SeqAsciiString::cast(this)->SeqAsciiStringGet(index); 2194 return SeqAsciiString::cast(this)->SeqAsciiStringGet(index);
2150 case kSeqStringTag | kTwoByteStringTag: 2195 case kSeqStringTag | kTwoByteStringTag:
2151 return SeqTwoByteString::cast(this)->SeqTwoByteStringGet(index); 2196 return SeqTwoByteString::cast(this)->SeqTwoByteStringGet(index);
2152 case kConsStringTag | kAsciiStringTag: 2197 case kConsStringTag | kAsciiStringTag:
2153 case kConsStringTag | kTwoByteStringTag: 2198 case kConsStringTag | kTwoByteStringTag:
2154 return ConsString::cast(this)->ConsStringGet(index); 2199 return ConsString::cast(this)->ConsStringGet(index);
2155 case kExternalStringTag | kAsciiStringTag: 2200 case kExternalStringTag | kAsciiStringTag:
2156 return ExternalAsciiString::cast(this)->ExternalAsciiStringGet(index); 2201 return ExternalAsciiString::cast(this)->ExternalAsciiStringGet(index);
2157 case kExternalStringTag | kTwoByteStringTag: 2202 case kExternalStringTag | kTwoByteStringTag:
2158 return ExternalTwoByteString::cast(this)->ExternalTwoByteStringGet(index); 2203 return ExternalTwoByteString::cast(this)->ExternalTwoByteStringGet(index);
2204 case kSlicedStringTag | kAsciiStringTag:
2205 case kSlicedStringTag | kTwoByteStringTag:
2206 return SlicedString::cast(this)->SlicedStringGet(index);
2159 default: 2207 default:
2160 break; 2208 break;
2161 } 2209 }
2162 2210
2163 UNREACHABLE(); 2211 UNREACHABLE();
2164 return 0; 2212 return 0;
2165 } 2213 }
2166 2214
2167 2215
2168 void String::Set(int index, uint16_t value) { 2216 void String::Set(int index, uint16_t value) {
2169 ASSERT(index >= 0 && index < length()); 2217 ASSERT(index >= 0 && index < length());
2170 ASSERT(StringShape(this).IsSequential()); 2218 ASSERT(StringShape(this).IsSequential());
2171 2219
2172 return this->IsAsciiRepresentation() 2220 return this->IsAsciiRepresentation()
2173 ? SeqAsciiString::cast(this)->SeqAsciiStringSet(index, value) 2221 ? SeqAsciiString::cast(this)->SeqAsciiStringSet(index, value)
2174 : SeqTwoByteString::cast(this)->SeqTwoByteStringSet(index, value); 2222 : SeqTwoByteString::cast(this)->SeqTwoByteStringSet(index, value);
2175 } 2223 }
2176 2224
2177 2225
2178 bool String::IsFlat() { 2226 bool String::IsFlat() {
2179 switch (StringShape(this).representation_tag()) { 2227 if (!StringShape(this).IsCons()) return true;
2180 case kConsStringTag: { 2228 return ConsString::cast(this)->second()->length() == 0;
2181 String* second = ConsString::cast(this)->second();
2182 // Only flattened strings have second part empty.
2183 return second->length() == 0;
2184 }
2185 default:
2186 return true;
2187 }
2188 } 2229 }
2189 2230
2190 2231
2232 String* String::GetUnderlying() {
2233 // Giving direct access to underlying string only makes sense if the
2234 // wrapping string is already flattened.
2235 ASSERT(this->IsFlat());
2236 ASSERT(StringShape(this).IsIndirect());
2237 STATIC_ASSERT(ConsString::kFirstOffset == SlicedString::kParentOffset);
2238 const int kUnderlyingOffset = SlicedString::kParentOffset;
2239 return String::cast(READ_FIELD(this, kUnderlyingOffset));
2240 }
2241
2242
2191 uint16_t SeqAsciiString::SeqAsciiStringGet(int index) { 2243 uint16_t SeqAsciiString::SeqAsciiStringGet(int index) {
2192 ASSERT(index >= 0 && index < length()); 2244 ASSERT(index >= 0 && index < length());
2193 return READ_BYTE_FIELD(this, kHeaderSize + index * kCharSize); 2245 return READ_BYTE_FIELD(this, kHeaderSize + index * kCharSize);
2194 } 2246 }
2195 2247
2196 2248
2197 void SeqAsciiString::SeqAsciiStringSet(int index, uint16_t value) { 2249 void SeqAsciiString::SeqAsciiStringSet(int index, uint16_t value) {
2198 ASSERT(index >= 0 && index < length() && value <= kMaxAsciiCharCode); 2250 ASSERT(index >= 0 && index < length() && value <= kMaxAsciiCharCode);
2199 WRITE_BYTE_FIELD(this, kHeaderSize + index * kCharSize, 2251 WRITE_BYTE_FIELD(this, kHeaderSize + index * kCharSize,
2200 static_cast<byte>(value)); 2252 static_cast<byte>(value));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2236 int SeqTwoByteString::SeqTwoByteStringSize(InstanceType instance_type) { 2288 int SeqTwoByteString::SeqTwoByteStringSize(InstanceType instance_type) {
2237 return SizeFor(length()); 2289 return SizeFor(length());
2238 } 2290 }
2239 2291
2240 2292
2241 int SeqAsciiString::SeqAsciiStringSize(InstanceType instance_type) { 2293 int SeqAsciiString::SeqAsciiStringSize(InstanceType instance_type) {
2242 return SizeFor(length()); 2294 return SizeFor(length());
2243 } 2295 }
2244 2296
2245 2297
2298 String* SlicedString::parent() {
2299 return String::cast(READ_FIELD(this, kParentOffset));
2300 }
2301
2302
2303 void SlicedString::set_parent(String* parent) {
2304 ASSERT(parent->IsSeqString());
2305 WRITE_FIELD(this, kParentOffset, parent);
2306 }
2307
2308
2309 SMI_ACCESSORS(SlicedString, offset, kOffsetOffset)
2310
2311
2246 String* ConsString::first() { 2312 String* ConsString::first() {
2247 return String::cast(READ_FIELD(this, kFirstOffset)); 2313 return String::cast(READ_FIELD(this, kFirstOffset));
2248 } 2314 }
2249 2315
2250 2316
2251 Object* ConsString::unchecked_first() { 2317 Object* ConsString::unchecked_first() {
2252 return READ_FIELD(this, kFirstOffset); 2318 return READ_FIELD(this, kFirstOffset);
2253 } 2319 }
2254 2320
2255 2321
(...skipping 2255 matching lines...) Expand 10 before | Expand all | Expand 10 after
4511 #undef WRITE_INT_FIELD 4577 #undef WRITE_INT_FIELD
4512 #undef READ_SHORT_FIELD 4578 #undef READ_SHORT_FIELD
4513 #undef WRITE_SHORT_FIELD 4579 #undef WRITE_SHORT_FIELD
4514 #undef READ_BYTE_FIELD 4580 #undef READ_BYTE_FIELD
4515 #undef WRITE_BYTE_FIELD 4581 #undef WRITE_BYTE_FIELD
4516 4582
4517 4583
4518 } } // namespace v8::internal 4584 } } // namespace v8::internal
4519 4585
4520 #endif // V8_OBJECTS_INL_H_ 4586 #endif // V8_OBJECTS_INL_H_
OLDNEW
« src/objects.cc ('K') | « src/objects-debug.cc ('k') | src/objects-visiting.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698