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