OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 25 matching lines...) Expand all Loading... |
36 // independent of the V8 heap and internalizing them later. During parsing, | 36 // independent of the V8 heap and internalizing them later. During parsing, |
37 // AstStrings and AstValues are created and stored outside the heap, in | 37 // AstStrings and AstValues are created and stored outside the heap, in |
38 // AstValueFactory. After parsing, the strings and values are internalized | 38 // AstValueFactory. After parsing, the strings and values are internalized |
39 // (moved into the V8 heap). | 39 // (moved into the V8 heap). |
40 namespace v8 { | 40 namespace v8 { |
41 namespace internal { | 41 namespace internal { |
42 | 42 |
43 class AstString : public ZoneObject { | 43 class AstString : public ZoneObject { |
44 public: | 44 public: |
45 explicit AstString(bool is_raw) | 45 explicit AstString(bool is_raw) |
46 : bit_field_(IsRawStringBits::encode(is_raw)) {} | 46 : next_(nullptr), bit_field_(IsRawStringBits::encode(is_raw)) {} |
47 | |
48 ~AstString() {} | |
49 | 47 |
50 int length() const; | 48 int length() const; |
51 bool IsEmpty() const { return length() == 0; } | 49 bool IsEmpty() const { return length() == 0; } |
52 | 50 |
53 // Puts the string into the V8 heap. | 51 // Puts the string into the V8 heap. |
54 void Internalize(Isolate* isolate); | 52 void Internalize(Isolate* isolate); |
55 | 53 |
56 // This function can be called after internalizing. | 54 // This function can be called after internalizing. |
57 V8_INLINE Handle<String> string() const { | 55 V8_INLINE Handle<String> string() const { |
58 DCHECK(!string_.is_null()); | 56 DCHECK(!string_.is_null()); |
59 return string_; | 57 return string_; |
60 } | 58 } |
61 | 59 |
| 60 AstString** next_location() { return &next_; } |
| 61 AstString* next() const { return next_; } |
| 62 |
62 protected: | 63 protected: |
63 // This is null until the string is internalized. | 64 // This is a pointer to the next uninternalized string until internalized. |
64 Handle<String> string_; | 65 union { |
| 66 Handle<String> string_; |
| 67 AstString* next_; |
| 68 }; |
65 // Poor-man's virtual dispatch to AstRawString / AstConsString. Takes less | 69 // Poor-man's virtual dispatch to AstRawString / AstConsString. Takes less |
66 // memory. | 70 // memory. |
67 class IsRawStringBits : public BitField<bool, 0, 1> {}; | 71 class IsRawStringBits : public BitField<bool, 0, 1> {}; |
68 int bit_field_; | 72 int bit_field_; |
69 }; | 73 }; |
70 | 74 |
71 | 75 |
72 class AstRawString final : public AstString { | 76 class AstRawString final : public AstString { |
73 public: | 77 public: |
74 int length() const { | 78 int length() const { |
75 if (is_one_byte()) return literal_bytes_.length(); | 79 if (is_one_byte()) return literal_bytes_.length(); |
76 return literal_bytes_.length() / 2; | 80 return literal_bytes_.length() / 2; |
77 } | 81 } |
78 | 82 |
79 int byte_length() const { return literal_bytes_.length(); } | 83 int byte_length() const { return literal_bytes_.length(); } |
80 | 84 |
81 void Internalize(Isolate* isolate); | 85 void Internalize(Isolate* isolate); |
82 | 86 |
83 bool AsArrayIndex(uint32_t* index, HandleDereferenceMode deref_mode = | 87 bool AsArrayIndex(uint32_t* index) const; |
84 HandleDereferenceMode::kAllowed) const; | |
85 | 88 |
86 // The string is not null-terminated, use length() to find out the length. | 89 // The string is not null-terminated, use length() to find out the length. |
87 const unsigned char* raw_data() const { | 90 const unsigned char* raw_data() const { |
88 return literal_bytes_.start(); | 91 return literal_bytes_.start(); |
89 } | 92 } |
90 | 93 |
91 bool is_one_byte() const { return IsOneByteBits::decode(bit_field_); } | 94 bool is_one_byte() const { return IsOneByteBits::decode(bit_field_); } |
92 | 95 |
93 bool IsOneByteEqualTo(const char* data) const; | 96 bool IsOneByteEqualTo(const char* data) const; |
94 uint16_t FirstCharacter() const { | 97 uint16_t FirstCharacter() const { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 177 |
175 Smi* AsSmi() const { | 178 Smi* AsSmi() const { |
176 CHECK_EQ(SMI, type_); | 179 CHECK_EQ(SMI, type_); |
177 return Smi::FromInt(smi_); | 180 return Smi::FromInt(smi_); |
178 } | 181 } |
179 | 182 |
180 bool EqualsString(const AstRawString* string) const { | 183 bool EqualsString(const AstRawString* string) const { |
181 return type_ == STRING && string_ == string; | 184 return type_ == STRING && string_ == string; |
182 } | 185 } |
183 | 186 |
184 bool IsPropertyName( | 187 bool IsPropertyName() const; |
185 HandleDereferenceMode deref_mode = HandleDereferenceMode::kAllowed) const; | |
186 | 188 |
187 bool BooleanValue() const; | 189 bool BooleanValue() const; |
188 | 190 |
189 bool IsSmi() const { return type_ == SMI; } | 191 bool IsSmi() const { return type_ == SMI; } |
190 bool IsFalse() const { return type_ == BOOLEAN && !bool_; } | 192 bool IsFalse() const { return type_ == BOOLEAN && !bool_; } |
191 bool IsTrue() const { return type_ == BOOLEAN && bool_; } | 193 bool IsTrue() const { return type_ == BOOLEAN && bool_; } |
192 bool IsUndefined() const { return type_ == UNDEFINED; } | 194 bool IsUndefined() const { return type_ == UNDEFINED; } |
193 bool IsTheHole() const { return type_ == THE_HOLE; } | 195 bool IsTheHole() const { return type_ == THE_HOLE; } |
194 bool IsNull() const { return type_ == NULL_TYPE; } | 196 bool IsNull() const { return type_ == NULL_TYPE; } |
195 | 197 |
196 void Internalize(Isolate* isolate); | 198 void Internalize(Isolate* isolate); |
197 | 199 |
198 // Can be called after Internalize has been called. | 200 // Can be called after Internalize has been called. |
199 V8_INLINE Handle<Object> value() const { | 201 V8_INLINE Handle<Object> value() const { |
200 if (type_ == STRING) { | 202 if (type_ == STRING) { |
201 return string_->string(); | 203 return string_->string(); |
202 } | 204 } |
203 DCHECK(!value_.is_null()); | 205 DCHECK(!value_.is_null()); |
204 return value_; | 206 return value_; |
205 } | 207 } |
| 208 AstValue* next() const { return next_; } |
| 209 void set_next(AstValue* next) { next_ = next; } |
206 | 210 |
207 private: | 211 private: |
208 friend class AstValueFactory; | 212 friend class AstValueFactory; |
209 | 213 |
210 enum Type { | 214 enum Type { |
211 STRING, | 215 STRING, |
212 SYMBOL, | 216 SYMBOL, |
213 NUMBER, | 217 NUMBER, |
214 NUMBER_WITH_DOT, | 218 NUMBER_WITH_DOT, |
215 SMI, | 219 SMI, |
216 BOOLEAN, | 220 BOOLEAN, |
217 NULL_TYPE, | 221 NULL_TYPE, |
218 UNDEFINED, | 222 UNDEFINED, |
219 THE_HOLE | 223 THE_HOLE |
220 }; | 224 }; |
221 | 225 |
222 explicit AstValue(const AstRawString* s) : type_(STRING) { string_ = s; } | 226 explicit AstValue(const AstRawString* s) : type_(STRING), next_(nullptr) { |
| 227 string_ = s; |
| 228 } |
223 | 229 |
224 explicit AstValue(const char* name) : type_(SYMBOL) { symbol_name_ = name; } | 230 explicit AstValue(const char* name) : type_(SYMBOL), next_(nullptr) { |
| 231 symbol_name_ = name; |
| 232 } |
225 | 233 |
226 explicit AstValue(double n, bool with_dot) { | 234 explicit AstValue(double n, bool with_dot) : next_(nullptr) { |
227 if (with_dot) { | 235 if (with_dot) { |
228 type_ = NUMBER_WITH_DOT; | 236 type_ = NUMBER_WITH_DOT; |
229 number_ = n; | 237 number_ = n; |
230 } else { | 238 } else { |
231 int int_value; | 239 int int_value; |
232 if (DoubleToSmiInteger(n, &int_value)) { | 240 if (DoubleToSmiInteger(n, &int_value)) { |
233 type_ = SMI; | 241 type_ = SMI; |
234 smi_ = int_value; | 242 smi_ = int_value; |
235 } else { | 243 } else { |
236 type_ = NUMBER; | 244 type_ = NUMBER; |
237 number_ = n; | 245 number_ = n; |
238 } | 246 } |
239 } | 247 } |
240 } | 248 } |
241 | 249 |
242 AstValue(Type t, int i) : type_(t) { | 250 AstValue(Type t, int i) : type_(t), next_(nullptr) { |
243 DCHECK(type_ == SMI); | 251 DCHECK(type_ == SMI); |
244 smi_ = i; | 252 smi_ = i; |
245 } | 253 } |
246 | 254 |
247 explicit AstValue(bool b) : type_(BOOLEAN) { bool_ = b; } | 255 explicit AstValue(bool b) : type_(BOOLEAN), next_(nullptr) { bool_ = b; } |
248 | 256 |
249 explicit AstValue(Type t) : type_(t) { | 257 explicit AstValue(Type t) : type_(t), next_(nullptr) { |
250 DCHECK(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE); | 258 DCHECK(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE); |
251 } | 259 } |
252 | 260 |
253 Type type_; | 261 Type type_; |
254 | 262 |
255 // Uninternalized value. | 263 // Uninternalized value. |
256 union { | 264 union { |
257 const AstRawString* string_; | 265 const AstRawString* string_; |
258 double number_; | 266 double number_; |
259 int smi_; | 267 int smi_; |
260 bool bool_; | 268 bool bool_; |
261 ZoneList<const AstRawString*>* strings_; | 269 const AstRawString* strings_; |
262 const char* symbol_name_; | 270 const char* symbol_name_; |
263 }; | 271 }; |
264 | 272 |
265 // Internalized value (empty before internalized). | 273 // Internalized value (pointer to the next uninternalized value until |
266 Handle<Object> value_; | 274 // internalized). |
| 275 union { |
| 276 Handle<Object> value_; |
| 277 AstValue* next_; |
| 278 }; |
267 }; | 279 }; |
268 | 280 |
269 | 281 |
270 // For generating constants. | 282 // For generating constants. |
271 #define STRING_CONSTANTS(F) \ | 283 #define STRING_CONSTANTS(F) \ |
272 F(anonymous_function, "(anonymous function)") \ | 284 F(anonymous_function, "(anonymous function)") \ |
273 F(arguments, "arguments") \ | 285 F(arguments, "arguments") \ |
274 F(async, "async") \ | 286 F(async, "async") \ |
275 F(await, "await") \ | 287 F(await, "await") \ |
276 F(constructor, "constructor") \ | 288 F(constructor, "constructor") \ |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 F(true_value) \ | 321 F(true_value) \ |
310 F(false_value) \ | 322 F(false_value) \ |
311 F(null_value) \ | 323 F(null_value) \ |
312 F(undefined_value) \ | 324 F(undefined_value) \ |
313 F(the_hole_value) | 325 F(the_hole_value) |
314 | 326 |
315 class AstValueFactory { | 327 class AstValueFactory { |
316 public: | 328 public: |
317 AstValueFactory(Zone* zone, uint32_t hash_seed) | 329 AstValueFactory(Zone* zone, uint32_t hash_seed) |
318 : string_table_(AstRawStringCompare), | 330 : string_table_(AstRawStringCompare), |
| 331 values_(nullptr), |
| 332 strings_end_(&strings_), |
319 zone_(zone), | 333 zone_(zone), |
320 isolate_(NULL), | 334 isolate_(NULL), |
321 hash_seed_(hash_seed) { | 335 hash_seed_(hash_seed) { |
| 336 ResetStrings(); |
322 #define F(name, str) name##_string_ = NULL; | 337 #define F(name, str) name##_string_ = NULL; |
323 STRING_CONSTANTS(F) | 338 STRING_CONSTANTS(F) |
324 #undef F | 339 #undef F |
325 #define F(name) name##_ = NULL; | 340 #define F(name) name##_ = NULL; |
326 OTHER_CONSTANTS(F) | 341 OTHER_CONSTANTS(F) |
327 #undef F | 342 #undef F |
328 } | 343 } |
329 | 344 |
330 Zone* zone() const { return zone_; } | 345 Zone* zone() const { return zone_; } |
331 | 346 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 const AstValue* NewSymbol(const char* name); | 381 const AstValue* NewSymbol(const char* name); |
367 const AstValue* NewNumber(double number, bool with_dot = false); | 382 const AstValue* NewNumber(double number, bool with_dot = false); |
368 const AstValue* NewSmi(int number); | 383 const AstValue* NewSmi(int number); |
369 const AstValue* NewBoolean(bool b); | 384 const AstValue* NewBoolean(bool b); |
370 const AstValue* NewStringList(ZoneList<const AstRawString*>* strings); | 385 const AstValue* NewStringList(ZoneList<const AstRawString*>* strings); |
371 const AstValue* NewNull(); | 386 const AstValue* NewNull(); |
372 const AstValue* NewUndefined(); | 387 const AstValue* NewUndefined(); |
373 const AstValue* NewTheHole(); | 388 const AstValue* NewTheHole(); |
374 | 389 |
375 private: | 390 private: |
| 391 AstValue* AddValue(AstValue* value) { |
| 392 if (isolate_) { |
| 393 value->Internalize(isolate_); |
| 394 } else { |
| 395 value->set_next(values_); |
| 396 values_ = value; |
| 397 } |
| 398 return value; |
| 399 } |
| 400 AstString* AddString(AstString* string) { |
| 401 if (isolate_) { |
| 402 string->Internalize(isolate_); |
| 403 } else { |
| 404 *strings_end_ = string; |
| 405 strings_end_ = string->next_location(); |
| 406 } |
| 407 return string; |
| 408 } |
| 409 void ResetStrings() { |
| 410 strings_ = nullptr; |
| 411 strings_end_ = &strings_; |
| 412 } |
376 AstRawString* GetOneByteStringInternal(Vector<const uint8_t> literal); | 413 AstRawString* GetOneByteStringInternal(Vector<const uint8_t> literal); |
377 AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal); | 414 AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal); |
378 AstRawString* GetString(uint32_t hash, bool is_one_byte, | 415 AstRawString* GetString(uint32_t hash, bool is_one_byte, |
379 Vector<const byte> literal_bytes); | 416 Vector<const byte> literal_bytes); |
380 | 417 |
381 static bool AstRawStringCompare(void* a, void* b); | 418 static bool AstRawStringCompare(void* a, void* b); |
382 | 419 |
383 // All strings are copied here, one after another (no NULLs inbetween). | 420 // All strings are copied here, one after another (no NULLs inbetween). |
384 base::HashMap string_table_; | 421 base::HashMap string_table_; |
385 // For keeping track of all AstValues and AstRawStrings we've created (so that | 422 // For keeping track of all AstValues and AstRawStrings we've created (so that |
386 // they can be internalized later). | 423 // they can be internalized later). |
387 List<AstValue*> values_; | 424 AstValue* values_; |
388 List<AstString*> strings_; | 425 // We need to keep track of strings_ in order, since cons strings require |
| 426 // their members to be internalized first. |
| 427 AstString* strings_; |
| 428 AstString** strings_end_; |
389 Zone* zone_; | 429 Zone* zone_; |
390 Isolate* isolate_; | 430 Isolate* isolate_; |
391 | 431 |
392 uint32_t hash_seed_; | 432 uint32_t hash_seed_; |
393 | 433 |
394 #define F(name, str) const AstRawString* name##_string_; | 434 #define F(name, str) const AstRawString* name##_string_; |
395 STRING_CONSTANTS(F) | 435 STRING_CONSTANTS(F) |
396 #undef F | 436 #undef F |
397 | 437 |
398 #define F(name) AstValue* name##_; | 438 #define F(name) AstValue* name##_; |
399 OTHER_CONSTANTS(F) | 439 OTHER_CONSTANTS(F) |
400 #undef F | 440 #undef F |
401 }; | 441 }; |
402 } // namespace internal | 442 } // namespace internal |
403 } // namespace v8 | 443 } // namespace v8 |
404 | 444 |
405 #undef STRING_CONSTANTS | 445 #undef STRING_CONSTANTS |
406 #undef OTHER_CONSTANTS | 446 #undef OTHER_CONSTANTS |
407 | 447 |
408 #endif // V8_AST_AST_VALUE_FACTORY_H_ | 448 #endif // V8_AST_AST_VALUE_FACTORY_H_ |
OLD | NEW |