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 #ifndef VM_PARSER_H_ | 5 #ifndef VM_PARSER_H_ |
6 #define VM_PARSER_H_ | 6 #define VM_PARSER_H_ |
7 | 7 |
8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
9 | 9 |
10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 struct ParamList; | 37 struct ParamList; |
38 struct QualIdent; | 38 struct QualIdent; |
39 class TopLevel; | 39 class TopLevel; |
40 class RecursionChecker; | 40 class RecursionChecker; |
41 | 41 |
42 // We cache compile time constants during compilation. This allows us | 42 // We cache compile time constants during compilation. This allows us |
43 // to look them up when the same code gets compiled again. During | 43 // to look them up when the same code gets compiled again. During |
44 // background compilation, we are not able to evaluate the constants | 44 // background compilation, we are not able to evaluate the constants |
45 // so this cache is necessary to support background compilation. | 45 // so this cache is necessary to support background compilation. |
46 // | 46 // |
47 // There are two places a constant can be cached, depending on whether | 47 // We cache the constants with the script itself. This is helpful during isolate |
48 // the script is in the vm heap (snapshot) or not. | 48 // reloading, as it allows us to reference the compile time constants associated |
49 // | 49 // with a particular version of a script. The map key is simply the |
50 // When a script is in the vm heap, we are not able to cache the | 50 // TokenPosition where the constant is defined. |
51 // constants on the script itself (it is read-only) so we cache them | |
52 // in a per-isolate shared map in the object store. In this case the | |
53 // map key is a pair (array) with 2 elements: | |
54 // | |
55 // - key[0] contains the canonicalized url of the script. | |
56 // - key[1] contains the token position of the constant in the script. | |
57 // | |
58 // The VMConstantsMap type is used to implement this per-isolate map. | |
59 // | |
60 // When a script is not in the vm heap, we cache the constants with | |
61 // the script itself. This is helpful during isolate reloading, as it | |
62 // allows us to reference the compile time constants associated with a | |
63 // particular version of a script. In this case the map key is simply | |
64 // the TokenPosition where the constant is defined. | |
65 // | |
66 // The ConstantsMap type is used to implement this per-script map. | |
67 | |
68 // The key for the per-isolate compile time constants map. By using a | |
69 // ValueObject we are able to look up a constant without allocating a | |
70 // key pair (array). | |
71 // The per-script compile-time constants map. | |
72 struct UrlAndPosKey : ValueObject { | |
73 UrlAndPosKey(const String& url, TokenPosition pos) | |
74 : script_url(url), token_pos(pos) { } | |
75 const String& script_url; | |
76 TokenPosition token_pos; | |
77 }; | |
78 | |
79 // The per-isolate compile-time constants map. | |
80 class VMConstMapKeyEqualsTraits { | |
81 public: | |
82 static const char* Name() { return "VMConstMapKeyEqualsTraits"; } | |
83 static bool ReportStats() { return false; } | |
84 | |
85 static bool IsMatch(const Object& a, const Object& b) { | |
86 const Array& key1 = Array::Cast(a); | |
87 const Array& key2 = Array::Cast(b); | |
88 // Compare raw strings of script url symbol and raw smi of token positon. | |
89 return (key1.At(0) == key2.At(0)) && (key1.At(1) == key2.At(1)); | |
90 } | |
91 static bool IsMatch(const UrlAndPosKey& key1, const Object& b) { | |
92 const Array& key2 = Array::Cast(b); | |
93 // Compare raw strings of script url symbol and token positon. | |
94 return (key1.script_url.raw() == key2.At(0)) | |
95 && (key1.token_pos.value() == Smi::Value(Smi::RawCast(key2.At(1)))); | |
96 } | |
97 static uword Hash(const Object& obj) { | |
98 const Array& key = Array::Cast(obj); | |
99 intptr_t url_hash = String::HashRawSymbol(String::RawCast(key.At(0))); | |
100 intptr_t pos = Smi::Value(Smi::RawCast(key.At(1))); | |
101 return HashValue(url_hash, pos); | |
102 } | |
103 static uword Hash(const UrlAndPosKey& key) { | |
104 return HashValue(String::HashRawSymbol(key.script_url.raw()), | |
105 key.token_pos.value()); | |
106 } | |
107 // Used by CacheConstantValue if a new constant is added to the map. | |
108 static RawObject* NewKey(const UrlAndPosKey& key) { | |
109 const Array& key_obj = Array::Handle(Array::New(2)); | |
110 key_obj.SetAt(0, key.script_url); | |
111 key_obj.SetAt(1, Smi::Handle(Smi::New(key.token_pos.value()))); | |
112 return key_obj.raw();; | |
113 } | |
114 | |
115 private: | |
116 static uword HashValue(intptr_t url_hash, intptr_t pos) { | |
117 return url_hash * pos % (Smi::kMaxValue - 13); | |
118 } | |
119 }; | |
120 typedef UnorderedHashMap<VMConstMapKeyEqualsTraits> VMConstantsMap; | |
121 | |
122 class ConstMapKeyEqualsTraits { | 51 class ConstMapKeyEqualsTraits { |
123 public: | 52 public: |
124 static const char* Name() { return "ConstMapKeyEqualsTraits"; } | 53 static const char* Name() { return "ConstMapKeyEqualsTraits"; } |
125 static bool ReportStats() { return false; } | 54 static bool ReportStats() { return false; } |
126 | 55 |
127 static bool IsMatch(const Object& a, const Object& b) { | 56 static bool IsMatch(const Object& a, const Object& b) { |
128 const Smi& key1 = Smi::Cast(a); | 57 const Smi& key1 = Smi::Cast(a); |
129 const Smi& key2 = Smi::Cast(b); | 58 const Smi& key2 = Smi::Cast(b); |
130 return (key1.Value() == key2.Value()); | 59 return (key1.Value() == key2.Value()); |
131 } | 60 } |
(...skipping 905 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1037 | 966 |
1038 intptr_t recursion_counter_; | 967 intptr_t recursion_counter_; |
1039 friend class RecursionChecker; | 968 friend class RecursionChecker; |
1040 | 969 |
1041 DISALLOW_COPY_AND_ASSIGN(Parser); | 970 DISALLOW_COPY_AND_ASSIGN(Parser); |
1042 }; | 971 }; |
1043 | 972 |
1044 } // namespace dart | 973 } // namespace dart |
1045 | 974 |
1046 #endif // VM_PARSER_H_ | 975 #endif // VM_PARSER_H_ |
OLD | NEW |