blob: 74d0c2a2e258f4140fa7f0b344b4eadfa8172854 [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),
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000122 decls_(0) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000123 SetDefaults(type, NULL, Handle<SerializedScopeInfo>::null());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000124 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) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000135 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::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.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),
151 decls_(4) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000152 ASSERT(!scope_info.is_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);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000160}
161
162
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000163void Scope::SetDefaults(Type type,
164 Scope* outer_scope,
165 Handle<SerializedScopeInfo> scope_info) {
166 outer_scope_ = outer_scope;
167 type_ = type;
168 scope_name_ = FACTORY->empty_symbol();
169 dynamics_ = NULL;
170 receiver_ = NULL;
171 function_ = NULL;
172 arguments_ = NULL;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000173 illegal_redecl_ = NULL;
174 scope_inside_with_ = false;
175 scope_contains_with_ = false;
176 scope_calls_eval_ = false;
177 // Inherit the strict mode from the parent scope.
178 strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
179 outer_scope_calls_eval_ = false;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000180 outer_scope_calls_non_strict_eval_ = false;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000181 inner_scope_calls_eval_ = false;
182 outer_scope_is_eval_scope_ = false;
183 force_eager_compilation_ = false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000184 num_var_or_const_ = 0;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000185 num_stack_slots_ = 0;
186 num_heap_slots_ = 0;
187 scope_info_ = scope_info;
188}
189
190
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000191Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
192 Scope* global_scope) {
193 ASSERT(!info->closure().is_null());
194 // If we have a serialized scope info, reuse it.
195 Scope* innermost_scope = NULL;
196 Scope* scope = NULL;
197
198 SerializedScopeInfo* scope_info = info->closure()->shared()->scope_info();
199 if (scope_info != SerializedScopeInfo::Empty()) {
200 JSFunction* current = *info->closure();
201 do {
202 current = current->context()->closure();
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000203 Handle<SerializedScopeInfo> scope_info(current->shared()->scope_info());
204 if (*scope_info != SerializedScopeInfo::Empty()) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000205 scope = new Scope(scope, scope_info);
206 if (innermost_scope == NULL) innermost_scope = scope;
207 } else {
208 ASSERT(current->context()->IsGlobalContext());
209 }
210 } while (!current->context()->IsGlobalContext());
211 }
212
213 global_scope->AddInnerScope(scope);
214 if (innermost_scope == NULL) innermost_scope = global_scope;
215
216 return innermost_scope;
217}
218
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000219
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000220bool Scope::Analyze(CompilationInfo* info) {
221 ASSERT(info->function() != NULL);
222 Scope* top = info->function()->scope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000223
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000224 while (top->outer_scope() != NULL) top = top->outer_scope();
225 top->AllocateVariables(info->calling_context());
226
227#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000228 if (info->isolate()->bootstrapper()->IsActive()
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000229 ? FLAG_print_builtin_scopes
230 : FLAG_print_scopes) {
231 info->function()->scope()->Print();
232 }
233#endif
234
235 info->SetScope(info->function()->scope());
236 return true; // Can not fail.
237}
238
239
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240void Scope::Initialize(bool inside_with) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000241 ASSERT(!resolved());
242
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 // Add this scope as a new inner scope of the outer scope.
244 if (outer_scope_ != NULL) {
245 outer_scope_->inner_scopes_.Add(this);
246 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
247 } else {
248 scope_inside_with_ = inside_with;
249 }
250
251 // Declare convenience variables.
252 // Declare and allocate receiver (even for the global scope, and even
253 // if naccesses_ == 0).
254 // NOTE: When loading parameters in the global scope, we must take
255 // care not to access them as properties of the global object, but
256 // instead load them directly from the stack. Currently, the only
257 // such parameter is 'this' which is passed on the stack when
258 // invoking scripts
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000259 Variable* var =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000260 variables_.Declare(this, FACTORY->this_symbol(), Variable::VAR,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000261 false, Variable::THIS);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000262 var->set_rewrite(new Slot(var, Slot::PARAMETER, -1));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000263 receiver_ = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264
265 if (is_function_scope()) {
266 // Declare 'arguments' variable which exists in all functions.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000267 // Note that it might never be accessed, in which case it won't be
268 // allocated during variable allocation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000269 variables_.Declare(this, FACTORY->arguments_symbol(), Variable::VAR,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000270 true, Variable::ARGUMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271 }
272}
273
274
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000275Variable* Scope::LocalLookup(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000276 Variable* result = variables_.Lookup(name);
277 if (result != NULL || !resolved()) {
278 return result;
279 }
whesse@chromium.org7b260152011-06-20 15:33:18 +0000280 // If the scope is resolved, we can find a variable in serialized scope
281 // info.
282 //
283 // We should never lookup 'arguments' in this scope as it is implicitly
284 // present in every scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000285 ASSERT(*name != *FACTORY->arguments_symbol());
whesse@chromium.org7b260152011-06-20 15:33:18 +0000286 // There should be no local slot with the given name.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000287 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
288
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000289 // Check context slot lookup.
290 Variable::Mode mode;
291 int index = scope_info_->ContextSlotIndex(*name, &mode);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000292 if (index < 0) {
293 // Check parameters.
294 mode = Variable::VAR;
295 index = scope_info_->ParameterIndex(*name);
296 if (index < 0) {
297 // Check the function name.
298 index = scope_info_->FunctionContextSlotIndex(*name);
299 if (index < 0) return NULL;
300 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000301 }
302
whesse@chromium.org7b260152011-06-20 15:33:18 +0000303 Variable* var =
304 variables_.Declare(this, name, mode, true, Variable::NORMAL);
305 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
306 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307}
308
309
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000310Variable* Scope::Lookup(Handle<String> name) {
311 for (Scope* scope = this;
312 scope != NULL;
313 scope = scope->outer_scope()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000314 Variable* var = scope->LocalLookup(name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000315 if (var != NULL) return var;
316 }
317 return NULL;
318}
319
320
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321Variable* Scope::DeclareFunctionVar(Handle<String> name) {
322 ASSERT(is_function_scope() && function_ == NULL);
ager@chromium.org3e875802009-06-29 08:26:34 +0000323 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 return function_;
325}
326
327
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000328void Scope::DeclareParameter(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000329 ASSERT(!resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000330 ASSERT(is_function_scope());
331 Variable* var =
332 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
333 params_.Add(var);
334}
335
336
337Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
338 ASSERT(!resolved());
339 // This function handles VAR and CONST modes. DYNAMIC variables are
340 // introduces during variable allocation, INTERNAL variables are allocated
341 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000343 ++num_var_or_const_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000344 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
345}
346
347
348Variable* Scope::DeclareGlobal(Handle<String> name) {
349 ASSERT(is_global_scope());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000350 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000351 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352}
353
354
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000355VariableProxy* Scope::NewUnresolved(Handle<String> name,
356 bool inside_with,
357 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358 // Note that we must not share the unresolved variables with
359 // the same name because they may be removed selectively via
360 // RemoveUnresolved().
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000361 ASSERT(!resolved());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000362 VariableProxy* proxy = new VariableProxy(name, false, inside_with, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 unresolved_.Add(proxy);
364 return proxy;
365}
366
367
368void Scope::RemoveUnresolved(VariableProxy* var) {
369 // Most likely (always?) any variable we want to remove
370 // was just added before, so we search backwards.
371 for (int i = unresolved_.length(); i-- > 0;) {
372 if (unresolved_[i] == var) {
373 unresolved_.Remove(i);
374 return;
375 }
376 }
377}
378
379
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000380Variable* Scope::NewTemporary(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000381 ASSERT(!resolved());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000382 Variable* var =
383 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000385 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386}
387
388
389void Scope::AddDeclaration(Declaration* declaration) {
390 decls_.Add(declaration);
391}
392
393
394void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000395 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 if (!HasIllegalRedeclaration()) {
397 illegal_redecl_ = expression;
398 }
399 ASSERT(HasIllegalRedeclaration());
400}
401
402
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000403void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404 ASSERT(HasIllegalRedeclaration());
405 illegal_redecl_->Accept(visitor);
406}
407
408
409template<class Allocator>
410void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
411 // Collect variables in this scope.
412 // Note that the function_ variable - if present - is not
413 // collected here but handled separately in ScopeInfo
414 // which is the current user of this function).
415 for (int i = 0; i < temps_.length(); i++) {
416 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000417 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418 locals->Add(var);
419 }
420 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000421 for (VariableMap::Entry* p = variables_.Start();
422 p != NULL;
423 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000425 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 locals->Add(var);
427 }
428 }
429}
430
431
432// Make sure the method gets instantiated by the template system.
433template void Scope::CollectUsedVariables(
434 List<Variable*, FreeStoreAllocationPolicy>* locals);
435template void Scope::CollectUsedVariables(
436 List<Variable*, PreallocatedStorage>* locals);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000437template void Scope::CollectUsedVariables(
438 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439
440
ager@chromium.org381abbb2009-02-25 13:23:22 +0000441void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 ASSERT(outer_scope_ == NULL); // eval or global scopes only
443
444 // 1) Propagate scope information.
445 // If we are in an eval scope, we may have other outer scopes about
446 // which we don't know anything at this point. Thus we must be conservative
447 // and assume they may invoke eval themselves. Eventually we could capture
448 // this information in the ScopeInfo and then use it here (by traversing
449 // the call chain stack, at compile time).
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000450
ager@chromium.org381abbb2009-02-25 13:23:22 +0000451 bool eval_scope = is_eval_scope();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000452 bool outer_scope_calls_eval = false;
453 bool outer_scope_calls_non_strict_eval = false;
454 if (!is_global_scope()) {
455 context->ComputeEvalScopeInfo(&outer_scope_calls_eval,
456 &outer_scope_calls_non_strict_eval);
457 }
458 PropagateScopeInfo(outer_scope_calls_eval,
459 outer_scope_calls_non_strict_eval,
460 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 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000592 if (is_strict_mode()) Indent(n1, "// strict mode scope\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
594 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
595 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
596 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000597 if (outer_scope_calls_non_strict_eval_) {
598 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
599 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000601 if (outer_scope_is_eval_scope_) {
602 Indent(n1, "// outer scope is 'eval' scope\n");
603 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 if (num_stack_slots_ > 0) { Indent(n1, "// ");
605 PrintF("%d stack slots\n", num_stack_slots_); }
606 if (num_heap_slots_ > 0) { Indent(n1, "// ");
607 PrintF("%d heap slots\n", num_heap_slots_); }
608
609 // Print locals.
610 PrettyPrinter printer;
611 Indent(n1, "// function var\n");
612 if (function_ != NULL) {
613 PrintVar(&printer, n1, function_);
614 }
615
616 Indent(n1, "// temporary vars\n");
617 for (int i = 0; i < temps_.length(); i++) {
618 PrintVar(&printer, n1, temps_[i]);
619 }
620
621 Indent(n1, "// local vars\n");
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000622 PrintMap(&printer, n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000623
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000624 Indent(n1, "// dynamic vars\n");
625 if (dynamics_ != NULL) {
626 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
627 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
628 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
629 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630
631 // Print inner scopes (disable by providing negative n).
632 if (n >= 0) {
633 for (int i = 0; i < inner_scopes_.length(); i++) {
634 PrintF("\n");
635 inner_scopes_[i]->Print(n1);
636 }
637 }
638
639 Indent(n0, "}\n");
640}
641#endif // DEBUG
642
643
ager@chromium.org381abbb2009-02-25 13:23:22 +0000644Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000645 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000646 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000647 Variable* var = map->Lookup(name);
648 if (var == NULL) {
649 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000650 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000651 // Allocate it by giving it a dynamic lookup.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000652 var->set_rewrite(new Slot(var, Slot::LOOKUP, -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 return var;
655}
656
657
658// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000659// the statically resolved variable belonging to an outer scope, or
660// NULL. It may be NULL because a) we couldn't find a variable, or b)
661// because the variable is just a guess (and may be shadowed by
662// another variable that is introduced dynamically via an 'eval' call
663// or a 'with' statement).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000664Variable* Scope::LookupRecursive(Handle<String> name,
665 bool inner_lookup,
666 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667 // If we find a variable, but the current scope calls 'eval', the found
668 // variable may not be the correct one (the 'eval' may introduce a
669 // property with the same name). In that case, remember that the variable
670 // found is just a guess.
671 bool guess = scope_calls_eval_;
672
673 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000674 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675
676 if (var != NULL) {
677 // We found a variable. If this is not an inner lookup, we are done.
678 // (Even if there is an 'eval' in this scope which introduces the
679 // same variable again, the resulting variable remains the same.
680 // Note that enclosing 'with' statements are handled at the call site.)
681 if (!inner_lookup)
682 return var;
683
684 } else {
685 // We did not find a variable locally. Check against the function variable,
686 // if any. We can do this for all scopes, since the function variable is
687 // only present - if at all - for function scopes.
688 //
689 // This lookup corresponds to a lookup in the "intermediate" scope sitting
690 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
691 // the name of named function literal is kept in an intermediate scope
ager@chromium.org32912102009-01-16 10:38:43 +0000692 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 if (function_ != NULL && function_->name().is_identical_to(name)) {
694 var = function_;
695
696 } else if (outer_scope_ != NULL) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000697 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 // We may have found a variable in an outer scope. However, if
699 // the current scope is inside a 'with', the actual variable may
700 // be a property introduced via the 'with' statement. Then, the
701 // variable we may have found is just a guess.
702 if (scope_inside_with_)
703 guess = true;
704 }
705
706 // If we did not find a variable, we are done.
707 if (var == NULL)
708 return NULL;
709 }
710
711 ASSERT(var != NULL);
712
713 // If this is a lookup from an inner scope, mark the variable.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000714 if (inner_lookup) {
715 var->MarkAsAccessedFromInnerScope();
716 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000718 // If the variable we have found is just a guess, invalidate the
719 // result. If the found variable is local, record that fact so we
720 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000721 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000722 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000724 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725
726 return var;
727}
728
729
ager@chromium.org381abbb2009-02-25 13:23:22 +0000730void Scope::ResolveVariable(Scope* global_scope,
731 Handle<Context> context,
732 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733 ASSERT(global_scope == NULL || global_scope->is_global_scope());
734
735 // If the proxy is already resolved there's nothing to do
736 // (functions and consts may be resolved by the parser).
737 if (proxy->var() != NULL) return;
738
739 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000740 Variable* invalidated_local = NULL;
741 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742
743 if (proxy->inside_with()) {
744 // If we are inside a local 'with' statement, all bets are off
745 // and we cannot resolve the proxy to a local variable even if
746 // we found an outer matching variable.
747 // Note that we must do a lookup anyway, because if we find one,
748 // we must mark that variable as potentially accessed from this
749 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000750 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751
752 } else {
753 // We are not inside a local 'with' statement.
754
755 if (var == NULL) {
756 // We did not find the variable. We have a global variable
757 // if we are in the global scope (we know already that we
758 // are outside a 'with' statement) or if there is no way
759 // that the variable might be introduced dynamically (through
760 // a local or outer eval() call, or an outer 'with' statement),
761 // or we don't know about the outer scope (because we are
762 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000763 if (is_global_scope() ||
764 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
765 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766 // We must have a global variable.
767 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000768 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000769
770 } else if (scope_inside_with_) {
771 // If we are inside a with statement we give up and look up
772 // the variable at runtime.
773 var = NonLocal(proxy->name(), Variable::DYNAMIC);
774
775 } else if (invalidated_local != NULL) {
776 // No with statements are involved and we found a local
777 // variable that might be shadowed by eval introduced
778 // variables.
779 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
780 var->set_local_if_not_shadowed(invalidated_local);
781
782 } else if (outer_scope_is_eval_scope_) {
783 // No with statements and we did not find a local and the code
784 // is executed with a call to eval. The context contains
785 // scope information that we can use to determine if the
786 // variable is global if it is not shadowed by eval-introduced
787 // variables.
788 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
789 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
790
791 } else {
792 var = NonLocal(proxy->name(), Variable::DYNAMIC);
793 }
794
795 } else {
796 // No with statements and we did not find a local and the code
797 // is not executed with a call to eval. We know that this
798 // variable is global unless it is shadowed by eval-introduced
799 // variables.
800 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801 }
802 }
803 }
804
805 proxy->BindTo(var);
806}
807
808
ager@chromium.org381abbb2009-02-25 13:23:22 +0000809void Scope::ResolveVariablesRecursively(Scope* global_scope,
810 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811 ASSERT(global_scope == NULL || global_scope->is_global_scope());
812
813 // Resolve unresolved variables for this scope.
814 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000815 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816 }
817
818 // Resolve unresolved variables for inner scopes.
819 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000820 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821 }
822}
823
824
ager@chromium.org381abbb2009-02-25 13:23:22 +0000825bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000826 bool outer_scope_calls_non_strict_eval,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000827 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 if (outer_scope_calls_eval) {
829 outer_scope_calls_eval_ = true;
830 }
831
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000832 if (outer_scope_calls_non_strict_eval) {
833 outer_scope_calls_non_strict_eval_ = true;
834 }
835
ager@chromium.org381abbb2009-02-25 13:23:22 +0000836 if (outer_scope_is_eval_scope) {
837 outer_scope_is_eval_scope_ = true;
838 }
839
840 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
841 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000842 bool calls_non_strict_eval =
843 (scope_calls_eval_ && !is_strict_mode()) ||
844 outer_scope_calls_non_strict_eval_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845 for (int i = 0; i < inner_scopes_.length(); i++) {
846 Scope* inner_scope = inner_scopes_[i];
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000847 if (inner_scope->PropagateScopeInfo(calls_eval,
848 calls_non_strict_eval,
849 is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850 inner_scope_calls_eval_ = true;
851 }
852 if (inner_scope->force_eager_compilation_) {
853 force_eager_compilation_ = true;
854 }
855 }
856
857 return scope_calls_eval_ || inner_scope_calls_eval_;
858}
859
860
861bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000862 // Give var a read/write use if there is a chance it might be accessed
863 // via an eval() call. This is only possible if the variable has a
864 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 if ((var->is_this() || var->name()->length() > 0) &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000866 (var->is_accessed_from_inner_scope() ||
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000867 scope_calls_eval_ || inner_scope_calls_eval_ ||
868 scope_contains_with_)) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000869 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000871 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000872 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873}
874
875
876bool Scope::MustAllocateInContext(Variable* var) {
877 // If var is accessed from an inner scope, or if there is a
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000878 // possibility that it might be accessed from the current or an inner
879 // scope (through an eval() call), it must be allocated in the
880 // context. Exception: temporary variables are not allocated in the
881 // context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 return
883 var->mode() != Variable::TEMPORARY &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000884 (var->is_accessed_from_inner_scope() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 scope_calls_eval_ || inner_scope_calls_eval_ ||
886 scope_contains_with_ || var->is_global());
887}
888
889
890bool Scope::HasArgumentsParameter() {
891 for (int i = 0; i < params_.length(); i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000892 if (params_[i]->name().is_identical_to(FACTORY->arguments_symbol()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 return true;
894 }
895 return false;
896}
897
898
899void Scope::AllocateStackSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000900 var->set_rewrite(new Slot(var, Slot::LOCAL, num_stack_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901}
902
903
904void Scope::AllocateHeapSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000905 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906}
907
908
909void Scope::AllocateParameterLocals() {
910 ASSERT(is_function_scope());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000911 Variable* arguments = LocalLookup(FACTORY->arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000913
whesse@chromium.org7b260152011-06-20 15:33:18 +0000914 bool uses_nonstrict_arguments = false;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000915
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
917 // 'arguments' is used. Unless there is also a parameter called
whesse@chromium.org7b260152011-06-20 15:33:18 +0000918 // 'arguments', we must be conservative and allocate all parameters to
919 // the context assuming they will be captured by the arguments object.
920 // If we have a parameter named 'arguments', a (new) value is always
921 // assigned to it via the function invocation. Then 'arguments' denotes
922 // that specific parameter value and cannot be used to access the
923 // parameters, which is why we don't need to allocate an arguments
924 // object in that case.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925
926 // We are using 'arguments'. Tell the code generator that is needs to
927 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000928 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000930 // In strict mode 'arguments' does not alias formal parameters.
931 // Therefore in strict mode we allocate parameters as if 'arguments'
932 // were not used.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000933 uses_nonstrict_arguments = !is_strict_mode();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000934 }
935
whesse@chromium.org7b260152011-06-20 15:33:18 +0000936 // The same parameter may occur multiple times in the parameters_ list.
937 // If it does, and if it is not copied into the context object, it must
938 // receive the highest parameter index for that parameter; thus iteration
939 // order is relevant!
940 for (int i = params_.length() - 1; i >= 0; --i) {
941 Variable* var = params_[i];
942 ASSERT(var->scope() == this);
943 if (uses_nonstrict_arguments) {
944 // Give the parameter a use from an inner scope, to force allocation
945 // to the context.
946 var->MarkAsAccessedFromInnerScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947 }
948
whesse@chromium.org7b260152011-06-20 15:33:18 +0000949 if (MustAllocate(var)) {
950 if (MustAllocateInContext(var)) {
951 ASSERT(var->rewrite() == NULL || var->IsContextSlot());
952 if (var->rewrite() == NULL) {
953 AllocateHeapSlot(var);
954 }
955 } else {
956 ASSERT(var->rewrite() == NULL || var->IsParameter());
957 if (var->rewrite() == NULL) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000958 var->set_rewrite(new Slot(var, Slot::PARAMETER, i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959 }
960 }
961 }
962 }
963}
964
965
966void Scope::AllocateNonParameterLocal(Variable* var) {
967 ASSERT(var->scope() == this);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000968 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org7b260152011-06-20 15:33:18 +0000969 !var->IsVariable(FACTORY->result_symbol()) ||
970 var->AsSlot() == NULL ||
971 var->AsSlot()->type() != Slot::LOCAL);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000972 if (var->rewrite() == NULL && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 if (MustAllocateInContext(var)) {
974 AllocateHeapSlot(var);
975 } else {
976 AllocateStackSlot(var);
977 }
978 }
979}
980
981
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000983 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 for (int i = 0; i < temps_.length(); i++) {
985 AllocateNonParameterLocal(temps_[i]);
986 }
987
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000988 for (VariableMap::Entry* p = variables_.Start();
989 p != NULL;
990 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 Variable* var = reinterpret_cast<Variable*>(p->value);
992 AllocateNonParameterLocal(var);
993 }
994
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000995 // For now, function_ must be allocated at the very end. If it gets
996 // allocated in the context, it must be the last slot in the context,
997 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
999 if (function_ != NULL) {
1000 AllocateNonParameterLocal(function_);
1001 }
1002}
1003
1004
1005void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 // Allocate variables for inner scopes.
1007 for (int i = 0; i < inner_scopes_.length(); i++) {
1008 inner_scopes_[i]->AllocateVariablesRecursively();
1009 }
1010
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001011 // If scope is already resolved, we still need to allocate
1012 // variables in inner scopes which might not had been resolved yet.
1013 if (resolved()) return;
1014 // The number of slots required for variables.
1015 num_stack_slots_ = 0;
1016 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1017
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018 // Allocate variables for this scope.
1019 // Parameters must be allocated first, if any.
1020 if (is_function_scope()) AllocateParameterLocals();
1021 AllocateNonParameterLocals();
1022
1023 // Allocate context if necessary.
1024 bool must_have_local_context = false;
1025 if (scope_calls_eval_ || scope_contains_with_) {
1026 // The context for the eval() call or 'with' statement in this scope.
1027 // Unless we are in the global or an eval scope, we need a local
1028 // context even if we didn't statically allocate any locals in it,
1029 // and the compiler will access the context variable. If we are
1030 // not in an inner scope, the scope is provided from the outside.
1031 must_have_local_context = is_function_scope();
1032 }
1033
1034 // If we didn't allocate any locals in the local context, then we only
1035 // need the minimal number of slots if we must have a local context.
1036 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1037 !must_have_local_context) {
1038 num_heap_slots_ = 0;
1039 }
1040
1041 // Allocation done.
1042 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1043}
1044
1045} } // namespace v8::internal