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