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