blob: cffbef6d1f8455bd89eb1caabde85d669f8d5331 [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);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000151 SetDefaults(FUNCTION_SCOPE, NULL, scope_info);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000152 ASSERT(resolved());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000153 if (scope_info->HasHeapAllocatedLocals()) {
154 num_heap_slots_ = scope_info_->NumberOfContextSlots();
155 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000156
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000157 AddInnerScope(inner_scope);
158
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000159 // This scope's arguments shadow (if present) is context-allocated if an inner
160 // scope accesses this one's parameters. Allocate the arguments_shadow_
161 // variable if necessary.
162 Variable::Mode mode;
163 int arguments_shadow_index =
164 scope_info_->ContextSlotIndex(Heap::arguments_shadow_symbol(), &mode);
165 if (arguments_shadow_index >= 0) {
166 ASSERT(mode == Variable::INTERNAL);
167 arguments_shadow_ = new Variable(this,
168 Factory::arguments_shadow_symbol(),
169 Variable::INTERNAL,
170 true,
171 Variable::ARGUMENTS);
172 arguments_shadow_->set_rewrite(
173 new Slot(arguments_shadow_, Slot::CONTEXT, arguments_shadow_index));
174 arguments_shadow_->set_is_used(true);
175 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000176}
177
178
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000179Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
180 Scope* global_scope) {
181 ASSERT(!info->closure().is_null());
182 // If we have a serialized scope info, reuse it.
183 Scope* innermost_scope = NULL;
184 Scope* scope = NULL;
185
186 SerializedScopeInfo* scope_info = info->closure()->shared()->scope_info();
187 if (scope_info != SerializedScopeInfo::Empty()) {
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 if (innermost_scope == NULL) innermost_scope = scope;
195 } else {
196 ASSERT(current->context()->IsGlobalContext());
197 }
198 } while (!current->context()->IsGlobalContext());
199 }
200
201 global_scope->AddInnerScope(scope);
202 if (innermost_scope == NULL) innermost_scope = global_scope;
203
204 return innermost_scope;
205}
206
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000207
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000208bool Scope::Analyze(CompilationInfo* info) {
209 ASSERT(info->function() != NULL);
210 Scope* top = info->function()->scope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000211
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000212 while (top->outer_scope() != NULL) top = top->outer_scope();
213 top->AllocateVariables(info->calling_context());
214
215#ifdef DEBUG
216 if (Bootstrapper::IsActive()
217 ? FLAG_print_builtin_scopes
218 : FLAG_print_scopes) {
219 info->function()->scope()->Print();
220 }
221#endif
222
223 info->SetScope(info->function()->scope());
224 return true; // Can not fail.
225}
226
227
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228void Scope::Initialize(bool inside_with) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000229 ASSERT(!resolved());
230
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 // Add this scope as a new inner scope of the outer scope.
232 if (outer_scope_ != NULL) {
233 outer_scope_->inner_scopes_.Add(this);
234 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
235 } else {
236 scope_inside_with_ = inside_with;
237 }
238
239 // Declare convenience variables.
240 // Declare and allocate receiver (even for the global scope, and even
241 // if naccesses_ == 0).
242 // NOTE: When loading parameters in the global scope, we must take
243 // care not to access them as properties of the global object, but
244 // instead load them directly from the stack. Currently, the only
245 // such parameter is 'this' which is passed on the stack when
246 // invoking scripts
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000247 Variable* var =
248 variables_.Declare(this, Factory::this_symbol(), Variable::VAR,
249 false, Variable::THIS);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000250 var->set_rewrite(new Slot(var, Slot::PARAMETER, -1));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000251 receiver_ = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252
253 if (is_function_scope()) {
254 // Declare 'arguments' variable which exists in all functions.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000255 // Note that it might never be accessed, in which case it won't be
256 // allocated during variable allocation.
257 variables_.Declare(this, Factory::arguments_symbol(), Variable::VAR,
258 true, Variable::ARGUMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 }
260}
261
262
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000263Variable* Scope::LocalLookup(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000264 Variable* result = variables_.Lookup(name);
265 if (result != NULL || !resolved()) {
266 return result;
267 }
268 // If the scope is resolved, we can find a variable in serialized scope info.
269
270 // We should never lookup 'arguments' in this scope
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000271 // as it is implicitly present in any scope.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000272 ASSERT(*name != *Factory::arguments_symbol());
273
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000274 // Assert that there is no local slot with the given name.
275 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
276
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000277 // Check context slot lookup.
278 Variable::Mode mode;
279 int index = scope_info_->ContextSlotIndex(*name, &mode);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000280 if (index >= 0) {
281 Variable* var =
282 variables_.Declare(this, name, mode, true, Variable::NORMAL);
283 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
284 return var;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000285 }
286
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000287 index = scope_info_->ParameterIndex(*name);
288 if (index >= 0) {
289 // ".arguments" must be present in context slots.
290 ASSERT(arguments_shadow_ != NULL);
291 Variable* var =
292 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
293 Property* rewrite =
294 new Property(new VariableProxy(arguments_shadow_),
295 new Literal(Handle<Object>(Smi::FromInt(index))),
296 RelocInfo::kNoPosition,
297 Property::SYNTHETIC);
298 rewrite->set_is_arguments_access(true);
299 var->set_rewrite(rewrite);
300 return var;
301 }
302
303 index = scope_info_->FunctionContextSlotIndex(*name);
304 if (index >= 0) {
305 // Check that there is no local slot with the given name.
306 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
307 Variable* var =
308 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
309 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
310 return var;
311 }
312
313 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314}
315
316
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000317Variable* Scope::Lookup(Handle<String> name) {
318 for (Scope* scope = this;
319 scope != NULL;
320 scope = scope->outer_scope()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000321 Variable* var = scope->LocalLookup(name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000322 if (var != NULL) return var;
323 }
324 return NULL;
325}
326
327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328Variable* Scope::DeclareFunctionVar(Handle<String> name) {
329 ASSERT(is_function_scope() && function_ == NULL);
ager@chromium.org3e875802009-06-29 08:26:34 +0000330 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 return function_;
332}
333
334
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000335Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 // DYNAMIC variables are introduces during variable allocation,
337 // INTERNAL variables are allocated explicitly, and TEMPORARY
338 // variables are allocated via NewTemporary().
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000339 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000341 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
342}
343
344
345Variable* Scope::DeclareGlobal(Handle<String> name) {
346 ASSERT(is_global_scope());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000347 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000348 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349}
350
351
352void Scope::AddParameter(Variable* var) {
353 ASSERT(is_function_scope());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000354 ASSERT(LocalLookup(var->name()) == var);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 params_.Add(var);
356}
357
358
359VariableProxy* Scope::NewUnresolved(Handle<String> name, bool inside_with) {
360 // Note that we must not share the unresolved variables with
361 // the same name because they may be removed selectively via
362 // RemoveUnresolved().
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000363 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 VariableProxy* proxy = new VariableProxy(name, false, inside_with);
365 unresolved_.Add(proxy);
366 return proxy;
367}
368
369
370void Scope::RemoveUnresolved(VariableProxy* var) {
371 // Most likely (always?) any variable we want to remove
372 // was just added before, so we search backwards.
373 for (int i = unresolved_.length(); i-- > 0;) {
374 if (unresolved_[i] == var) {
375 unresolved_.Remove(i);
376 return;
377 }
378 }
379}
380
381
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000382Variable* Scope::NewTemporary(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000383 ASSERT(!resolved());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000384 Variable* var =
385 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000387 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388}
389
390
391void Scope::AddDeclaration(Declaration* declaration) {
392 decls_.Add(declaration);
393}
394
395
396void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000397 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 if (!HasIllegalRedeclaration()) {
399 illegal_redecl_ = expression;
400 }
401 ASSERT(HasIllegalRedeclaration());
402}
403
404
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000405void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 ASSERT(HasIllegalRedeclaration());
407 illegal_redecl_->Accept(visitor);
408}
409
410
411template<class Allocator>
412void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
413 // Collect variables in this scope.
414 // Note that the function_ variable - if present - is not
415 // collected here but handled separately in ScopeInfo
416 // which is the current user of this function).
417 for (int i = 0; i < temps_.length(); i++) {
418 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000419 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420 locals->Add(var);
421 }
422 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000423 for (VariableMap::Entry* p = variables_.Start();
424 p != NULL;
425 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000427 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 locals->Add(var);
429 }
430 }
431}
432
433
434// Make sure the method gets instantiated by the template system.
435template void Scope::CollectUsedVariables(
436 List<Variable*, FreeStoreAllocationPolicy>* locals);
437template void Scope::CollectUsedVariables(
438 List<Variable*, PreallocatedStorage>* locals);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000439template void Scope::CollectUsedVariables(
440 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441
442
ager@chromium.org381abbb2009-02-25 13:23:22 +0000443void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444 ASSERT(outer_scope_ == NULL); // eval or global scopes only
445
446 // 1) Propagate scope information.
447 // If we are in an eval scope, we may have other outer scopes about
448 // which we don't know anything at this point. Thus we must be conservative
449 // and assume they may invoke eval themselves. Eventually we could capture
450 // this information in the ScopeInfo and then use it here (by traversing
451 // the call chain stack, at compile time).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000452 bool eval_scope = is_eval_scope();
453 PropagateScopeInfo(eval_scope, eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454
455 // 2) Resolve variables.
456 Scope* global_scope = NULL;
457 if (is_global_scope()) global_scope = this;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000458 ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459
460 // 3) Allocate variables.
461 AllocateVariablesRecursively();
462}
463
464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465bool Scope::AllowsLazyCompilation() const {
466 return !force_eager_compilation_ && HasTrivialOuterContext();
467}
468
469
470bool Scope::HasTrivialContext() const {
471 // A function scope has a trivial context if it always is the global
472 // context. We iteratively scan out the context chain to see if
473 // there is anything that makes this scope non-trivial; otherwise we
474 // return true.
475 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
476 if (scope->is_eval_scope()) return false;
477 if (scope->scope_inside_with_) return false;
478 if (scope->num_heap_slots_ > 0) return false;
479 }
480 return true;
481}
482
483
484bool Scope::HasTrivialOuterContext() const {
485 Scope* outer = outer_scope_;
486 if (outer == NULL) return true;
487 // Note that the outer context may be trivial in general, but the current
488 // scope may be inside a 'with' statement in which case the outer context
489 // for this scope is not trivial.
490 return !scope_inside_with_ && outer->HasTrivialContext();
491}
492
493
494int Scope::ContextChainLength(Scope* scope) {
495 int n = 0;
496 for (Scope* s = this; s != scope; s = s->outer_scope_) {
497 ASSERT(s != NULL); // scope must be in the scope chain
498 if (s->num_heap_slots() > 0) n++;
499 }
500 return n;
501}
502
503
504#ifdef DEBUG
505static const char* Header(Scope::Type type) {
506 switch (type) {
507 case Scope::EVAL_SCOPE: return "eval";
508 case Scope::FUNCTION_SCOPE: return "function";
509 case Scope::GLOBAL_SCOPE: return "global";
510 }
511 UNREACHABLE();
512 return NULL;
513}
514
515
516static void Indent(int n, const char* str) {
517 PrintF("%*s%s", n, "", str);
518}
519
520
521static void PrintName(Handle<String> name) {
522 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
523 PrintF("%s", *s);
524}
525
526
527static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000528 if (var->is_used() || var->rewrite() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 Indent(indent, Variable::Mode2String(var->mode()));
530 PrintF(" ");
531 PrintName(var->name());
532 PrintF("; // ");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000533 if (var->rewrite() != NULL) {
534 PrintF("%s, ", printer->Print(var->rewrite()));
535 if (var->is_accessed_from_inner_scope()) PrintF(", ");
536 }
537 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 PrintF("\n");
539 }
540}
541
542
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000543static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
544 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000545 Variable* var = reinterpret_cast<Variable*>(p->value);
546 PrintVar(printer, indent, var);
547 }
548}
549
550
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551void Scope::Print(int n) {
552 int n0 = (n > 0 ? n : 0);
553 int n1 = n0 + 2; // indentation
554
555 // Print header.
556 Indent(n0, Header(type_));
557 if (scope_name_->length() > 0) {
558 PrintF(" ");
559 PrintName(scope_name_);
560 }
561
562 // Print parameters, if any.
563 if (is_function_scope()) {
564 PrintF(" (");
565 for (int i = 0; i < params_.length(); i++) {
566 if (i > 0) PrintF(", ");
567 PrintName(params_[i]->name());
568 }
569 PrintF(")");
570 }
571
572 PrintF(" {\n");
573
574 // Function name, if any (named function literals, only).
575 if (function_ != NULL) {
576 Indent(n1, "// (local) function name: ");
577 PrintName(function_->name());
578 PrintF("\n");
579 }
580
581 // Scope info.
582 if (HasTrivialOuterContext()) {
583 Indent(n1, "// scope has trivial outer context\n");
584 }
585 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
586 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
587 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
588 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
589 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000590 if (outer_scope_is_eval_scope_) {
591 Indent(n1, "// outer scope is 'eval' scope\n");
592 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 if (num_stack_slots_ > 0) { Indent(n1, "// ");
594 PrintF("%d stack slots\n", num_stack_slots_); }
595 if (num_heap_slots_ > 0) { Indent(n1, "// ");
596 PrintF("%d heap slots\n", num_heap_slots_); }
597
598 // Print locals.
599 PrettyPrinter printer;
600 Indent(n1, "// function var\n");
601 if (function_ != NULL) {
602 PrintVar(&printer, n1, function_);
603 }
604
605 Indent(n1, "// temporary vars\n");
606 for (int i = 0; i < temps_.length(); i++) {
607 PrintVar(&printer, n1, temps_[i]);
608 }
609
610 Indent(n1, "// local vars\n");
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000611 PrintMap(&printer, n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000613 Indent(n1, "// dynamic vars\n");
614 if (dynamics_ != NULL) {
615 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
616 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
617 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
618 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619
620 // Print inner scopes (disable by providing negative n).
621 if (n >= 0) {
622 for (int i = 0; i < inner_scopes_.length(); i++) {
623 PrintF("\n");
624 inner_scopes_[i]->Print(n1);
625 }
626 }
627
628 Indent(n0, "}\n");
629}
630#endif // DEBUG
631
632
ager@chromium.org381abbb2009-02-25 13:23:22 +0000633Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000634 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000635 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000636 Variable* var = map->Lookup(name);
637 if (var == NULL) {
638 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000639 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000640 // Allocate it by giving it a dynamic lookup.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000641 var->set_rewrite(new Slot(var, Slot::LOOKUP, -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 return var;
644}
645
646
647// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000648// the statically resolved variable belonging to an outer scope, or
649// NULL. It may be NULL because a) we couldn't find a variable, or b)
650// because the variable is just a guess (and may be shadowed by
651// another variable that is introduced dynamically via an 'eval' call
652// or a 'with' statement).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000653Variable* Scope::LookupRecursive(Handle<String> name,
654 bool inner_lookup,
655 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 // If we find a variable, but the current scope calls 'eval', the found
657 // variable may not be the correct one (the 'eval' may introduce a
658 // property with the same name). In that case, remember that the variable
659 // found is just a guess.
660 bool guess = scope_calls_eval_;
661
662 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000663 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664
665 if (var != NULL) {
666 // We found a variable. If this is not an inner lookup, we are done.
667 // (Even if there is an 'eval' in this scope which introduces the
668 // same variable again, the resulting variable remains the same.
669 // Note that enclosing 'with' statements are handled at the call site.)
670 if (!inner_lookup)
671 return var;
672
673 } else {
674 // We did not find a variable locally. Check against the function variable,
675 // if any. We can do this for all scopes, since the function variable is
676 // only present - if at all - for function scopes.
677 //
678 // This lookup corresponds to a lookup in the "intermediate" scope sitting
679 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
680 // the name of named function literal is kept in an intermediate scope
ager@chromium.org32912102009-01-16 10:38:43 +0000681 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682 if (function_ != NULL && function_->name().is_identical_to(name)) {
683 var = function_;
684
685 } else if (outer_scope_ != NULL) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000686 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687 // We may have found a variable in an outer scope. However, if
688 // the current scope is inside a 'with', the actual variable may
689 // be a property introduced via the 'with' statement. Then, the
690 // variable we may have found is just a guess.
691 if (scope_inside_with_)
692 guess = true;
693 }
694
695 // If we did not find a variable, we are done.
696 if (var == NULL)
697 return NULL;
698 }
699
700 ASSERT(var != NULL);
701
702 // If this is a lookup from an inner scope, mark the variable.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000703 if (inner_lookup) {
704 var->MarkAsAccessedFromInnerScope();
705 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000707 // If the variable we have found is just a guess, invalidate the
708 // result. If the found variable is local, record that fact so we
709 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000710 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000711 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000713 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714
715 return var;
716}
717
718
ager@chromium.org381abbb2009-02-25 13:23:22 +0000719void Scope::ResolveVariable(Scope* global_scope,
720 Handle<Context> context,
721 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 ASSERT(global_scope == NULL || global_scope->is_global_scope());
723
724 // If the proxy is already resolved there's nothing to do
725 // (functions and consts may be resolved by the parser).
726 if (proxy->var() != NULL) return;
727
728 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000729 Variable* invalidated_local = NULL;
730 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731
732 if (proxy->inside_with()) {
733 // If we are inside a local 'with' statement, all bets are off
734 // and we cannot resolve the proxy to a local variable even if
735 // we found an outer matching variable.
736 // Note that we must do a lookup anyway, because if we find one,
737 // we must mark that variable as potentially accessed from this
738 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000739 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740
741 } else {
742 // We are not inside a local 'with' statement.
743
744 if (var == NULL) {
745 // We did not find the variable. We have a global variable
746 // if we are in the global scope (we know already that we
747 // are outside a 'with' statement) or if there is no way
748 // that the variable might be introduced dynamically (through
749 // a local or outer eval() call, or an outer 'with' statement),
750 // or we don't know about the outer scope (because we are
751 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000752 if (is_global_scope() ||
753 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
754 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 // We must have a global variable.
756 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000757 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000758
759 } else if (scope_inside_with_) {
760 // If we are inside a with statement we give up and look up
761 // the variable at runtime.
762 var = NonLocal(proxy->name(), Variable::DYNAMIC);
763
764 } else if (invalidated_local != NULL) {
765 // No with statements are involved and we found a local
766 // variable that might be shadowed by eval introduced
767 // variables.
768 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
769 var->set_local_if_not_shadowed(invalidated_local);
770
771 } else if (outer_scope_is_eval_scope_) {
772 // No with statements and we did not find a local and the code
773 // is executed with a call to eval. The context contains
774 // scope information that we can use to determine if the
775 // variable is global if it is not shadowed by eval-introduced
776 // variables.
777 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
778 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
779
780 } else {
781 var = NonLocal(proxy->name(), Variable::DYNAMIC);
782 }
783
784 } else {
785 // No with statements and we did not find a local and the code
786 // is not executed with a call to eval. We know that this
787 // variable is global unless it is shadowed by eval-introduced
788 // variables.
789 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790 }
791 }
792 }
793
794 proxy->BindTo(var);
795}
796
797
ager@chromium.org381abbb2009-02-25 13:23:22 +0000798void Scope::ResolveVariablesRecursively(Scope* global_scope,
799 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000800 ASSERT(global_scope == NULL || global_scope->is_global_scope());
801
802 // Resolve unresolved variables for this scope.
803 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000804 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000805 }
806
807 // Resolve unresolved variables for inner scopes.
808 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000809 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810 }
811}
812
813
ager@chromium.org381abbb2009-02-25 13:23:22 +0000814bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
815 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816 if (outer_scope_calls_eval) {
817 outer_scope_calls_eval_ = true;
818 }
819
ager@chromium.org381abbb2009-02-25 13:23:22 +0000820 if (outer_scope_is_eval_scope) {
821 outer_scope_is_eval_scope_ = true;
822 }
823
824 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
825 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826 for (int i = 0; i < inner_scopes_.length(); i++) {
827 Scope* inner_scope = inner_scopes_[i];
ager@chromium.org381abbb2009-02-25 13:23:22 +0000828 if (inner_scope->PropagateScopeInfo(calls_eval, is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829 inner_scope_calls_eval_ = true;
830 }
831 if (inner_scope->force_eager_compilation_) {
832 force_eager_compilation_ = true;
833 }
834 }
835
836 return scope_calls_eval_ || inner_scope_calls_eval_;
837}
838
839
840bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000841 // Give var a read/write use if there is a chance it might be accessed
842 // via an eval() call. This is only possible if the variable has a
843 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000844 if ((var->is_this() || var->name()->length() > 0) &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000845 (var->is_accessed_from_inner_scope() ||
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000846 scope_calls_eval_ || inner_scope_calls_eval_ ||
847 scope_contains_with_)) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000848 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000850 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000851 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852}
853
854
855bool Scope::MustAllocateInContext(Variable* var) {
856 // If var is accessed from an inner scope, or if there is a
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000857 // possibility that it might be accessed from the current or an inner
858 // scope (through an eval() call), it must be allocated in the
859 // context. Exception: temporary variables are not allocated in the
860 // context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000861 return
862 var->mode() != Variable::TEMPORARY &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000863 (var->is_accessed_from_inner_scope() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864 scope_calls_eval_ || inner_scope_calls_eval_ ||
865 scope_contains_with_ || var->is_global());
866}
867
868
869bool Scope::HasArgumentsParameter() {
870 for (int i = 0; i < params_.length(); i++) {
871 if (params_[i]->name().is_identical_to(Factory::arguments_symbol()))
872 return true;
873 }
874 return false;
875}
876
877
878void Scope::AllocateStackSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000879 var->set_rewrite(new Slot(var, Slot::LOCAL, num_stack_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880}
881
882
883void Scope::AllocateHeapSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000884 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885}
886
887
888void Scope::AllocateParameterLocals() {
889 ASSERT(is_function_scope());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000890 Variable* arguments = LocalLookup(Factory::arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000892
893 // Parameters are rewritten to arguments[i] if 'arguments' is used in
894 // a non-strict mode function. Strict mode code doesn't alias arguments.
895 bool rewrite_parameters = false;
896
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
898 // 'arguments' is used. Unless there is also a parameter called
899 // 'arguments', we must be conservative and access all parameters via
900 // the arguments object: The i'th parameter is rewritten into
901 // '.arguments[i]' (*). If we have a parameter named 'arguments', a
902 // (new) value is always assigned to it via the function
903 // invocation. Then 'arguments' denotes that specific parameter value
904 // and cannot be used to access the parameters, which is why we don't
905 // need to rewrite in that case.
906 //
907 // (*) Instead of having a parameter called 'arguments', we may have an
908 // assignment to 'arguments' in the function body, at some arbitrary
909 // point in time (possibly through an 'eval()' call!). After that
910 // assignment any re-write of parameters would be invalid (was bug
911 // 881452). Thus, we introduce a shadow '.arguments'
912 // variable which also points to the arguments object. For rewrites we
913 // use '.arguments' which remains valid even if we assign to
914 // 'arguments'. To summarize: If we need to rewrite, we allocate an
915 // 'arguments' object dynamically upon function invocation. The compiler
916 // introduces 2 local variables 'arguments' and '.arguments', both of
917 // which originally point to the arguments object that was
918 // allocated. All parameters are rewritten into property accesses via
919 // the '.arguments' variable. Thus, any changes to properties of
920 // 'arguments' are reflected in the variables and vice versa. If the
921 // 'arguments' variable is changed, '.arguments' still points to the
922 // correct arguments object and the rewrites still work.
923
924 // We are using 'arguments'. Tell the code generator that is needs to
925 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000926 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000928 // In strict mode 'arguments' does not alias formal parameters.
929 // Therefore in strict mode we allocate parameters as if 'arguments'
930 // were not used.
931 rewrite_parameters = !is_strict_mode();
932 }
933
934 if (rewrite_parameters) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935 // We also need the '.arguments' shadow variable. Declare it and create
936 // and bind the corresponding proxy. It's ok to declare it only now
937 // because it's a local variable that is allocated after the parameters
938 // have been allocated.
939 //
940 // Note: This is "almost" at temporary variable but we cannot use
941 // NewTemporary() because the mode needs to be INTERNAL since this
942 // variable may be allocated in the heap-allocated context (temporaries
943 // are never allocated in the context).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000944 arguments_shadow_ = new Variable(this,
945 Factory::arguments_shadow_symbol(),
946 Variable::INTERNAL,
947 true,
948 Variable::ARGUMENTS);
949 arguments_shadow_->set_is_used(true);
950 temps_.Add(arguments_shadow_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951
952 // Allocate the parameters by rewriting them into '.arguments[i]' accesses.
953 for (int i = 0; i < params_.length(); i++) {
954 Variable* var = params_[i];
955 ASSERT(var->scope() == this);
956 if (MustAllocate(var)) {
957 if (MustAllocateInContext(var)) {
958 // It is ok to set this only now, because arguments is a local
959 // variable that is allocated after the parameters have been
960 // allocated.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000961 arguments_shadow_->MarkAsAccessedFromInnerScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000963 Property* rewrite =
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000964 new Property(new VariableProxy(arguments_shadow_),
965 new Literal(Handle<Object>(Smi::FromInt(i))),
966 RelocInfo::kNoPosition,
967 Property::SYNTHETIC);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000968 rewrite->set_is_arguments_access(true);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000969 var->set_rewrite(rewrite);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 }
971 }
972
973 } else {
974 // The arguments object is not used, so we can access parameters directly.
975 // The same parameter may occur multiple times in the parameters_ list.
976 // If it does, and if it is not copied into the context object, it must
977 // receive the highest parameter index for that parameter; thus iteration
978 // order is relevant!
979 for (int i = 0; i < params_.length(); i++) {
980 Variable* var = params_[i];
981 ASSERT(var->scope() == this);
982 if (MustAllocate(var)) {
983 if (MustAllocateInContext(var)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000984 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000985 (var->AsSlot() != NULL &&
986 var->AsSlot()->type() == Slot::CONTEXT));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000987 if (var->rewrite() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 // Only set the heap allocation if the parameter has not
989 // been allocated yet.
990 AllocateHeapSlot(var);
991 }
992 } else {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000993 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000994 (var->AsSlot() != NULL &&
995 var->AsSlot()->type() == Slot::PARAMETER));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 // Set the parameter index always, even if the parameter
997 // was seen before! (We need to access the actual parameter
998 // supplied for the last occurrence of a multiply declared
999 // parameter.)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001000 var->set_rewrite(new Slot(var, Slot::PARAMETER, i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 }
1002 }
1003 }
1004 }
1005}
1006
1007
1008void Scope::AllocateNonParameterLocal(Variable* var) {
1009 ASSERT(var->scope() == this);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001010 ASSERT(var->rewrite() == NULL ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 (!var->IsVariable(Factory::result_symbol())) ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001012 (var->AsSlot() == NULL || var->AsSlot()->type() != Slot::LOCAL));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001013 if (var->rewrite() == NULL && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014 if (MustAllocateInContext(var)) {
1015 AllocateHeapSlot(var);
1016 } else {
1017 AllocateStackSlot(var);
1018 }
1019 }
1020}
1021
1022
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001024 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 for (int i = 0; i < temps_.length(); i++) {
1026 AllocateNonParameterLocal(temps_[i]);
1027 }
1028
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001029 for (VariableMap::Entry* p = variables_.Start();
1030 p != NULL;
1031 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 Variable* var = reinterpret_cast<Variable*>(p->value);
1033 AllocateNonParameterLocal(var);
1034 }
1035
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001036 // For now, function_ must be allocated at the very end. If it gets
1037 // allocated in the context, it must be the last slot in the context,
1038 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1040 if (function_ != NULL) {
1041 AllocateNonParameterLocal(function_);
1042 }
1043}
1044
1045
1046void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 // Allocate variables for inner scopes.
1048 for (int i = 0; i < inner_scopes_.length(); i++) {
1049 inner_scopes_[i]->AllocateVariablesRecursively();
1050 }
1051
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001052 // If scope is already resolved, we still need to allocate
1053 // variables in inner scopes which might not had been resolved yet.
1054 if (resolved()) return;
1055 // The number of slots required for variables.
1056 num_stack_slots_ = 0;
1057 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1058
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 // Allocate variables for this scope.
1060 // Parameters must be allocated first, if any.
1061 if (is_function_scope()) AllocateParameterLocals();
1062 AllocateNonParameterLocals();
1063
1064 // Allocate context if necessary.
1065 bool must_have_local_context = false;
1066 if (scope_calls_eval_ || scope_contains_with_) {
1067 // The context for the eval() call or 'with' statement in this scope.
1068 // Unless we are in the global or an eval scope, we need a local
1069 // context even if we didn't statically allocate any locals in it,
1070 // and the compiler will access the context variable. If we are
1071 // not in an inner scope, the scope is provided from the outside.
1072 must_have_local_context = is_function_scope();
1073 }
1074
1075 // If we didn't allocate any locals in the local context, then we only
1076 // need the minimal number of slots if we must have a local context.
1077 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1078 !must_have_local_context) {
1079 num_heap_slots_ = 0;
1080 }
1081
1082 // Allocation done.
1083 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1084}
1085
1086} } // namespace v8::internal