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.
7 #include "src/cpu-profiler.h"
8 #include "src/ic/call-optimization.h"
9 #include "src/ic/handler-compiler.h"
10 #include "src/ic/ic.h"
11 #include "src/ic/ic-inl.h"
17 Handle<Code> PropertyHandlerCompiler::Find(Handle<Name> name,
18 Handle<Map> stub_holder,
20 CacheHolderFlag cache_holder,
21 Code::StubType type) {
22 Code::Flags flags = Code::ComputeHandlerFlags(kind, type, cache_holder);
23 Object* probe = stub_holder->FindInCodeCache(*name, flags);
24 if (probe->IsCode()) return handle(Code::cast(probe));
25 return Handle<Code>::null();
29 Handle<Code> NamedLoadHandlerCompiler::ComputeLoadNonexistent(
30 Handle<Name> name, Handle<Map> receiver_map) {
31 Isolate* isolate = name->GetIsolate();
32 if (receiver_map->prototype()->IsNull()) {
33 // TODO(jkummerow/verwaest): If there is no prototype and the property
34 // is nonexistent, introduce a builtin to handle this (fast properties
35 // -> return undefined, dictionary properties -> do negative lookup).
36 return Handle<Code>();
39 Handle<Map> stub_holder_map =
40 IC::GetHandlerCacheHolder(receiver_map, false, isolate, &flag);
42 // If no dictionary mode objects are present in the prototype chain, the load
43 // nonexistent IC stub can be shared for all names for a given map and we use
44 // the empty string for the map cache in that case. If there are dictionary
45 // mode objects involved, we need to do negative lookups in the stub and
46 // therefore the stub will be specific to the name.
47 Handle<Name> cache_name =
48 receiver_map->is_dictionary_map()
50 : Handle<Name>::cast(isolate->factory()->nonexistent_symbol());
51 Handle<Map> current_map = stub_holder_map;
52 Handle<JSObject> last(JSObject::cast(receiver_map->prototype()));
54 if (current_map->is_dictionary_map()) cache_name = name;
55 if (current_map->prototype()->IsNull()) break;
56 if (name->IsPrivate()) {
57 // TODO(verwaest): Use nonexistent_private_symbol.
59 JSReceiver* prototype = JSReceiver::cast(current_map->prototype());
60 if (!prototype->map()->is_hidden_prototype() &&
61 !prototype->map()->IsGlobalObjectMap()) {
66 last = handle(JSObject::cast(current_map->prototype()));
67 current_map = handle(last->map());
69 // Compile the stub that is either shared for all names or
70 // name specific if there are global objects involved.
71 Handle<Code> handler = PropertyHandlerCompiler::Find(
72 cache_name, stub_holder_map, Code::LOAD_IC, flag, Code::FAST);
73 if (!handler.is_null()) return handler;
75 NamedLoadHandlerCompiler compiler(isolate, receiver_map, last, flag);
76 handler = compiler.CompileLoadNonexistent(cache_name);
77 Map::UpdateCodeCache(stub_holder_map, cache_name, handler);
82 Handle<Code> PropertyHandlerCompiler::GetCode(Code::Kind kind,
85 Code::Flags flags = Code::ComputeHandlerFlags(kind, type, cache_holder());
86 Handle<Code> code = GetCodeWithFlags(flags, name);
87 PROFILE(isolate(), CodeCreateEvent(Logger::HANDLER_TAG, *code, *name));
89 code->VerifyEmbeddedObjects();
95 #define __ ACCESS_MASM(masm())
98 Register NamedLoadHandlerCompiler::FrontendHeader(Register object_reg,
101 ReturnHolder return_what) {
102 PrototypeCheckType check_type = CHECK_ALL_MAPS;
103 int function_index = -1;
104 if (map()->instance_type() < FIRST_NONSTRING_TYPE) {
105 function_index = Context::STRING_FUNCTION_INDEX;
106 } else if (map()->instance_type() == SYMBOL_TYPE) {
107 function_index = Context::SYMBOL_FUNCTION_INDEX;
108 } else if (map()->instance_type() == HEAP_NUMBER_TYPE) {
109 function_index = Context::NUMBER_FUNCTION_INDEX;
110 } else if (*map() == isolate()->heap()->boolean_map()) {
111 function_index = Context::BOOLEAN_FUNCTION_INDEX;
113 check_type = SKIP_RECEIVER;
116 if (check_type == CHECK_ALL_MAPS) {
117 GenerateDirectLoadGlobalFunctionPrototype(masm(), function_index,
119 Object* function = isolate()->native_context()->get(function_index);
120 Object* prototype = JSFunction::cast(function)->instance_prototype();
121 Handle<Map> map(JSObject::cast(prototype)->map());
123 object_reg = scratch1();
126 // Check that the maps starting from the prototype haven't changed.
127 return CheckPrototypes(object_reg, scratch1(), scratch2(), scratch3(), name,
128 miss, check_type, return_what);
132 // Frontend for store uses the name register. It has to be restored before a
134 Register NamedStoreHandlerCompiler::FrontendHeader(Register object_reg,
137 ReturnHolder return_what) {
138 return CheckPrototypes(object_reg, this->name(), scratch1(), scratch2(), name,
139 miss, SKIP_RECEIVER, return_what);
143 Register PropertyHandlerCompiler::Frontend(Handle<Name> name) {
145 if (IC::ICUseVector(kind())) {
148 Register reg = FrontendHeader(receiver(), name, &miss, RETURN_HOLDER);
149 FrontendFooter(name, &miss);
150 // The footer consumes the vector and slot from the stack if miss occurs.
151 if (IC::ICUseVector(kind())) {
152 DiscardVectorAndSlot();
158 void PropertyHandlerCompiler::NonexistentFrontendHeader(Handle<Name> name,
163 Handle<Map> last_map;
164 if (holder().is_null()) {
165 holder_reg = receiver();
167 // If |type| has null as its prototype, |holder()| is
168 // Handle<JSObject>::null().
169 DCHECK(last_map->prototype() == isolate()->heap()->null_value());
171 last_map = handle(holder()->map());
172 // This condition matches the branches below.
174 last_map->is_dictionary_map() && !last_map->IsJSGlobalObjectMap();
176 FrontendHeader(receiver(), name, miss,
177 need_holder ? RETURN_HOLDER : DONT_RETURN_ANYTHING);
180 if (last_map->is_dictionary_map()) {
181 if (last_map->IsJSGlobalObjectMap()) {
182 Handle<JSGlobalObject> global =
184 ? Handle<JSGlobalObject>::cast(isolate()->global_object())
185 : Handle<JSGlobalObject>::cast(holder());
186 GenerateCheckPropertyCell(masm(), global, name, scratch1, miss);
188 if (!name->IsUniqueName()) {
189 DCHECK(name->IsString());
190 name = factory()->InternalizeString(Handle<String>::cast(name));
192 DCHECK(holder().is_null() ||
193 holder()->property_dictionary()->FindEntry(name) ==
194 NameDictionary::kNotFound);
195 GenerateDictionaryNegativeLookup(masm(), miss, holder_reg, name, scratch1,
202 Handle<Code> NamedLoadHandlerCompiler::CompileLoadField(Handle<Name> name,
204 Register reg = Frontend(name);
205 __ Move(receiver(), reg);
206 LoadFieldStub stub(isolate(), field);
207 GenerateTailCall(masm(), stub.GetCode());
208 return GetCode(kind(), Code::FAST, name);
212 Handle<Code> NamedLoadHandlerCompiler::CompileLoadConstant(Handle<Name> name,
213 int constant_index) {
214 Register reg = Frontend(name);
215 __ Move(receiver(), reg);
216 LoadConstantStub stub(isolate(), constant_index);
217 GenerateTailCall(masm(), stub.GetCode());
218 return GetCode(kind(), Code::FAST, name);
222 Handle<Code> NamedLoadHandlerCompiler::CompileLoadNonexistent(
225 if (IC::ICUseVector(kind())) {
226 DCHECK(kind() == Code::LOAD_IC);
229 NonexistentFrontendHeader(name, &miss, scratch2(), scratch3());
230 if (IC::ICUseVector(kind())) {
231 DiscardVectorAndSlot();
233 GenerateLoadConstant(isolate()->factory()->undefined_value());
234 FrontendFooter(name, &miss);
235 return GetCode(kind(), Code::FAST, name);
239 Handle<Code> NamedLoadHandlerCompiler::CompileLoadCallback(
240 Handle<Name> name, Handle<ExecutableAccessorInfo> callback) {
241 Register reg = Frontend(name);
242 GenerateLoadCallback(reg, callback);
243 return GetCode(kind(), Code::FAST, name);
247 Handle<Code> NamedLoadHandlerCompiler::CompileLoadCallback(
248 Handle<Name> name, const CallOptimization& call_optimization,
249 int accessor_index) {
250 DCHECK(call_optimization.is_simple_api_call());
251 Register holder = Frontend(name);
252 GenerateApiAccessorCall(masm(), call_optimization, map(), receiver(),
253 scratch2(), false, no_reg, holder, accessor_index);
254 return GetCode(kind(), Code::FAST, name);
258 void NamedLoadHandlerCompiler::InterceptorVectorSlotPush(Register holder_reg) {
259 if (IC::ICUseVector(kind())) {
260 if (holder_reg.is(receiver())) {
263 DCHECK(holder_reg.is(scratch1()));
264 PushVectorAndSlot(scratch2(), scratch3());
270 void NamedLoadHandlerCompiler::InterceptorVectorSlotPop(Register holder_reg,
272 if (IC::ICUseVector(kind())) {
273 if (mode == DISCARD) {
274 DiscardVectorAndSlot();
276 if (holder_reg.is(receiver())) {
279 DCHECK(holder_reg.is(scratch1()));
280 PopVectorAndSlot(scratch2(), scratch3());
287 Handle<Code> NamedLoadHandlerCompiler::CompileLoadInterceptor(
288 LookupIterator* it) {
289 // So far the most popular follow ups for interceptor loads are DATA and
290 // ExecutableAccessorInfo, so inline only them. Other cases may be added
292 bool inline_followup = false;
293 switch (it->state()) {
294 case LookupIterator::TRANSITION:
296 case LookupIterator::ACCESS_CHECK:
297 case LookupIterator::INTERCEPTOR:
298 case LookupIterator::JSPROXY:
299 case LookupIterator::NOT_FOUND:
300 case LookupIterator::INTEGER_INDEXED_EXOTIC:
302 case LookupIterator::DATA:
304 it->property_details().type() == DATA && !it->is_dictionary_holder();
306 case LookupIterator::ACCESSOR: {
307 Handle<Object> accessors = it->GetAccessors();
308 if (accessors->IsExecutableAccessorInfo()) {
309 Handle<ExecutableAccessorInfo> info =
310 Handle<ExecutableAccessorInfo>::cast(accessors);
311 inline_followup = info->getter() != NULL &&
312 ExecutableAccessorInfo::IsCompatibleReceiverMap(
313 isolate(), info, map());
314 } else if (accessors->IsAccessorPair()) {
315 Handle<JSObject> property_holder(it->GetHolder<JSObject>());
316 Handle<Object> getter(Handle<AccessorPair>::cast(accessors)->getter(),
318 if (!getter->IsJSFunction()) break;
319 if (!property_holder->HasFastProperties()) break;
320 auto function = Handle<JSFunction>::cast(getter);
321 CallOptimization call_optimization(function);
322 Handle<Map> receiver_map = map();
323 inline_followup = call_optimization.is_simple_api_call() &&
324 call_optimization.IsCompatibleReceiverMap(
325 receiver_map, property_holder);
331 InterceptorVectorSlotPush(receiver());
332 bool lost_holder_register = false;
333 auto holder_orig = holder();
334 // non masking interceptors must check the entire chain, so temporarily reset
335 // the holder to be that last element for the FrontendHeader call.
336 if (holder()->GetNamedInterceptor()->non_masking()) {
337 DCHECK(!inline_followup);
338 JSObject* last = *holder();
339 PrototypeIterator iter(isolate(), last);
340 while (!iter.IsAtEnd()) {
341 lost_holder_register = true;
342 last = JSObject::cast(iter.GetCurrent());
345 auto last_handle = handle(last);
346 set_holder(last_handle);
348 Register reg = FrontendHeader(receiver(), it->name(), &miss, RETURN_HOLDER);
349 // Reset the holder so further calculations are correct.
350 set_holder(holder_orig);
351 if (lost_holder_register) {
352 if (*it->GetReceiver() == *holder()) {
355 // Reload lost holder register.
356 auto cell = isolate()->factory()->NewWeakCell(holder());
357 __ LoadWeakValue(reg, cell, &miss);
360 FrontendFooter(it->name(), &miss);
361 InterceptorVectorSlotPop(reg);
362 if (inline_followup) {
363 // TODO(368): Compile in the whole chain: all the interceptors in
364 // prototypes and ultimate answer.
365 GenerateLoadInterceptorWithFollowup(it, reg);
367 GenerateLoadInterceptor(reg);
369 return GetCode(kind(), Code::FAST, it->name());
373 void NamedLoadHandlerCompiler::GenerateLoadPostInterceptor(
374 LookupIterator* it, Register interceptor_reg) {
375 Handle<JSObject> real_named_property_holder(it->GetHolder<JSObject>());
377 Handle<Map> holder_map(holder()->map());
379 set_holder(real_named_property_holder);
382 InterceptorVectorSlotPush(interceptor_reg);
384 FrontendHeader(interceptor_reg, it->name(), &miss, RETURN_HOLDER);
385 FrontendFooter(it->name(), &miss);
386 // We discard the vector and slot now because we don't miss below this point.
387 InterceptorVectorSlotPop(reg, DISCARD);
389 switch (it->state()) {
390 case LookupIterator::ACCESS_CHECK:
391 case LookupIterator::INTERCEPTOR:
392 case LookupIterator::JSPROXY:
393 case LookupIterator::NOT_FOUND:
394 case LookupIterator::INTEGER_INDEXED_EXOTIC:
395 case LookupIterator::TRANSITION:
397 case LookupIterator::DATA: {
398 DCHECK_EQ(DATA, it->property_details().type());
399 __ Move(receiver(), reg);
400 LoadFieldStub stub(isolate(), it->GetFieldIndex());
401 GenerateTailCall(masm(), stub.GetCode());
404 case LookupIterator::ACCESSOR:
405 if (it->GetAccessors()->IsExecutableAccessorInfo()) {
406 Handle<ExecutableAccessorInfo> info =
407 Handle<ExecutableAccessorInfo>::cast(it->GetAccessors());
408 DCHECK_NOT_NULL(info->getter());
409 GenerateLoadCallback(reg, info);
411 auto function = handle(JSFunction::cast(
412 AccessorPair::cast(*it->GetAccessors())->getter()));
413 CallOptimization call_optimization(function);
414 GenerateApiAccessorCall(masm(), call_optimization, holder_map,
415 receiver(), scratch2(), false, no_reg, reg,
416 it->GetAccessorIndex());
422 Handle<Code> NamedLoadHandlerCompiler::CompileLoadViaGetter(
423 Handle<Name> name, int accessor_index, int expected_arguments) {
424 Register holder = Frontend(name);
425 GenerateLoadViaGetter(masm(), map(), receiver(), holder, accessor_index,
426 expected_arguments, scratch2());
427 return GetCode(kind(), Code::FAST, name);
431 // TODO(verwaest): Cleanup. holder() is actually the receiver.
432 Handle<Code> NamedStoreHandlerCompiler::CompileStoreTransition(
433 Handle<Map> transition, Handle<Name> name) {
436 // Check that we are allowed to write this.
437 bool is_nonexistent = holder()->map() == transition->GetBackPointer();
438 if (is_nonexistent) {
439 // Find the top object.
440 Handle<JSObject> last;
441 PrototypeIterator::WhereToEnd end =
442 name->IsPrivate() ? PrototypeIterator::END_AT_NON_HIDDEN
443 : PrototypeIterator::END_AT_NULL;
444 PrototypeIterator iter(isolate(), holder());
445 while (!iter.IsAtEnd(end)) {
446 last = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
449 if (!last.is_null()) set_holder(last);
450 NonexistentFrontendHeader(name, &miss, scratch1(), scratch2());
452 FrontendHeader(receiver(), name, &miss, DONT_RETURN_ANYTHING);
453 DCHECK(holder()->HasFastProperties());
456 int descriptor = transition->LastAdded();
457 Handle<DescriptorArray> descriptors(transition->instance_descriptors());
458 PropertyDetails details = descriptors->GetDetails(descriptor);
459 Representation representation = details.representation();
460 DCHECK(!representation.IsNone());
462 // Stub is never generated for objects that require access checks.
463 DCHECK(!transition->is_access_check_needed());
465 // Call to respective StoreTransitionStub.
466 if (details.type() == DATA_CONSTANT) {
467 GenerateRestoreMap(transition, scratch2(), &miss);
468 DCHECK(descriptors->GetValue(descriptor)->IsJSFunction());
469 Register map_reg = StoreTransitionDescriptor::MapRegister();
470 GenerateConstantCheck(map_reg, descriptor, value(), scratch2(), &miss);
471 GenerateRestoreName(name);
472 StoreTransitionStub stub(isolate());
473 GenerateTailCall(masm(), stub.GetCode());
476 if (representation.IsHeapObject()) {
477 GenerateFieldTypeChecks(descriptors->GetFieldType(descriptor), value(),
480 StoreTransitionStub::StoreMode store_mode =
481 Map::cast(transition->GetBackPointer())->unused_property_fields() == 0
482 ? StoreTransitionStub::ExtendStorageAndStoreMapAndValue
483 : StoreTransitionStub::StoreMapAndValue;
485 GenerateRestoreMap(transition, scratch2(), &miss);
486 GenerateRestoreName(name);
487 StoreTransitionStub stub(isolate(),
488 FieldIndex::ForDescriptor(*transition, descriptor),
489 representation, store_mode);
490 GenerateTailCall(masm(), stub.GetCode());
493 GenerateRestoreName(&miss, name);
494 TailCallBuiltin(masm(), MissBuiltin(kind()));
496 return GetCode(kind(), Code::FAST, name);
500 Handle<Code> NamedStoreHandlerCompiler::CompileStoreField(LookupIterator* it) {
502 DCHECK(it->representation().IsHeapObject());
504 GenerateFieldTypeChecks(*it->GetFieldType(), value(), &miss);
505 StoreFieldStub stub(isolate(), it->GetFieldIndex(), it->representation());
506 GenerateTailCall(masm(), stub.GetCode());
509 TailCallBuiltin(masm(), MissBuiltin(kind()));
510 return GetCode(kind(), Code::FAST, it->name());
514 Handle<Code> NamedStoreHandlerCompiler::CompileStoreViaSetter(
515 Handle<JSObject> object, Handle<Name> name, int accessor_index,
516 int expected_arguments) {
517 Register holder = Frontend(name);
518 GenerateStoreViaSetter(masm(), map(), receiver(), holder, accessor_index,
519 expected_arguments, scratch2());
521 return GetCode(kind(), Code::FAST, name);
525 Handle<Code> NamedStoreHandlerCompiler::CompileStoreCallback(
526 Handle<JSObject> object, Handle<Name> name,
527 const CallOptimization& call_optimization, int accessor_index) {
528 Register holder = Frontend(name);
529 GenerateApiAccessorCall(masm(), call_optimization, handle(object->map()),
530 receiver(), scratch2(), true, value(), holder,
532 return GetCode(kind(), Code::FAST, name);
539 void ElementHandlerCompiler::CompileElementHandlers(
540 MapHandleList* receiver_maps, CodeHandleList* handlers,
541 LanguageMode language_mode) {
542 for (int i = 0; i < receiver_maps->length(); ++i) {
543 Handle<Map> receiver_map = receiver_maps->at(i);
544 Handle<Code> cached_stub;
546 if (receiver_map->IsStringMap()) {
547 cached_stub = LoadIndexedStringStub(isolate()).GetCode();
548 } else if (receiver_map->instance_type() < FIRST_JS_RECEIVER_TYPE) {
549 cached_stub = is_strong(language_mode)
550 ? isolate()->builtins()->KeyedLoadIC_Slow_Strong()
551 : isolate()->builtins()->KeyedLoadIC_Slow();
553 bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE;
554 ElementsKind elements_kind = receiver_map->elements_kind();
556 // No need to check for an elements-free prototype chain here, the
557 // generated stub code needs to check that dynamically anyway.
558 bool convert_hole_to_undefined =
559 (is_js_array && elements_kind == FAST_HOLEY_ELEMENTS &&
561 isolate()->get_initial_js_array_map(elements_kind)) &&
562 !is_strong(language_mode);
564 if (receiver_map->has_indexed_interceptor()) {
565 cached_stub = LoadIndexedInterceptorStub(isolate()).GetCode();
566 } else if (IsSloppyArgumentsElements(elements_kind)) {
567 cached_stub = KeyedLoadSloppyArgumentsStub(isolate()).GetCode();
568 } else if (IsFastElementsKind(elements_kind) ||
569 IsExternalArrayElementsKind(elements_kind) ||
570 IsFixedTypedArrayElementsKind(elements_kind)) {
571 cached_stub = LoadFastElementStub(isolate(), is_js_array, elements_kind,
572 convert_hole_to_undefined).GetCode();
574 DCHECK(elements_kind == DICTIONARY_ELEMENTS);
576 LoadICState(is_strong(language_mode) ? LoadICState::kStrongModeState
578 cached_stub = LoadDictionaryElementStub(isolate(), state).GetCode();
582 handlers->Add(cached_stub);
585 } // namespace internal