accessor-assembler.cc 140 KB
Newer Older
1 2 3 4 5 6
// 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"

7
#include "src/ast/ast.h"
8
#include "src/code-factory.h"
9
#include "src/counters.h"
10
#include "src/ic/handler-configuration.h"
11
#include "src/ic/ic.h"
12
#include "src/ic/keyed-store-generic.h"
13
#include "src/ic/stub-cache.h"
14
#include "src/objects-inl.h"
15
#include "src/objects/cell.h"
16
#include "src/objects/foreign.h"
17
#include "src/objects/heap-number.h"
18
#include "src/objects/module.h"
19
#include "src/objects/smi.h"
20 21 22 23 24

namespace v8 {
namespace internal {

using compiler::CodeAssemblerState;
25
using compiler::Node;
26 27 28 29
template <typename T>
using TNode = compiler::TNode<T>;
template <typename T>
using SloppyTNode = compiler::SloppyTNode<T>;
30 31 32

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

33
// Loads dataX field from the DataHandler object.
34
TNode<MaybeObject> AccessorAssembler::LoadHandlerDataField(
35
    SloppyTNode<DataHandler> handler, int data_index) {
36
#ifdef DEBUG
37 38
  TNode<Map> handler_map = LoadMap(handler);
  TNode<Int32T> instance_type = LoadMapInstanceType(handler_map);
39 40 41 42 43 44
#endif
  CSA_ASSERT(this,
             Word32Or(InstanceTypeEqual(instance_type, LOAD_HANDLER_TYPE),
                      InstanceTypeEqual(instance_type, STORE_HANDLER_TYPE)));
  int offset = 0;
  int minimum_size = 0;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  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;
61 62 63 64
  }
  USE(minimum_size);
  CSA_ASSERT(this, UintPtrGreaterThanOrEqual(
                       LoadMapInstanceSizeInWords(handler_map),
65
                       IntPtrConstant(minimum_size / kTaggedSize)));
66
  return LoadMaybeWeakObjectField(handler, offset);
67 68
}

69 70 71
TNode<MaybeObject> AccessorAssembler::TryMonomorphicCase(
    Node* slot, Node* vector, Node* receiver_map, Label* if_handler,
    TVariable<MaybeObject>* var_handler, Label* if_miss) {
72 73 74 75 76
  Comment("TryMonomorphicCase");
  DCHECK_EQ(MachineRepresentation::kTagged, var_handler->rep());

  // TODO(ishell): add helper class that hides offset computations for a series
  // of loads.
77 78
  CSA_ASSERT(this, IsFeedbackVector(vector), vector);
  int32_t header_size = FeedbackVector::kFeedbackSlotsOffset - kHeapObjectTag;
79 80 81
  // 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.
82
  Node* offset = ElementOffsetFromIndex(slot, HOLEY_ELEMENTS, SMI_PARAMETERS);
83 84 85
  TNode<MaybeObject> feedback = ReinterpretCast<MaybeObject>(
      Load(MachineType::AnyTagged(), vector,
           IntPtrAdd(offset, IntPtrConstant(header_size))));
86 87

  // Try to quickly handle the monomorphic case without knowing for sure
88 89
  // if we have a weak reference in feedback.
  GotoIf(IsNotWeakReferenceTo(feedback, CAST(receiver_map)), if_miss);
90

91
  TNode<MaybeObject> handler = UncheckedCast<MaybeObject>(
92
      Load(MachineType::AnyTagged(), vector,
93
           IntPtrAdd(offset, IntPtrConstant(header_size + kTaggedSize))));
94

95
  *var_handler = handler;
96 97 98 99
  Goto(if_handler);
  return feedback;
}

100 101
void AccessorAssembler::HandlePolymorphicCase(
    Node* receiver_map, TNode<WeakFixedArray> feedback, Label* if_handler,
102
    TVariable<MaybeObject>* var_handler, Label* if_miss) {
103 104 105 106 107 108
  Comment("HandlePolymorphicCase");
  DCHECK_EQ(MachineRepresentation::kTagged, var_handler->rep());

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

109 110 111
  // Load the {feedback} array length.
  TNode<IntPtrT> length = LoadAndUntagWeakFixedArrayLength(feedback);
  CSA_ASSERT(this, IntPtrLessThanOrEqual(IntPtrConstant(1), length));
112

113 114 115 116 117 118 119 120
  // This is a hand-crafted loop that only compares against the {length}
  // in the end, since we already know that we will have at least a single
  // entry in the {feedback} array anyways.
  TVARIABLE(IntPtrT, var_index, IntPtrConstant(0));
  Label loop(this, &var_index), loop_next(this);
  Goto(&loop);
  BIND(&loop);
  {
121
    TNode<MaybeObject> maybe_cached_map =
122
        LoadWeakFixedArrayElement(feedback, var_index.value());
123
    CSA_ASSERT(this, IsWeakOrCleared(maybe_cached_map));
124
    GotoIf(IsNotWeakReferenceTo(maybe_cached_map, CAST(receiver_map)),
125
           &loop_next);
126 127

    // Found, now call handler.
128
    TNode<MaybeObject> handler =
129
        LoadWeakFixedArrayElement(feedback, var_index.value(), kTaggedSize);
130
    *var_handler = handler;
131 132
    Goto(if_handler);

133 134 135 136
    BIND(&loop_next);
    var_index =
        Signed(IntPtrAdd(var_index.value(), IntPtrConstant(kEntrySize)));
    Branch(IntPtrLessThan(var_index.value(), length), &loop, if_miss);
137 138 139
  }
}

140
void AccessorAssembler::HandleLoadICHandlerCase(
141
    const LoadICParameters* p, TNode<Object> handler, Label* miss,
142 143
    ExitPoint* exit_point, ICMode ic_mode, OnNonExistent on_nonexistent,
    ElementSupport support_elements) {
144
  Comment("have_handler");
145

146
  VARIABLE(var_holder, MachineRepresentation::kTagged, p->holder);
147
  VARIABLE(var_smi_handler, MachineRepresentation::kTagged, handler);
148 149 150

  Variable* vars[] = {&var_holder, &var_smi_handler};
  Label if_smi_handler(this, 2, vars);
jgruber's avatar
jgruber committed
151 152
  Label try_proto_handler(this, Label::kDeferred),
      call_handler(this, Label::kDeferred);
153 154 155 156 157

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

  // |handler| is a Smi, encoding what to do. See SmiHandler methods
  // for the encoding format.
158
  BIND(&if_smi_handler);
159 160
  {
    HandleLoadICSmiHandlerCase(p, var_holder.value(), var_smi_handler.value(),
161
                               handler, miss, exit_point, on_nonexistent,
162
                               support_elements);
163 164
  }

165
  BIND(&try_proto_handler);
166
  {
167
    GotoIf(IsCodeMap(LoadMap(CAST(handler))), &call_handler);
168 169
    HandleLoadICProtoHandler(p, handler, &var_holder, &var_smi_handler,
                             &if_smi_handler, miss, exit_point, ic_mode);
170 171
  }

172
  BIND(&call_handler);
173
  {
174
    exit_point->ReturnCallStub(LoadWithVectorDescriptor{}, handler, p->context,
175
                               p->receiver, p->name, p->slot, p->vector);
176 177 178
  }
}

179 180 181 182 183
void AccessorAssembler::HandleLoadCallbackProperty(const LoadICParameters* p,
                                                   TNode<JSObject> holder,
                                                   TNode<WordT> handler_word,
                                                   ExitPoint* exit_point) {
  Comment("native_data_property_load");
184 185
  TNode<IntPtrT> descriptor =
      Signed(DecodeWord<LoadHandler::DescriptorBits>(handler_word));
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

  Label runtime(this, Label::kDeferred);
  Callable callable = CodeFactory::ApiGetter(isolate());
  TNode<AccessorInfo> accessor_info =
      CAST(LoadDescriptorValue(LoadMap(holder), descriptor));

  GotoIf(IsRuntimeCallStatsEnabled(), &runtime);
  exit_point->ReturnCallStub(callable, p->context, p->receiver, holder,
                             accessor_info);

  BIND(&runtime);
  exit_point->ReturnCallRuntime(Runtime::kLoadCallbackProperty, p->context,
                                p->receiver, holder, accessor_info, p->name);
}

void AccessorAssembler::HandleLoadAccessor(
    const LoadICParameters* p, TNode<CallHandlerInfo> call_handler_info,
    TNode<WordT> handler_word, TNode<DataHandler> handler,
    TNode<IntPtrT> handler_kind, ExitPoint* exit_point) {
  Comment("api_getter");
  Label runtime(this, Label::kDeferred);
  // Context is stored either in data2 or data3 field depending on whether
  // the access check is enabled for this handler or not.
  TNode<MaybeObject> maybe_context = Select<MaybeObject>(
      IsSetWord<LoadHandler::DoAccessCheckOnReceiverBits>(handler_word),
      [=] { return LoadHandlerDataField(handler, 3); },
      [=] { return LoadHandlerDataField(handler, 2); });

214 215 216
  CSA_ASSERT(this, IsWeakOrCleared(maybe_context));
  CSA_CHECK(this, IsNotCleared(maybe_context));
  TNode<Object> context = GetHeapObjectAssumeWeak(maybe_context);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

  GotoIf(IsRuntimeCallStatsEnabled(), &runtime);
  {
    TNode<Foreign> foreign = CAST(
        LoadObjectField(call_handler_info, CallHandlerInfo::kJsCallbackOffset));
    TNode<WordT> callback = TNode<WordT>::UncheckedCast(LoadObjectField(
        foreign, Foreign::kForeignAddressOffset, MachineType::Pointer()));
    TNode<Object> 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);
241
    Callable callable = CodeFactory::CallApiCallback(isolate());
242
    TNode<IntPtrT> argc = IntPtrConstant(0);
243 244
    exit_point->Return(CallStub(callable, context, callback, argc, data,
                                api_holder.value(), p->receiver));
245 246 247 248 249 250 251 252
  }

  BIND(&runtime);
  exit_point->ReturnCallRuntime(Runtime::kLoadAccessorProperty, context,
                                p->receiver, SmiTag(handler_kind),
                                call_handler_info);
}

253 254 255 256 257
void AccessorAssembler::HandleLoadField(Node* holder, Node* handler_word,
                                        Variable* var_double_value,
                                        Label* rebox_double,
                                        ExitPoint* exit_point) {
  Comment("field_load");
258
  Node* index = DecodeWord<LoadHandler::FieldIndexBits>(handler_word);
259
  Node* offset = IntPtrMul(index, IntPtrConstant(kTaggedSize));
260 261 262 263 264

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

265
  BIND(&inobject);
266 267 268 269 270
  {
    Label is_double(this);
    GotoIf(IsSetWord<LoadHandler::IsDoubleBits>(handler_word), &is_double);
    exit_point->Return(LoadObjectField(holder, offset));

271
    BIND(&is_double);
272 273 274 275 276 277 278 279 280 281
    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);
  }

282
  BIND(&out_of_object);
283 284
  {
    Label is_double(this);
285
    Node* properties = LoadFastProperties(holder);
286 287 288 289
    Node* value = LoadObjectField(properties, offset);
    GotoIf(IsSetWord<LoadHandler::IsDoubleBits>(handler_word), &is_double);
    exit_point->Return(value);

290
    BIND(&is_double);
291 292 293 294 295
    var_double_value->Bind(LoadHeapNumberValue(value));
    Goto(rebox_double);
  }
}

296 297 298
TNode<Object> AccessorAssembler::LoadDescriptorValue(
    TNode<Map> map, TNode<IntPtrT> descriptor_entry) {
  return CAST(LoadDescriptorValueOrFieldType(map, descriptor_entry));
299 300 301
}

TNode<MaybeObject> AccessorAssembler::LoadDescriptorValueOrFieldType(
302
    TNode<Map> map, TNode<IntPtrT> descriptor_entry) {
303
  TNode<DescriptorArray> descriptors = LoadMapDescriptors(map);
304
  return LoadFieldTypeByDescriptorEntry(descriptors, descriptor_entry);
305 306
}

307
void AccessorAssembler::HandleLoadICSmiHandlerCase(
308 309 310
    const LoadICParameters* p, Node* holder, SloppyTNode<Smi> smi_handler,
    SloppyTNode<Object> handler, Label* miss, ExitPoint* exit_point,
    OnNonExistent on_nonexistent, ElementSupport support_elements) {
311
  VARIABLE(var_double_value, MachineRepresentation::kFloat64);
312 313
  Label rebox_double(this, &var_double_value);

314 315 316
  TNode<WordT> handler_word = SmiUntag(smi_handler);
  TNode<IntPtrT> handler_kind =
      Signed(DecodeWord<LoadHandler::KindBits>(handler_word));
317
  if (support_elements == kSupportElements) {
318 319 320 321 322
    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);
323

324
    BIND(&if_element);
325 326 327 328 329 330
    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 =
331
        DecodeWord32FromWord<LoadHandler::ElementsKindBits>(handler_word);
332 333
    Label if_hole(this), unimplemented_elements_kind(this),
        if_oob(this, Label::kDeferred);
334 335
    EmitElementLoad(holder, elements, elements_kind, intptr_index,
                    is_jsarray_condition, &if_hole, &rebox_double,
336 337
                    &var_double_value, &unimplemented_elements_kind, &if_oob,
                    miss, exit_point);
338

339
    BIND(&unimplemented_elements_kind);
340 341 342 343 344 345 346
    {
      // Smi handlers should only be installed for supported elements kinds.
      // Crash if we get here.
      DebugBreak();
      Goto(miss);
    }

347 348 349 350 351 352 353 354 355 356
    BIND(&if_oob);
    {
      Comment("out of bounds elements access");
      Label return_undefined(this);

      // 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);

357 358 359 360 361
      // 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, except
      // in case of typed arrays, where integer indexed properties
      // aren't looked up in the prototype chain.
362
      GotoIf(IsJSTypedArray(holder), &return_undefined);
363
      GotoIf(IntPtrLessThan(intptr_index, IntPtrConstant(0)), miss);
364 365 366 367 368 369 370 371 372 373

      // 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());
    }

374
    BIND(&if_hole);
375 376
    {
      Comment("convert hole");
377
      GotoIfNot(IsSetWord<LoadHandler::ConvertHoleBits>(handler_word), miss);
378
      GotoIf(IsNoElementsProtectorCellInvalid(), miss);
379
      exit_point->Return(UndefinedConstant());
380 381
    }

382 383
    BIND(&if_indexed_string);
    {
384 385
      Label if_oob(this, Label::kDeferred);

386 387
      Comment("indexed string");
      Node* intptr_index = TryToIntptr(p->name, miss);
388
      Node* length = LoadStringLengthAsWord(holder);
389
      GotoIf(UintPtrGreaterThanOrEqual(intptr_index, length), &if_oob);
390
      TNode<Int32T> code = StringCharCodeAt(holder, intptr_index);
391
      TNode<String> result = StringFromSingleCharCode(code);
392
      Return(result);
393 394 395 396 397

      BIND(&if_oob);
      Node* allow_out_of_bounds =
          IsSetWord<LoadHandler::AllowOutOfBoundsBits>(handler_word);
      GotoIfNot(allow_out_of_bounds, miss);
398
      GotoIf(IsNoElementsProtectorCellInvalid(), miss);
399
      Return(UndefinedConstant());
400 401 402
    }

    BIND(&if_property);
403 404 405
    Comment("property_load");
  }

406
  Label constant(this), field(this), normal(this, Label::kDeferred),
407
      interceptor(this, Label::kDeferred), nonexistent(this),
408
      accessor(this, Label::kDeferred), global(this, Label::kDeferred),
409 410
      module_export(this, Label::kDeferred), proxy(this, Label::kDeferred),
      native_data_property(this), api_getter(this);
411
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kField)), &field);
412

413 414 415 416 417 418
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kConstant)),
         &constant);

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

419 420 421
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kNormal)),
         &normal);

422 423 424
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kAccessor)),
         &accessor);

425 426 427 428 429 430 431 432 433 434 435
  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);

436 437 438
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kGlobal)),
         &global);

439 440
  GotoIf(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kProxy)), &proxy);

441 442
  Branch(WordEqual(handler_kind, IntPtrConstant(LoadHandler::kModuleExport)),
         &module_export, &interceptor);
443

444
  BIND(&field);
445 446
  HandleLoadField(holder, handler_word, &var_double_value, &rebox_double,
                  exit_point);
447

448
  BIND(&nonexistent);
449
  // This is a handler for a load of a non-existent value.
450
  if (on_nonexistent == OnNonExistent::kThrowReferenceError) {
451 452 453
    exit_point->ReturnCallRuntime(Runtime::kThrowReferenceError, p->context,
                                  p->name);
  } else {
454
    DCHECK_EQ(OnNonExistent::kReturnUndefined, on_nonexistent);
455 456 457
    exit_point->Return(UndefinedConstant());
  }

458
  BIND(&constant);
459 460
  {
    Comment("constant_load");
461 462
    TNode<IntPtrT> descriptor =
        Signed(DecodeWord<LoadHandler::DescriptorBits>(handler_word));
463
    Node* value = LoadDescriptorValue(LoadMap(holder), descriptor);
464

465
    exit_point->Return(value);
466
  }
467

468
  BIND(&normal);
469 470
  {
    Comment("load_normal");
471 472
    TNode<NameDictionary> properties = CAST(LoadSlowProperties(holder));
    TVARIABLE(IntPtrT, var_name_index);
473
    Label found(this, &var_name_index);
474
    NameDictionaryLookup<NameDictionary>(properties, CAST(p->name), &found,
475
                                         &var_name_index, miss);
476
    BIND(&found);
477
    {
478 479
      VARIABLE(var_details, MachineRepresentation::kWord32);
      VARIABLE(var_value, MachineRepresentation::kTagged);
480 481 482 483 484 485 486 487
      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);
    }
  }

488
  BIND(&accessor);
489 490
  {
    Comment("accessor_load");
491 492
    TNode<IntPtrT> descriptor =
        Signed(DecodeWord<LoadHandler::DescriptorBits>(handler_word));
493
    Node* accessor_pair = LoadDescriptorValue(LoadMap(holder), descriptor);
494 495 496 497 498 499 500 501
    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));
  }

502
  BIND(&native_data_property);
503
  HandleLoadCallbackProperty(p, CAST(holder), handler_word, exit_point);
504 505

  BIND(&api_getter);
506 507
  HandleLoadAccessor(p, CAST(holder), handler_word, CAST(handler), handler_kind,
                     exit_point);
508

509 510
  BIND(&proxy);
  {
511 512 513 514 515 516 517
    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) {
518 519
      DCHECK_NE(on_nonexistent, OnNonExistent::kThrowReferenceError);

520 521 522 523 524 525
      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),
526 527
          p->context, holder, var_unique.value(), p->receiver,
          SmiConstant(on_nonexistent));
528 529 530 531 532 533 534 535

      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,
536 537
                                    p->context, holder, p->name, p->receiver,
                                    SmiConstant(on_nonexistent));
538 539 540
    } else {
      exit_point->ReturnCallStub(
          Builtins::CallableFor(isolate(), Builtins::kProxyGetProperty),
541 542
          p->context, holder, p->name, p->receiver,
          SmiConstant(on_nonexistent));
543
    }
544 545
  }

546
  BIND(&global);
547 548 549 550 551 552 553 554 555 556 557 558
  {
    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));
  }

559
  BIND(&interceptor);
560 561 562 563 564 565 566
  {
    Comment("load_interceptor");
    exit_point->ReturnCallRuntime(Runtime::kLoadPropertyWithInterceptor,
                                  p->context, p->name, p->receiver, holder,
                                  p->slot, p->vector);
  }

567 568 569 570 571 572 573
  BIND(&module_export);
  {
    Comment("module export");
    Node* index = DecodeWord<LoadHandler::ExportsIndexBits>(handler_word);
    Node* module =
        LoadObjectField(p->receiver, JSModuleNamespace::kModuleOffset,
                        MachineType::TaggedPointer());
574 575
    TNode<ObjectHashTable> exports = CAST(LoadObjectField(
        module, Module::kExportsOffset, MachineType::TaggedPointer()));
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    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);
    }
  }

592
  BIND(&rebox_double);
593
  exit_point->Return(AllocateHeapNumberWithValue(var_double_value.value()));
594 595
}

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
// 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,
617
    ICMode ic_mode) {
618 619 620 621
  //
  // Check prototype validity cell.
  //
  {
622
    Node* maybe_validity_cell =
623
        LoadObjectField(handler, ICHandler::kValidityCellOffset);
624
    CheckPrototypeValidityCell(maybe_validity_cell, miss);
625
  }
626

627 628 629 630 631 632 633 634 635
  //
  // 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);
636

637 638
      CSA_ASSERT(this, IsCodeMap(LoadMap(smi_or_code_handler)));
      on_code_handler(smi_or_code_handler);
639

640 641 642 643 644 645 646 647 648 649 650 651
      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;
652
    if (ic_mode == ICMode::kGlobalIC) {
653
      CSA_ASSERT(this, IsClearWord(handler_flags, mask));
654 655
    } else {
      DCHECK_EQ(ICMode::kNonGlobalIC, ic_mode);
656 657 658 659 660 661 662

      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)));
663 664
      Branch(IsSetWord<LoadHandler::DoAccessCheckOnReceiverBits>(handler_flags),
             &if_do_access_check, &if_lookup_on_receiver);
665

666 667
      BIND(&if_do_access_check);
      {
668
        TNode<MaybeObject> data2 = LoadHandlerDataField(handler, 2);
669 670 671
        CSA_ASSERT(this, IsWeakOrCleared(data2));
        TNode<Object> expected_native_context =
            GetHeapObjectAssumeWeak(data2, miss);
672 673 674 675 676 677 678 679 680 681 682 683 684
        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)));

685 686 687
        TNode<NameDictionary> properties =
            CAST(LoadSlowProperties(p->receiver));
        TVARIABLE(IntPtrT, var_name_index);
688
        Label found(this, &var_name_index);
689
        NameDictionaryLookup<NameDictionary>(properties, CAST(p->name), &found,
690 691 692 693 694 695 696 697 698 699 700 701
                                             &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);
702
    }
703 704 705
    return smi_or_code_handler;
  }
}
706

707 708 709 710 711 712
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());
713

714 715 716 717 718 719
  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) {
720 721
        VARIABLE(var_details, MachineRepresentation::kWord32);
        VARIABLE(var_value, MachineRepresentation::kTagged);
722 723
        LoadPropertyFromNameDictionary(properties, name_index, &var_details,
                                       &var_value);
724 725 726 727
        Node* value =
            CallGetterIfAccessor(var_value.value(), var_details.value(),
                                 p->context, p->receiver, miss);
        exit_point->Return(value);
728 729
      },
      miss, ic_mode);
730

731
  TNode<MaybeObject> maybe_holder = LoadHandlerDataField(handler, 1);
732

733
  Label load_from_cached_holder(this), done(this);
734

735 736
  Branch(IsStrongReferenceTo(maybe_holder, NullConstant()), &done,
         &load_from_cached_holder);
737

738
  BIND(&load_from_cached_holder);
739
  {
740
    // For regular holders, having passed the receiver map check and the
741 742
    // validity cell check implies that |holder| is alive. However, for global
    // object receivers, |maybe_holder| may be cleared.
743 744
    CSA_ASSERT(this, IsWeakOrCleared(maybe_holder));
    Node* holder = GetHeapObjectAssumeWeak(maybe_holder, miss);
745

746 747
    var_holder->Bind(holder);
    Goto(&done);
748
  }
749

750
  BIND(&done);
751 752 753 754
  {
    var_smi_handler->Bind(smi_handler);
    Goto(if_smi_handler);
  }
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
}

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);
773 774
}

775 776
void AccessorAssembler::JumpIfDataProperty(Node* details, Label* writable,
                                           Label* readonly) {
777 778 779 780 781 782 783 784
  if (readonly) {
    // Accessor properties never have the READ_ONLY attribute set.
    GotoIf(IsSetWord32(details, PropertyDetails::kAttributesReadOnlyMask),
           readonly);
  } else {
    CSA_ASSERT(this, IsNotSetWord32(details,
                                    PropertyDetails::kAttributesReadOnlyMask));
  }
785 786 787 788 789
  Node* kind = DecodeWord32<PropertyDetails::KindField>(details);
  GotoIf(Word32Equal(kind, Int32Constant(kData)), writable);
  // Fall through if it's an accessor property.
}

790 791
void AccessorAssembler::HandleStoreICNativeDataProperty(
    const StoreICParameters* p, Node* holder, Node* handler_word) {
792
  Comment("native_data_property_store");
793 794
  TNode<IntPtrT> descriptor =
      Signed(DecodeWord<StoreHandler::DescriptorBits>(handler_word));
795 796 797 798
  Node* accessor_info = LoadDescriptorValue(LoadMap(holder), descriptor);
  CSA_CHECK(this, IsAccessorInfo(accessor_info));

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

802
void AccessorAssembler::HandleStoreICHandlerCase(
803 804
    const StoreICParameters* p, TNode<MaybeObject> handler, Label* miss,
    ICMode ic_mode, ElementSupport support_elements) {
805
  Label if_smi_handler(this), if_nonsmi_handler(this);
806
  Label if_proto_handler(this), if_element_handler(this), call_handler(this),
807
      store_transition_or_global(this);
808

809
  Branch(TaggedIsSmi(handler), &if_smi_handler, &if_nonsmi_handler);
810 811 812

  // |handler| is a Smi, encoding what to do. See SmiHandler methods
  // for the encoding format.
813
  BIND(&if_smi_handler);
814 815
  {
    Node* holder = p->receiver;
816
    Node* handler_word = SmiUntag(CAST(handler));
817

818 819
    Label if_fast_smi(this), if_proxy(this);

820 821
    STATIC_ASSERT(StoreHandler::kGlobalProxy + 1 == StoreHandler::kNormal);
    STATIC_ASSERT(StoreHandler::kNormal + 1 == StoreHandler::kProxy);
822 823 824 825
    STATIC_ASSERT(StoreHandler::kProxy + 1 == StoreHandler::kKindsNumber);

    Node* handler_kind = DecodeWord<StoreHandler::KindBits>(handler_word);
    GotoIf(IntPtrLessThan(handler_kind,
826
                          IntPtrConstant(StoreHandler::kGlobalProxy)),
827 828 829
           &if_fast_smi);
    GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kProxy)),
           &if_proxy);
830 831
    CSA_ASSERT(this,
               WordEqual(handler_kind, IntPtrConstant(StoreHandler::kNormal)));
832
    TNode<NameDictionary> properties = CAST(LoadSlowProperties(holder));
833

834
    TVARIABLE(IntPtrT, var_name_index);
835
    Label dictionary_found(this, &var_name_index);
836 837
    NameDictionaryLookup<NameDictionary>(
        properties, CAST(p->name), &dictionary_found, &var_name_index, miss);
838
    BIND(&dictionary_found);
839 840 841 842 843 844 845 846 847 848 849 850 851 852
    {
      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);
    }

853
    BIND(&if_fast_smi);
854 855
    {
      Node* handler_kind = DecodeWord<StoreHandler::KindBits>(handler_word);
856

857 858 859 860 861 862
      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);
863

864 865 866 867 868 869 870 871
      BIND(&accessor);
      HandleStoreAccessor(p, holder, handler_word);

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

      BIND(&data);
      // Handle non-transitioning field stores.
872
      HandleStoreICSmiHandlerCase(handler_word, holder, p->value, miss);
873
    }
874 875

    BIND(&if_proxy);
876
    HandleStoreToProxy(p, holder, miss, support_elements);
877 878
  }

879
  BIND(&if_nonsmi_handler);
880
  {
881
    GotoIf(IsWeakOrCleared(handler), &store_transition_or_global);
882
    TNode<HeapObject> strong_handler = CAST(handler);
883
    TNode<Map> handler_map = LoadMap(strong_handler);
884
    Branch(IsCodeMap(handler_map), &call_handler, &if_proto_handler);
885

886 887 888 889 890
    BIND(&if_proto_handler);
    {
      HandleStoreICProtoHandler(p, CAST(strong_handler), miss, ic_mode,
                                support_elements);
    }
891

892 893 894
    // |handler| is a heap object. Must be code, call it.
    BIND(&call_handler);
    {
895 896 897
      TailCallStub(StoreWithVectorDescriptor{}, CAST(strong_handler),
                   CAST(p->context), p->receiver, p->name, p->value, p->slot,
                   p->vector);
898
    }
899
  }
900

901
  BIND(&store_transition_or_global);
902 903
  {
    // Load value or miss if the {handler} weak cell is cleared.
904 905 906
    CSA_ASSERT(this, IsWeakOrCleared(handler));
    TNode<HeapObject> map_or_property_cell =
        GetHeapObjectAssumeWeak(handler, miss);
907 908 909 910 911 912 913 914 915 916 917 918 919 920

    Label store_global(this), store_transition(this);
    Branch(IsMap(map_or_property_cell), &store_transition, &store_global);

    BIND(&store_global);
    {
      TNode<PropertyCell> property_cell = CAST(map_or_property_cell);
      ExitPoint direct_exit(this);
      StoreGlobalIC_PropertyCellCase(property_cell, p->value, &direct_exit,
                                     miss);
    }
    BIND(&store_transition);
    {
      TNode<Map> map = CAST(map_or_property_cell);
921 922
      HandleStoreICTransitionMapHandlerCase(p, map, miss,
                                            kCheckPrototypeValidity);
923 924
      Return(p->value);
    }
925
  }
926 927 928
}

void AccessorAssembler::HandleStoreICTransitionMapHandlerCase(
929
    const StoreICParameters* p, TNode<Map> transition_map, Label* miss,
930 931 932 933 934 935 936
    StoreTransitionMapFlags flags) {
  DCHECK_EQ(0, flags & ~kStoreTransitionMapFlagsMask);
  if (flags & kCheckPrototypeValidity) {
    Node* maybe_validity_cell =
        LoadObjectField(transition_map, Map::kPrototypeValidityCellOffset);
    CheckPrototypeValidityCell(maybe_validity_cell, miss);
  }
937 938 939 940 941 942 943 944

  TNode<Uint32T> bitfield3 = LoadMapBitField3(transition_map);
  CSA_ASSERT(this, IsClearWord32<Map::IsDictionaryMapBit>(bitfield3));
  GotoIf(IsSetWord32<Map::IsDeprecatedBit>(bitfield3), miss);

  // Load last descriptor details.
  Node* nof = DecodeWordFromWord32<Map::NumberOfOwnDescriptorsBits>(bitfield3);
  CSA_ASSERT(this, WordNotEqual(nof, IntPtrConstant(0)));
945
  TNode<DescriptorArray> descriptors = LoadMapDescriptors(transition_map);
946 947

  Node* factor = IntPtrConstant(DescriptorArray::kEntrySize);
948 949
  TNode<IntPtrT> last_key_index = UncheckedCast<IntPtrT>(IntPtrAdd(
      IntPtrConstant(DescriptorArray::ToKeyIndex(-1)), IntPtrMul(nof, factor)));
950
  if (flags & kValidateTransitionHandler) {
951
    TNode<Name> key = LoadKeyByKeyIndex(descriptors, last_key_index);
952 953
    GotoIf(WordNotEqual(key, p->name), miss);
  } else {
954 955
    CSA_ASSERT(this, WordEqual(LoadKeyByKeyIndex(descriptors, last_key_index),
                               p->name));
956
  }
957
  Node* details = LoadDetailsByKeyIndex(descriptors, last_key_index);
958
  if (flags & kValidateTransitionHandler) {
959 960 961 962
    // Follow transitions only in the following cases:
    // 1) name is a non-private symbol and attributes equal to NONE,
    // 2) name is a private symbol and attributes equal to DONT_ENUM.
    Label attributes_ok(this);
963 964
    const int kKindAndAttributesDontDeleteReadOnlyMask =
        PropertyDetails::KindField::kMask |
965 966
        PropertyDetails::kAttributesDontDeleteMask |
        PropertyDetails::kAttributesReadOnlyMask;
967 968 969 970 971
    STATIC_ASSERT(kData == 0);
    // Both DontDelete and ReadOnly attributes must not be set and it has to be
    // a kData property.
    GotoIf(IsSetWord32(details, kKindAndAttributesDontDeleteReadOnlyMask),
           miss);
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

    // DontEnum attribute is allowed only for private symbols and vice versa.
    Branch(Word32Equal(
               IsSetWord32(details, PropertyDetails::kAttributesDontEnumMask),
               IsPrivateSymbol(p->name)),
           &attributes_ok, miss);

    BIND(&attributes_ok);
  }

  OverwriteExistingFastDataProperty(p->receiver, transition_map, descriptors,
                                    last_key_index, details, p->value, miss,
                                    true);
}

987 988 989
void AccessorAssembler::CheckFieldType(TNode<DescriptorArray> descriptors,
                                       Node* name_index, Node* representation,
                                       Node* value, Label* bailout) {
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
  Label r_smi(this), r_double(this), r_heapobject(this), all_fine(this);
  // Ignore FLAG_track_fields etc. and always emit code for all checks,
  // because this builtin is part of the snapshot and therefore should
  // be flag independent.
  GotoIf(Word32Equal(representation, Int32Constant(Representation::kSmi)),
         &r_smi);
  GotoIf(Word32Equal(representation, Int32Constant(Representation::kDouble)),
         &r_double);
  GotoIf(
      Word32Equal(representation, Int32Constant(Representation::kHeapObject)),
      &r_heapobject);
  GotoIf(Word32Equal(representation, Int32Constant(Representation::kNone)),
         bailout);
  CSA_ASSERT(this, Word32Equal(representation,
                               Int32Constant(Representation::kTagged)));
  Goto(&all_fine);

  BIND(&r_smi);
  { Branch(TaggedIsSmi(value), &all_fine, bailout); }

  BIND(&r_double);
  {
    GotoIf(TaggedIsSmi(value), &all_fine);
    Node* value_map = LoadMap(value);
    // While supporting mutable HeapNumbers would be straightforward, such
    // objects should not end up here anyway.
1016 1017
    CSA_ASSERT(this, WordNotEqual(value_map,
                                  LoadRoot(RootIndex::kMutableHeapNumberMap)));
1018 1019 1020 1021 1022 1023
    Branch(IsHeapNumberMap(value_map), &all_fine, bailout);
  }

  BIND(&r_heapobject);
  {
    GotoIf(TaggedIsSmi(value), bailout);
1024 1025
    TNode<MaybeObject> field_type = LoadFieldTypeByKeyIndex(
        descriptors, UncheckedCast<IntPtrT>(name_index));
1026 1027
    const Address kNoneType = FieldType::None().ptr();
    const Address kAnyType = FieldType::Any().ptr();
1028 1029
    DCHECK_NE(static_cast<uint32_t>(kNoneType), kClearedWeakHeapObjectLower32);
    DCHECK_NE(static_cast<uint32_t>(kAnyType), kClearedWeakHeapObjectLower32);
1030
    // FieldType::None can't hold any value.
1031 1032 1033
    GotoIf(WordEqual(BitcastMaybeObjectToWord(field_type),
                     IntPtrConstant(kNoneType)),
           bailout);
1034
    // FieldType::Any can hold any value.
1035 1036 1037 1038 1039
    GotoIf(WordEqual(BitcastMaybeObjectToWord(field_type),
                     IntPtrConstant(kAnyType)),
           &all_fine);
    // Cleared weak references count as FieldType::None, which can't hold any
    // value.
1040 1041
    TNode<Map> field_type_map =
        CAST(GetHeapObjectAssumeWeak(field_type, bailout));
1042
    // FieldType::Class(...) performs a map check.
1043
    Branch(WordEqual(LoadMap(value), field_type_map), &all_fine, bailout);
1044 1045 1046 1047 1048
  }

  BIND(&all_fine);
}

1049 1050 1051 1052 1053
TNode<BoolT> AccessorAssembler::IsPropertyDetailsConst(Node* details) {
  return Word32Equal(DecodeWord32<PropertyDetails::ConstnessField>(details),
                     Int32Constant(static_cast<int32_t>(VariableMode::kConst)));
}

1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
void AccessorAssembler::OverwriteExistingFastDataProperty(
    Node* object, Node* object_map, Node* descriptors,
    Node* descriptor_name_index, Node* details, Node* value, Label* slow,
    bool do_transitioning_store) {
  Label done(this), if_field(this), if_descriptor(this);

  CSA_ASSERT(this,
             Word32Equal(DecodeWord32<PropertyDetails::KindField>(details),
                         Int32Constant(kData)));

  Branch(Word32Equal(DecodeWord32<PropertyDetails::LocationField>(details),
                     Int32Constant(kField)),
         &if_field, &if_descriptor);

  BIND(&if_field);
  {
    Node* representation =
        DecodeWord32<PropertyDetails::RepresentationField>(details);

1073 1074
    CheckFieldType(CAST(descriptors), descriptor_name_index, representation,
                   value, slow);
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

    Node* field_index =
        DecodeWordFromWord32<PropertyDetails::FieldIndexField>(details);
    field_index = IntPtrAdd(field_index,
                            LoadMapInobjectPropertiesStartInWords(object_map));
    Node* instance_size_in_words = LoadMapInstanceSizeInWords(object_map);

    Label inobject(this), backing_store(this);
    Branch(UintPtrLessThan(field_index, instance_size_in_words), &inobject,
           &backing_store);

    BIND(&inobject);
    {
1088
      Node* field_offset = TimesTaggedSize(field_index);
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
      Label tagged_rep(this), double_rep(this);
      Branch(
          Word32Equal(representation, Int32Constant(Representation::kDouble)),
          &double_rep, &tagged_rep);
      BIND(&double_rep);
      {
        Node* double_value = ChangeNumberToFloat64(value);
        if (FLAG_unbox_double_fields) {
          if (do_transitioning_store) {
            StoreMap(object, object_map);
1099 1100 1101 1102 1103 1104 1105
          } else if (FLAG_track_constant_fields) {
            Label if_mutable(this);
            GotoIfNot(IsPropertyDetailsConst(details), &if_mutable);
            Node* current_value =
                LoadObjectField(object, field_offset, MachineType::Float64());
            Branch(Float64Equal(current_value, double_value), &done, slow);
            BIND(&if_mutable);
1106 1107 1108 1109 1110 1111
          }
          StoreObjectFieldNoWriteBarrier(object, field_offset, double_value,
                                         MachineRepresentation::kFloat64);
        } else {
          if (do_transitioning_store) {
            Node* mutable_heap_number =
1112
                AllocateMutableHeapNumberWithValue(double_value);
1113 1114 1115 1116
            StoreMap(object, object_map);
            StoreObjectField(object, field_offset, mutable_heap_number);
          } else {
            Node* mutable_heap_number = LoadObjectField(object, field_offset);
1117 1118 1119 1120 1121 1122 1123
            if (FLAG_track_constant_fields) {
              Label if_mutable(this);
              GotoIfNot(IsPropertyDetailsConst(details), &if_mutable);
              Node* current_value = LoadHeapNumberValue(mutable_heap_number);
              Branch(Float64Equal(current_value, double_value), &done, slow);
              BIND(&if_mutable);
            }
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
            StoreHeapNumberValue(mutable_heap_number, double_value);
          }
        }
        Goto(&done);
      }

      BIND(&tagged_rep);
      {
        if (do_transitioning_store) {
          StoreMap(object, object_map);
1134 1135 1136 1137 1138 1139 1140
        } else if (FLAG_track_constant_fields) {
          Label if_mutable(this);
          GotoIfNot(IsPropertyDetailsConst(details), &if_mutable);
          Node* current_value =
              LoadObjectField(object, field_offset, MachineType::AnyTagged());
          Branch(WordEqual(current_value, value), &done, slow);
          BIND(&if_mutable);
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
        }
        StoreObjectField(object, field_offset, value);
        Goto(&done);
      }
    }

    BIND(&backing_store);
    {
      Node* backing_store_index =
          IntPtrSub(field_index, instance_size_in_words);

      if (do_transitioning_store) {
        // Allocate mutable heap number before extending properties backing
        // store to ensure that heap verifier will not see the heap in
        // inconsistent state.
        VARIABLE(var_value, MachineRepresentation::kTagged, value);
        {
          Label cont(this);
          GotoIf(Word32NotEqual(representation,
                                Int32Constant(Representation::kDouble)),
                 &cont);
          {
            Node* double_value = ChangeNumberToFloat64(value);
            Node* mutable_heap_number =
1165
                AllocateMutableHeapNumberWithValue(double_value);
1166 1167 1168 1169 1170 1171
            var_value.Bind(mutable_heap_number);
            Goto(&cont);
          }
          BIND(&cont);
        }

1172 1173 1174 1175
        TNode<PropertyArray> properties =
            CAST(ExtendPropertiesBackingStore(object, backing_store_index));
        StorePropertyArrayElement(properties, backing_store_index,
                                  var_value.value());
1176 1177 1178 1179 1180
        StoreMap(object, object_map);
        Goto(&done);

      } else {
        Label tagged_rep(this), double_rep(this);
1181
        TNode<PropertyArray> properties = CAST(LoadFastProperties(object));
1182 1183 1184 1185 1186 1187
        Branch(
            Word32Equal(representation, Int32Constant(Representation::kDouble)),
            &double_rep, &tagged_rep);
        BIND(&double_rep);
        {
          Node* mutable_heap_number =
1188
              LoadPropertyArrayElement(properties, backing_store_index);
1189
          Node* double_value = ChangeNumberToFloat64(value);
1190 1191 1192 1193 1194 1195 1196
          if (FLAG_track_constant_fields) {
            Label if_mutable(this);
            GotoIfNot(IsPropertyDetailsConst(details), &if_mutable);
            Node* current_value = LoadHeapNumberValue(mutable_heap_number);
            Branch(Float64Equal(current_value, double_value), &done, slow);
            BIND(&if_mutable);
          }
1197 1198 1199 1200 1201
          StoreHeapNumberValue(mutable_heap_number, double_value);
          Goto(&done);
        }
        BIND(&tagged_rep);
        {
1202 1203 1204 1205 1206 1207 1208 1209
          if (FLAG_track_constant_fields) {
            Label if_mutable(this);
            GotoIfNot(IsPropertyDetailsConst(details), &if_mutable);
            Node* current_value =
                LoadPropertyArrayElement(properties, backing_store_index);
            Branch(WordEqual(current_value, value), &done, slow);
            BIND(&if_mutable);
          }
1210
          StorePropertyArrayElement(properties, backing_store_index, value);
1211 1212 1213 1214 1215 1216 1217 1218 1219
          Goto(&done);
        }
      }
    }
  }

  BIND(&if_descriptor);
  {
    // Check that constant matches value.
1220 1221
    Node* constant = LoadValueByKeyIndex(
        CAST(descriptors), UncheckedCast<IntPtrT>(descriptor_name_index));
1222 1223 1224 1225 1226 1227
    GotoIf(WordNotEqual(value, constant), slow);

    if (do_transitioning_store) {
      StoreMap(object, object_map);
    }
    Goto(&done);
1228
  }
1229
  BIND(&done);
1230 1231
}

1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
void AccessorAssembler::CheckPrototypeValidityCell(Node* maybe_validity_cell,
                                                   Label* miss) {
  Label done(this);
  GotoIf(WordEqual(maybe_validity_cell, SmiConstant(Map::kPrototypeChainValid)),
         &done);
  CSA_ASSERT(this, TaggedIsNotSmi(maybe_validity_cell));

  Node* cell_value = LoadObjectField(maybe_validity_cell, Cell::kValueOffset);
  Branch(WordEqual(cell_value, SmiConstant(Map::kPrototypeChainValid)), &done,
         miss);

  BIND(&done);
}

1246 1247 1248
void AccessorAssembler::HandleStoreAccessor(const StoreICParameters* p,
                                            Node* holder, Node* handler_word) {
  Comment("accessor_store");
1249 1250
  TNode<IntPtrT> descriptor =
      Signed(DecodeWord<StoreHandler::DescriptorBits>(handler_word));
1251 1252 1253 1254 1255 1256 1257 1258 1259
  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));
}

1260
void AccessorAssembler::HandleStoreICProtoHandler(
1261 1262
    const StoreICParameters* p, TNode<StoreHandler> handler, Label* miss,
    ICMode ic_mode, ElementSupport support_elements) {
1263 1264
  Comment("HandleStoreICProtoHandler");

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
  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);
      {
1275 1276
        TailCallStub(StoreWithVectorDescriptor{}, code_handler, p->context,
                     p->receiver, p->name, p->value, p->slot, p->vector);
1277 1278 1279 1280
      }

      BIND(&if_transitioning_element_store);
      {
1281 1282 1283
        TNode<MaybeObject> maybe_transition_map =
            LoadHandlerDataField(handler, 1);
        TNode<Map> transition_map =
1284
            CAST(GetHeapObjectAssumeWeak(maybe_transition_map, miss));
1285 1286 1287

        GotoIf(IsDeprecatedMap(transition_map), miss);

1288 1289 1290
        TailCallStub(StoreTransitionDescriptor{}, code_handler, p->context,
                     p->receiver, p->name, transition_map, p->value, p->slot,
                     p->vector);
1291 1292 1293 1294 1295 1296
      }
    };
  }

  Node* smi_handler = HandleProtoHandler<StoreHandler>(
      p, handler, on_code_handler,
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
      // on_found_on_receiver
      [=](Node* properties, Node* name_index) {
        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);
1307

1308 1309
        StoreValueByKeyIndex<NameDictionary>(
            CAST(properties), UncheckedCast<IntPtrT>(name_index), p->value);
1310 1311 1312
        Return(p->value);
      },
      miss, ic_mode);
1313 1314

  {
1315 1316
    Label if_add_normal(this), if_store_global_proxy(this), if_api_setter(this),
        if_accessor(this), if_native_data_property(this);
1317

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

1321
    Node* handler_kind = DecodeWord<StoreHandler::KindBits>(handler_word);
1322
    GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kNormal)),
1323
           &if_add_normal);
1324

1325
    TNode<MaybeObject> maybe_holder = LoadHandlerDataField(handler, 1);
1326 1327
    CSA_ASSERT(this, IsWeakOrCleared(maybe_holder));
    TNode<Object> holder = GetHeapObjectAssumeWeak(maybe_holder, miss);
1328

1329
    GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kGlobalProxy)),
1330
           &if_store_global_proxy);
1331

1332 1333 1334
    GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kAccessor)),
           &if_accessor);

1335 1336 1337 1338
    GotoIf(WordEqual(handler_kind,
                     IntPtrConstant(StoreHandler::kNativeDataProperty)),
           &if_native_data_property);

1339 1340 1341 1342 1343 1344 1345
    GotoIf(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kApiSetter)),
           &if_api_setter);

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

1346 1347 1348 1349
    CSA_ASSERT(this,
               WordEqual(handler_kind, IntPtrConstant(StoreHandler::kProxy)));
    HandleStoreToProxy(p, holder, miss, support_elements);

1350 1351 1352 1353 1354 1355 1356
    BIND(&if_add_normal);
    {
      // This is a case of "transitioning store" to a dictionary mode object
      // when the property is still does not exist. The "existing property"
      // case is covered above by LookupOnReceiver bit handling of the smi
      // handler.
      Label slow(this);
1357
      TNode<Map> receiver_map = LoadMap(p->receiver);
1358 1359
      InvalidateValidityCellIfPrototype(receiver_map);

1360 1361
      TNode<NameDictionary> properties = CAST(LoadSlowProperties(p->receiver));
      Add<NameDictionary>(properties, CAST(p->name), p->value, &slow);
1362 1363 1364 1365 1366 1367 1368
      Return(p->value);

      BIND(&slow);
      TailCallRuntime(Runtime::kAddDictionaryProperty, p->context, p->receiver,
                      p->name, p->value);
    }

1369 1370 1371
    BIND(&if_accessor);
    HandleStoreAccessor(p, holder, handler_word);

1372 1373 1374
    BIND(&if_native_data_property);
    HandleStoreICNativeDataProperty(p, holder, handler_word);

1375 1376 1377
    BIND(&if_api_setter);
    {
      Comment("api_setter");
1378 1379 1380 1381 1382
      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.
1383
      TNode<MaybeObject> maybe_context = Select<MaybeObject>(
1384
          IsSetWord<LoadHandler::DoAccessCheckOnReceiverBits>(handler_word),
1385 1386 1387
          [=] { return LoadHandlerDataField(handler, 3); },
          [=] { return LoadHandlerDataField(handler, 2); });

1388 1389 1390 1391
      CSA_ASSERT(this, IsWeakOrCleared(maybe_context));
      TNode<Object> context = Select<Object>(
          IsCleared(maybe_context), [=] { return SmiConstant(0); },
          [=] { return GetHeapObjectAssumeWeak(maybe_context); });
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413

      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);
1414
      Callable callable = CodeFactory::CallApiCallback(isolate());
1415
      TNode<IntPtrT> argc = IntPtrConstant(1);
1416
      Return(CallStub(callable, context, callback, argc, data,
1417
                      api_holder.value(), p->receiver, p->value));
1418 1419
    }

1420 1421 1422
    BIND(&if_store_global_proxy);
    {
      ExitPoint direct_exit(this);
1423
      StoreGlobalIC_PropertyCellCase(holder, p->value, &direct_exit, miss);
1424
    }
1425 1426 1427
  }
}

1428 1429
void AccessorAssembler::HandleStoreToProxy(const StoreICParameters* p,
                                           Node* proxy, Label* miss,
1430
                                           ElementSupport support_elements) {
1431 1432 1433
  VARIABLE(var_index, MachineType::PointerRepresentation());
  VARIABLE(var_unique, MachineRepresentation::kTagged);

1434
  Label if_index(this), if_unique_name(this),
1435
      to_name_failed(this, Label::kDeferred);
1436

1437 1438 1439 1440 1441 1442
  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,
1443
                var_unique.value(), p->value, p->receiver);
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
    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,
1454
                    p->name, p->value, p->receiver);
1455
  } else {
1456
    Node* name = CallBuiltin(Builtins::kToName, p->context, p->name);
1457
    TailCallBuiltin(Builtins::kProxySetProperty, p->context, proxy, name,
1458
                    p->value, p->receiver);
1459 1460 1461
  }
}

1462 1463 1464
void AccessorAssembler::HandleStoreICSmiHandlerCase(Node* handler_word,
                                                    Node* holder, Node* value,
                                                    Label* miss) {
1465
  Comment("field store");
1466 1467
#ifdef DEBUG
  Node* handler_kind = DecodeWord<StoreHandler::KindBits>(handler_word);
1468
  if (FLAG_track_constant_fields) {
1469 1470
    CSA_ASSERT(
        this,
1471 1472 1473
        Word32Or(WordEqual(handler_kind, IntPtrConstant(StoreHandler::kField)),
                 WordEqual(handler_kind,
                           IntPtrConstant(StoreHandler::kConstField))));
1474
  } else {
1475 1476
    CSA_ASSERT(this,
               WordEqual(handler_kind, IntPtrConstant(StoreHandler::kField)));
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
  }
#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);

1497
  BIND(&if_tagged_field);
1498 1499 1500
  {
    Comment("store tagged field");
    HandleStoreFieldAndReturn(handler_word, holder, Representation::Tagged(),
1501
                              value, miss);
1502 1503
  }

1504
  BIND(&if_double_field);
1505 1506 1507
  {
    Comment("store double field");
    HandleStoreFieldAndReturn(handler_word, holder, Representation::Double(),
1508
                              value, miss);
1509 1510
  }

1511
  BIND(&if_heap_object_field);
1512 1513
  {
    Comment("store heap object field");
1514
    HandleStoreFieldAndReturn(handler_word, holder,
1515
                              Representation::HeapObject(), value, miss);
1516 1517
  }

1518
  BIND(&if_smi_field);
1519 1520 1521
  {
    Comment("store smi field");
    HandleStoreFieldAndReturn(handler_word, holder, Representation::Smi(),
1522
                              value, miss);
1523 1524 1525
  }
}

1526 1527 1528
void AccessorAssembler::HandleStoreFieldAndReturn(Node* handler_word,
                                                  Node* holder,
                                                  Representation representation,
1529 1530 1531
                                                  Node* value, Label* miss) {
  Node* prepared_value =
      PrepareValueForStore(handler_word, holder, representation, value, miss);
1532 1533 1534 1535 1536

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

1537
  BIND(&if_inobject);
1538
  {
1539
    StoreNamedField(handler_word, holder, true, representation, prepared_value,
1540
                    miss);
1541 1542 1543
    Return(value);
  }

1544
  BIND(&if_out_of_object);
1545
  {
1546
    StoreNamedField(handler_word, holder, false, representation, prepared_value,
1547
                    miss);
1548 1549 1550 1551
    Return(value);
  }
}

1552 1553
Node* AccessorAssembler::PrepareValueForStore(Node* handler_word, Node* holder,
                                              Representation representation,
1554
                                              Node* value, Label* bailout) {
1555 1556 1557 1558 1559
  if (representation.IsDouble()) {
    value = TryTaggedToFloat64(value, bailout);

  } else if (representation.IsHeapObject()) {
    GotoIf(TaggedIsSmi(value), bailout);
1560 1561

    Label done(this);
1562
    if (FLAG_track_constant_fields) {
1563 1564 1565
      // Skip field type check in favor of constant value check when storing
      // to constant field.
      GotoIf(WordEqual(DecodeWord<StoreHandler::KindBits>(handler_word),
1566
                       IntPtrConstant(StoreHandler::kConstField)),
1567 1568
             &done);
    }
1569 1570
    TNode<IntPtrT> descriptor =
        Signed(DecodeWord<StoreHandler::DescriptorBits>(handler_word));
1571 1572
    TNode<MaybeObject> maybe_field_type =
        LoadDescriptorValueOrFieldType(LoadMap(holder), descriptor);
1573 1574 1575 1576

    GotoIf(TaggedIsSmi(maybe_field_type), &done);
    // Check that value type matches the field type.
    {
1577
      Node* field_type = GetHeapObjectAssumeWeak(maybe_field_type, bailout);
1578 1579
      Branch(WordEqual(LoadMap(value), field_type), &done, bailout);
    }
1580
    BIND(&done);
1581 1582

  } else if (representation.IsSmi()) {
1583
    GotoIfNot(TaggedIsSmi(value), bailout);
1584 1585 1586 1587 1588 1589 1590

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

1591 1592
Node* AccessorAssembler::ExtendPropertiesBackingStore(Node* object,
                                                      Node* index) {
1593 1594
  Comment("[ Extend storage");

1595
  ParameterMode mode = OptimalParameterMode();
1596

1597 1598 1599
  // 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);
1600
  VARIABLE(var_encoded_hash, MachineRepresentation::kWord32);
1601
  VARIABLE(var_length, ParameterRepresentation(mode));
1602

1603 1604 1605 1606 1607 1608 1609 1610
  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);
  {
1611
    Node* hash = SmiToInt32(properties);
1612 1613 1614
    Node* encoded_hash =
        Word32Shl(hash, Int32Constant(PropertyArray::HashField::kShift));
    var_encoded_hash.Bind(encoded_hash);
1615 1616 1617 1618 1619 1620 1621 1622
    var_length.Bind(IntPtrOrSmiConstant(0, mode));
    var_properties.Bind(EmptyFixedArrayConstant());
    Goto(&extend_store);
  }

  BIND(&if_property_array);
  {
    Node* length_and_hash_int32 = LoadAndUntagToWord32ObjectField(
1623
        var_properties.value(), PropertyArray::kLengthAndHashOffset);
1624 1625 1626 1627 1628
    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)));
1629
    Node* length = IntPtrToParameter(length_intptr, mode);
1630 1631 1632
    var_length.Bind(length);
    Goto(&extend_store);
  }
1633

1634 1635
  BIND(&extend_store);
  {
1636 1637 1638
    VARIABLE(var_new_properties, MachineRepresentation::kTaggedPointer,
             var_properties.value());
    Label done(this);
1639 1640 1641
    // 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.
1642 1643
    GotoIf(UintPtrLessThan(index, ParameterToIntPtr(var_length.value(), mode)),
           &done);
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660

    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);
1661
    var_new_properties.Bind(new_properties);
1662 1663 1664 1665 1666 1667 1668

    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,
1669 1670
                            var_length.value(), SKIP_WRITE_BARRIER, mode,
                            DestroySource::kYes);
1671 1672 1673 1674

    // TODO(gsathya): Clean up the type conversions by creating smarter
    // helpers that do the correct op based on the mode.
    Node* new_capacity_int32 =
1675
        TruncateIntPtrToInt32(ParameterToIntPtr(new_capacity, mode));
1676
    Node* new_length_and_hash_int32 =
1677
        Word32Or(var_encoded_hash.value(), new_capacity_int32);
1678
    StoreObjectField(new_properties, PropertyArray::kLengthAndHashOffset,
1679
                     SmiFromInt32(new_length_and_hash_int32));
1680 1681 1682
    StoreObjectField(object, JSObject::kPropertiesOrHashOffset, new_properties);
    Comment("] Extend storage");
    Goto(&done);
1683 1684
    BIND(&done);
    return var_new_properties.value();
1685
  }
1686 1687
}

1688 1689 1690
void AccessorAssembler::StoreNamedField(Node* handler_word, Node* object,
                                        bool is_inobject,
                                        Representation representation,
1691
                                        Node* value, Label* bailout) {
1692 1693 1694
  bool store_value_as_double = representation.IsDouble();
  Node* property_storage = object;
  if (!is_inobject) {
1695
    property_storage = LoadFastProperties(object);
1696 1697
  }

1698
  Node* index = DecodeWord<StoreHandler::FieldIndexBits>(handler_word);
1699
  Node* offset = IntPtrMul(index, IntPtrConstant(kTaggedSize));
1700 1701
  if (representation.IsDouble()) {
    if (!FLAG_unbox_double_fields || !is_inobject) {
1702 1703 1704 1705
      // Load the mutable heap number.
      property_storage = LoadObjectField(property_storage, offset);
      // Store the double value into it.
      offset = IntPtrConstant(HeapNumber::kValueOffset);
1706 1707 1708
    }
  }

1709
  // Do constant value check if necessary.
1710
  if (FLAG_track_constant_fields) {
1711
    Label done(this);
1712
    GotoIfNot(WordEqual(DecodeWord<StoreHandler::KindBits>(handler_word),
1713
                        IntPtrConstant(StoreHandler::kConstField)),
1714
              &done);
1715 1716 1717 1718
    {
      if (store_value_as_double) {
        Node* current_value =
            LoadObjectField(property_storage, offset, MachineType::Float64());
1719
        GotoIfNot(Float64Equal(current_value, value), bailout);
1720 1721
      } else {
        Node* current_value = LoadObjectField(property_storage, offset);
1722
        GotoIfNot(WordEqual(current_value, value), bailout);
1723 1724 1725
      }
      Goto(&done);
    }
1726
    BIND(&done);
1727 1728 1729
  }

  // Do the store.
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
  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);
  }
}

1740 1741 1742 1743 1744
void AccessorAssembler::EmitFastElementsBoundsCheck(Node* object,
                                                    Node* elements,
                                                    Node* intptr_index,
                                                    Node* is_jsarray_condition,
                                                    Label* miss) {
1745
  VARIABLE(var_length, MachineType::PointerRepresentation());
1746 1747 1748 1749 1750 1751 1752
  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);
  }
1753
  BIND(&if_array);
1754
  {
1755
    var_length.Bind(SmiUntag(LoadFastJSArrayLength(object)));
1756 1757
    Goto(&length_loaded);
  }
1758
  BIND(&length_loaded);
1759
  GotoIfNot(UintPtrLessThan(intptr_index, var_length.value()), miss);
1760 1761
}

1762
void AccessorAssembler::EmitElementLoad(
1763 1764 1765 1766 1767
    Node* object, Node* elements, Node* elements_kind,
    SloppyTNode<IntPtrT> 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) {
1768 1769 1770 1771
  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(
1772
      Int32GreaterThan(elements_kind, Int32Constant(LAST_FAST_ELEMENTS_KIND)),
1773 1774 1775 1776 1777
      &if_nonfast);

  EmitFastElementsBoundsCheck(object, elements, intptr_index,
                              is_jsarray_condition, out_of_bounds);
  int32_t kinds[] = {// Handled by if_fast_packed.
1778
                     PACKED_SMI_ELEMENTS, PACKED_ELEMENTS,
1779
                     // Handled by if_fast_holey.
1780
                     HOLEY_SMI_ELEMENTS, HOLEY_ELEMENTS,
1781
                     // Handled by if_fast_double.
1782
                     PACKED_DOUBLE_ELEMENTS,
1783
                     // Handled by if_fast_holey_double.
1784
                     HOLEY_DOUBLE_ELEMENTS};
1785 1786 1787 1788
  Label* labels[] = {// FAST_{SMI,}_ELEMENTS
                     &if_fast_packed, &if_fast_packed,
                     // FAST_HOLEY_{SMI,}_ELEMENTS
                     &if_fast_holey, &if_fast_holey,
1789
                     // PACKED_DOUBLE_ELEMENTS
1790
                     &if_fast_double,
1791
                     // HOLEY_DOUBLE_ELEMENTS
1792
                     &if_fast_holey_double};
1793 1794
  Switch(elements_kind, unimplemented_elements_kind, kinds, labels,
         arraysize(kinds));
1795

1796
  BIND(&if_fast_packed);
1797 1798
  {
    Comment("fast packed elements");
1799 1800
    exit_point->Return(
        UnsafeLoadFixedArrayElement(CAST(elements), intptr_index));
1801 1802
  }

1803
  BIND(&if_fast_holey);
1804 1805
  {
    Comment("fast holey elements");
1806
    Node* element = UnsafeLoadFixedArrayElement(CAST(elements), intptr_index);
1807
    GotoIf(WordEqual(element, TheHoleConstant()), if_hole);
1808
    exit_point->Return(element);
1809 1810
  }

1811
  BIND(&if_fast_double);
1812 1813
  {
    Comment("packed double elements");
1814 1815
    var_double_value->Bind(LoadFixedDoubleArrayElement(elements, intptr_index,
                                                       MachineType::Float64()));
1816 1817 1818
    Goto(rebox_double);
  }

1819
  BIND(&if_fast_holey_double);
1820 1821 1822 1823 1824 1825 1826 1827 1828
  {
    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);
  }

1829
  BIND(&if_nonfast);
1830 1831
  {
    STATIC_ASSERT(LAST_ELEMENTS_KIND == LAST_FIXED_TYPED_ARRAY_ELEMENTS_KIND);
1832
    GotoIf(Int32GreaterThanOrEqual(
1833
               elements_kind,
1834
               Int32Constant(FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND)),
1835
           &if_typed_array);
1836
    GotoIf(Word32Equal(elements_kind, Int32Constant(DICTIONARY_ELEMENTS)),
1837 1838 1839 1840
           &if_dictionary);
    Goto(unimplemented_elements_kind);
  }

1841
  BIND(&if_dictionary);
1842 1843 1844
  {
    Comment("dictionary elements");
    GotoIf(IntPtrLessThan(intptr_index, IntPtrConstant(0)), out_of_bounds);
1845

1846
    TNode<Object> value = BasicLoadNumberDictionaryElement(
1847 1848
        CAST(elements), intptr_index, miss, if_hole);
    exit_point->Return(value);
1849 1850
  }

1851
  BIND(&if_typed_array);
1852 1853
  {
    Comment("typed elements");
1854
    // Check if buffer has been detached.
1855
    Node* buffer = LoadObjectField(object, JSArrayBufferView::kBufferOffset);
1856
    GotoIf(IsDetachedBuffer(buffer), miss);
1857 1858

    // Bounds check.
1859
    Node* length = SmiUntag(LoadJSTypedArrayLength(CAST(object)));
1860
    GotoIfNot(UintPtrLessThan(intptr_index, length), out_of_bounds);
1861

1862
    Node* backing_store = LoadFixedTypedArrayBackingStore(CAST(elements));
1863 1864 1865

    Label uint8_elements(this), int8_elements(this), uint16_elements(this),
        int16_elements(this), uint32_elements(this), int32_elements(this),
1866 1867
        float32_elements(this), float64_elements(this), bigint64_elements(this),
        biguint64_elements(this);
1868
    Label* elements_kind_labels[] = {
1869 1870 1871 1872
        &uint8_elements,    &uint8_elements,    &int8_elements,
        &uint16_elements,   &int16_elements,    &uint32_elements,
        &int32_elements,    &float32_elements,  &float64_elements,
        &bigint64_elements, &biguint64_elements};
1873
    int32_t elements_kinds[] = {
1874 1875 1876 1877
        UINT8_ELEMENTS,    UINT8_CLAMPED_ELEMENTS, INT8_ELEMENTS,
        UINT16_ELEMENTS,   INT16_ELEMENTS,         UINT32_ELEMENTS,
        INT32_ELEMENTS,    FLOAT32_ELEMENTS,       FLOAT64_ELEMENTS,
        BIGINT64_ELEMENTS, BIGUINT64_ELEMENTS};
1878 1879 1880 1881 1882
    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));
1883 1884
    Switch(elements_kind, miss, elements_kinds, elements_kind_labels,
           kTypedElementsKindCount);
1885
    BIND(&uint8_elements);
1886 1887
    {
      Comment("UINT8_ELEMENTS");  // Handles UINT8_CLAMPED_ELEMENTS too.
1888
      Node* element = Load(MachineType::Uint8(), backing_store, intptr_index);
1889
      exit_point->Return(SmiFromInt32(element));
1890
    }
1891
    BIND(&int8_elements);
1892 1893
    {
      Comment("INT8_ELEMENTS");
1894
      Node* element = Load(MachineType::Int8(), backing_store, intptr_index);
1895
      exit_point->Return(SmiFromInt32(element));
1896
    }
1897
    BIND(&uint16_elements);
1898 1899 1900
    {
      Comment("UINT16_ELEMENTS");
      Node* index = WordShl(intptr_index, IntPtrConstant(1));
1901
      Node* element = Load(MachineType::Uint16(), backing_store, index);
1902
      exit_point->Return(SmiFromInt32(element));
1903
    }
1904
    BIND(&int16_elements);
1905 1906 1907
    {
      Comment("INT16_ELEMENTS");
      Node* index = WordShl(intptr_index, IntPtrConstant(1));
1908
      Node* element = Load(MachineType::Int16(), backing_store, index);
1909
      exit_point->Return(SmiFromInt32(element));
1910
    }
1911
    BIND(&uint32_elements);
1912 1913 1914 1915
    {
      Comment("UINT32_ELEMENTS");
      Node* index = WordShl(intptr_index, IntPtrConstant(2));
      Node* element = Load(MachineType::Uint32(), backing_store, index);
1916
      exit_point->Return(ChangeUint32ToTagged(element));
1917
    }
1918
    BIND(&int32_elements);
1919 1920 1921 1922
    {
      Comment("INT32_ELEMENTS");
      Node* index = WordShl(intptr_index, IntPtrConstant(2));
      Node* element = Load(MachineType::Int32(), backing_store, index);
1923
      exit_point->Return(ChangeInt32ToTagged(element));
1924
    }
1925
    BIND(&float32_elements);
1926 1927 1928 1929 1930 1931 1932
    {
      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);
    }
1933
    BIND(&float64_elements);
1934 1935 1936 1937 1938 1939 1940
    {
      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);
    }
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
    BIND(&bigint64_elements);
    {
      Comment("BIGINT64_ELEMENTS");
      exit_point->Return(LoadFixedTypedArrayElementAsTagged(
          backing_store, intptr_index, BIGINT64_ELEMENTS, INTPTR_PARAMETERS));
    }
    BIND(&biguint64_elements);
    {
      Comment("BIGUINT64_ELEMENTS");
      exit_point->Return(LoadFixedTypedArrayElementAsTagged(
          backing_store, intptr_index, BIGUINT64_ELEMENTS, INTPTR_PARAMETERS));
    }
1953 1954 1955
  }
}

1956 1957
void AccessorAssembler::NameDictionaryNegativeLookup(Node* object,
                                                     SloppyTNode<Name> name,
1958
                                                     Label* miss) {
1959
  CSA_ASSERT(this, IsDictionaryMap(LoadMap(object)));
1960
  TNode<NameDictionary> properties = CAST(LoadSlowProperties(object));
1961
  // Ensure the property does not exist in a dictionary-mode object.
1962
  TVARIABLE(IntPtrT, var_name_index);
1963 1964 1965
  Label done(this);
  NameDictionaryLookup<NameDictionary>(properties, name, miss, &var_name_index,
                                       &done);
1966
  BIND(&done);
1967 1968
}

1969 1970 1971 1972 1973 1974 1975
void AccessorAssembler::InvalidateValidityCellIfPrototype(Node* map,
                                                          Node* bitfield2) {
  Label is_prototype(this), cont(this);
  if (bitfield2 == nullptr) {
    bitfield2 = LoadMapBitField2(map);
  }

1976
  Branch(IsSetWord32(bitfield2, Map::IsPrototypeMapBit::kMask), &is_prototype,
1977 1978 1979
         &cont);

  BIND(&is_prototype);
1980 1981 1982 1983 1984 1985 1986
  {
    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(
1987
        ExternalReference::invalidate_prototype_chains_function());
1988 1989 1990 1991
    CallCFunction1(MachineType::AnyTagged(), MachineType::AnyTagged(), function,
                   map);
    Goto(&cont);
  }
1992 1993 1994
  BIND(&cont);
}

1995
void AccessorAssembler::GenericElementLoad(Node* receiver, Node* receiver_map,
1996 1997
                                           SloppyTNode<Int32T> instance_type,
                                           Node* index, Label* slow) {
1998
  Comment("integer index");
1999 2000 2001

  ExitPoint direct_exit(this);

2002
  Label if_custom(this), if_element_hole(this), if_oob(this);
2003 2004
  // Receivers requiring non-standard element accesses (interceptors, access
  // checks, strings and string wrappers, proxies) are handled in the runtime.
2005
  GotoIf(IsCustomElementsReceiverInstanceType(instance_type), &if_custom);
2006 2007
  Node* elements = LoadElements(receiver);
  Node* elements_kind = LoadMapElementsKind(receiver_map);
2008
  Node* is_jsarray_condition = InstanceTypeEqual(instance_type, JS_ARRAY_TYPE);
2009
  VARIABLE(var_double_value, MachineRepresentation::kFloat64);
2010 2011 2012 2013 2014 2015 2016
  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,
2017 2018
                  &var_double_value, unimplemented_elements_kind, &if_oob, slow,
                  &direct_exit);
2019

2020
  BIND(&rebox_double);
2021 2022
  Return(AllocateHeapNumberWithValue(var_double_value.value()));

2023
  BIND(&if_oob);
2024 2025 2026
  {
    Comment("out of bounds");
    // Positive OOB indices are effectively the same as hole loads.
2027 2028 2029 2030 2031
    GotoIf(IntPtrGreaterThanOrEqual(index, IntPtrConstant(0)),
           &if_element_hole);
    // Negative keys can't take the fast OOB path, except for typed arrays.
    GotoIfNot(InstanceTypeEqual(instance_type, JS_TYPED_ARRAY_TYPE), slow);
    Return(UndefinedConstant());
2032 2033
  }

2034
  BIND(&if_element_hole);
2035 2036 2037 2038 2039
  {
    Comment("found the hole");
    Label return_undefined(this);
    BranchIfPrototypesHaveNoElements(receiver_map, &return_undefined, slow);

2040
    BIND(&return_undefined);
2041 2042
    Return(UndefinedConstant());
  }
2043 2044 2045 2046 2047 2048

  BIND(&if_custom);
  {
    Comment("check if string");
    GotoIfNot(IsStringInstanceType(instance_type), slow);
    Comment("load string character");
2049
    TNode<IntPtrT> length = LoadStringLengthAsWord(receiver);
2050 2051 2052 2053 2054
    GotoIfNot(UintPtrLessThan(index, length), slow);
    IncrementCounter(isolate()->counters()->ic_keyed_load_generic_smi(), 1);
    TailCallBuiltin(Builtins::kStringCharAt, NoContextConstant(), receiver,
                    index);
  }
2055 2056 2057
}

void AccessorAssembler::GenericPropertyLoad(Node* receiver, Node* receiver_map,
2058
                                            SloppyTNode<Int32T> instance_type,
2059
                                            const LoadICParameters* p,
2060 2061
                                            Label* slow,
                                            UseStubCache use_stub_cache) {
2062 2063
  ExitPoint direct_exit(this);

2064 2065
  Comment("key is unique name");
  Label if_found_on_receiver(this), if_property_dictionary(this),
2066
      lookup_prototype_chain(this), special_receiver(this);
2067 2068
  VARIABLE(var_details, MachineRepresentation::kWord32);
  VARIABLE(var_value, MachineRepresentation::kTagged);
2069 2070

  // Receivers requiring non-standard accesses (interceptors, access
2071
  // checks, strings and string wrappers) are handled in the runtime.
2072
  GotoIf(IsSpecialReceiverInstanceType(instance_type), &special_receiver);
2073

2074
  // Check if the receiver has fast or slow properties.
2075
  Node* bitfield3 = LoadMapBitField3(receiver_map);
2076 2077
  GotoIf(IsSetWord32<Map::IsDictionaryMapBit>(bitfield3),
         &if_property_dictionary);
2078 2079 2080

  // Try looking up the property on the receiver; if unsuccessful, look
  // for a handler in the stub cache.
2081
  TNode<DescriptorArray> descriptors = LoadMapDescriptors(receiver_map);
2082

2083
  Label if_descriptor_found(this), try_stub_cache(this);
2084
  TVARIABLE(IntPtrT, var_name_index);
2085 2086
  Label* notfound = use_stub_cache == kUseStubCache ? &try_stub_cache
                                                    : &lookup_prototype_chain;
2087
  DescriptorLookup(p->name, descriptors, bitfield3, &if_descriptor_found,
2088
                   &var_name_index, notfound);
2089

2090
  BIND(&if_descriptor_found);
2091
  {
2092
    LoadPropertyFromFastObject(receiver, receiver_map, descriptors,
2093 2094 2095 2096 2097
                               var_name_index.value(), &var_details,
                               &var_value);
    Goto(&if_found_on_receiver);
  }

2098
  if (use_stub_cache == kUseStubCache) {
2099 2100 2101 2102 2103 2104 2105
    Label stub_cache(this);
    BIND(&try_stub_cache);
    // When there is no feedback vector don't use stub cache.
    GotoIfNot(IsUndefined(p->vector), &stub_cache);
    // Fall back to the slow path for private symbols.
    Branch(IsPrivateSymbol(p->name), slow, &lookup_prototype_chain);

2106
    BIND(&stub_cache);
2107
    Comment("stub cache probe for fast property load");
2108
    TVARIABLE(MaybeObject, var_handler);
2109
    Label found_handler(this, &var_handler), stub_cache_miss(this);
2110
    TryProbeStubCache(isolate()->load_stub_cache(), receiver, p->name,
2111
                      &found_handler, &var_handler, &stub_cache_miss);
2112
    BIND(&found_handler);
2113
    {
2114
      HandleLoadICHandlerCase(p, CAST(var_handler.value()), &stub_cache_miss,
2115 2116
                              &direct_exit);
    }
2117

2118
    BIND(&stub_cache_miss);
2119 2120 2121 2122 2123 2124 2125 2126 2127
    {
      // 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);
    }
  }

2128
  BIND(&if_property_dictionary);
2129 2130 2131 2132 2133
  {
    Comment("dictionary property load");
    // We checked for LAST_CUSTOM_ELEMENTS_RECEIVER before, which rules out
    // seeing global objects here (which would need special handling).

2134
    TVARIABLE(IntPtrT, var_name_index);
2135
    Label dictionary_found(this, &var_name_index);
2136 2137 2138
    TNode<NameDictionary> properties = CAST(LoadSlowProperties(receiver));
    NameDictionaryLookup<NameDictionary>(properties, CAST(p->name),
                                         &dictionary_found, &var_name_index,
2139
                                         &lookup_prototype_chain);
2140
    BIND(&dictionary_found);
2141 2142 2143 2144 2145 2146 2147
    {
      LoadPropertyFromNameDictionary(properties, var_name_index.value(),
                                     &var_details, &var_value);
      Goto(&if_found_on_receiver);
    }
  }

2148
  BIND(&if_found_on_receiver);
2149 2150
  {
    Node* value = CallGetterIfAccessor(var_value.value(), var_details.value(),
2151
                                       p->context, receiver, slow);
2152 2153 2154 2155
    IncrementCounter(isolate()->counters()->ic_keyed_load_generic_symbol(), 1);
    Return(value);
  }

2156
  BIND(&lookup_prototype_chain);
2157
  {
2158 2159
    VARIABLE(var_holder_map, MachineRepresentation::kTagged);
    VARIABLE(var_holder_instance_type, MachineRepresentation::kWord32);
2160 2161 2162 2163
    Label return_undefined(this);
    Variable* merged_variables[] = {&var_holder_map, &var_holder_instance_type};
    Label loop(this, arraysize(merged_variables), merged_variables);

2164 2165
    var_holder_map.Bind(receiver_map);
    var_holder_instance_type.Bind(instance_type);
2166
    // Private symbols must not be looked up on the prototype chain.
2167
    GotoIf(IsPrivateSymbol(p->name), &return_undefined);
2168
    Goto(&loop);
2169
    BIND(&loop);
2170 2171
    {
      // Bailout if it can be an integer indexed exotic case.
2172 2173
      GotoIf(InstanceTypeEqual(var_holder_instance_type.value(),
                               JS_TYPED_ARRAY_TYPE),
2174 2175 2176 2177 2178 2179 2180 2181
             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);
2182
      TryGetOwnProperty(p->context, receiver, proto, proto_map,
2183
                        proto_instance_type, p->name, &return_value, &var_value,
2184 2185 2186 2187
                        &next_proto, &goto_slow);

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

2191
      BIND(&goto_slow);
2192 2193
      Goto(slow);

2194
      BIND(&return_value);
2195 2196 2197
      Return(var_value.value());
    }

2198
    BIND(&return_undefined);
2199 2200
    Return(UndefinedConstant());
  }
2201 2202 2203

  BIND(&special_receiver);
  {
2204
    // TODO(jkummerow): Consider supporting JSModuleNamespace.
2205
    GotoIfNot(InstanceTypeEqual(instance_type, JS_PROXY_TYPE), slow);
2206

2207 2208 2209
    // Private field/symbol lookup is not supported.
    GotoIf(IsPrivateSymbol(p->name), slow);

2210 2211 2212
    direct_exit.ReturnCallStub(
        Builtins::CallableFor(isolate(), Builtins::kProxyGetProperty),
        p->context, receiver /*holder is the same as receiver*/, p->name,
2213
        receiver, SmiConstant(OnNonExistent::kReturnUndefined));
2214
  }
2215 2216
}

2217 2218
//////////////////// Stub cache access helpers.

2219
enum AccessorAssembler::StubCacheTable : int {
2220 2221 2222 2223
  kPrimary = static_cast<int>(StubCache::kPrimary),
  kSecondary = static_cast<int>(StubCache::kSecondary)
};

2224
Node* AccessorAssembler::StubCachePrimaryOffset(Node* name, Node* map) {
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
  // 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).
2237 2238 2239 2240
  Node* map_word = BitcastTaggedToWord(map);

  Node* map32 = TruncateIntPtrToInt32(UncheckedCast<IntPtrT>(
      WordXor(map_word, WordShr(map_word, StubCache::kMapKeyShift))));
2241
  // Base the offset on a simple combination of name and map.
2242
  Node* hash = Int32Add(hash_field, map32);
2243 2244 2245 2246 2247
  uint32_t mask = (StubCache::kPrimaryTableSize - 1)
                  << StubCache::kCacheIndexShift;
  return ChangeUint32ToWord(Word32And(hash, Int32Constant(mask)));
}

2248
Node* AccessorAssembler::StubCacheSecondaryOffset(Node* name, Node* seed) {
2249 2250 2251
  // See v8::internal::StubCache::SecondaryOffset().

  // Use the seed from the primary cache in the secondary cache.
2252 2253
  Node* name32 = TruncateIntPtrToInt32(BitcastTaggedToWord(name));
  Node* hash = Int32Sub(TruncateIntPtrToInt32(seed), name32);
2254 2255 2256 2257 2258 2259
  hash = Int32Add(hash, Int32Constant(StubCache::kSecondaryMagic));
  int32_t mask = (StubCache::kSecondaryTableSize - 1)
                 << StubCache::kCacheIndexShift;
  return ChangeUint32ToWord(Word32And(hash, Int32Constant(mask)));
}

2260 2261 2262 2263
void AccessorAssembler::TryProbeStubCacheTable(
    StubCache* stub_cache, StubCacheTable table_id, Node* entry_offset,
    Node* name, Node* map, Label* if_handler,
    TVariable<MaybeObject>* var_handler, Label* if_miss) {
2264 2265 2266 2267 2268 2269 2270
  StubCache::Table table = static_cast<StubCache::Table>(table_id);
  // 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.
2271 2272
  Node* key_base = ExternalConstant(
      ExternalReference::Create(stub_cache->key_reference(table)));
2273 2274 2275 2276
  Node* entry_key = Load(MachineType::Pointer(), key_base, entry_offset);
  GotoIf(WordNotEqual(name, entry_key), if_miss);

  // Get the map entry from the cache.
2277 2278 2279
  DCHECK_EQ(kSystemPointerSize * 2,
            stub_cache->map_reference(table).address() -
                stub_cache->key_reference(table).address());
2280 2281
  Node* entry_map =
      Load(MachineType::Pointer(), key_base,
2282
           IntPtrAdd(entry_offset, IntPtrConstant(kSystemPointerSize * 2)));
2283 2284
  GotoIf(WordNotEqual(map, entry_map), if_miss);

2285 2286
  DCHECK_EQ(kSystemPointerSize, stub_cache->value_reference(table).address() -
                                    stub_cache->key_reference(table).address());
2287
  TNode<MaybeObject> handler = ReinterpretCast<MaybeObject>(
2288
      Load(MachineType::AnyTagged(), key_base,
2289
           IntPtrAdd(entry_offset, IntPtrConstant(kSystemPointerSize))));
2290 2291

  // We found the handler.
2292
  *var_handler = handler;
2293 2294 2295
  Goto(if_handler);
}

2296 2297
void AccessorAssembler::TryProbeStubCache(StubCache* stub_cache, Node* receiver,
                                          Node* name, Label* if_handler,
2298
                                          TVariable<MaybeObject>* var_handler,
2299
                                          Label* if_miss) {
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314
  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);

2315
  BIND(&try_secondary);
2316 2317 2318 2319 2320 2321 2322
  {
    // 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);
  }

2323
  BIND(&miss);
2324 2325 2326 2327 2328 2329 2330 2331
  {
    IncrementCounter(counters->megamorphic_stub_cache_misses(), 1);
    Goto(if_miss);
  }
}

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

2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
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.
2343 2344
  Label stub_call(this, Label::kDeferred), miss(this, Label::kDeferred),
      no_feedback(this, Label::kDeferred);
2345

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

  GotoIf(IsUndefined(p->vector), &no_feedback);
2350

2351 2352 2353 2354
  // Inlined fast path.
  {
    Comment("LoadIC_BytecodeHandler_fast");

2355
    TVARIABLE(MaybeObject, var_handler);
2356 2357
    Label try_polymorphic(this), if_handler(this, &var_handler);

2358
    TNode<MaybeObject> feedback =
2359 2360 2361
        TryMonomorphicCase(p->slot, p->vector, recv_map, &if_handler,
                           &var_handler, &try_polymorphic);

2362
    BIND(&if_handler);
2363
    HandleLoadICHandlerCase(p, CAST(var_handler.value()), &miss, exit_point);
2364

2365
    BIND(&try_polymorphic);
2366
    {
2367 2368
      TNode<HeapObject> strong_feedback =
          GetHeapObjectIfStrong(feedback, &miss);
2369 2370
      GotoIfNot(IsWeakFixedArrayMap(LoadMap(strong_feedback)), &stub_call);
      HandlePolymorphicCase(recv_map, CAST(strong_feedback), &if_handler,
2371
                            &var_handler, &miss);
2372 2373 2374
    }
  }

2375
  BIND(&stub_call);
2376 2377 2378 2379
  {
    Comment("LoadIC_BytecodeHandler_noninlined");

    // Call into the stub that implements the non-inlined parts of LoadIC.
2380 2381
    Callable ic =
        Builtins::CallableFor(isolate(), Builtins::kLoadIC_Noninlined);
2382 2383 2384 2385 2386
    Node* code_target = HeapConstant(ic.code());
    exit_point->ReturnCallStub(ic.descriptor(), code_target, p->context,
                               p->receiver, p->name, p->slot, p->vector);
  }

2387 2388 2389 2390 2391 2392 2393 2394 2395
  BIND(&no_feedback);
  {
    Comment("LoadIC_BytecodeHandler_nofeedback");
    // Call into the stub that implements the non-inlined parts of LoadIC.
    exit_point->ReturnCallStub(
        Builtins::CallableFor(isolate(), Builtins::kLoadIC_Uninitialized),
        p->context, p->receiver, p->name, p->slot, p->vector);
  }

2396
  BIND(&miss);
2397 2398 2399 2400 2401 2402 2403 2404
  {
    Comment("LoadIC_BytecodeHandler_miss");

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

2405
void AccessorAssembler::LoadIC(const LoadICParameters* p) {
2406 2407 2408 2409
  // Must be kept in sync with LoadIC_BytecodeHandler.

  ExitPoint direct_exit(this);

2410
  TVARIABLE(MaybeObject, var_handler);
2411 2412
  Label if_handler(this, &var_handler), non_inlined(this, Label::kDeferred),
      try_polymorphic(this), miss(this, Label::kDeferred);
2413 2414

  Node* receiver_map = LoadReceiverMap(p->receiver);
2415
  GotoIf(IsDeprecatedMap(receiver_map), &miss);
2416 2417

  // Check monomorphic case.
2418
  TNode<MaybeObject> feedback =
2419 2420
      TryMonomorphicCase(p->slot, p->vector, receiver_map, &if_handler,
                         &var_handler, &try_polymorphic);
2421
  BIND(&if_handler);
2422
  HandleLoadICHandlerCase(p, CAST(var_handler.value()), &miss, &direct_exit);
2423

2424
  BIND(&try_polymorphic);
2425
  TNode<HeapObject> strong_feedback = GetHeapObjectIfStrong(feedback, &miss);
2426 2427 2428
  {
    // Check polymorphic case.
    Comment("LoadIC_try_polymorphic");
2429 2430
    GotoIfNot(IsWeakFixedArrayMap(LoadMap(strong_feedback)), &non_inlined);
    HandlePolymorphicCase(receiver_map, CAST(strong_feedback), &if_handler,
2431
                          &var_handler, &miss);
2432 2433
  }

2434
  BIND(&non_inlined);
2435 2436 2437 2438
  {
    LoadIC_Noninlined(p, receiver_map, strong_feedback, &var_handler,
                      &if_handler, &miss, &direct_exit);
  }
2439

2440
  BIND(&miss);
2441 2442 2443 2444 2445
  direct_exit.ReturnCallRuntime(Runtime::kLoadIC_Miss, p->context, p->receiver,
                                p->name, p->slot, p->vector);
}

void AccessorAssembler::LoadIC_Noninlined(const LoadICParameters* p,
2446 2447
                                          Node* receiver_map,
                                          TNode<HeapObject> feedback,
2448
                                          TVariable<MaybeObject>* var_handler,
2449 2450 2451 2452 2453 2454 2455
                                          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)));
2456
  CSA_ASSERT(this, WordNotEqual(receiver_map, feedback));
2457
  CSA_ASSERT(this, Word32BinaryNot(IsWeakFixedArrayMap(LoadMap(feedback))));
2458 2459
  DCHECK_EQ(MachineRepresentation::kTagged, var_handler->rep());

2460 2461
  {
    // Check megamorphic case.
2462
    GotoIfNot(WordEqual(feedback, LoadRoot(RootIndex::kmegamorphic_symbol)),
2463
              &try_uninitialized);
2464 2465

    TryProbeStubCache(isolate()->load_stub_cache(), p->receiver, p->name,
2466
                      if_handler, var_handler, miss);
2467
  }
2468

2469
  BIND(&try_uninitialized);
2470 2471
  {
    // Check uninitialized case.
2472 2473
    GotoIfNot(WordEqual(feedback, LoadRoot(RootIndex::kuninitialized_symbol)),
              miss);
2474 2475 2476
    exit_point->ReturnCallStub(
        Builtins::CallableFor(isolate(), Builtins::kLoadIC_Uninitialized),
        p->context, p->receiver, p->name, p->slot, p->vector);
2477 2478 2479
  }
}

2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
// TODO(8860): This check is only required so we can make prototypes fast on
// the first load. This is not really useful when there is no feedback vector
// and may not be important when lazily allocating feedback vectors. Once lazy
// allocation of feedback vectors has landed try to eliminate this check.
void AccessorAssembler::BranchIfPrototypeShouldbeFast(Node* receiver_map,
                                                      Label* prototype_not_fast,
                                                      Label* prototype_fast) {
  VARIABLE(var_map, MachineRepresentation::kTagged);
  var_map.Bind(receiver_map);
  Label loop_body(this, &var_map);
  Goto(&loop_body);

  BIND(&loop_body);
  {
    Node* map = var_map.value();
    Node* prototype = LoadMapPrototype(map);
    GotoIf(IsNull(prototype), prototype_fast);
    TNode<PrototypeInfo> proto_info =
        LoadMapPrototypeInfo(receiver_map, prototype_not_fast);
    GotoIf(IsNull(prototype), prototype_not_fast);
    TNode<Uint32T> flags =
        LoadObjectField<Uint32T>(proto_info, PrototypeInfo::kBitFieldOffset);
    GotoIf(Word32Equal(flags, Uint32Constant(0)), prototype_not_fast);

    Node* prototype_map = LoadMap(prototype);
    var_map.Bind(prototype_map);
    Goto(&loop_body);
  }
}

2510
void AccessorAssembler::LoadIC_Uninitialized(const LoadICParameters* p) {
2511 2512 2513
  Label miss(this, Label::kDeferred),
      check_if_fast_prototype(this, Label::kDeferred),
      check_function_prototype(this);
2514 2515 2516 2517 2518
  Node* receiver = p->receiver;
  GotoIf(TaggedIsSmi(receiver), &miss);
  Node* receiver_map = LoadMap(receiver);
  Node* instance_type = LoadMapInstanceType(receiver_map);

2519
  GotoIf(IsUndefined(p->vector), &check_if_fast_prototype);
2520
  // Optimistically write the state transition to the vector.
2521
  StoreFeedbackVectorSlot(p->vector, p->slot,
2522
                          LoadRoot(RootIndex::kpremonomorphic_symbol),
2523
                          SKIP_WRITE_BARRIER, 0, SMI_PARAMETERS);
2524
  StoreWeakReferenceInFeedbackVector(p->vector, p->slot, receiver_map,
2525
                                     kTaggedSize, SMI_PARAMETERS);
2526
  Goto(&check_function_prototype);
2527

2528 2529 2530 2531 2532 2533 2534
  BIND(&check_if_fast_prototype);
  {
    BranchIfPrototypeShouldbeFast(receiver_map, &miss,
                                  &check_function_prototype);
  }

  BIND(&check_function_prototype);
2535 2536 2537
  {
    // Special case for Function.prototype load, because it's very common
    // for ICs that are only executed once (MyFunc.prototype.foo = ...).
2538 2539 2540
    Label not_function_prototype(this, Label::kDeferred);
    GotoIfNot(InstanceTypeEqual(instance_type, JS_FUNCTION_TYPE),
              &not_function_prototype);
2541
    GotoIfNot(IsPrototypeString(p->name), &not_function_prototype);
2542

2543 2544
    GotoIfPrototypeRequiresRuntimeLookup(CAST(receiver), CAST(receiver_map),
                                         &not_function_prototype);
2545 2546 2547 2548
    Return(LoadJSFunctionPrototype(receiver, &miss));
    BIND(&not_function_prototype);
  }

2549
  GenericPropertyLoad(receiver, receiver_map, instance_type, p, &miss,
2550 2551
                      kDontUseStubCache);

2552
  BIND(&miss);
2553
  {
2554 2555
    Label call_runtime(this, Label::kDeferred);
    GotoIf(IsUndefined(p->vector), &call_runtime);
2556
    // Undo the optimistic state transition.
2557
    StoreFeedbackVectorSlot(p->vector, p->slot,
2558
                            LoadRoot(RootIndex::kuninitialized_symbol),
2559
                            SKIP_WRITE_BARRIER, 0, SMI_PARAMETERS);
2560
    Goto(&call_runtime);
2561

2562
    BIND(&call_runtime);
2563 2564 2565 2566 2567
    TailCallRuntime(Runtime::kLoadIC_Miss, p->context, p->receiver, p->name,
                    p->slot, p->vector);
  }
}

2568
void AccessorAssembler::LoadGlobalIC(Node* vector, Node* slot,
2569 2570 2571 2572 2573 2574
                                     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);
2575 2576 2577
  GotoIf(IsUndefined(vector), &miss);

  LoadGlobalIC_TryPropertyCellCase(CAST(vector), slot, lazy_context, exit_point,
2578 2579 2580
                                   &try_handler, &miss, slot_mode);

  BIND(&try_handler);
2581
  LoadGlobalIC_TryHandlerCase(CAST(vector), slot, lazy_context, lazy_name,
2582 2583 2584 2585 2586 2587 2588 2589
                              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,
2590 2591
                                  ParameterToTagged(slot, slot_mode), vector,
                                  SmiConstant(typeof_mode));
2592 2593 2594
  }
}

2595
void AccessorAssembler::LoadGlobalIC_TryPropertyCellCase(
2596 2597 2598
    TNode<FeedbackVector> vector, Node* slot,
    const LazyNode<Context>& lazy_context, ExitPoint* exit_point,
    Label* try_handler, Label* miss, ParameterMode slot_mode) {
2599 2600
  Comment("LoadGlobalIC_TryPropertyCellCase");

2601
  Label if_lexical_var(this), if_property_cell(this);
2602
  TNode<MaybeObject> maybe_weak_ref =
2603
      LoadFeedbackVectorSlot(vector, slot, 0, slot_mode);
2604
  Branch(TaggedIsSmi(maybe_weak_ref), &if_lexical_var, &if_property_cell);
2605

2606 2607
  BIND(&if_property_cell);
  {
2608
    // Load value or try handler case if the weak reference is cleared.
2609
    CSA_ASSERT(this, IsWeakOrCleared(maybe_weak_ref));
2610
    TNode<PropertyCell> property_cell =
2611
        CAST(GetHeapObjectAssumeWeak(maybe_weak_ref, try_handler));
2612 2613
    TNode<Object> value =
        LoadObjectField(property_cell, PropertyCell::kValueOffset);
2614 2615 2616 2617 2618 2619 2620
    GotoIf(WordEqual(value, TheHoleConstant()), miss);
    exit_point->Return(value);
  }

  BIND(&if_lexical_var);
  {
    Comment("Load lexical variable");
2621
    TNode<IntPtrT> lexical_handler = SmiUntag(CAST(maybe_weak_ref));
2622
    TNode<IntPtrT> context_index =
2623
        Signed(DecodeWord<FeedbackNexus::ContextIndexBits>(lexical_handler));
2624
    TNode<IntPtrT> slot_index =
2625
        Signed(DecodeWord<FeedbackNexus::SlotIndexBits>(lexical_handler));
2626
    TNode<Context> context = lazy_context();
2627 2628 2629 2630
    TNode<Context> script_context = LoadScriptContext(context, context_index);
    TNode<Object> result = LoadContextElement(script_context, slot_index);
    exit_point->Return(result);
  }
2631
}
2632

2633 2634 2635 2636 2637
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) {
2638
  Comment("LoadGlobalIC_TryHandlerCase");
2639

2640
  Label call_handler(this), non_smi(this);
2641

2642
  TNode<MaybeObject> feedback_element =
2643
      LoadFeedbackVectorSlot(vector, slot, kTaggedSize, slot_mode);
2644
  TNode<Object> handler = CAST(feedback_element);
2645
  GotoIf(WordEqual(handler, LoadRoot(RootIndex::kuninitialized_symbol)), miss);
2646

2647 2648 2649
  OnNonExistent on_nonexistent = typeof_mode == NOT_INSIDE_TYPEOF
                                     ? OnNonExistent::kThrowReferenceError
                                     : OnNonExistent::kReturnUndefined;
2650

2651 2652 2653 2654 2655
  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);
2656

2657 2658
  LoadICParameters p(context, receiver, lazy_name(),
                     ParameterToTagged(slot, slot_mode), vector, holder);
2659

2660 2661
  HandleLoadICHandlerCase(&p, handler, miss, exit_point, ICMode::kGlobalIC,
                          on_nonexistent);
2662 2663
}

2664
void AccessorAssembler::KeyedLoadIC(const LoadICParameters* p) {
2665 2666
  ExitPoint direct_exit(this);

2667
  TVARIABLE(MaybeObject, var_handler);
jgruber's avatar
jgruber committed
2668 2669 2670
  Label if_handler(this, &var_handler), try_polymorphic(this, Label::kDeferred),
      try_megamorphic(this, Label::kDeferred),
      try_polymorphic_name(this, Label::kDeferred),
2671
      miss(this, Label::kDeferred), generic(this, Label::kDeferred);
2672 2673

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

2676 2677
  GotoIf(IsUndefined(p->vector), &generic);

2678
  // Check monomorphic case.
2679
  TNode<MaybeObject> feedback =
2680 2681
      TryMonomorphicCase(p->slot, p->vector, receiver_map, &if_handler,
                         &var_handler, &try_polymorphic);
2682
  BIND(&if_handler);
2683
  {
2684 2685
    HandleLoadICHandlerCase(p, CAST(var_handler.value()), &miss, &direct_exit,
                            ICMode::kNonGlobalIC,
2686
                            OnNonExistent::kReturnUndefined, kSupportElements);
2687
  }
2688

2689
  BIND(&try_polymorphic);
2690
  TNode<HeapObject> strong_feedback = GetHeapObjectIfStrong(feedback, &miss);
2691 2692 2693
  {
    // Check polymorphic case.
    Comment("KeyedLoadIC_try_polymorphic");
2694 2695
    GotoIfNot(IsWeakFixedArrayMap(LoadMap(strong_feedback)), &try_megamorphic);
    HandlePolymorphicCase(receiver_map, CAST(strong_feedback), &if_handler,
2696
                          &var_handler, &miss);
2697 2698
  }

2699
  BIND(&try_megamorphic);
2700 2701 2702
  {
    // Check megamorphic case.
    Comment("KeyedLoadIC_try_megamorphic");
2703 2704 2705 2706 2707 2708
    Branch(WordEqual(strong_feedback, LoadRoot(RootIndex::kmegamorphic_symbol)),
           &generic, &try_polymorphic_name);
  }

  BIND(&generic);
  {
2709
    // TODO(jkummerow): Inline this? Or some of it?
2710 2711
    TailCallBuiltin(Builtins::kKeyedLoadIC_Megamorphic, p->context, p->receiver,
                    p->name, p->slot, p->vector);
2712
  }
2713

2714
  BIND(&try_polymorphic_name);
2715
  {
2716 2717
    // We might have a name in feedback, and a weak fixed array in the next
    // slot.
2718
    Node* name = p->name;
2719
    Comment("KeyedLoadIC_try_polymorphic_name");
2720 2721 2722 2723 2724 2725
    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}.
2726
    GotoIf(WordEqual(strong_feedback, name), &if_polymorphic_name);
2727 2728 2729 2730 2731 2732 2733 2734

    // 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.
2735
      Branch(WordEqual(strong_feedback, var_name.value()), &if_polymorphic_name,
2736 2737 2738 2739 2740 2741 2742
             &miss);
    }

    BIND(&if_notinternalized);
    {
      // Try to internalize the {name}.
      Node* function = ExternalConstant(
2743
          ExternalReference::try_internalize_string_function());
2744 2745 2746 2747 2748
      Node* const isolate_ptr =
          ExternalConstant(ExternalReference::isolate_address(isolate()));
      var_name.Bind(CallCFunction2(
          MachineType::AnyTagged(), MachineType::Pointer(),
          MachineType::AnyTagged(), function, isolate_ptr, name));
2749 2750 2751 2752 2753
      Goto(&if_internalized);
    }

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

2762
  BIND(&miss);
2763 2764 2765 2766 2767 2768 2769
  {
    Comment("KeyedLoadIC_miss");
    TailCallRuntime(Runtime::kKeyedLoadIC_Miss, p->context, p->receiver,
                    p->name, p->slot, p->vector);
  }
}

2770
void AccessorAssembler::KeyedLoadICGeneric(const LoadICParameters* p) {
2771
  VARIABLE(var_index, MachineType::PointerRepresentation());
2772 2773 2774
  VARIABLE(var_unique, MachineRepresentation::kTagged, p->name);
  Label if_index(this), if_unique_name(this), if_notunique(this),
      if_other(this, Label::kDeferred), if_runtime(this, Label::kDeferred);
2775 2776

  Node* receiver = p->receiver;
2777
  GotoIf(TaggedIsSmi(receiver), &if_runtime);
2778

2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
  TryToName(p->name, &if_index, &var_index, &if_unique_name, &var_unique,
            &if_other, &if_notunique);

  BIND(&if_other);
  {
    Node* name = CallBuiltin(Builtins::kToName, p->context, p->name);
    var_unique.Bind(name);
    TryToName(name, &if_index, &var_index, &if_unique_name, &var_unique,
              &if_runtime, &if_notunique);
  }
2789

2790
  BIND(&if_index);
2791
  {
2792 2793
    Node* receiver_map = LoadMap(receiver);
    Node* instance_type = LoadMapInstanceType(receiver_map);
2794
    GenericElementLoad(receiver, receiver_map, instance_type, var_index.value(),
2795
                       &if_runtime);
2796 2797
  }

2798
  BIND(&if_unique_name);
2799
  {
2800 2801
    LoadICParameters pp = *p;
    pp.name = var_unique.value();
2802 2803 2804 2805
    Node* receiver_map = LoadMap(receiver);
    Node* instance_type = LoadMapInstanceType(receiver_map);
    GenericPropertyLoad(receiver, receiver_map, instance_type, &pp,
                        &if_runtime);
2806 2807
  }

2808 2809 2810
  BIND(&if_notunique);
  {
    if (FLAG_internalize_on_the_fly) {
2811 2812 2813
      // 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
2814
      // point), so we take the {if_runtime} path instead.
2815
      Label if_in_string_table(this);
2816 2817 2818
      TryInternalizeString(var_unique.value(), &if_index, &var_index,
                           &if_in_string_table, &var_unique, &if_runtime,
                           &if_runtime);
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828

      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();
2829 2830 2831 2832
        Node* receiver_map = LoadMap(receiver);
        Node* instance_type = LoadMapInstanceType(receiver_map);
        GenericPropertyLoad(receiver, receiver_map, instance_type, &pp,
                            &if_runtime, kDontUseStubCache);
2833
      }
2834
    } else {
2835
      Goto(&if_runtime);
2836 2837 2838
    }
  }

2839
  BIND(&if_runtime);
2840 2841 2842 2843
  {
    Comment("KeyedLoadGeneric_slow");
    IncrementCounter(isolate()->counters()->ic_keyed_load_generic_slow(), 1);
    // TODO(jkummerow): Should we use the GetProperty TF stub instead?
2844
    TailCallRuntime(Runtime::kGetProperty, p->context, p->receiver,
2845
                    var_unique.value());
2846 2847 2848
  }
}

2849
void AccessorAssembler::KeyedLoadICPolymorphicName(const LoadICParameters* p) {
2850
  TVARIABLE(MaybeObject, var_handler);
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
  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)));
2865
  CSA_ASSERT(this, WordEqual(name, CAST(LoadFeedbackVectorSlot(
2866
                                       vector, slot, 0, SMI_PARAMETERS))));
2867 2868

  // Check if we have a matching handler for the {receiver_map}.
2869
  TNode<MaybeObject> feedback_element =
2870
      LoadFeedbackVectorSlot(vector, slot, kTaggedSize, SMI_PARAMETERS);
2871
  TNode<WeakFixedArray> array = CAST(feedback_element);
2872
  HandlePolymorphicCase(receiver_map, array, &if_handler, &var_handler, &miss);
2873 2874 2875 2876

  BIND(&if_handler);
  {
    ExitPoint direct_exit(this);
2877 2878
    HandleLoadICHandlerCase(p, CAST(var_handler.value()), &miss, &direct_exit,
                            ICMode::kNonGlobalIC,
2879
                            OnNonExistent::kReturnUndefined, kOnlyProperties);
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
  }

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

2890
void AccessorAssembler::StoreIC(const StoreICParameters* p) {
2891 2892 2893 2894 2895 2896
  TVARIABLE(MaybeObject, var_handler,
            ReinterpretCast<MaybeObject>(SmiConstant(0)));

  Label if_handler(this, &var_handler),
      if_handler_from_stub_cache(this, &var_handler, Label::kDeferred),
      try_polymorphic(this, Label::kDeferred),
2897
      try_megamorphic(this, Label::kDeferred),
2898 2899
      try_uninitialized(this, Label::kDeferred), miss(this, Label::kDeferred),
      no_feedback(this, Label::kDeferred);
2900 2901

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

2904 2905
  GotoIf(IsUndefined(p->vector), &no_feedback);

2906
  // Check monomorphic case.
2907
  TNode<MaybeObject> feedback =
2908 2909
      TryMonomorphicCase(p->slot, p->vector, receiver_map, &if_handler,
                         &var_handler, &try_polymorphic);
2910
  BIND(&if_handler);
2911 2912
  {
    Comment("StoreIC_if_handler");
2913 2914
    HandleStoreICHandlerCase(p, var_handler.value(), &miss,
                             ICMode::kNonGlobalIC);
2915 2916
  }

2917
  BIND(&try_polymorphic);
2918
  TNode<HeapObject> strong_feedback = GetHeapObjectIfStrong(feedback, &miss);
2919 2920 2921
  {
    // Check polymorphic case.
    Comment("StoreIC_try_polymorphic");
2922 2923
    GotoIfNot(IsWeakFixedArrayMap(LoadMap(strong_feedback)), &try_megamorphic);
    HandlePolymorphicCase(receiver_map, CAST(strong_feedback), &if_handler,
2924
                          &var_handler, &miss);
2925 2926
  }

2927
  BIND(&try_megamorphic);
2928 2929
  {
    // Check megamorphic case.
2930 2931 2932
    GotoIfNot(
        WordEqual(strong_feedback, LoadRoot(RootIndex::kmegamorphic_symbol)),
        &try_uninitialized);
2933 2934

    TryProbeStubCache(isolate()->store_stub_cache(), p->receiver, p->name,
2935
                      &if_handler, &var_handler, &miss);
2936
  }
2937
  BIND(&try_uninitialized);
2938 2939
  {
    // Check uninitialized case.
2940
    Branch(
2941
        WordEqual(strong_feedback, LoadRoot(RootIndex::kuninitialized_symbol)),
2942 2943 2944 2945 2946
        &no_feedback, &miss);
  }

  BIND(&no_feedback);
  {
2947 2948
    TailCallBuiltin(Builtins::kStoreIC_Uninitialized, p->context, p->receiver,
                    p->name, p->value, p->slot, p->vector);
2949
  }
2950

2951
  BIND(&miss);
2952 2953 2954 2955 2956 2957
  {
    TailCallRuntime(Runtime::kStoreIC_Miss, p->context, p->value, p->slot,
                    p->vector, p->receiver, p->name);
  }
}

2958
void AccessorAssembler::StoreGlobalIC(const StoreICParameters* pp) {
2959
  Label if_lexical_var(this), if_heapobject(this);
2960
  TNode<MaybeObject> maybe_weak_ref =
2961
      LoadFeedbackVectorSlot(pp->vector, pp->slot, 0, SMI_PARAMETERS);
2962
  Branch(TaggedIsSmi(maybe_weak_ref), &if_lexical_var, &if_heapobject);
2963

2964
  BIND(&if_heapobject);
2965
  {
2966
    Label try_handler(this), miss(this, Label::kDeferred);
2967 2968 2969 2970
    GotoIf(
        WordEqual(maybe_weak_ref, LoadRoot(RootIndex::kpremonomorphic_symbol)),
        &miss);

2971
    CSA_ASSERT(this, IsWeakOrCleared(maybe_weak_ref));
2972
    TNode<PropertyCell> property_cell =
2973
        CAST(GetHeapObjectAssumeWeak(maybe_weak_ref, &try_handler));
2974 2975 2976 2977

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

2979 2980 2981
    BIND(&try_handler);
    {
      Comment("StoreGlobalIC_try_handler");
2982
      TNode<MaybeObject> handler = LoadFeedbackVectorSlot(
2983
          pp->vector, pp->slot, kTaggedSize, SMI_PARAMETERS);
2984

2985
      GotoIf(WordEqual(handler, LoadRoot(RootIndex::kuninitialized_symbol)),
2986
             &miss);
2987

2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
      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);
    }
3002 3003
  }

3004
  BIND(&if_lexical_var);
3005
  {
3006
    Comment("Store lexical variable");
3007
    TNode<IntPtrT> lexical_handler = SmiUntag(CAST(maybe_weak_ref));
3008
    TNode<IntPtrT> context_index =
3009
        Signed(DecodeWord<FeedbackNexus::ContextIndexBits>(lexical_handler));
3010
    TNode<IntPtrT> slot_index =
3011
        Signed(DecodeWord<FeedbackNexus::SlotIndexBits>(lexical_handler));
3012 3013
    TNode<Context> script_context =
        LoadScriptContext(CAST(pp->context), context_index);
3014
    StoreContextElement(script_context, slot_index, pp->value);
3015
    Return(pp->value);
3016 3017 3018
  }
}

3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032
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);
3033 3034 3035 3036 3037
  GotoIf(IsSetWord32(details, PropertyDetails::kAttributesReadOnlyMask), miss);
  CSA_ASSERT(this,
             Word32Equal(DecodeWord32<PropertyDetails::KindField>(details),
                         Int32Constant(kData)));

3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082
  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);
  }
}

3083
void AccessorAssembler::KeyedStoreIC(const StoreICParameters* p) {
jgruber's avatar
jgruber committed
3084
  Label miss(this, Label::kDeferred);
3085
  {
3086
    TVARIABLE(MaybeObject, var_handler);
3087

jgruber's avatar
jgruber committed
3088 3089 3090
    Label if_handler(this, &var_handler),
        try_polymorphic(this, Label::kDeferred),
        try_megamorphic(this, Label::kDeferred),
3091
        no_feedback(this, Label::kDeferred),
jgruber's avatar
jgruber committed
3092
        try_polymorphic_name(this, Label::kDeferred);
3093

3094
    Node* receiver_map = LoadReceiverMap(p->receiver);
3095
    GotoIf(IsDeprecatedMap(receiver_map), &miss);
3096

3097 3098
    GotoIf(IsUndefined(p->vector), &no_feedback);

3099
    // Check monomorphic case.
3100
    TNode<MaybeObject> feedback =
3101 3102
        TryMonomorphicCase(p->slot, p->vector, receiver_map, &if_handler,
                           &var_handler, &try_polymorphic);
3103
    BIND(&if_handler);
3104 3105
    {
      Comment("KeyedStoreIC_if_handler");
3106 3107
      HandleStoreICHandlerCase(p, var_handler.value(), &miss,
                               ICMode::kNonGlobalIC, kSupportElements);
3108
    }
3109

3110
    BIND(&try_polymorphic);
3111
    TNode<HeapObject> strong_feedback = GetHeapObjectIfStrong(feedback, &miss);
3112 3113 3114
    {
      // CheckPolymorphic case.
      Comment("KeyedStoreIC_try_polymorphic");
3115 3116 3117
      GotoIfNot(IsWeakFixedArrayMap(LoadMap(strong_feedback)),
                &try_megamorphic);
      HandlePolymorphicCase(receiver_map, CAST(strong_feedback), &if_handler,
3118
                            &var_handler, &miss);
3119
    }
3120

3121
    BIND(&try_megamorphic);
3122 3123 3124
    {
      // Check megamorphic case.
      Comment("KeyedStoreIC_try_megamorphic");
3125
      Branch(
3126
          WordEqual(strong_feedback, LoadRoot(RootIndex::kmegamorphic_symbol)),
3127 3128 3129 3130 3131
          &no_feedback, &try_polymorphic_name);
    }

    BIND(&no_feedback);
    {
3132
      TailCallBuiltin(Builtins::kKeyedStoreIC_Megamorphic, p->context,
3133
                      p->receiver, p->name, p->value, p->slot);
3134
    }
3135

3136
    BIND(&try_polymorphic_name);
3137 3138 3139
    {
      // We might have a name in feedback, and a fixed array in the next slot.
      Comment("KeyedStoreIC_try_polymorphic_name");
3140
      GotoIfNot(WordEqual(strong_feedback, p->name), &miss);
3141 3142
      // If the name comparison succeeded, we know we have a feedback vector
      // with at least one map/handler pair.
3143
      TNode<MaybeObject> feedback_element = LoadFeedbackVectorSlot(
3144
          p->vector, p->slot, kTaggedSize, SMI_PARAMETERS);
3145
      TNode<WeakFixedArray> array = CAST(feedback_element);
3146
      HandlePolymorphicCase(receiver_map, array, &if_handler, &var_handler,
3147
                            &miss);
3148
    }
3149
  }
3150
  BIND(&miss);
3151 3152 3153 3154 3155 3156 3157
  {
    Comment("KeyedStoreIC_miss");
    TailCallRuntime(Runtime::kKeyedStoreIC_Miss, p->context, p->value, p->slot,
                    p->vector, p->receiver, p->name);
  }
}

3158 3159 3160
void AccessorAssembler::StoreInArrayLiteralIC(const StoreICParameters* p) {
  Label miss(this, Label::kDeferred);
  {
3161
    TVARIABLE(MaybeObject, var_handler);
3162 3163 3164 3165 3166 3167 3168

    Label if_handler(this, &var_handler),
        try_polymorphic(this, Label::kDeferred),
        try_megamorphic(this, Label::kDeferred);

    Node* array_map = LoadReceiverMap(p->receiver);
    GotoIf(IsDeprecatedMap(array_map), &miss);
3169 3170 3171

    GotoIf(IsUndefined(p->vector), &miss);

3172
    TNode<MaybeObject> feedback =
3173 3174 3175 3176 3177 3178 3179 3180
        TryMonomorphicCase(p->slot, p->vector, array_map, &if_handler,
                           &var_handler, &try_polymorphic);

    BIND(&if_handler);
    {
      Comment("StoreInArrayLiteralIC_if_handler");
      // This is a stripped-down version of HandleStoreICHandlerCase.

3181
      TNode<HeapObject> handler = CAST(var_handler.value());
3182 3183
      Label if_transitioning_element_store(this);
      GotoIfNot(IsCode(handler), &if_transitioning_element_store);
3184 3185
      TailCallStub(StoreWithVectorDescriptor{}, CAST(handler), CAST(p->context),
                   p->receiver, p->name, p->value, p->slot, p->vector);
3186 3187 3188

      BIND(&if_transitioning_element_store);
      {
3189 3190 3191
        TNode<MaybeObject> maybe_transition_map =
            LoadHandlerDataField(CAST(handler), 1);
        TNode<Map> transition_map =
3192
            CAST(GetHeapObjectAssumeWeak(maybe_transition_map, &miss));
3193 3194 3195
        GotoIf(IsDeprecatedMap(transition_map), &miss);
        Node* code = LoadObjectField(handler, StoreHandler::kSmiHandlerOffset);
        CSA_ASSERT(this, IsCode(code));
3196 3197
        TailCallStub(StoreTransitionDescriptor{}, code, p->context, p->receiver,
                     p->name, transition_map, p->value, p->slot, p->vector);
3198 3199 3200 3201
      }
    }

    BIND(&try_polymorphic);
3202
    TNode<HeapObject> strong_feedback = GetHeapObjectIfStrong(feedback, &miss);
3203 3204
    {
      Comment("StoreInArrayLiteralIC_try_polymorphic");
3205 3206 3207
      GotoIfNot(IsWeakFixedArrayMap(LoadMap(strong_feedback)),
                &try_megamorphic);
      HandlePolymorphicCase(array_map, CAST(strong_feedback), &if_handler,
3208
                            &var_handler, &miss);
3209 3210 3211 3212 3213
    }

    BIND(&try_megamorphic);
    {
      Comment("StoreInArrayLiteralIC_try_megamorphic");
3214 3215 3216 3217 3218 3219 3220 3221
      CSA_ASSERT(this,
                 Word32Or(WordEqual(strong_feedback,
                                    LoadRoot(RootIndex::kuninitialized_symbol)),
                          WordEqual(strong_feedback,
                                    LoadRoot(RootIndex::kmegamorphic_symbol))));
      GotoIfNot(
          WordEqual(strong_feedback, LoadRoot(RootIndex::kmegamorphic_symbol)),
          &miss);
3222 3223 3224 3225 3226 3227 3228 3229
      TailCallRuntime(Runtime::kStoreInArrayLiteralIC_Slow, p->context,
                      p->value, p->receiver, p->name);
    }
  }

  BIND(&miss);
  {
    Comment("StoreInArrayLiteralIC_miss");
3230 3231
    TailCallRuntime(Runtime::kStoreInArrayLiteralIC_Miss, p->context, p->value,
                    p->slot, p->vector, p->receiver, p->name);
3232 3233 3234
  }
}

3235 3236
//////////////////// Public methods.

3237
void AccessorAssembler::GenerateLoadIC() {
3238
  typedef LoadWithVectorDescriptor Descriptor;
3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249

  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);
}

3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274
void AccessorAssembler::GenerateLoadIC_Megamorphic() {
  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);
  TVARIABLE(MaybeObject, var_handler);
  Label if_handler(this, &var_handler), miss(this, Label::kDeferred);

  TryProbeStubCache(isolate()->load_stub_cache(), receiver, name, &if_handler,
                    &var_handler, &miss);

  BIND(&if_handler);
  LoadICParameters p(context, receiver, name, slot, vector);
  HandleLoadICHandlerCase(&p, CAST(var_handler.value()), &miss, &direct_exit);

  BIND(&miss);
  direct_exit.ReturnCallRuntime(Runtime::kLoadIC_Miss, context, receiver, name,
                                slot, vector);
}

3275 3276 3277 3278 3279 3280 3281 3282 3283 3284
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);
3285
  TVARIABLE(MaybeObject, var_handler);
3286 3287 3288
  Label if_handler(this, &var_handler), miss(this, Label::kDeferred);

  Node* receiver_map = LoadReceiverMap(receiver);
3289 3290
  TNode<MaybeObject> feedback_element =
      LoadFeedbackVectorSlot(vector, slot, 0, SMI_PARAMETERS);
3291
  TNode<HeapObject> feedback = CAST(feedback_element);
3292 3293 3294 3295 3296

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

3297
  BIND(&if_handler);
3298
  HandleLoadICHandlerCase(&p, CAST(var_handler.value()), &miss, &direct_exit);
3299

3300
  BIND(&miss);
3301 3302 3303 3304
  direct_exit.ReturnCallRuntime(Runtime::kLoadIC_Miss, context, receiver, name,
                                slot, vector);
}

3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
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);
}

3318
void AccessorAssembler::GenerateLoadICTrampoline() {
3319
  typedef LoadDescriptor Descriptor;
3320 3321 3322 3323 3324

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

3327
  TailCallBuiltin(Builtins::kLoadIC, context, receiver, name, slot, vector);
3328 3329
}

3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342
void AccessorAssembler::GenerateLoadICTrampoline_Megamorphic() {
  typedef LoadDescriptor Descriptor;

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

  TailCallBuiltin(Builtins::kLoadIC_Megamorphic, context, receiver, name, slot,
                  vector);
}

3343
void AccessorAssembler::GenerateLoadGlobalIC(TypeofMode typeof_mode) {
3344
  typedef LoadGlobalWithVectorDescriptor Descriptor;
3345

3346
  Node* name = Parameter(Descriptor::kName);
3347 3348 3349 3350
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);

3351
  ExitPoint direct_exit(this);
3352 3353 3354 3355 3356 3357
  LoadGlobalIC(
      vector, slot,
      // lazy_context
      [=] { return CAST(context); },
      // lazy_name
      [=] { return CAST(name); }, typeof_mode, &direct_exit);
3358 3359
}

3360
void AccessorAssembler::GenerateLoadGlobalICTrampoline(TypeofMode typeof_mode) {
3361
  typedef LoadGlobalDescriptor Descriptor;
3362

3363
  Node* name = Parameter(Descriptor::kName);
3364 3365
  Node* slot = Parameter(Descriptor::kSlot);
  Node* context = Parameter(Descriptor::kContext);
3366
  Node* vector = LoadFeedbackVectorForStub();
3367

3368 3369 3370
  Callable callable =
      CodeFactory::LoadGlobalICInOptimizedCode(isolate(), typeof_mode);
  TailCallStub(callable, context, name, slot, vector);
3371 3372
}

3373
void AccessorAssembler::GenerateKeyedLoadIC() {
3374
  typedef LoadWithVectorDescriptor Descriptor;
3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385

  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);
}

3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398
void AccessorAssembler::GenerateKeyedLoadIC_Megamorphic() {
  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);
}

3399
void AccessorAssembler::GenerateKeyedLoadICTrampoline() {
3400
  typedef LoadDescriptor Descriptor;
3401 3402 3403 3404 3405

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

3408 3409
  TailCallBuiltin(Builtins::kKeyedLoadIC, context, receiver, name, slot,
                  vector);
3410 3411
}

3412 3413
void AccessorAssembler::GenerateKeyedLoadICTrampoline_Megamorphic() {
  typedef LoadDescriptor Descriptor;
3414 3415 3416 3417 3418

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

3421 3422
  TailCallBuiltin(Builtins::kKeyedLoadIC_Megamorphic, context, receiver, name,
                  slot, vector);
3423 3424
}

3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437
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);
}

3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459
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();

3460
  TailCallBuiltin(Builtins::kStoreGlobalIC, context, name, value, slot, vector);
3461 3462
}

3463
void AccessorAssembler::GenerateStoreIC() {
3464
  typedef StoreWithVectorDescriptor Descriptor;
3465 3466 3467 3468 3469 3470 3471 3472 3473

  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);
3474
  StoreIC(&p);
3475 3476
}

3477
void AccessorAssembler::GenerateStoreICTrampoline() {
3478
  typedef StoreDescriptor Descriptor;
3479 3480 3481 3482 3483 3484

  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);
3485
  Node* vector = LoadFeedbackVectorForStub();
3486

3487 3488
  TailCallBuiltin(Builtins::kStoreIC, context, receiver, name, value, slot,
                  vector);
3489 3490
}

3491
void AccessorAssembler::GenerateKeyedStoreIC() {
3492
  typedef StoreWithVectorDescriptor Descriptor;
3493 3494 3495 3496 3497 3498 3499 3500 3501

  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);
3502
  KeyedStoreIC(&p);
3503 3504
}

3505
void AccessorAssembler::GenerateKeyedStoreICTrampoline() {
3506
  typedef StoreDescriptor Descriptor;
3507 3508 3509 3510 3511 3512

  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);
3513
  Node* vector = LoadFeedbackVectorForStub();
3514

3515 3516
  TailCallBuiltin(Builtins::kKeyedStoreIC, context, receiver, name, value, slot,
                  vector);
3517 3518
}

3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
void AccessorAssembler::GenerateStoreInArrayLiteralIC() {
  typedef StoreWithVectorDescriptor Descriptor;

  Node* array = Parameter(Descriptor::kReceiver);
  Node* index = 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, array, index, value, slot, vector);
  StoreInArrayLiteralIC(&p);
}

3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555
void AccessorAssembler::GenerateCloneObjectIC_Slow() {
  typedef CloneObjectWithVectorDescriptor Descriptor;
  TNode<HeapObject> source = CAST(Parameter(Descriptor::kSource));
  TNode<Smi> flags = CAST(Parameter(Descriptor::kFlags));
  TNode<Context> context = CAST(Parameter(Descriptor::kContext));

  // The Slow case uses the same call interface as CloneObjectIC, so that it
  // can be tail called from it. However, the feedback slot and vector are not
  // used.

  TNode<Context> native_context = LoadNativeContext(context);
  TNode<JSFunction> object_fn =
      CAST(LoadContextElement(native_context, Context::OBJECT_FUNCTION_INDEX));
  TNode<Map> initial_map = CAST(
      LoadObjectField(object_fn, JSFunction::kPrototypeOrInitialMapOffset));
  CSA_ASSERT(this, IsMap(initial_map));

  TNode<JSObject> result = CAST(AllocateJSObjectFromMap(initial_map));

  {
    Label did_set_proto_if_needed(this);
    TNode<BoolT> is_null_proto = SmiNotEqual(
        SmiAnd(flags, SmiConstant(ObjectLiteral::kHasNullPrototype)),
3556
        SmiConstant(Smi::zero()));
3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577
    GotoIfNot(is_null_proto, &did_set_proto_if_needed);

    CallRuntime(Runtime::kInternalSetPrototype, context, result,
                NullConstant());

    Goto(&did_set_proto_if_needed);
    BIND(&did_set_proto_if_needed);
  }

  ReturnIf(IsNullOrUndefined(source), result);

  CSA_ASSERT(this, IsJSReceiver(source));

  Label call_runtime(this, Label::kDeferred);
  Label done(this);

  TNode<Map> map = LoadMap(source);
  TNode<Int32T> type = LoadMapInstanceType(map);
  {
    Label cont(this);
    GotoIf(IsJSObjectInstanceType(type), &cont);
3578
    GotoIf(InstanceTypeEqual(type, JS_PROXY_TYPE), &call_runtime);
3579 3580 3581 3582 3583 3584 3585 3586
    GotoIfNot(IsStringInstanceType(type), &done);
    Branch(SmiEqual(LoadStringLengthAsSmi(CAST(source)), SmiConstant(0)), &done,
           &call_runtime);
    BIND(&cont);
  }

  GotoIfNot(IsEmptyFixedArray(LoadElements(CAST(source))), &call_runtime);

3587 3588 3589 3590 3591 3592
  ForEachEnumerableOwnProperty(
      context, map, CAST(source), kPropertyAdditionOrder,
      [=](TNode<Name> key, TNode<Object> value) {
        SetPropertyInLiteral(context, result, key, value);
      },
      &call_runtime);
3593 3594 3595 3596 3597 3598 3599 3600 3601 3602
  Goto(&done);

  BIND(&call_runtime);
  CallRuntime(Runtime::kCopyDataProperties, context, result, source);

  Goto(&done);
  BIND(&done);
  Return(result);
}

3603 3604
void AccessorAssembler::GenerateCloneObjectIC() {
  typedef CloneObjectWithVectorDescriptor Descriptor;
3605
  TNode<HeapObject> source = CAST(Parameter(Descriptor::kSource));
3606 3607 3608 3609 3610 3611 3612
  Node* flags = Parameter(Descriptor::kFlags);
  Node* slot = Parameter(Descriptor::kSlot);
  Node* vector = Parameter(Descriptor::kVector);
  Node* context = Parameter(Descriptor::kContext);
  TVARIABLE(MaybeObject, var_handler);
  Label if_handler(this, &var_handler);
  Label miss(this, Label::kDeferred), try_polymorphic(this, Label::kDeferred),
3613
      try_megamorphic(this, Label::kDeferred), slow(this, Label::kDeferred);
3614

3615
  TNode<Map> source_map = LoadMap(UncheckedCast<HeapObject>(source));
3616
  GotoIf(IsDeprecatedMap(source_map), &miss);
3617 3618 3619

  GotoIf(IsUndefined(vector), &slow);

3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638
  TNode<MaybeObject> feedback = TryMonomorphicCase(
      slot, vector, source_map, &if_handler, &var_handler, &try_polymorphic);

  BIND(&if_handler);
  {
    Comment("CloneObjectIC_if_handler");

    // Handlers for the CloneObjectIC stub are weak references to the Map of
    // a result object.
    TNode<Map> result_map = CAST(var_handler.value());
    TVARIABLE(Object, var_properties, EmptyFixedArrayConstant());
    TVARIABLE(FixedArrayBase, var_elements, EmptyFixedArrayConstant());

    Label allocate_object(this);
    GotoIf(IsNullOrUndefined(source), &allocate_object);
    CSA_SLOW_ASSERT(this, IsJSObjectMap(result_map));

    // The IC fast case should only be taken if the result map a compatible
    // elements kind with the source object.
3639
    TNode<FixedArrayBase> source_elements = LoadElements(CAST(source));
3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660

    auto flags = ExtractFixedArrayFlag::kAllFixedArraysDontCopyCOW;
    var_elements = CAST(CloneFixedArray(source_elements, flags));

    // Copy the PropertyArray backing store. The source PropertyArray must be
    // either an Smi, or a PropertyArray.
    // FIXME: Make a CSA macro for this
    TNode<Object> source_properties =
        LoadObjectField(source, JSObject::kPropertiesOrHashOffset);
    {
      GotoIf(TaggedIsSmi(source_properties), &allocate_object);
      GotoIf(IsEmptyFixedArray(source_properties), &allocate_object);

      // This IC requires that the source object has fast properties
      CSA_SLOW_ASSERT(this, IsPropertyArray(CAST(source_properties)));
      TNode<IntPtrT> length = LoadPropertyArrayLength(
          UncheckedCast<PropertyArray>(source_properties));
      GotoIf(IntPtrEqual(length, IntPtrConstant(0)), &allocate_object);

      auto mode = INTPTR_PARAMETERS;
      var_properties = CAST(AllocatePropertyArray(length, mode));
3661 3662
      FillPropertyArrayWithUndefined(var_properties.value(), IntPtrConstant(0),
                                     length, mode);
3663
      CopyPropertyArrayValues(source_properties, var_properties.value(), length,
3664
                              SKIP_WRITE_BARRIER, mode, DestroySource::kNo);
3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
    }

    Goto(&allocate_object);
    BIND(&allocate_object);
    TNode<JSObject> object = UncheckedCast<JSObject>(AllocateJSObjectFromMap(
        result_map, var_properties.value(), var_elements.value()));
    ReturnIf(IsNullOrUndefined(source), object);

    // Lastly, clone any in-object properties.
    // Determine the inobject property capacity of both objects, and copy the
    // smaller number into the resulting object.
3676 3677 3678 3679 3680 3681
    TNode<IntPtrT> source_start =
        LoadMapInobjectPropertiesStartInWords(source_map);
    TNode<IntPtrT> source_size = LoadMapInstanceSizeInWords(source_map);
    TNode<IntPtrT> result_start =
        LoadMapInobjectPropertiesStartInWords(result_map);
    TNode<IntPtrT> field_offset_difference =
3682
        TimesTaggedSize(IntPtrSub(result_start, source_start));
3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694

    // If MutableHeapNumbers may be present in-object, allocations may occur
    // within this loop, thus the write barrier is required.
    //
    // TODO(caitp): skip the write barrier until the first MutableHeapNumber
    // field is found
    const bool may_use_mutable_heap_numbers = !FLAG_unbox_double_fields;

    BuildFastLoop(
        source_start, source_size,
        [=](Node* field_index) {
          TNode<IntPtrT> field_offset =
3695
              TimesTaggedSize(UncheckedCast<IntPtrT>(field_index));
3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712

          if (may_use_mutable_heap_numbers) {
            TNode<Object> field = LoadObjectField(source, field_offset);
            field = CloneIfMutablePrimitive(field);
            TNode<IntPtrT> result_offset =
                IntPtrAdd(field_offset, field_offset_difference);
            StoreObjectField(object, result_offset, field);
          } else {
            // Copy fields as raw data.
            TNode<IntPtrT> field =
                LoadObjectField<IntPtrT>(source, field_offset);
            TNode<IntPtrT> result_offset =
                IntPtrAdd(field_offset, field_offset_difference);
            StoreObjectFieldNoWriteBarrier(object, result_offset, field);
          }
        },
        1, INTPTR_PARAMETERS, IndexAdvanceMode::kPost);
3713 3714 3715 3716
    Return(object);
  }

  BIND(&try_polymorphic);
3717
  TNode<HeapObject> strong_feedback = GetHeapObjectIfStrong(feedback, &miss);
3718 3719 3720 3721
  {
    Comment("CloneObjectIC_try_polymorphic");
    GotoIfNot(IsWeakFixedArrayMap(LoadMap(strong_feedback)), &try_megamorphic);
    HandlePolymorphicCase(source_map, CAST(strong_feedback), &if_handler,
3722
                          &var_handler, &miss);
3723 3724 3725 3726 3727
  }

  BIND(&try_megamorphic);
  {
    Comment("CloneObjectIC_try_megamorphic");
3728 3729 3730 3731 3732 3733 3734 3735
    CSA_ASSERT(this,
               Word32Or(WordEqual(strong_feedback,
                                  LoadRoot(RootIndex::kuninitialized_symbol)),
                        WordEqual(strong_feedback,
                                  LoadRoot(RootIndex::kmegamorphic_symbol))));
    GotoIfNot(
        WordEqual(strong_feedback, LoadRoot(RootIndex::kmegamorphic_symbol)),
        &miss);
3736 3737 3738 3739 3740
    Goto(&slow);
  }

  BIND(&slow);
  {
3741 3742
    TailCallBuiltin(Builtins::kCloneObjectIC_Slow, context, source, flags, slot,
                    vector);
3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756
  }

  BIND(&miss);
  {
    Comment("CloneObjectIC_miss");
    Node* map_or_result = CallRuntime(Runtime::kCloneObjectIC_Miss, context,
                                      source, flags, slot, vector);
    var_handler = UncheckedCast<MaybeObject>(map_or_result);
    GotoIf(IsMap(map_or_result), &if_handler);
    CSA_ASSERT(this, IsJSObject(map_or_result));
    Return(map_or_result);
  }
}

3757 3758
}  // namespace internal
}  // namespace v8