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

Side by Side Diff: src/ast-value-factory.cc

Issue 335293004: New try: Parser: Delay internalizing strings and values (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: variable renaming + passing const & Created 6 years, 6 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
(Empty)
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "src/ast-value-factory.h"
29
30 #include "src/api.h"
31 #include "src/objects.h"
32
33 namespace v8 {
34 namespace internal {
35
36 namespace {
37
38 template <typename Char>
39 int vector_hash(Vector<const Char> string) {
40 int hash = 0;
41 for (int i = 0; i < string.length(); i++) {
42 int c = static_cast<int>(string[i]);
43 hash += c;
44 hash += (hash << 10);
45 hash ^= (hash >> 6);
46 }
47 return hash;
48 }
49
50
51 // For using StringToArrayIndex.
52 class OneByteStringStream {
53 public:
54 explicit OneByteStringStream(Vector<const byte> lb) :
55 literal_bytes_(lb), pos_(0) {}
56
57 bool HasMore() { return pos_ < literal_bytes_.length(); }
58 uint16_t GetNext() { return literal_bytes_[pos_++]; }
59
60 private:
61 Vector<const byte> literal_bytes_;
62 int pos_;
63 };
64
65 }
66
67
68 bool AstString::AsArrayIndex(uint32_t* index) const {
69 ASSERT(type_ == NORMAL);
70 if (!string_.is_null())
71 return string_->AsArrayIndex(index);
72 if (!is_one_byte_ || literal_bytes_.length() == 0 ||
73 literal_bytes_.length() > String::kMaxArrayIndexSize)
74 return false;
75 OneByteStringStream stream(literal_bytes_);
76 return StringToArrayIndex(&stream, index);
77 }
78
79
80 bool AstString::IsOneByteEqualTo(const char* data) const {
81 ASSERT(type_ == NORMAL);
82 int length = static_cast<int>(strlen(data));
83 if (is_one_byte_ && literal_bytes_.length() == length) {
84 const char* token = reinterpret_cast<const char*>(literal_bytes_.start());
85 return !strncmp(token, data, length);
86 }
87 return false;
88 }
89
90
91 void AstString::Internalize(Isolate* isolate) {
92 if (!string_.is_null()) return;
93 if (type_ == CONS) {
94 // Strings are internalized in creation order, so left and right are already
95 // internalized.
96 string_ = isolate->factory()
97 ->NewConsString(left_->string(), right_->string())
98 .ToHandleChecked();
99 return;
100 }
101 if (literal_bytes_.length() == 0) {
102 string_ = isolate->factory()->empty_string();
103 } else if (is_one_byte_) {
104 string_ = isolate->factory()->InternalizeOneByteString(literal_bytes_);
105 } else {
106 string_ = isolate->factory()->InternalizeTwoByteString(
107 Vector<const uint16_t>::cast(literal_bytes_));
108 }
109 }
110
111
112 bool AstString::Compare(void* a, void* b) {
113 AstString* string1 = reinterpret_cast<AstString*>(a);
114 AstString* string2 = reinterpret_cast<AstString*>(b);
115 ASSERT(string1->type_ == NORMAL);
116 ASSERT(string2->type_ == NORMAL);
117 if (string1->is_one_byte_ != string2->is_one_byte_) return false;
118 if (string1->hash_ != string2->hash_) return false;
119 int length = string1->literal_bytes_.length();
120 if (string2->literal_bytes_.length() != length) return false;
121 return memcmp(string1->literal_bytes_.start(),
122 string2->literal_bytes_.start(), length) == 0;
123 }
124
125
126 bool AstValue::IsPropertyName() const {
127 if (type_ == STRING) {
128 uint32_t index;
129 return !string_->AsArrayIndex(&index);
130 }
131 return false;
132 }
133
134
135 bool AstValue::BooleanValue() const {
136 switch (type_) {
137 case STRING:
138 ASSERT(string_ != NULL);
139 return !string_->IsEmpty();
140 case SYMBOL:
141 UNREACHABLE();
142 break;
143 case NUMBER:
144 return DoubleToBoolean(number_);
145 case SMI:
146 return smi_ != 0;
147 case STRING_ARRAY:
148 UNREACHABLE();
149 break;
150 case BOOLEAN:
151 return bool_;
152 case NULL_TYPE:
153 return false;
154 case THE_HOLE:
155 UNREACHABLE();
156 break;
157 case UNDEFINED:
158 return false;
159 }
160 UNREACHABLE();
161 return false;
162 }
163
164
165 void AstValue::Internalize(Isolate* isolate) {
166 switch (type_) {
167 case STRING:
168 ASSERT(string_ != NULL);
169 // Strings are already internalized.
170 ASSERT(!string_->string().is_null());
171 break;
172 case SYMBOL:
173 value_ = Object::GetProperty(
174 isolate, handle(isolate->native_context()->builtins()),
175 symbol_name_).ToHandleChecked();
176 break;
177 case NUMBER:
178 value_ = isolate->factory()->NewNumber(number_, TENURED);
179 break;
180 case SMI:
181 value_ = handle(Smi::FromInt(smi_), isolate);
182 break;
183 case BOOLEAN:
184 if (bool_) {
185 value_ = isolate->factory()->true_value();
186 } else {
187 value_ = isolate->factory()->false_value();
188 }
189 break;
190 case STRING_ARRAY: {
191 ASSERT(strings_ != NULL);
192 Factory* factory = isolate->factory();
193 int len = strings_->length();
194 Handle<FixedArray> elements = factory->NewFixedArray(len, TENURED);
195 for (int i = 0; i < len; i++) {
196 const AstString* string = (*strings_)[i];
197 Handle<Object> element = string->string();
198 // Strings are already internalized.
199 ASSERT(!element.is_null());
200 elements->set(i, *element);
201 }
202 value_ =
203 factory->NewJSArrayWithElements(elements, FAST_ELEMENTS, TENURED);
204 break;
205 }
206 case NULL_TYPE:
207 value_ = isolate->factory()->null_value();
208 break;
209 case THE_HOLE:
210 value_ = isolate->factory()->the_hole_value();
211 break;
212 case UNDEFINED:
213 value_ = isolate->factory()->undefined_value();
214 break;
215 }
216 }
217
218
219 const AstString* AstValueFactory::GetOneByteString(
220 const Vector<const uint8_t>& literal) {
221 return GetString(vector_hash(literal), true, literal);
222 }
223
224
225 const AstString* AstValueFactory::GetTwoByteString(
226 const Vector<const uint16_t>& literal) {
227 return GetString(vector_hash(literal), false,
228 Vector<const byte>::cast(literal));
229 }
230
231
232 const AstString* AstValueFactory::GetString(Handle<String> literal) {
233 DisallowHeapAllocation no_gc;
234 String::FlatContent content = literal->GetFlatContent();
235 if (content.IsAscii()) {
236 return GetOneByteString(content.ToOneByteVector());
237 }
238 ASSERT(content.IsTwoByte());
239 return GetTwoByteString(content.ToUC16Vector());
240 }
241
242
243 const AstString* AstValueFactory::NewConsString(const AstString* left,
244 const AstString* right) {
245 AstString key(left, right);
246 // This Vector will be valid as long as the Collector is alive (meaning that
247 // the AstString will not be moved).
248 AstString* new_string = new (zone_) AstString(left, right);
249 strings_.Add(new_string);
250 if (isolate_) {
251 new_string[0].Internalize(isolate_);
252 }
253 return new_string;
254 }
255
256
257 void AstValueFactory::Internalize(Isolate* isolate) {
258 if (isolate_) {
259 // Everything is already internalized.
260 return;
261 }
262 // Strings need to be internalized before values, because values refer to
263 // strings.
264 for (int i = 0; i < strings_.length(); ++i) {
265 strings_[i]->Internalize(isolate);
266 }
267 for (int i = 0; i < values_.length(); ++i) {
268 values_[i]->Internalize(isolate);
269 }
270 isolate_ = isolate;
271 }
272
273
274 const AstValue* AstValueFactory::NewString(const AstString* string) {
275 AstValue* value = new (zone_) AstValue(string);
276 ASSERT(string != NULL);
277 if (isolate_) {
278 value->Internalize(isolate_);
279 }
280 values_.Add(value);
281 return value;
282 }
283
284
285 const AstValue* AstValueFactory::NewSymbol(const char* name) {
286 AstValue* value = new (zone_) AstValue(name);
287 if (isolate_) {
288 value->Internalize(isolate_);
289 }
290 values_.Add(value);
291 return value;
292 }
293
294
295 const AstValue* AstValueFactory::NewNumber(double number) {
296 AstValue* value = new (zone_) AstValue(number);
297 if (isolate_) {
298 value->Internalize(isolate_);
299 }
300 values_.Add(value);
301 return value;
302 }
303
304
305 const AstValue* AstValueFactory::NewSmi(int number) {
306 AstValue* value =
307 new (zone_) AstValue(AstValue::SMI, number);
308 if (isolate_) {
309 value->Internalize(isolate_);
310 }
311 values_.Add(value);
312 return value;
313 }
314
315
316 const AstValue* AstValueFactory::NewBoolean(bool b) {
317 AstValue* value = new (zone_) AstValue(b);
318 if (isolate_) {
319 value->Internalize(isolate_);
320 }
321 values_.Add(value);
322 return value;
323 }
324
325
326 const AstValue* AstValueFactory::NewStringList(
327 ZoneList<const AstString*>* strings) {
328 AstValue* value = new (zone_) AstValue(strings);
329 if (isolate_) {
330 value->Internalize(isolate_);
331 }
332 values_.Add(value);
333 return value;
334 }
335
336
337 const AstValue* AstValueFactory::NewNull() {
338 AstValue* value = new (zone_) AstValue(AstValue::NULL_TYPE);
339 if (isolate_) {
340 value->Internalize(isolate_);
341 }
342 values_.Add(value);
343 return value;
344 }
345
346
347 const AstValue* AstValueFactory::NewUndefined() {
348 AstValue* value = new (zone_) AstValue(AstValue::UNDEFINED);
349 if (isolate_) {
350 value->Internalize(isolate_);
351 }
352 values_.Add(value);
353 return value;
354 }
355
356
357 const AstValue* AstValueFactory::NewTheHole() {
358 AstValue* value = new (zone_) AstValue(AstValue::THE_HOLE);
359 if (isolate_) {
360 value->Internalize(isolate_);
361 }
362 values_.Add(value);
363 return value;
364 }
365
366
367 const AstString* AstValueFactory::GetString(
368 int hash, bool is_one_byte, const Vector<const byte>& literal_bytes) {
369 // literal_bytes here points to whatever the user passed, and this is OK
370 // because we use vector_compare (which checks the contents) to compare
371 // against the AstStrings which are in the string_table_. We should not return
372 // this AstString.
373 AstString key(is_one_byte, literal_bytes, hash);
374 HashMap::Entry* entry = string_table_.Lookup(&key, hash, true);
375 if (entry->value == NULL) {
376 // Copy literal contents for later comparison.
377 int length = literal_bytes.length();
378 byte* new_literal_bytes = zone_->NewArray<byte>(length);
379 memcpy(new_literal_bytes, literal_bytes.start(), length);
380 AstString* new_string = new (zone_) AstString(
381 is_one_byte, Vector<const byte>(new_literal_bytes, length), hash);
382 entry->key = new_string;
383 strings_.Add(new_string);
384 if (isolate_) {
385 new_string->Internalize(isolate_);
386 }
387 entry->value = reinterpret_cast<void*>(1);
388 }
389 return reinterpret_cast<AstString*>(entry->key);
390 }
391
392
393 } } // namespace v8::internal
OLDNEW
« src/ast-value-factory.h ('K') | « src/ast-value-factory.h ('k') | src/compiler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698