blob: 7eadab0152c8ab8bf165f233d2c806d58646179e [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);
160
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000161 // This scope's arguments shadow (if present) is context-allocated if an inner
162 // scope accesses this one's parameters. Allocate the arguments_shadow_
163 // variable if necessary.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 Isolate* isolate = Isolate::Current();
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000165 Variable::Mode mode;
166 int arguments_shadow_index =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000167 scope_info_->ContextSlotIndex(
168 isolate->heap()->arguments_shadow_symbol(), &mode);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000169 if (arguments_shadow_index >= 0) {
170 ASSERT(mode == Variable::INTERNAL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000171 arguments_shadow_ = new Variable(
172 this,
173 isolate->factory()->arguments_shadow_symbol(),
174 Variable::INTERNAL,
175 true,
176 Variable::ARGUMENTS);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000177 arguments_shadow_->set_rewrite(
178 new Slot(arguments_shadow_, Slot::CONTEXT, arguments_shadow_index));
179 arguments_shadow_->set_is_used(true);
180 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000181}
182
183
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000184void Scope::SetDefaults(Type type,
185 Scope* outer_scope,
186 Handle<SerializedScopeInfo> scope_info) {
187 outer_scope_ = outer_scope;
188 type_ = type;
189 scope_name_ = FACTORY->empty_symbol();
190 dynamics_ = NULL;
191 receiver_ = NULL;
192 function_ = NULL;
193 arguments_ = NULL;
194 arguments_shadow_ = NULL;
195 illegal_redecl_ = NULL;
196 scope_inside_with_ = false;
197 scope_contains_with_ = false;
198 scope_calls_eval_ = false;
199 // Inherit the strict mode from the parent scope.
200 strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
201 outer_scope_calls_eval_ = false;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000202 outer_scope_calls_non_strict_eval_ = false;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000203 inner_scope_calls_eval_ = false;
204 outer_scope_is_eval_scope_ = false;
205 force_eager_compilation_ = false;
206 num_stack_slots_ = 0;
207 num_heap_slots_ = 0;
208 scope_info_ = scope_info;
209}
210
211
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000212Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
213 Scope* global_scope) {
214 ASSERT(!info->closure().is_null());
215 // If we have a serialized scope info, reuse it.
216 Scope* innermost_scope = NULL;
217 Scope* scope = NULL;
218
219 SerializedScopeInfo* scope_info = info->closure()->shared()->scope_info();
220 if (scope_info != SerializedScopeInfo::Empty()) {
221 JSFunction* current = *info->closure();
222 do {
223 current = current->context()->closure();
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000224 Handle<SerializedScopeInfo> scope_info(current->shared()->scope_info());
225 if (*scope_info != SerializedScopeInfo::Empty()) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000226 scope = new Scope(scope, scope_info);
227 if (innermost_scope == NULL) innermost_scope = scope;
228 } else {
229 ASSERT(current->context()->IsGlobalContext());
230 }
231 } while (!current->context()->IsGlobalContext());
232 }
233
234 global_scope->AddInnerScope(scope);
235 if (innermost_scope == NULL) innermost_scope = global_scope;
236
237 return innermost_scope;
238}
239
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000240
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000241bool Scope::Analyze(CompilationInfo* info) {
242 ASSERT(info->function() != NULL);
243 Scope* top = info->function()->scope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000244
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000245 while (top->outer_scope() != NULL) top = top->outer_scope();
246 top->AllocateVariables(info->calling_context());
247
248#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000249 if (info->isolate()->bootstrapper()->IsActive()
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000250 ? FLAG_print_builtin_scopes
251 : FLAG_print_scopes) {
252 info->function()->scope()->Print();
253 }
254#endif
255
256 info->SetScope(info->function()->scope());
257 return true; // Can not fail.
258}
259
260
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261void Scope::Initialize(bool inside_with) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000262 ASSERT(!resolved());
263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 // Add this scope as a new inner scope of the outer scope.
265 if (outer_scope_ != NULL) {
266 outer_scope_->inner_scopes_.Add(this);
267 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
268 } else {
269 scope_inside_with_ = inside_with;
270 }
271
272 // Declare convenience variables.
273 // Declare and allocate receiver (even for the global scope, and even
274 // if naccesses_ == 0).
275 // NOTE: When loading parameters in the global scope, we must take
276 // care not to access them as properties of the global object, but
277 // instead load them directly from the stack. Currently, the only
278 // such parameter is 'this' which is passed on the stack when
279 // invoking scripts
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000280 Variable* var =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000281 variables_.Declare(this, FACTORY->this_symbol(), Variable::VAR,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000282 false, Variable::THIS);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000283 var->set_rewrite(new Slot(var, Slot::PARAMETER, -1));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000284 receiver_ = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285
286 if (is_function_scope()) {
287 // Declare 'arguments' variable which exists in all functions.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000288 // Note that it might never be accessed, in which case it won't be
289 // allocated during variable allocation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 variables_.Declare(this, FACTORY->arguments_symbol(), Variable::VAR,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000291 true, Variable::ARGUMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 }
293}
294
295
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000296Variable* Scope::LocalLookup(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000297 Variable* result = variables_.Lookup(name);
298 if (result != NULL || !resolved()) {
299 return result;
300 }
301 // If the scope is resolved, we can find a variable in serialized scope info.
302
303 // We should never lookup 'arguments' in this scope
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000304 // as it is implicitly present in any scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 ASSERT(*name != *FACTORY->arguments_symbol());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000306
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000307 // Assert that there is no local slot with the given name.
308 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
309
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000310 // Check context slot lookup.
311 Variable::Mode mode;
312 int index = scope_info_->ContextSlotIndex(*name, &mode);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000313 if (index >= 0) {
314 Variable* var =
315 variables_.Declare(this, name, mode, true, Variable::NORMAL);
316 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
317 return var;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000318 }
319
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000320 index = scope_info_->ParameterIndex(*name);
321 if (index >= 0) {
322 // ".arguments" must be present in context slots.
323 ASSERT(arguments_shadow_ != NULL);
324 Variable* var =
325 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
326 Property* rewrite =
327 new Property(new VariableProxy(arguments_shadow_),
328 new Literal(Handle<Object>(Smi::FromInt(index))),
329 RelocInfo::kNoPosition,
330 Property::SYNTHETIC);
331 rewrite->set_is_arguments_access(true);
332 var->set_rewrite(rewrite);
333 return var;
334 }
335
336 index = scope_info_->FunctionContextSlotIndex(*name);
337 if (index >= 0) {
338 // Check that there is no local slot with the given name.
339 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
340 Variable* var =
341 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
342 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
343 return var;
344 }
345
346 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347}
348
349
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000350Variable* Scope::Lookup(Handle<String> name) {
351 for (Scope* scope = this;
352 scope != NULL;
353 scope = scope->outer_scope()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000354 Variable* var = scope->LocalLookup(name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000355 if (var != NULL) return var;
356 }
357 return NULL;
358}
359
360
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361Variable* Scope::DeclareFunctionVar(Handle<String> name) {
362 ASSERT(is_function_scope() && function_ == NULL);
ager@chromium.org3e875802009-06-29 08:26:34 +0000363 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 return function_;
365}
366
367
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000368Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369 // DYNAMIC variables are introduces during variable allocation,
370 // INTERNAL variables are allocated explicitly, and TEMPORARY
371 // variables are allocated via NewTemporary().
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000372 ASSERT(!resolved());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000374 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
375}
376
377
378Variable* Scope::DeclareGlobal(Handle<String> name) {
379 ASSERT(is_global_scope());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000380 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000381 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382}
383
384
385void Scope::AddParameter(Variable* var) {
386 ASSERT(is_function_scope());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000387 ASSERT(LocalLookup(var->name()) == var);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388 params_.Add(var);
389}
390
391
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000392VariableProxy* Scope::NewUnresolved(Handle<String> name,
393 bool inside_with,
394 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395 // Note that we must not share the unresolved variables with
396 // the same name because they may be removed selectively via
397 // RemoveUnresolved().
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000398 ASSERT(!resolved());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000399 VariableProxy* proxy = new VariableProxy(name, false, inside_with, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 unresolved_.Add(proxy);
401 return proxy;
402}
403
404
405void Scope::RemoveUnresolved(VariableProxy* var) {
406 // Most likely (always?) any variable we want to remove
407 // was just added before, so we search backwards.
408 for (int i = unresolved_.length(); i-- > 0;) {
409 if (unresolved_[i] == var) {
410 unresolved_.Remove(i);
411 return;
412 }
413 }
414}
415
416
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000417Variable* Scope::NewTemporary(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000418 ASSERT(!resolved());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000419 Variable* var =
420 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000422 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423}
424
425
426void Scope::AddDeclaration(Declaration* declaration) {
427 decls_.Add(declaration);
428}
429
430
431void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000432 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 if (!HasIllegalRedeclaration()) {
434 illegal_redecl_ = expression;
435 }
436 ASSERT(HasIllegalRedeclaration());
437}
438
439
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000440void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 ASSERT(HasIllegalRedeclaration());
442 illegal_redecl_->Accept(visitor);
443}
444
445
446template<class Allocator>
447void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
448 // Collect variables in this scope.
449 // Note that the function_ variable - if present - is not
450 // collected here but handled separately in ScopeInfo
451 // which is the current user of this function).
452 for (int i = 0; i < temps_.length(); i++) {
453 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000454 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 locals->Add(var);
456 }
457 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000458 for (VariableMap::Entry* p = variables_.Start();
459 p != NULL;
460 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000462 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463 locals->Add(var);
464 }
465 }
466}
467
468
469// Make sure the method gets instantiated by the template system.
470template void Scope::CollectUsedVariables(
471 List<Variable*, FreeStoreAllocationPolicy>* locals);
472template void Scope::CollectUsedVariables(
473 List<Variable*, PreallocatedStorage>* locals);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000474template void Scope::CollectUsedVariables(
475 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476
477
ager@chromium.org381abbb2009-02-25 13:23:22 +0000478void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 ASSERT(outer_scope_ == NULL); // eval or global scopes only
480
481 // 1) Propagate scope information.
482 // If we are in an eval scope, we may have other outer scopes about
483 // which we don't know anything at this point. Thus we must be conservative
484 // and assume they may invoke eval themselves. Eventually we could capture
485 // this information in the ScopeInfo and then use it here (by traversing
486 // the call chain stack, at compile time).
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000487
ager@chromium.org381abbb2009-02-25 13:23:22 +0000488 bool eval_scope = is_eval_scope();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000489 bool outer_scope_calls_eval = false;
490 bool outer_scope_calls_non_strict_eval = false;
491 if (!is_global_scope()) {
492 context->ComputeEvalScopeInfo(&outer_scope_calls_eval,
493 &outer_scope_calls_non_strict_eval);
494 }
495 PropagateScopeInfo(outer_scope_calls_eval,
496 outer_scope_calls_non_strict_eval,
497 eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498
499 // 2) Resolve variables.
500 Scope* global_scope = NULL;
501 if (is_global_scope()) global_scope = this;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000502 ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503
504 // 3) Allocate variables.
505 AllocateVariablesRecursively();
506}
507
508
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509bool Scope::AllowsLazyCompilation() const {
510 return !force_eager_compilation_ && HasTrivialOuterContext();
511}
512
513
514bool Scope::HasTrivialContext() const {
515 // A function scope has a trivial context if it always is the global
516 // context. We iteratively scan out the context chain to see if
517 // there is anything that makes this scope non-trivial; otherwise we
518 // return true.
519 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
520 if (scope->is_eval_scope()) return false;
521 if (scope->scope_inside_with_) return false;
522 if (scope->num_heap_slots_ > 0) return false;
523 }
524 return true;
525}
526
527
528bool Scope::HasTrivialOuterContext() const {
529 Scope* outer = outer_scope_;
530 if (outer == NULL) return true;
531 // Note that the outer context may be trivial in general, but the current
532 // scope may be inside a 'with' statement in which case the outer context
533 // for this scope is not trivial.
534 return !scope_inside_with_ && outer->HasTrivialContext();
535}
536
537
538int Scope::ContextChainLength(Scope* scope) {
539 int n = 0;
540 for (Scope* s = this; s != scope; s = s->outer_scope_) {
541 ASSERT(s != NULL); // scope must be in the scope chain
542 if (s->num_heap_slots() > 0) n++;
543 }
544 return n;
545}
546
547
548#ifdef DEBUG
549static const char* Header(Scope::Type type) {
550 switch (type) {
551 case Scope::EVAL_SCOPE: return "eval";
552 case Scope::FUNCTION_SCOPE: return "function";
553 case Scope::GLOBAL_SCOPE: return "global";
554 }
555 UNREACHABLE();
556 return NULL;
557}
558
559
560static void Indent(int n, const char* str) {
561 PrintF("%*s%s", n, "", str);
562}
563
564
565static void PrintName(Handle<String> name) {
566 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
567 PrintF("%s", *s);
568}
569
570
571static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000572 if (var->is_used() || var->rewrite() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 Indent(indent, Variable::Mode2String(var->mode()));
574 PrintF(" ");
575 PrintName(var->name());
576 PrintF("; // ");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000577 if (var->rewrite() != NULL) {
578 PrintF("%s, ", printer->Print(var->rewrite()));
579 if (var->is_accessed_from_inner_scope()) PrintF(", ");
580 }
581 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 PrintF("\n");
583 }
584}
585
586
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000587static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
588 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000589 Variable* var = reinterpret_cast<Variable*>(p->value);
590 PrintVar(printer, indent, var);
591 }
592}
593
594
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595void Scope::Print(int n) {
596 int n0 = (n > 0 ? n : 0);
597 int n1 = n0 + 2; // indentation
598
599 // Print header.
600 Indent(n0, Header(type_));
601 if (scope_name_->length() > 0) {
602 PrintF(" ");
603 PrintName(scope_name_);
604 }
605
606 // Print parameters, if any.
607 if (is_function_scope()) {
608 PrintF(" (");
609 for (int i = 0; i < params_.length(); i++) {
610 if (i > 0) PrintF(", ");
611 PrintName(params_[i]->name());
612 }
613 PrintF(")");
614 }
615
616 PrintF(" {\n");
617
618 // Function name, if any (named function literals, only).
619 if (function_ != NULL) {
620 Indent(n1, "// (local) function name: ");
621 PrintName(function_->name());
622 PrintF("\n");
623 }
624
625 // Scope info.
626 if (HasTrivialOuterContext()) {
627 Indent(n1, "// scope has trivial outer context\n");
628 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000629 if (is_strict_mode()) Indent(n1, "// strict mode scope\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
631 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
632 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
633 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000634 if (outer_scope_calls_non_strict_eval_) {
635 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
636 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000638 if (outer_scope_is_eval_scope_) {
639 Indent(n1, "// outer scope is 'eval' scope\n");
640 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 if (num_stack_slots_ > 0) { Indent(n1, "// ");
642 PrintF("%d stack slots\n", num_stack_slots_); }
643 if (num_heap_slots_ > 0) { Indent(n1, "// ");
644 PrintF("%d heap slots\n", num_heap_slots_); }
645
646 // Print locals.
647 PrettyPrinter printer;
648 Indent(n1, "// function var\n");
649 if (function_ != NULL) {
650 PrintVar(&printer, n1, function_);
651 }
652
653 Indent(n1, "// temporary vars\n");
654 for (int i = 0; i < temps_.length(); i++) {
655 PrintVar(&printer, n1, temps_[i]);
656 }
657
658 Indent(n1, "// local vars\n");
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000659 PrintMap(&printer, n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000661 Indent(n1, "// dynamic vars\n");
662 if (dynamics_ != NULL) {
663 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
664 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
665 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
666 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667
668 // Print inner scopes (disable by providing negative n).
669 if (n >= 0) {
670 for (int i = 0; i < inner_scopes_.length(); i++) {
671 PrintF("\n");
672 inner_scopes_[i]->Print(n1);
673 }
674 }
675
676 Indent(n0, "}\n");
677}
678#endif // DEBUG
679
680
ager@chromium.org381abbb2009-02-25 13:23:22 +0000681Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000682 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000683 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000684 Variable* var = map->Lookup(name);
685 if (var == NULL) {
686 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000687 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000688 // Allocate it by giving it a dynamic lookup.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000689 var->set_rewrite(new Slot(var, Slot::LOOKUP, -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691 return var;
692}
693
694
695// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000696// the statically resolved variable belonging to an outer scope, or
697// NULL. It may be NULL because a) we couldn't find a variable, or b)
698// because the variable is just a guess (and may be shadowed by
699// another variable that is introduced dynamically via an 'eval' call
700// or a 'with' statement).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000701Variable* Scope::LookupRecursive(Handle<String> name,
702 bool inner_lookup,
703 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704 // If we find a variable, but the current scope calls 'eval', the found
705 // variable may not be the correct one (the 'eval' may introduce a
706 // property with the same name). In that case, remember that the variable
707 // found is just a guess.
708 bool guess = scope_calls_eval_;
709
710 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000711 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712
713 if (var != NULL) {
714 // We found a variable. If this is not an inner lookup, we are done.
715 // (Even if there is an 'eval' in this scope which introduces the
716 // same variable again, the resulting variable remains the same.
717 // Note that enclosing 'with' statements are handled at the call site.)
718 if (!inner_lookup)
719 return var;
720
721 } else {
722 // We did not find a variable locally. Check against the function variable,
723 // if any. We can do this for all scopes, since the function variable is
724 // only present - if at all - for function scopes.
725 //
726 // This lookup corresponds to a lookup in the "intermediate" scope sitting
727 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
728 // the name of named function literal is kept in an intermediate scope
ager@chromium.org32912102009-01-16 10:38:43 +0000729 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730 if (function_ != NULL && function_->name().is_identical_to(name)) {
731 var = function_;
732
733 } else if (outer_scope_ != NULL) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000734 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735 // We may have found a variable in an outer scope. However, if
736 // the current scope is inside a 'with', the actual variable may
737 // be a property introduced via the 'with' statement. Then, the
738 // variable we may have found is just a guess.
739 if (scope_inside_with_)
740 guess = true;
741 }
742
743 // If we did not find a variable, we are done.
744 if (var == NULL)
745 return NULL;
746 }
747
748 ASSERT(var != NULL);
749
750 // If this is a lookup from an inner scope, mark the variable.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000751 if (inner_lookup) {
752 var->MarkAsAccessedFromInnerScope();
753 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000755 // If the variable we have found is just a guess, invalidate the
756 // result. If the found variable is local, record that fact so we
757 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000758 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000759 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000761 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762
763 return var;
764}
765
766
ager@chromium.org381abbb2009-02-25 13:23:22 +0000767void Scope::ResolveVariable(Scope* global_scope,
768 Handle<Context> context,
769 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770 ASSERT(global_scope == NULL || global_scope->is_global_scope());
771
772 // If the proxy is already resolved there's nothing to do
773 // (functions and consts may be resolved by the parser).
774 if (proxy->var() != NULL) return;
775
776 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000777 Variable* invalidated_local = NULL;
778 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779
780 if (proxy->inside_with()) {
781 // If we are inside a local 'with' statement, all bets are off
782 // and we cannot resolve the proxy to a local variable even if
783 // we found an outer matching variable.
784 // Note that we must do a lookup anyway, because if we find one,
785 // we must mark that variable as potentially accessed from this
786 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000787 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788
789 } else {
790 // We are not inside a local 'with' statement.
791
792 if (var == NULL) {
793 // We did not find the variable. We have a global variable
794 // if we are in the global scope (we know already that we
795 // are outside a 'with' statement) or if there is no way
796 // that the variable might be introduced dynamically (through
797 // a local or outer eval() call, or an outer 'with' statement),
798 // or we don't know about the outer scope (because we are
799 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000800 if (is_global_scope() ||
801 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
802 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803 // We must have a global variable.
804 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000805 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000806
807 } else if (scope_inside_with_) {
808 // If we are inside a with statement we give up and look up
809 // the variable at runtime.
810 var = NonLocal(proxy->name(), Variable::DYNAMIC);
811
812 } else if (invalidated_local != NULL) {
813 // No with statements are involved and we found a local
814 // variable that might be shadowed by eval introduced
815 // variables.
816 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
817 var->set_local_if_not_shadowed(invalidated_local);
818
819 } else if (outer_scope_is_eval_scope_) {
820 // No with statements and we did not find a local and the code
821 // is executed with a call to eval. The context contains
822 // scope information that we can use to determine if the
823 // variable is global if it is not shadowed by eval-introduced
824 // variables.
825 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
826 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
827
828 } else {
829 var = NonLocal(proxy->name(), Variable::DYNAMIC);
830 }
831
832 } else {
833 // No with statements and we did not find a local and the code
834 // is not executed with a call to eval. We know that this
835 // variable is global unless it is shadowed by eval-introduced
836 // variables.
837 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838 }
839 }
840 }
841
842 proxy->BindTo(var);
843}
844
845
ager@chromium.org381abbb2009-02-25 13:23:22 +0000846void Scope::ResolveVariablesRecursively(Scope* global_scope,
847 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848 ASSERT(global_scope == NULL || global_scope->is_global_scope());
849
850 // Resolve unresolved variables for this scope.
851 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000852 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853 }
854
855 // Resolve unresolved variables for inner scopes.
856 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000857 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 }
859}
860
861
ager@chromium.org381abbb2009-02-25 13:23:22 +0000862bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000863 bool outer_scope_calls_non_strict_eval,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000864 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 if (outer_scope_calls_eval) {
866 outer_scope_calls_eval_ = true;
867 }
868
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000869 if (outer_scope_calls_non_strict_eval) {
870 outer_scope_calls_non_strict_eval_ = true;
871 }
872
ager@chromium.org381abbb2009-02-25 13:23:22 +0000873 if (outer_scope_is_eval_scope) {
874 outer_scope_is_eval_scope_ = true;
875 }
876
877 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
878 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000879 bool calls_non_strict_eval =
880 (scope_calls_eval_ && !is_strict_mode()) ||
881 outer_scope_calls_non_strict_eval_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 for (int i = 0; i < inner_scopes_.length(); i++) {
883 Scope* inner_scope = inner_scopes_[i];
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000884 if (inner_scope->PropagateScopeInfo(calls_eval,
885 calls_non_strict_eval,
886 is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 inner_scope_calls_eval_ = true;
888 }
889 if (inner_scope->force_eager_compilation_) {
890 force_eager_compilation_ = true;
891 }
892 }
893
894 return scope_calls_eval_ || inner_scope_calls_eval_;
895}
896
897
898bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000899 // Give var a read/write use if there is a chance it might be accessed
900 // via an eval() call. This is only possible if the variable has a
901 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 if ((var->is_this() || var->name()->length() > 0) &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000903 (var->is_accessed_from_inner_scope() ||
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000904 scope_calls_eval_ || inner_scope_calls_eval_ ||
905 scope_contains_with_)) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000906 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000908 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000909 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910}
911
912
913bool Scope::MustAllocateInContext(Variable* var) {
914 // If var is accessed from an inner scope, or if there is a
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000915 // possibility that it might be accessed from the current or an inner
916 // scope (through an eval() call), it must be allocated in the
917 // context. Exception: temporary variables are not allocated in the
918 // context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 return
920 var->mode() != Variable::TEMPORARY &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000921 (var->is_accessed_from_inner_scope() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 scope_calls_eval_ || inner_scope_calls_eval_ ||
923 scope_contains_with_ || var->is_global());
924}
925
926
927bool Scope::HasArgumentsParameter() {
928 for (int i = 0; i < params_.length(); i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000929 if (params_[i]->name().is_identical_to(FACTORY->arguments_symbol()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930 return true;
931 }
932 return false;
933}
934
935
936void Scope::AllocateStackSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000937 var->set_rewrite(new Slot(var, Slot::LOCAL, num_stack_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938}
939
940
941void Scope::AllocateHeapSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000942 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943}
944
945
946void Scope::AllocateParameterLocals() {
947 ASSERT(is_function_scope());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000948 Variable* arguments = LocalLookup(FACTORY->arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000950
951 // Parameters are rewritten to arguments[i] if 'arguments' is used in
952 // a non-strict mode function. Strict mode code doesn't alias arguments.
953 bool rewrite_parameters = false;
954
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
956 // 'arguments' is used. Unless there is also a parameter called
957 // 'arguments', we must be conservative and access all parameters via
958 // the arguments object: The i'th parameter is rewritten into
959 // '.arguments[i]' (*). If we have a parameter named 'arguments', a
960 // (new) value is always assigned to it via the function
961 // invocation. Then 'arguments' denotes that specific parameter value
962 // and cannot be used to access the parameters, which is why we don't
963 // need to rewrite in that case.
964 //
965 // (*) Instead of having a parameter called 'arguments', we may have an
966 // assignment to 'arguments' in the function body, at some arbitrary
967 // point in time (possibly through an 'eval()' call!). After that
968 // assignment any re-write of parameters would be invalid (was bug
969 // 881452). Thus, we introduce a shadow '.arguments'
970 // variable which also points to the arguments object. For rewrites we
971 // use '.arguments' which remains valid even if we assign to
972 // 'arguments'. To summarize: If we need to rewrite, we allocate an
973 // 'arguments' object dynamically upon function invocation. The compiler
974 // introduces 2 local variables 'arguments' and '.arguments', both of
975 // which originally point to the arguments object that was
976 // allocated. All parameters are rewritten into property accesses via
977 // the '.arguments' variable. Thus, any changes to properties of
978 // 'arguments' are reflected in the variables and vice versa. If the
979 // 'arguments' variable is changed, '.arguments' still points to the
980 // correct arguments object and the rewrites still work.
981
982 // We are using 'arguments'. Tell the code generator that is needs to
983 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000984 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000986 // In strict mode 'arguments' does not alias formal parameters.
987 // Therefore in strict mode we allocate parameters as if 'arguments'
988 // were not used.
989 rewrite_parameters = !is_strict_mode();
990 }
991
992 if (rewrite_parameters) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 // We also need the '.arguments' shadow variable. Declare it and create
994 // and bind the corresponding proxy. It's ok to declare it only now
995 // because it's a local variable that is allocated after the parameters
996 // have been allocated.
997 //
998 // Note: This is "almost" at temporary variable but we cannot use
999 // NewTemporary() because the mode needs to be INTERNAL since this
1000 // variable may be allocated in the heap-allocated context (temporaries
1001 // are never allocated in the context).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001002 arguments_shadow_ = new Variable(this,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001003 FACTORY->arguments_shadow_symbol(),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001004 Variable::INTERNAL,
1005 true,
1006 Variable::ARGUMENTS);
1007 arguments_shadow_->set_is_used(true);
1008 temps_.Add(arguments_shadow_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009
1010 // Allocate the parameters by rewriting them into '.arguments[i]' accesses.
1011 for (int i = 0; i < params_.length(); i++) {
1012 Variable* var = params_[i];
1013 ASSERT(var->scope() == this);
1014 if (MustAllocate(var)) {
1015 if (MustAllocateInContext(var)) {
1016 // It is ok to set this only now, because arguments is a local
1017 // variable that is allocated after the parameters have been
1018 // allocated.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001019 arguments_shadow_->MarkAsAccessedFromInnerScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001021 Property* rewrite =
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001022 new Property(new VariableProxy(arguments_shadow_),
1023 new Literal(Handle<Object>(Smi::FromInt(i))),
1024 RelocInfo::kNoPosition,
1025 Property::SYNTHETIC);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001026 rewrite->set_is_arguments_access(true);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001027 var->set_rewrite(rewrite);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028 }
1029 }
1030
1031 } else {
1032 // The arguments object is not used, so we can access parameters directly.
1033 // The same parameter may occur multiple times in the parameters_ list.
1034 // If it does, and if it is not copied into the context object, it must
1035 // receive the highest parameter index for that parameter; thus iteration
1036 // order is relevant!
1037 for (int i = 0; i < params_.length(); i++) {
1038 Variable* var = params_[i];
1039 ASSERT(var->scope() == this);
1040 if (MustAllocate(var)) {
1041 if (MustAllocateInContext(var)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001042 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001043 (var->AsSlot() != NULL &&
1044 var->AsSlot()->type() == Slot::CONTEXT));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001045 if (var->rewrite() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 // Only set the heap allocation if the parameter has not
1047 // been allocated yet.
1048 AllocateHeapSlot(var);
1049 }
1050 } else {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001051 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001052 (var->AsSlot() != NULL &&
1053 var->AsSlot()->type() == Slot::PARAMETER));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054 // Set the parameter index always, even if the parameter
1055 // was seen before! (We need to access the actual parameter
1056 // supplied for the last occurrence of a multiply declared
1057 // parameter.)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001058 var->set_rewrite(new Slot(var, Slot::PARAMETER, i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 }
1060 }
1061 }
1062 }
1063}
1064
1065
1066void Scope::AllocateNonParameterLocal(Variable* var) {
1067 ASSERT(var->scope() == this);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001068 ASSERT(var->rewrite() == NULL ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001069 (!var->IsVariable(FACTORY->result_symbol())) ||
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001070 (var->AsSlot() == NULL || var->AsSlot()->type() != Slot::LOCAL));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001071 if (var->rewrite() == NULL && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 if (MustAllocateInContext(var)) {
1073 AllocateHeapSlot(var);
1074 } else {
1075 AllocateStackSlot(var);
1076 }
1077 }
1078}
1079
1080
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001082 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083 for (int i = 0; i < temps_.length(); i++) {
1084 AllocateNonParameterLocal(temps_[i]);
1085 }
1086
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001087 for (VariableMap::Entry* p = variables_.Start();
1088 p != NULL;
1089 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 Variable* var = reinterpret_cast<Variable*>(p->value);
1091 AllocateNonParameterLocal(var);
1092 }
1093
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001094 // For now, function_ must be allocated at the very end. If it gets
1095 // allocated in the context, it must be the last slot in the context,
1096 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1098 if (function_ != NULL) {
1099 AllocateNonParameterLocal(function_);
1100 }
1101}
1102
1103
1104void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105 // Allocate variables for inner scopes.
1106 for (int i = 0; i < inner_scopes_.length(); i++) {
1107 inner_scopes_[i]->AllocateVariablesRecursively();
1108 }
1109
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001110 // If scope is already resolved, we still need to allocate
1111 // variables in inner scopes which might not had been resolved yet.
1112 if (resolved()) return;
1113 // The number of slots required for variables.
1114 num_stack_slots_ = 0;
1115 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117 // Allocate variables for this scope.
1118 // Parameters must be allocated first, if any.
1119 if (is_function_scope()) AllocateParameterLocals();
1120 AllocateNonParameterLocals();
1121
1122 // Allocate context if necessary.
1123 bool must_have_local_context = false;
1124 if (scope_calls_eval_ || scope_contains_with_) {
1125 // The context for the eval() call or 'with' statement in this scope.
1126 // Unless we are in the global or an eval scope, we need a local
1127 // context even if we didn't statically allocate any locals in it,
1128 // and the compiler will access the context variable. If we are
1129 // not in an inner scope, the scope is provided from the outside.
1130 must_have_local_context = is_function_scope();
1131 }
1132
1133 // If we didn't allocate any locals in the local context, then we only
1134 // need the minimal number of slots if we must have a local context.
1135 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1136 !must_have_local_context) {
1137 num_heap_slots_ = 0;
1138 }
1139
1140 // Allocation done.
1141 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1142}
1143
1144} } // namespace v8::internal