OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_SCOPEINFO_H_ | 5 #ifndef V8_SCOPEINFO_H_ |
6 #define V8_SCOPEINFO_H_ | 6 #define V8_SCOPEINFO_H_ |
7 | 7 |
8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
9 #include "src/modules.h" | 9 #include "src/modules.h" |
10 #include "src/variables.h" | 10 #include "src/variables.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 | 14 |
15 // Cache for mapping (data, property name) into context slot index. | 15 // Cache for mapping (data, property name) into context slot index. |
16 // The cache contains both positive and negative results. | 16 // The cache contains both positive and negative results. |
17 // Slot index equals -1 means the property is absent. | 17 // Slot index equals -1 means the property is absent. |
18 // Cleared at startup and prior to mark sweep collection. | 18 // Cleared at startup and prior to mark sweep collection. |
19 class ContextSlotCache { | 19 class ContextSlotCache { |
20 public: | 20 public: |
21 // Lookup context slot index for (data, name). | 21 // Lookup context slot index for (data, name). |
22 // If absent, kNotFound is returned. | 22 // If absent, kNotFound is returned. |
23 int Lookup(Object* data, String* name, VariableMode* mode, | 23 int Lookup(Object* data, String* name, VariableMode* mode, |
24 InitializationFlag* init_flag, | 24 VariableLocation* location, InitializationFlag* init_flag, |
25 MaybeAssignedFlag* maybe_assigned_flag); | 25 MaybeAssignedFlag* maybe_assigned_flag); |
26 | 26 |
27 // Update an element in the cache. | 27 // Update an element in the cache. |
28 void Update(Handle<Object> data, Handle<String> name, VariableMode mode, | 28 void Update(Handle<Object> data, Handle<String> name, VariableMode mode, |
29 InitializationFlag init_flag, | 29 VariableLocation location, InitializationFlag init_flag, |
30 MaybeAssignedFlag maybe_assigned_flag, int slot_index); | 30 MaybeAssignedFlag maybe_assigned_flag, int slot_index); |
31 | 31 |
32 // Clear the cache. | 32 // Clear the cache. |
33 void Clear(); | 33 void Clear(); |
34 | 34 |
35 static const int kNotFound = -2; | 35 static const int kNotFound = -2; |
36 | 36 |
37 private: | 37 private: |
38 ContextSlotCache() { | 38 ContextSlotCache() { |
39 for (int i = 0; i < kLength; ++i) { | 39 for (int i = 0; i < kLength; ++i) { |
40 keys_[i].data = NULL; | 40 keys_[i].data = NULL; |
41 keys_[i].name = NULL; | 41 keys_[i].name = NULL; |
42 values_[i] = kNotFound; | 42 values_[i] = kNotFound; |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 inline static int Hash(Object* data, String* name); | 46 inline static int Hash(Object* data, String* name); |
47 | 47 |
48 #ifdef DEBUG | 48 #ifdef DEBUG |
49 void ValidateEntry(Handle<Object> data, Handle<String> name, | 49 void ValidateEntry(Handle<Object> data, Handle<String> name, |
50 VariableMode mode, InitializationFlag init_flag, | 50 VariableMode mode, VariableLocation location, |
| 51 InitializationFlag init_flag, |
51 MaybeAssignedFlag maybe_assigned_flag, int slot_index); | 52 MaybeAssignedFlag maybe_assigned_flag, int slot_index); |
52 #endif | 53 #endif |
53 | 54 |
54 static const int kLength = 256; | 55 static const int kLength = 256; |
55 struct Key { | 56 struct Key { |
56 Object* data; | 57 Object* data; |
57 String* name; | 58 String* name; |
58 }; | 59 }; |
59 | 60 |
60 struct Value { | 61 struct Value { |
61 Value(VariableMode mode, InitializationFlag init_flag, | 62 enum VariableLocationFlag { kContext, kGlobal }; |
62 MaybeAssignedFlag maybe_assigned_flag, int index) { | 63 |
| 64 Value(VariableMode mode, VariableLocation location, |
| 65 InitializationFlag init_flag, MaybeAssignedFlag maybe_assigned_flag, |
| 66 int index) { |
| 67 DCHECK(location == VariableLocation::CONTEXT || |
| 68 location == VariableLocation::GLOBAL); |
| 69 VariableLocationFlag location_flag = |
| 70 location == VariableLocation::CONTEXT ? kContext : kGlobal; |
63 DCHECK(ModeField::is_valid(mode)); | 71 DCHECK(ModeField::is_valid(mode)); |
| 72 DCHECK(VariableLocationField::is_valid(location_flag)); |
64 DCHECK(InitField::is_valid(init_flag)); | 73 DCHECK(InitField::is_valid(init_flag)); |
65 DCHECK(MaybeAssignedField::is_valid(maybe_assigned_flag)); | 74 DCHECK(MaybeAssignedField::is_valid(maybe_assigned_flag)); |
66 DCHECK(IndexField::is_valid(index)); | 75 DCHECK(IndexField::is_valid(index)); |
67 value_ = ModeField::encode(mode) | IndexField::encode(index) | | 76 value_ = ModeField::encode(mode) | IndexField::encode(index) | |
| 77 VariableLocationField::encode(location_flag) | |
68 InitField::encode(init_flag) | | 78 InitField::encode(init_flag) | |
69 MaybeAssignedField::encode(maybe_assigned_flag); | 79 MaybeAssignedField::encode(maybe_assigned_flag); |
70 DCHECK(mode == this->mode()); | 80 DCHECK(mode == this->mode()); |
| 81 DCHECK(location == this->location()); |
71 DCHECK(init_flag == this->initialization_flag()); | 82 DCHECK(init_flag == this->initialization_flag()); |
72 DCHECK(maybe_assigned_flag == this->maybe_assigned_flag()); | 83 DCHECK(maybe_assigned_flag == this->maybe_assigned_flag()); |
73 DCHECK(index == this->index()); | 84 DCHECK(index == this->index()); |
74 } | 85 } |
75 | 86 |
76 explicit inline Value(uint32_t value) : value_(value) {} | 87 explicit inline Value(uint32_t value) : value_(value) {} |
77 | 88 |
78 uint32_t raw() { return value_; } | 89 uint32_t raw() { return value_; } |
79 | 90 |
80 VariableMode mode() { return ModeField::decode(value_); } | 91 VariableMode mode() { return ModeField::decode(value_); } |
81 | 92 |
| 93 VariableLocation location() { |
| 94 switch (VariableLocationField::decode(value_)) { |
| 95 case kContext: |
| 96 return VariableLocation::CONTEXT; |
| 97 case kGlobal: |
| 98 return VariableLocation::GLOBAL; |
| 99 } |
| 100 UNREACHABLE(); |
| 101 return VariableLocation::CONTEXT; |
| 102 } |
| 103 |
82 InitializationFlag initialization_flag() { | 104 InitializationFlag initialization_flag() { |
83 return InitField::decode(value_); | 105 return InitField::decode(value_); |
84 } | 106 } |
85 | 107 |
86 MaybeAssignedFlag maybe_assigned_flag() { | 108 MaybeAssignedFlag maybe_assigned_flag() { |
87 return MaybeAssignedField::decode(value_); | 109 return MaybeAssignedField::decode(value_); |
88 } | 110 } |
89 | 111 |
90 int index() { return IndexField::decode(value_); } | 112 int index() { return IndexField::decode(value_); } |
91 | 113 |
92 // Bit fields in value_ (type, shift, size). Must be public so the | 114 // Bit fields in value_ (type, shift, size). Must be public so the |
93 // constants can be embedded in generated code. | 115 // constants can be embedded in generated code. |
94 class ModeField : public BitField<VariableMode, 0, 4> {}; | 116 class ModeField : public BitField<VariableMode, 0, 4> {}; |
95 class InitField : public BitField<InitializationFlag, 4, 1> {}; | 117 class InitField : public BitField<InitializationFlag, 4, 1> {}; |
96 class MaybeAssignedField : public BitField<MaybeAssignedFlag, 5, 1> {}; | 118 class MaybeAssignedField : public BitField<MaybeAssignedFlag, 5, 1> {}; |
97 class IndexField : public BitField<int, 6, 32 - 6> {}; | 119 class VariableLocationField : public BitField<VariableLocationFlag, 6, 1> { |
| 120 }; |
| 121 class IndexField : public BitField<int, 7, 32 - 7> {}; |
98 | 122 |
99 private: | 123 private: |
100 uint32_t value_; | 124 uint32_t value_; |
101 }; | 125 }; |
102 | 126 |
103 Key keys_[kLength]; | 127 Key keys_[kLength]; |
104 uint32_t values_[kLength]; | 128 uint32_t values_[kLength]; |
105 | 129 |
106 friend class Isolate; | 130 friend class Isolate; |
107 DISALLOW_COPY_AND_ASSIGN(ContextSlotCache); | 131 DISALLOW_COPY_AND_ASSIGN(ContextSlotCache); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 } | 189 } |
166 void set_index(int i, int index) { | 190 void set_index(int i, int index) { |
167 set(index_offset(i), Smi::FromInt(index)); | 191 set(index_offset(i), Smi::FromInt(index)); |
168 } | 192 } |
169 }; | 193 }; |
170 | 194 |
171 | 195 |
172 } } // namespace v8::internal | 196 } } // namespace v8::internal |
173 | 197 |
174 #endif // V8_SCOPEINFO_H_ | 198 #endif // V8_SCOPEINFO_H_ |
OLD | NEW |