karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 30 | #include "scopes.h" |
| 31 | |
| 32 | #include "bootstrapper.h" |
| 33 | #include "compiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | #include "scopeinfo.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 36 | #include "allocation-inl.h" |
| 37 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 38 | namespace v8 { |
| 39 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | // A Zone allocator for use with LocalsMap. |
| 43 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 44 | // TODO(isolates): It is probably worth it to change the Allocator class to |
| 45 | // take a pointer to an isolate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | class ZoneAllocator: public Allocator { |
| 47 | public: |
| 48 | /* nothing to do */ |
| 49 | virtual ~ZoneAllocator() {} |
| 50 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 51 | virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | |
| 53 | /* ignored - Zone is freed in one fell swoop */ |
| 54 | virtual void Delete(void* p) {} |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | static ZoneAllocator LocalsMapAllocator; |
| 59 | |
| 60 | |
| 61 | // ---------------------------------------------------------------------------- |
| 62 | // Implementation of LocalsMap |
| 63 | // |
| 64 | // Note: We are storing the handle locations as key values in the hash map. |
| 65 | // When inserting a new variable via Declare(), we rely on the fact that |
| 66 | // the handle location remains alive for the duration of that variable |
| 67 | // use. Because a Variable holding a handle with the same location exists |
| 68 | // this is ensured. |
| 69 | |
| 70 | static bool Match(void* key1, void* key2) { |
| 71 | String* name1 = *reinterpret_cast<String**>(key1); |
| 72 | String* name2 = *reinterpret_cast<String**>(key2); |
| 73 | ASSERT(name1->IsSymbol()); |
| 74 | ASSERT(name2->IsSymbol()); |
| 75 | return name1 == name2; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | // Dummy constructor |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 80 | VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 81 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 82 | VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {} |
| 83 | VariableMap::~VariableMap() {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 84 | |
| 85 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 86 | Variable* VariableMap::Declare(Scope* scope, |
| 87 | Handle<String> name, |
| 88 | Variable::Mode mode, |
| 89 | bool is_valid_lhs, |
| 90 | Variable::Kind kind) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true); |
| 92 | if (p->value == NULL) { |
| 93 | // The variable has not been declared yet -> insert it. |
| 94 | ASSERT(p->key == name.location()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 95 | p->value = new Variable(scope, name, mode, is_valid_lhs, kind); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 96 | } |
| 97 | return reinterpret_cast<Variable*>(p->value); |
| 98 | } |
| 99 | |
| 100 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 101 | Variable* VariableMap::Lookup(Handle<String> name) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 102 | HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false); |
| 103 | if (p != NULL) { |
| 104 | ASSERT(*reinterpret_cast<String**>(p->key) == *name); |
| 105 | ASSERT(p->value != NULL); |
| 106 | return reinterpret_cast<Variable*>(p->value); |
| 107 | } |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | // ---------------------------------------------------------------------------- |
| 113 | // Implementation of Scope |
| 114 | |
| 115 | |
| 116 | // Dummy constructor |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 117 | Scope::Scope(Type type) |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 118 | : isolate_(Isolate::Current()), |
| 119 | inner_scopes_(0), |
| 120 | variables_(false), |
| 121 | temps_(0), |
| 122 | params_(0), |
| 123 | unresolved_(0), |
| 124 | decls_(0), |
| 125 | already_resolved_(false) { |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 126 | SetDefaults(type, NULL, Handle<SerializedScopeInfo>::null()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
| 130 | Scope::Scope(Scope* outer_scope, Type type) |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 131 | : isolate_(Isolate::Current()), |
| 132 | inner_scopes_(4), |
| 133 | variables_(), |
| 134 | temps_(4), |
| 135 | params_(4), |
| 136 | unresolved_(16), |
| 137 | decls_(4), |
| 138 | already_resolved_(false) { |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 139 | SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | // At some point we might want to provide outer scopes to |
| 141 | // eval scopes (by walking the stack and reading the scope info). |
| 142 | // In that case, the ASSERT below needs to be adjusted. |
| 143 | ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); |
| 144 | ASSERT(!HasIllegalRedeclaration()); |
| 145 | } |
| 146 | |
| 147 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 148 | Scope::Scope(Scope* inner_scope, |
| 149 | Type type, |
| 150 | Handle<SerializedScopeInfo> scope_info) |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 151 | : isolate_(Isolate::Current()), |
| 152 | inner_scopes_(4), |
| 153 | variables_(), |
| 154 | temps_(4), |
| 155 | params_(4), |
| 156 | unresolved_(16), |
| 157 | decls_(4), |
| 158 | already_resolved_(true) { |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 159 | ASSERT(!scope_info.is_null()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 160 | SetDefaults(type, NULL, scope_info); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 161 | if (scope_info->HasHeapAllocatedLocals()) { |
| 162 | num_heap_slots_ = scope_info_->NumberOfContextSlots(); |
| 163 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 164 | AddInnerScope(inner_scope); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 168 | Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name) |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 169 | : isolate_(Isolate::Current()), |
| 170 | inner_scopes_(1), |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 171 | variables_(), |
| 172 | temps_(0), |
| 173 | params_(0), |
| 174 | unresolved_(0), |
| 175 | decls_(0), |
| 176 | already_resolved_(true) { |
| 177 | SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null()); |
| 178 | AddInnerScope(inner_scope); |
| 179 | ++num_var_or_const_; |
| 180 | Variable* variable = variables_.Declare(this, |
| 181 | catch_variable_name, |
| 182 | Variable::VAR, |
| 183 | true, // Valid left-hand side. |
| 184 | Variable::NORMAL); |
| 185 | AllocateHeapSlot(variable); |
| 186 | } |
| 187 | |
| 188 | |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 189 | void Scope::SetDefaults(Type type, |
| 190 | Scope* outer_scope, |
| 191 | Handle<SerializedScopeInfo> scope_info) { |
| 192 | outer_scope_ = outer_scope; |
| 193 | type_ = type; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 194 | scope_name_ = isolate_->factory()->empty_symbol(); |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 195 | dynamics_ = NULL; |
| 196 | receiver_ = NULL; |
| 197 | function_ = NULL; |
| 198 | arguments_ = NULL; |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 199 | illegal_redecl_ = NULL; |
| 200 | scope_inside_with_ = false; |
| 201 | scope_contains_with_ = false; |
| 202 | scope_calls_eval_ = false; |
| 203 | // Inherit the strict mode from the parent scope. |
| 204 | strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_; |
| 205 | outer_scope_calls_eval_ = false; |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 206 | outer_scope_calls_non_strict_eval_ = false; |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 207 | inner_scope_calls_eval_ = false; |
| 208 | outer_scope_is_eval_scope_ = false; |
| 209 | force_eager_compilation_ = false; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 210 | num_var_or_const_ = 0; |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 211 | num_stack_slots_ = 0; |
| 212 | num_heap_slots_ = 0; |
| 213 | scope_info_ = scope_info; |
| 214 | } |
| 215 | |
| 216 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 217 | Scope* Scope::DeserializeScopeChain(CompilationInfo* info, |
| 218 | Scope* global_scope) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 219 | // Reconstruct the outer scope chain from a closure's context chain. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 220 | ASSERT(!info->closure().is_null()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 221 | Context* context = info->closure()->context(); |
| 222 | Scope* current_scope = NULL; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 223 | Scope* innermost_scope = NULL; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 224 | bool contains_with = false; |
| 225 | while (!context->IsGlobalContext()) { |
| 226 | if (context->IsWithContext()) { |
| 227 | // All the inner scopes are inside a with. |
| 228 | contains_with = true; |
| 229 | for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) { |
| 230 | s->scope_inside_with_ = true; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 231 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 232 | } else { |
| 233 | if (context->IsFunctionContext()) { |
| 234 | SerializedScopeInfo* scope_info = |
| 235 | context->closure()->shared()->scope_info(); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 236 | current_scope = new Scope(current_scope, FUNCTION_SCOPE, |
| 237 | Handle<SerializedScopeInfo>(scope_info)); |
| 238 | } else if (context->IsBlockContext()) { |
| 239 | SerializedScopeInfo* scope_info = |
| 240 | SerializedScopeInfo::cast(context->extension()); |
| 241 | current_scope = new Scope(current_scope, BLOCK_SCOPE, |
| 242 | Handle<SerializedScopeInfo>(scope_info)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 243 | } else { |
| 244 | ASSERT(context->IsCatchContext()); |
| 245 | String* name = String::cast(context->extension()); |
| 246 | current_scope = new Scope(current_scope, Handle<String>(name)); |
| 247 | } |
| 248 | if (contains_with) current_scope->RecordWithStatement(); |
| 249 | if (innermost_scope == NULL) innermost_scope = current_scope; |
| 250 | } |
| 251 | |
| 252 | // Forget about a with when we move to a context for a different function. |
| 253 | if (context->previous()->closure() != context->closure()) { |
| 254 | contains_with = false; |
| 255 | } |
| 256 | context = context->previous(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 257 | } |
| 258 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 259 | global_scope->AddInnerScope(current_scope); |
| 260 | return (innermost_scope == NULL) ? global_scope : innermost_scope; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 261 | } |
| 262 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 263 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 264 | bool Scope::Analyze(CompilationInfo* info) { |
| 265 | ASSERT(info->function() != NULL); |
| 266 | Scope* top = info->function()->scope(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 267 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 268 | while (top->outer_scope() != NULL) top = top->outer_scope(); |
| 269 | top->AllocateVariables(info->calling_context()); |
| 270 | |
| 271 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 272 | if (info->isolate()->bootstrapper()->IsActive() |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 273 | ? FLAG_print_builtin_scopes |
| 274 | : FLAG_print_scopes) { |
| 275 | info->function()->scope()->Print(); |
| 276 | } |
| 277 | #endif |
| 278 | |
| 279 | info->SetScope(info->function()->scope()); |
| 280 | return true; // Can not fail. |
| 281 | } |
| 282 | |
| 283 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | void Scope::Initialize(bool inside_with) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 285 | ASSERT(!already_resolved()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 286 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 287 | // Add this scope as a new inner scope of the outer scope. |
| 288 | if (outer_scope_ != NULL) { |
| 289 | outer_scope_->inner_scopes_.Add(this); |
| 290 | scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with; |
| 291 | } else { |
| 292 | scope_inside_with_ = inside_with; |
| 293 | } |
| 294 | |
| 295 | // Declare convenience variables. |
| 296 | // Declare and allocate receiver (even for the global scope, and even |
| 297 | // if naccesses_ == 0). |
| 298 | // NOTE: When loading parameters in the global scope, we must take |
| 299 | // care not to access them as properties of the global object, but |
| 300 | // instead load them directly from the stack. Currently, the only |
| 301 | // such parameter is 'this' which is passed on the stack when |
| 302 | // invoking scripts |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 303 | if (is_catch_scope() || is_block_scope()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 304 | ASSERT(outer_scope() != NULL); |
| 305 | receiver_ = outer_scope()->receiver(); |
| 306 | } else { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 307 | ASSERT(is_function_scope() || |
| 308 | is_global_scope() || |
| 309 | is_eval_scope()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 310 | Variable* var = |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 311 | variables_.Declare(this, |
| 312 | isolate_->factory()->this_symbol(), |
| 313 | Variable::VAR, |
| 314 | false, |
| 315 | Variable::THIS); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 316 | var->AllocateTo(Variable::PARAMETER, -1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 317 | receiver_ = var; |
| 318 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 319 | |
| 320 | if (is_function_scope()) { |
| 321 | // Declare 'arguments' variable which exists in all functions. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 322 | // Note that it might never be accessed, in which case it won't be |
| 323 | // allocated during variable allocation. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 324 | variables_.Declare(this, |
| 325 | isolate_->factory()->arguments_symbol(), |
| 326 | Variable::VAR, |
| 327 | true, |
| 328 | Variable::ARGUMENTS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 333 | Scope* Scope::FinalizeBlockScope() { |
| 334 | ASSERT(is_block_scope()); |
| 335 | ASSERT(temps_.is_empty()); |
| 336 | ASSERT(params_.is_empty()); |
| 337 | |
| 338 | if (num_var_or_const() > 0) return this; |
| 339 | |
| 340 | // Remove this scope from outer scope. |
| 341 | for (int i = 0; i < outer_scope_->inner_scopes_.length(); i++) { |
| 342 | if (outer_scope_->inner_scopes_[i] == this) { |
| 343 | outer_scope_->inner_scopes_.Remove(i); |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // Reparent inner scopes. |
| 349 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 350 | outer_scope()->AddInnerScope(inner_scopes_[i]); |
| 351 | } |
| 352 | |
| 353 | // Move unresolved variables |
| 354 | for (int i = 0; i < unresolved_.length(); i++) { |
| 355 | outer_scope()->unresolved_.Add(unresolved_[i]); |
| 356 | } |
| 357 | |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 362 | Variable* Scope::LocalLookup(Handle<String> name) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 363 | Variable* result = variables_.Lookup(name); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 364 | if (result != NULL || scope_info_.is_null()) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 365 | return result; |
| 366 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 367 | // If we have a serialized scope info, we might find the variable there. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 368 | // |
| 369 | // We should never lookup 'arguments' in this scope as it is implicitly |
| 370 | // present in every scope. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 371 | ASSERT(*name != *isolate_->factory()->arguments_symbol()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 372 | // There should be no local slot with the given name. |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 373 | ASSERT(scope_info_->StackSlotIndex(*name) < 0); |
| 374 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 375 | // Check context slot lookup. |
| 376 | Variable::Mode mode; |
| 377 | int index = scope_info_->ContextSlotIndex(*name, &mode); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 378 | if (index < 0) { |
| 379 | // Check parameters. |
| 380 | mode = Variable::VAR; |
| 381 | index = scope_info_->ParameterIndex(*name); |
| 382 | if (index < 0) { |
| 383 | // Check the function name. |
| 384 | index = scope_info_->FunctionContextSlotIndex(*name); |
| 385 | if (index < 0) return NULL; |
| 386 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 387 | } |
| 388 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 389 | Variable* var = |
| 390 | variables_.Declare(this, name, mode, true, Variable::NORMAL); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 391 | var->AllocateTo(Variable::CONTEXT, index); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 392 | return var; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 396 | Variable* Scope::Lookup(Handle<String> name) { |
| 397 | for (Scope* scope = this; |
| 398 | scope != NULL; |
| 399 | scope = scope->outer_scope()) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 400 | Variable* var = scope->LocalLookup(name); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 401 | if (var != NULL) return var; |
| 402 | } |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 407 | Variable* Scope::DeclareFunctionVar(Handle<String> name) { |
| 408 | ASSERT(is_function_scope() && function_ == NULL); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 409 | Variable* function_var = |
| 410 | new Variable(this, name, Variable::CONST, true, Variable::NORMAL); |
| 411 | function_ = new(isolate_->zone()) VariableProxy(isolate_, function_var); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 412 | return function_var; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 416 | void Scope::DeclareParameter(Handle<String> name, Variable::Mode mode) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 417 | ASSERT(!already_resolved()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 418 | ASSERT(is_function_scope()); |
| 419 | Variable* var = |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 420 | variables_.Declare(this, name, mode, true, Variable::NORMAL); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 421 | params_.Add(var); |
| 422 | } |
| 423 | |
| 424 | |
| 425 | Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 426 | ASSERT(!already_resolved()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 427 | // This function handles VAR and CONST modes. DYNAMIC variables are |
| 428 | // introduces during variable allocation, INTERNAL variables are allocated |
| 429 | // explicitly, and TEMPORARY variables are allocated via NewTemporary(). |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 430 | ASSERT(mode == Variable::VAR || |
| 431 | mode == Variable::CONST || |
| 432 | mode == Variable::LET); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 433 | ++num_var_or_const_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 434 | return variables_.Declare(this, name, mode, true, Variable::NORMAL); |
| 435 | } |
| 436 | |
| 437 | |
| 438 | Variable* Scope::DeclareGlobal(Handle<String> name) { |
| 439 | ASSERT(is_global_scope()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 440 | return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, |
| 441 | true, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 442 | Variable::NORMAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 446 | VariableProxy* Scope::NewUnresolved(Handle<String> name, |
| 447 | bool inside_with, |
| 448 | int position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 449 | // Note that we must not share the unresolved variables with |
| 450 | // the same name because they may be removed selectively via |
| 451 | // RemoveUnresolved(). |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 452 | ASSERT(!already_resolved()); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 453 | VariableProxy* proxy = new(isolate_->zone()) VariableProxy( |
| 454 | isolate_, name, false, inside_with, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 455 | unresolved_.Add(proxy); |
| 456 | return proxy; |
| 457 | } |
| 458 | |
| 459 | |
| 460 | void Scope::RemoveUnresolved(VariableProxy* var) { |
| 461 | // Most likely (always?) any variable we want to remove |
| 462 | // was just added before, so we search backwards. |
| 463 | for (int i = unresolved_.length(); i-- > 0;) { |
| 464 | if (unresolved_[i] == var) { |
| 465 | unresolved_.Remove(i); |
| 466 | return; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 472 | Variable* Scope::NewTemporary(Handle<String> name) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 473 | ASSERT(!already_resolved()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 474 | Variable* var = new Variable(this, |
| 475 | name, |
| 476 | Variable::TEMPORARY, |
| 477 | true, |
| 478 | Variable::NORMAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 479 | temps_.Add(var); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 480 | return var; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | |
| 484 | void Scope::AddDeclaration(Declaration* declaration) { |
| 485 | decls_.Add(declaration); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | void Scope::SetIllegalRedeclaration(Expression* expression) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 490 | // Record only the first illegal redeclaration. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 491 | if (!HasIllegalRedeclaration()) { |
| 492 | illegal_redecl_ = expression; |
| 493 | } |
| 494 | ASSERT(HasIllegalRedeclaration()); |
| 495 | } |
| 496 | |
| 497 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 498 | void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 499 | ASSERT(HasIllegalRedeclaration()); |
| 500 | illegal_redecl_->Accept(visitor); |
| 501 | } |
| 502 | |
| 503 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 504 | Declaration* Scope::CheckConflictingVarDeclarations() { |
| 505 | int length = decls_.length(); |
| 506 | for (int i = 0; i < length; i++) { |
| 507 | Declaration* decl = decls_[i]; |
| 508 | if (decl->mode() != Variable::VAR) continue; |
| 509 | Handle<String> name = decl->proxy()->name(); |
| 510 | bool cond = true; |
| 511 | for (Scope* scope = decl->scope(); cond ; scope = scope->outer_scope_) { |
| 512 | // There is a conflict if there exists a non-VAR binding. |
| 513 | Variable* other_var = scope->variables_.Lookup(name); |
| 514 | if (other_var != NULL && other_var->mode() != Variable::VAR) { |
| 515 | return decl; |
| 516 | } |
| 517 | |
| 518 | // Include declaration scope in the iteration but stop after. |
| 519 | if (!scope->is_block_scope() && !scope->is_catch_scope()) cond = false; |
| 520 | } |
| 521 | } |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 526 | template<class Allocator> |
| 527 | void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) { |
| 528 | // Collect variables in this scope. |
| 529 | // Note that the function_ variable - if present - is not |
| 530 | // collected here but handled separately in ScopeInfo |
| 531 | // which is the current user of this function). |
| 532 | for (int i = 0; i < temps_.length(); i++) { |
| 533 | Variable* var = temps_[i]; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 534 | if (var->is_used()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 535 | locals->Add(var); |
| 536 | } |
| 537 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 538 | for (VariableMap::Entry* p = variables_.Start(); |
| 539 | p != NULL; |
| 540 | p = variables_.Next(p)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 541 | Variable* var = reinterpret_cast<Variable*>(p->value); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 542 | if (var->is_used()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 543 | locals->Add(var); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | |
| 549 | // Make sure the method gets instantiated by the template system. |
| 550 | template void Scope::CollectUsedVariables( |
| 551 | List<Variable*, FreeStoreAllocationPolicy>* locals); |
| 552 | template void Scope::CollectUsedVariables( |
| 553 | List<Variable*, PreallocatedStorage>* locals); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 554 | template void Scope::CollectUsedVariables( |
| 555 | List<Variable*, ZoneListAllocationPolicy>* locals); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 556 | |
| 557 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 558 | void Scope::AllocateVariables(Handle<Context> context) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | ASSERT(outer_scope_ == NULL); // eval or global scopes only |
| 560 | |
| 561 | // 1) Propagate scope information. |
| 562 | // If we are in an eval scope, we may have other outer scopes about |
| 563 | // which we don't know anything at this point. Thus we must be conservative |
| 564 | // and assume they may invoke eval themselves. Eventually we could capture |
| 565 | // this information in the ScopeInfo and then use it here (by traversing |
| 566 | // the call chain stack, at compile time). |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 567 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 568 | bool eval_scope = is_eval_scope(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 569 | bool outer_scope_calls_eval = false; |
| 570 | bool outer_scope_calls_non_strict_eval = false; |
| 571 | if (!is_global_scope()) { |
| 572 | context->ComputeEvalScopeInfo(&outer_scope_calls_eval, |
| 573 | &outer_scope_calls_non_strict_eval); |
| 574 | } |
| 575 | PropagateScopeInfo(outer_scope_calls_eval, |
| 576 | outer_scope_calls_non_strict_eval, |
| 577 | eval_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 578 | |
| 579 | // 2) Resolve variables. |
| 580 | Scope* global_scope = NULL; |
| 581 | if (is_global_scope()) global_scope = this; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 582 | ResolveVariablesRecursively(global_scope, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 583 | |
| 584 | // 3) Allocate variables. |
| 585 | AllocateVariablesRecursively(); |
| 586 | } |
| 587 | |
| 588 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 589 | bool Scope::AllowsLazyCompilation() const { |
| 590 | return !force_eager_compilation_ && HasTrivialOuterContext(); |
| 591 | } |
| 592 | |
| 593 | |
| 594 | bool Scope::HasTrivialContext() const { |
| 595 | // A function scope has a trivial context if it always is the global |
| 596 | // context. We iteratively scan out the context chain to see if |
| 597 | // there is anything that makes this scope non-trivial; otherwise we |
| 598 | // return true. |
| 599 | for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) { |
| 600 | if (scope->is_eval_scope()) return false; |
| 601 | if (scope->scope_inside_with_) return false; |
| 602 | if (scope->num_heap_slots_ > 0) return false; |
| 603 | } |
| 604 | return true; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | bool Scope::HasTrivialOuterContext() const { |
| 609 | Scope* outer = outer_scope_; |
| 610 | if (outer == NULL) return true; |
| 611 | // Note that the outer context may be trivial in general, but the current |
| 612 | // scope may be inside a 'with' statement in which case the outer context |
| 613 | // for this scope is not trivial. |
| 614 | return !scope_inside_with_ && outer->HasTrivialContext(); |
| 615 | } |
| 616 | |
| 617 | |
| 618 | int Scope::ContextChainLength(Scope* scope) { |
| 619 | int n = 0; |
| 620 | for (Scope* s = this; s != scope; s = s->outer_scope_) { |
| 621 | ASSERT(s != NULL); // scope must be in the scope chain |
| 622 | if (s->num_heap_slots() > 0) n++; |
| 623 | } |
| 624 | return n; |
| 625 | } |
| 626 | |
| 627 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 628 | Scope* Scope::DeclarationScope() { |
| 629 | Scope* scope = this; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 630 | while (scope->is_catch_scope() || |
| 631 | scope->is_block_scope()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 632 | scope = scope->outer_scope(); |
| 633 | } |
| 634 | return scope; |
| 635 | } |
| 636 | |
| 637 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 638 | Handle<SerializedScopeInfo> Scope::GetSerializedScopeInfo() { |
| 639 | if (scope_info_.is_null()) { |
| 640 | scope_info_ = SerializedScopeInfo::Create(this); |
| 641 | } |
| 642 | return scope_info_; |
| 643 | } |
| 644 | |
| 645 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 646 | #ifdef DEBUG |
| 647 | static const char* Header(Scope::Type type) { |
| 648 | switch (type) { |
| 649 | case Scope::EVAL_SCOPE: return "eval"; |
| 650 | case Scope::FUNCTION_SCOPE: return "function"; |
| 651 | case Scope::GLOBAL_SCOPE: return "global"; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 652 | case Scope::CATCH_SCOPE: return "catch"; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 653 | case Scope::BLOCK_SCOPE: return "block"; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 654 | } |
| 655 | UNREACHABLE(); |
| 656 | return NULL; |
| 657 | } |
| 658 | |
| 659 | |
| 660 | static void Indent(int n, const char* str) { |
| 661 | PrintF("%*s%s", n, "", str); |
| 662 | } |
| 663 | |
| 664 | |
| 665 | static void PrintName(Handle<String> name) { |
| 666 | SmartPointer<char> s = name->ToCString(DISALLOW_NULLS); |
| 667 | PrintF("%s", *s); |
| 668 | } |
| 669 | |
| 670 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 671 | static void PrintLocation(Variable* var) { |
| 672 | switch (var->location()) { |
| 673 | case Variable::UNALLOCATED: |
| 674 | break; |
| 675 | case Variable::PARAMETER: |
| 676 | PrintF("parameter[%d]", var->index()); |
| 677 | break; |
| 678 | case Variable::LOCAL: |
| 679 | PrintF("local[%d]", var->index()); |
| 680 | break; |
| 681 | case Variable::CONTEXT: |
| 682 | PrintF("context[%d]", var->index()); |
| 683 | break; |
| 684 | case Variable::LOOKUP: |
| 685 | PrintF("lookup"); |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | |
| 691 | static void PrintVar(int indent, Variable* var) { |
| 692 | if (var->is_used() || !var->IsUnallocated()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 693 | Indent(indent, Variable::Mode2String(var->mode())); |
| 694 | PrintF(" "); |
| 695 | PrintName(var->name()); |
| 696 | PrintF("; // "); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 697 | PrintLocation(var); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 698 | if (var->is_accessed_from_inner_function_scope()) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 699 | if (!var->IsUnallocated()) PrintF(", "); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 700 | PrintF("inner scope access"); |
| 701 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | PrintF("\n"); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 707 | static void PrintMap(int indent, VariableMap* map) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 708 | for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 709 | Variable* var = reinterpret_cast<Variable*>(p->value); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 710 | PrintVar(indent, var); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
| 714 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 715 | void Scope::Print(int n) { |
| 716 | int n0 = (n > 0 ? n : 0); |
| 717 | int n1 = n0 + 2; // indentation |
| 718 | |
| 719 | // Print header. |
| 720 | Indent(n0, Header(type_)); |
| 721 | if (scope_name_->length() > 0) { |
| 722 | PrintF(" "); |
| 723 | PrintName(scope_name_); |
| 724 | } |
| 725 | |
| 726 | // Print parameters, if any. |
| 727 | if (is_function_scope()) { |
| 728 | PrintF(" ("); |
| 729 | for (int i = 0; i < params_.length(); i++) { |
| 730 | if (i > 0) PrintF(", "); |
| 731 | PrintName(params_[i]->name()); |
| 732 | } |
| 733 | PrintF(")"); |
| 734 | } |
| 735 | |
| 736 | PrintF(" {\n"); |
| 737 | |
| 738 | // Function name, if any (named function literals, only). |
| 739 | if (function_ != NULL) { |
| 740 | Indent(n1, "// (local) function name: "); |
| 741 | PrintName(function_->name()); |
| 742 | PrintF("\n"); |
| 743 | } |
| 744 | |
| 745 | // Scope info. |
| 746 | if (HasTrivialOuterContext()) { |
| 747 | Indent(n1, "// scope has trivial outer context\n"); |
| 748 | } |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 749 | if (is_strict_mode()) Indent(n1, "// strict mode scope\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 750 | if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); |
| 751 | if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); |
| 752 | if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); |
| 753 | if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n"); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 754 | if (outer_scope_calls_non_strict_eval_) { |
| 755 | Indent(n1, "// outer scope calls 'eval' in non-strict context\n"); |
| 756 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 757 | if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 758 | if (outer_scope_is_eval_scope_) { |
| 759 | Indent(n1, "// outer scope is 'eval' scope\n"); |
| 760 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 761 | if (num_stack_slots_ > 0) { Indent(n1, "// "); |
| 762 | PrintF("%d stack slots\n", num_stack_slots_); } |
| 763 | if (num_heap_slots_ > 0) { Indent(n1, "// "); |
| 764 | PrintF("%d heap slots\n", num_heap_slots_); } |
| 765 | |
| 766 | // Print locals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 767 | Indent(n1, "// function var\n"); |
| 768 | if (function_ != NULL) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 769 | PrintVar(n1, function_->var()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | Indent(n1, "// temporary vars\n"); |
| 773 | for (int i = 0; i < temps_.length(); i++) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 774 | PrintVar(n1, temps_[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | Indent(n1, "// local vars\n"); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 778 | PrintMap(n1, &variables_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 779 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 780 | Indent(n1, "// dynamic vars\n"); |
| 781 | if (dynamics_ != NULL) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 782 | PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC)); |
| 783 | PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL)); |
| 784 | PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 785 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 786 | |
| 787 | // Print inner scopes (disable by providing negative n). |
| 788 | if (n >= 0) { |
| 789 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 790 | PrintF("\n"); |
| 791 | inner_scopes_[i]->Print(n1); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | Indent(n0, "}\n"); |
| 796 | } |
| 797 | #endif // DEBUG |
| 798 | |
| 799 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 800 | Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 801 | if (dynamics_ == NULL) dynamics_ = new DynamicScopePart(); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 802 | VariableMap* map = dynamics_->GetMap(mode); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 803 | Variable* var = map->Lookup(name); |
| 804 | if (var == NULL) { |
| 805 | // Declare a new non-local. |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 806 | var = map->Declare(NULL, name, mode, true, Variable::NORMAL); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 807 | // Allocate it by giving it a dynamic lookup. |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 808 | var->AllocateTo(Variable::LOOKUP, -1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 809 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 810 | return var; |
| 811 | } |
| 812 | |
| 813 | |
| 814 | // Lookup a variable starting with this scope. The result is either |
ager@chromium.org | fe22fc4 | 2009-11-05 11:55:54 +0000 | [diff] [blame] | 815 | // the statically resolved variable belonging to an outer scope, or |
| 816 | // NULL. It may be NULL because a) we couldn't find a variable, or b) |
| 817 | // because the variable is just a guess (and may be shadowed by |
| 818 | // another variable that is introduced dynamically via an 'eval' call |
| 819 | // or a 'with' statement). |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 820 | Variable* Scope::LookupRecursive(Handle<String> name, |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 821 | bool from_inner_function, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 822 | Variable** invalidated_local) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 823 | // If we find a variable, but the current scope calls 'eval', the found |
| 824 | // variable may not be the correct one (the 'eval' may introduce a |
| 825 | // property with the same name). In that case, remember that the variable |
| 826 | // found is just a guess. |
| 827 | bool guess = scope_calls_eval_; |
| 828 | |
| 829 | // Try to find the variable in this scope. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 830 | Variable* var = LocalLookup(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 831 | |
| 832 | if (var != NULL) { |
| 833 | // We found a variable. If this is not an inner lookup, we are done. |
| 834 | // (Even if there is an 'eval' in this scope which introduces the |
| 835 | // same variable again, the resulting variable remains the same. |
| 836 | // Note that enclosing 'with' statements are handled at the call site.) |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 837 | if (!from_inner_function) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 838 | return var; |
| 839 | |
| 840 | } else { |
| 841 | // We did not find a variable locally. Check against the function variable, |
| 842 | // if any. We can do this for all scopes, since the function variable is |
| 843 | // only present - if at all - for function scopes. |
| 844 | // |
| 845 | // This lookup corresponds to a lookup in the "intermediate" scope sitting |
| 846 | // between this scope and the outer scope. (ECMA-262, 3rd., requires that |
| 847 | // the name of named function literal is kept in an intermediate scope |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 848 | // in between this scope and the next outer scope.) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 849 | if (function_ != NULL && function_->name().is_identical_to(name)) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 850 | var = function_->var(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 851 | |
| 852 | } else if (outer_scope_ != NULL) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 853 | var = outer_scope_->LookupRecursive( |
| 854 | name, |
| 855 | is_function_scope() || from_inner_function, |
| 856 | invalidated_local); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | // We may have found a variable in an outer scope. However, if |
| 858 | // the current scope is inside a 'with', the actual variable may |
| 859 | // be a property introduced via the 'with' statement. Then, the |
| 860 | // variable we may have found is just a guess. |
| 861 | if (scope_inside_with_) |
| 862 | guess = true; |
| 863 | } |
| 864 | |
| 865 | // If we did not find a variable, we are done. |
| 866 | if (var == NULL) |
| 867 | return NULL; |
| 868 | } |
| 869 | |
| 870 | ASSERT(var != NULL); |
| 871 | |
| 872 | // If this is a lookup from an inner scope, mark the variable. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 873 | if (from_inner_function) { |
| 874 | var->MarkAsAccessedFromInnerFunctionScope(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 875 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 876 | |
ager@chromium.org | fe22fc4 | 2009-11-05 11:55:54 +0000 | [diff] [blame] | 877 | // If the variable we have found is just a guess, invalidate the |
| 878 | // result. If the found variable is local, record that fact so we |
| 879 | // can generate fast code to get it if it is not shadowed by eval. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 880 | if (guess) { |
ager@chromium.org | fe22fc4 | 2009-11-05 11:55:54 +0000 | [diff] [blame] | 881 | if (!var->is_global()) *invalidated_local = var; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 882 | var = NULL; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 883 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 884 | |
| 885 | return var; |
| 886 | } |
| 887 | |
| 888 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 889 | void Scope::ResolveVariable(Scope* global_scope, |
| 890 | Handle<Context> context, |
| 891 | VariableProxy* proxy) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 892 | ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
| 893 | |
| 894 | // If the proxy is already resolved there's nothing to do |
| 895 | // (functions and consts may be resolved by the parser). |
| 896 | if (proxy->var() != NULL) return; |
| 897 | |
| 898 | // Otherwise, try to resolve the variable. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 899 | Variable* invalidated_local = NULL; |
| 900 | Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 901 | |
| 902 | if (proxy->inside_with()) { |
| 903 | // If we are inside a local 'with' statement, all bets are off |
| 904 | // and we cannot resolve the proxy to a local variable even if |
| 905 | // we found an outer matching variable. |
| 906 | // Note that we must do a lookup anyway, because if we find one, |
| 907 | // we must mark that variable as potentially accessed from this |
| 908 | // inner scope (the property may not be in the 'with' object). |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 909 | var = NonLocal(proxy->name(), Variable::DYNAMIC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 910 | |
| 911 | } else { |
| 912 | // We are not inside a local 'with' statement. |
| 913 | |
| 914 | if (var == NULL) { |
| 915 | // We did not find the variable. We have a global variable |
| 916 | // if we are in the global scope (we know already that we |
| 917 | // are outside a 'with' statement) or if there is no way |
| 918 | // that the variable might be introduced dynamically (through |
| 919 | // a local or outer eval() call, or an outer 'with' statement), |
| 920 | // or we don't know about the outer scope (because we are |
| 921 | // in an eval scope). |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 922 | if (is_global_scope() || |
| 923 | !(scope_inside_with_ || outer_scope_is_eval_scope_ || |
| 924 | scope_calls_eval_ || outer_scope_calls_eval_)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 925 | // We must have a global variable. |
| 926 | ASSERT(global_scope != NULL); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 927 | var = global_scope->DeclareGlobal(proxy->name()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 928 | |
| 929 | } else if (scope_inside_with_) { |
| 930 | // If we are inside a with statement we give up and look up |
| 931 | // the variable at runtime. |
| 932 | var = NonLocal(proxy->name(), Variable::DYNAMIC); |
| 933 | |
| 934 | } else if (invalidated_local != NULL) { |
| 935 | // No with statements are involved and we found a local |
| 936 | // variable that might be shadowed by eval introduced |
| 937 | // variables. |
| 938 | var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL); |
| 939 | var->set_local_if_not_shadowed(invalidated_local); |
| 940 | |
| 941 | } else if (outer_scope_is_eval_scope_) { |
| 942 | // No with statements and we did not find a local and the code |
| 943 | // is executed with a call to eval. The context contains |
| 944 | // scope information that we can use to determine if the |
| 945 | // variable is global if it is not shadowed by eval-introduced |
| 946 | // variables. |
| 947 | if (context->GlobalIfNotShadowedByEval(proxy->name())) { |
| 948 | var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); |
| 949 | |
| 950 | } else { |
| 951 | var = NonLocal(proxy->name(), Variable::DYNAMIC); |
| 952 | } |
| 953 | |
| 954 | } else { |
| 955 | // No with statements and we did not find a local and the code |
| 956 | // is not executed with a call to eval. We know that this |
| 957 | // variable is global unless it is shadowed by eval-introduced |
| 958 | // variables. |
| 959 | var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | proxy->BindTo(var); |
| 965 | } |
| 966 | |
| 967 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 968 | void Scope::ResolveVariablesRecursively(Scope* global_scope, |
| 969 | Handle<Context> context) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 970 | ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
| 971 | |
| 972 | // Resolve unresolved variables for this scope. |
| 973 | for (int i = 0; i < unresolved_.length(); i++) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 974 | ResolveVariable(global_scope, context, unresolved_[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | // Resolve unresolved variables for inner scopes. |
| 978 | for (int i = 0; i < inner_scopes_.length(); i++) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 979 | inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
| 983 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 984 | bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval, |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 985 | bool outer_scope_calls_non_strict_eval, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 986 | bool outer_scope_is_eval_scope) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 987 | if (outer_scope_calls_eval) { |
| 988 | outer_scope_calls_eval_ = true; |
| 989 | } |
| 990 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 991 | if (outer_scope_calls_non_strict_eval) { |
| 992 | outer_scope_calls_non_strict_eval_ = true; |
| 993 | } |
| 994 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 995 | if (outer_scope_is_eval_scope) { |
| 996 | outer_scope_is_eval_scope_ = true; |
| 997 | } |
| 998 | |
| 999 | bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_; |
| 1000 | bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_; |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 1001 | bool calls_non_strict_eval = |
| 1002 | (scope_calls_eval_ && !is_strict_mode()) || |
| 1003 | outer_scope_calls_non_strict_eval_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1004 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 1005 | Scope* inner_scope = inner_scopes_[i]; |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 1006 | if (inner_scope->PropagateScopeInfo(calls_eval, |
| 1007 | calls_non_strict_eval, |
| 1008 | is_eval)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1009 | inner_scope_calls_eval_ = true; |
| 1010 | } |
| 1011 | if (inner_scope->force_eager_compilation_) { |
| 1012 | force_eager_compilation_ = true; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | return scope_calls_eval_ || inner_scope_calls_eval_; |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | bool Scope::MustAllocate(Variable* var) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1021 | // Give var a read/write use if there is a chance it might be accessed |
| 1022 | // via an eval() call. This is only possible if the variable has a |
| 1023 | // visible name. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1024 | if ((var->is_this() || var->name()->length() > 0) && |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1025 | (var->is_accessed_from_inner_function_scope() || |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1026 | scope_calls_eval_ || |
| 1027 | inner_scope_calls_eval_ || |
| 1028 | scope_contains_with_ || |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1029 | is_catch_scope() || |
| 1030 | is_block_scope())) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1031 | var->set_is_used(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1032 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1033 | // Global variables do not need to be allocated. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1034 | return !var->is_global() && var->is_used(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | bool Scope::MustAllocateInContext(Variable* var) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1039 | // If var is accessed from an inner scope, or if there is a possibility |
| 1040 | // that it might be accessed from the current or an inner scope (through |
| 1041 | // an eval() call or a runtime with lookup), it must be allocated in the |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1042 | // context. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1043 | // |
| 1044 | // Exceptions: temporary variables are never allocated in a context; |
| 1045 | // catch-bound variables are always allocated in a context. |
| 1046 | if (var->mode() == Variable::TEMPORARY) return false; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1047 | if (is_catch_scope() || is_block_scope()) return true; |
| 1048 | return var->is_accessed_from_inner_function_scope() || |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1049 | scope_calls_eval_ || |
| 1050 | inner_scope_calls_eval_ || |
| 1051 | scope_contains_with_ || |
| 1052 | var->is_global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | |
| 1056 | bool Scope::HasArgumentsParameter() { |
| 1057 | for (int i = 0; i < params_.length(); i++) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1058 | if (params_[i]->name().is_identical_to( |
| 1059 | isolate_->factory()->arguments_symbol())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1060 | return true; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1061 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1062 | } |
| 1063 | return false; |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | void Scope::AllocateStackSlot(Variable* var) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 1068 | var->AllocateTo(Variable::LOCAL, num_stack_slots_++); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | void Scope::AllocateHeapSlot(Variable* var) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 1073 | var->AllocateTo(Variable::CONTEXT, num_heap_slots_++); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | |
| 1077 | void Scope::AllocateParameterLocals() { |
| 1078 | ASSERT(is_function_scope()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1079 | Variable* arguments = LocalLookup(isolate_->factory()->arguments_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1080 | ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1081 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1082 | bool uses_nonstrict_arguments = false; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1083 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1084 | if (MustAllocate(arguments) && !HasArgumentsParameter()) { |
| 1085 | // 'arguments' is used. Unless there is also a parameter called |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1086 | // 'arguments', we must be conservative and allocate all parameters to |
| 1087 | // the context assuming they will be captured by the arguments object. |
| 1088 | // If we have a parameter named 'arguments', a (new) value is always |
| 1089 | // assigned to it via the function invocation. Then 'arguments' denotes |
| 1090 | // that specific parameter value and cannot be used to access the |
| 1091 | // parameters, which is why we don't need to allocate an arguments |
| 1092 | // object in that case. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1093 | |
| 1094 | // We are using 'arguments'. Tell the code generator that is needs to |
| 1095 | // allocate the arguments object by setting 'arguments_'. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1096 | arguments_ = arguments; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1097 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1098 | // In strict mode 'arguments' does not alias formal parameters. |
| 1099 | // Therefore in strict mode we allocate parameters as if 'arguments' |
| 1100 | // were not used. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1101 | uses_nonstrict_arguments = !is_strict_mode(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1104 | // The same parameter may occur multiple times in the parameters_ list. |
| 1105 | // If it does, and if it is not copied into the context object, it must |
| 1106 | // receive the highest parameter index for that parameter; thus iteration |
| 1107 | // order is relevant! |
| 1108 | for (int i = params_.length() - 1; i >= 0; --i) { |
| 1109 | Variable* var = params_[i]; |
| 1110 | ASSERT(var->scope() == this); |
| 1111 | if (uses_nonstrict_arguments) { |
| 1112 | // Give the parameter a use from an inner scope, to force allocation |
| 1113 | // to the context. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1114 | var->MarkAsAccessedFromInnerFunctionScope(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1117 | if (MustAllocate(var)) { |
| 1118 | if (MustAllocateInContext(var)) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 1119 | ASSERT(var->IsUnallocated() || var->IsContextSlot()); |
| 1120 | if (var->IsUnallocated()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1121 | AllocateHeapSlot(var); |
| 1122 | } |
| 1123 | } else { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 1124 | ASSERT(var->IsUnallocated() || var->IsParameter()); |
| 1125 | if (var->IsUnallocated()) { |
| 1126 | var->AllocateTo(Variable::PARAMETER, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | void Scope::AllocateNonParameterLocal(Variable* var) { |
| 1135 | ASSERT(var->scope() == this); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame^] | 1136 | ASSERT(!var->IsVariable(isolate_->factory()->result_symbol()) || |
| 1137 | !var->IsStackLocal()); |
| 1138 | if (var->IsUnallocated() && MustAllocate(var)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1139 | if (MustAllocateInContext(var)) { |
| 1140 | AllocateHeapSlot(var); |
| 1141 | } else { |
| 1142 | AllocateStackSlot(var); |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1148 | void Scope::AllocateNonParameterLocals() { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1149 | // All variables that have no rewrite yet are non-parameter locals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1150 | for (int i = 0; i < temps_.length(); i++) { |
| 1151 | AllocateNonParameterLocal(temps_[i]); |
| 1152 | } |
| 1153 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1154 | for (VariableMap::Entry* p = variables_.Start(); |
| 1155 | p != NULL; |
| 1156 | p = variables_.Next(p)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1157 | Variable* var = reinterpret_cast<Variable*>(p->value); |
| 1158 | AllocateNonParameterLocal(var); |
| 1159 | } |
| 1160 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1161 | // For now, function_ must be allocated at the very end. If it gets |
| 1162 | // allocated in the context, it must be the last slot in the context, |
| 1163 | // because of the current ScopeInfo implementation (see |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1164 | // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor). |
| 1165 | if (function_ != NULL) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1166 | AllocateNonParameterLocal(function_->var()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | |
| 1171 | void Scope::AllocateVariablesRecursively() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1172 | // Allocate variables for inner scopes. |
| 1173 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 1174 | inner_scopes_[i]->AllocateVariablesRecursively(); |
| 1175 | } |
| 1176 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1177 | // If scope is already resolved, we still need to allocate |
| 1178 | // variables in inner scopes which might not had been resolved yet. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1179 | if (already_resolved()) return; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1180 | // The number of slots required for variables. |
| 1181 | num_stack_slots_ = 0; |
| 1182 | num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
| 1183 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1184 | // Allocate variables for this scope. |
| 1185 | // Parameters must be allocated first, if any. |
| 1186 | if (is_function_scope()) AllocateParameterLocals(); |
| 1187 | AllocateNonParameterLocals(); |
| 1188 | |
| 1189 | // Allocate context if necessary. |
| 1190 | bool must_have_local_context = false; |
| 1191 | if (scope_calls_eval_ || scope_contains_with_) { |
| 1192 | // The context for the eval() call or 'with' statement in this scope. |
| 1193 | // Unless we are in the global or an eval scope, we need a local |
| 1194 | // context even if we didn't statically allocate any locals in it, |
| 1195 | // and the compiler will access the context variable. If we are |
| 1196 | // not in an inner scope, the scope is provided from the outside. |
| 1197 | must_have_local_context = is_function_scope(); |
| 1198 | } |
| 1199 | |
| 1200 | // If we didn't allocate any locals in the local context, then we only |
| 1201 | // need the minimal number of slots if we must have a local context. |
| 1202 | if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && |
| 1203 | !must_have_local_context) { |
| 1204 | num_heap_slots_ = 0; |
| 1205 | } |
| 1206 | |
| 1207 | // Allocation done. |
| 1208 | ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); |
| 1209 | } |
| 1210 | |
| 1211 | } } // namespace v8::internal |