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