OLD | NEW |
---|---|
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/symbols.h" | 5 #include "vm/symbols.h" |
6 | 6 |
7 #include "vm/handles.h" | 7 #include "vm/handles.h" |
8 #include "vm/handles_impl.h" | 8 #include "vm/handles_impl.h" |
9 #include "vm/isolate.h" | 9 #include "vm/isolate.h" |
10 #include "vm/object.h" | 10 #include "vm/object.h" |
11 #include "vm/object_store.h" | 11 #include "vm/object_store.h" |
12 #include "vm/raw_object.h" | 12 #include "vm/raw_object.h" |
13 #include "vm/snapshot_ids.h" | 13 #include "vm/snapshot_ids.h" |
14 #include "vm/unicode.h" | 14 #include "vm/unicode.h" |
15 #include "vm/visitor.h" | 15 #include "vm/visitor.h" |
16 | 16 |
17 namespace dart { | 17 namespace dart { |
18 | 18 |
19 RawString* Symbols::predefined_[Symbols::kNumberOfOneCharCodeSymbols]; | 19 RawString* Symbols::predefined_[Symbols::kNumberOfOneCharCodeSymbols]; |
20 String* Symbols::symbol_handles_[Symbols::kMaxPredefinedId]; | 20 String* Symbols::symbol_handles_[Symbols::kMaxPredefinedId]; |
21 | 21 |
22 static const char* names[] = { | 22 static const char* names[] = { |
23 NULL, | 23 NULL, |
24 | 24 |
25 #define DEFINE_SYMBOL_LITERAL(symbol, literal) \ | 25 #define DEFINE_SYMBOL_LITERAL(symbol, literal) \ |
26 literal, | 26 literal, |
27 PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL) | 27 PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL) |
28 #undef DEFINE_SYMBOL_LITERAL | 28 #undef DEFINE_SYMBOL_LITERAL |
29 | |
30 "", // matches kKwTableStart. | |
31 | |
32 #define DEFINE_KEYWORD_SYMBOL_INDEX(token, chars, ignore1, ignore2) \ | |
33 chars, | |
34 DART_KEYWORD_LIST(DEFINE_KEYWORD_SYMBOL_INDEX) | |
35 #undef DEFINE_KEYWORD_SYMBOL_INDEX | |
29 }; | 36 }; |
30 | 37 |
31 intptr_t Symbols::num_of_grows_; | 38 intptr_t Symbols::num_of_grows_; |
32 intptr_t Symbols::collision_count_[kMaxCollisionBuckets]; | 39 intptr_t Symbols::collision_count_[kMaxCollisionBuckets]; |
33 | 40 |
34 DEFINE_FLAG(bool, dump_symbol_stats, false, "Dump symbol table statistics"); | 41 DEFINE_FLAG(bool, dump_symbol_stats, false, "Dump symbol table statistics"); |
35 | 42 |
36 | 43 |
37 const char* Symbols::Name(SymbolId symbol) { | 44 const char* Symbols::Name(SymbolId symbol) { |
38 ASSERT((symbol > kIllegal) && (symbol < kNullCharId)); | 45 ASSERT((symbol > kIllegal) && (symbol < kNullCharId)); |
39 return names[symbol]; | 46 return names[symbol]; |
40 } | 47 } |
41 | 48 |
42 | 49 |
50 const String& Symbols::Keyword(Token::Kind keyword) { | |
51 const int kw_index = keyword - Token::kFirstKeyword; | |
52 ASSERT((0 <= kw_index) && (kw_index < Token::numKeywords)); | |
53 ASSERT(Token::kABSTRACT == Token::kFirstKeyword); | |
54 const intptr_t keyword_id = Symbols::kABSTRACTId + kw_index; | |
Ivan Posva
2013/11/14 19:37:57
This should all be based on kKwTableStart if I am
hausner
2013/11/14 19:41:43
Yes. The kABSTRACTId is a left-over from before I
| |
55 ASSERT(symbol_handles_[keyword_id] != NULL); | |
56 return *symbol_handles_[keyword_id]; | |
57 } | |
58 | |
59 | |
43 void Symbols::InitOnce(Isolate* isolate) { | 60 void Symbols::InitOnce(Isolate* isolate) { |
44 // Should only be run by the vm isolate. | 61 // Should only be run by the vm isolate. |
45 ASSERT(isolate == Dart::vm_isolate()); | 62 ASSERT(isolate == Dart::vm_isolate()); |
46 | 63 |
47 if (FLAG_dump_symbol_stats) { | 64 if (FLAG_dump_symbol_stats) { |
48 num_of_grows_ = 0; | 65 num_of_grows_ = 0; |
49 for (intptr_t i = 0; i < kMaxCollisionBuckets; i++) { | 66 for (intptr_t i = 0; i < kMaxCollisionBuckets; i++) { |
50 collision_count_[i] = 0; | 67 collision_count_[i] = 0; |
51 } | 68 } |
52 } | 69 } |
53 | 70 |
54 // Create and setup a symbol table in the vm isolate. | 71 // Create and setup a symbol table in the vm isolate. |
55 SetupSymbolTable(isolate); | 72 SetupSymbolTable(isolate); |
56 | 73 |
57 // Create all predefined symbols. | 74 // Create all predefined symbols. |
58 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kNullCharId); | 75 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kNullCharId); |
59 ObjectStore* object_store = isolate->object_store(); | 76 ObjectStore* object_store = isolate->object_store(); |
60 Array& symbol_table = Array::Handle(); | 77 Array& symbol_table = Array::Handle(); |
61 | 78 |
62 | 79 |
63 // First set up all the predefined string symbols. | 80 // First set up all the predefined string symbols. |
64 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { | 81 for (intptr_t i = 1; i < Symbols::kKwTableStart; i++) { |
65 // The symbol_table needs to be reloaded as it might have grown in the | 82 // The symbol_table needs to be reloaded as it might have grown in the |
66 // previous iteration. | 83 // previous iteration. |
67 symbol_table = object_store->symbol_table(); | 84 symbol_table = object_store->symbol_table(); |
68 String* str = String::ReadOnlyHandle(); | 85 String* str = String::ReadOnlyHandle(); |
69 *str = OneByteString::New(names[i], Heap::kOld); | 86 *str = OneByteString::New(names[i], Heap::kOld); |
70 Add(symbol_table, *str); | 87 Add(symbol_table, *str); |
71 symbol_handles_[i] = str; | 88 symbol_handles_[i] = str; |
72 } | 89 } |
73 Object::RegisterSingletonClassNames(); | 90 Object::RegisterSingletonClassNames(); |
74 | 91 |
92 // Create symbols for language keywords. Some keywords are equal to | |
93 // symbols we already created, so use New() instead of Add() to ensure | |
94 // that the symbols are canonicalized. | |
95 for (intptr_t i = Symbols::kKwTableStart; i < Symbols::kNullCharId; i++) { | |
96 String* str = String::ReadOnlyHandle(); | |
97 *str = New(names[i]); | |
98 symbol_handles_[i] = str; | |
99 } | |
100 | |
75 // Add Latin1 characters as Symbols, so that Symbols::FromCharCode is fast. | 101 // Add Latin1 characters as Symbols, so that Symbols::FromCharCode is fast. |
76 for (intptr_t c = 0; c < kNumberOfOneCharCodeSymbols; c++) { | 102 for (intptr_t c = 0; c < kNumberOfOneCharCodeSymbols; c++) { |
77 // The symbol_table needs to be reloaded as it might have grown in the | 103 // The symbol_table needs to be reloaded as it might have grown in the |
78 // previous iteration. | 104 // previous iteration. |
79 symbol_table = object_store->symbol_table(); | 105 symbol_table = object_store->symbol_table(); |
80 intptr_t idx = (kNullCharId + c); | 106 intptr_t idx = (kNullCharId + c); |
81 ASSERT(idx < kMaxPredefinedId); | 107 ASSERT(idx < kMaxPredefinedId); |
82 ASSERT(Utf::IsLatin1(c)); | 108 ASSERT(Utf::IsLatin1(c)); |
83 uint8_t ch = static_cast<uint8_t>(c); | 109 uint8_t ch = static_cast<uint8_t>(c); |
84 String* str = String::ReadOnlyHandle(); | 110 String* str = String::ReadOnlyHandle(); |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
447 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 473 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
448 ASSERT(IsVMSymbolId(object_id)); | 474 ASSERT(IsVMSymbolId(object_id)); |
449 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 475 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
450 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { | 476 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { |
451 return symbol_handles_[i]->raw(); | 477 return symbol_handles_[i]->raw(); |
452 } | 478 } |
453 return Object::null(); | 479 return Object::null(); |
454 } | 480 } |
455 | 481 |
456 } // namespace dart | 482 } // namespace dart |
OLD | NEW |