| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 | 141 |
| 142 | 142 |
| 143 Scope::Scope(Scope* inner_scope, SerializedScopeInfo* scope_info) | 143 Scope::Scope(Scope* inner_scope, SerializedScopeInfo* scope_info) |
| 144 : inner_scopes_(4), | 144 : inner_scopes_(4), |
| 145 variables_(), | 145 variables_(), |
| 146 temps_(4), | 146 temps_(4), |
| 147 params_(4), | 147 params_(4), |
| 148 unresolved_(16), | 148 unresolved_(16), |
| 149 decls_(4) { | 149 decls_(4) { |
| 150 ASSERT(scope_info != NULL); | 150 ASSERT(scope_info != NULL); |
| 151 SetDefaults(FUNCTION_SCOPE, inner_scope->outer_scope(), scope_info); | 151 SetDefaults(FUNCTION_SCOPE, NULL, scope_info); |
| 152 ASSERT(resolved()); | 152 ASSERT(resolved()); |
| 153 InsertAfterScope(inner_scope); | |
| 154 if (scope_info->HasHeapAllocatedLocals()) { | 153 if (scope_info->HasHeapAllocatedLocals()) { |
| 155 num_heap_slots_ = scope_info_->NumberOfContextSlots(); | 154 num_heap_slots_ = scope_info_->NumberOfContextSlots(); |
| 156 } | 155 } |
| 157 | 156 |
| 157 AddInnerScope(inner_scope); |
| 158 |
| 158 // This scope's arguments shadow (if present) is context-allocated if an inner | 159 // This scope's arguments shadow (if present) is context-allocated if an inner |
| 159 // scope accesses this one's parameters. Allocate the arguments_shadow_ | 160 // scope accesses this one's parameters. Allocate the arguments_shadow_ |
| 160 // variable if necessary. | 161 // variable if necessary. |
| 161 Variable::Mode mode; | 162 Variable::Mode mode; |
| 162 int arguments_shadow_index = | 163 int arguments_shadow_index = |
| 163 scope_info_->ContextSlotIndex(Heap::arguments_shadow_symbol(), &mode); | 164 scope_info_->ContextSlotIndex(Heap::arguments_shadow_symbol(), &mode); |
| 164 if (arguments_shadow_index >= 0) { | 165 if (arguments_shadow_index >= 0) { |
| 165 ASSERT(mode == Variable::INTERNAL); | 166 ASSERT(mode == Variable::INTERNAL); |
| 166 arguments_shadow_ = new Variable(this, | 167 arguments_shadow_ = new Variable(this, |
| 167 Factory::arguments_shadow_symbol(), | 168 Factory::arguments_shadow_symbol(), |
| 168 Variable::INTERNAL, | 169 Variable::INTERNAL, |
| 169 true, | 170 true, |
| 170 Variable::ARGUMENTS); | 171 Variable::ARGUMENTS); |
| 171 arguments_shadow_->set_rewrite( | 172 arguments_shadow_->set_rewrite( |
| 172 new Slot(arguments_shadow_, Slot::CONTEXT, arguments_shadow_index)); | 173 new Slot(arguments_shadow_, Slot::CONTEXT, arguments_shadow_index)); |
| 173 arguments_shadow_->set_is_used(true); | 174 arguments_shadow_->set_is_used(true); |
| 174 } | 175 } |
| 175 } | 176 } |
| 176 | 177 |
| 177 | 178 |
| 179 Scope* Scope::DeserializeScopeChain(CompilationInfo* info, |
| 180 Scope* global_scope) { |
| 181 ASSERT(!info->closure().is_null()); |
| 182 // If we have a serialized scope info, reuse it. |
| 183 Scope* innermost_scope = NULL; |
| 184 Scope* scope = NULL; |
| 185 |
| 186 SerializedScopeInfo* scope_info = info->closure()->shared()->scope_info(); |
| 187 if (scope_info != SerializedScopeInfo::Empty()) { |
| 188 JSFunction* current = *info->closure(); |
| 189 do { |
| 190 current = current->context()->closure(); |
| 191 SerializedScopeInfo* scope_info = current->shared()->scope_info(); |
| 192 if (scope_info != SerializedScopeInfo::Empty()) { |
| 193 scope = new Scope(scope, scope_info); |
| 194 if (innermost_scope == NULL) innermost_scope = scope; |
| 195 } else { |
| 196 ASSERT(current->context()->IsGlobalContext()); |
| 197 } |
| 198 } while (!current->context()->IsGlobalContext()); |
| 199 } |
| 200 |
| 201 global_scope->AddInnerScope(scope); |
| 202 if (innermost_scope == NULL) innermost_scope = global_scope; |
| 203 |
| 204 return innermost_scope; |
| 205 } |
| 206 |
| 178 | 207 |
| 179 bool Scope::Analyze(CompilationInfo* info) { | 208 bool Scope::Analyze(CompilationInfo* info) { |
| 180 ASSERT(info->function() != NULL); | 209 ASSERT(info->function() != NULL); |
| 181 Scope* top = info->function()->scope(); | 210 Scope* top = info->function()->scope(); |
| 182 | 211 |
| 183 // If we have a serialized scope info, reuse it. | |
| 184 if (!info->closure().is_null()) { | |
| 185 SerializedScopeInfo* scope_info = info->closure()->shared()->scope_info(); | |
| 186 if (scope_info != SerializedScopeInfo::Empty()) { | |
| 187 Scope* scope = top; | |
| 188 JSFunction* current = *info->closure(); | |
| 189 do { | |
| 190 current = current->context()->closure(); | |
| 191 SerializedScopeInfo* scope_info = current->shared()->scope_info(); | |
| 192 if (scope_info != SerializedScopeInfo::Empty()) { | |
| 193 scope = new Scope(scope, scope_info); | |
| 194 } else { | |
| 195 ASSERT(current->context()->IsGlobalContext()); | |
| 196 } | |
| 197 } while (!current->context()->IsGlobalContext()); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 while (top->outer_scope() != NULL) top = top->outer_scope(); | 212 while (top->outer_scope() != NULL) top = top->outer_scope(); |
| 202 top->AllocateVariables(info->calling_context()); | 213 top->AllocateVariables(info->calling_context()); |
| 203 | 214 |
| 204 #ifdef DEBUG | 215 #ifdef DEBUG |
| 205 if (Bootstrapper::IsActive() | 216 if (Bootstrapper::IsActive() |
| 206 ? FLAG_print_builtin_scopes | 217 ? FLAG_print_builtin_scopes |
| 207 : FLAG_print_scopes) { | 218 : FLAG_print_scopes) { |
| 208 info->function()->scope()->Print(); | 219 info->function()->scope()->Print(); |
| 209 } | 220 } |
| 210 #endif | 221 #endif |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 | 882 |
| 872 void Scope::AllocateHeapSlot(Variable* var) { | 883 void Scope::AllocateHeapSlot(Variable* var) { |
| 873 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++)); | 884 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++)); |
| 874 } | 885 } |
| 875 | 886 |
| 876 | 887 |
| 877 void Scope::AllocateParameterLocals() { | 888 void Scope::AllocateParameterLocals() { |
| 878 ASSERT(is_function_scope()); | 889 ASSERT(is_function_scope()); |
| 879 Variable* arguments = LocalLookup(Factory::arguments_symbol()); | 890 Variable* arguments = LocalLookup(Factory::arguments_symbol()); |
| 880 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly | 891 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly |
| 892 |
| 893 // Parameters are rewritten to arguments[i] if 'arguments' is used in |
| 894 // a non-strict mode function. Strict mode code doesn't alias arguments. |
| 895 bool rewrite_parameters = false; |
| 896 |
| 881 if (MustAllocate(arguments) && !HasArgumentsParameter()) { | 897 if (MustAllocate(arguments) && !HasArgumentsParameter()) { |
| 882 // 'arguments' is used. Unless there is also a parameter called | 898 // 'arguments' is used. Unless there is also a parameter called |
| 883 // 'arguments', we must be conservative and access all parameters via | 899 // 'arguments', we must be conservative and access all parameters via |
| 884 // the arguments object: The i'th parameter is rewritten into | 900 // the arguments object: The i'th parameter is rewritten into |
| 885 // '.arguments[i]' (*). If we have a parameter named 'arguments', a | 901 // '.arguments[i]' (*). If we have a parameter named 'arguments', a |
| 886 // (new) value is always assigned to it via the function | 902 // (new) value is always assigned to it via the function |
| 887 // invocation. Then 'arguments' denotes that specific parameter value | 903 // invocation. Then 'arguments' denotes that specific parameter value |
| 888 // and cannot be used to access the parameters, which is why we don't | 904 // and cannot be used to access the parameters, which is why we don't |
| 889 // need to rewrite in that case. | 905 // need to rewrite in that case. |
| 890 // | 906 // |
| (...skipping 11 matching lines...) Expand all Loading... |
| 902 // allocated. All parameters are rewritten into property accesses via | 918 // allocated. All parameters are rewritten into property accesses via |
| 903 // the '.arguments' variable. Thus, any changes to properties of | 919 // the '.arguments' variable. Thus, any changes to properties of |
| 904 // 'arguments' are reflected in the variables and vice versa. If the | 920 // 'arguments' are reflected in the variables and vice versa. If the |
| 905 // 'arguments' variable is changed, '.arguments' still points to the | 921 // 'arguments' variable is changed, '.arguments' still points to the |
| 906 // correct arguments object and the rewrites still work. | 922 // correct arguments object and the rewrites still work. |
| 907 | 923 |
| 908 // We are using 'arguments'. Tell the code generator that is needs to | 924 // We are using 'arguments'. Tell the code generator that is needs to |
| 909 // allocate the arguments object by setting 'arguments_'. | 925 // allocate the arguments object by setting 'arguments_'. |
| 910 arguments_ = arguments; | 926 arguments_ = arguments; |
| 911 | 927 |
| 928 // In strict mode 'arguments' does not alias formal parameters. |
| 929 // Therefore in strict mode we allocate parameters as if 'arguments' |
| 930 // were not used. |
| 931 rewrite_parameters = !is_strict_mode(); |
| 932 } |
| 933 |
| 934 if (rewrite_parameters) { |
| 912 // We also need the '.arguments' shadow variable. Declare it and create | 935 // We also need the '.arguments' shadow variable. Declare it and create |
| 913 // and bind the corresponding proxy. It's ok to declare it only now | 936 // and bind the corresponding proxy. It's ok to declare it only now |
| 914 // because it's a local variable that is allocated after the parameters | 937 // because it's a local variable that is allocated after the parameters |
| 915 // have been allocated. | 938 // have been allocated. |
| 916 // | 939 // |
| 917 // Note: This is "almost" at temporary variable but we cannot use | 940 // Note: This is "almost" at temporary variable but we cannot use |
| 918 // NewTemporary() because the mode needs to be INTERNAL since this | 941 // NewTemporary() because the mode needs to be INTERNAL since this |
| 919 // variable may be allocated in the heap-allocated context (temporaries | 942 // variable may be allocated in the heap-allocated context (temporaries |
| 920 // are never allocated in the context). | 943 // are never allocated in the context). |
| 921 arguments_shadow_ = new Variable(this, | 944 arguments_shadow_ = new Variable(this, |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1054 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && | 1077 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && |
| 1055 !must_have_local_context) { | 1078 !must_have_local_context) { |
| 1056 num_heap_slots_ = 0; | 1079 num_heap_slots_ = 0; |
| 1057 } | 1080 } |
| 1058 | 1081 |
| 1059 // Allocation done. | 1082 // Allocation done. |
| 1060 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); | 1083 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); |
| 1061 } | 1084 } |
| 1062 | 1085 |
| 1063 } } // namespace v8::internal | 1086 } } // namespace v8::internal |
| OLD | NEW |