accessor-assembler.cc 115 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/ic/accessor-assembler.h"

#include "src/code-factory.h"
#include "src/code-stubs.h"
9
#include "src/counters.h"
10
#include "src/ic/handler-configuration.h"
11
#include "src/ic/ic.h"
12
#include "src/ic/stub-cache.h"
13
#include "src/objects-inl.h"
14 15 16 17 18

namespace v8 {
namespace internal {

using compiler::CodeAssemblerState;
19
using compiler::Node;
20 21 22

//////////////////// Private helpers.

23 24 25 26 27 28 29 30 31 32 33
// Loads dataX field from the DataHandler object.
Node* AccessorAssembler::LoadHandlerDataField(Node* handler, int data_index) {
#ifdef DEBUG
  Node* handler_map = LoadMap(handler);
  Node* instance_type = LoadMapInstanceType(handler_map);
#endif
  CSA_ASSERT(this,
             Word32Or(InstanceTypeEqual(instance_type, LOAD_HANDLER_TYPE),
                      InstanceTypeEqual(instance_type, STORE_HANDLER_TYPE)));
  int offset = 0;
  int minimum_size = 0;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  switch (data_index) {
    case 1:
      offset = DataHandler::kData1Offset;
      minimum_size = DataHandler::kSizeWithData1;
      break;
    case 2:
      offset = DataHandler::kData2Offset;
      minimum_size = DataHandler::kSizeWithData2;
      break;
    case 3:
      offset = DataHandler::kData3Offset;
      minimum_size = DataHandler::kSizeWithData3;
      break;
    default:
      UNREACHABLE();
      break;
50 51 52 53 54 55 56 57
  }
  USE(minimum_size);
  CSA_ASSERT(this, UintPtrGreaterThanOrEqual(
                       LoadMapInstanceSizeInWords(handler_map),
                       IntPtrConstant(minimum_size / kPointerSize)));
  return LoadObjectField(handler, offset);
}

58 59 60 61 62
Node* AccessorAssembler::TryMonomorphicCase(Node* slot, Node* vector,
                                            Node* receiver_map,
                                            Label* if_handler,
                                            Variable* var_handler,
                                            Label* if_miss) {
63 64 65 66 67
  Comment("TryMonomorphicCase");
  DCHECK_EQ(MachineRepresentation::kTagged, var_handler->rep());

  // TODO(ishell): add helper class that hides offset computations for a series
  // of loads.
68 69
  CSA_ASSERT(this, IsFeedbackVector(vector), vector);
  int32_t header_size = FeedbackVector::kFeedbackSlotsOffset - kHeapObjectTag;
70 71 72
  // Adding |header_size| with a separate IntPtrAdd rather than passing it
  // into ElementOffsetFromIndex() allows it to be folded into a single
  // [base, index, offset] indirect memory access on x64.
73
  Node* offset = ElementOffsetFromIndex(slot, HOLEY_ELEMENTS, SMI_PARAMETERS);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  Node* feedback = Load(MachineType::AnyTagged(), vector,
                        IntPtrAdd(offset, IntPtrConstant(header_size)));

  // Try to quickly handle the monomorphic case without knowing for sure
  // if we have a weak cell in feedback. We do know it's safe to look
  // at WeakCell::kValueOffset.
  GotoIf(WordNotEqual(receiver_map, LoadWeakCellValueUnchecked(feedback)),
         if_miss);

  Node* handler =
      Load(MachineType::AnyTagged(), vector,
           IntPtrAdd(offset, IntPtrConstant(header_size + kPointerSize)));

  var_handler->Bind(handler);
  Goto(if_handler);
  return feedback;
}

92 93 94 95
void AccessorAssembler::HandlePolymorphicCase(Node* receiver_map,
                                              Node* feedback, Label* if_handler,
                                              Variable* var_handler,
                                              Label* if_miss,
96
                                              int min_feedback_capacity) {
97 98 99
  Comment("HandlePolymorphicCase");
  DCHECK_EQ(MachineRepresentation::kTagged, var_handler->rep());

100 101 102 103
  // Deferred so the unrolled case can omit frame construction in bytecode
  // handler.
  Label loop(this, Label::kDeferred);

104 105 106
  // Iterate {feedback} array.
  const int kEntrySize = 2;

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  // Loading feedback's length is delayed until we need it when looking past
  // the first {min_feedback_capacity} (map, handler) pairs.
  Node* length = nullptr;
  CSA_ASSERT(this, SmiGreaterThanOrEqual(
                       LoadFixedArrayBaseLength(feedback),
                       SmiConstant(min_feedback_capacity * kEntrySize)));

  const int kUnrolledIterations = IC::kMaxPolymorphicMapCount;
  for (int i = 0; i < kUnrolledIterations; i++) {
    int map_index = i * kEntrySize;
    int handler_index = i * kEntrySize + 1;

    if (i >= min_feedback_capacity) {
      if (length == nullptr) length = LoadFixedArrayBaseLength(feedback);
      GotoIf(SmiGreaterThanOrEqual(SmiConstant(handler_index), length),
             if_miss);
    }

125
    Label next_entry(this);
126
    Node* cached_map =
127
        LoadWeakCellValue(LoadFixedArrayElement(feedback, map_index));
128 129 130
    GotoIf(WordNotEqual(receiver_map, cached_map), &next_entry);

    // Found, now call handler.
131
    Node* handler = LoadFixedArrayElement(feedback, handler_index);
132 133 134
    var_handler->Bind(handler);
    Goto(if_handler);

135
    BIND(&next_entry);
136
  }
137
  Goto(&loop);
138

139
  // Loop from {kUnrolledIterations}*kEntrySize to {length}.
140
  BIND(&loop);
141 142
  Node* start_index = IntPtrConstant(kUnrolledIterations * kEntrySize);
  Node* end_index = LoadAndUntagFixedArrayBaseLength(feedback);
143
  BuildFastLoop(
144
      start_index, end_index,
145
      [this, receiver_map, feedback, if_handler, var_handler](Node* index) {
146 147
        Node* cached_map =
            LoadWeakCellValue(LoadFixedArrayElement(feedback, index));
148

149 150
        Label next_entry(this);
        GotoIf(WordNotEqual(receiver_map, cached_map), &next_entry);
151 152

        // Found, now call handler.
153
        Node* handler = LoadFixedArrayElement(feedback, index, kPointerSize);
154
        var_handler->Bind(handler);
155
        Goto(if_handler);
156

157
        BIND(&next_entry);
158
      },
159
      kEntrySize, INTPTR_PARAMETERS, IndexAdvanceMode::kPost);
160 161 162 163
  // The loop falls through if no handler was found.
  Goto(if_miss);
}

164
void AccessorAssembler::HandleLoadICHandlerCase(
165
    const LoadICParameters* p, Node* handler, Label* miss,
166 167
    ExitPoint* exit_point, ICMode ic_mode, OnNonExistent on_nonexistent,
    ElementSupport support_elements) {
168
  Comment("have_handler");
169

170
  VARIABLE(var_holder, MachineRepresentation::kTagged, p->holder);
171
  VARIABLE(var_smi_handler, MachineRepresentation::kTagged, handler);
172 173 174

  Variable* vars[] = {&var_holder, &var_smi_handler};
  Label if_smi_handler(this, 2, vars);
jgruber's avatar
jgruber committed
175 176
  Label try_proto_handler(this, Label::kDeferred),
      call_handler(this, Label::kDeferred);
177 178 179 180 181

  Branch(TaggedIsSmi(handler), &if_smi_handler, &try_proto_handler);

  // |handler| is a Smi, encoding what to do. See SmiHandler methods
  // for the encoding format.
182
  BIND(&if_smi_handler);
183 184
  {
    HandleLoadICSmiHandlerCase(p, var_holder.value(), var_smi_handler.value(),
185
                               handler, miss, exit_point, on_nonexistent,
186
                               support_elements);
187 188
  }

189
  BIND(&try_proto_handler);
190 191
  {
    GotoIf(IsCodeMap(LoadMap(handler)), &call_handler);
192 193
    HandleLoadICProtoHandler(p, handler, &var_holder, &var_smi_handler,
                             &if_smi_handler, miss, exit_point, ic_mode);
194 195
  }

196
  BIND(&call_handler);
197 198
  {
    typedef LoadWithVectorDescriptor Descriptor;
199 200
    exit_point->ReturnCallStub(Descriptor(isolate()), handler, p->context,
                               p->receiver, p->name, p->slot, p->vector);
201 202 203
  }
}

204 205 206 207 208
void AccessorAssembler::HandleLoadField(Node* holder, Node* handler_word,
                                        Variable* var_double_value,
                                        Label* rebox_double,
                                        ExitPoint* exit_point) {
  Comment("field_load");
209 210
  Node* index = DecodeWord<LoadHandler::FieldIndexBits>(handler_word);
  Node* offset = IntPtrMul(index, IntPtrConstant(kPointerSize));
211 212 213 214 215

  Label inobject(this), out_of_object(this);
  Branch(IsSetWord<LoadHandler::IsInobjectBits>(handler_word), &inobject,
         &out_of_object);

216
  BIND(&inobject);
217 218 219 220 221
  {
    Label is_double(this);
    GotoIf(IsSetWord<LoadHandler::IsDoubleBits>(handler_word), &is_double);
    exit_point->Return(LoadObjectField(holder, offset));

222
    BIND(&is_double);
223 224 225 226 227 228 229 230 231 232
    if (FLAG_unbox_double_fields) {
      var_double_value->Bind(
          LoadObjectField(holder, offset, MachineType::Float64()));
    } else {
      Node* mutable_heap_number = LoadObjectField(holder, offset);
      var_double_value->Bind(LoadHeapNumberValue(mutable_heap_number));
    }
    Goto(rebox_double);
  }

233
  BIND(&out_of_object);
234 235
  {
    Label is_double(this);
236
    Node* properties = LoadFastProperties(holder);
237 238 239 240
    Node* value = LoadObjectField(properties, offset);
    GotoIf(IsSetWord<LoadHandler::IsDoubleBits>(handler_word), &is_double);
    exit_point->Return(value);

241
    BIND(&is_double);
242 243 244 245 246
    var_double_value->Bind(LoadHeapNumberValue(value));
    Goto(rebox_double);
  }
}

247 248 249 250 251 252 253 254 255 256 257 258
Node* AccessorAssembler::LoadDescriptorValue(Node* map, Node* descriptor) {
  Node* descriptors = LoadMapDescriptors(map);
  Node* scaled_descriptor =
      IntPtrMul(descriptor, IntPtrConstant(DescriptorArray::kEntrySize));
  Node* value_index = IntPtrAdd(
      scaled_descriptor, IntPtrConstant(DescriptorArray::kFirstIndex +
                                        DescriptorArray::kEntryValueIndex));
  CSA_ASSERT(this, UintPtrLessThan(descriptor, LoadAndUntagFixedArrayBaseLength(
                                                   descriptors)));
  return LoadFixedArrayElement(descriptors, value_index);
}

259
void AccessorAssembler::HandleLoadICSmiHandlerCase(
260
    const LoadICParameters* p, Node* holder, Node* smi_handler, Node* handler,
261
    Label* miss, ExitPoint* exit_point, OnNonExistent on_nonexistent,
262
    ElementSupport support_elements) {
263
  VARIABLE(var_double_value, MachineRepresentation::kFloat64);
264 265 266 267 268
  Label rebox_double(this, &var_double_value);

  Node* handler_word = SmiUntag(smi_handler);
  Node* handler_kind = DecodeWord<LoadHandler::KindBits>(handler_word);
  if (support_elements == kSupportElements) {
269 270 271 272 273
    Label if_element(this), if_indexed_string(this), if_property(this);
    GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kElement)),
           &if_element);
    Branch(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kIndexedString)),
           &if_indexed_string, &if_property);
274

275
    BIND(&if_element);
276 277 278 279 280 281
    Comment("element_load");
    Node* intptr_index = TryToIntptr(p->name, miss);
    Node* elements = LoadElements(holder);
    Node* is_jsarray_condition =
        IsSetWord<LoadHandler::IsJsArrayBits>(handler_word);
    Node* elements_kind =
282
        DecodeWord32FromWord<LoadHandler::ElementsKindBits>(handler_word);
283 284
    Label if_hole(this), unimplemented_elements_kind(this),
        if_oob(this, Label::kDeferred);
285 286
    EmitElementLoad(holder, elements, elements_kind, intptr_index,
                    is_jsarray_condition, &if_hole, &rebox_double,
287 288
                    &var_double_value, &unimplemented_elements_kind, &if_oob,
                    miss, exit_point);
289

290
    BIND(&unimplemented_elements_kind);
291 292 293 294 295 296 297
    {
      // Smi handlers should only be installed for supported elements kinds.
      // Crash if we get here.
      DebugBreak();
      Goto(miss);
    }

298 299 300 301 302
    BIND(&if_oob);
    {
      Comment("out of bounds elements access");
      Label return_undefined(this);

303 304 305 306 307
      // Negative indices aren't valid array indices (according to
      // the ECMAScript specification), and are stored as properties
      // in V8, not elements. So we cannot handle them here.
      GotoIf(IntPtrLessThan(intptr_index, IntPtrConstant(0)), miss);

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
      // Check if we're allowed to handle OOB accesses.
      Node* allow_out_of_bounds =
          IsSetWord<LoadHandler::AllowOutOfBoundsBits>(handler_word);
      GotoIfNot(allow_out_of_bounds, miss);

      // For typed arrays we never lookup elements in the prototype chain.
      GotoIf(IsJSTypedArray(holder), &return_undefined);

      // For all other receivers we need to check that the prototype chain
      // doesn't contain any elements.
      BranchIfPrototypesHaveNoElements(LoadMap(holder), &return_undefined,
                                       miss);

      BIND(&return_undefined);
      exit_point->Return(UndefinedConstant());
    }

325
    BIND(&if_hole);
326 327
    {
      Comment("convert hole");
328
      GotoIfNot(IsSetWord<LoadHandler::ConvertHoleBits>(handler_word), miss);
329
      GotoIf(IsNoElementsProtectorCellInvalid(), miss);
330
      exit_point->Return(UndefinedConstant());
331 332
    }

333 334
    BIND(&if_indexed_string);
    {
335 336
      Label if_oob(this, Label::kDeferred);

337 338
      Comment("indexed string");
      Node* intptr_index = TryToIntptr(p->name, miss);
339
      Node* length = LoadStringLengthAsWord(holder);
340
      GotoIf(UintPtrGreaterThanOrEqual(intptr_index, length), &if_oob);
341 342
      TNode<Int32T> code = StringCharCodeAt(holder, intptr_index);
      TNode<String> result = StringFromCharCode(code);
343
      Return(result);
344 345 346 347 348

      BIND(&if_oob);
      Node* allow_out_of_bounds =
          IsSetWord<LoadHandler::AllowOutOfBoundsBits>(handler_word);
      GotoIfNot(allow_out_of_bounds, miss);
349
      GotoIf(IsNoElementsProtectorCellInvalid(), miss);
350
      Return(UndefinedConstant());
351 352 353
    }

    BIND(&if_property);
354 355 356
    Comment("property_load");
  }

357
  Label constant(this), field(this), normal(this, Label::kDeferred),
358
      interceptor(this, Label::kDeferred), nonexistent(this),
359
      accessor(this, Label::kDeferred), global(this, Label::kDeferred),
360 361
      module_export(this, Label::kDeferred), proxy(this, Label::kDeferred),
      native_data_property(this), api_getter(this);
362
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kField)), &field);
363

364 365 366 367 368 369
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kConstant)),
         &constant);

  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kNonExistent)),
         &nonexistent);

370 371 372
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kNormal)),
         &normal);

373 374 375
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kAccessor)),
         &accessor);

376 377 378 379 380 381 382 383 384 385 386
  GotoIf(
      WordEqual(handler_kind, IntPtrConstant(LoadHandler::kNativeDataProperty)),
      &native_data_property);

  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kApiGetter)),
         &api_getter);

  GotoIf(WordEqual(handler_kind,
                   IntPtrConstant(LoadHandler::kApiGetterHolderIsPrototype)),
         &api_getter);

387 388 389
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kGlobal)),
         &global);

390 391
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kProxy)), &proxy);

392 393
  Branch(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kModuleExport)),
         &module_export, &interceptor);
394

395
  BIND(&field);
396 397
  HandleLoadField(holder, handler_word, &var_double_value, &rebox_double,
                  exit_point);
398

399
  BIND(&nonexistent);
400
  // This is a handler for a load of a non-existent value.
401
  if (on_nonexistent == OnNonExistent::kThrowReferenceError) {
402 403 404
    exit_point->ReturnCallRuntime(Runtime::kThrowReferenceError, p->context,
                                  p->name);
  } else {
405
    DCHECK_EQ(OnNonExistent::kReturnUndefined, on_nonexistent);
406 407 408
    exit_point->Return(UndefinedConstant());
  }

409
  BIND(&constant);
410 411
  {
    Comment("constant_load");
412
    Node* descriptor = DecodeWord<LoadHandler::DescriptorBits>(handler_word);
413
    Node* value = LoadDescriptorValue(LoadMap(holder), descriptor);
414

415
    exit_point->Return(value);
416
  }
417

418
  BIND(&normal);
419 420
  {
    Comment("load_normal");
421
    Node* properties = LoadSlowProperties(holder);
422
    VARIABLE(var_name_index, MachineType::PointerRepresentation());
423 424 425
    Label found(this, &var_name_index);
    NameDictionaryLookup<NameDictionary>(properties, p->name, &found,
                                         &var_name_index, miss);
426
    BIND(&found);
427
    {
428 429
      VARIABLE(var_details, MachineRepresentation::kWord32);
      VARIABLE(var_value, MachineRepresentation::kTagged);
430 431 432 433 434 435 436 437
      LoadPropertyFromNameDictionary(properties, var_name_index.value(),
                                     &var_details, &var_value);
      Node* value = CallGetterIfAccessor(var_value.value(), var_details.value(),
                                         p->context, p->receiver, miss);
      exit_point->Return(value);
    }
  }

438
  BIND(&accessor);
439 440 441
  {
    Comment("accessor_load");
    Node* descriptor = DecodeWord<LoadHandler::DescriptorBits>(handler_word);
442
    Node* accessor_pair = LoadDescriptorValue(LoadMap(holder), descriptor);
443 444 445 446 447 448 449 450
    CSA_ASSERT(this, IsAccessorPair(accessor_pair));
    Node* getter = LoadObjectField(accessor_pair, AccessorPair::kGetterOffset);
    CSA_ASSERT(this, Word32BinaryNot(IsTheHole(getter)));

    Callable callable = CodeFactory::Call(isolate());
    exit_point->Return(CallJS(callable, p->context, getter, p->receiver));
  }

451 452 453 454 455 456 457 458 459 460 461 462 463 464
  BIND(&native_data_property);
  {
    Comment("native_data_property_load");
    Node* descriptor = DecodeWord<LoadHandler::DescriptorBits>(handler_word);
    Node* accessor_info = LoadDescriptorValue(LoadMap(holder), descriptor);

    Callable callable = CodeFactory::ApiGetter(isolate());
    exit_point->ReturnCallStub(callable, p->context, p->receiver, holder,
                               accessor_info);
  }

  BIND(&api_getter);
  {
    Comment("api_getter");
465 466 467 468 469 470 471 472 473 474 475 476
    CSA_ASSERT(this, TaggedIsNotSmi(handler));
    Node* call_handler_info = holder;

    // Context is stored either in data2 or data3 field depending on whether
    // the access check is enabled for this handler or not.
    Node* context_cell = Select(
        IsSetWord<LoadHandler::DoAccessCheckOnReceiverBits>(handler_word),
        [=] { return LoadHandlerDataField(handler, 3); },
        [=] { return LoadHandlerDataField(handler, 2); },
        MachineRepresentation::kTagged);

    Node* context = LoadWeakCellValueUnchecked(context_cell);
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    Node* foreign =
        LoadObjectField(call_handler_info, CallHandlerInfo::kJsCallbackOffset);
    Node* callback = LoadObjectField(foreign, Foreign::kForeignAddressOffset,
                                     MachineType::Pointer());
    Node* data =
        LoadObjectField(call_handler_info, CallHandlerInfo::kDataOffset);

    VARIABLE(api_holder, MachineRepresentation::kTagged, p->receiver);
    Label load(this);
    GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kApiGetter)),
           &load);

    CSA_ASSERT(
        this,
        WordEqual(handler_kind,
                  IntPtrConstant(LoadHandler::kApiGetterHolderIsPrototype)));

    api_holder.Bind(LoadMapPrototype(LoadMap(p->receiver)));
    Goto(&load);

    BIND(&load);
    Callable callable = CodeFactory::CallApiCallback(isolate(), 0);
    exit_point->Return(CallStub(callable, nullptr, context, data,
                                api_holder.value(), callback, p->receiver));
  }

503 504
  BIND(&proxy);
  {
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
    VARIABLE(var_index, MachineType::PointerRepresentation());
    VARIABLE(var_unique, MachineRepresentation::kTagged);

    Label if_index(this), if_unique_name(this),
        to_name_failed(this, Label::kDeferred);

    if (support_elements == kSupportElements) {
      TryToName(p->name, &if_index, &var_index, &if_unique_name, &var_unique,
                &to_name_failed);

      BIND(&if_unique_name);
      exit_point->ReturnCallStub(
          Builtins::CallableFor(isolate(), Builtins::kProxyGetProperty),
          p->context, holder, var_unique.value(), p->receiver);

      BIND(&if_index);
      // TODO(mslekova): introduce TryToName that doesn't try to compute
      // the intptr index value
      Goto(&to_name_failed);

      BIND(&to_name_failed);
      exit_point->ReturnCallRuntime(Runtime::kGetPropertyWithReceiver,
                                    p->context, holder, p->name, p->receiver);

    } else {
      exit_point->ReturnCallStub(
          Builtins::CallableFor(isolate(), Builtins::kProxyGetProperty),
          p->context, holder, p->name, p->receiver);
    }
534 535
  }

536
  BIND(&global);
537 538 539 540 541 542 543 544 545 546 547 548
  {
    CSA_ASSERT(this, IsPropertyCell(holder));
    // Ensure the property cell doesn't contain the hole.
    Node* value = LoadObjectField(holder, PropertyCell::kValueOffset);
    Node* details =
        LoadAndUntagToWord32ObjectField(holder, PropertyCell::kDetailsOffset);
    GotoIf(IsTheHole(value), miss);

    exit_point->Return(
        CallGetterIfAccessor(value, details, p->context, p->receiver, miss));
  }

549
  BIND(&interceptor);
550 551 552 553 554 555 556
  {
    Comment("load_interceptor");
    exit_point->ReturnCallRuntime(Runtime::kLoadPropertyWithInterceptor,
                                  p->context, p->name, p->receiver, holder,
                                  p->slot, p->vector);
  }

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
  BIND(&module_export);
  {
    Comment("module export");
    Node* index = DecodeWord<LoadHandler::ExportsIndexBits>(handler_word);
    Node* module =
        LoadObjectField(p->receiver, JSModuleNamespace::kModuleOffset,
                        MachineType::TaggedPointer());
    Node* exports = LoadObjectField(module, Module::kExportsOffset,
                                    MachineType::TaggedPointer());
    Node* cell = LoadFixedArrayElement(exports, index);
    // The handler is only installed for exports that exist.
    CSA_ASSERT(this, IsCell(cell));
    Node* value = LoadCellValue(cell);
    Label is_the_hole(this, Label::kDeferred);
    GotoIf(IsTheHole(value), &is_the_hole);
    exit_point->Return(value);

    BIND(&is_the_hole);
    {
      Node* message = SmiConstant(MessageTemplate::kNotDefined);
      exit_point->ReturnCallRuntime(Runtime::kThrowReferenceError, p->context,
                                    message, p->name);
    }
  }

582
  BIND(&rebox_double);
583
  exit_point->Return(AllocateHeapNumberWithValue(var_double_value.value()));
584 585
}

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
// Performs actions common to both load and store handlers:
// 1. Checks prototype validity cell.
// 2. If |on_code_handler| is provided, then it checks if the sub handler is
//    a smi or code and if it's a code then it calls |on_code_handler| to
//    generate a code that handles Code handlers.
//    If |on_code_handler| is not provided, then only smi sub handler are
//    expected.
// 3. Does access check on receiver if ICHandler::DoAccessCheckOnReceiverBits
//    bit is set in the smi handler.
// 4. Does dictionary lookup on receiver if ICHandler::LookupOnReceiverBits bit
//    is set in the smi handler. If |on_found_on_receiver| is provided then
//    it calls it to generate a code that handles the "found on receiver case"
//    or just misses if the |on_found_on_receiver| is not provided.
// 5. Falls through in a case of a smi handler which is returned from this
//    function (tagged!).
// TODO(ishell): Remove templatezation once we move common bits from
// Load/StoreHandler to the base class.
template <typename ICHandler, typename ICParameters>
Node* AccessorAssembler::HandleProtoHandler(
    const ICParameters* p, Node* handler, const OnCodeHandler& on_code_handler,
    const OnFoundOnReceiver& on_found_on_receiver, Label* miss,
607
    ICMode ic_mode) {
608 609 610 611 612 613 614 615 616 617 618 619 620 621
  //
  // Check prototype validity cell.
  //
  {
    Label done(this);
    Node* validity_cell =
        LoadObjectField(handler, ICHandler::kValidityCellOffset);
    GotoIf(WordEqual(validity_cell, SmiConstant(0)), &done);
    Node* cell_value = LoadObjectField(validity_cell, Cell::kValueOffset);
    GotoIf(WordNotEqual(cell_value, SmiConstant(Map::kPrototypeChainValid)),
           miss);
    Goto(&done);
    BIND(&done);
  }
622

623 624 625 626 627 628 629 630 631
  //
  // Check smi handler bits.
  //
  {
    Node* smi_or_code_handler =
        LoadObjectField(handler, ICHandler::kSmiHandlerOffset);
    if (on_code_handler) {
      Label if_smi_handler(this);
      GotoIf(TaggedIsSmi(smi_or_code_handler), &if_smi_handler);
632

633 634
      CSA_ASSERT(this, IsCodeMap(LoadMap(smi_or_code_handler)));
      on_code_handler(smi_or_code_handler);
635

636 637 638 639 640 641 642 643 644 645 646 647
      BIND(&if_smi_handler);
    } else {
      CSA_ASSERT(this, TaggedIsSmi(smi_or_code_handler));
    }
    Node* handler_flags = SmiUntag(smi_or_code_handler);

    // Lookup on receiver and access checks are not necessary for global ICs
    // because in the former case the validity cell check guards modifications
    // of the global object and the latter is not applicable to the global
    // object.
    int mask = ICHandler::LookupOnReceiverBits::kMask |
               ICHandler::DoAccessCheckOnReceiverBits::kMask;
648
    if (ic_mode == ICMode::kGlobalIC) {
649
      CSA_ASSERT(this, IsClearWord(handler_flags, mask));
650 651
    } else {
      DCHECK_EQ(ICMode::kNonGlobalIC, ic_mode);
652 653 654 655 656 657 658

      Label done(this), if_do_access_check(this), if_lookup_on_receiver(this);
      GotoIf(IsClearWord(handler_flags, mask), &done);
      // Only one of the bits can be set at a time.
      CSA_ASSERT(this,
                 WordNotEqual(WordAnd(handler_flags, IntPtrConstant(mask)),
                              IntPtrConstant(mask)));
659 660
      Branch(IsSetWord<LoadHandler::DoAccessCheckOnReceiverBits>(handler_flags),
             &if_do_access_check, &if_lookup_on_receiver);
661

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
      BIND(&if_do_access_check);
      {
        Node* data2 = LoadHandlerDataField(handler, 2);
        Node* expected_native_context = LoadWeakCellValue(data2, miss);
        EmitAccessCheck(expected_native_context, p->context, p->receiver, &done,
                        miss);
      }

      // Dictionary lookup on receiver is not necessary for Load/StoreGlobalIC
      // because prototype validity cell check already guards modifications of
      // the global object.
      BIND(&if_lookup_on_receiver);
      {
        DCHECK_EQ(ICMode::kNonGlobalIC, ic_mode);
        CSA_ASSERT(this, Word32BinaryNot(HasInstanceType(
                             p->receiver, JS_GLOBAL_OBJECT_TYPE)));

        Node* properties = LoadSlowProperties(p->receiver);
        VARIABLE(var_name_index, MachineType::PointerRepresentation());
        Label found(this, &var_name_index);
        NameDictionaryLookup<NameDictionary>(properties, p->name, &found,
                                             &var_name_index, &done);
        BIND(&found);
        {
          if (on_found_on_receiver) {
            on_found_on_receiver(properties, var_name_index.value());
          } else {
            Goto(miss);
          }
        }
      }

      BIND(&done);
695
    }
696 697 698
    return smi_or_code_handler;
  }
}
699

700 701 702 703 704 705
void AccessorAssembler::HandleLoadICProtoHandler(
    const LoadICParameters* p, Node* handler, Variable* var_holder,
    Variable* var_smi_handler, Label* if_smi_handler, Label* miss,
    ExitPoint* exit_point, ICMode ic_mode) {
  DCHECK_EQ(MachineRepresentation::kTagged, var_holder->rep());
  DCHECK_EQ(MachineRepresentation::kTagged, var_smi_handler->rep());
706

707 708 709 710 711 712
  Node* smi_handler = HandleProtoHandler<LoadHandler>(
      p, handler,
      // Code sub-handlers are not expected in LoadICs, so no |on_code_handler|.
      nullptr,
      // on_found_on_receiver
      [=](Node* properties, Node* name_index) {
713 714
        VARIABLE(var_details, MachineRepresentation::kWord32);
        VARIABLE(var_value, MachineRepresentation::kTagged);
715 716
        LoadPropertyFromNameDictionary(properties, name_index, &var_details,
                                       &var_value);
717 718 719 720
        Node* value =
            CallGetterIfAccessor(var_value.value(), var_details.value(),
                                 p->context, p->receiver, miss);
        exit_point->Return(value);
721 722
      },
      miss, ic_mode);
723

724
  Node* maybe_holder_cell = LoadHandlerDataField(handler, 1);
725

726
  Label load_from_cached_holder(this), done(this);
727

728
  Branch(IsNull(maybe_holder_cell), &done, &load_from_cached_holder);
729

730
  BIND(&load_from_cached_holder);
731
  {
732 733 734 735
    // For regular holders, having passed the receiver map check and the
    // validity cell check implies that |holder| is alive. However, for
    // global object receivers, the |maybe_holder_cell| may be cleared.
    Node* holder = LoadWeakCellValue(maybe_holder_cell, miss);
736

737 738
    var_holder->Bind(holder);
    Goto(&done);
739
  }
740

741
  BIND(&done);
742 743 744 745
  {
    var_smi_handler->Bind(smi_handler);
    Goto(if_smi_handler);
  }
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
}

void AccessorAssembler::EmitAccessCheck(Node* expected_native_context,
                                        Node* context, Node* receiver,
                                        Label* can_access, Label* miss) {
  CSA_ASSERT(this, IsNativeContext(expected_native_context));

  Node* native_context = LoadNativeContext(context);
  GotoIf(WordEqual(expected_native_context, native_context), can_access);
  // If the receiver is not a JSGlobalProxy then we miss.
  GotoIfNot(IsJSGlobalProxy(receiver), miss);
  // For JSGlobalProxy receiver try to compare security tokens of current
  // and expected native contexts.
  Node* expected_token = LoadContextElement(expected_native_context,
                                            Context::SECURITY_TOKEN_INDEX);
  Node* current_token =
      LoadContextElement(native_context, Context::SECURITY_TOKEN_INDEX);
  Branch(WordEqual(expected_token, current_token), can_access, miss);
764 765
}

766 767 768 769 770 771 772 773 774 775
void AccessorAssembler::JumpIfDataProperty(Node* details, Label* writable,
                                           Label* readonly) {
  // Accessor properties never have the READ_ONLY attribute set.
  GotoIf(IsSetWord32(details, PropertyDetails::kAttributesReadOnlyMask),
         readonly);
  Node* kind = DecodeWord32<PropertyDetails::KindField>(details);
  GotoIf(Word32Equal(kind, Int32Constant(kData)), writable);
  // Fall through if it's an accessor property.
}

776 777 778 779 780 781 782 783 784 785 786 787
void AccessorAssembler::HandleStoreICNativeDataProperty(
    const StoreICParameters* p, Node* holder, Node* handler_word) {
  Node* descriptor = DecodeWord<StoreHandler::DescriptorBits>(handler_word);
  Node* accessor_info = LoadDescriptorValue(LoadMap(holder), descriptor);
  CSA_CHECK(this, IsAccessorInfo(accessor_info));

  Node* language_mode = GetLanguageMode(p->vector, p->slot);

  TailCallRuntime(Runtime::kStoreCallbackProperty, p->context, p->receiver,
                  holder, accessor_info, p->name, p->value, language_mode);
}

788
void AccessorAssembler::HandleStoreICHandlerCase(
789
    const StoreICParameters* p, Node* handler, Label* miss, ICMode ic_mode,
790
    ElementSupport support_elements) {
791
  Label if_smi_handler(this), if_nonsmi_handler(this);
792 793
  Label if_proto_handler(this), if_element_handler(this), call_handler(this),
      store_global(this);
794

795
  Branch(TaggedIsSmi(handler), &if_smi_handler, &if_nonsmi_handler);
796 797 798

  // |handler| is a Smi, encoding what to do. See SmiHandler methods
  // for the encoding format.
799
  BIND(&if_smi_handler);
800 801 802 803
  {
    Node* holder = p->receiver;
    Node* handler_word = SmiUntag(handler);

804 805
    Label if_fast_smi(this), if_proxy(this);

806 807
    STATIC_ASSERT(StoreHandler::kGlobalProxy + 1 == StoreHandler::kNormal);
    STATIC_ASSERT(StoreHandler::kNormal + 1 == StoreHandler::kProxy);
808 809 810 811
    STATIC_ASSERT(StoreHandler::kProxy + 1 == StoreHandler::kKindsNumber);

    Node* handler_kind = DecodeWord<StoreHandler::KindBits>(handler_word);
    GotoIf(IntPtrLessThan(handler_kind,
812
                          IntPtrConstant(StoreHandler::kGlobalProxy)),
813 814 815
           &if_fast_smi);
    GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kProxy)),
           &if_proxy);
816 817
    CSA_ASSERT(this,
               WordEqual(handler_kind, IntPtrConstant(StoreHandler::kNormal)));
818
    Node* properties = LoadSlowProperties(holder);
819

820
    VARIABLE(var_name_index, MachineType::PointerRepresentation());
821 822 823
    Label dictionary_found(this, &var_name_index);
    NameDictionaryLookup<NameDictionary>(properties, p->name, &dictionary_found,
                                         &var_name_index, miss);
824
    BIND(&dictionary_found);
825 826 827 828 829 830 831 832 833 834 835 836 837 838
    {
      Node* details = LoadDetailsByKeyIndex<NameDictionary>(
          properties, var_name_index.value());
      // Check that the property is a writable data property (no accessor).
      const int kTypeAndReadOnlyMask = PropertyDetails::KindField::kMask |
                                       PropertyDetails::kAttributesReadOnlyMask;
      STATIC_ASSERT(kData == 0);
      GotoIf(IsSetWord32(details, kTypeAndReadOnlyMask), miss);

      StoreValueByKeyIndex<NameDictionary>(properties, var_name_index.value(),
                                           p->value);
      Return(p->value);
    }

839
    BIND(&if_fast_smi);
840 841
    {
      Node* handler_kind = DecodeWord<StoreHandler::KindBits>(handler_word);
842

843 844 845 846 847 848
      Label data(this), accessor(this), native_data_property(this);
      GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kAccessor)),
             &accessor);
      Branch(WordEqual(handler_kind,
                       IntPtrConstant(StoreHandler::kNativeDataProperty)),
             &native_data_property, &data);
849

850 851 852 853 854 855 856 857 858 859 860
      BIND(&accessor);
      HandleStoreAccessor(p, holder, handler_word);

      BIND(&native_data_property);
      HandleStoreICNativeDataProperty(p, holder, handler_word);

      BIND(&data);
      // Handle non-transitioning field stores.
      HandleStoreICSmiHandlerCase(handler_word, holder, p->value, nullptr,
                                  miss);
    }
861 862

    BIND(&if_proxy);
863
    HandleStoreToProxy(p, holder, miss, support_elements);
864 865
  }

866
  BIND(&if_nonsmi_handler);
867 868
  {
    Node* handler_map = LoadMap(handler);
869
    GotoIf(IsWeakCellMap(handler_map), &store_global);
870 871 872
    Branch(IsCodeMap(handler_map), &call_handler, &if_proto_handler);
  }

873
  BIND(&if_proto_handler);
874
  HandleStoreICProtoHandler(p, handler, miss, ic_mode, support_elements);
875 876

  // |handler| is a heap object. Must be code, call it.
877
  BIND(&call_handler);
878 879 880 881 882
  {
    StoreWithVectorDescriptor descriptor(isolate());
    TailCallStub(descriptor, handler, p->context, p->receiver, p->name,
                 p->value, p->slot, p->vector);
  }
883

884
  BIND(&store_global);
885
  {
886
    // Load value or miss if the {handler} weak cell is cleared.
887 888
    Node* cell = LoadWeakCellValue(handler, miss);

889 890
    ExitPoint direct_exit(this);
    StoreGlobalIC_PropertyCellCase(cell, p->value, &direct_exit, miss);
891
  }
892 893
}

894 895 896 897 898 899 900 901 902 903 904 905 906
void AccessorAssembler::HandleStoreAccessor(const StoreICParameters* p,
                                            Node* holder, Node* handler_word) {
  Comment("accessor_store");
  Node* descriptor = DecodeWord<StoreHandler::DescriptorBits>(handler_word);
  Node* accessor_pair = LoadDescriptorValue(LoadMap(holder), descriptor);
  CSA_ASSERT(this, IsAccessorPair(accessor_pair));
  Node* setter = LoadObjectField(accessor_pair, AccessorPair::kSetterOffset);
  CSA_ASSERT(this, Word32BinaryNot(IsTheHole(setter)));

  Callable callable = CodeFactory::Call(isolate());
  Return(CallJS(callable, p->context, setter, p->receiver, p->value));
}

907
void AccessorAssembler::HandleStoreICProtoHandler(
908
    const StoreICParameters* p, Node* handler, Label* miss, ICMode ic_mode,
909
    ElementSupport support_elements) {
910 911
  Comment("HandleStoreICProtoHandler");

912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
  OnCodeHandler on_code_handler;
  if (support_elements == kSupportElements) {
    // Code sub-handlers are expected only in KeyedStoreICs.
    on_code_handler = [=](Node* code_handler) {
      // This is either element store or transitioning element store.
      Label if_element_store(this), if_transitioning_element_store(this);
      Branch(IsStoreHandler0Map(LoadMap(handler)), &if_element_store,
             &if_transitioning_element_store);
      BIND(&if_element_store);
      {
        StoreWithVectorDescriptor descriptor(isolate());
        TailCallStub(descriptor, code_handler, p->context, p->receiver, p->name,
                     p->value, p->slot, p->vector);
      }

      BIND(&if_transitioning_element_store);
      {
        Node* transition_map_cell = LoadHandlerDataField(handler, 1);
        Node* transition_map = LoadWeakCellValue(transition_map_cell, miss);
        CSA_ASSERT(this, IsMap(transition_map));

        GotoIf(IsDeprecatedMap(transition_map), miss);

        StoreTransitionDescriptor descriptor(isolate());
        TailCallStub(descriptor, code_handler, p->context, p->receiver, p->name,
                     transition_map, p->value, p->slot, p->vector);
      }
    };
  }

  Node* smi_handler = HandleProtoHandler<StoreHandler>(
      p, handler, on_code_handler,
944 945 946 947 948 949 950 951 952 953 954
      // on_found_on_receiver
      [=](Node* properties, Node* name_index) {
        // TODO(ishell): combine with |found| case inside |if_store_normal|.
        Node* details =
            LoadDetailsByKeyIndex<NameDictionary>(properties, name_index);
        // Check that the property is a writable data property (no accessor).
        const int kTypeAndReadOnlyMask =
            PropertyDetails::KindField::kMask |
            PropertyDetails::kAttributesReadOnlyMask;
        STATIC_ASSERT(kData == 0);
        GotoIf(IsSetWord32(details, kTypeAndReadOnlyMask), miss);
955

956 957 958 959
        StoreValueByKeyIndex<NameDictionary>(properties, name_index, p->value);
        Return(p->value);
      },
      miss, ic_mode);
960

961 962
  Label if_transition_map(this), if_holder_object(this);

963 964 965 966 967
  Node* maybe_transition_or_holder_cell = LoadHandlerDataField(handler, 1);
  Node* maybe_transition_or_holder =
      LoadWeakCellValue(maybe_transition_or_holder_cell, miss);
  Branch(IsMap(maybe_transition_or_holder), &if_transition_map,
         &if_holder_object);
968

969
  BIND(&if_transition_map);
970
  {
971
    Label if_transition_to_constant(this), if_store_normal(this);
972 973

    Node* holder = p->receiver;
974
    Node* transition_map = maybe_transition_or_holder;
975

976
    GotoIf(IsDeprecatedMap(transition_map), miss);
977 978
    Node* handler_word = SmiUntag(smi_handler);

979
    Node* handler_kind = DecodeWord<StoreHandler::KindBits>(handler_word);
980
    GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kNormal)),
981
           &if_store_normal);
982 983 984
    GotoIf(WordEqual(handler_kind,
                     IntPtrConstant(StoreHandler::kTransitionToConstant)),
           &if_transition_to_constant);
985

986
    CSA_ASSERT(this,
987 988
               WordEqual(handler_kind,
                         IntPtrConstant(StoreHandler::kTransitionToField)));
989 990

    // Handle transitioning field stores.
991
    HandleStoreICSmiHandlerCase(handler_word, holder, p->value, transition_map,
992 993
                                miss);

994
    BIND(&if_transition_to_constant);
995 996
    {
      // Check that constant matches value.
997
      Node* descriptor = DecodeWord<StoreHandler::DescriptorBits>(handler_word);
998
      Node* constant = LoadDescriptorValue(transition_map, descriptor);
999 1000
      GotoIf(WordNotEqual(p->value, constant), miss);

1001
      StoreMap(p->receiver, transition_map);
1002 1003
      Return(p->value);
    }
1004

1005
    BIND(&if_store_normal);
1006
    {
1007
      Node* properties = LoadSlowProperties(p->receiver);
1008

1009
      VARIABLE(var_name_index, MachineType::PointerRepresentation());
1010 1011 1012
      Label found(this, &var_name_index), not_found(this);
      NameDictionaryLookup<NameDictionary>(properties, p->name, &found,
                                           &var_name_index, &not_found);
1013
      BIND(&found);
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
      {
        Node* details = LoadDetailsByKeyIndex<NameDictionary>(
            properties, var_name_index.value());
        // Check that the property is a writable data property (no accessor).
        const int kTypeAndReadOnlyMask =
            PropertyDetails::KindField::kMask |
            PropertyDetails::kAttributesReadOnlyMask;
        STATIC_ASSERT(kData == 0);
        GotoIf(IsSetWord32(details, kTypeAndReadOnlyMask), miss);

        StoreValueByKeyIndex<NameDictionary>(properties, var_name_index.value(),
                                             p->value);
        Return(p->value);
      }

1029
      BIND(&not_found);
1030 1031
      {
        Label slow(this);
1032 1033 1034
        Node* receiver_map = LoadMap(p->receiver);
        InvalidateValidityCellIfPrototype(receiver_map);

1035 1036 1037
        Add<NameDictionary>(properties, p->name, p->value, &slow);
        Return(p->value);

1038
        BIND(&slow);
1039 1040
        TailCallRuntime(Runtime::kAddDictionaryProperty, p->context,
                        p->receiver, p->name, p->value);
1041 1042
      }
    }
1043 1044 1045
  }
  BIND(&if_holder_object);
  {
1046 1047
    Label if_store_global_proxy(this), if_api_setter(this), if_accessor(this),
        if_native_data_property(this);
1048
    Node* holder = maybe_transition_or_holder;
1049 1050 1051 1052 1053

    CSA_ASSERT(this, TaggedIsSmi(smi_handler));
    Node* handler_word = SmiUntag(smi_handler);

    Node* handler_kind = DecodeWord<StoreHandler::KindBits>(handler_word);
1054
    GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kGlobalProxy)),
1055
           &if_store_global_proxy);
1056

1057 1058 1059
    GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kAccessor)),
           &if_accessor);

1060 1061 1062 1063
    GotoIf(WordEqual(handler_kind,
                     IntPtrConstant(StoreHandler::kNativeDataProperty)),
           &if_native_data_property);

1064 1065 1066 1067 1068 1069 1070
    GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kApiSetter)),
           &if_api_setter);

    GotoIf(WordEqual(handler_kind,
                     IntPtrConstant(StoreHandler::kApiSetterHolderIsPrototype)),
           &if_api_setter);

1071 1072 1073 1074
    CSA_ASSERT(this,
               WordEqual(handler_kind, IntPtrConstant(StoreHandler::kProxy)));
    HandleStoreToProxy(p, holder, miss, support_elements);

1075 1076 1077
    BIND(&if_accessor);
    HandleStoreAccessor(p, holder, handler_word);

1078 1079 1080
    BIND(&if_native_data_property);
    HandleStoreICNativeDataProperty(p, holder, handler_word);

1081 1082 1083
    BIND(&if_api_setter);
    {
      Comment("api_setter");
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
      CSA_ASSERT(this, TaggedIsNotSmi(handler));
      Node* call_handler_info = holder;

      // Context is stored either in data2 or data3 field depending on whether
      // the access check is enabled for this handler or not.
      Node* context_cell = Select(
          IsSetWord<LoadHandler::DoAccessCheckOnReceiverBits>(handler_word),
          [=] { return LoadHandlerDataField(handler, 3); },
          [=] { return LoadHandlerDataField(handler, 2); },
          MachineRepresentation::kTagged);

      Node* context = LoadWeakCellValueUnchecked(context_cell);
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122

      Node* foreign = LoadObjectField(call_handler_info,
                                      CallHandlerInfo::kJsCallbackOffset);
      Node* callback = LoadObjectField(foreign, Foreign::kForeignAddressOffset,
                                       MachineType::Pointer());
      Node* data =
          LoadObjectField(call_handler_info, CallHandlerInfo::kDataOffset);

      VARIABLE(api_holder, MachineRepresentation::kTagged, p->receiver);
      Label store(this);
      GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kApiSetter)),
             &store);

      CSA_ASSERT(
          this,
          WordEqual(handler_kind,
                    IntPtrConstant(StoreHandler::kApiSetterHolderIsPrototype)));

      api_holder.Bind(LoadMapPrototype(LoadMap(p->receiver)));
      Goto(&store);

      BIND(&store);
      Callable callable = CodeFactory::CallApiCallback(isolate(), 1);
      Return(CallStub(callable, nullptr, context, data, api_holder.value(),
                      callback, p->receiver, p->value));
    }

1123 1124 1125
    BIND(&if_store_global_proxy);
    {
      ExitPoint direct_exit(this);
1126
      StoreGlobalIC_PropertyCellCase(holder, p->value, &direct_exit, miss);
1127
    }
1128 1129 1130
  }
}

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
Node* AccessorAssembler::GetLanguageMode(Node* vector, Node* slot) {
  VARIABLE(var_language_mode, MachineRepresentation::kTaggedSigned,
           SmiConstant(LanguageMode::kStrict));
  Label language_mode_determined(this);
  BranchIfStrictMode(vector, slot, &language_mode_determined);
  var_language_mode.Bind(SmiConstant(LanguageMode::kSloppy));
  Goto(&language_mode_determined);
  BIND(&language_mode_determined);
  return var_language_mode.value();
}

1142 1143
void AccessorAssembler::HandleStoreToProxy(const StoreICParameters* p,
                                           Node* proxy, Label* miss,
1144
                                           ElementSupport support_elements) {
1145 1146 1147
  VARIABLE(var_index, MachineType::PointerRepresentation());
  VARIABLE(var_unique, MachineRepresentation::kTagged);

1148
  Label if_index(this), if_unique_name(this),
1149
      to_name_failed(this, Label::kDeferred);
1150 1151

  Node* language_mode = GetLanguageMode(p->vector, p->slot);
1152 1153 1154 1155 1156 1157 1158

  if (support_elements == kSupportElements) {
    TryToName(p->name, &if_index, &var_index, &if_unique_name, &var_unique,
              &to_name_failed);

    BIND(&if_unique_name);
    CallBuiltin(Builtins::kProxySetProperty, p->context, proxy,
1159
                var_unique.value(), p->value, p->receiver, language_mode);
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
    Return(p->value);

    // The index case is handled earlier by the runtime.
    BIND(&if_index);
    // TODO(mslekova): introduce TryToName that doesn't try to compute
    // the intptr index value
    Goto(&to_name_failed);

    BIND(&to_name_failed);
    TailCallRuntime(Runtime::kSetPropertyWithReceiver, p->context, proxy,
1170
                    p->name, p->value, p->receiver, language_mode);
1171 1172 1173
  } else {
    Node* name = ToName(p->context, p->name);
    TailCallBuiltin(Builtins::kProxySetProperty, p->context, proxy, name,
1174
                    p->value, p->receiver, language_mode);
1175 1176 1177
  }
}

1178 1179 1180 1181
void AccessorAssembler::HandleStoreICSmiHandlerCase(Node* handler_word,
                                                    Node* holder, Node* value,
                                                    Node* transition,
                                                    Label* miss) {
1182 1183 1184 1185 1186 1187
  Comment(transition ? "transitioning field store" : "field store");
#ifdef DEBUG
  Node* handler_kind = DecodeWord<StoreHandler::KindBits>(handler_word);
  if (transition) {
    CSA_ASSERT(
        this,
1188 1189 1190 1191 1192
        Word32Or(
            WordEqual(handler_kind,
                      IntPtrConstant(StoreHandler::kTransitionToField)),
            WordEqual(handler_kind,
                      IntPtrConstant(StoreHandler::kTransitionToConstant))));
1193
  } else {
1194 1195
    if (FLAG_track_constant_fields) {
      CSA_ASSERT(
1196 1197 1198 1199
          this, Word32Or(WordEqual(handler_kind,
                                   IntPtrConstant(StoreHandler::kField)),
                         WordEqual(handler_kind,
                                   IntPtrConstant(StoreHandler::kConstField))));
1200
    } else {
1201 1202
      CSA_ASSERT(this,
                 WordEqual(handler_kind, IntPtrConstant(StoreHandler::kField)));
1203
    }
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
  }
#endif

  Node* field_representation =
      DecodeWord<StoreHandler::FieldRepresentationBits>(handler_word);

  Label if_smi_field(this), if_double_field(this), if_heap_object_field(this),
      if_tagged_field(this);

  GotoIf(WordEqual(field_representation, IntPtrConstant(StoreHandler::kTagged)),
         &if_tagged_field);
  GotoIf(WordEqual(field_representation,
                   IntPtrConstant(StoreHandler::kHeapObject)),
         &if_heap_object_field);
  GotoIf(WordEqual(field_representation, IntPtrConstant(StoreHandler::kDouble)),
         &if_double_field);
  CSA_ASSERT(this, WordEqual(field_representation,
                             IntPtrConstant(StoreHandler::kSmi)));
  Goto(&if_smi_field);

1224
  BIND(&if_tagged_field);
1225 1226 1227 1228 1229 1230
  {
    Comment("store tagged field");
    HandleStoreFieldAndReturn(handler_word, holder, Representation::Tagged(),
                              value, transition, miss);
  }

1231
  BIND(&if_double_field);
1232 1233 1234 1235 1236 1237
  {
    Comment("store double field");
    HandleStoreFieldAndReturn(handler_word, holder, Representation::Double(),
                              value, transition, miss);
  }

1238
  BIND(&if_heap_object_field);
1239 1240
  {
    Comment("store heap object field");
1241 1242 1243
    HandleStoreFieldAndReturn(handler_word, holder,
                              Representation::HeapObject(), value, transition,
                              miss);
1244 1245
  }

1246
  BIND(&if_smi_field);
1247 1248 1249 1250 1251 1252 1253
  {
    Comment("store smi field");
    HandleStoreFieldAndReturn(handler_word, holder, Representation::Smi(),
                              value, transition, miss);
  }
}

1254 1255 1256 1257 1258
void AccessorAssembler::HandleStoreFieldAndReturn(Node* handler_word,
                                                  Node* holder,
                                                  Representation representation,
                                                  Node* value, Node* transition,
                                                  Label* miss) {
1259
  bool transition_to_field = transition != nullptr;
1260 1261
  Node* prepared_value = PrepareValueForStore(
      handler_word, holder, representation, transition, value, miss);
1262 1263 1264 1265 1266

  Label if_inobject(this), if_out_of_object(this);
  Branch(IsSetWord<StoreHandler::IsInobjectBits>(handler_word), &if_inobject,
         &if_out_of_object);

1267
  BIND(&if_inobject);
1268
  {
1269
    StoreNamedField(handler_word, holder, true, representation, prepared_value,
1270
                    transition_to_field, miss);
1271
    if (transition_to_field) {
1272
      StoreMap(holder, transition);
1273 1274 1275 1276
    }
    Return(value);
  }

1277
  BIND(&if_out_of_object);
1278
  {
1279
    if (transition_to_field) {
1280
      ExtendPropertiesBackingStore(holder, handler_word);
1281 1282
    }

1283
    StoreNamedField(handler_word, holder, false, representation, prepared_value,
1284
                    transition_to_field, miss);
1285
    if (transition_to_field) {
1286
      StoreMap(holder, transition);
1287 1288 1289 1290 1291
    }
    Return(value);
  }
}

1292 1293 1294 1295
Node* AccessorAssembler::PrepareValueForStore(Node* handler_word, Node* holder,
                                              Representation representation,
                                              Node* transition, Node* value,
                                              Label* bailout) {
1296 1297 1298 1299 1300
  if (representation.IsDouble()) {
    value = TryTaggedToFloat64(value, bailout);

  } else if (representation.IsHeapObject()) {
    GotoIf(TaggedIsSmi(value), bailout);
1301 1302 1303 1304 1305 1306

    Label done(this);
    if (FLAG_track_constant_fields && !transition) {
      // Skip field type check in favor of constant value check when storing
      // to constant field.
      GotoIf(WordEqual(DecodeWord<StoreHandler::KindBits>(handler_word),
1307
                       IntPtrConstant(StoreHandler::kConstField)),
1308 1309
             &done);
    }
1310
    Node* descriptor = DecodeWord<StoreHandler::DescriptorBits>(handler_word);
1311 1312
    Node* maybe_field_type = LoadDescriptorValue(
        transition ? transition : LoadMap(holder), descriptor);
1313 1314 1315 1316 1317 1318 1319

    GotoIf(TaggedIsSmi(maybe_field_type), &done);
    // Check that value type matches the field type.
    {
      Node* field_type = LoadWeakCellValue(maybe_field_type, bailout);
      Branch(WordEqual(LoadMap(value), field_type), &done, bailout);
    }
1320
    BIND(&done);
1321 1322

  } else if (representation.IsSmi()) {
1323
    GotoIfNot(TaggedIsSmi(value), bailout);
1324 1325 1326 1327 1328 1329 1330

  } else {
    DCHECK(representation.IsTagged());
  }
  return value;
}

1331 1332 1333 1334 1335 1336
void AccessorAssembler::ExtendPropertiesBackingStore(Node* object,
                                                     Node* handler_word) {
  Label done(this);
  GotoIfNot(IsSetWord<StoreHandler::ExtendStorageBits>(handler_word), &done);
  Comment("[ Extend storage");

1337
  ParameterMode mode = OptimalParameterMode();
1338

1339 1340 1341
  // TODO(gsathya): Clean up the type conversions by creating smarter
  // helpers that do the correct op based on the mode.
  VARIABLE(var_properties, MachineRepresentation::kTaggedPointer);
1342
  VARIABLE(var_encoded_hash, MachineRepresentation::kWord32);
1343
  VARIABLE(var_length, ParameterRepresentation(mode));
1344

1345 1346 1347 1348 1349 1350 1351 1352
  Node* properties = LoadObjectField(object, JSObject::kPropertiesOrHashOffset);
  var_properties.Bind(properties);

  Label if_smi_hash(this), if_property_array(this), extend_store(this);
  Branch(TaggedIsSmi(properties), &if_smi_hash, &if_property_array);

  BIND(&if_smi_hash);
  {
1353 1354 1355 1356
    Node* hash = SmiToWord32(properties);
    Node* encoded_hash =
        Word32Shl(hash, Int32Constant(PropertyArray::HashField::kShift));
    var_encoded_hash.Bind(encoded_hash);
1357 1358 1359 1360 1361 1362 1363 1364
    var_length.Bind(IntPtrOrSmiConstant(0, mode));
    var_properties.Bind(EmptyFixedArrayConstant());
    Goto(&extend_store);
  }

  BIND(&if_property_array);
  {
    Node* length_and_hash_int32 = LoadAndUntagToWord32ObjectField(
1365
        var_properties.value(), PropertyArray::kLengthAndHashOffset);
1366 1367 1368 1369 1370
    var_encoded_hash.Bind(Word32And(
        length_and_hash_int32, Int32Constant(PropertyArray::HashField::kMask)));
    Node* length_intptr = ChangeInt32ToIntPtr(
        Word32And(length_and_hash_int32,
                  Int32Constant(PropertyArray::LengthField::kMask)));
1371 1372 1373 1374
    Node* length = WordToParameter(length_intptr, mode);
    var_length.Bind(length);
    Goto(&extend_store);
  }
1375

1376 1377 1378 1379 1380
  BIND(&extend_store);
  {
    // Previous property deletion could have left behind unused backing store
    // capacity even for a map that think it doesn't have any unused fields.
    // Perform a bounds check to see if we actually have to grow the array.
1381 1382
    Node* index = DecodeWord<StoreHandler::FieldIndexBits>(handler_word);
    Node* offset = IntPtrMul(index, IntPtrConstant(kPointerSize));
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
    Node* size = ElementOffsetFromIndex(var_length.value(), PACKED_ELEMENTS,
                                        mode, FixedArray::kHeaderSize);
    GotoIf(UintPtrLessThan(offset, size), &done);

    Node* delta = IntPtrOrSmiConstant(JSObject::kFieldsAdded, mode);
    Node* new_capacity = IntPtrOrSmiAdd(var_length.value(), delta, mode);

    // Grow properties array.
    DCHECK(kMaxNumberOfDescriptors + JSObject::kFieldsAdded <
           FixedArrayBase::GetMaxLengthForNewSpaceAllocation(PACKED_ELEMENTS));
    // The size of a new properties backing store is guaranteed to be small
    // enough that the new backing store will be allocated in new space.
    CSA_ASSERT(this,
               UintPtrOrSmiLessThan(
                   new_capacity,
                   IntPtrOrSmiConstant(
                       kMaxNumberOfDescriptors + JSObject::kFieldsAdded, mode),
                   mode));

    Node* new_properties = AllocatePropertyArray(new_capacity, mode);

    FillPropertyArrayWithUndefined(new_properties, var_length.value(),
                                   new_capacity, mode);

    // |new_properties| is guaranteed to be in new space, so we can skip
    // the write barrier.
    CopyPropertyArrayValues(var_properties.value(), new_properties,
                            var_length.value(), SKIP_WRITE_BARRIER, mode);

    // TODO(gsathya): Clean up the type conversions by creating smarter
    // helpers that do the correct op based on the mode.
    Node* new_capacity_int32 =
        TruncateWordToWord32(ParameterToWord(new_capacity, mode));
    Node* new_length_and_hash_int32 =
1417
        Word32Or(var_encoded_hash.value(), new_capacity_int32);
1418
    StoreObjectField(new_properties, PropertyArray::kLengthAndHashOffset,
1419 1420 1421 1422 1423
                     SmiFromWord32(new_length_and_hash_int32));
    StoreObjectField(object, JSObject::kPropertiesOrHashOffset, new_properties);
    Comment("] Extend storage");
    Goto(&done);
  }
1424
  BIND(&done);
1425 1426
}

1427 1428 1429
void AccessorAssembler::StoreNamedField(Node* handler_word, Node* object,
                                        bool is_inobject,
                                        Representation representation,
1430 1431
                                        Node* value, bool transition_to_field,
                                        Label* bailout) {
1432 1433 1434
  bool store_value_as_double = representation.IsDouble();
  Node* property_storage = object;
  if (!is_inobject) {
1435
    property_storage = LoadFastProperties(object);
1436 1437
  }

1438 1439
  Node* index = DecodeWord<StoreHandler::FieldIndexBits>(handler_word);
  Node* offset = IntPtrMul(index, IntPtrConstant(kPointerSize));
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
  if (representation.IsDouble()) {
    if (!FLAG_unbox_double_fields || !is_inobject) {
      if (transition_to_field) {
        Node* heap_number = AllocateHeapNumberWithValue(value, MUTABLE);
        // Store the new mutable heap number into the object.
        value = heap_number;
        store_value_as_double = false;
      } else {
        // Load the heap number.
        property_storage = LoadObjectField(property_storage, offset);
        // Store the double value into it.
        offset = IntPtrConstant(HeapNumber::kValueOffset);
      }
    }
  }

1456 1457 1458
  // Do constant value check if necessary.
  if (FLAG_track_constant_fields && !transition_to_field) {
    Label done(this);
1459
    GotoIfNot(WordEqual(DecodeWord<StoreHandler::KindBits>(handler_word),
1460
                        IntPtrConstant(StoreHandler::kConstField)),
1461
              &done);
1462 1463 1464 1465
    {
      if (store_value_as_double) {
        Node* current_value =
            LoadObjectField(property_storage, offset, MachineType::Float64());
1466
        GotoIfNot(Float64Equal(current_value, value), bailout);
1467 1468
      } else {
        Node* current_value = LoadObjectField(property_storage, offset);
1469
        GotoIfNot(WordEqual(current_value, value), bailout);
1470 1471 1472
      }
      Goto(&done);
    }
1473
    BIND(&done);
1474 1475 1476
  }

  // Do the store.
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
  if (store_value_as_double) {
    StoreObjectFieldNoWriteBarrier(property_storage, offset, value,
                                   MachineRepresentation::kFloat64);
  } else if (representation.IsSmi()) {
    StoreObjectFieldNoWriteBarrier(property_storage, offset, value);
  } else {
    StoreObjectField(property_storage, offset, value);
  }
}

1487 1488 1489 1490 1491
void AccessorAssembler::EmitFastElementsBoundsCheck(Node* object,
                                                    Node* elements,
                                                    Node* intptr_index,
                                                    Node* is_jsarray_condition,
                                                    Label* miss) {
1492
  VARIABLE(var_length, MachineType::PointerRepresentation());
1493 1494 1495 1496 1497 1498 1499
  Comment("Fast elements bounds check");
  Label if_array(this), length_loaded(this, &var_length);
  GotoIf(is_jsarray_condition, &if_array);
  {
    var_length.Bind(SmiUntag(LoadFixedArrayBaseLength(elements)));
    Goto(&length_loaded);
  }
1500
  BIND(&if_array);
1501
  {
1502
    var_length.Bind(SmiUntag(LoadFastJSArrayLength(object)));
1503 1504
    Goto(&length_loaded);
  }
1505
  BIND(&length_loaded);
1506
  GotoIfNot(UintPtrLessThan(intptr_index, var_length.value()), miss);
1507 1508
}

1509 1510 1511 1512 1513
void AccessorAssembler::EmitElementLoad(
    Node* object, Node* elements, Node* elements_kind, Node* intptr_index,
    Node* is_jsarray_condition, Label* if_hole, Label* rebox_double,
    Variable* var_double_value, Label* unimplemented_elements_kind,
    Label* out_of_bounds, Label* miss, ExitPoint* exit_point) {
1514 1515 1516 1517
  Label if_typed_array(this), if_fast_packed(this), if_fast_holey(this),
      if_fast_double(this), if_fast_holey_double(this), if_nonfast(this),
      if_dictionary(this);
  GotoIf(
1518
      Int32GreaterThan(elements_kind, Int32Constant(LAST_FAST_ELEMENTS_KIND)),
1519 1520 1521 1522 1523
      &if_nonfast);

  EmitFastElementsBoundsCheck(object, elements, intptr_index,
                              is_jsarray_condition, out_of_bounds);
  int32_t kinds[] = {// Handled by if_fast_packed.
1524
                     PACKED_SMI_ELEMENTS, PACKED_ELEMENTS,
1525
                     // Handled by if_fast_holey.
1526
                     HOLEY_SMI_ELEMENTS, HOLEY_ELEMENTS,
1527
                     // Handled by if_fast_double.
1528
                     PACKED_DOUBLE_ELEMENTS,
1529
                     // Handled by if_fast_holey_double.
1530
                     HOLEY_DOUBLE_ELEMENTS};
1531 1532 1533 1534
  Label* labels[] = {// FAST_{SMI,}_ELEMENTS
                     &if_fast_packed, &if_fast_packed,
                     // FAST_HOLEY_{SMI,}_ELEMENTS
                     &if_fast_holey, &if_fast_holey,
1535
                     // PACKED_DOUBLE_ELEMENTS
1536
                     &if_fast_double,
1537
                     // HOLEY_DOUBLE_ELEMENTS
1538
                     &if_fast_holey_double};
1539 1540
  Switch(elements_kind, unimplemented_elements_kind, kinds, labels,
         arraysize(kinds));
1541

1542
  BIND(&if_fast_packed);
1543 1544
  {
    Comment("fast packed elements");
1545
    exit_point->Return(LoadFixedArrayElement(elements, intptr_index));
1546 1547
  }

1548
  BIND(&if_fast_holey);
1549 1550
  {
    Comment("fast holey elements");
1551
    Node* element = LoadFixedArrayElement(elements, intptr_index);
1552
    GotoIf(WordEqual(element, TheHoleConstant()), if_hole);
1553
    exit_point->Return(element);
1554 1555
  }

1556
  BIND(&if_fast_double);
1557 1558
  {
    Comment("packed double elements");
1559 1560
    var_double_value->Bind(LoadFixedDoubleArrayElement(elements, intptr_index,
                                                       MachineType::Float64()));
1561 1562 1563
    Goto(rebox_double);
  }

1564
  BIND(&if_fast_holey_double);
1565 1566 1567 1568 1569 1570 1571 1572 1573
  {
    Comment("holey double elements");
    Node* value = LoadFixedDoubleArrayElement(elements, intptr_index,
                                              MachineType::Float64(), 0,
                                              INTPTR_PARAMETERS, if_hole);
    var_double_value->Bind(value);
    Goto(rebox_double);
  }

1574
  BIND(&if_nonfast);
1575 1576
  {
    STATIC_ASSERT(LAST_ELEMENTS_KIND == LAST_FIXED_TYPED_ARRAY_ELEMENTS_KIND);
1577
    GotoIf(Int32GreaterThanOrEqual(
1578
               elements_kind,
1579
               Int32Constant(FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND)),
1580
           &if_typed_array);
1581
    GotoIf(Word32Equal(elements_kind, Int32Constant(DICTIONARY_ELEMENTS)),
1582 1583 1584 1585
           &if_dictionary);
    Goto(unimplemented_elements_kind);
  }

1586
  BIND(&if_dictionary);
1587 1588 1589
  {
    Comment("dictionary elements");
    GotoIf(IntPtrLessThan(intptr_index, IntPtrConstant(0)), out_of_bounds);
1590
    VARIABLE(var_entry, MachineType::PointerRepresentation());
1591
    Label if_found(this);
1592 1593
    NumberDictionaryLookup(elements, intptr_index, &if_found, &var_entry,
                           if_hole);
1594
    BIND(&if_found);
1595
    // Check that the value is a data property.
1596 1597
    Node* index = EntryToIndex<NumberDictionary>(var_entry.value());
    Node* details = LoadDetailsByKeyIndex<NumberDictionary>(elements, index);
1598 1599
    Node* kind = DecodeWord32<PropertyDetails::KindField>(details);
    // TODO(jkummerow): Support accessors without missing?
1600
    GotoIfNot(Word32Equal(kind, Int32Constant(kData)), miss);
1601
    // Finally, load the value.
1602
    exit_point->Return(LoadValueByKeyIndex<NumberDictionary>(elements, index));
1603 1604
  }

1605
  BIND(&if_typed_array);
1606 1607 1608 1609
  {
    Comment("typed elements");
    // Check if buffer has been neutered.
    Node* buffer = LoadObjectField(object, JSArrayBufferView::kBufferOffset);
1610
    GotoIf(IsDetachedBuffer(buffer), miss);
1611 1612 1613

    // Bounds check.
    Node* length =
1614
        SmiUntag(CAST(LoadObjectField(object, JSTypedArray::kLengthOffset)));
1615
    GotoIfNot(UintPtrLessThan(intptr_index, length), out_of_bounds);
1616 1617 1618 1619 1620 1621

    // Backing store = external_pointer + base_pointer.
    Node* external_pointer =
        LoadObjectField(elements, FixedTypedArrayBase::kExternalPointerOffset,
                        MachineType::Pointer());
    Node* base_pointer =
1622 1623 1624
        LoadObjectField(elements, FixedTypedArrayBase::kBasePointerOffset);
    Node* backing_store =
        IntPtrAdd(external_pointer, BitcastTaggedToWord(base_pointer));
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641

    Label uint8_elements(this), int8_elements(this), uint16_elements(this),
        int16_elements(this), uint32_elements(this), int32_elements(this),
        float32_elements(this), float64_elements(this);
    Label* elements_kind_labels[] = {
        &uint8_elements,  &uint8_elements,   &int8_elements,
        &uint16_elements, &int16_elements,   &uint32_elements,
        &int32_elements,  &float32_elements, &float64_elements};
    int32_t elements_kinds[] = {
        UINT8_ELEMENTS,  UINT8_CLAMPED_ELEMENTS, INT8_ELEMENTS,
        UINT16_ELEMENTS, INT16_ELEMENTS,         UINT32_ELEMENTS,
        INT32_ELEMENTS,  FLOAT32_ELEMENTS,       FLOAT64_ELEMENTS};
    const size_t kTypedElementsKindCount =
        LAST_FIXED_TYPED_ARRAY_ELEMENTS_KIND -
        FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND + 1;
    DCHECK_EQ(kTypedElementsKindCount, arraysize(elements_kinds));
    DCHECK_EQ(kTypedElementsKindCount, arraysize(elements_kind_labels));
1642 1643
    Switch(elements_kind, miss, elements_kinds, elements_kind_labels,
           kTypedElementsKindCount);
1644
    BIND(&uint8_elements);
1645 1646
    {
      Comment("UINT8_ELEMENTS");  // Handles UINT8_CLAMPED_ELEMENTS too.
1647
      Node* element = Load(MachineType::Uint8(), backing_store, intptr_index);
1648
      exit_point->Return(SmiFromWord32(element));
1649
    }
1650
    BIND(&int8_elements);
1651 1652
    {
      Comment("INT8_ELEMENTS");
1653
      Node* element = Load(MachineType::Int8(), backing_store, intptr_index);
1654
      exit_point->Return(SmiFromWord32(element));
1655
    }
1656
    BIND(&uint16_elements);
1657 1658 1659
    {
      Comment("UINT16_ELEMENTS");
      Node* index = WordShl(intptr_index, IntPtrConstant(1));
1660
      Node* element = Load(MachineType::Uint16(), backing_store, index);
1661
      exit_point->Return(SmiFromWord32(element));
1662
    }
1663
    BIND(&int16_elements);
1664 1665 1666
    {
      Comment("INT16_ELEMENTS");
      Node* index = WordShl(intptr_index, IntPtrConstant(1));
1667
      Node* element = Load(MachineType::Int16(), backing_store, index);
1668
      exit_point->Return(SmiFromWord32(element));
1669
    }
1670
    BIND(&uint32_elements);
1671 1672 1673 1674
    {
      Comment("UINT32_ELEMENTS");
      Node* index = WordShl(intptr_index, IntPtrConstant(2));
      Node* element = Load(MachineType::Uint32(), backing_store, index);
1675
      exit_point->Return(ChangeUint32ToTagged(element));
1676
    }
1677
    BIND(&int32_elements);
1678 1679 1680 1681
    {
      Comment("INT32_ELEMENTS");
      Node* index = WordShl(intptr_index, IntPtrConstant(2));
      Node* element = Load(MachineType::Int32(), backing_store, index);
1682
      exit_point->Return(ChangeInt32ToTagged(element));
1683
    }
1684
    BIND(&float32_elements);
1685 1686 1687 1688 1689 1690 1691
    {
      Comment("FLOAT32_ELEMENTS");
      Node* index = WordShl(intptr_index, IntPtrConstant(2));
      Node* element = Load(MachineType::Float32(), backing_store, index);
      var_double_value->Bind(ChangeFloat32ToFloat64(element));
      Goto(rebox_double);
    }
1692
    BIND(&float64_elements);
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
    {
      Comment("FLOAT64_ELEMENTS");
      Node* index = WordShl(intptr_index, IntPtrConstant(3));
      Node* element = Load(MachineType::Float64(), backing_store, index);
      var_double_value->Bind(element);
      Goto(rebox_double);
    }
  }
}

1703 1704
void AccessorAssembler::NameDictionaryNegativeLookup(Node* object, Node* name,
                                                     Label* miss) {
1705
  CSA_ASSERT(this, IsDictionaryMap(LoadMap(object)));
1706
  Node* properties = LoadSlowProperties(object);
1707
  // Ensure the property does not exist in a dictionary-mode object.
1708
  VARIABLE(var_name_index, MachineType::PointerRepresentation());
1709 1710 1711
  Label done(this);
  NameDictionaryLookup<NameDictionary>(properties, name, miss, &var_name_index,
                                       &done);
1712
  BIND(&done);
1713 1714
}

1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
void AccessorAssembler::BranchIfStrictMode(Node* vector, Node* slot,
                                           Label* if_strict) {
  Node* sfi =
      LoadObjectField(vector, FeedbackVector::kSharedFunctionInfoOffset);
  Node* metadata =
      LoadObjectField(sfi, SharedFunctionInfo::kFeedbackMetadataOffset);
  Node* slot_int = SmiToWord32(slot);

  // See VectorICComputer::index().
  const int kItemsPerWord = FeedbackMetadata::VectorICComputer::kItemsPerWord;
  Node* word_index = Int32Div(slot_int, Int32Constant(kItemsPerWord));
  Node* word_offset = Int32Mod(slot_int, Int32Constant(kItemsPerWord));
  Node* data = SmiToWord32(LoadFixedArrayElement(
      metadata, ChangeInt32ToIntPtr(word_index),
      FeedbackMetadata::kReservedIndexCount * kPointerSize, INTPTR_PARAMETERS));
  // See VectorICComputer::decode().
  const int kBitsPerItem = FeedbackMetadata::kFeedbackSlotKindBits;
  Node* shift = Int32Mul(word_offset, Int32Constant(kBitsPerItem));
  const int kMask = FeedbackMetadata::VectorICComputer::kMask;
  Node* kind = Word32And(Word32Shr(data, shift), Int32Constant(kMask));

  STATIC_ASSERT(FeedbackSlotKind::kStoreGlobalSloppy <=
                FeedbackSlotKind::kLastSloppyKind);
  STATIC_ASSERT(FeedbackSlotKind::kStoreKeyedSloppy <=
                FeedbackSlotKind::kLastSloppyKind);
  STATIC_ASSERT(FeedbackSlotKind::kStoreNamedSloppy <=
                FeedbackSlotKind::kLastSloppyKind);
  GotoIfNot(Int32LessThanOrEqual(kind, Int32Constant(static_cast<int>(
                                           FeedbackSlotKind::kLastSloppyKind))),
            if_strict);
}

1747 1748 1749 1750 1751 1752 1753
void AccessorAssembler::InvalidateValidityCellIfPrototype(Node* map,
                                                          Node* bitfield2) {
  Label is_prototype(this), cont(this);
  if (bitfield2 == nullptr) {
    bitfield2 = LoadMapBitField2(map);
  }

1754
  Branch(IsSetWord32(bitfield2, Map::IsPrototypeMapBit::kMask), &is_prototype,
1755 1756 1757
         &cont);

  BIND(&is_prototype);
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
  {
    Node* maybe_prototype_info =
        LoadObjectField(map, Map::kTransitionsOrPrototypeInfoOffset);
    // If there's no prototype info then there's nothing to invalidate.
    GotoIf(TaggedIsSmi(maybe_prototype_info), &cont);

    Node* function = ExternalConstant(
        ExternalReference::invalidate_prototype_chains_function(isolate()));
    CallCFunction1(MachineType::AnyTagged(), MachineType::AnyTagged(), function,
                   map);
    Goto(&cont);
  }
1770 1771 1772
  BIND(&cont);
}

1773 1774 1775 1776
void AccessorAssembler::GenericElementLoad(Node* receiver, Node* receiver_map,
                                           Node* instance_type, Node* index,
                                           Label* slow) {
  Comment("integer index");
1777 1778 1779

  ExitPoint direct_exit(this);

1780
  Label if_custom(this), if_element_hole(this), if_oob(this);
1781 1782 1783 1784
  // Receivers requiring non-standard element accesses (interceptors, access
  // checks, strings and string wrappers, proxies) are handled in the runtime.
  GotoIf(Int32LessThanOrEqual(instance_type,
                              Int32Constant(LAST_CUSTOM_ELEMENTS_RECEIVER)),
1785
         &if_custom);
1786 1787
  Node* elements = LoadElements(receiver);
  Node* elements_kind = LoadMapElementsKind(receiver_map);
1788
  Node* is_jsarray_condition = InstanceTypeEqual(instance_type, JS_ARRAY_TYPE);
1789
  VARIABLE(var_double_value, MachineRepresentation::kFloat64);
1790 1791 1792 1793 1794 1795 1796
  Label rebox_double(this, &var_double_value);

  // Unimplemented elements kinds fall back to a runtime call.
  Label* unimplemented_elements_kind = slow;
  IncrementCounter(isolate()->counters()->ic_keyed_load_generic_smi(), 1);
  EmitElementLoad(receiver, elements, elements_kind, index,
                  is_jsarray_condition, &if_element_hole, &rebox_double,
1797 1798
                  &var_double_value, unimplemented_elements_kind, &if_oob, slow,
                  &direct_exit);
1799

1800
  BIND(&rebox_double);
1801 1802
  Return(AllocateHeapNumberWithValue(var_double_value.value()));

1803
  BIND(&if_oob);
1804 1805 1806 1807 1808 1809 1810 1811
  {
    Comment("out of bounds");
    // Negative keys can't take the fast OOB path.
    GotoIf(IntPtrLessThan(index, IntPtrConstant(0)), slow);
    // Positive OOB indices are effectively the same as hole loads.
    Goto(&if_element_hole);
  }

1812
  BIND(&if_element_hole);
1813 1814 1815 1816 1817
  {
    Comment("found the hole");
    Label return_undefined(this);
    BranchIfPrototypesHaveNoElements(receiver_map, &return_undefined, slow);

1818
    BIND(&return_undefined);
1819 1820
    Return(UndefinedConstant());
  }
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832

  BIND(&if_custom);
  {
    Comment("check if string");
    GotoIfNot(IsStringInstanceType(instance_type), slow);
    Comment("load string character");
    Node* length = LoadAndUntagObjectField(receiver, String::kLengthOffset);
    GotoIfNot(UintPtrLessThan(index, length), slow);
    IncrementCounter(isolate()->counters()->ic_keyed_load_generic_smi(), 1);
    TailCallBuiltin(Builtins::kStringCharAt, NoContextConstant(), receiver,
                    index);
  }
1833 1834 1835
}

void AccessorAssembler::GenericPropertyLoad(Node* receiver, Node* receiver_map,
1836
                                            Node* instance_type,
1837
                                            const LoadICParameters* p,
1838 1839
                                            Label* slow,
                                            UseStubCache use_stub_cache) {
1840 1841
  ExitPoint direct_exit(this);

1842 1843
  Comment("key is unique name");
  Label if_found_on_receiver(this), if_property_dictionary(this),
1844
      lookup_prototype_chain(this), special_receiver(this);
1845 1846
  VARIABLE(var_details, MachineRepresentation::kWord32);
  VARIABLE(var_value, MachineRepresentation::kTagged);
1847 1848

  // Receivers requiring non-standard accesses (interceptors, access
1849
  // checks, strings and string wrappers) are handled in the runtime.
1850 1851
  GotoIf(Int32LessThanOrEqual(instance_type,
                              Int32Constant(LAST_SPECIAL_RECEIVER_TYPE)),
1852
         &special_receiver);
1853

1854
  // Check if the receiver has fast or slow properties.
1855
  Node* bitfield3 = LoadMapBitField3(receiver_map);
1856 1857
  GotoIf(IsSetWord32<Map::IsDictionaryMapBit>(bitfield3),
         &if_property_dictionary);
1858 1859 1860

  // Try looking up the property on the receiver; if unsuccessful, look
  // for a handler in the stub cache.
1861
  Node* descriptors = LoadMapDescriptors(receiver_map);
1862 1863

  Label if_descriptor_found(this), stub_cache(this);
1864
  VARIABLE(var_name_index, MachineType::PointerRepresentation());
1865 1866
  Label* notfound =
      use_stub_cache == kUseStubCache ? &stub_cache : &lookup_prototype_chain;
1867
  DescriptorLookup(p->name, descriptors, bitfield3, &if_descriptor_found,
1868
                   &var_name_index, notfound);
1869

1870
  BIND(&if_descriptor_found);
1871
  {
1872
    LoadPropertyFromFastObject(receiver, receiver_map, descriptors,
1873 1874 1875 1876 1877
                               var_name_index.value(), &var_details,
                               &var_value);
    Goto(&if_found_on_receiver);
  }

1878
  if (use_stub_cache == kUseStubCache) {
1879
    BIND(&stub_cache);
1880
    Comment("stub cache probe for fast property load");
1881
    VARIABLE(var_handler, MachineRepresentation::kTagged);
1882
    Label found_handler(this, &var_handler), stub_cache_miss(this);
1883
    TryProbeStubCache(isolate()->load_stub_cache(), receiver, p->name,
1884
                      &found_handler, &var_handler, &stub_cache_miss);
1885
    BIND(&found_handler);
1886 1887 1888 1889
    {
      HandleLoadICHandlerCase(p, var_handler.value(), &stub_cache_miss,
                              &direct_exit);
    }
1890

1891
    BIND(&stub_cache_miss);
1892 1893 1894 1895 1896 1897 1898 1899 1900
    {
      // TODO(jkummerow): Check if the property exists on the prototype
      // chain. If it doesn't, then there's no point in missing.
      Comment("KeyedLoadGeneric_miss");
      TailCallRuntime(Runtime::kKeyedLoadIC_Miss, p->context, p->receiver,
                      p->name, p->slot, p->vector);
    }
  }

1901
  BIND(&if_property_dictionary);
1902 1903 1904 1905 1906
  {
    Comment("dictionary property load");
    // We checked for LAST_CUSTOM_ELEMENTS_RECEIVER before, which rules out
    // seeing global objects here (which would need special handling).

1907
    VARIABLE(var_name_index, MachineType::PointerRepresentation());
1908
    Label dictionary_found(this, &var_name_index);
1909
    Node* properties = LoadSlowProperties(receiver);
1910
    NameDictionaryLookup<NameDictionary>(properties, p->name, &dictionary_found,
1911 1912
                                         &var_name_index,
                                         &lookup_prototype_chain);
1913
    BIND(&dictionary_found);
1914 1915 1916 1917 1918 1919 1920
    {
      LoadPropertyFromNameDictionary(properties, var_name_index.value(),
                                     &var_details, &var_value);
      Goto(&if_found_on_receiver);
    }
  }

1921
  BIND(&if_found_on_receiver);
1922 1923
  {
    Node* value = CallGetterIfAccessor(var_value.value(), var_details.value(),
1924
                                       p->context, receiver, slow);
1925 1926 1927 1928
    IncrementCounter(isolate()->counters()->ic_keyed_load_generic_symbol(), 1);
    Return(value);
  }

1929
  BIND(&lookup_prototype_chain);
1930
  {
1931 1932
    VARIABLE(var_holder_map, MachineRepresentation::kTagged);
    VARIABLE(var_holder_instance_type, MachineRepresentation::kWord32);
1933 1934 1935 1936
    Label return_undefined(this);
    Variable* merged_variables[] = {&var_holder_map, &var_holder_instance_type};
    Label loop(this, arraysize(merged_variables), merged_variables);

1937 1938
    var_holder_map.Bind(receiver_map);
    var_holder_instance_type.Bind(instance_type);
1939
    // Private symbols must not be looked up on the prototype chain.
1940
    GotoIf(IsPrivateSymbol(p->name), &return_undefined);
1941
    Goto(&loop);
1942
    BIND(&loop);
1943 1944
    {
      // Bailout if it can be an integer indexed exotic case.
1945 1946
      GotoIf(InstanceTypeEqual(var_holder_instance_type.value(),
                               JS_TYPED_ARRAY_TYPE),
1947 1948 1949 1950 1951 1952 1953 1954
             slow);
      Node* proto = LoadMapPrototype(var_holder_map.value());
      GotoIf(WordEqual(proto, NullConstant()), &return_undefined);
      Node* proto_map = LoadMap(proto);
      Node* proto_instance_type = LoadMapInstanceType(proto_map);
      var_holder_map.Bind(proto_map);
      var_holder_instance_type.Bind(proto_instance_type);
      Label next_proto(this), return_value(this, &var_value), goto_slow(this);
1955
      TryGetOwnProperty(p->context, receiver, proto, proto_map,
1956
                        proto_instance_type, p->name, &return_value, &var_value,
1957 1958 1959 1960
                        &next_proto, &goto_slow);

      // This trampoline and the next are required to appease Turbofan's
      // variable merging.
1961
      BIND(&next_proto);
1962 1963
      Goto(&loop);

1964
      BIND(&goto_slow);
1965 1966
      Goto(slow);

1967
      BIND(&return_value);
1968 1969 1970
      Return(var_value.value());
    }

1971
    BIND(&return_undefined);
1972 1973
    Return(UndefinedConstant());
  }
1974 1975 1976

  BIND(&special_receiver);
  {
1977
    // TODO(jkummerow): Consider supporting JSModuleNamespace.
1978
    GotoIfNot(InstanceTypeEqual(instance_type, JS_PROXY_TYPE), slow);
1979 1980 1981 1982 1983 1984

    direct_exit.ReturnCallStub(
        Builtins::CallableFor(isolate(), Builtins::kProxyGetProperty),
        p->context, receiver /*holder is the same as receiver*/, p->name,
        receiver);
  }
1985 1986
}

1987 1988
//////////////////// Stub cache access helpers.

1989
enum AccessorAssembler::StubCacheTable : int {
1990 1991 1992 1993
  kPrimary = static_cast<int>(StubCache::kPrimary),
  kSecondary = static_cast<int>(StubCache::kSecondary)
};

1994
Node* AccessorAssembler::StubCachePrimaryOffset(Node* name, Node* map) {
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
  // See v8::internal::StubCache::PrimaryOffset().
  STATIC_ASSERT(StubCache::kCacheIndexShift == Name::kHashShift);
  // Compute the hash of the name (use entire hash field).
  Node* hash_field = LoadNameHashField(name);
  CSA_ASSERT(this,
             Word32Equal(Word32And(hash_field,
                                   Int32Constant(Name::kHashNotComputedMask)),
                         Int32Constant(0)));

  // Using only the low bits in 64-bit mode is unlikely to increase the
  // risk of collision even if the heap is spread over an area larger than
  // 4Gb (and not at all if it isn't).
2007
  Node* map32 = TruncateWordToWord32(BitcastTaggedToWord(map));
2008
  // Base the offset on a simple combination of name and map.
2009
  Node* hash = Int32Add(hash_field, map32);
2010 2011 2012 2013 2014
  uint32_t mask = (StubCache::kPrimaryTableSize - 1)
                  << StubCache::kCacheIndexShift;
  return ChangeUint32ToWord(Word32And(hash, Int32Constant(mask)));
}

2015
Node* AccessorAssembler::StubCacheSecondaryOffset(Node* name, Node* seed) {
2016 2017 2018
  // See v8::internal::StubCache::SecondaryOffset().

  // Use the seed from the primary cache in the secondary cache.
2019 2020
  Node* name32 = TruncateWordToWord32(BitcastTaggedToWord(name));
  Node* hash = Int32Sub(TruncateWordToWord32(seed), name32);
2021 2022 2023 2024 2025 2026
  hash = Int32Add(hash, Int32Constant(StubCache::kSecondaryMagic));
  int32_t mask = (StubCache::kSecondaryTableSize - 1)
                 << StubCache::kCacheIndexShift;
  return ChangeUint32ToWord(Word32And(hash, Int32Constant(mask)));
}

2027 2028 2029 2030 2031 2032
void AccessorAssembler::TryProbeStubCacheTable(StubCache* stub_cache,
                                               StubCacheTable table_id,
                                               Node* entry_offset, Node* name,
                                               Node* map, Label* if_handler,
                                               Variable* var_handler,
                                               Label* if_miss) {
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
  StubCache::Table table = static_cast<StubCache::Table>(table_id);
#ifdef DEBUG
  if (FLAG_test_secondary_stub_cache && table == StubCache::kPrimary) {
    Goto(if_miss);
    return;
  } else if (FLAG_test_primary_stub_cache && table == StubCache::kSecondary) {
    Goto(if_miss);
    return;
  }
#endif
  // The {table_offset} holds the entry offset times four (due to masking
  // and shifting optimizations).
  const int kMultiplier = sizeof(StubCache::Entry) >> Name::kHashShift;
  entry_offset = IntPtrMul(entry_offset, IntPtrConstant(kMultiplier));

  // Check that the key in the entry matches the name.
  Node* key_base =
      ExternalConstant(ExternalReference(stub_cache->key_reference(table)));
  Node* entry_key = Load(MachineType::Pointer(), key_base, entry_offset);
  GotoIf(WordNotEqual(name, entry_key), if_miss);

  // Get the map entry from the cache.
  DCHECK_EQ(kPointerSize * 2, stub_cache->map_reference(table).address() -
                                  stub_cache->key_reference(table).address());
  Node* entry_map =
      Load(MachineType::Pointer(), key_base,
           IntPtrAdd(entry_offset, IntPtrConstant(kPointerSize * 2)));
  GotoIf(WordNotEqual(map, entry_map), if_miss);

  DCHECK_EQ(kPointerSize, stub_cache->value_reference(table).address() -
                              stub_cache->key_reference(table).address());
  Node* handler = Load(MachineType::TaggedPointer(), key_base,
                       IntPtrAdd(entry_offset, IntPtrConstant(kPointerSize)));

  // We found the handler.
  var_handler->Bind(handler);
  Goto(if_handler);
}

2072 2073 2074 2075
void AccessorAssembler::TryProbeStubCache(StubCache* stub_cache, Node* receiver,
                                          Node* name, Label* if_handler,
                                          Variable* var_handler,
                                          Label* if_miss) {
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
  Label try_secondary(this), miss(this);

  Counters* counters = isolate()->counters();
  IncrementCounter(counters->megamorphic_stub_cache_probes(), 1);

  // Check that the {receiver} isn't a smi.
  GotoIf(TaggedIsSmi(receiver), &miss);

  Node* receiver_map = LoadMap(receiver);

  // Probe the primary table.
  Node* primary_offset = StubCachePrimaryOffset(name, receiver_map);
  TryProbeStubCacheTable(stub_cache, kPrimary, primary_offset, name,
                         receiver_map, if_handler, var_handler, &try_secondary);

2091
  BIND(&try_secondary);
2092 2093 2094 2095 2096 2097 2098
  {
    // Probe the secondary table.
    Node* secondary_offset = StubCacheSecondaryOffset(name, primary_offset);
    TryProbeStubCacheTable(stub_cache, kSecondary, secondary_offset, name,
                           receiver_map, if_handler, var_handler, &miss);
  }

2099
  BIND(&miss);
2100 2101 2102 2103 2104 2105 2106 2107
  {
    IncrementCounter(counters->megamorphic_stub_cache_misses(), 1);
    Goto(if_miss);
  }
}

//////////////////// Entry points into private implementation (one per stub).

2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
void AccessorAssembler::LoadIC_BytecodeHandler(const LoadICParameters* p,
                                               ExitPoint* exit_point) {
  // Must be kept in sync with LoadIC.

  // This function is hand-tuned to omit frame construction for common cases,
  // e.g.: monomorphic field and constant loads through smi handlers.
  // Polymorphic ICs with a hit in the first two entries also omit frames.
  // TODO(jgruber): Frame omission is fragile and can be affected by minor
  // changes in control flow and logic. We currently have no way of ensuring
  // that no frame is constructed, so it's easy to break this optimization by
  // accident.
  Label stub_call(this, Label::kDeferred), miss(this, Label::kDeferred);

  // Inlined fast path.
  {
    Comment("LoadIC_BytecodeHandler_fast");

    Node* recv_map = LoadReceiverMap(p->receiver);
    GotoIf(IsDeprecatedMap(recv_map), &miss);

2128
    VARIABLE(var_handler, MachineRepresentation::kTagged);
2129 2130 2131 2132 2133 2134
    Label try_polymorphic(this), if_handler(this, &var_handler);

    Node* feedback =
        TryMonomorphicCase(p->slot, p->vector, recv_map, &if_handler,
                           &var_handler, &try_polymorphic);

2135
    BIND(&if_handler);
2136 2137
    HandleLoadICHandlerCase(p, var_handler.value(), &miss, exit_point);

2138
    BIND(&try_polymorphic);
2139 2140 2141 2142 2143 2144 2145 2146
    {
      GotoIfNot(WordEqual(LoadMap(feedback), FixedArrayMapConstant()),
                &stub_call);
      HandlePolymorphicCase(recv_map, feedback, &if_handler, &var_handler,
                            &miss, 2);
    }
  }

2147
  BIND(&stub_call);
2148 2149 2150 2151
  {
    Comment("LoadIC_BytecodeHandler_noninlined");

    // Call into the stub that implements the non-inlined parts of LoadIC.
2152 2153
    Callable ic =
        Builtins::CallableFor(isolate(), Builtins::kLoadIC_Noninlined);
2154 2155 2156 2157 2158
    Node* code_target = HeapConstant(ic.code());
    exit_point->ReturnCallStub(ic.descriptor(), code_target, p->context,
                               p->receiver, p->name, p->slot, p->vector);
  }

2159
  BIND(&miss);
2160 2161 2162 2163 2164 2165 2166 2167
  {
    Comment("LoadIC_BytecodeHandler_miss");

    exit_point->ReturnCallRuntime(Runtime::kLoadIC_Miss, p->context,
                                  p->receiver, p->name, p->slot, p->vector);
  }
}

2168
void AccessorAssembler::LoadIC(const LoadICParameters* p) {
2169 2170 2171 2172
  // Must be kept in sync with LoadIC_BytecodeHandler.

  ExitPoint direct_exit(this);

2173
  VARIABLE(var_handler, MachineRepresentation::kTagged);
2174 2175
  Label if_handler(this, &var_handler), non_inlined(this, Label::kDeferred),
      try_polymorphic(this), miss(this, Label::kDeferred);
2176 2177

  Node* receiver_map = LoadReceiverMap(p->receiver);
2178
  GotoIf(IsDeprecatedMap(receiver_map), &miss);
2179 2180 2181 2182 2183

  // Check monomorphic case.
  Node* feedback =
      TryMonomorphicCase(p->slot, p->vector, receiver_map, &if_handler,
                         &var_handler, &try_polymorphic);
2184
  BIND(&if_handler);
2185
  HandleLoadICHandlerCase(p, var_handler.value(), &miss, &direct_exit);
2186

2187
  BIND(&try_polymorphic);
2188 2189 2190
  {
    // Check polymorphic case.
    Comment("LoadIC_try_polymorphic");
2191
    GotoIfNot(WordEqual(LoadMap(feedback), FixedArrayMapConstant()),
2192
              &non_inlined);
2193 2194 2195 2196
    HandlePolymorphicCase(receiver_map, feedback, &if_handler, &var_handler,
                          &miss, 2);
  }

2197
  BIND(&non_inlined);
2198 2199 2200
  LoadIC_Noninlined(p, receiver_map, feedback, &var_handler, &if_handler, &miss,
                    &direct_exit);

2201
  BIND(&miss);
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
  direct_exit.ReturnCallRuntime(Runtime::kLoadIC_Miss, p->context, p->receiver,
                                p->name, p->slot, p->vector);
}

void AccessorAssembler::LoadIC_Noninlined(const LoadICParameters* p,
                                          Node* receiver_map, Node* feedback,
                                          Variable* var_handler,
                                          Label* if_handler, Label* miss,
                                          ExitPoint* exit_point) {
  Label try_uninitialized(this, Label::kDeferred);

  // Neither deprecated map nor monomorphic. These cases are handled in the
  // bytecode handler.
  CSA_ASSERT(this, Word32BinaryNot(IsDeprecatedMap(receiver_map)));
  CSA_ASSERT(this,
             WordNotEqual(receiver_map, LoadWeakCellValueUnchecked(feedback)));
  CSA_ASSERT(this, WordNotEqual(LoadMap(feedback), FixedArrayMapConstant()));
  DCHECK_EQ(MachineRepresentation::kTagged, var_handler->rep());

2221 2222
  {
    // Check megamorphic case.
2223
    GotoIfNot(WordEqual(feedback, LoadRoot(Heap::kmegamorphic_symbolRootIndex)),
2224
              &try_uninitialized);
2225 2226

    TryProbeStubCache(isolate()->load_stub_cache(), p->receiver, p->name,
2227
                      if_handler, var_handler, miss);
2228
  }
2229

2230
  BIND(&try_uninitialized);
2231 2232 2233 2234
  {
    // Check uninitialized case.
    GotoIfNot(
        WordEqual(feedback, LoadRoot(Heap::kuninitialized_symbolRootIndex)),
2235
        miss);
2236 2237 2238
    exit_point->ReturnCallStub(
        Builtins::CallableFor(isolate(), Builtins::kLoadIC_Uninitialized),
        p->context, p->receiver, p->name, p->slot, p->vector);
2239 2240 2241 2242
  }
}

void AccessorAssembler::LoadIC_Uninitialized(const LoadICParameters* p) {
2243
  Label miss(this, Label::kDeferred);
2244 2245 2246 2247 2248 2249
  Node* receiver = p->receiver;
  GotoIf(TaggedIsSmi(receiver), &miss);
  Node* receiver_map = LoadMap(receiver);
  Node* instance_type = LoadMapInstanceType(receiver_map);

  // Optimistically write the state transition to the vector.
2250 2251 2252
  StoreFeedbackVectorSlot(p->vector, p->slot,
                          LoadRoot(Heap::kpremonomorphic_symbolRootIndex),
                          SKIP_WRITE_BARRIER, 0, SMI_PARAMETERS);
2253

2254 2255 2256
  {
    // Special case for Function.prototype load, because it's very common
    // for ICs that are only executed once (MyFunc.prototype.foo = ...).
2257 2258 2259
    Label not_function_prototype(this, Label::kDeferred);
    GotoIfNot(InstanceTypeEqual(instance_type, JS_FUNCTION_TYPE),
              &not_function_prototype);
2260
    GotoIfNot(IsPrototypeString(p->name), &not_function_prototype);
2261 2262 2263 2264

    // if (!(has_prototype_slot() && !has_non_instance_prototype())) use generic
    // property loading mechanism.
    GotoIfNot(
2265 2266 2267 2268 2269
        Word32Equal(
            Word32And(LoadMapBitField(receiver_map),
                      Int32Constant(Map::HasPrototypeSlotBit::kMask |
                                    Map::HasNonInstancePrototypeBit::kMask)),
            Int32Constant(Map::HasPrototypeSlotBit::kMask)),
2270
        &not_function_prototype);
2271 2272 2273 2274
    Return(LoadJSFunctionPrototype(receiver, &miss));
    BIND(&not_function_prototype);
  }

2275
  GenericPropertyLoad(receiver, receiver_map, instance_type, p, &miss,
2276 2277
                      kDontUseStubCache);

2278
  BIND(&miss);
2279
  {
2280
    // Undo the optimistic state transition.
2281 2282 2283
    StoreFeedbackVectorSlot(p->vector, p->slot,
                            LoadRoot(Heap::kuninitialized_symbolRootIndex),
                            SKIP_WRITE_BARRIER, 0, SMI_PARAMETERS);
2284

2285 2286 2287 2288 2289
    TailCallRuntime(Runtime::kLoadIC_Miss, p->context, p->receiver, p->name,
                    p->slot, p->vector);
  }
}

2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
void AccessorAssembler::LoadGlobalIC(TNode<FeedbackVector> vector, Node* slot,
                                     const LazyNode<Context>& lazy_context,
                                     const LazyNode<Name>& lazy_name,
                                     TypeofMode typeof_mode,
                                     ExitPoint* exit_point,
                                     ParameterMode slot_mode) {
  Label try_handler(this, Label::kDeferred), miss(this, Label::kDeferred);
  LoadGlobalIC_TryPropertyCellCase(vector, slot, lazy_context, exit_point,
                                   &try_handler, &miss, slot_mode);

  BIND(&try_handler);
  LoadGlobalIC_TryHandlerCase(vector, slot, lazy_context, lazy_name,
                              typeof_mode, exit_point, &miss, slot_mode);

  BIND(&miss);
  {
    Comment("LoadGlobalIC_MissCase");
    TNode<Context> context = lazy_context();
    TNode<Name> name = lazy_name();
    exit_point->ReturnCallRuntime(Runtime::kLoadGlobalIC_Miss, context, name,
                                  ParameterToTagged(slot, slot_mode), vector);
  }
}

2314
void AccessorAssembler::LoadGlobalIC_TryPropertyCellCase(
2315 2316 2317
    TNode<FeedbackVector> vector, Node* slot,
    const LazyNode<Context>& lazy_context, ExitPoint* exit_point,
    Label* try_handler, Label* miss, ParameterMode slot_mode) {
2318 2319
  Comment("LoadGlobalIC_TryPropertyCellCase");

2320
  Label if_lexical_var(this), if_property_cell(this);
2321 2322
  TNode<Object> maybe_weak_cell =
      LoadFeedbackVectorSlot(vector, slot, 0, slot_mode);
2323
  Branch(TaggedIsSmi(maybe_weak_cell), &if_lexical_var, &if_property_cell);
2324

2325 2326
  BIND(&if_property_cell);
  {
2327
    TNode<WeakCell> weak_cell = CAST(maybe_weak_cell);
2328 2329

    // Load value or try handler case if the {weak_cell} is cleared.
2330 2331 2332 2333
    TNode<PropertyCell> property_cell =
        CAST(LoadWeakCellValue(weak_cell, try_handler));
    TNode<Object> value =
        LoadObjectField(property_cell, PropertyCell::kValueOffset);
2334 2335 2336 2337 2338 2339 2340
    GotoIf(WordEqual(value, TheHoleConstant()), miss);
    exit_point->Return(value);
  }

  BIND(&if_lexical_var);
  {
    Comment("Load lexical variable");
2341
    TNode<IntPtrT> lexical_handler = SmiUntag(CAST(maybe_weak_cell));
2342 2343 2344 2345
    TNode<IntPtrT> context_index =
        Signed(DecodeWord<GlobalICNexus::ContextIndexBits>(lexical_handler));
    TNode<IntPtrT> slot_index =
        Signed(DecodeWord<GlobalICNexus::SlotIndexBits>(lexical_handler));
2346
    TNode<Context> context = lazy_context();
2347 2348 2349 2350
    TNode<Context> script_context = LoadScriptContext(context, context_index);
    TNode<Object> result = LoadContextElement(script_context, slot_index);
    exit_point->Return(result);
  }
2351
}
2352

2353 2354 2355 2356 2357
void AccessorAssembler::LoadGlobalIC_TryHandlerCase(
    TNode<FeedbackVector> vector, Node* slot,
    const LazyNode<Context>& lazy_context, const LazyNode<Name>& lazy_name,
    TypeofMode typeof_mode, ExitPoint* exit_point, Label* miss,
    ParameterMode slot_mode) {
2358
  Comment("LoadGlobalIC_TryHandlerCase");
2359

2360
  Label call_handler(this), non_smi(this);
2361

2362
  Node* handler = LoadFeedbackVectorSlot(vector, slot, kPointerSize, slot_mode);
2363 2364
  GotoIf(WordEqual(handler, LoadRoot(Heap::kuninitialized_symbolRootIndex)),
         miss);
2365

2366 2367 2368
  OnNonExistent on_nonexistent = typeof_mode == NOT_INSIDE_TYPEOF
                                     ? OnNonExistent::kThrowReferenceError
                                     : OnNonExistent::kReturnUndefined;
2369

2370 2371 2372 2373 2374
  TNode<Context> context = lazy_context();
  TNode<Context> native_context = LoadNativeContext(context);
  TNode<JSGlobalProxy> receiver =
      CAST(LoadContextElement(native_context, Context::GLOBAL_PROXY_INDEX));
  Node* holder = LoadContextElement(native_context, Context::EXTENSION_INDEX);
2375

2376 2377
  LoadICParameters p(context, receiver, lazy_name(),
                     ParameterToTagged(slot, slot_mode), vector, holder);
2378

2379 2380
  HandleLoadICHandlerCase(&p, handler, miss, exit_point, ICMode::kGlobalIC,
                          on_nonexistent);
2381 2382
}

2383
void AccessorAssembler::KeyedLoadIC(const LoadICParameters* p) {
2384 2385
  ExitPoint direct_exit(this);

2386
  VARIABLE(var_handler, MachineRepresentation::kTagged);
jgruber's avatar
jgruber committed
2387 2388 2389 2390
  Label if_handler(this, &var_handler), try_polymorphic(this, Label::kDeferred),
      try_megamorphic(this, Label::kDeferred),
      try_polymorphic_name(this, Label::kDeferred),
      miss(this, Label::kDeferred);
2391 2392

  Node* receiver_map = LoadReceiverMap(p->receiver);
2393
  GotoIf(IsDeprecatedMap(receiver_map), &miss);
2394 2395 2396 2397 2398

  // Check monomorphic case.
  Node* feedback =
      TryMonomorphicCase(p->slot, p->vector, receiver_map, &if_handler,
                         &var_handler, &try_polymorphic);
2399
  BIND(&if_handler);
2400 2401
  {
    HandleLoadICHandlerCase(p, var_handler.value(), &miss, &direct_exit,
2402 2403
                            ICMode::kNonGlobalIC,
                            OnNonExistent::kReturnUndefined, kSupportElements);
2404
  }
2405

2406
  BIND(&try_polymorphic);
2407 2408 2409
  {
    // Check polymorphic case.
    Comment("KeyedLoadIC_try_polymorphic");
2410 2411
    GotoIfNot(WordEqual(LoadMap(feedback), FixedArrayMapConstant()),
              &try_megamorphic);
2412 2413 2414 2415
    HandlePolymorphicCase(receiver_map, feedback, &if_handler, &var_handler,
                          &miss, 2);
  }

2416
  BIND(&try_megamorphic);
2417 2418 2419
  {
    // Check megamorphic case.
    Comment("KeyedLoadIC_try_megamorphic");
2420 2421
    GotoIfNot(WordEqual(feedback, LoadRoot(Heap::kmegamorphic_symbolRootIndex)),
              &try_polymorphic_name);
2422
    // TODO(jkummerow): Inline this? Or some of it?
2423 2424 2425
    TailCallStub(
        Builtins::CallableFor(isolate(), Builtins::kKeyedLoadIC_Megamorphic),
        p->context, p->receiver, p->name, p->slot, p->vector);
2426
  }
2427
  BIND(&try_polymorphic_name);
2428 2429
  {
    // We might have a name in feedback, and a fixed array in the next slot.
2430
    Node* name = p->name;
2431
    Comment("KeyedLoadIC_try_polymorphic_name");
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
    VARIABLE(var_name, MachineRepresentation::kTagged, name);
    VARIABLE(var_index, MachineType::PointerRepresentation());
    Label if_polymorphic_name(this, &var_name), if_internalized(this),
        if_notinternalized(this, Label::kDeferred);

    // Fast-case: The recorded {feedback} matches the {name}.
    GotoIf(WordEqual(feedback, name), &if_polymorphic_name);

    // Try to internalize the {name} if it isn't already.
    TryToName(name, &miss, &var_index, &if_internalized, &var_name, &miss,
              &if_notinternalized);

    BIND(&if_internalized);
    {
      // The {var_name} now contains a unique name.
      Branch(WordEqual(feedback, var_name.value()), &if_polymorphic_name,
             &miss);
    }

    BIND(&if_notinternalized);
    {
      // Try to internalize the {name}.
      Node* function = ExternalConstant(
          ExternalReference::try_internalize_string_function(isolate()));
      var_name.Bind(CallCFunction1(MachineType::AnyTagged(),
                                   MachineType::AnyTagged(), function, name));
      Goto(&if_internalized);
    }

    BIND(&if_polymorphic_name);
    {
      // If the name comparison succeeded, we know we have a fixed array with
      // at least one map/handler pair.
      Node* name = var_name.value();
      TailCallBuiltin(Builtins::kKeyedLoadIC_PolymorphicName, p->context,
                      p->receiver, name, p->slot, p->vector);
    }
2469
  }
2470

2471
  BIND(&miss);
2472 2473 2474 2475 2476 2477 2478
  {
    Comment("KeyedLoadIC_miss");
    TailCallRuntime(Runtime::kKeyedLoadIC_Miss, p->context, p->receiver,
                    p->name, p->slot, p->vector);
  }
}

2479
void AccessorAssembler::KeyedLoadICGeneric(const LoadICParameters* p) {
2480 2481
  VARIABLE(var_index, MachineType::PointerRepresentation());
  VARIABLE(var_unique, MachineRepresentation::kTagged);
2482
  var_unique.Bind(p->name);  // Dummy initialization.
2483
  Label if_index(this), if_unique_name(this), if_notunique(this), slow(this);
2484 2485 2486 2487 2488 2489

  Node* receiver = p->receiver;
  GotoIf(TaggedIsSmi(receiver), &slow);
  Node* receiver_map = LoadMap(receiver);
  Node* instance_type = LoadMapInstanceType(receiver_map);

2490 2491
  TryToName(p->name, &if_index, &var_index, &if_unique_name, &var_unique, &slow,
            &if_notunique);
2492

2493
  BIND(&if_index);
2494
  {
2495 2496
    GenericElementLoad(receiver, receiver_map, instance_type, var_index.value(),
                       &slow);
2497 2498
  }

2499
  BIND(&if_unique_name);
2500
  {
2501 2502 2503
    LoadICParameters pp = *p;
    pp.name = var_unique.value();
    GenericPropertyLoad(receiver, receiver_map, instance_type, &pp, &slow);
2504 2505
  }

2506 2507 2508
  BIND(&if_notunique);
  {
    if (FLAG_internalize_on_the_fly) {
2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
      // Ideally we could return undefined directly here if the name is not
      // found in the string table, i.e. it was never internalized, but that
      // invariant doesn't hold with named property interceptors (at this
      // point), so we take the {slow} path instead.
      Label if_in_string_table(this);
      TryInternalizeString(p->name, &if_index, &var_index, &if_in_string_table,
                           &var_unique, &slow, &slow);

      BIND(&if_in_string_table);
      {
        // TODO(bmeurer): We currently use a version of GenericPropertyLoad
        // here, where we don't try to probe the megamorphic stub cache after
        // successfully internalizing the incoming string. Past experiments
        // with this have shown that it causes too much traffic on the stub
        // cache. We may want to re-evaluate that in the future.
        LoadICParameters pp = *p;
        pp.name = var_unique.value();
        GenericPropertyLoad(receiver, receiver_map, instance_type, &pp, &slow,
                            kDontUseStubCache);
      }
2529 2530 2531 2532 2533
    } else {
      Goto(&slow);
    }
  }

2534
  BIND(&slow);
2535 2536 2537 2538 2539 2540 2541 2542 2543
  {
    Comment("KeyedLoadGeneric_slow");
    IncrementCounter(isolate()->counters()->ic_keyed_load_generic_slow(), 1);
    // TODO(jkummerow): Should we use the GetProperty TF stub instead?
    TailCallRuntime(Runtime::kKeyedGetProperty, p->context, p->receiver,
                    p->name);
  }
}

2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
void AccessorAssembler::KeyedLoadICPolymorphicName(const LoadICParameters* p) {
  VARIABLE(var_handler, MachineRepresentation::kTagged);
  Label if_handler(this, &var_handler), miss(this, Label::kDeferred);

  Node* receiver = p->receiver;
  Node* receiver_map = LoadReceiverMap(receiver);
  Node* name = p->name;
  Node* vector = p->vector;
  Node* slot = p->slot;
  Node* context = p->context;

  // When we get here, we know that the {name} matches the recorded
  // feedback name in the {vector} and can safely be used for the
  // LoadIC handler logic below.
  CSA_ASSERT(this, IsName(name));
  CSA_ASSERT(this, Word32BinaryNot(IsDeprecatedMap(receiver_map)));
  CSA_ASSERT(this, WordEqual(name, LoadFeedbackVectorSlot(vector, slot, 0,
                                                          SMI_PARAMETERS)));

  // Check if we have a matching handler for the {receiver_map}.
  Node* array =
      LoadFeedbackVectorSlot(vector, slot, kPointerSize, SMI_PARAMETERS);
  HandlePolymorphicCase(receiver_map, array, &if_handler, &var_handler, &miss,
                        1);

  BIND(&if_handler);
  {
    ExitPoint direct_exit(this);
    HandleLoadICHandlerCase(p, var_handler.value(), &miss, &direct_exit,
2573 2574
                            ICMode::kNonGlobalIC,
                            OnNonExistent::kReturnUndefined, kOnlyProperties);
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584
  }

  BIND(&miss);
  {
    Comment("KeyedLoadIC_miss");
    TailCallRuntime(Runtime::kKeyedLoadIC_Miss, context, receiver, name, slot,
                    vector);
  }
}

2585
void AccessorAssembler::StoreIC(const StoreICParameters* p) {
2586
  VARIABLE(var_handler, MachineRepresentation::kTagged);
jgruber's avatar
jgruber committed
2587
  Label if_handler(this, &var_handler), try_polymorphic(this, Label::kDeferred),
2588 2589
      try_megamorphic(this, Label::kDeferred),
      try_uninitialized(this, Label::kDeferred), miss(this, Label::kDeferred);
2590 2591

  Node* receiver_map = LoadReceiverMap(p->receiver);
2592
  GotoIf(IsDeprecatedMap(receiver_map), &miss);
2593 2594 2595 2596 2597

  // Check monomorphic case.
  Node* feedback =
      TryMonomorphicCase(p->slot, p->vector, receiver_map, &if_handler,
                         &var_handler, &try_polymorphic);
2598
  BIND(&if_handler);
2599 2600
  {
    Comment("StoreIC_if_handler");
2601 2602
    HandleStoreICHandlerCase(p, var_handler.value(), &miss,
                             ICMode::kNonGlobalIC);
2603 2604
  }

2605
  BIND(&try_polymorphic);
2606 2607 2608
  {
    // Check polymorphic case.
    Comment("StoreIC_try_polymorphic");
2609
    GotoIfNot(
2610 2611 2612 2613 2614 2615
        WordEqual(LoadMap(feedback), LoadRoot(Heap::kFixedArrayMapRootIndex)),
        &try_megamorphic);
    HandlePolymorphicCase(receiver_map, feedback, &if_handler, &var_handler,
                          &miss, 2);
  }

2616
  BIND(&try_megamorphic);
2617 2618
  {
    // Check megamorphic case.
2619
    GotoIfNot(WordEqual(feedback, LoadRoot(Heap::kmegamorphic_symbolRootIndex)),
2620
              &try_uninitialized);
2621 2622 2623 2624

    TryProbeStubCache(isolate()->store_stub_cache(), p->receiver, p->name,
                      &if_handler, &var_handler, &miss);
  }
2625
  BIND(&try_uninitialized);
2626 2627 2628 2629 2630
  {
    // Check uninitialized case.
    GotoIfNot(
        WordEqual(feedback, LoadRoot(Heap::kuninitialized_symbolRootIndex)),
        &miss);
2631 2632 2633
    Callable stub =
        Builtins::CallableFor(isolate(), Builtins::kStoreIC_Uninitialized);
    TailCallStub(stub, p->context, p->receiver, p->name, p->value, p->slot,
2634 2635
                 p->vector);
  }
2636
  BIND(&miss);
2637 2638 2639 2640 2641 2642
  {
    TailCallRuntime(Runtime::kStoreIC_Miss, p->context, p->value, p->slot,
                    p->vector, p->receiver, p->name);
  }
}

2643
void AccessorAssembler::StoreGlobalIC(const StoreICParameters* pp) {
2644 2645
  Label if_lexical_var(this), if_property_cell(this);
  Node* maybe_weak_cell =
2646
      LoadFeedbackVectorSlot(pp->vector, pp->slot, 0, SMI_PARAMETERS);
2647
  Branch(TaggedIsSmi(maybe_weak_cell), &if_lexical_var, &if_property_cell);
2648

2649
  BIND(&if_property_cell);
2650
  {
2651 2652 2653 2654 2655 2656
    Label try_handler(this), miss(this, Label::kDeferred);
    Node* property_cell = LoadWeakCellValue(maybe_weak_cell, &try_handler);

    ExitPoint direct_exit(this);
    StoreGlobalIC_PropertyCellCase(property_cell, pp->value, &direct_exit,
                                   &miss);
2657

2658 2659 2660 2661 2662
    BIND(&try_handler);
    {
      Comment("StoreGlobalIC_try_handler");
      Node* handler = LoadFeedbackVectorSlot(pp->vector, pp->slot, kPointerSize,
                                             SMI_PARAMETERS);
2663

2664 2665
      GotoIf(WordEqual(handler, LoadRoot(Heap::kuninitialized_symbolRootIndex)),
             &miss);
2666

2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
      StoreICParameters p = *pp;
      DCHECK_NULL(p.receiver);
      Node* native_context = LoadNativeContext(p.context);
      p.receiver =
          LoadContextElement(native_context, Context::GLOBAL_PROXY_INDEX);

      HandleStoreICHandlerCase(&p, handler, &miss, ICMode::kGlobalIC);
    }

    BIND(&miss);
    {
      TailCallRuntime(Runtime::kStoreGlobalIC_Miss, pp->context, pp->value,
                      pp->slot, pp->vector, pp->name);
    }
2681 2682
  }

2683
  BIND(&if_lexical_var);
2684
  {
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
    Comment("Store lexical variable");
    TNode<IntPtrT> lexical_handler = SmiUntag(maybe_weak_cell);
    TNode<IntPtrT> context_index =
        Signed(DecodeWord<GlobalICNexus::ContextIndexBits>(lexical_handler));
    TNode<IntPtrT> slot_index =
        Signed(DecodeWord<GlobalICNexus::SlotIndexBits>(lexical_handler));
    TNode<Context> script_context =
        LoadScriptContext(CAST(pp->context), context_index);
    StoreContextElement(script_context, slot_index, CAST(pp->value));
    Return(pp->value);
2695 2696 2697
  }
}

2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
void AccessorAssembler::StoreGlobalIC_PropertyCellCase(Node* property_cell,
                                                       Node* value,
                                                       ExitPoint* exit_point,
                                                       Label* miss) {
  Comment("StoreGlobalIC_TryPropertyCellCase");
  CSA_ASSERT(this, IsPropertyCell(property_cell));

  // Load the payload of the global parameter cell. A hole indicates that
  // the cell has been invalidated and that the store must be handled by the
  // runtime.
  Node* cell_contents =
      LoadObjectField(property_cell, PropertyCell::kValueOffset);
  Node* details = LoadAndUntagToWord32ObjectField(property_cell,
                                                  PropertyCell::kDetailsOffset);
2712 2713 2714 2715 2716
  GotoIf(IsSetWord32(details, PropertyDetails::kAttributesReadOnlyMask), miss);
  CSA_ASSERT(this,
             Word32Equal(DecodeWord32<PropertyDetails::KindField>(details),
                         Int32Constant(kData)));

2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761
  Node* type = DecodeWord32<PropertyDetails::PropertyCellTypeField>(details);

  Label constant(this), store(this), not_smi(this);

  GotoIf(Word32Equal(type, Int32Constant(
                               static_cast<int>(PropertyCellType::kConstant))),
         &constant);

  GotoIf(IsTheHole(cell_contents), miss);

  GotoIf(Word32Equal(
             type, Int32Constant(static_cast<int>(PropertyCellType::kMutable))),
         &store);
  CSA_ASSERT(this,
             Word32Or(Word32Equal(type, Int32Constant(static_cast<int>(
                                            PropertyCellType::kConstantType))),
                      Word32Equal(type, Int32Constant(static_cast<int>(
                                            PropertyCellType::kUndefined)))));

  GotoIfNot(TaggedIsSmi(cell_contents), &not_smi);
  GotoIfNot(TaggedIsSmi(value), miss);
  Goto(&store);

  BIND(&not_smi);
  {
    GotoIf(TaggedIsSmi(value), miss);
    Node* expected_map = LoadMap(cell_contents);
    Node* map = LoadMap(value);
    GotoIfNot(WordEqual(expected_map, map), miss);
    Goto(&store);
  }

  BIND(&store);
  {
    StoreObjectField(property_cell, PropertyCell::kValueOffset, value);
    exit_point->Return(value);
  }

  BIND(&constant);
  {
    GotoIfNot(WordEqual(cell_contents, value), miss);
    exit_point->Return(value);
  }
}

2762
void AccessorAssembler::KeyedStoreIC(const StoreICParameters* p) {
jgruber's avatar
jgruber committed
2763
  Label miss(this, Label::kDeferred);
2764
  {
2765
    VARIABLE(var_handler, MachineRepresentation::kTagged);
2766

jgruber's avatar
jgruber committed
2767 2768 2769 2770
    Label if_handler(this, &var_handler),
        try_polymorphic(this, Label::kDeferred),
        try_megamorphic(this, Label::kDeferred),
        try_polymorphic_name(this, Label::kDeferred);
2771

2772
    Node* receiver_map = LoadReceiverMap(p->receiver);
2773
    GotoIf(IsDeprecatedMap(receiver_map), &miss);
2774

2775 2776 2777 2778
    // Check monomorphic case.
    Node* feedback =
        TryMonomorphicCase(p->slot, p->vector, receiver_map, &if_handler,
                           &var_handler, &try_polymorphic);
2779
    BIND(&if_handler);
2780 2781
    {
      Comment("KeyedStoreIC_if_handler");
2782 2783
      HandleStoreICHandlerCase(p, var_handler.value(), &miss,
                               ICMode::kNonGlobalIC, kSupportElements);
2784
    }
2785

2786
    BIND(&try_polymorphic);
2787 2788 2789
    {
      // CheckPolymorphic case.
      Comment("KeyedStoreIC_try_polymorphic");
2790
      GotoIfNot(
2791 2792
          WordEqual(LoadMap(feedback), LoadRoot(Heap::kFixedArrayMapRootIndex)),
          &try_megamorphic);
2793 2794
      HandlePolymorphicCase(receiver_map, feedback, &if_handler, &var_handler,
                            &miss, 2);
2795
    }
2796

2797
    BIND(&try_megamorphic);
2798 2799 2800
    {
      // Check megamorphic case.
      Comment("KeyedStoreIC_try_megamorphic");
2801
      GotoIfNot(
2802 2803 2804
          WordEqual(feedback, LoadRoot(Heap::kmegamorphic_symbolRootIndex)),
          &try_polymorphic_name);
      TailCallStub(
2805
          Builtins::CallableFor(isolate(), Builtins::kKeyedStoreIC_Megamorphic),
2806 2807
          p->context, p->receiver, p->name, p->value, p->slot, p->vector);
    }
2808

2809
    BIND(&try_polymorphic_name);
2810 2811 2812
    {
      // We might have a name in feedback, and a fixed array in the next slot.
      Comment("KeyedStoreIC_try_polymorphic_name");
2813
      GotoIfNot(WordEqual(feedback, p->name), &miss);
2814 2815 2816 2817
      // If the name comparison succeeded, we know we have a feedback vector
      // with at least one map/handler pair.
      Node* array = LoadFeedbackVectorSlot(p->vector, p->slot, kPointerSize,
                                           SMI_PARAMETERS);
2818 2819 2820
      HandlePolymorphicCase(receiver_map, array, &if_handler, &var_handler,
                            &miss, 1);
    }
2821
  }
2822
  BIND(&miss);
2823 2824 2825 2826 2827 2828 2829 2830 2831
  {
    Comment("KeyedStoreIC_miss");
    TailCallRuntime(Runtime::kKeyedStoreIC_Miss, p->context, p->value, p->slot,
                    p->vector, p->receiver, p->name);
  }
}

//////////////////// Public methods.

2832
void AccessorAssembler::GenerateLoadIC() {
2833
  typedef LoadWithVectorDescriptor Descriptor;
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

  LoadICParameters p(context, receiver, name, slot, vector);
  LoadIC(&p);
}

2845 2846 2847 2848 2849 2850 2851 2852 2853 2854
void AccessorAssembler::GenerateLoadIC_Noninlined() {
  typedef LoadWithVectorDescriptor Descriptor;

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

  ExitPoint direct_exit(this);
2855
  VARIABLE(var_handler, MachineRepresentation::kTagged);
2856 2857 2858
  Label if_handler(this, &var_handler), miss(this, Label::kDeferred);

  Node* receiver_map = LoadReceiverMap(receiver);
2859
  Node* feedback = LoadFeedbackVectorSlot(vector, slot, 0, SMI_PARAMETERS);
2860 2861 2862 2863 2864

  LoadICParameters p(context, receiver, name, slot, vector);
  LoadIC_Noninlined(&p, receiver_map, feedback, &var_handler, &if_handler,
                    &miss, &direct_exit);

2865
  BIND(&if_handler);
2866 2867
  HandleLoadICHandlerCase(&p, var_handler.value(), &miss, &direct_exit);

2868
  BIND(&miss);
2869 2870 2871 2872
  direct_exit.ReturnCallRuntime(Runtime::kLoadIC_Miss, context, receiver, name,
                                slot, vector);
}

2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
void AccessorAssembler::GenerateLoadIC_Uninitialized() {
  typedef LoadWithVectorDescriptor Descriptor;

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

  LoadICParameters p(context, receiver, name, slot, vector);
  LoadIC_Uninitialized(&p);
}

2886
void AccessorAssembler::GenerateLoadICTrampoline() {
2887
  typedef LoadDescriptor Descriptor;
2888 2889 2890 2891 2892

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* context = Parameter(Descriptor::kContext);
2893
  Node* vector = LoadFeedbackVectorForStub();
2894

2895
  TailCallBuiltin(Builtins::kLoadIC, context, receiver, name, slot, vector);
2896 2897
}

2898 2899
void AccessorAssembler::GenerateLoadField() {
  typedef LoadFieldDescriptor Descriptor;
2900 2901 2902 2903 2904 2905 2906 2907

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = nullptr;
  Node* slot = nullptr;
  Node* vector = nullptr;
  Node* context = Parameter(Descriptor::kContext);
  LoadICParameters p(context, receiver, name, slot, vector);

2908 2909
  ExitPoint direct_exit(this);

2910
  VARIABLE(var_double_value, MachineRepresentation::kFloat64);
2911 2912 2913 2914 2915 2916 2917
  Label rebox_double(this, &var_double_value);

  Node* smi_handler = Parameter(Descriptor::kSmiHandler);
  Node* handler_word = SmiUntag(smi_handler);
  HandleLoadField(receiver, handler_word, &var_double_value, &rebox_double,
                  &direct_exit);

2918
  BIND(&rebox_double);
2919
  Return(AllocateHeapNumberWithValue(var_double_value.value()));
2920 2921
}

2922
void AccessorAssembler::GenerateLoadGlobalIC(TypeofMode typeof_mode) {
2923
  typedef LoadGlobalWithVectorDescriptor Descriptor;
2924

2925
  Node* name = Parameter(Descriptor::kName);
2926 2927 2928 2929
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

2930 2931 2932 2933 2934 2935
  ExitPoint direct_exit(this);
  LoadGlobalIC(CAST(vector), slot,
               // lazy_context
               [=] { return CAST(context); },
               // lazy_name
               [=] { return CAST(name); }, typeof_mode, &direct_exit);
2936 2937
}

2938
void AccessorAssembler::GenerateLoadGlobalICTrampoline(TypeofMode typeof_mode) {
2939
  typedef LoadGlobalDescriptor Descriptor;
2940

2941
  Node* name = Parameter(Descriptor::kName);
2942 2943
  Node* slot = Parameter(Descriptor::kSlot);
  Node* context = Parameter(Descriptor::kContext);
2944
  Node* vector = LoadFeedbackVectorForStub();
2945

2946 2947 2948
  Callable callable =
      CodeFactory::LoadGlobalICInOptimizedCode(isolate(), typeof_mode);
  TailCallStub(callable, context, name, slot, vector);
2949 2950
}

2951
void AccessorAssembler::GenerateKeyedLoadIC() {
2952
  typedef LoadWithVectorDescriptor Descriptor;
2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

  LoadICParameters p(context, receiver, name, slot, vector);
  KeyedLoadIC(&p);
}

2964
void AccessorAssembler::GenerateKeyedLoadICTrampoline() {
2965
  typedef LoadDescriptor Descriptor;
2966 2967 2968 2969 2970

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* context = Parameter(Descriptor::kContext);
2971
  Node* vector = LoadFeedbackVectorForStub();
2972

2973 2974
  TailCallBuiltin(Builtins::kKeyedLoadIC, context, receiver, name, slot,
                  vector);
2975 2976
}

2977
void AccessorAssembler::GenerateKeyedLoadIC_Megamorphic() {
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989
  typedef LoadWithVectorDescriptor Descriptor;

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

  LoadICParameters p(context, receiver, name, slot, vector);
  KeyedLoadICGeneric(&p);
}

2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
void AccessorAssembler::GenerateKeyedLoadIC_PolymorphicName() {
  typedef LoadWithVectorDescriptor Descriptor;

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

  LoadICParameters p(context, receiver, name, slot, vector);
  KeyedLoadICPolymorphicName(&p);
}

3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029
void AccessorAssembler::GenerateStoreGlobalIC() {
  typedef StoreGlobalWithVectorDescriptor Descriptor;

  Node* name = Parameter(Descriptor::kName);
  Node* value = Parameter(Descriptor::kValue);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

  StoreICParameters p(context, nullptr, name, value, slot, vector);
  StoreGlobalIC(&p);
}

void AccessorAssembler::GenerateStoreGlobalICTrampoline() {
  typedef StoreGlobalDescriptor Descriptor;

  Node* name = Parameter(Descriptor::kName);
  Node* value = Parameter(Descriptor::kValue);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* context = Parameter(Descriptor::kContext);
  Node* vector = LoadFeedbackVectorForStub();

  Callable callable =
      Builtins::CallableFor(isolate(), Builtins::kStoreGlobalIC);
  TailCallStub(callable, context, name, value, slot, vector);
}

3030
void AccessorAssembler::GenerateStoreIC() {
3031
  typedef StoreWithVectorDescriptor Descriptor;
3032 3033 3034 3035 3036 3037 3038 3039 3040

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* value = Parameter(Descriptor::kValue);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

  StoreICParameters p(context, receiver, name, value, slot, vector);
3041
  StoreIC(&p);
3042 3043
}

3044
void AccessorAssembler::GenerateStoreICTrampoline() {
3045
  typedef StoreDescriptor Descriptor;
3046 3047 3048 3049 3050 3051

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* value = Parameter(Descriptor::kValue);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* context = Parameter(Descriptor::kContext);
3052
  Node* vector = LoadFeedbackVectorForStub();
3053

3054
  Callable callable = Builtins::CallableFor(isolate(), Builtins::kStoreIC);
3055
  TailCallStub(callable, context, receiver, name, value, slot, vector);
3056 3057
}

3058
void AccessorAssembler::GenerateKeyedStoreIC() {
3059
  typedef StoreWithVectorDescriptor Descriptor;
3060 3061 3062 3063 3064 3065 3066 3067 3068

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* value = Parameter(Descriptor::kValue);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

  StoreICParameters p(context, receiver, name, value, slot, vector);
3069
  KeyedStoreIC(&p);
3070 3071
}

3072
void AccessorAssembler::GenerateKeyedStoreICTrampoline() {
3073
  typedef StoreDescriptor Descriptor;
3074 3075 3076 3077 3078 3079

  Node* receiver = Parameter(Descriptor::kReceiver);
  Node* name = Parameter(Descriptor::kName);
  Node* value = Parameter(Descriptor::kValue);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* context = Parameter(Descriptor::kContext);
3080
  Node* vector = LoadFeedbackVectorForStub();
3081

3082
  Callable callable = Builtins::CallableFor(isolate(), Builtins::kKeyedStoreIC);
3083
  TailCallStub(callable, context, receiver, name, value, slot, vector);
3084 3085 3086 3087
}

}  // namespace internal
}  // namespace v8