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