blob: f4bcaa8fba6d8c2750227a74199e2e3100875f5f [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
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000043// TODO(isolates): It is probably worth it to change the Allocator class to
44// take a pointer to an isolate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045class ZoneAllocator: public Allocator {
46 public:
47 /* nothing to do */
48 virtual ~ZoneAllocator() {}
49
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000050 virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051
52 /* ignored - Zone is freed in one fell swoop */
53 virtual void Delete(void* p) {}
54};
55
56
57static ZoneAllocator LocalsMapAllocator;
58
59
60// ----------------------------------------------------------------------------
61// Implementation of LocalsMap
62//
63// Note: We are storing the handle locations as key values in the hash map.
64// When inserting a new variable via Declare(), we rely on the fact that
65// the handle location remains alive for the duration of that variable
66// use. Because a Variable holding a handle with the same location exists
67// this is ensured.
68
69static bool Match(void* key1, void* key2) {
70 String* name1 = *reinterpret_cast<String**>(key1);
71 String* name2 = *reinterpret_cast<String**>(key2);
72 ASSERT(name1->IsSymbol());
73 ASSERT(name2->IsSymbol());
74 return name1 == name2;
75}
76
77
78// Dummy constructor
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000079VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000081VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {}
82VariableMap::~VariableMap() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000083
84
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000085Variable* VariableMap::Declare(Scope* scope,
86 Handle<String> name,
87 Variable::Mode mode,
88 bool is_valid_lhs,
89 Variable::Kind kind) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true);
91 if (p->value == NULL) {
92 // The variable has not been declared yet -> insert it.
93 ASSERT(p->key == name.location());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000094 p->value = new Variable(scope, name, mode, is_valid_lhs, kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095 }
96 return reinterpret_cast<Variable*>(p->value);
97}
98
99
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000100Variable* VariableMap::Lookup(Handle<String> name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false);
102 if (p != NULL) {
103 ASSERT(*reinterpret_cast<String**>(p->key) == *name);
104 ASSERT(p->value != NULL);
105 return reinterpret_cast<Variable*>(p->value);
106 }
107 return NULL;
108}
109
110
111// ----------------------------------------------------------------------------
112// Implementation of Scope
113
114
115// Dummy constructor
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000116Scope::Scope(Type type)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000117 : inner_scopes_(0),
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000118 variables_(false),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 temps_(0),
120 params_(0),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 unresolved_(0),
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000122 decls_(0) {
123 SetDefaults(type, NULL, NULL);
124 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125}
126
127
128Scope::Scope(Scope* outer_scope, Type type)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000129 : inner_scopes_(4),
130 variables_(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 temps_(4),
132 params_(4),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 unresolved_(16),
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000134 decls_(4) {
135 SetDefaults(type, outer_scope, NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136 // At some point we might want to provide outer scopes to
137 // eval scopes (by walking the stack and reading the scope info).
138 // In that case, the ASSERT below needs to be adjusted.
139 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL));
140 ASSERT(!HasIllegalRedeclaration());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000141 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142}
143
144
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000145Scope::Scope(Scope* inner_scope, SerializedScopeInfo* scope_info)
146 : inner_scopes_(4),
147 variables_(),
148 temps_(4),
149 params_(4),
150 unresolved_(16),
151 decls_(4) {
152 ASSERT(scope_info != NULL);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000153 SetDefaults(FUNCTION_SCOPE, NULL, scope_info);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000154 ASSERT(resolved());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000155 if (scope_info->HasHeapAllocatedLocals()) {
156 num_heap_slots_ = scope_info_->NumberOfContextSlots();
157 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000158
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000159 AddInnerScope(inner_scope);
160
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000161 // This scope's arguments shadow (if present) is context-allocated if an inner
162 // scope accesses this one's parameters. Allocate the arguments_shadow_
163 // variable if necessary.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 Isolate* isolate = Isolate::Current();
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000165 Variable::Mode mode;
166 int arguments_shadow_index =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000167 scope_info_->ContextSlotIndex(
168 isolate->heap()->arguments_shadow_symbol(), &mode);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000169 if (arguments_shadow_index >= 0) {
170 ASSERT(mode == Variable::INTERNAL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000171 arguments_shadow_ = new Variable(
172 this,
173 isolate->factory()->arguments_shadow_symbol(),
174 Variable::INTERNAL,
175 true,
176 Variable::ARGUMENTS);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000177 arguments_shadow_->set_rewrite(
178 new Slot(arguments_shadow_, Slot::CONTEXT, arguments_shadow_index));
179 arguments_shadow_->set_is_used(true);
180 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000181}
182
183
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000184Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
185 Scope* global_scope) {
186 ASSERT(!info->closure().is_null());
187 // If we have a serialized scope info, reuse it.
188 Scope* innermost_scope = NULL;
189 Scope* scope = NULL;
190
191 SerializedScopeInfo* scope_info = info->closure()->shared()->scope_info();
192 if (scope_info != SerializedScopeInfo::Empty()) {
193 JSFunction* current = *info->closure();
194 do {
195 current = current->context()->closure();
196 SerializedScopeInfo* scope_info = current->shared()->scope_info();
197 if (scope_info != SerializedScopeInfo::Empty()) {
198 scope = new Scope(scope, scope_info);
199 if (innermost_scope == NULL) innermost_scope = scope;
200 } else {
201 ASSERT(current->context()->IsGlobalContext());
202 }
203 } while (!current->context()->IsGlobalContext());
204 }
205
206 global_scope->AddInnerScope(scope);
207 if (innermost_scope == NULL) innermost_scope = global_scope;
208
209 return innermost_scope;
210}
211
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000212
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000213bool Scope::Analyze(CompilationInfo* info) {
214 ASSERT(info->function() != NULL);
215 Scope* top = info->function()->scope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000216
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000217 while (top->outer_scope() != NULL) top = top->outer_scope();
218 top->AllocateVariables(info->calling_context());
219
220#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000221 if (info->isolate()->bootstrapper()->IsActive()
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000222 ? FLAG_print_builtin_scopes
223 : FLAG_print_scopes) {
224 info->function()->scope()->Print();
225 }
226#endif
227
228 info->SetScope(info->function()->scope());
229 return true; // Can not fail.
230}
231
232
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233void Scope::Initialize(bool inside_with) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000234 ASSERT(!resolved());
235
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000236 // Add this scope as a new inner scope of the outer scope.
237 if (outer_scope_ != NULL) {
238 outer_scope_->inner_scopes_.Add(this);
239 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
240 } else {
241 scope_inside_with_ = inside_with;
242 }
243
244 // Declare convenience variables.
245 // Declare and allocate receiver (even for the global scope, and even
246 // if naccesses_ == 0).
247 // NOTE: When loading parameters in the global scope, we must take
248 // care not to access them as properties of the global object, but
249 // instead load them directly from the stack. Currently, the only
250 // such parameter is 'this' which is passed on the stack when
251 // invoking scripts
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000252 Variable* var =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000253 variables_.Declare(this, FACTORY->this_symbol(), Variable::VAR,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000254 false, Variable::THIS);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000255 var->set_rewrite(new Slot(var, Slot::PARAMETER, -1));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000256 receiver_ = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257
258 if (is_function_scope()) {
259 // Declare 'arguments' variable which exists in all functions.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000260 // Note that it might never be accessed, in which case it won't be
261 // allocated during variable allocation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000262 variables_.Declare(this, FACTORY->arguments_symbol(), Variable::VAR,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000263 true, Variable::ARGUMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 }
265}
266
267
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000268Variable* Scope::LocalLookup(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000269 Variable* result = variables_.Lookup(name);
270 if (result != NULL || !resolved()) {
271 return result;
272 }
273 // If the scope is resolved, we can find a variable in serialized scope info.
274
275 // We should never lookup 'arguments' in this scope
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000276 // as it is implicitly present in any scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000277 ASSERT(*name != *FACTORY->arguments_symbol());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000278
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000279 // Assert that there is no local slot with the given name.
280 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
281
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000282 // Check context slot lookup.
283 Variable::Mode mode;
284 int index = scope_info_->ContextSlotIndex(*name, &mode);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000285 if (index >= 0) {
286 Variable* var =
287 variables_.Declare(this, name, mode, true, Variable::NORMAL);
288 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
289 return var;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000290 }
291
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000292 index = scope_info_->ParameterIndex(*name);
293 if (index >= 0) {
294 // ".arguments" must be present in context slots.
295 ASSERT(arguments_shadow_ != NULL);
296 Variable* var =
297 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
298 Property* rewrite =
299 new Property(new VariableProxy(arguments_shadow_),
300 new Literal(Handle<Object>(Smi::FromInt(index))),
301 RelocInfo::kNoPosition,
302 Property::SYNTHETIC);
303 rewrite->set_is_arguments_access(true);
304 var->set_rewrite(rewrite);
305 return var;
306 }
307
308 index = scope_info_->FunctionContextSlotIndex(*name);
309 if (index >= 0) {
310 // Check that there is no local slot with the given name.
311 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
312 Variable* var =
313 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
314 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
315 return var;
316 }
317
318 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319}
320
321
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000322Variable* Scope::Lookup(Handle<String> name) {
323 for (Scope* scope = this;
324 scope != NULL;
325 scope = scope->outer_scope()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000326 Variable* var = scope->LocalLookup(name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000327 if (var != NULL) return var;
328 }
329 return NULL;
330}
331
332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333Variable* Scope::DeclareFunctionVar(Handle<String> name) {
334 ASSERT(is_function_scope() && function_ == NULL);
ager@chromium.org3e875802009-06-29 08:26:34 +0000335 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 return function_;
337}
338
339
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000340Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 // DYNAMIC variables are introduces during variable allocation,
342 // INTERNAL variables are allocated explicitly, and TEMPORARY
343 // variables are allocated via NewTemporary().
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000344 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000346 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
347}
348
349
350Variable* Scope::DeclareGlobal(Handle<String> name) {
351 ASSERT(is_global_scope());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000352 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000353 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354}
355
356
357void Scope::AddParameter(Variable* var) {
358 ASSERT(is_function_scope());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000359 ASSERT(LocalLookup(var->name()) == var);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 params_.Add(var);
361}
362
363
364VariableProxy* Scope::NewUnresolved(Handle<String> name, bool inside_with) {
365 // Note that we must not share the unresolved variables with
366 // the same name because they may be removed selectively via
367 // RemoveUnresolved().
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000368 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369 VariableProxy* proxy = new VariableProxy(name, false, inside_with);
370 unresolved_.Add(proxy);
371 return proxy;
372}
373
374
375void Scope::RemoveUnresolved(VariableProxy* var) {
376 // Most likely (always?) any variable we want to remove
377 // was just added before, so we search backwards.
378 for (int i = unresolved_.length(); i-- > 0;) {
379 if (unresolved_[i] == var) {
380 unresolved_.Remove(i);
381 return;
382 }
383 }
384}
385
386
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000387Variable* Scope::NewTemporary(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000388 ASSERT(!resolved());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000389 Variable* var =
390 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000392 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393}
394
395
396void Scope::AddDeclaration(Declaration* declaration) {
397 decls_.Add(declaration);
398}
399
400
401void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000402 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403 if (!HasIllegalRedeclaration()) {
404 illegal_redecl_ = expression;
405 }
406 ASSERT(HasIllegalRedeclaration());
407}
408
409
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000410void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411 ASSERT(HasIllegalRedeclaration());
412 illegal_redecl_->Accept(visitor);
413}
414
415
416template<class Allocator>
417void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
418 // Collect variables in this scope.
419 // Note that the function_ variable - if present - is not
420 // collected here but handled separately in ScopeInfo
421 // which is the current user of this function).
422 for (int i = 0; i < temps_.length(); i++) {
423 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000424 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 locals->Add(var);
426 }
427 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000428 for (VariableMap::Entry* p = variables_.Start();
429 p != NULL;
430 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000432 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 locals->Add(var);
434 }
435 }
436}
437
438
439// Make sure the method gets instantiated by the template system.
440template void Scope::CollectUsedVariables(
441 List<Variable*, FreeStoreAllocationPolicy>* locals);
442template void Scope::CollectUsedVariables(
443 List<Variable*, PreallocatedStorage>* locals);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000444template void Scope::CollectUsedVariables(
445 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446
447
ager@chromium.org381abbb2009-02-25 13:23:22 +0000448void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 ASSERT(outer_scope_ == NULL); // eval or global scopes only
450
451 // 1) Propagate scope information.
452 // If we are in an eval scope, we may have other outer scopes about
453 // which we don't know anything at this point. Thus we must be conservative
454 // and assume they may invoke eval themselves. Eventually we could capture
455 // this information in the ScopeInfo and then use it here (by traversing
456 // the call chain stack, at compile time).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000457 bool eval_scope = is_eval_scope();
458 PropagateScopeInfo(eval_scope, eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459
460 // 2) Resolve variables.
461 Scope* global_scope = NULL;
462 if (is_global_scope()) global_scope = this;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000463 ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464
465 // 3) Allocate variables.
466 AllocateVariablesRecursively();
467}
468
469
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470bool Scope::AllowsLazyCompilation() const {
471 return !force_eager_compilation_ && HasTrivialOuterContext();
472}
473
474
475bool Scope::HasTrivialContext() const {
476 // A function scope has a trivial context if it always is the global
477 // context. We iteratively scan out the context chain to see if
478 // there is anything that makes this scope non-trivial; otherwise we
479 // return true.
480 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
481 if (scope->is_eval_scope()) return false;
482 if (scope->scope_inside_with_) return false;
483 if (scope->num_heap_slots_ > 0) return false;
484 }
485 return true;
486}
487
488
489bool Scope::HasTrivialOuterContext() const {
490 Scope* outer = outer_scope_;
491 if (outer == NULL) return true;
492 // Note that the outer context may be trivial in general, but the current
493 // scope may be inside a 'with' statement in which case the outer context
494 // for this scope is not trivial.
495 return !scope_inside_with_ && outer->HasTrivialContext();
496}
497
498
499int Scope::ContextChainLength(Scope* scope) {
500 int n = 0;
501 for (Scope* s = this; s != scope; s = s->outer_scope_) {
502 ASSERT(s != NULL); // scope must be in the scope chain
503 if (s->num_heap_slots() > 0) n++;
504 }
505 return n;
506}
507
508
509#ifdef DEBUG
510static const char* Header(Scope::Type type) {
511 switch (type) {
512 case Scope::EVAL_SCOPE: return "eval";
513 case Scope::FUNCTION_SCOPE: return "function";
514 case Scope::GLOBAL_SCOPE: return "global";
515 }
516 UNREACHABLE();
517 return NULL;
518}
519
520
521static void Indent(int n, const char* str) {
522 PrintF("%*s%s", n, "", str);
523}
524
525
526static void PrintName(Handle<String> name) {
527 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
528 PrintF("%s", *s);
529}
530
531
532static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000533 if (var->is_used() || var->rewrite() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 Indent(indent, Variable::Mode2String(var->mode()));
535 PrintF(" ");
536 PrintName(var->name());
537 PrintF("; // ");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000538 if (var->rewrite() != NULL) {
539 PrintF("%s, ", printer->Print(var->rewrite()));
540 if (var->is_accessed_from_inner_scope()) PrintF(", ");
541 }
542 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 PrintF("\n");
544 }
545}
546
547
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000548static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
549 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000550 Variable* var = reinterpret_cast<Variable*>(p->value);
551 PrintVar(printer, indent, var);
552 }
553}
554
555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556void Scope::Print(int n) {
557 int n0 = (n > 0 ? n : 0);
558 int n1 = n0 + 2; // indentation
559
560 // Print header.
561 Indent(n0, Header(type_));
562 if (scope_name_->length() > 0) {
563 PrintF(" ");
564 PrintName(scope_name_);
565 }
566
567 // Print parameters, if any.
568 if (is_function_scope()) {
569 PrintF(" (");
570 for (int i = 0; i < params_.length(); i++) {
571 if (i > 0) PrintF(", ");
572 PrintName(params_[i]->name());
573 }
574 PrintF(")");
575 }
576
577 PrintF(" {\n");
578
579 // Function name, if any (named function literals, only).
580 if (function_ != NULL) {
581 Indent(n1, "// (local) function name: ");
582 PrintName(function_->name());
583 PrintF("\n");
584 }
585
586 // Scope info.
587 if (HasTrivialOuterContext()) {
588 Indent(n1, "// scope has trivial outer context\n");
589 }
590 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
591 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
592 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
593 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
594 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000595 if (outer_scope_is_eval_scope_) {
596 Indent(n1, "// outer scope is 'eval' scope\n");
597 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 if (num_stack_slots_ > 0) { Indent(n1, "// ");
599 PrintF("%d stack slots\n", num_stack_slots_); }
600 if (num_heap_slots_ > 0) { Indent(n1, "// ");
601 PrintF("%d heap slots\n", num_heap_slots_); }
602
603 // Print locals.
604 PrettyPrinter printer;
605 Indent(n1, "// function var\n");
606 if (function_ != NULL) {
607 PrintVar(&printer, n1, function_);
608 }
609
610 Indent(n1, "// temporary vars\n");
611 for (int i = 0; i < temps_.length(); i++) {
612 PrintVar(&printer, n1, temps_[i]);
613 }
614
615 Indent(n1, "// local vars\n");
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000616 PrintMap(&printer, n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000618 Indent(n1, "// dynamic vars\n");
619 if (dynamics_ != NULL) {
620 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
621 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
622 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
623 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000624
625 // Print inner scopes (disable by providing negative n).
626 if (n >= 0) {
627 for (int i = 0; i < inner_scopes_.length(); i++) {
628 PrintF("\n");
629 inner_scopes_[i]->Print(n1);
630 }
631 }
632
633 Indent(n0, "}\n");
634}
635#endif // DEBUG
636
637
ager@chromium.org381abbb2009-02-25 13:23:22 +0000638Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000639 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000640 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000641 Variable* var = map->Lookup(name);
642 if (var == NULL) {
643 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000644 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000645 // Allocate it by giving it a dynamic lookup.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000646 var->set_rewrite(new Slot(var, Slot::LOOKUP, -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648 return var;
649}
650
651
652// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000653// the statically resolved variable belonging to an outer scope, or
654// NULL. It may be NULL because a) we couldn't find a variable, or b)
655// because the variable is just a guess (and may be shadowed by
656// another variable that is introduced dynamically via an 'eval' call
657// or a 'with' statement).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000658Variable* Scope::LookupRecursive(Handle<String> name,
659 bool inner_lookup,
660 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661 // If we find a variable, but the current scope calls 'eval', the found
662 // variable may not be the correct one (the 'eval' may introduce a
663 // property with the same name). In that case, remember that the variable
664 // found is just a guess.
665 bool guess = scope_calls_eval_;
666
667 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000668 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669
670 if (var != NULL) {
671 // We found a variable. If this is not an inner lookup, we are done.
672 // (Even if there is an 'eval' in this scope which introduces the
673 // same variable again, the resulting variable remains the same.
674 // Note that enclosing 'with' statements are handled at the call site.)
675 if (!inner_lookup)
676 return var;
677
678 } else {
679 // We did not find a variable locally. Check against the function variable,
680 // if any. We can do this for all scopes, since the function variable is
681 // only present - if at all - for function scopes.
682 //
683 // This lookup corresponds to a lookup in the "intermediate" scope sitting
684 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
685 // the name of named function literal is kept in an intermediate scope
ager@chromium.org32912102009-01-16 10:38:43 +0000686 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687 if (function_ != NULL && function_->name().is_identical_to(name)) {
688 var = function_;
689
690 } else if (outer_scope_ != NULL) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000691 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 // We may have found a variable in an outer scope. However, if
693 // the current scope is inside a 'with', the actual variable may
694 // be a property introduced via the 'with' statement. Then, the
695 // variable we may have found is just a guess.
696 if (scope_inside_with_)
697 guess = true;
698 }
699
700 // If we did not find a variable, we are done.
701 if (var == NULL)
702 return NULL;
703 }
704
705 ASSERT(var != NULL);
706
707 // If this is a lookup from an inner scope, mark the variable.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000708 if (inner_lookup) {
709 var->MarkAsAccessedFromInnerScope();
710 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000712 // If the variable we have found is just a guess, invalidate the
713 // result. If the found variable is local, record that fact so we
714 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000715 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000716 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000718 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719
720 return var;
721}
722
723
ager@chromium.org381abbb2009-02-25 13:23:22 +0000724void Scope::ResolveVariable(Scope* global_scope,
725 Handle<Context> context,
726 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727 ASSERT(global_scope == NULL || global_scope->is_global_scope());
728
729 // If the proxy is already resolved there's nothing to do
730 // (functions and consts may be resolved by the parser).
731 if (proxy->var() != NULL) return;
732
733 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000734 Variable* invalidated_local = NULL;
735 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736
737 if (proxy->inside_with()) {
738 // If we are inside a local 'with' statement, all bets are off
739 // and we cannot resolve the proxy to a local variable even if
740 // we found an outer matching variable.
741 // Note that we must do a lookup anyway, because if we find one,
742 // we must mark that variable as potentially accessed from this
743 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000744 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745
746 } else {
747 // We are not inside a local 'with' statement.
748
749 if (var == NULL) {
750 // We did not find the variable. We have a global variable
751 // if we are in the global scope (we know already that we
752 // are outside a 'with' statement) or if there is no way
753 // that the variable might be introduced dynamically (through
754 // a local or outer eval() call, or an outer 'with' statement),
755 // or we don't know about the outer scope (because we are
756 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000757 if (is_global_scope() ||
758 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
759 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760 // We must have a global variable.
761 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000762 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000763
764 } else if (scope_inside_with_) {
765 // If we are inside a with statement we give up and look up
766 // the variable at runtime.
767 var = NonLocal(proxy->name(), Variable::DYNAMIC);
768
769 } else if (invalidated_local != NULL) {
770 // No with statements are involved and we found a local
771 // variable that might be shadowed by eval introduced
772 // variables.
773 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
774 var->set_local_if_not_shadowed(invalidated_local);
775
776 } else if (outer_scope_is_eval_scope_) {
777 // No with statements and we did not find a local and the code
778 // is executed with a call to eval. The context contains
779 // scope information that we can use to determine if the
780 // variable is global if it is not shadowed by eval-introduced
781 // variables.
782 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
783 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
784
785 } else {
786 var = NonLocal(proxy->name(), Variable::DYNAMIC);
787 }
788
789 } else {
790 // No with statements and we did not find a local and the code
791 // is not executed with a call to eval. We know that this
792 // variable is global unless it is shadowed by eval-introduced
793 // variables.
794 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795 }
796 }
797 }
798
799 proxy->BindTo(var);
800}
801
802
ager@chromium.org381abbb2009-02-25 13:23:22 +0000803void Scope::ResolveVariablesRecursively(Scope* global_scope,
804 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000805 ASSERT(global_scope == NULL || global_scope->is_global_scope());
806
807 // Resolve unresolved variables for this scope.
808 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000809 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810 }
811
812 // Resolve unresolved variables for inner scopes.
813 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000814 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 }
816}
817
818
ager@chromium.org381abbb2009-02-25 13:23:22 +0000819bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
820 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821 if (outer_scope_calls_eval) {
822 outer_scope_calls_eval_ = true;
823 }
824
ager@chromium.org381abbb2009-02-25 13:23:22 +0000825 if (outer_scope_is_eval_scope) {
826 outer_scope_is_eval_scope_ = true;
827 }
828
829 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
830 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831 for (int i = 0; i < inner_scopes_.length(); i++) {
832 Scope* inner_scope = inner_scopes_[i];
ager@chromium.org381abbb2009-02-25 13:23:22 +0000833 if (inner_scope->PropagateScopeInfo(calls_eval, is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000834 inner_scope_calls_eval_ = true;
835 }
836 if (inner_scope->force_eager_compilation_) {
837 force_eager_compilation_ = true;
838 }
839 }
840
841 return scope_calls_eval_ || inner_scope_calls_eval_;
842}
843
844
845bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000846 // Give var a read/write use if there is a chance it might be accessed
847 // via an eval() call. This is only possible if the variable has a
848 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 if ((var->is_this() || var->name()->length() > 0) &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000850 (var->is_accessed_from_inner_scope() ||
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000851 scope_calls_eval_ || inner_scope_calls_eval_ ||
852 scope_contains_with_)) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000853 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000855 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000856 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857}
858
859
860bool Scope::MustAllocateInContext(Variable* var) {
861 // If var is accessed from an inner scope, or if there is a
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000862 // possibility that it might be accessed from the current or an inner
863 // scope (through an eval() call), it must be allocated in the
864 // context. Exception: temporary variables are not allocated in the
865 // context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 return
867 var->mode() != Variable::TEMPORARY &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000868 (var->is_accessed_from_inner_scope() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 scope_calls_eval_ || inner_scope_calls_eval_ ||
870 scope_contains_with_ || var->is_global());
871}
872
873
874bool Scope::HasArgumentsParameter() {
875 for (int i = 0; i < params_.length(); i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000876 if (params_[i]->name().is_identical_to(FACTORY->arguments_symbol()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 return true;
878 }
879 return false;
880}
881
882
883void Scope::AllocateStackSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000884 var->set_rewrite(new Slot(var, Slot::LOCAL, num_stack_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885}
886
887
888void Scope::AllocateHeapSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000889 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890}
891
892
893void Scope::AllocateParameterLocals() {
894 ASSERT(is_function_scope());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000895 Variable* arguments = LocalLookup(FACTORY->arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000897
898 // Parameters are rewritten to arguments[i] if 'arguments' is used in
899 // a non-strict mode function. Strict mode code doesn't alias arguments.
900 bool rewrite_parameters = false;
901
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
903 // 'arguments' is used. Unless there is also a parameter called
904 // 'arguments', we must be conservative and access all parameters via
905 // the arguments object: The i'th parameter is rewritten into
906 // '.arguments[i]' (*). If we have a parameter named 'arguments', a
907 // (new) value is always assigned to it via the function
908 // invocation. Then 'arguments' denotes that specific parameter value
909 // and cannot be used to access the parameters, which is why we don't
910 // need to rewrite in that case.
911 //
912 // (*) Instead of having a parameter called 'arguments', we may have an
913 // assignment to 'arguments' in the function body, at some arbitrary
914 // point in time (possibly through an 'eval()' call!). After that
915 // assignment any re-write of parameters would be invalid (was bug
916 // 881452). Thus, we introduce a shadow '.arguments'
917 // variable which also points to the arguments object. For rewrites we
918 // use '.arguments' which remains valid even if we assign to
919 // 'arguments'. To summarize: If we need to rewrite, we allocate an
920 // 'arguments' object dynamically upon function invocation. The compiler
921 // introduces 2 local variables 'arguments' and '.arguments', both of
922 // which originally point to the arguments object that was
923 // allocated. All parameters are rewritten into property accesses via
924 // the '.arguments' variable. Thus, any changes to properties of
925 // 'arguments' are reflected in the variables and vice versa. If the
926 // 'arguments' variable is changed, '.arguments' still points to the
927 // correct arguments object and the rewrites still work.
928
929 // We are using 'arguments'. Tell the code generator that is needs to
930 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000931 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000932
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000933 // In strict mode 'arguments' does not alias formal parameters.
934 // Therefore in strict mode we allocate parameters as if 'arguments'
935 // were not used.
936 rewrite_parameters = !is_strict_mode();
937 }
938
939 if (rewrite_parameters) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940 // We also need the '.arguments' shadow variable. Declare it and create
941 // and bind the corresponding proxy. It's ok to declare it only now
942 // because it's a local variable that is allocated after the parameters
943 // have been allocated.
944 //
945 // Note: This is "almost" at temporary variable but we cannot use
946 // NewTemporary() because the mode needs to be INTERNAL since this
947 // variable may be allocated in the heap-allocated context (temporaries
948 // are never allocated in the context).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000949 arguments_shadow_ = new Variable(this,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000950 FACTORY->arguments_shadow_symbol(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000951 Variable::INTERNAL,
952 true,
953 Variable::ARGUMENTS);
954 arguments_shadow_->set_is_used(true);
955 temps_.Add(arguments_shadow_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956
957 // Allocate the parameters by rewriting them into '.arguments[i]' accesses.
958 for (int i = 0; i < params_.length(); i++) {
959 Variable* var = params_[i];
960 ASSERT(var->scope() == this);
961 if (MustAllocate(var)) {
962 if (MustAllocateInContext(var)) {
963 // It is ok to set this only now, because arguments is a local
964 // variable that is allocated after the parameters have been
965 // allocated.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000966 arguments_shadow_->MarkAsAccessedFromInnerScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000968 Property* rewrite =
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000969 new Property(new VariableProxy(arguments_shadow_),
970 new Literal(Handle<Object>(Smi::FromInt(i))),
971 RelocInfo::kNoPosition,
972 Property::SYNTHETIC);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000973 rewrite->set_is_arguments_access(true);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000974 var->set_rewrite(rewrite);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 }
976 }
977
978 } else {
979 // The arguments object is not used, so we can access parameters directly.
980 // The same parameter may occur multiple times in the parameters_ list.
981 // If it does, and if it is not copied into the context object, it must
982 // receive the highest parameter index for that parameter; thus iteration
983 // order is relevant!
984 for (int i = 0; i < params_.length(); i++) {
985 Variable* var = params_[i];
986 ASSERT(var->scope() == this);
987 if (MustAllocate(var)) {
988 if (MustAllocateInContext(var)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000989 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000990 (var->AsSlot() != NULL &&
991 var->AsSlot()->type() == Slot::CONTEXT));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000992 if (var->rewrite() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 // Only set the heap allocation if the parameter has not
994 // been allocated yet.
995 AllocateHeapSlot(var);
996 }
997 } else {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000998 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000999 (var->AsSlot() != NULL &&
1000 var->AsSlot()->type() == Slot::PARAMETER));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 // Set the parameter index always, even if the parameter
1002 // was seen before! (We need to access the actual parameter
1003 // supplied for the last occurrence of a multiply declared
1004 // parameter.)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001005 var->set_rewrite(new Slot(var, Slot::PARAMETER, i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 }
1007 }
1008 }
1009 }
1010}
1011
1012
1013void Scope::AllocateNonParameterLocal(Variable* var) {
1014 ASSERT(var->scope() == this);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001015 ASSERT(var->rewrite() == NULL ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001016 (!var->IsVariable(FACTORY->result_symbol())) ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001017 (var->AsSlot() == NULL || var->AsSlot()->type() != Slot::LOCAL));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001018 if (var->rewrite() == NULL && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019 if (MustAllocateInContext(var)) {
1020 AllocateHeapSlot(var);
1021 } else {
1022 AllocateStackSlot(var);
1023 }
1024 }
1025}
1026
1027
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001029 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030 for (int i = 0; i < temps_.length(); i++) {
1031 AllocateNonParameterLocal(temps_[i]);
1032 }
1033
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001034 for (VariableMap::Entry* p = variables_.Start();
1035 p != NULL;
1036 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 Variable* var = reinterpret_cast<Variable*>(p->value);
1038 AllocateNonParameterLocal(var);
1039 }
1040
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001041 // For now, function_ must be allocated at the very end. If it gets
1042 // allocated in the context, it must be the last slot in the context,
1043 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1045 if (function_ != NULL) {
1046 AllocateNonParameterLocal(function_);
1047 }
1048}
1049
1050
1051void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052 // Allocate variables for inner scopes.
1053 for (int i = 0; i < inner_scopes_.length(); i++) {
1054 inner_scopes_[i]->AllocateVariablesRecursively();
1055 }
1056
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001057 // If scope is already resolved, we still need to allocate
1058 // variables in inner scopes which might not had been resolved yet.
1059 if (resolved()) return;
1060 // The number of slots required for variables.
1061 num_stack_slots_ = 0;
1062 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1063
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064 // Allocate variables for this scope.
1065 // Parameters must be allocated first, if any.
1066 if (is_function_scope()) AllocateParameterLocals();
1067 AllocateNonParameterLocals();
1068
1069 // Allocate context if necessary.
1070 bool must_have_local_context = false;
1071 if (scope_calls_eval_ || scope_contains_with_) {
1072 // The context for the eval() call or 'with' statement in this scope.
1073 // Unless we are in the global or an eval scope, we need a local
1074 // context even if we didn't statically allocate any locals in it,
1075 // and the compiler will access the context variable. If we are
1076 // not in an inner scope, the scope is provided from the outside.
1077 must_have_local_context = is_function_scope();
1078 }
1079
1080 // If we didn't allocate any locals in the local context, then we only
1081 // need the minimal number of slots if we must have a local context.
1082 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1083 !must_have_local_context) {
1084 num_heap_slots_ = 0;
1085 }
1086
1087 // Allocation done.
1088 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1089}
1090
1091} } // namespace v8::internal