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 #include "src/ast/scopeinfo.h" | 5 #include "src/ast/scopeinfo.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include "src/ast/context-slot-cache.h" | 9 #include "src/ast/context-slot-cache.h" |
10 #include "src/ast/scopes.h" | 10 #include "src/ast/scopes.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 HasSimpleParametersField::encode(has_simple_parameters) | | 109 HasSimpleParametersField::encode(has_simple_parameters) | |
110 FunctionKindField::encode(function_kind); | 110 FunctionKindField::encode(function_kind); |
111 scope_info->SetFlags(flags); | 111 scope_info->SetFlags(flags); |
112 scope_info->SetParameterCount(parameter_count); | 112 scope_info->SetParameterCount(parameter_count); |
113 scope_info->SetStackLocalCount(stack_local_count); | 113 scope_info->SetStackLocalCount(stack_local_count); |
114 scope_info->SetContextLocalCount(context_local_count); | 114 scope_info->SetContextLocalCount(context_local_count); |
115 scope_info->SetContextGlobalCount(context_global_count); | 115 scope_info->SetContextGlobalCount(context_global_count); |
116 | 116 |
117 int index = kVariablePartIndex; | 117 int index = kVariablePartIndex; |
118 // Add parameters. | 118 // Add parameters. |
119 DCHECK(index == scope_info->ParameterEntriesIndex()); | 119 DCHECK_EQ(index, scope_info->ParameterEntriesIndex()); |
120 if (scope->is_declaration_scope()) { | 120 if (scope->is_declaration_scope()) { |
121 for (int i = 0; i < parameter_count; ++i) { | 121 for (int i = 0; i < parameter_count; ++i) { |
122 scope_info->set(index++, | 122 scope_info->set(index++, |
123 *scope->AsDeclarationScope()->parameter(i)->name()); | 123 *scope->AsDeclarationScope()->parameter(i)->name()); |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
127 // Add stack locals' names. We are assuming that the stack locals' | 127 // Add stack locals' names. We are assuming that the stack locals' |
128 // slots are allocated in increasing order, so we can simply add | 128 // slots are allocated in increasing order, so we can simply add |
129 // them to the ScopeInfo object. | 129 // them to the ScopeInfo object. |
130 int first_slot_index; | 130 int first_slot_index; |
131 if (stack_local_count > 0) { | 131 if (stack_local_count > 0) { |
132 first_slot_index = stack_locals[0]->index(); | 132 first_slot_index = stack_locals[0]->index(); |
133 } else { | 133 } else { |
134 first_slot_index = 0; | 134 first_slot_index = 0; |
135 } | 135 } |
136 DCHECK(index == scope_info->StackLocalFirstSlotIndex()); | 136 DCHECK_EQ(index, scope_info->StackLocalFirstSlotIndex()); |
137 scope_info->set(index++, Smi::FromInt(first_slot_index)); | 137 scope_info->set(index++, Smi::FromInt(first_slot_index)); |
138 DCHECK(index == scope_info->StackLocalEntriesIndex()); | 138 DCHECK_EQ(index, scope_info->StackLocalEntriesIndex()); |
139 for (int i = 0; i < stack_local_count; ++i) { | 139 for (int i = 0; i < stack_local_count; ++i) { |
140 DCHECK(stack_locals[i]->index() == first_slot_index + i); | 140 DCHECK(stack_locals[i]->index() == first_slot_index + i); |
141 scope_info->set(index++, *stack_locals[i]->name()); | 141 scope_info->set(index++, *stack_locals[i]->name()); |
142 } | 142 } |
143 | 143 |
144 // Due to usage analysis, context-allocated locals are not necessarily in | 144 // Add context locals' names and info. Info lies beyond context globals' |
145 // increasing order: Some of them may be parameters which are allocated before | 145 // names. |
146 // the non-parameter locals. When the non-parameter locals are sorted | 146 // Make sure to store them in the order that they appear in the context. |
147 // according to usage, the allocated slot indices may not be in increasing | 147 DCHECK_EQ(index, scope_info->ContextLocalNameEntriesIndex()); |
148 // order with the variable list anymore. Thus, we first need to sort them by | 148 int info_index = index + context_local_count + context_global_count; |
149 // context slot index before adding them to the ScopeInfo object. | 149 DCHECK_EQ(info_index, scope_info->ContextLocalInfoEntriesIndex()); |
150 context_locals.Sort(&Variable::CompareIndex); | |
151 | |
152 // Add context locals' names. | |
153 DCHECK(index == scope_info->ContextLocalNameEntriesIndex()); | |
154 for (int i = 0; i < context_local_count; ++i) { | |
155 scope_info->set(index++, *context_locals[i]->name()); | |
156 } | |
157 | |
158 // Add context globals' names. | |
159 DCHECK(index == scope_info->ContextGlobalNameEntriesIndex()); | |
160 for (int i = 0; i < context_global_count; ++i) { | |
161 scope_info->set(index++, *context_globals[i]->name()); | |
162 } | |
163 | |
164 // Add context locals' info. | |
165 DCHECK(index == scope_info->ContextLocalInfoEntriesIndex()); | |
166 for (int i = 0; i < context_local_count; ++i) { | 150 for (int i = 0; i < context_local_count; ++i) { |
167 Variable* var = context_locals[i]; | 151 Variable* var = context_locals[i]; |
168 uint32_t value = | 152 int context_index = var->index() - Context::MIN_CONTEXT_SLOTS; |
| 153 uint32_t info = |
169 ContextLocalMode::encode(var->mode()) | | 154 ContextLocalMode::encode(var->mode()) | |
170 ContextLocalInitFlag::encode(var->initialization_flag()) | | 155 ContextLocalInitFlag::encode(var->initialization_flag()) | |
171 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); | 156 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); |
172 scope_info->set(index++, Smi::FromInt(value)); | 157 scope_info->set(index + context_index, *var->name()); |
| 158 scope_info->set(info_index + context_index, Smi::FromInt(info)); |
173 } | 159 } |
174 | 160 |
175 // Add context globals' info. | 161 index += context_local_count; |
176 DCHECK(index == scope_info->ContextGlobalInfoEntriesIndex()); | 162 |
| 163 // Add context globals' names and info. Info lies beyond context locals' info. |
| 164 DCHECK_EQ(index, scope_info->ContextGlobalNameEntriesIndex()); |
| 165 info_index = index + context_global_count + context_local_count; |
| 166 DCHECK_EQ(info_index, scope_info->ContextGlobalInfoEntriesIndex()); |
177 for (int i = 0; i < context_global_count; ++i) { | 167 for (int i = 0; i < context_global_count; ++i) { |
178 Variable* var = context_globals[i]; | 168 Variable* var = context_globals[i]; |
| 169 scope_info->set(index + i, *var->name()); |
179 // TODO(ishell): do we need this kind of info for globals here? | 170 // TODO(ishell): do we need this kind of info for globals here? |
180 uint32_t value = | 171 uint32_t info = |
181 ContextLocalMode::encode(var->mode()) | | 172 ContextLocalMode::encode(var->mode()) | |
182 ContextLocalInitFlag::encode(var->initialization_flag()) | | 173 ContextLocalInitFlag::encode(var->initialization_flag()) | |
183 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); | 174 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); |
184 scope_info->set(index++, Smi::FromInt(value)); | 175 scope_info->set(info_index + i, Smi::FromInt(info)); |
185 } | 176 } |
186 | 177 |
| 178 index += context_local_count + 2 * context_global_count; |
| 179 |
187 // If the receiver is allocated, add its index. | 180 // If the receiver is allocated, add its index. |
188 DCHECK(index == scope_info->ReceiverEntryIndex()); | 181 DCHECK(index == scope_info->ReceiverEntryIndex()); |
189 if (has_receiver) { | 182 if (has_receiver) { |
190 int var_index = scope->AsDeclarationScope()->receiver()->index(); | 183 int var_index = scope->AsDeclarationScope()->receiver()->index(); |
191 scope_info->set(index++, Smi::FromInt(var_index)); | 184 scope_info->set(index++, Smi::FromInt(var_index)); |
192 // ?? DCHECK(receiver_info != CONTEXT || var_index == | 185 // ?? DCHECK(receiver_info != CONTEXT || var_index == |
193 // scope_info->ContextLength() - 1); | 186 // scope_info->ContextLength() - 1); |
194 } | 187 } |
195 | 188 |
196 // If present, add the function variable name and its index. | 189 // If present, add the function variable name and its index. |
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 ContextLocalNameEntriesIndex() + ContextLocalCount(), this); | 688 ContextLocalNameEntriesIndex() + ContextLocalCount(), this); |
696 } | 689 } |
697 | 690 |
698 PrintF("}\n"); | 691 PrintF("}\n"); |
699 } | 692 } |
700 #endif // DEBUG | 693 #endif // DEBUG |
701 | 694 |
702 | 695 |
703 } // namespace internal | 696 } // namespace internal |
704 } // namespace v8 | 697 } // namespace v8 |
OLD | NEW |