blob: 70e11ed4399138d418f1f00c50772481ce7a3bca [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
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000364VariableProxy* Scope::NewUnresolved(Handle<String> name,
365 bool inside_with,
366 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 // Note that we must not share the unresolved variables with
368 // the same name because they may be removed selectively via
369 // RemoveUnresolved().
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000370 ASSERT(!resolved());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000371 VariableProxy* proxy = new VariableProxy(name, false, inside_with, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 unresolved_.Add(proxy);
373 return proxy;
374}
375
376
377void Scope::RemoveUnresolved(VariableProxy* var) {
378 // Most likely (always?) any variable we want to remove
379 // was just added before, so we search backwards.
380 for (int i = unresolved_.length(); i-- > 0;) {
381 if (unresolved_[i] == var) {
382 unresolved_.Remove(i);
383 return;
384 }
385 }
386}
387
388
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000389Variable* Scope::NewTemporary(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000390 ASSERT(!resolved());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000391 Variable* var =
392 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000394 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395}
396
397
398void Scope::AddDeclaration(Declaration* declaration) {
399 decls_.Add(declaration);
400}
401
402
403void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000404 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405 if (!HasIllegalRedeclaration()) {
406 illegal_redecl_ = expression;
407 }
408 ASSERT(HasIllegalRedeclaration());
409}
410
411
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000412void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 ASSERT(HasIllegalRedeclaration());
414 illegal_redecl_->Accept(visitor);
415}
416
417
418template<class Allocator>
419void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
420 // Collect variables in this scope.
421 // Note that the function_ variable - if present - is not
422 // collected here but handled separately in ScopeInfo
423 // which is the current user of this function).
424 for (int i = 0; i < temps_.length(); i++) {
425 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000426 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 locals->Add(var);
428 }
429 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000430 for (VariableMap::Entry* p = variables_.Start();
431 p != NULL;
432 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000434 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435 locals->Add(var);
436 }
437 }
438}
439
440
441// Make sure the method gets instantiated by the template system.
442template void Scope::CollectUsedVariables(
443 List<Variable*, FreeStoreAllocationPolicy>* locals);
444template void Scope::CollectUsedVariables(
445 List<Variable*, PreallocatedStorage>* locals);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000446template void Scope::CollectUsedVariables(
447 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448
449
ager@chromium.org381abbb2009-02-25 13:23:22 +0000450void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451 ASSERT(outer_scope_ == NULL); // eval or global scopes only
452
453 // 1) Propagate scope information.
454 // If we are in an eval scope, we may have other outer scopes about
455 // which we don't know anything at this point. Thus we must be conservative
456 // and assume they may invoke eval themselves. Eventually we could capture
457 // this information in the ScopeInfo and then use it here (by traversing
458 // the call chain stack, at compile time).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000459 bool eval_scope = is_eval_scope();
460 PropagateScopeInfo(eval_scope, eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461
462 // 2) Resolve variables.
463 Scope* global_scope = NULL;
464 if (is_global_scope()) global_scope = this;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000465 ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466
467 // 3) Allocate variables.
468 AllocateVariablesRecursively();
469}
470
471
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472bool Scope::AllowsLazyCompilation() const {
473 return !force_eager_compilation_ && HasTrivialOuterContext();
474}
475
476
477bool Scope::HasTrivialContext() const {
478 // A function scope has a trivial context if it always is the global
479 // context. We iteratively scan out the context chain to see if
480 // there is anything that makes this scope non-trivial; otherwise we
481 // return true.
482 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
483 if (scope->is_eval_scope()) return false;
484 if (scope->scope_inside_with_) return false;
485 if (scope->num_heap_slots_ > 0) return false;
486 }
487 return true;
488}
489
490
491bool Scope::HasTrivialOuterContext() const {
492 Scope* outer = outer_scope_;
493 if (outer == NULL) return true;
494 // Note that the outer context may be trivial in general, but the current
495 // scope may be inside a 'with' statement in which case the outer context
496 // for this scope is not trivial.
497 return !scope_inside_with_ && outer->HasTrivialContext();
498}
499
500
501int Scope::ContextChainLength(Scope* scope) {
502 int n = 0;
503 for (Scope* s = this; s != scope; s = s->outer_scope_) {
504 ASSERT(s != NULL); // scope must be in the scope chain
505 if (s->num_heap_slots() > 0) n++;
506 }
507 return n;
508}
509
510
511#ifdef DEBUG
512static const char* Header(Scope::Type type) {
513 switch (type) {
514 case Scope::EVAL_SCOPE: return "eval";
515 case Scope::FUNCTION_SCOPE: return "function";
516 case Scope::GLOBAL_SCOPE: return "global";
517 }
518 UNREACHABLE();
519 return NULL;
520}
521
522
523static void Indent(int n, const char* str) {
524 PrintF("%*s%s", n, "", str);
525}
526
527
528static void PrintName(Handle<String> name) {
529 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
530 PrintF("%s", *s);
531}
532
533
534static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000535 if (var->is_used() || var->rewrite() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536 Indent(indent, Variable::Mode2String(var->mode()));
537 PrintF(" ");
538 PrintName(var->name());
539 PrintF("; // ");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000540 if (var->rewrite() != NULL) {
541 PrintF("%s, ", printer->Print(var->rewrite()));
542 if (var->is_accessed_from_inner_scope()) PrintF(", ");
543 }
544 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 PrintF("\n");
546 }
547}
548
549
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000550static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
551 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000552 Variable* var = reinterpret_cast<Variable*>(p->value);
553 PrintVar(printer, indent, var);
554 }
555}
556
557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558void Scope::Print(int n) {
559 int n0 = (n > 0 ? n : 0);
560 int n1 = n0 + 2; // indentation
561
562 // Print header.
563 Indent(n0, Header(type_));
564 if (scope_name_->length() > 0) {
565 PrintF(" ");
566 PrintName(scope_name_);
567 }
568
569 // Print parameters, if any.
570 if (is_function_scope()) {
571 PrintF(" (");
572 for (int i = 0; i < params_.length(); i++) {
573 if (i > 0) PrintF(", ");
574 PrintName(params_[i]->name());
575 }
576 PrintF(")");
577 }
578
579 PrintF(" {\n");
580
581 // Function name, if any (named function literals, only).
582 if (function_ != NULL) {
583 Indent(n1, "// (local) function name: ");
584 PrintName(function_->name());
585 PrintF("\n");
586 }
587
588 // Scope info.
589 if (HasTrivialOuterContext()) {
590 Indent(n1, "// scope has trivial outer context\n");
591 }
592 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
593 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
594 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
595 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
596 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000597 if (outer_scope_is_eval_scope_) {
598 Indent(n1, "// outer scope is 'eval' scope\n");
599 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 if (num_stack_slots_ > 0) { Indent(n1, "// ");
601 PrintF("%d stack slots\n", num_stack_slots_); }
602 if (num_heap_slots_ > 0) { Indent(n1, "// ");
603 PrintF("%d heap slots\n", num_heap_slots_); }
604
605 // Print locals.
606 PrettyPrinter printer;
607 Indent(n1, "// function var\n");
608 if (function_ != NULL) {
609 PrintVar(&printer, n1, function_);
610 }
611
612 Indent(n1, "// temporary vars\n");
613 for (int i = 0; i < temps_.length(); i++) {
614 PrintVar(&printer, n1, temps_[i]);
615 }
616
617 Indent(n1, "// local vars\n");
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000618 PrintMap(&printer, n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000620 Indent(n1, "// dynamic vars\n");
621 if (dynamics_ != NULL) {
622 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
623 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
624 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
625 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626
627 // Print inner scopes (disable by providing negative n).
628 if (n >= 0) {
629 for (int i = 0; i < inner_scopes_.length(); i++) {
630 PrintF("\n");
631 inner_scopes_[i]->Print(n1);
632 }
633 }
634
635 Indent(n0, "}\n");
636}
637#endif // DEBUG
638
639
ager@chromium.org381abbb2009-02-25 13:23:22 +0000640Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000641 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000642 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000643 Variable* var = map->Lookup(name);
644 if (var == NULL) {
645 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000646 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000647 // Allocate it by giving it a dynamic lookup.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000648 var->set_rewrite(new Slot(var, Slot::LOOKUP, -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 return var;
651}
652
653
654// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000655// the statically resolved variable belonging to an outer scope, or
656// NULL. It may be NULL because a) we couldn't find a variable, or b)
657// because the variable is just a guess (and may be shadowed by
658// another variable that is introduced dynamically via an 'eval' call
659// or a 'with' statement).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000660Variable* Scope::LookupRecursive(Handle<String> name,
661 bool inner_lookup,
662 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 // If we find a variable, but the current scope calls 'eval', the found
664 // variable may not be the correct one (the 'eval' may introduce a
665 // property with the same name). In that case, remember that the variable
666 // found is just a guess.
667 bool guess = scope_calls_eval_;
668
669 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000670 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671
672 if (var != NULL) {
673 // We found a variable. If this is not an inner lookup, we are done.
674 // (Even if there is an 'eval' in this scope which introduces the
675 // same variable again, the resulting variable remains the same.
676 // Note that enclosing 'with' statements are handled at the call site.)
677 if (!inner_lookup)
678 return var;
679
680 } else {
681 // We did not find a variable locally. Check against the function variable,
682 // if any. We can do this for all scopes, since the function variable is
683 // only present - if at all - for function scopes.
684 //
685 // This lookup corresponds to a lookup in the "intermediate" scope sitting
686 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
687 // the name of named function literal is kept in an intermediate scope
ager@chromium.org32912102009-01-16 10:38:43 +0000688 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 if (function_ != NULL && function_->name().is_identical_to(name)) {
690 var = function_;
691
692 } else if (outer_scope_ != NULL) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000693 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694 // We may have found a variable in an outer scope. However, if
695 // the current scope is inside a 'with', the actual variable may
696 // be a property introduced via the 'with' statement. Then, the
697 // variable we may have found is just a guess.
698 if (scope_inside_with_)
699 guess = true;
700 }
701
702 // If we did not find a variable, we are done.
703 if (var == NULL)
704 return NULL;
705 }
706
707 ASSERT(var != NULL);
708
709 // If this is a lookup from an inner scope, mark the variable.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000710 if (inner_lookup) {
711 var->MarkAsAccessedFromInnerScope();
712 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000714 // If the variable we have found is just a guess, invalidate the
715 // result. If the found variable is local, record that fact so we
716 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000717 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000718 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000720 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721
722 return var;
723}
724
725
ager@chromium.org381abbb2009-02-25 13:23:22 +0000726void Scope::ResolveVariable(Scope* global_scope,
727 Handle<Context> context,
728 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729 ASSERT(global_scope == NULL || global_scope->is_global_scope());
730
731 // If the proxy is already resolved there's nothing to do
732 // (functions and consts may be resolved by the parser).
733 if (proxy->var() != NULL) return;
734
735 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000736 Variable* invalidated_local = NULL;
737 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738
739 if (proxy->inside_with()) {
740 // If we are inside a local 'with' statement, all bets are off
741 // and we cannot resolve the proxy to a local variable even if
742 // we found an outer matching variable.
743 // Note that we must do a lookup anyway, because if we find one,
744 // we must mark that variable as potentially accessed from this
745 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000746 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747
748 } else {
749 // We are not inside a local 'with' statement.
750
751 if (var == NULL) {
752 // We did not find the variable. We have a global variable
753 // if we are in the global scope (we know already that we
754 // are outside a 'with' statement) or if there is no way
755 // that the variable might be introduced dynamically (through
756 // a local or outer eval() call, or an outer 'with' statement),
757 // or we don't know about the outer scope (because we are
758 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000759 if (is_global_scope() ||
760 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
761 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762 // We must have a global variable.
763 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000764 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000765
766 } else if (scope_inside_with_) {
767 // If we are inside a with statement we give up and look up
768 // the variable at runtime.
769 var = NonLocal(proxy->name(), Variable::DYNAMIC);
770
771 } else if (invalidated_local != NULL) {
772 // No with statements are involved and we found a local
773 // variable that might be shadowed by eval introduced
774 // variables.
775 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
776 var->set_local_if_not_shadowed(invalidated_local);
777
778 } else if (outer_scope_is_eval_scope_) {
779 // No with statements and we did not find a local and the code
780 // is executed with a call to eval. The context contains
781 // scope information that we can use to determine if the
782 // variable is global if it is not shadowed by eval-introduced
783 // variables.
784 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
785 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
786
787 } else {
788 var = NonLocal(proxy->name(), Variable::DYNAMIC);
789 }
790
791 } else {
792 // No with statements and we did not find a local and the code
793 // is not executed with a call to eval. We know that this
794 // variable is global unless it is shadowed by eval-introduced
795 // variables.
796 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797 }
798 }
799 }
800
801 proxy->BindTo(var);
802}
803
804
ager@chromium.org381abbb2009-02-25 13:23:22 +0000805void Scope::ResolveVariablesRecursively(Scope* global_scope,
806 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807 ASSERT(global_scope == NULL || global_scope->is_global_scope());
808
809 // Resolve unresolved variables for this scope.
810 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000811 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812 }
813
814 // Resolve unresolved variables for inner scopes.
815 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000816 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817 }
818}
819
820
ager@chromium.org381abbb2009-02-25 13:23:22 +0000821bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
822 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823 if (outer_scope_calls_eval) {
824 outer_scope_calls_eval_ = true;
825 }
826
ager@chromium.org381abbb2009-02-25 13:23:22 +0000827 if (outer_scope_is_eval_scope) {
828 outer_scope_is_eval_scope_ = true;
829 }
830
831 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
832 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833 for (int i = 0; i < inner_scopes_.length(); i++) {
834 Scope* inner_scope = inner_scopes_[i];
ager@chromium.org381abbb2009-02-25 13:23:22 +0000835 if (inner_scope->PropagateScopeInfo(calls_eval, is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 inner_scope_calls_eval_ = true;
837 }
838 if (inner_scope->force_eager_compilation_) {
839 force_eager_compilation_ = true;
840 }
841 }
842
843 return scope_calls_eval_ || inner_scope_calls_eval_;
844}
845
846
847bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000848 // Give var a read/write use if there is a chance it might be accessed
849 // via an eval() call. This is only possible if the variable has a
850 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851 if ((var->is_this() || var->name()->length() > 0) &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000852 (var->is_accessed_from_inner_scope() ||
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000853 scope_calls_eval_ || inner_scope_calls_eval_ ||
854 scope_contains_with_)) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000855 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000857 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000858 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859}
860
861
862bool Scope::MustAllocateInContext(Variable* var) {
863 // If var is accessed from an inner scope, or if there is a
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000864 // possibility that it might be accessed from the current or an inner
865 // scope (through an eval() call), it must be allocated in the
866 // context. Exception: temporary variables are not allocated in the
867 // context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 return
869 var->mode() != Variable::TEMPORARY &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000870 (var->is_accessed_from_inner_scope() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871 scope_calls_eval_ || inner_scope_calls_eval_ ||
872 scope_contains_with_ || var->is_global());
873}
874
875
876bool Scope::HasArgumentsParameter() {
877 for (int i = 0; i < params_.length(); i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000878 if (params_[i]->name().is_identical_to(FACTORY->arguments_symbol()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879 return true;
880 }
881 return false;
882}
883
884
885void Scope::AllocateStackSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000886 var->set_rewrite(new Slot(var, Slot::LOCAL, num_stack_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887}
888
889
890void Scope::AllocateHeapSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000891 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892}
893
894
895void Scope::AllocateParameterLocals() {
896 ASSERT(is_function_scope());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000897 Variable* arguments = LocalLookup(FACTORY->arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000899
900 // Parameters are rewritten to arguments[i] if 'arguments' is used in
901 // a non-strict mode function. Strict mode code doesn't alias arguments.
902 bool rewrite_parameters = false;
903
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
905 // 'arguments' is used. Unless there is also a parameter called
906 // 'arguments', we must be conservative and access all parameters via
907 // the arguments object: The i'th parameter is rewritten into
908 // '.arguments[i]' (*). If we have a parameter named 'arguments', a
909 // (new) value is always assigned to it via the function
910 // invocation. Then 'arguments' denotes that specific parameter value
911 // and cannot be used to access the parameters, which is why we don't
912 // need to rewrite in that case.
913 //
914 // (*) Instead of having a parameter called 'arguments', we may have an
915 // assignment to 'arguments' in the function body, at some arbitrary
916 // point in time (possibly through an 'eval()' call!). After that
917 // assignment any re-write of parameters would be invalid (was bug
918 // 881452). Thus, we introduce a shadow '.arguments'
919 // variable which also points to the arguments object. For rewrites we
920 // use '.arguments' which remains valid even if we assign to
921 // 'arguments'. To summarize: If we need to rewrite, we allocate an
922 // 'arguments' object dynamically upon function invocation. The compiler
923 // introduces 2 local variables 'arguments' and '.arguments', both of
924 // which originally point to the arguments object that was
925 // allocated. All parameters are rewritten into property accesses via
926 // the '.arguments' variable. Thus, any changes to properties of
927 // 'arguments' are reflected in the variables and vice versa. If the
928 // 'arguments' variable is changed, '.arguments' still points to the
929 // correct arguments object and the rewrites still work.
930
931 // We are using 'arguments'. Tell the code generator that is needs to
932 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000933 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000935 // In strict mode 'arguments' does not alias formal parameters.
936 // Therefore in strict mode we allocate parameters as if 'arguments'
937 // were not used.
938 rewrite_parameters = !is_strict_mode();
939 }
940
941 if (rewrite_parameters) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 // We also need the '.arguments' shadow variable. Declare it and create
943 // and bind the corresponding proxy. It's ok to declare it only now
944 // because it's a local variable that is allocated after the parameters
945 // have been allocated.
946 //
947 // Note: This is "almost" at temporary variable but we cannot use
948 // NewTemporary() because the mode needs to be INTERNAL since this
949 // variable may be allocated in the heap-allocated context (temporaries
950 // are never allocated in the context).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000951 arguments_shadow_ = new Variable(this,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000952 FACTORY->arguments_shadow_symbol(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000953 Variable::INTERNAL,
954 true,
955 Variable::ARGUMENTS);
956 arguments_shadow_->set_is_used(true);
957 temps_.Add(arguments_shadow_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958
959 // Allocate the parameters by rewriting them into '.arguments[i]' accesses.
960 for (int i = 0; i < params_.length(); i++) {
961 Variable* var = params_[i];
962 ASSERT(var->scope() == this);
963 if (MustAllocate(var)) {
964 if (MustAllocateInContext(var)) {
965 // It is ok to set this only now, because arguments is a local
966 // variable that is allocated after the parameters have been
967 // allocated.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000968 arguments_shadow_->MarkAsAccessedFromInnerScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000970 Property* rewrite =
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000971 new Property(new VariableProxy(arguments_shadow_),
972 new Literal(Handle<Object>(Smi::FromInt(i))),
973 RelocInfo::kNoPosition,
974 Property::SYNTHETIC);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000975 rewrite->set_is_arguments_access(true);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000976 var->set_rewrite(rewrite);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 }
978 }
979
980 } else {
981 // The arguments object is not used, so we can access parameters directly.
982 // The same parameter may occur multiple times in the parameters_ list.
983 // If it does, and if it is not copied into the context object, it must
984 // receive the highest parameter index for that parameter; thus iteration
985 // order is relevant!
986 for (int i = 0; i < params_.length(); i++) {
987 Variable* var = params_[i];
988 ASSERT(var->scope() == this);
989 if (MustAllocate(var)) {
990 if (MustAllocateInContext(var)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000991 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000992 (var->AsSlot() != NULL &&
993 var->AsSlot()->type() == Slot::CONTEXT));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000994 if (var->rewrite() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 // Only set the heap allocation if the parameter has not
996 // been allocated yet.
997 AllocateHeapSlot(var);
998 }
999 } else {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001000 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001001 (var->AsSlot() != NULL &&
1002 var->AsSlot()->type() == Slot::PARAMETER));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 // Set the parameter index always, even if the parameter
1004 // was seen before! (We need to access the actual parameter
1005 // supplied for the last occurrence of a multiply declared
1006 // parameter.)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001007 var->set_rewrite(new Slot(var, Slot::PARAMETER, i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008 }
1009 }
1010 }
1011 }
1012}
1013
1014
1015void Scope::AllocateNonParameterLocal(Variable* var) {
1016 ASSERT(var->scope() == this);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001017 ASSERT(var->rewrite() == NULL ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001018 (!var->IsVariable(FACTORY->result_symbol())) ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001019 (var->AsSlot() == NULL || var->AsSlot()->type() != Slot::LOCAL));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001020 if (var->rewrite() == NULL && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021 if (MustAllocateInContext(var)) {
1022 AllocateHeapSlot(var);
1023 } else {
1024 AllocateStackSlot(var);
1025 }
1026 }
1027}
1028
1029
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001031 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 for (int i = 0; i < temps_.length(); i++) {
1033 AllocateNonParameterLocal(temps_[i]);
1034 }
1035
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001036 for (VariableMap::Entry* p = variables_.Start();
1037 p != NULL;
1038 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 Variable* var = reinterpret_cast<Variable*>(p->value);
1040 AllocateNonParameterLocal(var);
1041 }
1042
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001043 // For now, function_ must be allocated at the very end. If it gets
1044 // allocated in the context, it must be the last slot in the context,
1045 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1047 if (function_ != NULL) {
1048 AllocateNonParameterLocal(function_);
1049 }
1050}
1051
1052
1053void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054 // Allocate variables for inner scopes.
1055 for (int i = 0; i < inner_scopes_.length(); i++) {
1056 inner_scopes_[i]->AllocateVariablesRecursively();
1057 }
1058
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001059 // If scope is already resolved, we still need to allocate
1060 // variables in inner scopes which might not had been resolved yet.
1061 if (resolved()) return;
1062 // The number of slots required for variables.
1063 num_stack_slots_ = 0;
1064 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1065
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 // Allocate variables for this scope.
1067 // Parameters must be allocated first, if any.
1068 if (is_function_scope()) AllocateParameterLocals();
1069 AllocateNonParameterLocals();
1070
1071 // Allocate context if necessary.
1072 bool must_have_local_context = false;
1073 if (scope_calls_eval_ || scope_contains_with_) {
1074 // The context for the eval() call or 'with' statement in this scope.
1075 // Unless we are in the global or an eval scope, we need a local
1076 // context even if we didn't statically allocate any locals in it,
1077 // and the compiler will access the context variable. If we are
1078 // not in an inner scope, the scope is provided from the outside.
1079 must_have_local_context = is_function_scope();
1080 }
1081
1082 // If we didn't allocate any locals in the local context, then we only
1083 // need the minimal number of slots if we must have a local context.
1084 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1085 !must_have_local_context) {
1086 num_heap_slots_ = 0;
1087 }
1088
1089 // Allocation done.
1090 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1091}
1092
1093} } // namespace v8::internal