blob: fd573b03b8bd72cc924aaf24970d4693f31c6b52 [file] [log] [blame]
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001// Copyright 2010 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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.orgb61a0d12010-10-13 08:35:23 +000030#include "scopes.h"
31
32#include "bootstrapper.h"
33#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034#include "prettyprinter.h"
35#include "scopeinfo.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
kasperl@chromium.org71affb52009-05-26 05:44:31 +000037namespace v8 {
38namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
40// ----------------------------------------------------------------------------
41// A Zone allocator for use with LocalsMap.
42
43class ZoneAllocator: public Allocator {
44 public:
45 /* nothing to do */
46 virtual ~ZoneAllocator() {}
47
ager@chromium.orgc4c92722009-11-18 14:12:51 +000048 virtual void* New(size_t size) { return Zone::New(static_cast<int>(size)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049
50 /* ignored - Zone is freed in one fell swoop */
51 virtual void Delete(void* p) {}
52};
53
54
55static ZoneAllocator LocalsMapAllocator;
56
57
58// ----------------------------------------------------------------------------
59// Implementation of LocalsMap
60//
61// Note: We are storing the handle locations as key values in the hash map.
62// When inserting a new variable via Declare(), we rely on the fact that
63// the handle location remains alive for the duration of that variable
64// use. Because a Variable holding a handle with the same location exists
65// this is ensured.
66
67static bool Match(void* key1, void* key2) {
68 String* name1 = *reinterpret_cast<String**>(key1);
69 String* name2 = *reinterpret_cast<String**>(key2);
70 ASSERT(name1->IsSymbol());
71 ASSERT(name2->IsSymbol());
72 return name1 == name2;
73}
74
75
76// Dummy constructor
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000077VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000079VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {}
80VariableMap::~VariableMap() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081
82
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000083Variable* VariableMap::Declare(Scope* scope,
84 Handle<String> name,
85 Variable::Mode mode,
86 bool is_valid_lhs,
87 Variable::Kind kind) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true);
89 if (p->value == NULL) {
90 // The variable has not been declared yet -> insert it.
91 ASSERT(p->key == name.location());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000092 p->value = new Variable(scope, name, mode, is_valid_lhs, kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 }
94 return reinterpret_cast<Variable*>(p->value);
95}
96
97
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000098Variable* VariableMap::Lookup(Handle<String> name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false);
100 if (p != NULL) {
101 ASSERT(*reinterpret_cast<String**>(p->key) == *name);
102 ASSERT(p->value != NULL);
103 return reinterpret_cast<Variable*>(p->value);
104 }
105 return NULL;
106}
107
108
109// ----------------------------------------------------------------------------
110// Implementation of Scope
111
112
113// Dummy constructor
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000114Scope::Scope(Type type)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000115 : inner_scopes_(0),
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000116 variables_(false),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 temps_(0),
118 params_(0),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 unresolved_(0),
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000120 decls_(0) {
121 SetDefaults(type, NULL, NULL);
122 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123}
124
125
126Scope::Scope(Scope* outer_scope, Type type)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000127 : inner_scopes_(4),
128 variables_(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129 temps_(4),
130 params_(4),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 unresolved_(16),
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000132 decls_(4) {
133 SetDefaults(type, outer_scope, NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 // At some point we might want to provide outer scopes to
135 // eval scopes (by walking the stack and reading the scope info).
136 // In that case, the ASSERT below needs to be adjusted.
137 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL));
138 ASSERT(!HasIllegalRedeclaration());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000139 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140}
141
142
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000143Scope::Scope(Scope* inner_scope, SerializedScopeInfo* scope_info)
144 : inner_scopes_(4),
145 variables_(),
146 temps_(4),
147 params_(4),
148 unresolved_(16),
149 decls_(4) {
150 ASSERT(scope_info != NULL);
151 SetDefaults(FUNCTION_SCOPE, inner_scope->outer_scope(), scope_info);
152 ASSERT(resolved());
153 InsertAfterScope(inner_scope);
154 if (scope_info->HasHeapAllocatedLocals()) {
155 num_heap_slots_ = scope_info_->NumberOfContextSlots();
156 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000157
158 // This scope's arguments shadow (if present) is context-allocated if an inner
159 // scope accesses this one's parameters. Allocate the arguments_shadow_
160 // variable if necessary.
161 Variable::Mode mode;
162 int arguments_shadow_index =
163 scope_info_->ContextSlotIndex(Heap::arguments_shadow_symbol(), &mode);
164 if (arguments_shadow_index >= 0) {
165 ASSERT(mode == Variable::INTERNAL);
166 arguments_shadow_ = new Variable(this,
167 Factory::arguments_shadow_symbol(),
168 Variable::INTERNAL,
169 true,
170 Variable::ARGUMENTS);
171 arguments_shadow_->set_rewrite(
172 new Slot(arguments_shadow_, Slot::CONTEXT, arguments_shadow_index));
173 arguments_shadow_->set_is_used(true);
174 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000175}
176
177
178
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000179bool Scope::Analyze(CompilationInfo* info) {
180 ASSERT(info->function() != NULL);
181 Scope* top = info->function()->scope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000182
183 // If we have a serialized scope info, reuse it.
184 if (!info->closure().is_null()) {
185 SerializedScopeInfo* scope_info = info->closure()->shared()->scope_info();
186 if (scope_info != SerializedScopeInfo::Empty()) {
187 Scope* scope = top;
188 JSFunction* current = *info->closure();
189 do {
190 current = current->context()->closure();
191 SerializedScopeInfo* scope_info = current->shared()->scope_info();
192 if (scope_info != SerializedScopeInfo::Empty()) {
193 scope = new Scope(scope, scope_info);
194 } else {
195 ASSERT(current->context()->IsGlobalContext());
196 }
197 } while (!current->context()->IsGlobalContext());
198 }
199 }
200
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000201 while (top->outer_scope() != NULL) top = top->outer_scope();
202 top->AllocateVariables(info->calling_context());
203
204#ifdef DEBUG
205 if (Bootstrapper::IsActive()
206 ? FLAG_print_builtin_scopes
207 : FLAG_print_scopes) {
208 info->function()->scope()->Print();
209 }
210#endif
211
212 info->SetScope(info->function()->scope());
213 return true; // Can not fail.
214}
215
216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217void Scope::Initialize(bool inside_with) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000218 ASSERT(!resolved());
219
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220 // Add this scope as a new inner scope of the outer scope.
221 if (outer_scope_ != NULL) {
222 outer_scope_->inner_scopes_.Add(this);
223 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
224 } else {
225 scope_inside_with_ = inside_with;
226 }
227
228 // Declare convenience variables.
229 // Declare and allocate receiver (even for the global scope, and even
230 // if naccesses_ == 0).
231 // NOTE: When loading parameters in the global scope, we must take
232 // care not to access them as properties of the global object, but
233 // instead load them directly from the stack. Currently, the only
234 // such parameter is 'this' which is passed on the stack when
235 // invoking scripts
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000236 Variable* var =
237 variables_.Declare(this, Factory::this_symbol(), Variable::VAR,
238 false, Variable::THIS);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000239 var->set_rewrite(new Slot(var, Slot::PARAMETER, -1));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000240 receiver_ = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241
242 if (is_function_scope()) {
243 // Declare 'arguments' variable which exists in all functions.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000244 // Note that it might never be accessed, in which case it won't be
245 // allocated during variable allocation.
246 variables_.Declare(this, Factory::arguments_symbol(), Variable::VAR,
247 true, Variable::ARGUMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 }
249}
250
251
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000252Variable* Scope::LocalLookup(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000253 Variable* result = variables_.Lookup(name);
254 if (result != NULL || !resolved()) {
255 return result;
256 }
257 // If the scope is resolved, we can find a variable in serialized scope info.
258
259 // We should never lookup 'arguments' in this scope
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000260 // as it is implicitly present in any scope.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000261 ASSERT(*name != *Factory::arguments_symbol());
262
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000263 // Assert that there is no local slot with the given name.
264 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
265
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000266 // Check context slot lookup.
267 Variable::Mode mode;
268 int index = scope_info_->ContextSlotIndex(*name, &mode);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000269 if (index >= 0) {
270 Variable* var =
271 variables_.Declare(this, name, mode, true, Variable::NORMAL);
272 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
273 return var;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000274 }
275
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000276 index = scope_info_->ParameterIndex(*name);
277 if (index >= 0) {
278 // ".arguments" must be present in context slots.
279 ASSERT(arguments_shadow_ != NULL);
280 Variable* var =
281 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
282 Property* rewrite =
283 new Property(new VariableProxy(arguments_shadow_),
284 new Literal(Handle<Object>(Smi::FromInt(index))),
285 RelocInfo::kNoPosition,
286 Property::SYNTHETIC);
287 rewrite->set_is_arguments_access(true);
288 var->set_rewrite(rewrite);
289 return var;
290 }
291
292 index = scope_info_->FunctionContextSlotIndex(*name);
293 if (index >= 0) {
294 // Check that there is no local slot with the given name.
295 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
296 Variable* var =
297 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
298 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
299 return var;
300 }
301
302 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303}
304
305
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000306Variable* Scope::Lookup(Handle<String> name) {
307 for (Scope* scope = this;
308 scope != NULL;
309 scope = scope->outer_scope()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000310 Variable* var = scope->LocalLookup(name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000311 if (var != NULL) return var;
312 }
313 return NULL;
314}
315
316
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317Variable* Scope::DeclareFunctionVar(Handle<String> name) {
318 ASSERT(is_function_scope() && function_ == NULL);
ager@chromium.org3e875802009-06-29 08:26:34 +0000319 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 return function_;
321}
322
323
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000324Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 // DYNAMIC variables are introduces during variable allocation,
326 // INTERNAL variables are allocated explicitly, and TEMPORARY
327 // variables are allocated via NewTemporary().
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000328 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000330 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
331}
332
333
334Variable* Scope::DeclareGlobal(Handle<String> name) {
335 ASSERT(is_global_scope());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000336 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000337 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338}
339
340
341void Scope::AddParameter(Variable* var) {
342 ASSERT(is_function_scope());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000343 ASSERT(LocalLookup(var->name()) == var);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 params_.Add(var);
345}
346
347
348VariableProxy* Scope::NewUnresolved(Handle<String> name, bool inside_with) {
349 // Note that we must not share the unresolved variables with
350 // the same name because they may be removed selectively via
351 // RemoveUnresolved().
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000352 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 VariableProxy* proxy = new VariableProxy(name, false, inside_with);
354 unresolved_.Add(proxy);
355 return proxy;
356}
357
358
359void Scope::RemoveUnresolved(VariableProxy* var) {
360 // Most likely (always?) any variable we want to remove
361 // was just added before, so we search backwards.
362 for (int i = unresolved_.length(); i-- > 0;) {
363 if (unresolved_[i] == var) {
364 unresolved_.Remove(i);
365 return;
366 }
367 }
368}
369
370
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000371Variable* Scope::NewTemporary(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000372 ASSERT(!resolved());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000373 Variable* var =
374 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000376 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377}
378
379
380void Scope::AddDeclaration(Declaration* declaration) {
381 decls_.Add(declaration);
382}
383
384
385void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000386 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 if (!HasIllegalRedeclaration()) {
388 illegal_redecl_ = expression;
389 }
390 ASSERT(HasIllegalRedeclaration());
391}
392
393
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000394void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395 ASSERT(HasIllegalRedeclaration());
396 illegal_redecl_->Accept(visitor);
397}
398
399
400template<class Allocator>
401void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
402 // Collect variables in this scope.
403 // Note that the function_ variable - if present - is not
404 // collected here but handled separately in ScopeInfo
405 // which is the current user of this function).
406 for (int i = 0; i < temps_.length(); i++) {
407 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000408 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409 locals->Add(var);
410 }
411 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000412 for (VariableMap::Entry* p = variables_.Start();
413 p != NULL;
414 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000416 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417 locals->Add(var);
418 }
419 }
420}
421
422
423// Make sure the method gets instantiated by the template system.
424template void Scope::CollectUsedVariables(
425 List<Variable*, FreeStoreAllocationPolicy>* locals);
426template void Scope::CollectUsedVariables(
427 List<Variable*, PreallocatedStorage>* locals);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000428template void Scope::CollectUsedVariables(
429 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430
431
ager@chromium.org381abbb2009-02-25 13:23:22 +0000432void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 ASSERT(outer_scope_ == NULL); // eval or global scopes only
434
435 // 1) Propagate scope information.
436 // If we are in an eval scope, we may have other outer scopes about
437 // which we don't know anything at this point. Thus we must be conservative
438 // and assume they may invoke eval themselves. Eventually we could capture
439 // this information in the ScopeInfo and then use it here (by traversing
440 // the call chain stack, at compile time).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000441 bool eval_scope = is_eval_scope();
442 PropagateScopeInfo(eval_scope, eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443
444 // 2) Resolve variables.
445 Scope* global_scope = NULL;
446 if (is_global_scope()) global_scope = this;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000447 ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448
449 // 3) Allocate variables.
450 AllocateVariablesRecursively();
451}
452
453
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454bool Scope::AllowsLazyCompilation() const {
455 return !force_eager_compilation_ && HasTrivialOuterContext();
456}
457
458
459bool Scope::HasTrivialContext() const {
460 // A function scope has a trivial context if it always is the global
461 // context. We iteratively scan out the context chain to see if
462 // there is anything that makes this scope non-trivial; otherwise we
463 // return true.
464 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
465 if (scope->is_eval_scope()) return false;
466 if (scope->scope_inside_with_) return false;
467 if (scope->num_heap_slots_ > 0) return false;
468 }
469 return true;
470}
471
472
473bool Scope::HasTrivialOuterContext() const {
474 Scope* outer = outer_scope_;
475 if (outer == NULL) return true;
476 // Note that the outer context may be trivial in general, but the current
477 // scope may be inside a 'with' statement in which case the outer context
478 // for this scope is not trivial.
479 return !scope_inside_with_ && outer->HasTrivialContext();
480}
481
482
483int Scope::ContextChainLength(Scope* scope) {
484 int n = 0;
485 for (Scope* s = this; s != scope; s = s->outer_scope_) {
486 ASSERT(s != NULL); // scope must be in the scope chain
487 if (s->num_heap_slots() > 0) n++;
488 }
489 return n;
490}
491
492
493#ifdef DEBUG
494static const char* Header(Scope::Type type) {
495 switch (type) {
496 case Scope::EVAL_SCOPE: return "eval";
497 case Scope::FUNCTION_SCOPE: return "function";
498 case Scope::GLOBAL_SCOPE: return "global";
499 }
500 UNREACHABLE();
501 return NULL;
502}
503
504
505static void Indent(int n, const char* str) {
506 PrintF("%*s%s", n, "", str);
507}
508
509
510static void PrintName(Handle<String> name) {
511 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
512 PrintF("%s", *s);
513}
514
515
516static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000517 if (var->is_used() || var->rewrite() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 Indent(indent, Variable::Mode2String(var->mode()));
519 PrintF(" ");
520 PrintName(var->name());
521 PrintF("; // ");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000522 if (var->rewrite() != NULL) {
523 PrintF("%s, ", printer->Print(var->rewrite()));
524 if (var->is_accessed_from_inner_scope()) PrintF(", ");
525 }
526 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 PrintF("\n");
528 }
529}
530
531
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000532static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
533 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000534 Variable* var = reinterpret_cast<Variable*>(p->value);
535 PrintVar(printer, indent, var);
536 }
537}
538
539
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540void Scope::Print(int n) {
541 int n0 = (n > 0 ? n : 0);
542 int n1 = n0 + 2; // indentation
543
544 // Print header.
545 Indent(n0, Header(type_));
546 if (scope_name_->length() > 0) {
547 PrintF(" ");
548 PrintName(scope_name_);
549 }
550
551 // Print parameters, if any.
552 if (is_function_scope()) {
553 PrintF(" (");
554 for (int i = 0; i < params_.length(); i++) {
555 if (i > 0) PrintF(", ");
556 PrintName(params_[i]->name());
557 }
558 PrintF(")");
559 }
560
561 PrintF(" {\n");
562
563 // Function name, if any (named function literals, only).
564 if (function_ != NULL) {
565 Indent(n1, "// (local) function name: ");
566 PrintName(function_->name());
567 PrintF("\n");
568 }
569
570 // Scope info.
571 if (HasTrivialOuterContext()) {
572 Indent(n1, "// scope has trivial outer context\n");
573 }
574 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
575 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
576 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
577 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
578 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000579 if (outer_scope_is_eval_scope_) {
580 Indent(n1, "// outer scope is 'eval' scope\n");
581 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 if (num_stack_slots_ > 0) { Indent(n1, "// ");
583 PrintF("%d stack slots\n", num_stack_slots_); }
584 if (num_heap_slots_ > 0) { Indent(n1, "// ");
585 PrintF("%d heap slots\n", num_heap_slots_); }
586
587 // Print locals.
588 PrettyPrinter printer;
589 Indent(n1, "// function var\n");
590 if (function_ != NULL) {
591 PrintVar(&printer, n1, function_);
592 }
593
594 Indent(n1, "// temporary vars\n");
595 for (int i = 0; i < temps_.length(); i++) {
596 PrintVar(&printer, n1, temps_[i]);
597 }
598
599 Indent(n1, "// local vars\n");
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000600 PrintMap(&printer, n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000602 Indent(n1, "// dynamic vars\n");
603 if (dynamics_ != NULL) {
604 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
605 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
606 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
607 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608
609 // Print inner scopes (disable by providing negative n).
610 if (n >= 0) {
611 for (int i = 0; i < inner_scopes_.length(); i++) {
612 PrintF("\n");
613 inner_scopes_[i]->Print(n1);
614 }
615 }
616
617 Indent(n0, "}\n");
618}
619#endif // DEBUG
620
621
ager@chromium.org381abbb2009-02-25 13:23:22 +0000622Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000623 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000624 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000625 Variable* var = map->Lookup(name);
626 if (var == NULL) {
627 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000628 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000629 // Allocate it by giving it a dynamic lookup.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000630 var->set_rewrite(new Slot(var, Slot::LOOKUP, -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 return var;
633}
634
635
636// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000637// the statically resolved variable belonging to an outer scope, or
638// NULL. It may be NULL because a) we couldn't find a variable, or b)
639// because the variable is just a guess (and may be shadowed by
640// another variable that is introduced dynamically via an 'eval' call
641// or a 'with' statement).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000642Variable* Scope::LookupRecursive(Handle<String> name,
643 bool inner_lookup,
644 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645 // If we find a variable, but the current scope calls 'eval', the found
646 // variable may not be the correct one (the 'eval' may introduce a
647 // property with the same name). In that case, remember that the variable
648 // found is just a guess.
649 bool guess = scope_calls_eval_;
650
651 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000652 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653
654 if (var != NULL) {
655 // We found a variable. If this is not an inner lookup, we are done.
656 // (Even if there is an 'eval' in this scope which introduces the
657 // same variable again, the resulting variable remains the same.
658 // Note that enclosing 'with' statements are handled at the call site.)
659 if (!inner_lookup)
660 return var;
661
662 } else {
663 // We did not find a variable locally. Check against the function variable,
664 // if any. We can do this for all scopes, since the function variable is
665 // only present - if at all - for function scopes.
666 //
667 // This lookup corresponds to a lookup in the "intermediate" scope sitting
668 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
669 // the name of named function literal is kept in an intermediate scope
ager@chromium.org32912102009-01-16 10:38:43 +0000670 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 if (function_ != NULL && function_->name().is_identical_to(name)) {
672 var = function_;
673
674 } else if (outer_scope_ != NULL) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000675 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676 // We may have found a variable in an outer scope. However, if
677 // the current scope is inside a 'with', the actual variable may
678 // be a property introduced via the 'with' statement. Then, the
679 // variable we may have found is just a guess.
680 if (scope_inside_with_)
681 guess = true;
682 }
683
684 // If we did not find a variable, we are done.
685 if (var == NULL)
686 return NULL;
687 }
688
689 ASSERT(var != NULL);
690
691 // If this is a lookup from an inner scope, mark the variable.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000692 if (inner_lookup) {
693 var->MarkAsAccessedFromInnerScope();
694 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000696 // If the variable we have found is just a guess, invalidate the
697 // result. If the found variable is local, record that fact so we
698 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000699 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000700 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000702 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703
704 return var;
705}
706
707
ager@chromium.org381abbb2009-02-25 13:23:22 +0000708void Scope::ResolveVariable(Scope* global_scope,
709 Handle<Context> context,
710 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 ASSERT(global_scope == NULL || global_scope->is_global_scope());
712
713 // If the proxy is already resolved there's nothing to do
714 // (functions and consts may be resolved by the parser).
715 if (proxy->var() != NULL) return;
716
717 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000718 Variable* invalidated_local = NULL;
719 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720
721 if (proxy->inside_with()) {
722 // If we are inside a local 'with' statement, all bets are off
723 // and we cannot resolve the proxy to a local variable even if
724 // we found an outer matching variable.
725 // Note that we must do a lookup anyway, because if we find one,
726 // we must mark that variable as potentially accessed from this
727 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000728 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729
730 } else {
731 // We are not inside a local 'with' statement.
732
733 if (var == NULL) {
734 // We did not find the variable. We have a global variable
735 // if we are in the global scope (we know already that we
736 // are outside a 'with' statement) or if there is no way
737 // that the variable might be introduced dynamically (through
738 // a local or outer eval() call, or an outer 'with' statement),
739 // or we don't know about the outer scope (because we are
740 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000741 if (is_global_scope() ||
742 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
743 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 // We must have a global variable.
745 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000746 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000747
748 } else if (scope_inside_with_) {
749 // If we are inside a with statement we give up and look up
750 // the variable at runtime.
751 var = NonLocal(proxy->name(), Variable::DYNAMIC);
752
753 } else if (invalidated_local != NULL) {
754 // No with statements are involved and we found a local
755 // variable that might be shadowed by eval introduced
756 // variables.
757 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
758 var->set_local_if_not_shadowed(invalidated_local);
759
760 } else if (outer_scope_is_eval_scope_) {
761 // No with statements and we did not find a local and the code
762 // is executed with a call to eval. The context contains
763 // scope information that we can use to determine if the
764 // variable is global if it is not shadowed by eval-introduced
765 // variables.
766 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
767 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
768
769 } else {
770 var = NonLocal(proxy->name(), Variable::DYNAMIC);
771 }
772
773 } else {
774 // No with statements and we did not find a local and the code
775 // is not executed with a call to eval. We know that this
776 // variable is global unless it is shadowed by eval-introduced
777 // variables.
778 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 }
780 }
781 }
782
783 proxy->BindTo(var);
784}
785
786
ager@chromium.org381abbb2009-02-25 13:23:22 +0000787void Scope::ResolveVariablesRecursively(Scope* global_scope,
788 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789 ASSERT(global_scope == NULL || global_scope->is_global_scope());
790
791 // Resolve unresolved variables for this scope.
792 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000793 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 }
795
796 // Resolve unresolved variables for inner scopes.
797 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000798 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799 }
800}
801
802
ager@chromium.org381abbb2009-02-25 13:23:22 +0000803bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
804 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000805 if (outer_scope_calls_eval) {
806 outer_scope_calls_eval_ = true;
807 }
808
ager@chromium.org381abbb2009-02-25 13:23:22 +0000809 if (outer_scope_is_eval_scope) {
810 outer_scope_is_eval_scope_ = true;
811 }
812
813 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
814 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 for (int i = 0; i < inner_scopes_.length(); i++) {
816 Scope* inner_scope = inner_scopes_[i];
ager@chromium.org381abbb2009-02-25 13:23:22 +0000817 if (inner_scope->PropagateScopeInfo(calls_eval, is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 inner_scope_calls_eval_ = true;
819 }
820 if (inner_scope->force_eager_compilation_) {
821 force_eager_compilation_ = true;
822 }
823 }
824
825 return scope_calls_eval_ || inner_scope_calls_eval_;
826}
827
828
829bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000830 // Give var a read/write use if there is a chance it might be accessed
831 // via an eval() call. This is only possible if the variable has a
832 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833 if ((var->is_this() || var->name()->length() > 0) &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000834 (var->is_accessed_from_inner_scope() ||
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000835 scope_calls_eval_ || inner_scope_calls_eval_ ||
836 scope_contains_with_)) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000837 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000839 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000840 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841}
842
843
844bool Scope::MustAllocateInContext(Variable* var) {
845 // If var is accessed from an inner scope, or if there is a
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000846 // possibility that it might be accessed from the current or an inner
847 // scope (through an eval() call), it must be allocated in the
848 // context. Exception: temporary variables are not allocated in the
849 // context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850 return
851 var->mode() != Variable::TEMPORARY &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000852 (var->is_accessed_from_inner_scope() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853 scope_calls_eval_ || inner_scope_calls_eval_ ||
854 scope_contains_with_ || var->is_global());
855}
856
857
858bool Scope::HasArgumentsParameter() {
859 for (int i = 0; i < params_.length(); i++) {
860 if (params_[i]->name().is_identical_to(Factory::arguments_symbol()))
861 return true;
862 }
863 return false;
864}
865
866
867void Scope::AllocateStackSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000868 var->set_rewrite(new Slot(var, Slot::LOCAL, num_stack_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869}
870
871
872void Scope::AllocateHeapSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000873 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874}
875
876
877void Scope::AllocateParameterLocals() {
878 ASSERT(is_function_scope());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000879 Variable* arguments = LocalLookup(Factory::arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
881 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
882 // 'arguments' is used. Unless there is also a parameter called
883 // 'arguments', we must be conservative and access all parameters via
884 // the arguments object: The i'th parameter is rewritten into
885 // '.arguments[i]' (*). If we have a parameter named 'arguments', a
886 // (new) value is always assigned to it via the function
887 // invocation. Then 'arguments' denotes that specific parameter value
888 // and cannot be used to access the parameters, which is why we don't
889 // need to rewrite in that case.
890 //
891 // (*) Instead of having a parameter called 'arguments', we may have an
892 // assignment to 'arguments' in the function body, at some arbitrary
893 // point in time (possibly through an 'eval()' call!). After that
894 // assignment any re-write of parameters would be invalid (was bug
895 // 881452). Thus, we introduce a shadow '.arguments'
896 // variable which also points to the arguments object. For rewrites we
897 // use '.arguments' which remains valid even if we assign to
898 // 'arguments'. To summarize: If we need to rewrite, we allocate an
899 // 'arguments' object dynamically upon function invocation. The compiler
900 // introduces 2 local variables 'arguments' and '.arguments', both of
901 // which originally point to the arguments object that was
902 // allocated. All parameters are rewritten into property accesses via
903 // the '.arguments' variable. Thus, any changes to properties of
904 // 'arguments' are reflected in the variables and vice versa. If the
905 // 'arguments' variable is changed, '.arguments' still points to the
906 // correct arguments object and the rewrites still work.
907
908 // We are using 'arguments'. Tell the code generator that is needs to
909 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000910 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911
912 // We also need the '.arguments' shadow variable. Declare it and create
913 // and bind the corresponding proxy. It's ok to declare it only now
914 // because it's a local variable that is allocated after the parameters
915 // have been allocated.
916 //
917 // Note: This is "almost" at temporary variable but we cannot use
918 // NewTemporary() because the mode needs to be INTERNAL since this
919 // variable may be allocated in the heap-allocated context (temporaries
920 // are never allocated in the context).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000921 arguments_shadow_ = new Variable(this,
922 Factory::arguments_shadow_symbol(),
923 Variable::INTERNAL,
924 true,
925 Variable::ARGUMENTS);
926 arguments_shadow_->set_is_used(true);
927 temps_.Add(arguments_shadow_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
929 // Allocate the parameters by rewriting them into '.arguments[i]' accesses.
930 for (int i = 0; i < params_.length(); i++) {
931 Variable* var = params_[i];
932 ASSERT(var->scope() == this);
933 if (MustAllocate(var)) {
934 if (MustAllocateInContext(var)) {
935 // It is ok to set this only now, because arguments is a local
936 // variable that is allocated after the parameters have been
937 // allocated.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000938 arguments_shadow_->MarkAsAccessedFromInnerScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000940 Property* rewrite =
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000941 new Property(new VariableProxy(arguments_shadow_),
942 new Literal(Handle<Object>(Smi::FromInt(i))),
943 RelocInfo::kNoPosition,
944 Property::SYNTHETIC);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000945 rewrite->set_is_arguments_access(true);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000946 var->set_rewrite(rewrite);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947 }
948 }
949
950 } else {
951 // The arguments object is not used, so we can access parameters directly.
952 // The same parameter may occur multiple times in the parameters_ list.
953 // If it does, and if it is not copied into the context object, it must
954 // receive the highest parameter index for that parameter; thus iteration
955 // order is relevant!
956 for (int i = 0; i < params_.length(); i++) {
957 Variable* var = params_[i];
958 ASSERT(var->scope() == this);
959 if (MustAllocate(var)) {
960 if (MustAllocateInContext(var)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000961 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000962 (var->AsSlot() != NULL &&
963 var->AsSlot()->type() == Slot::CONTEXT));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000964 if (var->rewrite() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965 // Only set the heap allocation if the parameter has not
966 // been allocated yet.
967 AllocateHeapSlot(var);
968 }
969 } else {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000970 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000971 (var->AsSlot() != NULL &&
972 var->AsSlot()->type() == Slot::PARAMETER));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 // Set the parameter index always, even if the parameter
974 // was seen before! (We need to access the actual parameter
975 // supplied for the last occurrence of a multiply declared
976 // parameter.)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000977 var->set_rewrite(new Slot(var, Slot::PARAMETER, i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 }
979 }
980 }
981 }
982}
983
984
985void Scope::AllocateNonParameterLocal(Variable* var) {
986 ASSERT(var->scope() == this);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000987 ASSERT(var->rewrite() == NULL ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 (!var->IsVariable(Factory::result_symbol())) ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000989 (var->AsSlot() == NULL || var->AsSlot()->type() != Slot::LOCAL));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000990 if (var->rewrite() == NULL && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 if (MustAllocateInContext(var)) {
992 AllocateHeapSlot(var);
993 } else {
994 AllocateStackSlot(var);
995 }
996 }
997}
998
999
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001001 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002 for (int i = 0; i < temps_.length(); i++) {
1003 AllocateNonParameterLocal(temps_[i]);
1004 }
1005
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001006 for (VariableMap::Entry* p = variables_.Start();
1007 p != NULL;
1008 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 Variable* var = reinterpret_cast<Variable*>(p->value);
1010 AllocateNonParameterLocal(var);
1011 }
1012
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001013 // For now, function_ must be allocated at the very end. If it gets
1014 // allocated in the context, it must be the last slot in the context,
1015 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1017 if (function_ != NULL) {
1018 AllocateNonParameterLocal(function_);
1019 }
1020}
1021
1022
1023void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 // Allocate variables for inner scopes.
1025 for (int i = 0; i < inner_scopes_.length(); i++) {
1026 inner_scopes_[i]->AllocateVariablesRecursively();
1027 }
1028
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001029 // If scope is already resolved, we still need to allocate
1030 // variables in inner scopes which might not had been resolved yet.
1031 if (resolved()) return;
1032 // The number of slots required for variables.
1033 num_stack_slots_ = 0;
1034 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1035
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 // Allocate variables for this scope.
1037 // Parameters must be allocated first, if any.
1038 if (is_function_scope()) AllocateParameterLocals();
1039 AllocateNonParameterLocals();
1040
1041 // Allocate context if necessary.
1042 bool must_have_local_context = false;
1043 if (scope_calls_eval_ || scope_contains_with_) {
1044 // The context for the eval() call or 'with' statement in this scope.
1045 // Unless we are in the global or an eval scope, we need a local
1046 // context even if we didn't statically allocate any locals in it,
1047 // and the compiler will access the context variable. If we are
1048 // not in an inner scope, the scope is provided from the outside.
1049 must_have_local_context = is_function_scope();
1050 }
1051
1052 // If we didn't allocate any locals in the local context, then we only
1053 // need the minimal number of slots if we must have a local context.
1054 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1055 !must_have_local_context) {
1056 num_heap_slots_ = 0;
1057 }
1058
1059 // Allocation done.
1060 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1061}
1062
1063} } // namespace v8::internal