OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/v8.h" |
| 6 |
| 7 #include "src/ic/call-optimization.h" |
| 8 #include "src/ic/handler-compiler.h" |
| 9 #include "src/ic/ic-inl.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 |
| 14 |
| 15 Handle<Code> PropertyHandlerCompiler::Find(Handle<Name> name, |
| 16 Handle<Map> stub_holder, |
| 17 Code::Kind kind, |
| 18 CacheHolderFlag cache_holder, |
| 19 Code::StubType type) { |
| 20 Code::Flags flags = Code::ComputeHandlerFlags(kind, type, cache_holder); |
| 21 Object* probe = stub_holder->FindInCodeCache(*name, flags); |
| 22 if (probe->IsCode()) return handle(Code::cast(probe)); |
| 23 return Handle<Code>::null(); |
| 24 } |
| 25 |
| 26 |
| 27 Handle<Code> NamedLoadHandlerCompiler::ComputeLoadNonexistent( |
| 28 Handle<Name> name, Handle<HeapType> type) { |
| 29 Isolate* isolate = name->GetIsolate(); |
| 30 Handle<Map> receiver_map = IC::TypeToMap(*type, isolate); |
| 31 if (receiver_map->prototype()->IsNull()) { |
| 32 // TODO(jkummerow/verwaest): If there is no prototype and the property |
| 33 // is nonexistent, introduce a builtin to handle this (fast properties |
| 34 // -> return undefined, dictionary properties -> do negative lookup). |
| 35 return Handle<Code>(); |
| 36 } |
| 37 CacheHolderFlag flag; |
| 38 Handle<Map> stub_holder_map = |
| 39 IC::GetHandlerCacheHolder(*type, false, isolate, &flag); |
| 40 |
| 41 // If no dictionary mode objects are present in the prototype chain, the load |
| 42 // nonexistent IC stub can be shared for all names for a given map and we use |
| 43 // the empty string for the map cache in that case. If there are dictionary |
| 44 // mode objects involved, we need to do negative lookups in the stub and |
| 45 // therefore the stub will be specific to the name. |
| 46 Handle<Name> cache_name = |
| 47 receiver_map->is_dictionary_map() |
| 48 ? name |
| 49 : Handle<Name>::cast(isolate->factory()->nonexistent_symbol()); |
| 50 Handle<Map> current_map = stub_holder_map; |
| 51 Handle<JSObject> last(JSObject::cast(receiver_map->prototype())); |
| 52 while (true) { |
| 53 if (current_map->is_dictionary_map()) cache_name = name; |
| 54 if (current_map->prototype()->IsNull()) break; |
| 55 last = handle(JSObject::cast(current_map->prototype())); |
| 56 current_map = handle(last->map()); |
| 57 } |
| 58 // Compile the stub that is either shared for all names or |
| 59 // name specific if there are global objects involved. |
| 60 Handle<Code> handler = PropertyHandlerCompiler::Find( |
| 61 cache_name, stub_holder_map, Code::LOAD_IC, flag, Code::FAST); |
| 62 if (!handler.is_null()) return handler; |
| 63 |
| 64 NamedLoadHandlerCompiler compiler(isolate, type, last, flag); |
| 65 handler = compiler.CompileLoadNonexistent(cache_name); |
| 66 Map::UpdateCodeCache(stub_holder_map, cache_name, handler); |
| 67 return handler; |
| 68 } |
| 69 |
| 70 |
| 71 Handle<Code> PropertyHandlerCompiler::GetCode(Code::Kind kind, |
| 72 Code::StubType type, |
| 73 Handle<Name> name) { |
| 74 Code::Flags flags = Code::ComputeHandlerFlags(kind, type, cache_holder()); |
| 75 Handle<Code> code = GetCodeWithFlags(flags, name); |
| 76 PROFILE(isolate(), CodeCreateEvent(Logger::STUB_TAG, *code, *name)); |
| 77 return code; |
| 78 } |
| 79 |
| 80 |
| 81 #define __ ACCESS_MASM(masm()) |
| 82 |
| 83 |
| 84 Register NamedLoadHandlerCompiler::FrontendHeader(Register object_reg, |
| 85 Handle<Name> name, |
| 86 Label* miss) { |
| 87 PrototypeCheckType check_type = CHECK_ALL_MAPS; |
| 88 int function_index = -1; |
| 89 if (type()->Is(HeapType::String())) { |
| 90 function_index = Context::STRING_FUNCTION_INDEX; |
| 91 } else if (type()->Is(HeapType::Symbol())) { |
| 92 function_index = Context::SYMBOL_FUNCTION_INDEX; |
| 93 } else if (type()->Is(HeapType::Number())) { |
| 94 function_index = Context::NUMBER_FUNCTION_INDEX; |
| 95 } else if (type()->Is(HeapType::Boolean())) { |
| 96 function_index = Context::BOOLEAN_FUNCTION_INDEX; |
| 97 } else { |
| 98 check_type = SKIP_RECEIVER; |
| 99 } |
| 100 |
| 101 if (check_type == CHECK_ALL_MAPS) { |
| 102 GenerateDirectLoadGlobalFunctionPrototype(masm(), function_index, |
| 103 scratch1(), miss); |
| 104 Object* function = isolate()->native_context()->get(function_index); |
| 105 Object* prototype = JSFunction::cast(function)->instance_prototype(); |
| 106 set_type_for_object(handle(prototype, isolate())); |
| 107 object_reg = scratch1(); |
| 108 } |
| 109 |
| 110 // Check that the maps starting from the prototype haven't changed. |
| 111 return CheckPrototypes(object_reg, scratch1(), scratch2(), scratch3(), name, |
| 112 miss, check_type); |
| 113 } |
| 114 |
| 115 |
| 116 // Frontend for store uses the name register. It has to be restored before a |
| 117 // miss. |
| 118 Register NamedStoreHandlerCompiler::FrontendHeader(Register object_reg, |
| 119 Handle<Name> name, |
| 120 Label* miss) { |
| 121 return CheckPrototypes(object_reg, this->name(), scratch1(), scratch2(), name, |
| 122 miss, SKIP_RECEIVER); |
| 123 } |
| 124 |
| 125 |
| 126 Register PropertyHandlerCompiler::Frontend(Register object_reg, |
| 127 Handle<Name> name) { |
| 128 Label miss; |
| 129 Register reg = FrontendHeader(object_reg, name, &miss); |
| 130 FrontendFooter(name, &miss); |
| 131 return reg; |
| 132 } |
| 133 |
| 134 |
| 135 void PropertyHandlerCompiler::NonexistentFrontendHeader(Handle<Name> name, |
| 136 Label* miss, |
| 137 Register scratch1, |
| 138 Register scratch2) { |
| 139 Register holder_reg; |
| 140 Handle<Map> last_map; |
| 141 if (holder().is_null()) { |
| 142 holder_reg = receiver(); |
| 143 last_map = IC::TypeToMap(*type(), isolate()); |
| 144 // If |type| has null as its prototype, |holder()| is |
| 145 // Handle<JSObject>::null(). |
| 146 DCHECK(last_map->prototype() == isolate()->heap()->null_value()); |
| 147 } else { |
| 148 holder_reg = FrontendHeader(receiver(), name, miss); |
| 149 last_map = handle(holder()->map()); |
| 150 } |
| 151 |
| 152 if (last_map->is_dictionary_map()) { |
| 153 if (last_map->IsJSGlobalObjectMap()) { |
| 154 Handle<JSGlobalObject> global = |
| 155 holder().is_null() |
| 156 ? Handle<JSGlobalObject>::cast(type()->AsConstant()->Value()) |
| 157 : Handle<JSGlobalObject>::cast(holder()); |
| 158 GenerateCheckPropertyCell(masm(), global, name, scratch1, miss); |
| 159 } else { |
| 160 if (!name->IsUniqueName()) { |
| 161 DCHECK(name->IsString()); |
| 162 name = factory()->InternalizeString(Handle<String>::cast(name)); |
| 163 } |
| 164 DCHECK(holder().is_null() || |
| 165 holder()->property_dictionary()->FindEntry(name) == |
| 166 NameDictionary::kNotFound); |
| 167 GenerateDictionaryNegativeLookup(masm(), miss, holder_reg, name, scratch1, |
| 168 scratch2); |
| 169 } |
| 170 } |
| 171 } |
| 172 |
| 173 |
| 174 Handle<Code> NamedLoadHandlerCompiler::CompileLoadField(Handle<Name> name, |
| 175 FieldIndex field) { |
| 176 Register reg = Frontend(receiver(), name); |
| 177 __ Move(receiver(), reg); |
| 178 LoadFieldStub stub(isolate(), field); |
| 179 GenerateTailCall(masm(), stub.GetCode()); |
| 180 return GetCode(kind(), Code::FAST, name); |
| 181 } |
| 182 |
| 183 |
| 184 Handle<Code> NamedLoadHandlerCompiler::CompileLoadConstant(Handle<Name> name, |
| 185 int constant_index) { |
| 186 Register reg = Frontend(receiver(), name); |
| 187 __ Move(receiver(), reg); |
| 188 LoadConstantStub stub(isolate(), constant_index); |
| 189 GenerateTailCall(masm(), stub.GetCode()); |
| 190 return GetCode(kind(), Code::FAST, name); |
| 191 } |
| 192 |
| 193 |
| 194 Handle<Code> NamedLoadHandlerCompiler::CompileLoadNonexistent( |
| 195 Handle<Name> name) { |
| 196 Label miss; |
| 197 NonexistentFrontendHeader(name, &miss, scratch2(), scratch3()); |
| 198 GenerateLoadConstant(isolate()->factory()->undefined_value()); |
| 199 FrontendFooter(name, &miss); |
| 200 return GetCode(kind(), Code::FAST, name); |
| 201 } |
| 202 |
| 203 |
| 204 Handle<Code> NamedLoadHandlerCompiler::CompileLoadCallback( |
| 205 Handle<Name> name, Handle<ExecutableAccessorInfo> callback) { |
| 206 Register reg = Frontend(receiver(), name); |
| 207 GenerateLoadCallback(reg, callback); |
| 208 return GetCode(kind(), Code::FAST, name); |
| 209 } |
| 210 |
| 211 |
| 212 Handle<Code> NamedLoadHandlerCompiler::CompileLoadCallback( |
| 213 Handle<Name> name, const CallOptimization& call_optimization) { |
| 214 DCHECK(call_optimization.is_simple_api_call()); |
| 215 Frontend(receiver(), name); |
| 216 Handle<Map> receiver_map = IC::TypeToMap(*type(), isolate()); |
| 217 GenerateFastApiCall(masm(), call_optimization, receiver_map, receiver(), |
| 218 scratch1(), false, 0, NULL); |
| 219 return GetCode(kind(), Code::FAST, name); |
| 220 } |
| 221 |
| 222 |
| 223 Handle<Code> NamedLoadHandlerCompiler::CompileLoadInterceptor( |
| 224 LookupIterator* it) { |
| 225 // So far the most popular follow ups for interceptor loads are FIELD and |
| 226 // ExecutableAccessorInfo, so inline only them. Other cases may be added |
| 227 // later. |
| 228 bool inline_followup = it->state() == LookupIterator::PROPERTY; |
| 229 if (inline_followup) { |
| 230 switch (it->property_kind()) { |
| 231 case LookupIterator::DATA: |
| 232 inline_followup = it->property_details().type() == FIELD; |
| 233 break; |
| 234 case LookupIterator::ACCESSOR: { |
| 235 Handle<Object> accessors = it->GetAccessors(); |
| 236 inline_followup = accessors->IsExecutableAccessorInfo(); |
| 237 if (!inline_followup) break; |
| 238 Handle<ExecutableAccessorInfo> info = |
| 239 Handle<ExecutableAccessorInfo>::cast(accessors); |
| 240 inline_followup = info->getter() != NULL && |
| 241 ExecutableAccessorInfo::IsCompatibleReceiverType( |
| 242 isolate(), info, type()); |
| 243 } |
| 244 } |
| 245 } |
| 246 |
| 247 Register reg = Frontend(receiver(), it->name()); |
| 248 if (inline_followup) { |
| 249 // TODO(368): Compile in the whole chain: all the interceptors in |
| 250 // prototypes and ultimate answer. |
| 251 GenerateLoadInterceptorWithFollowup(it, reg); |
| 252 } else { |
| 253 GenerateLoadInterceptor(reg); |
| 254 } |
| 255 return GetCode(kind(), Code::FAST, it->name()); |
| 256 } |
| 257 |
| 258 |
| 259 void NamedLoadHandlerCompiler::GenerateLoadPostInterceptor( |
| 260 LookupIterator* it, Register interceptor_reg) { |
| 261 Handle<JSObject> real_named_property_holder(it->GetHolder<JSObject>()); |
| 262 |
| 263 set_type_for_object(holder()); |
| 264 set_holder(real_named_property_holder); |
| 265 Register reg = Frontend(interceptor_reg, it->name()); |
| 266 |
| 267 switch (it->property_kind()) { |
| 268 case LookupIterator::DATA: { |
| 269 DCHECK_EQ(FIELD, it->property_details().type()); |
| 270 __ Move(receiver(), reg); |
| 271 LoadFieldStub stub(isolate(), it->GetFieldIndex()); |
| 272 GenerateTailCall(masm(), stub.GetCode()); |
| 273 break; |
| 274 } |
| 275 case LookupIterator::ACCESSOR: |
| 276 Handle<ExecutableAccessorInfo> info = |
| 277 Handle<ExecutableAccessorInfo>::cast(it->GetAccessors()); |
| 278 DCHECK_NE(NULL, info->getter()); |
| 279 GenerateLoadCallback(reg, info); |
| 280 } |
| 281 } |
| 282 |
| 283 |
| 284 Handle<Code> NamedLoadHandlerCompiler::CompileLoadViaGetter( |
| 285 Handle<Name> name, Handle<JSFunction> getter) { |
| 286 Frontend(receiver(), name); |
| 287 GenerateLoadViaGetter(masm(), type(), receiver(), getter); |
| 288 return GetCode(kind(), Code::FAST, name); |
| 289 } |
| 290 |
| 291 |
| 292 // TODO(verwaest): Cleanup. holder() is actually the receiver. |
| 293 Handle<Code> NamedStoreHandlerCompiler::CompileStoreTransition( |
| 294 Handle<Map> transition, Handle<Name> name) { |
| 295 Label miss, slow; |
| 296 |
| 297 // Ensure no transitions to deprecated maps are followed. |
| 298 __ CheckMapDeprecated(transition, scratch1(), &miss); |
| 299 |
| 300 // Check that we are allowed to write this. |
| 301 bool is_nonexistent = holder()->map() == transition->GetBackPointer(); |
| 302 if (is_nonexistent) { |
| 303 // Find the top object. |
| 304 Handle<JSObject> last; |
| 305 PrototypeIterator iter(isolate(), holder()); |
| 306 while (!iter.IsAtEnd()) { |
| 307 last = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)); |
| 308 iter.Advance(); |
| 309 } |
| 310 if (!last.is_null()) set_holder(last); |
| 311 NonexistentFrontendHeader(name, &miss, scratch1(), scratch2()); |
| 312 } else { |
| 313 FrontendHeader(receiver(), name, &miss); |
| 314 DCHECK(holder()->HasFastProperties()); |
| 315 } |
| 316 |
| 317 GenerateStoreTransition(transition, name, receiver(), this->name(), value(), |
| 318 scratch1(), scratch2(), scratch3(), &miss, &slow); |
| 319 |
| 320 GenerateRestoreName(&miss, name); |
| 321 TailCallBuiltin(masm(), MissBuiltin(kind())); |
| 322 |
| 323 GenerateRestoreName(&slow, name); |
| 324 TailCallBuiltin(masm(), SlowBuiltin(kind())); |
| 325 return GetCode(kind(), Code::FAST, name); |
| 326 } |
| 327 |
| 328 |
| 329 Handle<Code> NamedStoreHandlerCompiler::CompileStoreField(LookupIterator* it) { |
| 330 Label miss; |
| 331 GenerateStoreField(it, value(), &miss); |
| 332 __ bind(&miss); |
| 333 TailCallBuiltin(masm(), MissBuiltin(kind())); |
| 334 return GetCode(kind(), Code::FAST, it->name()); |
| 335 } |
| 336 |
| 337 |
| 338 Handle<Code> NamedStoreHandlerCompiler::CompileStoreViaSetter( |
| 339 Handle<JSObject> object, Handle<Name> name, Handle<JSFunction> setter) { |
| 340 Frontend(receiver(), name); |
| 341 GenerateStoreViaSetter(masm(), type(), receiver(), setter); |
| 342 |
| 343 return GetCode(kind(), Code::FAST, name); |
| 344 } |
| 345 |
| 346 |
| 347 Handle<Code> NamedStoreHandlerCompiler::CompileStoreCallback( |
| 348 Handle<JSObject> object, Handle<Name> name, |
| 349 const CallOptimization& call_optimization) { |
| 350 Frontend(receiver(), name); |
| 351 Register values[] = {value()}; |
| 352 GenerateFastApiCall(masm(), call_optimization, handle(object->map()), |
| 353 receiver(), scratch1(), true, 1, values); |
| 354 return GetCode(kind(), Code::FAST, name); |
| 355 } |
| 356 |
| 357 |
| 358 #undef __ |
| 359 |
| 360 |
| 361 void ElementHandlerCompiler::CompileElementHandlers( |
| 362 MapHandleList* receiver_maps, CodeHandleList* handlers) { |
| 363 for (int i = 0; i < receiver_maps->length(); ++i) { |
| 364 Handle<Map> receiver_map = receiver_maps->at(i); |
| 365 Handle<Code> cached_stub; |
| 366 |
| 367 if ((receiver_map->instance_type() & kNotStringTag) == 0) { |
| 368 cached_stub = isolate()->builtins()->KeyedLoadIC_String(); |
| 369 } else if (receiver_map->instance_type() < FIRST_JS_RECEIVER_TYPE) { |
| 370 cached_stub = isolate()->builtins()->KeyedLoadIC_Slow(); |
| 371 } else { |
| 372 bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; |
| 373 ElementsKind elements_kind = receiver_map->elements_kind(); |
| 374 |
| 375 if (IsFastElementsKind(elements_kind) || |
| 376 IsExternalArrayElementsKind(elements_kind) || |
| 377 IsFixedTypedArrayElementsKind(elements_kind)) { |
| 378 cached_stub = LoadFastElementStub(isolate(), is_js_array, elements_kind) |
| 379 .GetCode(); |
| 380 } else if (elements_kind == SLOPPY_ARGUMENTS_ELEMENTS) { |
| 381 cached_stub = isolate()->builtins()->KeyedLoadIC_SloppyArguments(); |
| 382 } else { |
| 383 DCHECK(elements_kind == DICTIONARY_ELEMENTS); |
| 384 cached_stub = LoadDictionaryElementStub(isolate()).GetCode(); |
| 385 } |
| 386 } |
| 387 |
| 388 handlers->Add(cached_stub); |
| 389 } |
| 390 } |
| 391 |
| 392 |
| 393 void ElementHandlerCompiler::GenerateStoreDictionaryElement( |
| 394 MacroAssembler* masm) { |
| 395 KeyedStoreIC::GenerateSlow(masm); |
| 396 } |
| 397 } |
| 398 } // namespace v8::internal |
OLD | NEW |