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 14 matching lines...) Expand all Loading... |
25 &context_globals); | 25 &context_globals); |
26 const int stack_local_count = stack_locals.length(); | 26 const int stack_local_count = stack_locals.length(); |
27 const int context_local_count = context_locals.length(); | 27 const int context_local_count = context_locals.length(); |
28 const int context_global_count = context_globals.length(); | 28 const int context_global_count = context_globals.length(); |
29 // Make sure we allocate the correct amount. | 29 // Make sure we allocate the correct amount. |
30 DCHECK_EQ(scope->ContextLocalCount(), context_local_count); | 30 DCHECK_EQ(scope->ContextLocalCount(), context_local_count); |
31 DCHECK_EQ(scope->ContextGlobalCount(), context_global_count); | 31 DCHECK_EQ(scope->ContextGlobalCount(), context_global_count); |
32 | 32 |
33 // Determine use and location of the "this" binding if it is present. | 33 // Determine use and location of the "this" binding if it is present. |
34 VariableAllocationInfo receiver_info; | 34 VariableAllocationInfo receiver_info; |
35 if (scope->is_declaration_scope() && | 35 if (scope->has_this_declaration()) { |
36 scope->AsDeclarationScope()->has_this_declaration()) { | 36 Variable* var = scope->receiver(); |
37 Variable* var = scope->AsDeclarationScope()->receiver(); | |
38 if (!var->is_used()) { | 37 if (!var->is_used()) { |
39 receiver_info = UNUSED; | 38 receiver_info = UNUSED; |
40 } else if (var->IsContextSlot()) { | 39 } else if (var->IsContextSlot()) { |
41 receiver_info = CONTEXT; | 40 receiver_info = CONTEXT; |
42 } else { | 41 } else { |
43 DCHECK(var->IsParameter()); | 42 DCHECK(var->IsParameter()); |
44 receiver_info = STACK; | 43 receiver_info = STACK; |
45 } | 44 } |
46 } else { | 45 } else { |
47 receiver_info = NONE; | 46 receiver_info = NONE; |
48 } | 47 } |
49 | 48 |
50 bool has_new_target = | 49 bool has_new_target = scope->new_target_var() != nullptr; |
51 scope->is_declaration_scope() && | |
52 scope->AsDeclarationScope()->new_target_var() != nullptr; | |
53 | 50 |
54 // Determine use and location of the function variable if it is present. | 51 // Determine use and location of the function variable if it is present. |
55 VariableAllocationInfo function_name_info; | 52 VariableAllocationInfo function_name_info; |
56 VariableMode function_variable_mode; | 53 VariableMode function_variable_mode; |
57 if (scope->is_function_scope() && | 54 if (scope->is_function_scope() && scope->function() != NULL) { |
58 scope->AsDeclarationScope()->function() != nullptr) { | 55 Variable* var = scope->function()->proxy()->var(); |
59 Variable* var = scope->AsDeclarationScope()->function()->proxy()->var(); | |
60 if (!var->is_used()) { | 56 if (!var->is_used()) { |
61 function_name_info = UNUSED; | 57 function_name_info = UNUSED; |
62 } else if (var->IsContextSlot()) { | 58 } else if (var->IsContextSlot()) { |
63 function_name_info = CONTEXT; | 59 function_name_info = CONTEXT; |
64 } else { | 60 } else { |
65 DCHECK(var->IsStackLocal()); | 61 DCHECK(var->IsStackLocal()); |
66 function_name_info = STACK; | 62 function_name_info = STACK; |
67 } | 63 } |
68 function_variable_mode = var->mode(); | 64 function_variable_mode = var->mode(); |
69 } else { | 65 } else { |
70 function_name_info = NONE; | 66 function_name_info = NONE; |
71 function_variable_mode = VAR; | 67 function_variable_mode = VAR; |
72 } | 68 } |
73 DCHECK(context_global_count == 0 || scope->scope_type() == SCRIPT_SCOPE); | 69 DCHECK(context_global_count == 0 || scope->scope_type() == SCRIPT_SCOPE); |
74 | 70 |
75 const bool has_function_name = function_name_info != NONE; | 71 const bool has_function_name = function_name_info != NONE; |
76 const bool has_receiver = receiver_info == STACK || receiver_info == CONTEXT; | 72 const bool has_receiver = receiver_info == STACK || receiver_info == CONTEXT; |
77 const int parameter_count = scope->num_parameters(); | 73 const int parameter_count = scope->num_parameters(); |
78 const int length = kVariablePartIndex + parameter_count + | 74 const int length = kVariablePartIndex + parameter_count + |
79 (1 + stack_local_count) + 2 * context_local_count + | 75 (1 + stack_local_count) + 2 * context_local_count + |
80 2 * context_global_count + | 76 2 * context_global_count + |
81 (has_receiver ? 1 : 0) + (has_function_name ? 2 : 0); | 77 (has_receiver ? 1 : 0) + (has_function_name ? 2 : 0); |
82 | 78 |
83 Factory* factory = isolate->factory(); | 79 Factory* factory = isolate->factory(); |
84 Handle<ScopeInfo> scope_info = factory->NewScopeInfo(length); | 80 Handle<ScopeInfo> scope_info = factory->NewScopeInfo(length); |
85 | 81 |
86 bool has_simple_parameters = | 82 bool has_simple_parameters = |
87 scope->is_function_scope() && | 83 scope->is_function_scope() && scope->has_simple_parameters(); |
88 scope->AsDeclarationScope()->has_simple_parameters(); | |
89 FunctionKind function_kind = | |
90 scope->is_declaration_scope() | |
91 ? scope->AsDeclarationScope()->function_kind() | |
92 : kNormalFunction; | |
93 | 84 |
94 // Encode the flags. | 85 // Encode the flags. |
95 int flags = ScopeTypeField::encode(scope->scope_type()) | | 86 int flags = ScopeTypeField::encode(scope->scope_type()) | |
96 CallsEvalField::encode(scope->calls_eval()) | | 87 CallsEvalField::encode(scope->calls_eval()) | |
97 LanguageModeField::encode(scope->language_mode()) | | 88 LanguageModeField::encode(scope->language_mode()) | |
98 DeclarationScopeField::encode(scope->is_declaration_scope()) | | 89 DeclarationScopeField::encode(scope->is_declaration_scope()) | |
99 ReceiverVariableField::encode(receiver_info) | | 90 ReceiverVariableField::encode(receiver_info) | |
100 HasNewTargetField::encode(has_new_target) | | 91 HasNewTargetField::encode(has_new_target) | |
101 FunctionVariableField::encode(function_name_info) | | 92 FunctionVariableField::encode(function_name_info) | |
102 FunctionVariableMode::encode(function_variable_mode) | | 93 FunctionVariableMode::encode(function_variable_mode) | |
103 AsmModuleField::encode(scope->asm_module()) | | 94 AsmModuleField::encode(scope->asm_module()) | |
104 AsmFunctionField::encode(scope->asm_function()) | | 95 AsmFunctionField::encode(scope->asm_function()) | |
105 HasSimpleParametersField::encode(has_simple_parameters) | | 96 HasSimpleParametersField::encode(has_simple_parameters) | |
106 FunctionKindField::encode(function_kind); | 97 FunctionKindField::encode(scope->function_kind()); |
107 scope_info->SetFlags(flags); | 98 scope_info->SetFlags(flags); |
108 scope_info->SetParameterCount(parameter_count); | 99 scope_info->SetParameterCount(parameter_count); |
109 scope_info->SetStackLocalCount(stack_local_count); | 100 scope_info->SetStackLocalCount(stack_local_count); |
110 scope_info->SetContextLocalCount(context_local_count); | 101 scope_info->SetContextLocalCount(context_local_count); |
111 scope_info->SetContextGlobalCount(context_global_count); | 102 scope_info->SetContextGlobalCount(context_global_count); |
112 | 103 |
113 int index = kVariablePartIndex; | 104 int index = kVariablePartIndex; |
114 // Add parameters. | 105 // Add parameters. |
115 DCHECK(index == scope_info->ParameterEntriesIndex()); | 106 DCHECK(index == scope_info->ParameterEntriesIndex()); |
116 if (scope->is_declaration_scope()) { | 107 for (int i = 0; i < parameter_count; ++i) { |
117 for (int i = 0; i < parameter_count; ++i) { | 108 scope_info->set(index++, *scope->parameter(i)->name()); |
118 scope_info->set(index++, | |
119 *scope->AsDeclarationScope()->parameter(i)->name()); | |
120 } | |
121 } | 109 } |
122 | 110 |
123 // Add stack locals' names. We are assuming that the stack locals' | 111 // Add stack locals' names. We are assuming that the stack locals' |
124 // slots are allocated in increasing order, so we can simply add | 112 // slots are allocated in increasing order, so we can simply add |
125 // them to the ScopeInfo object. | 113 // them to the ScopeInfo object. |
126 int first_slot_index; | 114 int first_slot_index; |
127 if (stack_local_count > 0) { | 115 if (stack_local_count > 0) { |
128 first_slot_index = stack_locals[0]->index(); | 116 first_slot_index = stack_locals[0]->index(); |
129 } else { | 117 } else { |
130 first_slot_index = 0; | 118 first_slot_index = 0; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 uint32_t value = | 164 uint32_t value = |
177 ContextLocalMode::encode(var->mode()) | | 165 ContextLocalMode::encode(var->mode()) | |
178 ContextLocalInitFlag::encode(var->initialization_flag()) | | 166 ContextLocalInitFlag::encode(var->initialization_flag()) | |
179 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); | 167 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); |
180 scope_info->set(index++, Smi::FromInt(value)); | 168 scope_info->set(index++, Smi::FromInt(value)); |
181 } | 169 } |
182 | 170 |
183 // If the receiver is allocated, add its index. | 171 // If the receiver is allocated, add its index. |
184 DCHECK(index == scope_info->ReceiverEntryIndex()); | 172 DCHECK(index == scope_info->ReceiverEntryIndex()); |
185 if (has_receiver) { | 173 if (has_receiver) { |
186 int var_index = scope->AsDeclarationScope()->receiver()->index(); | 174 int var_index = scope->receiver()->index(); |
187 scope_info->set(index++, Smi::FromInt(var_index)); | 175 scope_info->set(index++, Smi::FromInt(var_index)); |
188 // ?? DCHECK(receiver_info != CONTEXT || var_index == | 176 // ?? DCHECK(receiver_info != CONTEXT || var_index == |
189 // scope_info->ContextLength() - 1); | 177 // scope_info->ContextLength() - 1); |
190 } | 178 } |
191 | 179 |
192 // If present, add the function variable name and its index. | 180 // If present, add the function variable name and its index. |
193 DCHECK(index == scope_info->FunctionNameEntryIndex()); | 181 DCHECK(index == scope_info->FunctionNameEntryIndex()); |
194 if (has_function_name) { | 182 if (has_function_name) { |
195 int var_index = | 183 int var_index = scope->function()->proxy()->var()->index(); |
196 scope->AsDeclarationScope()->function()->proxy()->var()->index(); | 184 scope_info->set(index++, *scope->function()->proxy()->name()); |
197 scope_info->set(index++, | |
198 *scope->AsDeclarationScope()->function()->proxy()->name()); | |
199 scope_info->set(index++, Smi::FromInt(var_index)); | 185 scope_info->set(index++, Smi::FromInt(var_index)); |
200 DCHECK(function_name_info != CONTEXT || | 186 DCHECK(function_name_info != CONTEXT || |
201 var_index == scope_info->ContextLength() - 1); | 187 var_index == scope_info->ContextLength() - 1); |
202 } | 188 } |
203 | 189 |
204 DCHECK(index == scope_info->length()); | 190 DCHECK(index == scope_info->length()); |
205 DCHECK(scope->num_parameters() == scope_info->ParameterCount()); | 191 DCHECK(scope->num_parameters() == scope_info->ParameterCount()); |
206 DCHECK(scope->num_heap_slots() == scope_info->ContextLength() || | 192 DCHECK(scope->num_heap_slots() == scope_info->ContextLength() || |
207 (scope->num_heap_slots() == kVariablePartIndex && | 193 (scope->num_heap_slots() == kVariablePartIndex && |
208 scope_info->ContextLength() == 0)); | 194 scope_info->ContextLength() == 0)); |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 ContextLocalNameEntriesIndex() + ContextLocalCount(), this); | 678 ContextLocalNameEntriesIndex() + ContextLocalCount(), this); |
693 } | 679 } |
694 | 680 |
695 PrintF("}\n"); | 681 PrintF("}\n"); |
696 } | 682 } |
697 #endif // DEBUG | 683 #endif // DEBUG |
698 | 684 |
699 | 685 |
700 } // namespace internal | 686 } // namespace internal |
701 } // namespace v8 | 687 } // namespace v8 |
OLD | NEW |