blob: f4bcaa8fba6d8c2750227a74199e2e3100875f5f [file] [log] [blame]
Ben Murdochf87a2032010-10-22 12:50:53 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
Ben Murdochf87a2032010-10-22 12:50:53 +010030#include "scopes.h"
31
32#include "bootstrapper.h"
33#include "compiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "prettyprinter.h"
35#include "scopeinfo.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036
37namespace v8 {
38namespace internal {
39
40// ----------------------------------------------------------------------------
41// A Zone allocator for use with LocalsMap.
42
Steve Block44f0eee2011-05-26 01:26:41 +010043// TODO(isolates): It is probably worth it to change the Allocator class to
44// take a pointer to an isolate.
Steve Blocka7e24c12009-10-30 11:49:00 +000045class ZoneAllocator: public Allocator {
46 public:
47 /* nothing to do */
48 virtual ~ZoneAllocator() {}
49
Steve Block44f0eee2011-05-26 01:26:41 +010050 virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); }
Steve Blocka7e24c12009-10-30 11:49:00 +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
79VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {}
80
81VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {}
82VariableMap::~VariableMap() {}
83
84
85Variable* VariableMap::Declare(Scope* scope,
86 Handle<String> name,
87 Variable::Mode mode,
88 bool is_valid_lhs,
89 Variable::Kind kind) {
90 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());
94 p->value = new Variable(scope, name, mode, is_valid_lhs, kind);
95 }
96 return reinterpret_cast<Variable*>(p->value);
97}
98
99
100Variable* VariableMap::Lookup(Handle<String> name) {
101 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
116Scope::Scope(Type type)
Ben Murdochb8e0da22011-05-16 14:20:40 +0100117 : inner_scopes_(0),
Steve Blocka7e24c12009-10-30 11:49:00 +0000118 variables_(false),
119 temps_(0),
120 params_(0),
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 unresolved_(0),
Ben Murdochb8e0da22011-05-16 14:20:40 +0100122 decls_(0) {
123 SetDefaults(type, NULL, NULL);
124 ASSERT(!resolved());
Steve Blocka7e24c12009-10-30 11:49:00 +0000125}
126
127
128Scope::Scope(Scope* outer_scope, Type type)
Ben Murdochb8e0da22011-05-16 14:20:40 +0100129 : inner_scopes_(4),
130 variables_(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 temps_(4),
132 params_(4),
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 unresolved_(16),
Ben Murdochb8e0da22011-05-16 14:20:40 +0100134 decls_(4) {
135 SetDefaults(type, outer_scope, NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +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());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100141 ASSERT(!resolved());
Steve Blocka7e24c12009-10-30 11:49:00 +0000142}
143
144
Ben Murdochb8e0da22011-05-16 14:20:40 +0100145Scope::Scope(Scope* inner_scope, SerializedScopeInfo* scope_info)
146 : inner_scopes_(4),
147 variables_(),
148 temps_(4),
149 params_(4),
150 unresolved_(16),
151 decls_(4) {
152 ASSERT(scope_info != NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100153 SetDefaults(FUNCTION_SCOPE, NULL, scope_info);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100154 ASSERT(resolved());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100155 if (scope_info->HasHeapAllocatedLocals()) {
156 num_heap_slots_ = scope_info_->NumberOfContextSlots();
157 }
158
Steve Block44f0eee2011-05-26 01:26:41 +0100159 AddInnerScope(inner_scope);
160
Ben Murdochb8e0da22011-05-16 14:20:40 +0100161 // 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.
Steve Block44f0eee2011-05-26 01:26:41 +0100164 Isolate* isolate = Isolate::Current();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100165 Variable::Mode mode;
166 int arguments_shadow_index =
Steve Block44f0eee2011-05-26 01:26:41 +0100167 scope_info_->ContextSlotIndex(
168 isolate->heap()->arguments_shadow_symbol(), &mode);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100169 if (arguments_shadow_index >= 0) {
170 ASSERT(mode == Variable::INTERNAL);
Steve Block44f0eee2011-05-26 01:26:41 +0100171 arguments_shadow_ = new Variable(
172 this,
173 isolate->factory()->arguments_shadow_symbol(),
174 Variable::INTERNAL,
175 true,
176 Variable::ARGUMENTS);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100177 arguments_shadow_->set_rewrite(
178 new Slot(arguments_shadow_, Slot::CONTEXT, arguments_shadow_index));
179 arguments_shadow_->set_is_used(true);
180 }
181}
182
183
Steve Block44f0eee2011-05-26 01:26:41 +0100184Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
185 Scope* global_scope) {
186 ASSERT(!info->closure().is_null());
187 // If we have a serialized scope info, reuse it.
188 Scope* innermost_scope = NULL;
189 Scope* scope = NULL;
190
191 SerializedScopeInfo* scope_info = info->closure()->shared()->scope_info();
192 if (scope_info != SerializedScopeInfo::Empty()) {
193 JSFunction* current = *info->closure();
194 do {
195 current = current->context()->closure();
196 SerializedScopeInfo* scope_info = current->shared()->scope_info();
197 if (scope_info != SerializedScopeInfo::Empty()) {
198 scope = new Scope(scope, scope_info);
199 if (innermost_scope == NULL) innermost_scope = scope;
200 } else {
201 ASSERT(current->context()->IsGlobalContext());
202 }
203 } while (!current->context()->IsGlobalContext());
204 }
205
206 global_scope->AddInnerScope(scope);
207 if (innermost_scope == NULL) innermost_scope = global_scope;
208
209 return innermost_scope;
210}
211
Ben Murdochb8e0da22011-05-16 14:20:40 +0100212
Ben Murdochf87a2032010-10-22 12:50:53 +0100213bool Scope::Analyze(CompilationInfo* info) {
214 ASSERT(info->function() != NULL);
215 Scope* top = info->function()->scope();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100216
Ben Murdochf87a2032010-10-22 12:50:53 +0100217 while (top->outer_scope() != NULL) top = top->outer_scope();
218 top->AllocateVariables(info->calling_context());
219
220#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100221 if (info->isolate()->bootstrapper()->IsActive()
Ben Murdochf87a2032010-10-22 12:50:53 +0100222 ? FLAG_print_builtin_scopes
223 : FLAG_print_scopes) {
224 info->function()->scope()->Print();
225 }
226#endif
227
228 info->SetScope(info->function()->scope());
229 return true; // Can not fail.
230}
231
232
Steve Blocka7e24c12009-10-30 11:49:00 +0000233void Scope::Initialize(bool inside_with) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100234 ASSERT(!resolved());
235
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 // Add this scope as a new inner scope of the outer scope.
237 if (outer_scope_ != NULL) {
238 outer_scope_->inner_scopes_.Add(this);
239 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
240 } else {
241 scope_inside_with_ = inside_with;
242 }
243
244 // Declare convenience variables.
245 // Declare and allocate receiver (even for the global scope, and even
246 // if naccesses_ == 0).
247 // NOTE: When loading parameters in the global scope, we must take
248 // care not to access them as properties of the global object, but
249 // instead load them directly from the stack. Currently, the only
250 // such parameter is 'this' which is passed on the stack when
251 // invoking scripts
252 Variable* var =
Steve Block44f0eee2011-05-26 01:26:41 +0100253 variables_.Declare(this, FACTORY->this_symbol(), Variable::VAR,
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 false, Variable::THIS);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100255 var->set_rewrite(new Slot(var, Slot::PARAMETER, -1));
Leon Clarkee46be812010-01-19 14:06:41 +0000256 receiver_ = var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000257
258 if (is_function_scope()) {
259 // Declare 'arguments' variable which exists in all functions.
260 // Note that it might never be accessed, in which case it won't be
261 // allocated during variable allocation.
Steve Block44f0eee2011-05-26 01:26:41 +0100262 variables_.Declare(this, FACTORY->arguments_symbol(), Variable::VAR,
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 true, Variable::ARGUMENTS);
264 }
265}
266
267
Steve Blocka7e24c12009-10-30 11:49:00 +0000268Variable* Scope::LocalLookup(Handle<String> name) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100269 Variable* result = variables_.Lookup(name);
270 if (result != NULL || !resolved()) {
271 return result;
272 }
273 // If the scope is resolved, we can find a variable in serialized scope info.
274
275 // We should never lookup 'arguments' in this scope
276 // as it is implicitly present in any scope.
Steve Block44f0eee2011-05-26 01:26:41 +0100277 ASSERT(*name != *FACTORY->arguments_symbol());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100278
279 // Assert that there is no local slot with the given name.
280 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
281
282 // Check context slot lookup.
283 Variable::Mode mode;
284 int index = scope_info_->ContextSlotIndex(*name, &mode);
285 if (index >= 0) {
286 Variable* var =
287 variables_.Declare(this, name, mode, true, Variable::NORMAL);
288 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
289 return var;
290 }
291
292 index = scope_info_->ParameterIndex(*name);
293 if (index >= 0) {
294 // ".arguments" must be present in context slots.
295 ASSERT(arguments_shadow_ != NULL);
296 Variable* var =
297 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
298 Property* rewrite =
299 new Property(new VariableProxy(arguments_shadow_),
300 new Literal(Handle<Object>(Smi::FromInt(index))),
301 RelocInfo::kNoPosition,
302 Property::SYNTHETIC);
303 rewrite->set_is_arguments_access(true);
304 var->set_rewrite(rewrite);
305 return var;
306 }
307
308 index = scope_info_->FunctionContextSlotIndex(*name);
309 if (index >= 0) {
310 // Check that there is no local slot with the given name.
311 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
312 Variable* var =
313 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
314 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
315 return var;
316 }
317
318 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000319}
320
321
322Variable* Scope::Lookup(Handle<String> name) {
323 for (Scope* scope = this;
324 scope != NULL;
325 scope = scope->outer_scope()) {
326 Variable* var = scope->LocalLookup(name);
327 if (var != NULL) return var;
328 }
329 return NULL;
330}
331
332
333Variable* Scope::DeclareFunctionVar(Handle<String> name) {
334 ASSERT(is_function_scope() && function_ == NULL);
335 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
336 return function_;
337}
338
339
340Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
341 // DYNAMIC variables are introduces during variable allocation,
342 // INTERNAL variables are allocated explicitly, and TEMPORARY
343 // variables are allocated via NewTemporary().
Ben Murdochb8e0da22011-05-16 14:20:40 +0100344 ASSERT(!resolved());
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
346 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
347}
348
349
350Variable* Scope::DeclareGlobal(Handle<String> name) {
351 ASSERT(is_global_scope());
Leon Clarkee46be812010-01-19 14:06:41 +0000352 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 Variable::NORMAL);
354}
355
356
357void Scope::AddParameter(Variable* var) {
358 ASSERT(is_function_scope());
359 ASSERT(LocalLookup(var->name()) == var);
360 params_.Add(var);
361}
362
363
364VariableProxy* Scope::NewUnresolved(Handle<String> name, bool inside_with) {
365 // Note that we must not share the unresolved variables with
366 // the same name because they may be removed selectively via
367 // RemoveUnresolved().
Ben Murdochb8e0da22011-05-16 14:20:40 +0100368 ASSERT(!resolved());
Steve Blocka7e24c12009-10-30 11:49:00 +0000369 VariableProxy* proxy = new VariableProxy(name, false, inside_with);
370 unresolved_.Add(proxy);
371 return proxy;
372}
373
374
375void Scope::RemoveUnresolved(VariableProxy* var) {
376 // Most likely (always?) any variable we want to remove
377 // was just added before, so we search backwards.
378 for (int i = unresolved_.length(); i-- > 0;) {
379 if (unresolved_[i] == var) {
380 unresolved_.Remove(i);
381 return;
382 }
383 }
384}
385
386
Ben Murdochb0fe1622011-05-05 13:52:32 +0100387Variable* Scope::NewTemporary(Handle<String> name) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100388 ASSERT(!resolved());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100389 Variable* var =
390 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 temps_.Add(var);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100392 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000393}
394
395
396void Scope::AddDeclaration(Declaration* declaration) {
397 decls_.Add(declaration);
398}
399
400
401void Scope::SetIllegalRedeclaration(Expression* expression) {
Steve Block1e0659c2011-05-24 12:43:12 +0100402 // Record only the first illegal redeclaration.
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 if (!HasIllegalRedeclaration()) {
404 illegal_redecl_ = expression;
405 }
406 ASSERT(HasIllegalRedeclaration());
407}
408
409
410void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
411 ASSERT(HasIllegalRedeclaration());
412 illegal_redecl_->Accept(visitor);
413}
414
415
416template<class Allocator>
417void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
418 // Collect variables in this scope.
419 // Note that the function_ variable - if present - is not
420 // collected here but handled separately in ScopeInfo
421 // which is the current user of this function).
422 for (int i = 0; i < temps_.length(); i++) {
423 Variable* var = temps_[i];
Steve Block6ded16b2010-05-10 14:33:55 +0100424 if (var->is_used()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 locals->Add(var);
426 }
427 }
428 for (VariableMap::Entry* p = variables_.Start();
429 p != NULL;
430 p = variables_.Next(p)) {
431 Variable* var = reinterpret_cast<Variable*>(p->value);
Steve Block6ded16b2010-05-10 14:33:55 +0100432 if (var->is_used()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000433 locals->Add(var);
434 }
435 }
436}
437
438
439// Make sure the method gets instantiated by the template system.
440template void Scope::CollectUsedVariables(
441 List<Variable*, FreeStoreAllocationPolicy>* locals);
442template void Scope::CollectUsedVariables(
443 List<Variable*, PreallocatedStorage>* locals);
444template void Scope::CollectUsedVariables(
445 List<Variable*, ZoneListAllocationPolicy>* locals);
446
447
448void Scope::AllocateVariables(Handle<Context> context) {
449 ASSERT(outer_scope_ == NULL); // eval or global scopes only
450
451 // 1) Propagate scope information.
452 // If we are in an eval scope, we may have other outer scopes about
453 // which we don't know anything at this point. Thus we must be conservative
454 // and assume they may invoke eval themselves. Eventually we could capture
455 // this information in the ScopeInfo and then use it here (by traversing
456 // the call chain stack, at compile time).
457 bool eval_scope = is_eval_scope();
458 PropagateScopeInfo(eval_scope, eval_scope);
459
460 // 2) Resolve variables.
461 Scope* global_scope = NULL;
462 if (is_global_scope()) global_scope = this;
463 ResolveVariablesRecursively(global_scope, context);
464
465 // 3) Allocate variables.
466 AllocateVariablesRecursively();
467}
468
469
470bool Scope::AllowsLazyCompilation() const {
471 return !force_eager_compilation_ && HasTrivialOuterContext();
472}
473
474
475bool Scope::HasTrivialContext() const {
476 // A function scope has a trivial context if it always is the global
477 // context. We iteratively scan out the context chain to see if
478 // there is anything that makes this scope non-trivial; otherwise we
479 // return true.
480 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
481 if (scope->is_eval_scope()) return false;
482 if (scope->scope_inside_with_) return false;
483 if (scope->num_heap_slots_ > 0) return false;
484 }
485 return true;
486}
487
488
489bool Scope::HasTrivialOuterContext() const {
490 Scope* outer = outer_scope_;
491 if (outer == NULL) return true;
492 // Note that the outer context may be trivial in general, but the current
493 // scope may be inside a 'with' statement in which case the outer context
494 // for this scope is not trivial.
495 return !scope_inside_with_ && outer->HasTrivialContext();
496}
497
498
499int Scope::ContextChainLength(Scope* scope) {
500 int n = 0;
501 for (Scope* s = this; s != scope; s = s->outer_scope_) {
502 ASSERT(s != NULL); // scope must be in the scope chain
503 if (s->num_heap_slots() > 0) n++;
504 }
505 return n;
506}
507
508
509#ifdef DEBUG
510static const char* Header(Scope::Type type) {
511 switch (type) {
512 case Scope::EVAL_SCOPE: return "eval";
513 case Scope::FUNCTION_SCOPE: return "function";
514 case Scope::GLOBAL_SCOPE: return "global";
515 }
516 UNREACHABLE();
517 return NULL;
518}
519
520
521static void Indent(int n, const char* str) {
522 PrintF("%*s%s", n, "", str);
523}
524
525
526static void PrintName(Handle<String> name) {
527 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
528 PrintF("%s", *s);
529}
530
531
532static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
Steve Block6ded16b2010-05-10 14:33:55 +0100533 if (var->is_used() || var->rewrite() != NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000534 Indent(indent, Variable::Mode2String(var->mode()));
535 PrintF(" ");
536 PrintName(var->name());
537 PrintF("; // ");
Steve Block6ded16b2010-05-10 14:33:55 +0100538 if (var->rewrite() != NULL) {
539 PrintF("%s, ", printer->Print(var->rewrite()));
540 if (var->is_accessed_from_inner_scope()) PrintF(", ");
541 }
542 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access");
Steve Blocka7e24c12009-10-30 11:49:00 +0000543 PrintF("\n");
544 }
545}
546
547
548static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
549 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
550 Variable* var = reinterpret_cast<Variable*>(p->value);
551 PrintVar(printer, indent, var);
552 }
553}
554
555
556void Scope::Print(int n) {
557 int n0 = (n > 0 ? n : 0);
558 int n1 = n0 + 2; // indentation
559
560 // Print header.
561 Indent(n0, Header(type_));
562 if (scope_name_->length() > 0) {
563 PrintF(" ");
564 PrintName(scope_name_);
565 }
566
567 // Print parameters, if any.
568 if (is_function_scope()) {
569 PrintF(" (");
570 for (int i = 0; i < params_.length(); i++) {
571 if (i > 0) PrintF(", ");
572 PrintName(params_[i]->name());
573 }
574 PrintF(")");
575 }
576
577 PrintF(" {\n");
578
579 // Function name, if any (named function literals, only).
580 if (function_ != NULL) {
581 Indent(n1, "// (local) function name: ");
582 PrintName(function_->name());
583 PrintF("\n");
584 }
585
586 // Scope info.
587 if (HasTrivialOuterContext()) {
588 Indent(n1, "// scope has trivial outer context\n");
589 }
590 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
591 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
592 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
593 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
594 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
595 if (outer_scope_is_eval_scope_) {
596 Indent(n1, "// outer scope is 'eval' scope\n");
597 }
598 if (num_stack_slots_ > 0) { Indent(n1, "// ");
599 PrintF("%d stack slots\n", num_stack_slots_); }
600 if (num_heap_slots_ > 0) { Indent(n1, "// ");
601 PrintF("%d heap slots\n", num_heap_slots_); }
602
603 // Print locals.
604 PrettyPrinter printer;
605 Indent(n1, "// function var\n");
606 if (function_ != NULL) {
607 PrintVar(&printer, n1, function_);
608 }
609
610 Indent(n1, "// temporary vars\n");
611 for (int i = 0; i < temps_.length(); i++) {
612 PrintVar(&printer, n1, temps_[i]);
613 }
614
615 Indent(n1, "// local vars\n");
616 PrintMap(&printer, n1, &variables_);
617
618 Indent(n1, "// dynamic vars\n");
619 if (dynamics_ != NULL) {
620 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
621 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
622 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
623 }
624
625 // Print inner scopes (disable by providing negative n).
626 if (n >= 0) {
627 for (int i = 0; i < inner_scopes_.length(); i++) {
628 PrintF("\n");
629 inner_scopes_[i]->Print(n1);
630 }
631 }
632
633 Indent(n0, "}\n");
634}
635#endif // DEBUG
636
637
638Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
639 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
640 VariableMap* map = dynamics_->GetMap(mode);
641 Variable* var = map->Lookup(name);
642 if (var == NULL) {
643 // Declare a new non-local.
644 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
645 // Allocate it by giving it a dynamic lookup.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100646 var->set_rewrite(new Slot(var, Slot::LOOKUP, -1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000647 }
648 return var;
649}
650
651
652// Lookup a variable starting with this scope. The result is either
Steve Blockd0582a62009-12-15 09:54:21 +0000653// the statically resolved variable belonging to an outer scope, or
654// NULL. It may be NULL because a) we couldn't find a variable, or b)
655// because the variable is just a guess (and may be shadowed by
656// another variable that is introduced dynamically via an 'eval' call
657// or a 'with' statement).
Steve Blocka7e24c12009-10-30 11:49:00 +0000658Variable* Scope::LookupRecursive(Handle<String> name,
659 bool inner_lookup,
660 Variable** invalidated_local) {
661 // If we find a variable, but the current scope calls 'eval', the found
662 // variable may not be the correct one (the 'eval' may introduce a
663 // property with the same name). In that case, remember that the variable
664 // found is just a guess.
665 bool guess = scope_calls_eval_;
666
667 // Try to find the variable in this scope.
668 Variable* var = LocalLookup(name);
669
670 if (var != NULL) {
671 // We found a variable. If this is not an inner lookup, we are done.
672 // (Even if there is an 'eval' in this scope which introduces the
673 // same variable again, the resulting variable remains the same.
674 // Note that enclosing 'with' statements are handled at the call site.)
675 if (!inner_lookup)
676 return var;
677
678 } else {
679 // We did not find a variable locally. Check against the function variable,
680 // if any. We can do this for all scopes, since the function variable is
681 // only present - if at all - for function scopes.
682 //
683 // This lookup corresponds to a lookup in the "intermediate" scope sitting
684 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
685 // the name of named function literal is kept in an intermediate scope
686 // in between this scope and the next outer scope.)
687 if (function_ != NULL && function_->name().is_identical_to(name)) {
688 var = function_;
689
690 } else if (outer_scope_ != NULL) {
691 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
692 // We may have found a variable in an outer scope. However, if
693 // the current scope is inside a 'with', the actual variable may
694 // be a property introduced via the 'with' statement. Then, the
695 // variable we may have found is just a guess.
696 if (scope_inside_with_)
697 guess = true;
698 }
699
700 // If we did not find a variable, we are done.
701 if (var == NULL)
702 return NULL;
703 }
704
705 ASSERT(var != NULL);
706
707 // If this is a lookup from an inner scope, mark the variable.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100708 if (inner_lookup) {
709 var->MarkAsAccessedFromInnerScope();
710 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000711
Steve Blockd0582a62009-12-15 09:54:21 +0000712 // If the variable we have found is just a guess, invalidate the
713 // result. If the found variable is local, record that fact so we
714 // can generate fast code to get it if it is not shadowed by eval.
Steve Blocka7e24c12009-10-30 11:49:00 +0000715 if (guess) {
Steve Blockd0582a62009-12-15 09:54:21 +0000716 if (!var->is_global()) *invalidated_local = var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000717 var = NULL;
718 }
719
720 return var;
721}
722
723
724void Scope::ResolveVariable(Scope* global_scope,
725 Handle<Context> context,
726 VariableProxy* proxy) {
727 ASSERT(global_scope == NULL || global_scope->is_global_scope());
728
729 // If the proxy is already resolved there's nothing to do
730 // (functions and consts may be resolved by the parser).
731 if (proxy->var() != NULL) return;
732
733 // Otherwise, try to resolve the variable.
734 Variable* invalidated_local = NULL;
735 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
736
737 if (proxy->inside_with()) {
738 // If we are inside a local 'with' statement, all bets are off
739 // and we cannot resolve the proxy to a local variable even if
740 // we found an outer matching variable.
741 // Note that we must do a lookup anyway, because if we find one,
742 // we must mark that variable as potentially accessed from this
743 // inner scope (the property may not be in the 'with' object).
744 var = NonLocal(proxy->name(), Variable::DYNAMIC);
745
746 } else {
747 // We are not inside a local 'with' statement.
748
749 if (var == NULL) {
750 // We did not find the variable. We have a global variable
751 // if we are in the global scope (we know already that we
752 // are outside a 'with' statement) or if there is no way
753 // that the variable might be introduced dynamically (through
754 // a local or outer eval() call, or an outer 'with' statement),
755 // or we don't know about the outer scope (because we are
756 // in an eval scope).
757 if (is_global_scope() ||
758 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
759 scope_calls_eval_ || outer_scope_calls_eval_)) {
760 // We must have a global variable.
761 ASSERT(global_scope != NULL);
762 var = global_scope->DeclareGlobal(proxy->name());
763
764 } else if (scope_inside_with_) {
765 // If we are inside a with statement we give up and look up
766 // the variable at runtime.
767 var = NonLocal(proxy->name(), Variable::DYNAMIC);
768
769 } else if (invalidated_local != NULL) {
770 // No with statements are involved and we found a local
771 // variable that might be shadowed by eval introduced
772 // variables.
773 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
774 var->set_local_if_not_shadowed(invalidated_local);
775
776 } else if (outer_scope_is_eval_scope_) {
777 // No with statements and we did not find a local and the code
778 // is executed with a call to eval. The context contains
779 // scope information that we can use to determine if the
780 // variable is global if it is not shadowed by eval-introduced
781 // variables.
782 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
783 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
784
785 } else {
786 var = NonLocal(proxy->name(), Variable::DYNAMIC);
787 }
788
789 } else {
790 // No with statements and we did not find a local and the code
791 // is not executed with a call to eval. We know that this
792 // variable is global unless it is shadowed by eval-introduced
793 // variables.
794 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
795 }
796 }
797 }
798
799 proxy->BindTo(var);
800}
801
802
803void Scope::ResolveVariablesRecursively(Scope* global_scope,
804 Handle<Context> context) {
805 ASSERT(global_scope == NULL || global_scope->is_global_scope());
806
807 // Resolve unresolved variables for this scope.
808 for (int i = 0; i < unresolved_.length(); i++) {
809 ResolveVariable(global_scope, context, unresolved_[i]);
810 }
811
812 // Resolve unresolved variables for inner scopes.
813 for (int i = 0; i < inner_scopes_.length(); i++) {
814 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
815 }
816}
817
818
819bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
820 bool outer_scope_is_eval_scope) {
821 if (outer_scope_calls_eval) {
822 outer_scope_calls_eval_ = true;
823 }
824
825 if (outer_scope_is_eval_scope) {
826 outer_scope_is_eval_scope_ = true;
827 }
828
829 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
830 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
831 for (int i = 0; i < inner_scopes_.length(); i++) {
832 Scope* inner_scope = inner_scopes_[i];
833 if (inner_scope->PropagateScopeInfo(calls_eval, is_eval)) {
834 inner_scope_calls_eval_ = true;
835 }
836 if (inner_scope->force_eager_compilation_) {
837 force_eager_compilation_ = true;
838 }
839 }
840
841 return scope_calls_eval_ || inner_scope_calls_eval_;
842}
843
844
845bool Scope::MustAllocate(Variable* var) {
846 // Give var a read/write use if there is a chance it might be accessed
847 // via an eval() call. This is only possible if the variable has a
848 // visible name.
849 if ((var->is_this() || var->name()->length() > 0) &&
Ben Murdochb8e0da22011-05-16 14:20:40 +0100850 (var->is_accessed_from_inner_scope() ||
Steve Blocka7e24c12009-10-30 11:49:00 +0000851 scope_calls_eval_ || inner_scope_calls_eval_ ||
852 scope_contains_with_)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100853 var->set_is_used(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000854 }
855 // Global variables do not need to be allocated.
Steve Block6ded16b2010-05-10 14:33:55 +0100856 return !var->is_global() && var->is_used();
Steve Blocka7e24c12009-10-30 11:49:00 +0000857}
858
859
860bool Scope::MustAllocateInContext(Variable* var) {
861 // If var is accessed from an inner scope, or if there is a
862 // possibility that it might be accessed from the current or an inner
863 // scope (through an eval() call), it must be allocated in the
864 // context. Exception: temporary variables are not allocated in the
865 // context.
866 return
867 var->mode() != Variable::TEMPORARY &&
Ben Murdochb8e0da22011-05-16 14:20:40 +0100868 (var->is_accessed_from_inner_scope() ||
Steve Blocka7e24c12009-10-30 11:49:00 +0000869 scope_calls_eval_ || inner_scope_calls_eval_ ||
870 scope_contains_with_ || var->is_global());
871}
872
873
874bool Scope::HasArgumentsParameter() {
875 for (int i = 0; i < params_.length(); i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100876 if (params_[i]->name().is_identical_to(FACTORY->arguments_symbol()))
Steve Blocka7e24c12009-10-30 11:49:00 +0000877 return true;
878 }
879 return false;
880}
881
882
883void Scope::AllocateStackSlot(Variable* var) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100884 var->set_rewrite(new Slot(var, Slot::LOCAL, num_stack_slots_++));
Steve Blocka7e24c12009-10-30 11:49:00 +0000885}
886
887
888void Scope::AllocateHeapSlot(Variable* var) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100889 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++));
Steve Blocka7e24c12009-10-30 11:49:00 +0000890}
891
892
893void Scope::AllocateParameterLocals() {
894 ASSERT(is_function_scope());
Steve Block44f0eee2011-05-26 01:26:41 +0100895 Variable* arguments = LocalLookup(FACTORY->arguments_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000896 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
Steve Block44f0eee2011-05-26 01:26:41 +0100897
898 // Parameters are rewritten to arguments[i] if 'arguments' is used in
899 // a non-strict mode function. Strict mode code doesn't alias arguments.
900 bool rewrite_parameters = false;
901
Steve Blocka7e24c12009-10-30 11:49:00 +0000902 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
903 // 'arguments' is used. Unless there is also a parameter called
904 // 'arguments', we must be conservative and access all parameters via
905 // the arguments object: The i'th parameter is rewritten into
906 // '.arguments[i]' (*). If we have a parameter named 'arguments', a
907 // (new) value is always assigned to it via the function
908 // invocation. Then 'arguments' denotes that specific parameter value
909 // and cannot be used to access the parameters, which is why we don't
910 // need to rewrite in that case.
911 //
912 // (*) Instead of having a parameter called 'arguments', we may have an
913 // assignment to 'arguments' in the function body, at some arbitrary
914 // point in time (possibly through an 'eval()' call!). After that
915 // assignment any re-write of parameters would be invalid (was bug
916 // 881452). Thus, we introduce a shadow '.arguments'
917 // variable which also points to the arguments object. For rewrites we
918 // use '.arguments' which remains valid even if we assign to
919 // 'arguments'. To summarize: If we need to rewrite, we allocate an
920 // 'arguments' object dynamically upon function invocation. The compiler
921 // introduces 2 local variables 'arguments' and '.arguments', both of
922 // which originally point to the arguments object that was
923 // allocated. All parameters are rewritten into property accesses via
924 // the '.arguments' variable. Thus, any changes to properties of
925 // 'arguments' are reflected in the variables and vice versa. If the
926 // 'arguments' variable is changed, '.arguments' still points to the
927 // correct arguments object and the rewrites still work.
928
929 // We are using 'arguments'. Tell the code generator that is needs to
930 // allocate the arguments object by setting 'arguments_'.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100931 arguments_ = arguments;
Steve Blocka7e24c12009-10-30 11:49:00 +0000932
Steve Block44f0eee2011-05-26 01:26:41 +0100933 // In strict mode 'arguments' does not alias formal parameters.
934 // Therefore in strict mode we allocate parameters as if 'arguments'
935 // were not used.
936 rewrite_parameters = !is_strict_mode();
937 }
938
939 if (rewrite_parameters) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000940 // We also need the '.arguments' shadow variable. Declare it and create
941 // and bind the corresponding proxy. It's ok to declare it only now
942 // because it's a local variable that is allocated after the parameters
943 // have been allocated.
944 //
945 // Note: This is "almost" at temporary variable but we cannot use
946 // NewTemporary() because the mode needs to be INTERNAL since this
947 // variable may be allocated in the heap-allocated context (temporaries
948 // are never allocated in the context).
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100949 arguments_shadow_ = new Variable(this,
Steve Block44f0eee2011-05-26 01:26:41 +0100950 FACTORY->arguments_shadow_symbol(),
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100951 Variable::INTERNAL,
952 true,
953 Variable::ARGUMENTS);
954 arguments_shadow_->set_is_used(true);
955 temps_.Add(arguments_shadow_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000956
957 // Allocate the parameters by rewriting them into '.arguments[i]' accesses.
958 for (int i = 0; i < params_.length(); i++) {
959 Variable* var = params_[i];
960 ASSERT(var->scope() == this);
961 if (MustAllocate(var)) {
962 if (MustAllocateInContext(var)) {
963 // It is ok to set this only now, because arguments is a local
964 // variable that is allocated after the parameters have been
965 // allocated.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100966 arguments_shadow_->MarkAsAccessedFromInnerScope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000967 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100968 Property* rewrite =
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100969 new Property(new VariableProxy(arguments_shadow_),
970 new Literal(Handle<Object>(Smi::FromInt(i))),
971 RelocInfo::kNoPosition,
972 Property::SYNTHETIC);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100973 rewrite->set_is_arguments_access(true);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100974 var->set_rewrite(rewrite);
Steve Blocka7e24c12009-10-30 11:49:00 +0000975 }
976 }
977
978 } else {
979 // The arguments object is not used, so we can access parameters directly.
980 // The same parameter may occur multiple times in the parameters_ list.
981 // If it does, and if it is not copied into the context object, it must
982 // receive the highest parameter index for that parameter; thus iteration
983 // order is relevant!
984 for (int i = 0; i < params_.length(); i++) {
985 Variable* var = params_[i];
986 ASSERT(var->scope() == this);
987 if (MustAllocate(var)) {
988 if (MustAllocateInContext(var)) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100989 ASSERT(var->rewrite() == NULL ||
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100990 (var->AsSlot() != NULL &&
991 var->AsSlot()->type() == Slot::CONTEXT));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100992 if (var->rewrite() == NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000993 // Only set the heap allocation if the parameter has not
994 // been allocated yet.
995 AllocateHeapSlot(var);
996 }
997 } else {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100998 ASSERT(var->rewrite() == NULL ||
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100999 (var->AsSlot() != NULL &&
1000 var->AsSlot()->type() == Slot::PARAMETER));
Steve Blocka7e24c12009-10-30 11:49:00 +00001001 // Set the parameter index always, even if the parameter
1002 // was seen before! (We need to access the actual parameter
1003 // supplied for the last occurrence of a multiply declared
1004 // parameter.)
Ben Murdochb8e0da22011-05-16 14:20:40 +01001005 var->set_rewrite(new Slot(var, Slot::PARAMETER, i));
Steve Blocka7e24c12009-10-30 11:49:00 +00001006 }
1007 }
1008 }
1009 }
1010}
1011
1012
1013void Scope::AllocateNonParameterLocal(Variable* var) {
1014 ASSERT(var->scope() == this);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001015 ASSERT(var->rewrite() == NULL ||
Steve Block44f0eee2011-05-26 01:26:41 +01001016 (!var->IsVariable(FACTORY->result_symbol())) ||
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001017 (var->AsSlot() == NULL || var->AsSlot()->type() != Slot::LOCAL));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001018 if (var->rewrite() == NULL && MustAllocate(var)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001019 if (MustAllocateInContext(var)) {
1020 AllocateHeapSlot(var);
1021 } else {
1022 AllocateStackSlot(var);
1023 }
1024 }
1025}
1026
1027
1028void Scope::AllocateNonParameterLocals() {
1029 // All variables that have no rewrite yet are non-parameter locals.
1030 for (int i = 0; i < temps_.length(); i++) {
1031 AllocateNonParameterLocal(temps_[i]);
1032 }
1033
1034 for (VariableMap::Entry* p = variables_.Start();
1035 p != NULL;
1036 p = variables_.Next(p)) {
1037 Variable* var = reinterpret_cast<Variable*>(p->value);
1038 AllocateNonParameterLocal(var);
1039 }
1040
1041 // For now, function_ must be allocated at the very end. If it gets
1042 // allocated in the context, it must be the last slot in the context,
1043 // because of the current ScopeInfo implementation (see
1044 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1045 if (function_ != NULL) {
1046 AllocateNonParameterLocal(function_);
1047 }
1048}
1049
1050
1051void Scope::AllocateVariablesRecursively() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001052 // Allocate variables for inner scopes.
1053 for (int i = 0; i < inner_scopes_.length(); i++) {
1054 inner_scopes_[i]->AllocateVariablesRecursively();
1055 }
1056
Ben Murdochb8e0da22011-05-16 14:20:40 +01001057 // If scope is already resolved, we still need to allocate
1058 // variables in inner scopes which might not had been resolved yet.
1059 if (resolved()) return;
1060 // The number of slots required for variables.
1061 num_stack_slots_ = 0;
1062 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1063
Steve Blocka7e24c12009-10-30 11:49:00 +00001064 // Allocate variables for this scope.
1065 // Parameters must be allocated first, if any.
1066 if (is_function_scope()) AllocateParameterLocals();
1067 AllocateNonParameterLocals();
1068
1069 // Allocate context if necessary.
1070 bool must_have_local_context = false;
1071 if (scope_calls_eval_ || scope_contains_with_) {
1072 // The context for the eval() call or 'with' statement in this scope.
1073 // Unless we are in the global or an eval scope, we need a local
1074 // context even if we didn't statically allocate any locals in it,
1075 // and the compiler will access the context variable. If we are
1076 // not in an inner scope, the scope is provided from the outside.
1077 must_have_local_context = is_function_scope();
1078 }
1079
1080 // If we didn't allocate any locals in the local context, then we only
1081 // need the minimal number of slots if we must have a local context.
1082 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1083 !must_have_local_context) {
1084 num_heap_slots_ = 0;
1085 }
1086
1087 // Allocation done.
1088 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1089}
1090
1091} } // namespace v8::internal