blob: 5546875c4bc42536ecfb36be047b104da169df5d [file] [log] [blame]
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001// Copyright 2011 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),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000122 decls_(0),
123 already_resolved_(false) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000124 SetDefaults(type, NULL, Handle<SerializedScopeInfo>::null());
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),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000134 decls_(4),
135 already_resolved_(false) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000136 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 // At some point we might want to provide outer scopes to
138 // eval scopes (by walking the stack and reading the scope info).
139 // In that case, the ASSERT below needs to be adjusted.
140 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL));
141 ASSERT(!HasIllegalRedeclaration());
142}
143
144
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000145Scope::Scope(Scope* inner_scope, Handle<SerializedScopeInfo> scope_info)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000146 : inner_scopes_(4),
147 variables_(),
148 temps_(4),
149 params_(4),
150 unresolved_(16),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000151 decls_(4),
152 already_resolved_(true) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000153 ASSERT(!scope_info.is_null());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000154 SetDefaults(FUNCTION_SCOPE, NULL, scope_info);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000155 if (scope_info->HasHeapAllocatedLocals()) {
156 num_heap_slots_ = scope_info_->NumberOfContextSlots();
157 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000158 AddInnerScope(inner_scope);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000159}
160
161
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000162Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
163 : inner_scopes_(1),
164 variables_(),
165 temps_(0),
166 params_(0),
167 unresolved_(0),
168 decls_(0),
169 already_resolved_(true) {
170 SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null());
171 AddInnerScope(inner_scope);
172 ++num_var_or_const_;
173 Variable* variable = variables_.Declare(this,
174 catch_variable_name,
175 Variable::VAR,
176 true, // Valid left-hand side.
177 Variable::NORMAL);
178 AllocateHeapSlot(variable);
179}
180
181
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000182void Scope::SetDefaults(Type type,
183 Scope* outer_scope,
184 Handle<SerializedScopeInfo> scope_info) {
185 outer_scope_ = outer_scope;
186 type_ = type;
187 scope_name_ = FACTORY->empty_symbol();
188 dynamics_ = NULL;
189 receiver_ = NULL;
190 function_ = NULL;
191 arguments_ = NULL;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000192 illegal_redecl_ = NULL;
193 scope_inside_with_ = false;
194 scope_contains_with_ = false;
195 scope_calls_eval_ = false;
196 // Inherit the strict mode from the parent scope.
197 strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
198 outer_scope_calls_eval_ = false;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000199 outer_scope_calls_non_strict_eval_ = false;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000200 inner_scope_calls_eval_ = false;
201 outer_scope_is_eval_scope_ = false;
202 force_eager_compilation_ = false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000203 num_var_or_const_ = 0;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000204 num_stack_slots_ = 0;
205 num_heap_slots_ = 0;
206 scope_info_ = scope_info;
207}
208
209
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000210Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
211 Scope* global_scope) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000212 // Reconstruct the outer scope chain from a closure's context chain.
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000213 ASSERT(!info->closure().is_null());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000214 Context* context = info->closure()->context();
215 Scope* current_scope = NULL;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000216 Scope* innermost_scope = NULL;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000217 bool contains_with = false;
218 while (!context->IsGlobalContext()) {
219 if (context->IsWithContext()) {
220 // All the inner scopes are inside a with.
221 contains_with = true;
222 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
223 s->scope_inside_with_ = true;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000224 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000225 } else {
226 if (context->IsFunctionContext()) {
227 SerializedScopeInfo* scope_info =
228 context->closure()->shared()->scope_info();
229 current_scope =
230 new Scope(current_scope, Handle<SerializedScopeInfo>(scope_info));
231 } else {
232 ASSERT(context->IsCatchContext());
233 String* name = String::cast(context->extension());
234 current_scope = new Scope(current_scope, Handle<String>(name));
235 }
236 if (contains_with) current_scope->RecordWithStatement();
237 if (innermost_scope == NULL) innermost_scope = current_scope;
238 }
239
240 // Forget about a with when we move to a context for a different function.
241 if (context->previous()->closure() != context->closure()) {
242 contains_with = false;
243 }
244 context = context->previous();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000245 }
246
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000247 global_scope->AddInnerScope(current_scope);
248 return (innermost_scope == NULL) ? global_scope : innermost_scope;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000249}
250
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000251
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000252bool Scope::Analyze(CompilationInfo* info) {
253 ASSERT(info->function() != NULL);
254 Scope* top = info->function()->scope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000255
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000256 while (top->outer_scope() != NULL) top = top->outer_scope();
257 top->AllocateVariables(info->calling_context());
258
259#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000260 if (info->isolate()->bootstrapper()->IsActive()
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000261 ? FLAG_print_builtin_scopes
262 : FLAG_print_scopes) {
263 info->function()->scope()->Print();
264 }
265#endif
266
267 info->SetScope(info->function()->scope());
268 return true; // Can not fail.
269}
270
271
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272void Scope::Initialize(bool inside_with) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000273 ASSERT(!already_resolved());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000274
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 // Add this scope as a new inner scope of the outer scope.
276 if (outer_scope_ != NULL) {
277 outer_scope_->inner_scopes_.Add(this);
278 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
279 } else {
280 scope_inside_with_ = inside_with;
281 }
282
283 // Declare convenience variables.
284 // Declare and allocate receiver (even for the global scope, and even
285 // if naccesses_ == 0).
286 // NOTE: When loading parameters in the global scope, we must take
287 // care not to access them as properties of the global object, but
288 // instead load them directly from the stack. Currently, the only
289 // such parameter is 'this' which is passed on the stack when
290 // invoking scripts
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000291 if (is_catch_scope()) {
292 ASSERT(outer_scope() != NULL);
293 receiver_ = outer_scope()->receiver();
294 } else {
295 Variable* var =
296 variables_.Declare(this, FACTORY->this_symbol(), Variable::VAR,
297 false, Variable::THIS);
298 var->set_rewrite(new Slot(var, Slot::PARAMETER, -1));
299 receiver_ = var;
300 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301
302 if (is_function_scope()) {
303 // Declare 'arguments' variable which exists in all functions.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000304 // Note that it might never be accessed, in which case it won't be
305 // allocated during variable allocation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000306 variables_.Declare(this, FACTORY->arguments_symbol(), Variable::VAR,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000307 true, Variable::ARGUMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 }
309}
310
311
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000312Variable* Scope::LocalLookup(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000313 Variable* result = variables_.Lookup(name);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000314 if (result != NULL || scope_info_.is_null()) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000315 return result;
316 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000317 // If we have a serialized scope info, we might find the variable there.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000318 //
319 // We should never lookup 'arguments' in this scope as it is implicitly
320 // present in every scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000321 ASSERT(*name != *FACTORY->arguments_symbol());
whesse@chromium.org7b260152011-06-20 15:33:18 +0000322 // There should be no local slot with the given name.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000323 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
324
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000325 // Check context slot lookup.
326 Variable::Mode mode;
327 int index = scope_info_->ContextSlotIndex(*name, &mode);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000328 if (index < 0) {
329 // Check parameters.
330 mode = Variable::VAR;
331 index = scope_info_->ParameterIndex(*name);
332 if (index < 0) {
333 // Check the function name.
334 index = scope_info_->FunctionContextSlotIndex(*name);
335 if (index < 0) return NULL;
336 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000337 }
338
whesse@chromium.org7b260152011-06-20 15:33:18 +0000339 Variable* var =
340 variables_.Declare(this, name, mode, true, Variable::NORMAL);
341 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
342 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343}
344
345
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000346Variable* Scope::Lookup(Handle<String> name) {
347 for (Scope* scope = this;
348 scope != NULL;
349 scope = scope->outer_scope()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000350 Variable* var = scope->LocalLookup(name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000351 if (var != NULL) return var;
352 }
353 return NULL;
354}
355
356
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357Variable* Scope::DeclareFunctionVar(Handle<String> name) {
358 ASSERT(is_function_scope() && function_ == NULL);
ager@chromium.org3e875802009-06-29 08:26:34 +0000359 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 return function_;
361}
362
363
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000364void Scope::DeclareParameter(Handle<String> name) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000365 ASSERT(!already_resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000366 ASSERT(is_function_scope());
367 Variable* var =
368 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
369 params_.Add(var);
370}
371
372
373Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000374 ASSERT(!already_resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000375 // This function handles VAR and CONST modes. DYNAMIC variables are
376 // introduces during variable allocation, INTERNAL variables are allocated
377 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000379 ++num_var_or_const_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000380 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
381}
382
383
384Variable* Scope::DeclareGlobal(Handle<String> name) {
385 ASSERT(is_global_scope());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000386 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000387 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388}
389
390
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000391VariableProxy* Scope::NewUnresolved(Handle<String> name,
392 bool inside_with,
393 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394 // Note that we must not share the unresolved variables with
395 // the same name because they may be removed selectively via
396 // RemoveUnresolved().
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000397 ASSERT(!already_resolved());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000398 VariableProxy* proxy = new VariableProxy(name, false, inside_with, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399 unresolved_.Add(proxy);
400 return proxy;
401}
402
403
404void Scope::RemoveUnresolved(VariableProxy* var) {
405 // Most likely (always?) any variable we want to remove
406 // was just added before, so we search backwards.
407 for (int i = unresolved_.length(); i-- > 0;) {
408 if (unresolved_[i] == var) {
409 unresolved_.Remove(i);
410 return;
411 }
412 }
413}
414
415
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000416Variable* Scope::NewTemporary(Handle<String> name) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000417 ASSERT(!already_resolved());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000418 Variable* var =
419 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000421 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422}
423
424
425void Scope::AddDeclaration(Declaration* declaration) {
426 decls_.Add(declaration);
427}
428
429
430void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000431 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 if (!HasIllegalRedeclaration()) {
433 illegal_redecl_ = expression;
434 }
435 ASSERT(HasIllegalRedeclaration());
436}
437
438
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000439void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440 ASSERT(HasIllegalRedeclaration());
441 illegal_redecl_->Accept(visitor);
442}
443
444
445template<class Allocator>
446void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
447 // Collect variables in this scope.
448 // Note that the function_ variable - if present - is not
449 // collected here but handled separately in ScopeInfo
450 // which is the current user of this function).
451 for (int i = 0; i < temps_.length(); i++) {
452 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000453 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 locals->Add(var);
455 }
456 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000457 for (VariableMap::Entry* p = variables_.Start();
458 p != NULL;
459 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000461 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 locals->Add(var);
463 }
464 }
465}
466
467
468// Make sure the method gets instantiated by the template system.
469template void Scope::CollectUsedVariables(
470 List<Variable*, FreeStoreAllocationPolicy>* locals);
471template void Scope::CollectUsedVariables(
472 List<Variable*, PreallocatedStorage>* locals);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000473template void Scope::CollectUsedVariables(
474 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
476
ager@chromium.org381abbb2009-02-25 13:23:22 +0000477void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478 ASSERT(outer_scope_ == NULL); // eval or global scopes only
479
480 // 1) Propagate scope information.
481 // If we are in an eval scope, we may have other outer scopes about
482 // which we don't know anything at this point. Thus we must be conservative
483 // and assume they may invoke eval themselves. Eventually we could capture
484 // this information in the ScopeInfo and then use it here (by traversing
485 // the call chain stack, at compile time).
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000486
ager@chromium.org381abbb2009-02-25 13:23:22 +0000487 bool eval_scope = is_eval_scope();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000488 bool outer_scope_calls_eval = false;
489 bool outer_scope_calls_non_strict_eval = false;
490 if (!is_global_scope()) {
491 context->ComputeEvalScopeInfo(&outer_scope_calls_eval,
492 &outer_scope_calls_non_strict_eval);
493 }
494 PropagateScopeInfo(outer_scope_calls_eval,
495 outer_scope_calls_non_strict_eval,
496 eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497
498 // 2) Resolve variables.
499 Scope* global_scope = NULL;
500 if (is_global_scope()) global_scope = this;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000501 ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502
503 // 3) Allocate variables.
504 AllocateVariablesRecursively();
505}
506
507
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508bool Scope::AllowsLazyCompilation() const {
509 return !force_eager_compilation_ && HasTrivialOuterContext();
510}
511
512
513bool Scope::HasTrivialContext() const {
514 // A function scope has a trivial context if it always is the global
515 // context. We iteratively scan out the context chain to see if
516 // there is anything that makes this scope non-trivial; otherwise we
517 // return true.
518 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
519 if (scope->is_eval_scope()) return false;
520 if (scope->scope_inside_with_) return false;
521 if (scope->num_heap_slots_ > 0) return false;
522 }
523 return true;
524}
525
526
527bool Scope::HasTrivialOuterContext() const {
528 Scope* outer = outer_scope_;
529 if (outer == NULL) return true;
530 // Note that the outer context may be trivial in general, but the current
531 // scope may be inside a 'with' statement in which case the outer context
532 // for this scope is not trivial.
533 return !scope_inside_with_ && outer->HasTrivialContext();
534}
535
536
537int Scope::ContextChainLength(Scope* scope) {
538 int n = 0;
539 for (Scope* s = this; s != scope; s = s->outer_scope_) {
540 ASSERT(s != NULL); // scope must be in the scope chain
541 if (s->num_heap_slots() > 0) n++;
542 }
543 return n;
544}
545
546
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000547Scope* Scope::DeclarationScope() {
548 Scope* scope = this;
549 while (scope->is_catch_scope()) {
550 scope = scope->outer_scope();
551 }
552 return scope;
553}
554
555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556#ifdef DEBUG
557static const char* Header(Scope::Type type) {
558 switch (type) {
559 case Scope::EVAL_SCOPE: return "eval";
560 case Scope::FUNCTION_SCOPE: return "function";
561 case Scope::GLOBAL_SCOPE: return "global";
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000562 case Scope::CATCH_SCOPE: return "catch";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 }
564 UNREACHABLE();
565 return NULL;
566}
567
568
569static void Indent(int n, const char* str) {
570 PrintF("%*s%s", n, "", str);
571}
572
573
574static void PrintName(Handle<String> name) {
575 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
576 PrintF("%s", *s);
577}
578
579
580static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000581 if (var->is_used() || var->rewrite() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 Indent(indent, Variable::Mode2String(var->mode()));
583 PrintF(" ");
584 PrintName(var->name());
585 PrintF("; // ");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000586 if (var->rewrite() != NULL) {
587 PrintF("%s, ", printer->Print(var->rewrite()));
588 if (var->is_accessed_from_inner_scope()) PrintF(", ");
589 }
590 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 PrintF("\n");
592 }
593}
594
595
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000596static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
597 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000598 Variable* var = reinterpret_cast<Variable*>(p->value);
599 PrintVar(printer, indent, var);
600 }
601}
602
603
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604void Scope::Print(int n) {
605 int n0 = (n > 0 ? n : 0);
606 int n1 = n0 + 2; // indentation
607
608 // Print header.
609 Indent(n0, Header(type_));
610 if (scope_name_->length() > 0) {
611 PrintF(" ");
612 PrintName(scope_name_);
613 }
614
615 // Print parameters, if any.
616 if (is_function_scope()) {
617 PrintF(" (");
618 for (int i = 0; i < params_.length(); i++) {
619 if (i > 0) PrintF(", ");
620 PrintName(params_[i]->name());
621 }
622 PrintF(")");
623 }
624
625 PrintF(" {\n");
626
627 // Function name, if any (named function literals, only).
628 if (function_ != NULL) {
629 Indent(n1, "// (local) function name: ");
630 PrintName(function_->name());
631 PrintF("\n");
632 }
633
634 // Scope info.
635 if (HasTrivialOuterContext()) {
636 Indent(n1, "// scope has trivial outer context\n");
637 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000638 if (is_strict_mode()) Indent(n1, "// strict mode scope\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
640 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
641 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
642 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000643 if (outer_scope_calls_non_strict_eval_) {
644 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
645 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000647 if (outer_scope_is_eval_scope_) {
648 Indent(n1, "// outer scope is 'eval' scope\n");
649 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 if (num_stack_slots_ > 0) { Indent(n1, "// ");
651 PrintF("%d stack slots\n", num_stack_slots_); }
652 if (num_heap_slots_ > 0) { Indent(n1, "// ");
653 PrintF("%d heap slots\n", num_heap_slots_); }
654
655 // Print locals.
656 PrettyPrinter printer;
657 Indent(n1, "// function var\n");
658 if (function_ != NULL) {
659 PrintVar(&printer, n1, function_);
660 }
661
662 Indent(n1, "// temporary vars\n");
663 for (int i = 0; i < temps_.length(); i++) {
664 PrintVar(&printer, n1, temps_[i]);
665 }
666
667 Indent(n1, "// local vars\n");
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000668 PrintMap(&printer, n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000670 Indent(n1, "// dynamic vars\n");
671 if (dynamics_ != NULL) {
672 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
673 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
674 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
675 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676
677 // Print inner scopes (disable by providing negative n).
678 if (n >= 0) {
679 for (int i = 0; i < inner_scopes_.length(); i++) {
680 PrintF("\n");
681 inner_scopes_[i]->Print(n1);
682 }
683 }
684
685 Indent(n0, "}\n");
686}
687#endif // DEBUG
688
689
ager@chromium.org381abbb2009-02-25 13:23:22 +0000690Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000691 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000692 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000693 Variable* var = map->Lookup(name);
694 if (var == NULL) {
695 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000696 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000697 // Allocate it by giving it a dynamic lookup.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000698 var->set_rewrite(new Slot(var, Slot::LOOKUP, -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700 return var;
701}
702
703
704// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000705// the statically resolved variable belonging to an outer scope, or
706// NULL. It may be NULL because a) we couldn't find a variable, or b)
707// because the variable is just a guess (and may be shadowed by
708// another variable that is introduced dynamically via an 'eval' call
709// or a 'with' statement).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000710Variable* Scope::LookupRecursive(Handle<String> name,
711 bool inner_lookup,
712 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 // If we find a variable, but the current scope calls 'eval', the found
714 // variable may not be the correct one (the 'eval' may introduce a
715 // property with the same name). In that case, remember that the variable
716 // found is just a guess.
717 bool guess = scope_calls_eval_;
718
719 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000720 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721
722 if (var != NULL) {
723 // We found a variable. If this is not an inner lookup, we are done.
724 // (Even if there is an 'eval' in this scope which introduces the
725 // same variable again, the resulting variable remains the same.
726 // Note that enclosing 'with' statements are handled at the call site.)
727 if (!inner_lookup)
728 return var;
729
730 } else {
731 // We did not find a variable locally. Check against the function variable,
732 // if any. We can do this for all scopes, since the function variable is
733 // only present - if at all - for function scopes.
734 //
735 // This lookup corresponds to a lookup in the "intermediate" scope sitting
736 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
737 // the name of named function literal is kept in an intermediate scope
ager@chromium.org32912102009-01-16 10:38:43 +0000738 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739 if (function_ != NULL && function_->name().is_identical_to(name)) {
740 var = function_;
741
742 } else if (outer_scope_ != NULL) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000743 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 // We may have found a variable in an outer scope. However, if
745 // the current scope is inside a 'with', the actual variable may
746 // be a property introduced via the 'with' statement. Then, the
747 // variable we may have found is just a guess.
748 if (scope_inside_with_)
749 guess = true;
750 }
751
752 // If we did not find a variable, we are done.
753 if (var == NULL)
754 return NULL;
755 }
756
757 ASSERT(var != NULL);
758
759 // If this is a lookup from an inner scope, mark the variable.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000760 if (inner_lookup) {
761 var->MarkAsAccessedFromInnerScope();
762 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000764 // If the variable we have found is just a guess, invalidate the
765 // result. If the found variable is local, record that fact so we
766 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000767 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000768 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000770 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771
772 return var;
773}
774
775
ager@chromium.org381abbb2009-02-25 13:23:22 +0000776void Scope::ResolveVariable(Scope* global_scope,
777 Handle<Context> context,
778 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 ASSERT(global_scope == NULL || global_scope->is_global_scope());
780
781 // If the proxy is already resolved there's nothing to do
782 // (functions and consts may be resolved by the parser).
783 if (proxy->var() != NULL) return;
784
785 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000786 Variable* invalidated_local = NULL;
787 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788
789 if (proxy->inside_with()) {
790 // If we are inside a local 'with' statement, all bets are off
791 // and we cannot resolve the proxy to a local variable even if
792 // we found an outer matching variable.
793 // Note that we must do a lookup anyway, because if we find one,
794 // we must mark that variable as potentially accessed from this
795 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000796 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797
798 } else {
799 // We are not inside a local 'with' statement.
800
801 if (var == NULL) {
802 // We did not find the variable. We have a global variable
803 // if we are in the global scope (we know already that we
804 // are outside a 'with' statement) or if there is no way
805 // that the variable might be introduced dynamically (through
806 // a local or outer eval() call, or an outer 'with' statement),
807 // or we don't know about the outer scope (because we are
808 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000809 if (is_global_scope() ||
810 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
811 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812 // We must have a global variable.
813 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000814 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000815
816 } else if (scope_inside_with_) {
817 // If we are inside a with statement we give up and look up
818 // the variable at runtime.
819 var = NonLocal(proxy->name(), Variable::DYNAMIC);
820
821 } else if (invalidated_local != NULL) {
822 // No with statements are involved and we found a local
823 // variable that might be shadowed by eval introduced
824 // variables.
825 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
826 var->set_local_if_not_shadowed(invalidated_local);
827
828 } else if (outer_scope_is_eval_scope_) {
829 // No with statements and we did not find a local and the code
830 // is executed with a call to eval. The context contains
831 // scope information that we can use to determine if the
832 // variable is global if it is not shadowed by eval-introduced
833 // variables.
834 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
835 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
836
837 } else {
838 var = NonLocal(proxy->name(), Variable::DYNAMIC);
839 }
840
841 } else {
842 // No with statements and we did not find a local and the code
843 // is not executed with a call to eval. We know that this
844 // variable is global unless it is shadowed by eval-introduced
845 // variables.
846 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847 }
848 }
849 }
850
851 proxy->BindTo(var);
852}
853
854
ager@chromium.org381abbb2009-02-25 13:23:22 +0000855void Scope::ResolveVariablesRecursively(Scope* global_scope,
856 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 ASSERT(global_scope == NULL || global_scope->is_global_scope());
858
859 // Resolve unresolved variables for this scope.
860 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000861 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 }
863
864 // Resolve unresolved variables for inner scopes.
865 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000866 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867 }
868}
869
870
ager@chromium.org381abbb2009-02-25 13:23:22 +0000871bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000872 bool outer_scope_calls_non_strict_eval,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000873 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874 if (outer_scope_calls_eval) {
875 outer_scope_calls_eval_ = true;
876 }
877
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000878 if (outer_scope_calls_non_strict_eval) {
879 outer_scope_calls_non_strict_eval_ = true;
880 }
881
ager@chromium.org381abbb2009-02-25 13:23:22 +0000882 if (outer_scope_is_eval_scope) {
883 outer_scope_is_eval_scope_ = true;
884 }
885
886 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
887 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000888 bool calls_non_strict_eval =
889 (scope_calls_eval_ && !is_strict_mode()) ||
890 outer_scope_calls_non_strict_eval_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 for (int i = 0; i < inner_scopes_.length(); i++) {
892 Scope* inner_scope = inner_scopes_[i];
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000893 if (inner_scope->PropagateScopeInfo(calls_eval,
894 calls_non_strict_eval,
895 is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 inner_scope_calls_eval_ = true;
897 }
898 if (inner_scope->force_eager_compilation_) {
899 force_eager_compilation_ = true;
900 }
901 }
902
903 return scope_calls_eval_ || inner_scope_calls_eval_;
904}
905
906
907bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000908 // Give var a read/write use if there is a chance it might be accessed
909 // via an eval() call. This is only possible if the variable has a
910 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 if ((var->is_this() || var->name()->length() > 0) &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000912 (var->is_accessed_from_inner_scope() ||
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000913 scope_calls_eval_ ||
914 inner_scope_calls_eval_ ||
915 scope_contains_with_ ||
916 is_catch_scope())) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000917 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000919 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000920 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921}
922
923
924bool Scope::MustAllocateInContext(Variable* var) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000925 // If var is accessed from an inner scope, or if there is a possibility
926 // that it might be accessed from the current or an inner scope (through
927 // an eval() call or a runtime with lookup), it must be allocated in the
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000928 // context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000929 //
930 // Exceptions: temporary variables are never allocated in a context;
931 // catch-bound variables are always allocated in a context.
932 if (var->mode() == Variable::TEMPORARY) return false;
933 if (is_catch_scope()) return true;
934 return var->is_accessed_from_inner_scope() ||
935 scope_calls_eval_ ||
936 inner_scope_calls_eval_ ||
937 scope_contains_with_ ||
938 var->is_global();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939}
940
941
942bool Scope::HasArgumentsParameter() {
943 for (int i = 0; i < params_.length(); i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000944 if (params_[i]->name().is_identical_to(FACTORY->arguments_symbol()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 return true;
946 }
947 return false;
948}
949
950
951void Scope::AllocateStackSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000952 var->set_rewrite(new Slot(var, Slot::LOCAL, num_stack_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953}
954
955
956void Scope::AllocateHeapSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000957 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958}
959
960
961void Scope::AllocateParameterLocals() {
962 ASSERT(is_function_scope());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000963 Variable* arguments = LocalLookup(FACTORY->arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000965
whesse@chromium.org7b260152011-06-20 15:33:18 +0000966 bool uses_nonstrict_arguments = false;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000967
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
969 // 'arguments' is used. Unless there is also a parameter called
whesse@chromium.org7b260152011-06-20 15:33:18 +0000970 // 'arguments', we must be conservative and allocate all parameters to
971 // the context assuming they will be captured by the arguments object.
972 // If we have a parameter named 'arguments', a (new) value is always
973 // assigned to it via the function invocation. Then 'arguments' denotes
974 // that specific parameter value and cannot be used to access the
975 // parameters, which is why we don't need to allocate an arguments
976 // object in that case.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977
978 // We are using 'arguments'. Tell the code generator that is needs to
979 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000980 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000982 // In strict mode 'arguments' does not alias formal parameters.
983 // Therefore in strict mode we allocate parameters as if 'arguments'
984 // were not used.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000985 uses_nonstrict_arguments = !is_strict_mode();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000986 }
987
whesse@chromium.org7b260152011-06-20 15:33:18 +0000988 // The same parameter may occur multiple times in the parameters_ list.
989 // If it does, and if it is not copied into the context object, it must
990 // receive the highest parameter index for that parameter; thus iteration
991 // order is relevant!
992 for (int i = params_.length() - 1; i >= 0; --i) {
993 Variable* var = params_[i];
994 ASSERT(var->scope() == this);
995 if (uses_nonstrict_arguments) {
996 // Give the parameter a use from an inner scope, to force allocation
997 // to the context.
998 var->MarkAsAccessedFromInnerScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999 }
1000
whesse@chromium.org7b260152011-06-20 15:33:18 +00001001 if (MustAllocate(var)) {
1002 if (MustAllocateInContext(var)) {
1003 ASSERT(var->rewrite() == NULL || var->IsContextSlot());
1004 if (var->rewrite() == NULL) {
1005 AllocateHeapSlot(var);
1006 }
1007 } else {
1008 ASSERT(var->rewrite() == NULL || var->IsParameter());
1009 if (var->rewrite() == NULL) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001010 var->set_rewrite(new Slot(var, Slot::PARAMETER, i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 }
1012 }
1013 }
1014 }
1015}
1016
1017
1018void Scope::AllocateNonParameterLocal(Variable* var) {
1019 ASSERT(var->scope() == this);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001020 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org7b260152011-06-20 15:33:18 +00001021 !var->IsVariable(FACTORY->result_symbol()) ||
1022 var->AsSlot() == NULL ||
1023 var->AsSlot()->type() != Slot::LOCAL);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001024 if (var->rewrite() == NULL && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 if (MustAllocateInContext(var)) {
1026 AllocateHeapSlot(var);
1027 } else {
1028 AllocateStackSlot(var);
1029 }
1030 }
1031}
1032
1033
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001035 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 for (int i = 0; i < temps_.length(); i++) {
1037 AllocateNonParameterLocal(temps_[i]);
1038 }
1039
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001040 for (VariableMap::Entry* p = variables_.Start();
1041 p != NULL;
1042 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043 Variable* var = reinterpret_cast<Variable*>(p->value);
1044 AllocateNonParameterLocal(var);
1045 }
1046
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001047 // For now, function_ must be allocated at the very end. If it gets
1048 // allocated in the context, it must be the last slot in the context,
1049 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001050 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1051 if (function_ != NULL) {
1052 AllocateNonParameterLocal(function_);
1053 }
1054}
1055
1056
1057void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058 // Allocate variables for inner scopes.
1059 for (int i = 0; i < inner_scopes_.length(); i++) {
1060 inner_scopes_[i]->AllocateVariablesRecursively();
1061 }
1062
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001063 // If scope is already resolved, we still need to allocate
1064 // variables in inner scopes which might not had been resolved yet.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001065 if (already_resolved()) return;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001066 // The number of slots required for variables.
1067 num_stack_slots_ = 0;
1068 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1069
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 // Allocate variables for this scope.
1071 // Parameters must be allocated first, if any.
1072 if (is_function_scope()) AllocateParameterLocals();
1073 AllocateNonParameterLocals();
1074
1075 // Allocate context if necessary.
1076 bool must_have_local_context = false;
1077 if (scope_calls_eval_ || scope_contains_with_) {
1078 // The context for the eval() call or 'with' statement in this scope.
1079 // Unless we are in the global or an eval scope, we need a local
1080 // context even if we didn't statically allocate any locals in it,
1081 // and the compiler will access the context variable. If we are
1082 // not in an inner scope, the scope is provided from the outside.
1083 must_have_local_context = is_function_scope();
1084 }
1085
1086 // If we didn't allocate any locals in the local context, then we only
1087 // need the minimal number of slots if we must have a local context.
1088 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1089 !must_have_local_context) {
1090 num_heap_slots_ = 0;
1091 }
1092
1093 // Allocation done.
1094 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1095}
1096
1097} } // namespace v8::internal