Revert of Expose SIMD.Float32x4 type to Javascript. (patchset #14 id:450001 of https...
[platform/upstream/v8.git] / src / ic / handler-compiler.cc
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/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"
12
13 namespace v8 {
14 namespace internal {
15
16
17 Handle<Code> PropertyHandlerCompiler::Find(Handle<Name> name,
18                                            Handle<Map> stub_holder,
19                                            Code::Kind kind,
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();
26 }
27
28
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>();
37   }
38   CacheHolderFlag flag;
39   Handle<Map> stub_holder_map =
40       IC::GetHandlerCacheHolder(receiver_map, false, isolate, &flag);
41
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()
49           ? name
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()));
53   while (true) {
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.
58       cache_name = name;
59       JSReceiver* prototype = JSReceiver::cast(current_map->prototype());
60       if (!prototype->map()->is_hidden_prototype() &&
61           !prototype->map()->IsGlobalObjectMap()) {
62         break;
63       }
64     }
65
66     last = handle(JSObject::cast(current_map->prototype()));
67     current_map = handle(last->map());
68   }
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;
74
75   NamedLoadHandlerCompiler compiler(isolate, receiver_map, last, flag);
76   handler = compiler.CompileLoadNonexistent(cache_name);
77   Map::UpdateCodeCache(stub_holder_map, cache_name, handler);
78   return handler;
79 }
80
81
82 Handle<Code> PropertyHandlerCompiler::GetCode(Code::Kind kind,
83                                               Code::StubType type,
84                                               Handle<Name> name) {
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));
88 #ifdef DEBUG
89   code->VerifyEmbeddedObjects();
90 #endif
91   return code;
92 }
93
94
95 #define __ ACCESS_MASM(masm())
96
97
98 Register NamedLoadHandlerCompiler::FrontendHeader(Register object_reg,
99                                                   Handle<Name> name,
100                                                   Label* miss,
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;
112   } else {
113     check_type = SKIP_RECEIVER;
114   }
115
116   if (check_type == CHECK_ALL_MAPS) {
117     GenerateDirectLoadGlobalFunctionPrototype(masm(), function_index,
118                                               scratch1(), miss);
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());
122     set_map(map);
123     object_reg = scratch1();
124   }
125
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);
129 }
130
131
132 // Frontend for store uses the name register. It has to be restored before a
133 // miss.
134 Register NamedStoreHandlerCompiler::FrontendHeader(Register object_reg,
135                                                    Handle<Name> name,
136                                                    Label* miss,
137                                                    ReturnHolder return_what) {
138   return CheckPrototypes(object_reg, this->name(), scratch1(), scratch2(), name,
139                          miss, SKIP_RECEIVER, return_what);
140 }
141
142
143 Register PropertyHandlerCompiler::Frontend(Handle<Name> name) {
144   Label miss;
145   if (IC::ICUseVector(kind())) {
146     PushVectorAndSlot();
147   }
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();
153   }
154   return reg;
155 }
156
157
158 void PropertyHandlerCompiler::NonexistentFrontendHeader(Handle<Name> name,
159                                                         Label* miss,
160                                                         Register scratch1,
161                                                         Register scratch2) {
162   Register holder_reg;
163   Handle<Map> last_map;
164   if (holder().is_null()) {
165     holder_reg = receiver();
166     last_map = map();
167     // If |type| has null as its prototype, |holder()| is
168     // Handle<JSObject>::null().
169     DCHECK(last_map->prototype() == isolate()->heap()->null_value());
170   } else {
171     last_map = handle(holder()->map());
172     // This condition matches the branches below.
173     bool need_holder =
174         last_map->is_dictionary_map() && !last_map->IsJSGlobalObjectMap();
175     holder_reg =
176         FrontendHeader(receiver(), name, miss,
177                        need_holder ? RETURN_HOLDER : DONT_RETURN_ANYTHING);
178   }
179
180   if (last_map->is_dictionary_map()) {
181     if (last_map->IsJSGlobalObjectMap()) {
182       Handle<JSGlobalObject> global =
183           holder().is_null()
184               ? Handle<JSGlobalObject>::cast(isolate()->global_object())
185               : Handle<JSGlobalObject>::cast(holder());
186       GenerateCheckPropertyCell(masm(), global, name, scratch1, miss);
187     } else {
188       if (!name->IsUniqueName()) {
189         DCHECK(name->IsString());
190         name = factory()->InternalizeString(Handle<String>::cast(name));
191       }
192       DCHECK(holder().is_null() ||
193              holder()->property_dictionary()->FindEntry(name) ==
194                  NameDictionary::kNotFound);
195       GenerateDictionaryNegativeLookup(masm(), miss, holder_reg, name, scratch1,
196                                        scratch2);
197     }
198   }
199 }
200
201
202 Handle<Code> NamedLoadHandlerCompiler::CompileLoadField(Handle<Name> name,
203                                                         FieldIndex field) {
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);
209 }
210
211
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);
219 }
220
221
222 Handle<Code> NamedLoadHandlerCompiler::CompileLoadNonexistent(
223     Handle<Name> name) {
224   Label miss;
225   if (IC::ICUseVector(kind())) {
226     DCHECK(kind() == Code::LOAD_IC);
227     PushVectorAndSlot();
228   }
229   NonexistentFrontendHeader(name, &miss, scratch2(), scratch3());
230   if (IC::ICUseVector(kind())) {
231     DiscardVectorAndSlot();
232   }
233   GenerateLoadConstant(isolate()->factory()->undefined_value());
234   FrontendFooter(name, &miss);
235   return GetCode(kind(), Code::FAST, name);
236 }
237
238
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);
244 }
245
246
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);
255 }
256
257
258 void NamedLoadHandlerCompiler::InterceptorVectorSlotPush(Register holder_reg) {
259   if (IC::ICUseVector(kind())) {
260     if (holder_reg.is(receiver())) {
261       PushVectorAndSlot();
262     } else {
263       DCHECK(holder_reg.is(scratch1()));
264       PushVectorAndSlot(scratch2(), scratch3());
265     }
266   }
267 }
268
269
270 void NamedLoadHandlerCompiler::InterceptorVectorSlotPop(Register holder_reg,
271                                                         PopMode mode) {
272   if (IC::ICUseVector(kind())) {
273     if (mode == DISCARD) {
274       DiscardVectorAndSlot();
275     } else {
276       if (holder_reg.is(receiver())) {
277         PopVectorAndSlot();
278       } else {
279         DCHECK(holder_reg.is(scratch1()));
280         PopVectorAndSlot(scratch2(), scratch3());
281       }
282     }
283   }
284 }
285
286
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
291   // later.
292   bool inline_followup = false;
293   switch (it->state()) {
294     case LookupIterator::TRANSITION:
295       UNREACHABLE();
296     case LookupIterator::ACCESS_CHECK:
297     case LookupIterator::INTERCEPTOR:
298     case LookupIterator::JSPROXY:
299     case LookupIterator::NOT_FOUND:
300     case LookupIterator::INTEGER_INDEXED_EXOTIC:
301       break;
302     case LookupIterator::DATA:
303       inline_followup =
304           it->property_details().type() == DATA && !it->is_dictionary_holder();
305       break;
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(),
317                               isolate());
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);
326       }
327     }
328   }
329
330   Label miss;
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());
343       iter.Advance();
344     }
345     auto last_handle = handle(last);
346     set_holder(last_handle);
347   }
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()) {
353       reg = receiver();
354     } else {
355       // Reload lost holder register.
356       auto cell = isolate()->factory()->NewWeakCell(holder());
357       __ LoadWeakValue(reg, cell, &miss);
358     }
359   }
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);
366   } else {
367     GenerateLoadInterceptor(reg);
368   }
369   return GetCode(kind(), Code::FAST, it->name());
370 }
371
372
373 void NamedLoadHandlerCompiler::GenerateLoadPostInterceptor(
374     LookupIterator* it, Register interceptor_reg) {
375   Handle<JSObject> real_named_property_holder(it->GetHolder<JSObject>());
376
377   Handle<Map> holder_map(holder()->map());
378   set_map(holder_map);
379   set_holder(real_named_property_holder);
380
381   Label miss;
382   InterceptorVectorSlotPush(interceptor_reg);
383   Register 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);
388
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:
396       UNREACHABLE();
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());
402       break;
403     }
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);
410       } else {
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());
417       }
418   }
419 }
420
421
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);
428 }
429
430
431 // TODO(verwaest): Cleanup. holder() is actually the receiver.
432 Handle<Code> NamedStoreHandlerCompiler::CompileStoreTransition(
433     Handle<Map> transition, Handle<Name> name) {
434   Label miss;
435
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));
447       iter.Advance();
448     }
449     if (!last.is_null()) set_holder(last);
450     NonexistentFrontendHeader(name, &miss, scratch1(), scratch2());
451   } else {
452     FrontendHeader(receiver(), name, &miss, DONT_RETURN_ANYTHING);
453     DCHECK(holder()->HasFastProperties());
454   }
455
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());
461
462   // Stub is never generated for objects that require access checks.
463   DCHECK(!transition->is_access_check_needed());
464
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());
474
475   } else {
476     if (representation.IsHeapObject()) {
477       GenerateFieldTypeChecks(descriptors->GetFieldType(descriptor), value(),
478                               &miss);
479     }
480     StoreTransitionStub::StoreMode store_mode =
481         Map::cast(transition->GetBackPointer())->unused_property_fields() == 0
482             ? StoreTransitionStub::ExtendStorageAndStoreMapAndValue
483             : StoreTransitionStub::StoreMapAndValue;
484
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());
491   }
492
493   GenerateRestoreName(&miss, name);
494   TailCallBuiltin(masm(), MissBuiltin(kind()));
495
496   return GetCode(kind(), Code::FAST, name);
497 }
498
499
500 Handle<Code> NamedStoreHandlerCompiler::CompileStoreField(LookupIterator* it) {
501   Label miss;
502   DCHECK(it->representation().IsHeapObject());
503
504   GenerateFieldTypeChecks(*it->GetFieldType(), value(), &miss);
505   StoreFieldStub stub(isolate(), it->GetFieldIndex(), it->representation());
506   GenerateTailCall(masm(), stub.GetCode());
507
508   __ bind(&miss);
509   TailCallBuiltin(masm(), MissBuiltin(kind()));
510   return GetCode(kind(), Code::FAST, it->name());
511 }
512
513
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());
520
521   return GetCode(kind(), Code::FAST, name);
522 }
523
524
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,
531                           accessor_index);
532   return GetCode(kind(), Code::FAST, name);
533 }
534
535
536 #undef __
537
538
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;
545
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();
552     } else {
553       bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE;
554       ElementsKind elements_kind = receiver_map->elements_kind();
555
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 &&
560            *receiver_map ==
561                isolate()->get_initial_js_array_map(elements_kind)) &&
562           !is_strong(language_mode);
563
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();
573       } else {
574         DCHECK(elements_kind == DICTIONARY_ELEMENTS);
575         LoadICState state =
576             LoadICState(is_strong(language_mode) ? LoadICState::kStrongModeState
577                                                  : kNoExtraICState);
578         cached_stub = LoadDictionaryElementStub(isolate(), state).GetCode();
579       }
580     }
581
582     handlers->Add(cached_stub);
583   }
584 }
585 }  // namespace internal
586 }  // namespace v8