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