karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // 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 <stdlib.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | |
| 32 | #include "scopeinfo.h" |
| 33 | #include "scopes.h" |
| 34 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 35 | #include "allocation-inl.h" |
| 36 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 37 | namespace v8 { |
| 38 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
| 40 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 41 | Handle<ScopeInfo> ScopeInfo::Create(Scope* scope, Zone* zone) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 42 | // Collect stack and context locals. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 43 | ZoneList<Variable*> stack_locals(scope->StackLocalCount(), zone); |
| 44 | ZoneList<Variable*> context_locals(scope->ContextLocalCount(), zone); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 45 | scope->CollectStackAndContextLocals(&stack_locals, &context_locals); |
| 46 | const int stack_local_count = stack_locals.length(); |
| 47 | const int context_local_count = context_locals.length(); |
| 48 | // Make sure we allocate the correct amount. |
| 49 | ASSERT(scope->StackLocalCount() == stack_local_count); |
| 50 | ASSERT(scope->ContextLocalCount() == context_local_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 52 | // Determine use and location of the function variable if it is present. |
| 53 | FunctionVariableInfo function_name_info; |
| 54 | VariableMode function_variable_mode; |
| 55 | if (scope->is_function_scope() && scope->function() != NULL) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 56 | Variable* var = scope->function()->proxy()->var(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 57 | if (!var->is_used()) { |
| 58 | function_name_info = UNUSED; |
| 59 | } else if (var->IsContextSlot()) { |
| 60 | function_name_info = CONTEXT; |
| 61 | } else { |
| 62 | ASSERT(var->IsStackLocal()); |
| 63 | function_name_info = STACK; |
| 64 | } |
| 65 | function_variable_mode = var->mode(); |
| 66 | } else { |
| 67 | function_name_info = NONE; |
| 68 | function_variable_mode = VAR; |
| 69 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 70 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 71 | const bool has_function_name = function_name_info != NONE; |
| 72 | const int parameter_count = scope->num_parameters(); |
| 73 | const int length = kVariablePartIndex |
| 74 | + parameter_count + stack_local_count + 2 * context_local_count |
| 75 | + (has_function_name ? 2 : 0); |
| 76 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 77 | Factory* factory = zone->isolate()->factory(); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 78 | Handle<ScopeInfo> scope_info = factory->NewScopeInfo(length); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 79 | |
| 80 | // Encode the flags. |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 81 | int flags = ScopeTypeField::encode(scope->scope_type()) | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 82 | CallsEvalField::encode(scope->calls_eval()) | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 83 | LanguageModeField::encode(scope->language_mode()) | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 84 | FunctionVariableField::encode(function_name_info) | |
| 85 | FunctionVariableMode::encode(function_variable_mode); |
| 86 | scope_info->SetFlags(flags); |
| 87 | scope_info->SetParameterCount(parameter_count); |
| 88 | scope_info->SetStackLocalCount(stack_local_count); |
| 89 | scope_info->SetContextLocalCount(context_local_count); |
| 90 | |
| 91 | int index = kVariablePartIndex; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 92 | // Add parameters. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 93 | ASSERT(index == scope_info->ParameterEntriesIndex()); |
| 94 | for (int i = 0; i < parameter_count; ++i) { |
| 95 | scope_info->set(index++, *scope->parameter(i)->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 96 | } |
| 97 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 98 | // Add stack locals' names. We are assuming that the stack locals' |
| 99 | // slots are allocated in increasing order, so we can simply add |
| 100 | // them to the ScopeInfo object. |
| 101 | ASSERT(index == scope_info->StackLocalEntriesIndex()); |
| 102 | for (int i = 0; i < stack_local_count; ++i) { |
| 103 | ASSERT(stack_locals[i]->index() == i); |
| 104 | scope_info->set(index++, *stack_locals[i]->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | } |
| 106 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 107 | // Due to usage analysis, context-allocated locals are not necessarily in |
| 108 | // increasing order: Some of them may be parameters which are allocated before |
| 109 | // the non-parameter locals. When the non-parameter locals are sorted |
| 110 | // according to usage, the allocated slot indices may not be in increasing |
| 111 | // order with the variable list anymore. Thus, we first need to sort them by |
| 112 | // context slot index before adding them to the ScopeInfo object. |
| 113 | context_locals.Sort(&Variable::CompareIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 115 | // Add context locals' names. |
| 116 | ASSERT(index == scope_info->ContextLocalNameEntriesIndex()); |
| 117 | for (int i = 0; i < context_local_count; ++i) { |
| 118 | scope_info->set(index++, *context_locals[i]->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | } |
| 120 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 121 | // Add context locals' info. |
| 122 | ASSERT(index == scope_info->ContextLocalInfoEntriesIndex()); |
| 123 | for (int i = 0; i < context_local_count; ++i) { |
| 124 | Variable* var = context_locals[i]; |
| 125 | uint32_t value = ContextLocalMode::encode(var->mode()) | |
| 126 | ContextLocalInitFlag::encode(var->initialization_flag()); |
| 127 | scope_info->set(index++, Smi::FromInt(value)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 129 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 130 | // If present, add the function variable name and its index. |
| 131 | ASSERT(index == scope_info->FunctionNameEntryIndex()); |
| 132 | if (has_function_name) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 133 | int var_index = scope->function()->proxy()->var()->index(); |
| 134 | scope_info->set(index++, *scope->function()->proxy()->name()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 135 | scope_info->set(index++, Smi::FromInt(var_index)); |
| 136 | ASSERT(function_name_info != STACK || |
| 137 | (var_index == scope_info->StackLocalCount() && |
| 138 | var_index == scope_info->StackSlotCount() - 1)); |
| 139 | ASSERT(function_name_info != CONTEXT || |
| 140 | var_index == scope_info->ContextLength() - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 141 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 142 | |
| 143 | ASSERT(index == scope_info->length()); |
| 144 | ASSERT(scope->num_parameters() == scope_info->ParameterCount()); |
| 145 | ASSERT(scope->num_stack_slots() == scope_info->StackSlotCount()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 146 | ASSERT(scope->num_heap_slots() == scope_info->ContextLength() || |
| 147 | (scope->num_heap_slots() == kVariablePartIndex && |
| 148 | scope_info->ContextLength() == 0)); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 149 | return scope_info; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 153 | ScopeInfo* ScopeInfo::Empty(Isolate* isolate) { |
| 154 | return reinterpret_cast<ScopeInfo*>(isolate->heap()->empty_fixed_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 158 | ScopeType ScopeInfo::scope_type() { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 159 | ASSERT(length() > 0); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 160 | return ScopeTypeField::decode(Flags()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 164 | bool ScopeInfo::CallsEval() { |
| 165 | return length() > 0 && CallsEvalField::decode(Flags()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 169 | LanguageMode ScopeInfo::language_mode() { |
| 170 | return length() > 0 ? LanguageModeField::decode(Flags()) : CLASSIC_MODE; |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 174 | int ScopeInfo::LocalCount() { |
| 175 | return StackLocalCount() + ContextLocalCount(); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | int ScopeInfo::StackSlotCount() { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 180 | if (length() > 0) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 181 | bool function_name_stack_slot = |
| 182 | FunctionVariableField::decode(Flags()) == STACK; |
| 183 | return StackLocalCount() + (function_name_stack_slot ? 1 : 0); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 189 | int ScopeInfo::ContextLength() { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 190 | if (length() > 0) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 191 | int context_locals = ContextLocalCount(); |
| 192 | bool function_name_context_slot = |
| 193 | FunctionVariableField::decode(Flags()) == CONTEXT; |
| 194 | bool has_context = context_locals > 0 || |
| 195 | function_name_context_slot || |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 196 | scope_type() == WITH_SCOPE || |
| 197 | (scope_type() == FUNCTION_SCOPE && CallsEval()) || |
| 198 | scope_type() == MODULE_SCOPE; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 199 | if (has_context) { |
| 200 | return Context::MIN_CONTEXT_SLOTS + context_locals + |
| 201 | (function_name_context_slot ? 1 : 0); |
| 202 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 203 | } |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 208 | bool ScopeInfo::HasFunctionName() { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 209 | if (length() > 0) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 210 | return NONE != FunctionVariableField::decode(Flags()); |
| 211 | } else { |
| 212 | return false; |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 213 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 217 | bool ScopeInfo::HasHeapAllocatedLocals() { |
| 218 | if (length() > 0) { |
| 219 | return ContextLocalCount() > 0; |
| 220 | } else { |
| 221 | return false; |
| 222 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 226 | bool ScopeInfo::HasContext() { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 227 | return ContextLength() > 0; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | String* ScopeInfo::FunctionName() { |
| 232 | ASSERT(HasFunctionName()); |
| 233 | return String::cast(get(FunctionNameEntryIndex())); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | String* ScopeInfo::ParameterName(int var) { |
| 238 | ASSERT(0 <= var && var < ParameterCount()); |
| 239 | int info_index = ParameterEntriesIndex() + var; |
| 240 | return String::cast(get(info_index)); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | String* ScopeInfo::LocalName(int var) { |
| 245 | ASSERT(0 <= var && var < LocalCount()); |
| 246 | ASSERT(StackLocalEntriesIndex() + StackLocalCount() == |
| 247 | ContextLocalNameEntriesIndex()); |
| 248 | int info_index = StackLocalEntriesIndex() + var; |
| 249 | return String::cast(get(info_index)); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | String* ScopeInfo::StackLocalName(int var) { |
| 254 | ASSERT(0 <= var && var < StackLocalCount()); |
| 255 | int info_index = StackLocalEntriesIndex() + var; |
| 256 | return String::cast(get(info_index)); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | String* ScopeInfo::ContextLocalName(int var) { |
| 261 | ASSERT(0 <= var && var < ContextLocalCount()); |
| 262 | int info_index = ContextLocalNameEntriesIndex() + var; |
| 263 | return String::cast(get(info_index)); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | VariableMode ScopeInfo::ContextLocalMode(int var) { |
| 268 | ASSERT(0 <= var && var < ContextLocalCount()); |
| 269 | int info_index = ContextLocalInfoEntriesIndex() + var; |
| 270 | int value = Smi::cast(get(info_index))->value(); |
| 271 | return ContextLocalMode::decode(value); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | InitializationFlag ScopeInfo::ContextLocalInitFlag(int var) { |
| 276 | ASSERT(0 <= var && var < ContextLocalCount()); |
| 277 | int info_index = ContextLocalInfoEntriesIndex() + var; |
| 278 | int value = Smi::cast(get(info_index))->value(); |
| 279 | return ContextLocalInitFlag::decode(value); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | int ScopeInfo::StackSlotIndex(String* name) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 284 | ASSERT(name->IsInternalizedString()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 285 | if (length() > 0) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 286 | int start = StackLocalEntriesIndex(); |
| 287 | int end = StackLocalEntriesIndex() + StackLocalCount(); |
| 288 | for (int i = start; i < end; ++i) { |
| 289 | if (name == get(i)) { |
| 290 | return i - start; |
| 291 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | return -1; |
| 295 | } |
| 296 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 297 | |
| 298 | int ScopeInfo::ContextSlotIndex(String* name, |
| 299 | VariableMode* mode, |
| 300 | InitializationFlag* init_flag) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 301 | ASSERT(name->IsInternalizedString()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 302 | ASSERT(mode != NULL); |
| 303 | ASSERT(init_flag != NULL); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 304 | if (length() > 0) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 305 | ContextSlotCache* context_slot_cache = GetIsolate()->context_slot_cache(); |
| 306 | int result = context_slot_cache->Lookup(this, name, mode, init_flag); |
| 307 | if (result != ContextSlotCache::kNotFound) { |
| 308 | ASSERT(result < ContextLength()); |
| 309 | return result; |
| 310 | } |
| 311 | |
| 312 | int start = ContextLocalNameEntriesIndex(); |
| 313 | int end = ContextLocalNameEntriesIndex() + ContextLocalCount(); |
| 314 | for (int i = start; i < end; ++i) { |
| 315 | if (name == get(i)) { |
| 316 | int var = i - start; |
| 317 | *mode = ContextLocalMode(var); |
| 318 | *init_flag = ContextLocalInitFlag(var); |
| 319 | result = Context::MIN_CONTEXT_SLOTS + var; |
| 320 | context_slot_cache->Update(this, name, *mode, *init_flag, result); |
| 321 | ASSERT(result < ContextLength()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 322 | return result; |
| 323 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 324 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 325 | // Cache as not found. Mode and init flag don't matter. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 326 | context_slot_cache->Update(this, name, INTERNAL, kNeedsInitialization, -1); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 327 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 332 | int ScopeInfo::ParameterIndex(String* name) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 333 | ASSERT(name->IsInternalizedString()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 334 | if (length() > 0) { |
| 335 | // We must read parameters from the end since for |
| 336 | // multiply declared parameters the value of the |
| 337 | // last declaration of that parameter is used |
| 338 | // inside a function (and thus we need to look |
| 339 | // at the last index). Was bug# 1110337. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 340 | int start = ParameterEntriesIndex(); |
| 341 | int end = ParameterEntriesIndex() + ParameterCount(); |
| 342 | for (int i = end - 1; i >= start; --i) { |
| 343 | if (name == get(i)) { |
| 344 | return i - start; |
| 345 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 352 | int ScopeInfo::FunctionContextSlotIndex(String* name, VariableMode* mode) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 353 | ASSERT(name->IsInternalizedString()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 354 | ASSERT(mode != NULL); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 355 | if (length() > 0) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 356 | if (FunctionVariableField::decode(Flags()) == CONTEXT && |
| 357 | FunctionName() == name) { |
| 358 | *mode = FunctionVariableMode::decode(Flags()); |
| 359 | return Smi::cast(get(FunctionNameEntryIndex() + 1))->value(); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 366 | bool ScopeInfo::CopyContextLocalsToScopeObject(Handle<ScopeInfo> scope_info, |
| 367 | Handle<Context> context, |
| 368 | Handle<JSObject> scope_object) { |
| 369 | Isolate* isolate = scope_info->GetIsolate(); |
| 370 | int local_count = scope_info->ContextLocalCount(); |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 371 | if (local_count == 0) return true; |
| 372 | // Fill all context locals to the context extension. |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 373 | int start = scope_info->ContextLocalNameEntriesIndex(); |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 374 | int end = start + local_count; |
| 375 | for (int i = start; i < end; ++i) { |
| 376 | int context_index = Context::MIN_CONTEXT_SLOTS + i - start; |
| 377 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 378 | isolate, |
| 379 | SetProperty(isolate, |
| 380 | scope_object, |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 381 | Handle<String>(String::cast(scope_info->get(i))), |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 382 | Handle<Object>(context->get(context_index), isolate), |
| 383 | ::NONE, |
| 384 | kNonStrictMode), |
| 385 | false); |
| 386 | } |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 391 | int ScopeInfo::ParameterEntriesIndex() { |
| 392 | ASSERT(length() > 0); |
| 393 | return kVariablePartIndex; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | int ScopeInfo::StackLocalEntriesIndex() { |
| 398 | return ParameterEntriesIndex() + ParameterCount(); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | int ScopeInfo::ContextLocalNameEntriesIndex() { |
| 403 | return StackLocalEntriesIndex() + StackLocalCount(); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | int ScopeInfo::ContextLocalInfoEntriesIndex() { |
| 408 | return ContextLocalNameEntriesIndex() + ContextLocalCount(); |
| 409 | } |
| 410 | |
| 411 | |
| 412 | int ScopeInfo::FunctionNameEntryIndex() { |
| 413 | return ContextLocalInfoEntriesIndex() + ContextLocalCount(); |
| 414 | } |
| 415 | |
| 416 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 417 | int ContextSlotCache::Hash(Object* data, String* name) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 418 | // Uses only lower 32 bits if pointers are larger. |
| 419 | uintptr_t addr_hash = |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 420 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(data)) >> 2; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 421 | return static_cast<int>((addr_hash ^ name->Hash()) % kLength); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 425 | int ContextSlotCache::Lookup(Object* data, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 426 | String* name, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 427 | VariableMode* mode, |
| 428 | InitializationFlag* init_flag) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 429 | int index = Hash(data, name); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 430 | Key& key = keys_[index]; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 431 | if ((key.data == data) && key.name->Equals(name)) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 432 | Value result(values_[index]); |
| 433 | if (mode != NULL) *mode = result.mode(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 434 | if (init_flag != NULL) *init_flag = result.initialization_flag(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 435 | return result.index() + kNotFound; |
| 436 | } |
| 437 | return kNotFound; |
| 438 | } |
| 439 | |
| 440 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 441 | void ContextSlotCache::Update(Object* data, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 442 | String* name, |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 443 | VariableMode mode, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 444 | InitializationFlag init_flag, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 445 | int slot_index) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 446 | String* internalized_name; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 447 | ASSERT(slot_index > kNotFound); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 448 | if (name->GetIsolate()->heap()->InternalizeStringIfExists( |
| 449 | name, &internalized_name)) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 450 | int index = Hash(data, internalized_name); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 451 | Key& key = keys_[index]; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 452 | key.data = data; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 453 | key.name = internalized_name; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 454 | // Please note value only takes a uint as index. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 455 | values_[index] = Value(mode, init_flag, slot_index - kNotFound).raw(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 456 | #ifdef DEBUG |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 457 | ValidateEntry(data, name, mode, init_flag, slot_index); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 458 | #endif |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
| 463 | void ContextSlotCache::Clear() { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 464 | for (int index = 0; index < kLength; index++) keys_[index].data = NULL; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 468 | #ifdef DEBUG |
| 469 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 470 | void ContextSlotCache::ValidateEntry(Object* data, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 471 | String* name, |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 472 | VariableMode mode, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 473 | InitializationFlag init_flag, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 474 | int slot_index) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 475 | String* internalized_name; |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 476 | if (name->GetIsolate()->heap()->InternalizeStringIfExists( |
| 477 | name, &internalized_name)) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 478 | int index = Hash(data, name); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 479 | Key& key = keys_[index]; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 480 | ASSERT(key.data == data); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 481 | ASSERT(key.name->Equals(name)); |
| 482 | Value result(values_[index]); |
| 483 | ASSERT(result.mode() == mode); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 484 | ASSERT(result.initialization_flag() == init_flag); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 485 | ASSERT(result.index() + kNotFound == slot_index); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 490 | static void PrintList(const char* list_name, |
| 491 | int nof_internal_slots, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 492 | int start, |
| 493 | int end, |
| 494 | ScopeInfo* scope_info) { |
| 495 | if (start < end) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 496 | PrintF("\n // %s\n", list_name); |
| 497 | if (nof_internal_slots > 0) { |
| 498 | PrintF(" %2d - %2d [internal slots]\n", 0 , nof_internal_slots - 1); |
| 499 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 500 | for (int i = nof_internal_slots; start < end; ++i, ++start) { |
| 501 | PrintF(" %2d ", i); |
| 502 | String::cast(scope_info->get(start))->ShortPrint(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 503 | PrintF("\n"); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 509 | void ScopeInfo::Print() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 510 | PrintF("ScopeInfo "); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 511 | if (HasFunctionName()) { |
| 512 | FunctionName()->ShortPrint(); |
| 513 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 514 | PrintF("/* no function name */"); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 515 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 516 | PrintF("{"); |
| 517 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 518 | PrintList("parameters", 0, |
| 519 | ParameterEntriesIndex(), |
| 520 | ParameterEntriesIndex() + ParameterCount(), |
| 521 | this); |
| 522 | PrintList("stack slots", 0, |
| 523 | StackLocalEntriesIndex(), |
| 524 | StackLocalEntriesIndex() + StackLocalCount(), |
| 525 | this); |
| 526 | PrintList("context slots", |
| 527 | Context::MIN_CONTEXT_SLOTS, |
| 528 | ContextLocalNameEntriesIndex(), |
| 529 | ContextLocalNameEntriesIndex() + ContextLocalCount(), |
| 530 | this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 531 | |
| 532 | PrintF("}\n"); |
| 533 | } |
| 534 | #endif // DEBUG |
| 535 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 536 | |
| 537 | //--------------------------------------------------------------------------- |
| 538 | // ModuleInfo. |
| 539 | |
| 540 | Handle<ModuleInfo> ModuleInfo::Create( |
| 541 | Isolate* isolate, Interface* interface, Scope* scope) { |
| 542 | Handle<ModuleInfo> info = Allocate(isolate, interface->Length()); |
| 543 | info->set_host_index(interface->Index()); |
| 544 | int i = 0; |
| 545 | for (Interface::Iterator it = interface->iterator(); |
| 546 | !it.done(); it.Advance(), ++i) { |
| 547 | Variable* var = scope->LocalLookup(it.name()); |
| 548 | info->set_name(i, *it.name()); |
| 549 | info->set_mode(i, var->mode()); |
| 550 | ASSERT((var->mode() == MODULE) == (it.interface()->IsModule())); |
| 551 | if (var->mode() == MODULE) { |
| 552 | ASSERT(it.interface()->IsFrozen()); |
| 553 | ASSERT(it.interface()->Index() >= 0); |
| 554 | info->set_index(i, it.interface()->Index()); |
| 555 | } else { |
| 556 | ASSERT(var->index() >= 0); |
| 557 | info->set_index(i, var->index()); |
| 558 | } |
| 559 | } |
| 560 | ASSERT(i == info->length()); |
| 561 | return info; |
| 562 | } |
| 563 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 564 | } } // namespace v8::internal |