OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 TEMPORARY // temporary variables (not user-visible), never | 67 TEMPORARY // temporary variables (not user-visible), never |
68 // in a context | 68 // in a context |
69 }; | 69 }; |
70 | 70 |
71 enum Kind { | 71 enum Kind { |
72 NORMAL, | 72 NORMAL, |
73 THIS, | 73 THIS, |
74 ARGUMENTS | 74 ARGUMENTS |
75 }; | 75 }; |
76 | 76 |
77 enum Location { | |
78 // Before and during variable allocation, a variable whose location is | |
79 // not yet determined. After allocation, a variable looked up as a | |
80 // property on the global object (and possibly absent). name() is the | |
81 // variable name, index() is invalid. | |
82 UNALLOCATED, | |
83 | |
84 // A slot in the parameter section on the stack. index() is the | |
85 // parameter index, counting left-to-right. The reciever is index -1; | |
86 // the first parameter is index 0. | |
87 PARAMETER, | |
88 | |
89 // A slot in the local section on the stack. index() is the variable | |
90 // index in the stack frame, starting at 0. | |
91 LOCAL, | |
92 | |
93 // An indexed slot in a heap context. index() is the variable index in | |
94 // the context object on the heap, starting at 0. scope() is the | |
95 // corresponding scope. | |
96 CONTEXT, | |
97 | |
98 // A named slot in a heap context. name() is the variable name in the | |
99 // context object on the heap, with lookup starting at the current | |
100 // context. index() is invalid. | |
101 LOOKUP | |
102 }; | |
103 | |
77 Variable(Scope* scope, | 104 Variable(Scope* scope, |
78 Handle<String> name, | 105 Handle<String> name, |
79 Mode mode, | 106 Mode mode, |
80 bool is_valid_lhs, | 107 bool is_valid_lhs, |
81 Kind kind); | 108 Kind kind); |
82 | 109 |
83 // Printing support | 110 // Printing support |
84 static const char* Mode2String(Mode mode); | 111 static const char* Mode2String(Mode mode); |
85 | 112 |
86 // Type testing & conversion. Global variables are not slots. | |
87 Property* AsProperty() const; | |
88 Slot* AsSlot() const; | |
89 | |
90 bool IsValidLeftHandSide() { return is_valid_LHS_; } | 113 bool IsValidLeftHandSide() { return is_valid_LHS_; } |
91 | 114 |
92 // The source code for an eval() call may refer to a variable that is | 115 // The source code for an eval() call may refer to a variable that is |
93 // in an outer scope about which we don't know anything (it may not | 116 // in an outer scope about which we don't know anything (it may not |
94 // be the global scope). scope() is NULL in that case. Currently the | 117 // be the global scope). scope() is NULL in that case. Currently the |
95 // scope is only used to follow the context chain length. | 118 // scope is only used to follow the context chain length. |
96 Scope* scope() const { return scope_; } | 119 Scope* scope() const { return scope_; } |
97 | 120 |
98 Handle<String> name() const { return name_; } | 121 Handle<String> name() const { return name_; } |
99 Mode mode() const { return mode_; } | 122 Mode mode() const { return mode_; } |
100 bool is_accessed_from_inner_function_scope() const { | 123 bool is_accessed_from_inner_function_scope() const { |
101 return is_accessed_from_inner_function_scope_; | 124 return is_accessed_from_inner_function_scope_; |
102 } | 125 } |
103 void MarkAsAccessedFromInnerFunctionScope() { | 126 void MarkAsAccessedFromInnerFunctionScope() { |
104 ASSERT(mode_ != TEMPORARY); | 127 ASSERT(mode_ != TEMPORARY); |
105 is_accessed_from_inner_function_scope_ = true; | 128 is_accessed_from_inner_function_scope_ = true; |
106 } | 129 } |
107 bool is_used() { return is_used_; } | 130 bool is_used() { return is_used_; } |
108 void set_is_used(bool flag) { is_used_ = flag; } | 131 void set_is_used(bool flag) { is_used_ = flag; } |
109 | 132 |
110 bool IsVariable(Handle<String> n) const { | 133 bool IsVariable(Handle<String> n) const { |
111 return !is_this() && name().is_identical_to(n); | 134 return !is_this() && name().is_identical_to(n); |
112 } | 135 } |
113 | 136 |
114 bool IsStackAllocated() const; | 137 bool IsUnallocated() const { return location_ == UNALLOCATED; } |
115 bool IsParameter() const; // Includes 'this'. | 138 bool IsParameter() const { return location_ == PARAMETER; } |
116 bool IsStackLocal() const; | 139 bool IsStackLocal() const { return location_ == LOCAL; } |
117 bool IsContextSlot() const; | 140 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); } |
141 bool IsContextSlot() const { return location_ == CONTEXT; } | |
142 bool IsLookupSlot() const { return location_ == LOOKUP; } | |
118 | 143 |
119 bool is_dynamic() const { | 144 bool is_dynamic() const { |
120 return (mode_ == DYNAMIC || | 145 return (mode_ == DYNAMIC || |
121 mode_ == DYNAMIC_GLOBAL || | 146 mode_ == DYNAMIC_GLOBAL || |
122 mode_ == DYNAMIC_LOCAL); | 147 mode_ == DYNAMIC_LOCAL); |
123 } | 148 } |
124 | 149 |
125 bool is_global() const; | 150 bool is_global() const; |
126 bool is_this() const { return kind_ == THIS; } | 151 bool is_this() const { return kind_ == THIS; } |
127 bool is_arguments() const { return kind_ == ARGUMENTS; } | 152 bool is_arguments() const { return kind_ == ARGUMENTS; } |
128 | 153 |
129 // True if the variable is named eval and not known to be shadowed. | 154 // True if the variable is named eval and not known to be shadowed. |
130 bool is_possibly_eval() const { | 155 bool is_possibly_eval() const { |
131 return IsVariable(FACTORY->eval_symbol()) && | 156 return IsVariable(FACTORY->eval_symbol()) && |
132 (mode_ == DYNAMIC || mode_ == DYNAMIC_GLOBAL); | 157 (mode_ == DYNAMIC || mode_ == DYNAMIC_GLOBAL); |
133 } | 158 } |
134 | 159 |
135 Variable* local_if_not_shadowed() const { | 160 Variable* local_if_not_shadowed() const { |
136 ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); | 161 ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); |
137 return local_if_not_shadowed_; | 162 return local_if_not_shadowed_; |
138 } | 163 } |
139 | 164 |
140 void set_local_if_not_shadowed(Variable* local) { | 165 void set_local_if_not_shadowed(Variable* local) { |
141 local_if_not_shadowed_ = local; | 166 local_if_not_shadowed_ = local; |
142 } | 167 } |
143 | 168 |
144 Slot* rewrite() const { return rewrite_; } | 169 Location location() const { return location_; } |
145 void set_rewrite(Slot* slot) { rewrite_ = slot; } | 170 int index() const { return index_; } |
171 | |
172 void AllocateTo(Location location, int index) { | |
173 location_ = location; | |
174 index_ = index; | |
175 } | |
146 | 176 |
147 private: | 177 private: |
148 Scope* scope_; | 178 Scope* scope_; |
149 Handle<String> name_; | 179 Handle<String> name_; |
150 Mode mode_; | 180 Mode mode_; |
151 Kind kind_; | 181 Kind kind_; |
182 Location location_; | |
fschneider
2011/09/06 09:34:22
Suggestion for improvement: Compress these three e
| |
183 int index_; | |
152 | 184 |
153 Variable* local_if_not_shadowed_; | 185 Variable* local_if_not_shadowed_; |
154 | 186 |
155 // Code generation. | |
156 Slot* rewrite_; | |
157 | |
158 // Valid as a LHS? (const and this are not valid LHS, for example) | 187 // Valid as a LHS? (const and this are not valid LHS, for example) |
159 bool is_valid_LHS_; | 188 bool is_valid_LHS_; |
160 | 189 |
161 // Usage info. | 190 // Usage info. |
162 bool is_accessed_from_inner_function_scope_; // set by variable resolver | 191 bool is_accessed_from_inner_function_scope_; // set by variable resolver |
163 bool is_used_; | 192 bool is_used_; |
164 }; | 193 }; |
165 | 194 |
166 | 195 |
167 } } // namespace v8::internal | 196 } } // namespace v8::internal |
168 | 197 |
169 #endif // V8_VARIABLES_H_ | 198 #endif // V8_VARIABLES_H_ |
OLD | NEW |