📄 scopes.cc.svn-base
字号:
scope_calls_eval_ || scope_inside_with_)) { // We must look up the variable at runtime, and we don't // know anything else. var = NonLocal(proxy->name()); } else { // We must have a global variable. ASSERT(global_scope != NULL); var = new Variable(global_scope, proxy->name(), Variable::DYNAMIC, true, false); // Ideally we simply rewrite these variables into property // accesses. Unfortunately, we cannot do this here at the // moment because then we can't differentiate between // global variable ('x') and global property ('this.x') access. // If 'x' doesn't exist, the former leads to an error, while the // latter returns undefined. Sigh... // var->rewrite_ = new Property(new Literal(env_->global()), // new Literal(proxy->name())); } } } proxy->BindTo(var);}void Scope::ResolveVariablesRecursively(Scope* global_scope) { ASSERT(global_scope == NULL || global_scope->is_global_scope()); // Resolve unresolved variables for this scope. for (int i = 0; i < unresolved_.length(); i++) { ResolveVariable(global_scope, unresolved_[i]); } // Resolve unresolved variables for inner scopes. for (int i = 0; i < inner_scopes_.length(); i++) { inner_scopes_[i]->ResolveVariablesRecursively(global_scope); }}bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval) { if (outer_scope_calls_eval) { outer_scope_calls_eval_ = true; } bool b = scope_calls_eval_ || outer_scope_calls_eval_; for (int i = 0; i < inner_scopes_.length(); i++) { Scope* inner_scope = inner_scopes_[i]; if (inner_scope->PropagateScopeInfo(b)) { inner_scope_calls_eval_ = true; } if (inner_scope->force_eager_compilation_) { force_eager_compilation_ = true; } } return scope_calls_eval_ || inner_scope_calls_eval_;}bool Scope::MustAllocate(Variable* var) { // Give var a read/write use if there is a chance it might be // accessed via an eval() call, or if it is a global variable. // This is only possible if the variable has a visible name. if ((var->is_this() || var->name()->length() > 0) && (var->is_accessed_from_inner_scope_ || scope_calls_eval_ || inner_scope_calls_eval_ || scope_contains_with_ || var->is_global())) { var->var_uses()->RecordAccess(1); } return var->var_uses()->is_used();}bool Scope::MustAllocateInContext(Variable* var) { // If var is accessed from an inner scope, or if there is a // possibility that it might be accessed from the current or // an inner scope (through an eval() call), it must be allocated // in the context. // Exceptions: Global variables and temporary variables must // never be allocated in the (FixedArray part of the) context. return var->mode() != Variable::TEMPORARY && (var->is_accessed_from_inner_scope_ || scope_calls_eval_ || inner_scope_calls_eval_ || scope_contains_with_ || var->is_global());}bool Scope::HasArgumentsParameter() { for (int i = 0; i < params_.length(); i++) { if (params_[i]->name().is_identical_to(Factory::arguments_symbol())) return true; } return false;}void Scope::AllocateStackSlot(Variable* var) { var->rewrite_ = new Slot(var, Slot::LOCAL, num_stack_slots_++);}void Scope::AllocateHeapSlot(Variable* var) { var->rewrite_ = new Slot(var, Slot::CONTEXT, num_heap_slots_++);}void Scope::AllocateParameterLocals() { ASSERT(is_function_scope()); Variable* arguments = Lookup(Factory::arguments_symbol()); ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly if (MustAllocate(arguments) && !HasArgumentsParameter()) { // 'arguments' is used. Unless there is also a parameter called // 'arguments', we must be conservative and access all parameters via // the arguments object: The i'th parameter is rewritten into // '.arguments[i]' (*). If we have a parameter named 'arguments', a // (new) value is always assigned to it via the function // invocation. Then 'arguments' denotes that specific parameter value // and cannot be used to access the parameters, which is why we don't // need to rewrite in that case. // // (*) Instead of having a parameter called 'arguments', we may have an // assignment to 'arguments' in the function body, at some arbitrary // point in time (possibly through an 'eval()' call!). After that // assignment any re-write of parameters would be invalid (was bug // 881452). Thus, we introduce a shadow '.arguments' // variable which also points to the arguments object. For rewrites we // use '.arguments' which remains valid even if we assign to // 'arguments'. To summarize: If we need to rewrite, we allocate an // 'arguments' object dynamically upon function invocation. The compiler // introduces 2 local variables 'arguments' and '.arguments', both of // which originally point to the arguments object that was // allocated. All parameters are rewritten into property accesses via // the '.arguments' variable. Thus, any changes to properties of // 'arguments' are reflected in the variables and vice versa. If the // 'arguments' variable is changed, '.arguments' still points to the // correct arguments object and the rewrites still work. // We are using 'arguments'. Tell the code generator that is needs to // allocate the arguments object by setting 'arguments_'. arguments_ = new VariableProxy(Factory::arguments_symbol(), false, false); arguments_->BindTo(arguments); // We also need the '.arguments' shadow variable. Declare it and create // and bind the corresponding proxy. It's ok to declare it only now // because it's a local variable that is allocated after the parameters // have been allocated. // // Note: This is "almost" at temporary variable but we cannot use // NewTemporary() because the mode needs to be INTERNAL since this // variable may be allocated in the heap-allocated context (temporaries // are never allocated in the context). Variable* arguments_shadow = new Variable(this, Factory::arguments_shadow_symbol(), Variable::INTERNAL, true, false); arguments_shadow_ = new VariableProxy(Factory::arguments_shadow_symbol(), false, false); arguments_shadow_->BindTo(arguments_shadow); temps_.Add(arguments_shadow); // Allocate the parameters by rewriting them into '.arguments[i]' accesses. for (int i = 0; i < params_.length(); i++) { Variable* var = params_[i]; ASSERT(var->scope() == this); if (MustAllocate(var)) { if (MustAllocateInContext(var)) { // It is ok to set this only now, because arguments is a local // variable that is allocated after the parameters have been // allocated. arguments_shadow->is_accessed_from_inner_scope_ = true; } var->rewrite_ = new Property(arguments_shadow_, new Literal(Handle<Object>(Smi::FromInt(i))), RelocInfo::kNoPosition); arguments_shadow->var_uses()->RecordUses(var->var_uses()); } } } else { // The arguments object is not used, so we can access parameters directly. // The same parameter may occur multiple times in the parameters_ list. // If it does, and if it is not copied into the context object, it must // receive the highest parameter index for that parameter; thus iteration // order is relevant! for (int i = 0; i < params_.length(); i++) { Variable* var = params_[i]; ASSERT(var->scope() == this); if (MustAllocate(var)) { if (MustAllocateInContext(var)) { ASSERT(var->rewrite_ == NULL || (var->slot() != NULL && var->slot()->type() == Slot::CONTEXT)); if (var->rewrite_ == NULL) { // Only set the heap allocation if the parameter has not // been allocated yet. AllocateHeapSlot(var); } } else { ASSERT(var->rewrite_ == NULL || (var->slot() != NULL && var->slot()->type() == Slot::PARAMETER)); // Set the parameter index always, even if the parameter // was seen before! (We need to access the actual parameter // supplied for the last occurrence of a multiply declared // parameter.) var->rewrite_ = new Slot(var, Slot::PARAMETER, i); } } } }}void Scope::AllocateNonParameterLocal(Variable* var) { ASSERT(var->scope() == this); ASSERT(var->rewrite_ == NULL || (!var->IsVariable(Factory::result_symbol())) || (var->slot() == NULL || var->slot()->type() != Slot::LOCAL)); if (MustAllocate(var) && var->rewrite_ == NULL) { if (MustAllocateInContext(var)) { AllocateHeapSlot(var); } else { AllocateStackSlot(var); } }}void Scope::AllocateNonParameterLocals() { // Each variable occurs exactly once in the locals_ list; all // variables that have no rewrite yet are non-parameter locals. // Sort them according to use such that the locals with more uses // get allocated first. if (FLAG_usage_computation) { // This is currently not implemented. } for (int i = 0; i < temps_.length(); i++) { AllocateNonParameterLocal(temps_[i]); } for (LocalsMap::Entry* p = locals_.Start(); p != NULL; p = locals_.Next(p)) { Variable* var = reinterpret_cast<Variable*>(p->value); AllocateNonParameterLocal(var); } // Note: For now, function_ must be allocated at the very end. If // it gets allocated in the context, it must be the last slot in the // context, because of the current ScopeInfo implementation (see // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor). if (function_ != NULL) { AllocateNonParameterLocal(function_); }}void Scope::AllocateVariablesRecursively() { // The number of slots required for variables. num_stack_slots_ = 0; num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; // Allocate variables for inner scopes. for (int i = 0; i < inner_scopes_.length(); i++) { inner_scopes_[i]->AllocateVariablesRecursively(); } // Allocate variables for this scope. // Parameters must be allocated first, if any. if (is_function_scope()) AllocateParameterLocals(); AllocateNonParameterLocals(); // Allocate context if necessary. bool must_have_local_context = false; if (scope_calls_eval_ || scope_contains_with_) { // The context for the eval() call or 'with' statement in this scope. // Unless we are in the global or an eval scope, we need a local // context even if we didn't statically allocate any locals in it, // and the compiler will access the context variable. If we are // not in an inner scope, the scope is provided from the outside. must_have_local_context = is_function_scope(); } // If we didn't allocate any locals in the local context, then we only // need the minimal number of slots if we must have a local context. if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && !must_have_local_context) { num_heap_slots_ = 0; } // Allocation done. ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);}} } // namespace v8::internal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -