ic-x64.cc 31.4 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#if V8_TARGET_ARCH_X64
6

7
#include "src/codegen.h"
8
#include "src/ic/ic.h"
9
#include "src/ic/ic-compiler.h"
10
#include "src/ic/stub-cache.h"
11

12 13
namespace v8 {
namespace internal {
14

15 16 17 18 19 20
// ----------------------------------------------------------------------------
// Static IC stub generators.
//

#define __ ACCESS_MASM(masm)

21

22
static void GenerateGlobalInstanceTypeCheck(MacroAssembler* masm, Register type,
23 24 25 26 27 28 29 30 31 32
                                            Label* global_object) {
  // Register usage:
  //   type: holds the receiver instance type on entry.
  __ cmpb(type, Immediate(JS_GLOBAL_OBJECT_TYPE));
  __ j(equal, global_object);
  __ cmpb(type, Immediate(JS_GLOBAL_PROXY_TYPE));
  __ j(equal, global_object);
}


33 34 35
// Helper function used to load a property from a dictionary backing storage.
// This function may return false negatives, so miss_label
// must always call a backup property load that is complete.
36 37
// This function is safe to call if name is not an internalized string,
// and will jump to the miss_label in that case.
38 39
// The generated code assumes that the receiver has slow properties,
// is not a global object and does not have interceptors.
40 41 42
static void GenerateDictionaryLoad(MacroAssembler* masm, Label* miss_label,
                                   Register elements, Register name,
                                   Register r0, Register r1, Register result) {
43 44 45 46 47 48 49 50 51 52 53 54 55
  // Register use:
  //
  // elements - holds the property dictionary on entry and is unchanged.
  //
  // name - holds the name of the property on entry and is unchanged.
  //
  // r0   - used to hold the capacity of the property dictionary.
  //
  // r1   - used to hold the index into the property dictionary.
  //
  // result - holds the result on exit if the load succeeded.

  Label done;
56

57
  // Probe the dictionary.
58 59
  NameDictionaryLookupStub::GeneratePositiveLookup(masm, miss_label, &done,
                                                   elements, name, r0, r1);
60

61
  // If probing finds an entry in the dictionary, r1 contains the
62 63
  // index into the dictionary. Check that the value is a normal
  // property.
64
  __ bind(&done);
65
  const int kElementsStartOffset =
66 67
      NameDictionary::kHeaderSize +
      NameDictionary::kElementsStartIndex * kPointerSize;
68
  const int kDetailsOffset = kElementsStartOffset + 2 * kPointerSize;
69 70
  __ Test(Operand(elements, r1, times_pointer_size,
                  kDetailsOffset - kHeapObjectTag),
71
          Smi::FromInt(PropertyDetails::TypeField::kMask));
72 73 74 75
  __ j(not_zero, miss_label);

  // Get the value at the masked, scaled index.
  const int kValueOffset = kElementsStartOffset + kPointerSize;
76 77
  __ movp(result, Operand(elements, r1, times_pointer_size,
                          kValueOffset - kHeapObjectTag));
78 79 80
}


81 82 83 84
// Helper function used to store a property to a dictionary backing
// storage. This function may fail to store a property even though it
// is in the dictionary, so code at miss_label must always call a
// backup property store that is complete. This function is safe to
85 86
// call if name is not an internalized string, and will jump to the miss_label
// in that case. The generated code assumes that the receiver has slow
87
// properties, is not a global object and does not have interceptors.
88 89 90
static void GenerateDictionaryStore(MacroAssembler* masm, Label* miss_label,
                                    Register elements, Register name,
                                    Register value, Register scratch0,
91 92 93 94 95 96 97 98 99
                                    Register scratch1) {
  // Register use:
  //
  // elements - holds the property dictionary on entry and is clobbered.
  //
  // name - holds the name of the property on entry and is unchanged.
  //
  // value - holds the value to store and is unchanged.
  //
100
  // scratch0 - used during the positive dictionary lookup and is clobbered.
101
  //
102
  // scratch1 - used for index into the property dictionary and is clobbered.
103 104 105
  Label done;

  // Probe the dictionary.
106 107
  NameDictionaryLookupStub::GeneratePositiveLookup(
      masm, miss_label, &done, elements, name, scratch0, scratch1);
108 109 110 111 112 113

  // If probing finds an entry in the dictionary, scratch0 contains the
  // index into the dictionary. Check that the value is a normal
  // property that is not read only.
  __ bind(&done);
  const int kElementsStartOffset =
114 115
      NameDictionary::kHeaderSize +
      NameDictionary::kElementsStartIndex * kPointerSize;
116
  const int kDetailsOffset = kElementsStartOffset + 2 * kPointerSize;
117
  const int kTypeAndReadOnlyMask =
118 119
      PropertyDetails::TypeField::kMask |
      PropertyDetails::AttributesField::encode(READ_ONLY);
120
  __ Test(Operand(elements, scratch1, times_pointer_size,
121 122 123 124 125 126
                  kDetailsOffset - kHeapObjectTag),
          Smi::FromInt(kTypeAndReadOnlyMask));
  __ j(not_zero, miss_label);

  // Store the value at the masked, scaled index.
  const int kValueOffset = kElementsStartOffset + kPointerSize;
127 128
  __ leap(scratch1, Operand(elements, scratch1, times_pointer_size,
                            kValueOffset - kHeapObjectTag));
129
  __ movp(Operand(scratch1, 0), value);
130 131

  // Update write barrier. Make sure not to clobber the value.
132
  __ movp(scratch0, value);
133
  __ RecordWrite(elements, scratch1, scratch0, kDontSaveFPRegs);
134 135 136
}


137 138 139
// Checks the receiver for special cases (value type, slow case bits).
// Falls through for regular JS object.
static void GenerateKeyedLoadReceiverCheck(MacroAssembler* masm,
140 141
                                           Register receiver, Register map,
                                           int interceptor_bit, Label* slow) {
142 143 144 145
  // Register use:
  //   receiver - holds the receiver and is unchanged.
  // Scratch registers:
  //   map - used to hold the map of the receiver.
146

147
  // Check that the object isn't a smi.
148
  __ JumpIfSmi(receiver, slow);
149 150 151 152 153

  // Check that the object is some kind of JS object EXCEPT JS Value type.
  // In the case that the object is a value-wrapper object,
  // we enter the runtime system to make sure that indexing
  // into string objects work as intended.
154
  DCHECK(JS_OBJECT_TYPE > JS_VALUE_TYPE);
155 156
  __ CmpObjectType(receiver, JS_OBJECT_TYPE, map);
  __ j(below, slow);
157 158

  // Check bit field.
159 160 161
  __ testb(
      FieldOperand(map, Map::kBitFieldOffset),
      Immediate((1 << Map::kIsAccessCheckNeeded) | (1 << interceptor_bit)));
162 163
  __ j(not_zero, slow);
}
164

165 166

// Loads an indexed element from a fast case array.
167 168 169
static void GenerateFastArrayLoad(MacroAssembler* masm, Register receiver,
                                  Register key, Register elements,
                                  Register scratch, Register result,
170
                                  Label* slow) {
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  // Register use:
  //
  // receiver - holds the receiver on entry.
  //            Unchanged unless 'result' is the same register.
  //
  // key      - holds the smi key on entry.
  //            Unchanged unless 'result' is the same register.
  //
  // result   - holds the result on exit if the load succeeded.
  //            Allowed to be the the same as 'receiver' or 'key'.
  //            Unchanged on bailout so 'receiver' and 'key' can be safely
  //            used by further computation.
  //
  // Scratch registers:
  //
186 187 188 189
  // elements - holds the elements of the receiver and its prototypes.
  //
  // scratch  - used to hold maps, prototypes, and the loaded value.
  Label check_prototypes, check_next_prototype;
190
  Label done, in_bounds, absent;
191

192
  __ movp(elements, FieldOperand(receiver, JSObject::kElementsOffset));
193
  __ AssertFastElements(elements);
194
  // Check that the key (index) is within bounds.
195 196
  __ SmiCompare(key, FieldOperand(elements, FixedArray::kLengthOffset));
  // Unsigned comparison rejects negative indices.
197 198 199 200 201 202 203 204 205 206 207 208
  __ j(below, &in_bounds);

  // Out-of-bounds. Check the prototype chain to see if we can just return
  // 'undefined'.
  __ SmiCompare(key, Smi::FromInt(0));
  __ j(less, slow);  // Negative keys can't take the fast OOB path.
  __ bind(&check_prototypes);
  __ movp(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
  __ bind(&check_next_prototype);
  __ movp(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
  // scratch: current prototype
  __ CompareRoot(scratch, Heap::kNullValueRootIndex);
209
  __ j(equal, &absent);
210 211 212 213 214 215 216 217 218 219 220 221 222 223
  __ movp(elements, FieldOperand(scratch, JSObject::kElementsOffset));
  __ movp(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
  // elements: elements of current prototype
  // scratch: map of current prototype
  __ CmpInstanceType(scratch, JS_OBJECT_TYPE);
  __ j(below, slow);
  __ testb(FieldOperand(scratch, Map::kBitFieldOffset),
           Immediate((1 << Map::kIsAccessCheckNeeded) |
                     (1 << Map::kHasIndexedInterceptor)));
  __ j(not_zero, slow);
  __ CompareRoot(elements, Heap::kEmptyFixedArrayRootIndex);
  __ j(not_equal, slow);
  __ jmp(&check_next_prototype);

224
  __ bind(&absent);
225 226
  __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
  __ jmp(&done);
227 228

  __ bind(&in_bounds);
229
  // Fast case: Do the load.
230
  SmiIndex index = masm->SmiToIndex(scratch, key, kPointerSizeLog2);
231
  __ movp(scratch, FieldOperand(elements, index.reg, index.scale,
232 233
                                FixedArray::kHeaderSize));
  __ CompareRoot(scratch, Heap::kTheHoleValueRootIndex);
234 235 236 237
  // In case the loaded value is the_hole we have to check the prototype chain.
  __ j(equal, &check_prototypes);
  __ Move(result, scratch);
  __ bind(&done);
238 239 240
}


241 242
// Checks whether a key is an array index string or a unique name.
// Falls through if the key is a unique name.
243 244 245
static void GenerateKeyNameCheck(MacroAssembler* masm, Register key,
                                 Register map, Register hash,
                                 Label* index_string, Label* not_unique) {
246 247 248 249 250
  // Register use:
  //   key - holds the key and is unchanged. Assumed to be non-smi.
  // Scratch registers:
  //   map - used to hold the map of the key.
  //   hash - used to hold the hash of the key.
251 252 253 254 255 256
  Label unique;
  __ CmpObjectType(key, LAST_UNIQUE_NAME_TYPE, map);
  __ j(above, not_unique);
  STATIC_ASSERT(LAST_UNIQUE_NAME_TYPE == FIRST_NONSTRING_TYPE);
  __ j(equal, &unique);

257
  // Is the string an array index, with cached numeric value?
258 259
  __ movl(hash, FieldOperand(key, Name::kHashFieldOffset));
  __ testl(hash, Immediate(Name::kContainsCachedArrayIndexMask));
260 261
  __ j(zero, index_string);  // The value in hash is used at jump target.

262 263
  // Is the string internalized? We already know it's a string so a single
  // bit test is enough.
264
  STATIC_ASSERT(kNotInternalizedTag != 0);
265
  __ testb(FieldOperand(map, Map::kInstanceTypeOffset),
266 267
           Immediate(kIsNotInternalizedMask));
  __ j(not_zero, not_unique);
268 269

  __ bind(&unique);
270 271
}

272
void KeyedLoadIC::GenerateMegamorphic(MacroAssembler* masm) {
273
  // The return address is on the stack.
274
  Label slow, check_name, index_smi, index_name, property_array_property;
275
  Label probe_dictionary, check_number_dictionary;
276

277 278
  Register receiver = LoadDescriptor::ReceiverRegister();
  Register key = LoadDescriptor::NameRegister();
279 280
  DCHECK(receiver.is(rdx));
  DCHECK(key.is(rcx));
281

282
  // Check that the key is a smi.
283
  __ JumpIfNotSmi(key, &check_name);
284 285 286 287
  __ bind(&index_smi);
  // Now the key is known to be a smi. This place is also jumped to from below
  // where a numeric string is converted to a smi.

288 289
  GenerateKeyedLoadReceiverCheck(masm, receiver, rax,
                                 Map::kHasIndexedInterceptor, &slow);
290

291
  // Check the receiver's map to see if it has fast elements.
292
  __ CheckFastElements(rax, &check_number_dictionary);
293

294
  GenerateFastArrayLoad(masm, receiver, key, rax, rbx, rax, &slow);
295
  Counters* counters = masm->isolate()->counters();
cbruni's avatar
cbruni committed
296
  __ IncrementCounter(counters->ic_keyed_load_generic_smi(), 1);
297 298
  __ ret(0);

299
  __ bind(&check_number_dictionary);
300 301
  __ SmiToInteger32(rbx, key);
  __ movp(rax, FieldOperand(receiver, JSObject::kElementsOffset));
302

303
  // Check whether the elements is a number dictionary.
304
  // rbx: key as untagged int32
305 306
  // rax: elements
  __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
307 308
                 Heap::kHashTableMapRootIndex);
  __ j(not_equal, &slow);
309
  __ LoadFromNumberDictionary(&slow, rax, key, rbx, r9, rdi, rax);
310 311
  __ ret(0);

312
  __ bind(&slow);
313
  // Slow case: Jump to runtime.
cbruni's avatar
cbruni committed
314
  __ IncrementCounter(counters->ic_keyed_load_generic_slow(), 1);
315
  KeyedLoadIC::GenerateRuntimeGetProperty(masm);
316

317
  __ bind(&check_name);
318
  GenerateKeyNameCheck(masm, key, rax, rbx, &index_name, &slow);
319

320 321
  GenerateKeyedLoadReceiverCheck(masm, receiver, rax, Map::kHasNamedInterceptor,
                                 &slow);
322

323 324
  // If the receiver is a fast-case object, check the stub cache. Otherwise
  // probe the dictionary.
325
  __ movp(rbx, FieldOperand(receiver, JSObject::kPropertiesOffset));
326 327
  __ CompareRoot(FieldOperand(rbx, HeapObject::kMapOffset),
                 Heap::kHashTableMapRootIndex);
328 329
  __ j(equal, &probe_dictionary);

330
  Register megamorphic_scratch = rdi;
331 332
  // The handlers in the stub cache expect a vector and slot. Since we won't
  // change the IC from any downstream misses, a dummy vector can be used.
333 334
  Register vector = LoadWithVectorDescriptor::VectorRegister();
  Register slot = LoadDescriptor::SlotRegister();
335
  DCHECK(!AreAliased(megamorphic_scratch, vector, slot));
336 337 338
  Handle<TypeFeedbackVector> dummy_vector =
      TypeFeedbackVector::DummyVector(masm->isolate());
  int slot_index = dummy_vector->GetIndex(
339
      FeedbackVectorSlot(TypeFeedbackVector::kDummyKeyedLoadICSlot));
340
  __ Move(vector, dummy_vector);
341
  __ Move(slot, Smi::FromInt(slot_index));
342

343 344
  masm->isolate()->load_stub_cache()->GenerateProbe(
      masm, receiver, key, megamorphic_scratch, no_reg);
345 346
  // Cache miss.
  GenerateMiss(masm);
347

348 349 350
  // Do a quick inline probe of the receiver's dictionary, if it
  // exists.
  __ bind(&probe_dictionary);
351 352
  // rbx: elements

353 354 355
  __ movp(rax, FieldOperand(receiver, JSObject::kMapOffset));
  __ movb(rax, FieldOperand(rax, Map::kInstanceTypeOffset));
  GenerateGlobalInstanceTypeCheck(masm, rax, &slow);
356

357
  GenerateDictionaryLoad(masm, &slow, rbx, key, rax, rdi, rax);
cbruni's avatar
cbruni committed
358
  __ IncrementCounter(counters->ic_keyed_load_generic_symbol(), 1);
359
  __ ret(0);
360

361
  __ bind(&index_name);
362
  __ IndexFromHash(rbx, key);
363
  __ jmp(&index_smi);
364 365
}

366

367
static void KeyedStoreGenerateMegamorphicHelper(
368 369
    MacroAssembler* masm, Label* fast_object, Label* fast_double, Label* slow,
    KeyedStoreCheckMap check_map, KeyedStoreIncrementLength increment_length) {
370 371 372
  Label transition_smi_elements;
  Label finish_object_store, non_double_value, transition_double_elements;
  Label fast_double_without_map_check;
373 374 375
  Register receiver = StoreDescriptor::ReceiverRegister();
  Register key = StoreDescriptor::NameRegister();
  Register value = StoreDescriptor::ValueRegister();
376 377 378
  DCHECK(receiver.is(rdx));
  DCHECK(key.is(rcx));
  DCHECK(value.is(rax));
379 380 381
  // Fast case: Do the store, could be either Object or double.
  __ bind(fast_object);
  // rbx: receiver's elements array (a FixedArray)
382
  // receiver is a JSArray.
383 384
  // r9: map of receiver
  if (check_map == kCheckMap) {
385
    __ movp(rdi, FieldOperand(rbx, HeapObject::kMapOffset));
386 387 388
    __ CompareRoot(rdi, Heap::kFixedArrayMapRootIndex);
    __ j(not_equal, fast_double);
  }
389 390 391 392 393

  // HOLECHECK: guards "A[i] = V"
  // We have to go to the runtime if the current value is the hole because
  // there may be a callback on the element
  Label holecheck_passed1;
394 395
  __ movp(kScratchRegister,
          FieldOperand(rbx, key, times_pointer_size, FixedArray::kHeaderSize));
396 397
  __ CompareRoot(kScratchRegister, Heap::kTheHoleValueRootIndex);
  __ j(not_equal, &holecheck_passed1);
398
  __ JumpIfDictionaryInPrototypeChain(receiver, rdi, kScratchRegister, slow);
399 400 401

  __ bind(&holecheck_passed1);

402 403
  // Smi stores don't require further checks.
  Label non_smi_value;
404
  __ JumpIfNotSmi(value, &non_smi_value);
405 406
  if (increment_length == kIncrementLength) {
    // Add 1 to receiver->length.
407 408
    __ leal(rdi, Operand(key, 1));
    __ Integer32ToSmiField(FieldOperand(receiver, JSArray::kLengthOffset), rdi);
409 410
  }
  // It's irrelevant whether array is smi-only or not when writing a smi.
411 412
  __ movp(FieldOperand(rbx, key, times_pointer_size, FixedArray::kHeaderSize),
          value);
413 414 415 416 417 418 419 420 421 422
  __ ret(0);

  __ bind(&non_smi_value);
  // Writing a non-smi, check whether array allows non-smi elements.
  // r9: receiver's map
  __ CheckFastObjectElements(r9, &transition_smi_elements);

  __ bind(&finish_object_store);
  if (increment_length == kIncrementLength) {
    // Add 1 to receiver->length.
423 424
    __ leal(rdi, Operand(key, 1));
    __ Integer32ToSmiField(FieldOperand(receiver, JSArray::kLengthOffset), rdi);
425
  }
426 427 428
  __ movp(FieldOperand(rbx, key, times_pointer_size, FixedArray::kHeaderSize),
          value);
  __ movp(rdx, value);  // Preserve the value which is returned.
429 430
  __ RecordWriteArray(rbx, rdx, key, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
                      OMIT_SMI_CHECK);
431 432 433 434 435 436 437 438 439 440
  __ ret(0);

  __ bind(fast_double);
  if (check_map == kCheckMap) {
    // Check for fast double array case. If this fails, call through to the
    // runtime.
    // rdi: elements array's map
    __ CompareRoot(rdi, Heap::kFixedDoubleArrayMapRootIndex);
    __ j(not_equal, slow);
  }
441 442 443 444 445

  // HOLECHECK: guards "A[i] double hole?"
  // We have to see if the double version of the hole is present. If so
  // go to the runtime.
  uint32_t offset = FixedDoubleArray::kHeaderSize + sizeof(kHoleNanLower32);
446
  __ cmpl(FieldOperand(rbx, key, times_8, offset), Immediate(kHoleNanUpper32));
447
  __ j(not_equal, &fast_double_without_map_check);
448
  __ JumpIfDictionaryInPrototypeChain(receiver, rdi, kScratchRegister, slow);
449

450
  __ bind(&fast_double_without_map_check);
451
  __ StoreNumberToDoubleElements(value, rbx, key, kScratchDoubleReg,
452 453 454
                                 &transition_double_elements);
  if (increment_length == kIncrementLength) {
    // Add 1 to receiver->length.
455 456
    __ leal(rdi, Operand(key, 1));
    __ Integer32ToSmiField(FieldOperand(receiver, JSArray::kLengthOffset), rdi);
457 458 459 460
  }
  __ ret(0);

  __ bind(&transition_smi_elements);
461
  __ movp(rbx, FieldOperand(receiver, HeapObject::kMapOffset));
462 463

  // Transition the array appropriately depending on the value type.
464
  __ movp(r9, FieldOperand(value, HeapObject::kMapOffset));
465 466 467 468 469 470
  __ CompareRoot(r9, Heap::kHeapNumberMapRootIndex);
  __ j(not_equal, &non_double_value);

  // Value is a double. Transition FAST_SMI_ELEMENTS ->
  // FAST_DOUBLE_ELEMENTS and complete the store.
  __ LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS,
471 472 473 474 475
                                         FAST_DOUBLE_ELEMENTS, rbx, rdi, slow);
  AllocationSiteMode mode =
      AllocationSite::GetMode(FAST_SMI_ELEMENTS, FAST_DOUBLE_ELEMENTS);
  ElementsTransitionGenerator::GenerateSmiToDouble(masm, receiver, key, value,
                                                   rbx, mode, slow);
476
  __ movp(rbx, FieldOperand(receiver, JSObject::kElementsOffset));
477 478 479 480
  __ jmp(&fast_double_without_map_check);

  __ bind(&non_double_value);
  // Value is not a double, FAST_SMI_ELEMENTS -> FAST_ELEMENTS
481 482
  __ LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS, FAST_ELEMENTS, rbx,
                                         rdi, slow);
483
  mode = AllocationSite::GetMode(FAST_SMI_ELEMENTS, FAST_ELEMENTS);
484 485
  ElementsTransitionGenerator::GenerateMapChangeElementsTransition(
      masm, receiver, key, value, rbx, mode, slow);
486
  __ movp(rbx, FieldOperand(receiver, JSObject::kElementsOffset));
487 488 489 490 491 492
  __ jmp(&finish_object_store);

  __ bind(&transition_double_elements);
  // Elements are FAST_DOUBLE_ELEMENTS, but value is an Object that's not a
  // HeapNumber. Make sure that the receiver is a Array with FAST_ELEMENTS and
  // transition array from FAST_DOUBLE_ELEMENTS to FAST_ELEMENTS
493
  __ movp(rbx, FieldOperand(receiver, HeapObject::kMapOffset));
494 495
  __ LoadTransitionedArrayMapConditional(FAST_DOUBLE_ELEMENTS, FAST_ELEMENTS,
                                         rbx, rdi, slow);
496
  mode = AllocationSite::GetMode(FAST_DOUBLE_ELEMENTS, FAST_ELEMENTS);
497 498
  ElementsTransitionGenerator::GenerateDoubleToObject(masm, receiver, key,
                                                      value, rbx, mode, slow);
499
  __ movp(rbx, FieldOperand(receiver, JSObject::kElementsOffset));
500 501 502 503
  __ jmp(&finish_object_store);
}


504
void KeyedStoreIC::GenerateMegamorphic(MacroAssembler* masm,
505
                                       LanguageMode language_mode) {
506
  // Return address is on the stack.
507 508
  Label slow, slow_with_tagged_index, fast_object, fast_object_grow;
  Label fast_double, fast_double_grow;
509
  Label array, extra, check_if_double_array, maybe_name_key, miss;
510 511
  Register receiver = StoreDescriptor::ReceiverRegister();
  Register key = StoreDescriptor::NameRegister();
512 513
  DCHECK(receiver.is(rdx));
  DCHECK(key.is(rcx));
514 515

  // Check that the object isn't a smi.
516
  __ JumpIfSmi(receiver, &slow_with_tagged_index);
517
  // Get the map from the receiver.
518
  __ movp(r9, FieldOperand(receiver, HeapObject::kMapOffset));
519 520
  // Check that the receiver does not require access checks.
  // The generic stub does not perform map checks.
521
  __ testb(FieldOperand(r9, Map::kBitFieldOffset),
522
           Immediate(1 << Map::kIsAccessCheckNeeded));
523
  __ j(not_zero, &slow_with_tagged_index);
524
  // Check that the key is a smi.
525
  __ JumpIfNotSmi(key, &maybe_name_key);
526
  __ SmiToInteger32(key, key);
527

528
  __ CmpInstanceType(r9, JS_ARRAY_TYPE);
529
  __ j(equal, &array);
530 531 532 533 534
  // Check that the object is some kind of JS object EXCEPT JS Value type. In
  // the case that the object is a value-wrapper object, we enter the runtime
  // system to make sure that indexing into string objects works as intended.
  STATIC_ASSERT(JS_VALUE_TYPE < JS_OBJECT_TYPE);
  __ CmpInstanceType(r9, JS_OBJECT_TYPE);
535 536 537
  __ j(below, &slow);

  // Object case: Check key against length in the elements array.
538
  __ movp(rbx, FieldOperand(receiver, JSObject::kElementsOffset));
539
  // Check array bounds.
540
  __ SmiCompareInteger32(FieldOperand(rbx, FixedArray::kLengthOffset), key);
541
  // rbx: FixedArray
542
  __ j(above, &fast_object);
543

544
  // Slow case: call runtime.
545
  __ bind(&slow);
546
  __ Integer32ToSmi(key, key);
547
  __ bind(&slow_with_tagged_index);
548
  PropertyICCompiler::GenerateRuntimeSetProperty(masm, language_mode);
549
  // Never returns to here.
550

551 552 553 554
  __ bind(&maybe_name_key);
  __ movp(r9, FieldOperand(key, HeapObject::kMapOffset));
  __ movzxbp(r9, FieldOperand(r9, Map::kInstanceTypeOffset));
  __ JumpIfNotUniqueNameInstanceType(r9, &slow_with_tagged_index);
555

556 557
  Register vector = StoreWithVectorDescriptor::VectorRegister();
  Register slot = StoreWithVectorDescriptor::SlotRegister();
558 559 560 561 562 563 564 565
  // The handlers in the stub cache expect a vector and slot. Since we won't
  // change the IC from any downstream misses, a dummy vector can be used.
  Handle<TypeFeedbackVector> dummy_vector =
      TypeFeedbackVector::DummyVector(masm->isolate());
  int slot_index = dummy_vector->GetIndex(
      FeedbackVectorSlot(TypeFeedbackVector::kDummyKeyedStoreICSlot));
  __ Move(vector, dummy_vector);
  __ Move(slot, Smi::FromInt(slot_index));
566

567 568
  masm->isolate()->store_stub_cache()->GenerateProbe(masm, receiver, key, r9,
                                                     no_reg);
569
  // Cache miss.
570
  __ jmp(&miss);
571

572 573 574 575
  // Extra capacity case: Check if there is extra capacity to
  // perform the store and update the length. Used for adding one
  // element to the array by writing to array[array.length].
  __ bind(&extra);
576
  // receiver is a JSArray.
577
  // rbx: receiver's elements array (a FixedArray)
578
  // flags: smicompare (receiver.length(), rbx)
579
  __ j(not_equal, &slow);  // do not leave holes in the array
580
  __ SmiCompareInteger32(FieldOperand(rbx, FixedArray::kLengthOffset), key);
581
  __ j(below_equal, &slow);
582
  // Increment index to get new length.
583
  __ movp(rdi, FieldOperand(rbx, HeapObject::kMapOffset));
584
  __ CompareRoot(rdi, Heap::kFixedArrayMapRootIndex);
585 586
  __ j(not_equal, &check_if_double_array);
  __ jmp(&fast_object_grow);
587

588
  __ bind(&check_if_double_array);
589 590 591
  // rdi: elements array's map
  __ CompareRoot(rdi, Heap::kFixedDoubleArrayMapRootIndex);
  __ j(not_equal, &slow);
592
  __ jmp(&fast_double_grow);
593 594

  // Array case: Get the length and the elements array from the JS
595 596
  // array. Check that the array is in fast mode (and writable); if it
  // is the length is always a smi.
597
  __ bind(&array);
598 599
  // receiver is a JSArray.
  __ movp(rbx, FieldOperand(receiver, JSObject::kElementsOffset));
600 601 602

  // Check the key against the length in the array, compute the
  // address to store into and fall through to fast case.
603
  __ SmiCompareInteger32(FieldOperand(receiver, JSArray::kLengthOffset), key);
604
  __ j(below_equal, &extra);
605

606 607 608 609 610
  KeyedStoreGenerateMegamorphicHelper(masm, &fast_object, &fast_double, &slow,
                                      kCheckMap, kDontIncrementLength);
  KeyedStoreGenerateMegamorphicHelper(masm, &fast_object_grow,
                                      &fast_double_grow, &slow, kDontCheckMap,
                                      kIncrementLength);
611

612 613
  __ bind(&miss);
  GenerateMiss(masm);
614 615
}

616
void LoadIC::GenerateNormal(MacroAssembler* masm) {
617
  Register dictionary = rax;
618 619
  DCHECK(!dictionary.is(LoadDescriptor::ReceiverRegister()));
  DCHECK(!dictionary.is(LoadDescriptor::NameRegister()));
620 621 622

  Label slow;

623
  __ movp(dictionary, FieldOperand(LoadDescriptor::ReceiverRegister(),
624 625
                                   JSObject::kPropertiesOffset));
  GenerateDictionaryLoad(masm, &slow, dictionary,
626
                         LoadDescriptor::NameRegister(), rbx, rdi, rax);
627 628
  __ ret(0);

629 630
  // Dictionary load failed, go slow (but don't miss).
  __ bind(&slow);
631
  LoadIC::GenerateRuntimeGetProperty(masm);
632 633
}

634

635 636 637
static void LoadIC_PushArgs(MacroAssembler* masm) {
  Register receiver = LoadDescriptor::ReceiverRegister();
  Register name = LoadDescriptor::NameRegister();
638 639
  Register slot = LoadDescriptor::SlotRegister();
  Register vector = LoadWithVectorDescriptor::VectorRegister();
640 641 642 643 644 645 646 647 648
  DCHECK(!rdi.is(receiver) && !rdi.is(name) && !rdi.is(slot) &&
         !rdi.is(vector));

  __ PopReturnAddressTo(rdi);
  __ Push(receiver);
  __ Push(name);
  __ Push(slot);
  __ Push(vector);
  __ PushReturnAddressFrom(rdi);
649
}
650 651


652
void LoadIC::GenerateMiss(MacroAssembler* masm) {
653
  // The return address is on the stack.
654

655
  Counters* counters = masm->isolate()->counters();
cbruni's avatar
cbruni committed
656
  __ IncrementCounter(counters->ic_load_miss(), 1);
657

658
  LoadIC_PushArgs(masm);
659 660

  // Perform tail call to the entry.
661
  __ TailCallRuntime(Runtime::kLoadIC_Miss);
662 663
}

664
void LoadIC::GenerateRuntimeGetProperty(MacroAssembler* masm) {
665
  // The return address is on the stack.
666 667
  Register receiver = LoadDescriptor::ReceiverRegister();
  Register name = LoadDescriptor::NameRegister();
668

669
  DCHECK(!rbx.is(receiver) && !rbx.is(name));
670

671 672 673 674
  __ PopReturnAddressTo(rbx);
  __ Push(receiver);
  __ Push(name);
  __ PushReturnAddressFrom(rbx);
675

676
  // Do tail-call to runtime routine.
677
  __ TailCallRuntime(Runtime::kGetProperty);
678 679 680
}


681
void KeyedLoadIC::GenerateMiss(MacroAssembler* masm) {
682
  // The return address is on the stack.
683
  Counters* counters = masm->isolate()->counters();
cbruni's avatar
cbruni committed
684
  __ IncrementCounter(counters->ic_keyed_load_miss(), 1);
685

686
  LoadIC_PushArgs(masm);
687 688

  // Perform tail call to the entry.
689
  __ TailCallRuntime(Runtime::kKeyedLoadIC_Miss);
690 691
}

692
void KeyedLoadIC::GenerateRuntimeGetProperty(MacroAssembler* masm) {
693
  // The return address is on the stack.
694 695
  Register receiver = LoadDescriptor::ReceiverRegister();
  Register name = LoadDescriptor::NameRegister();
696

697
  DCHECK(!rbx.is(receiver) && !rbx.is(name));
698

699 700 701 702
  __ PopReturnAddressTo(rbx);
  __ Push(receiver);
  __ Push(name);
  __ PushReturnAddressFrom(rbx);
703

704
  // Do tail-call to runtime routine.
705
  __ TailCallRuntime(Runtime::kKeyedGetProperty);
706 707
}

708
static void StoreIC_PushArgs(MacroAssembler* masm) {
709 710 711
  Register receiver = StoreDescriptor::ReceiverRegister();
  Register name = StoreDescriptor::NameRegister();
  Register value = StoreDescriptor::ValueRegister();
712 713
  Register temp = r11;
  DCHECK(!temp.is(receiver) && !temp.is(name) && !temp.is(value));
714

715
  __ PopReturnAddressTo(temp);
716 717 718
  __ Push(receiver);
  __ Push(name);
  __ Push(value);
719 720
  Register slot = StoreWithVectorDescriptor::SlotRegister();
  Register vector = StoreWithVectorDescriptor::VectorRegister();
721 722 723
  DCHECK(!temp.is(slot) && !temp.is(vector));
  __ Push(slot);
  __ Push(vector);
724
  __ PushReturnAddressFrom(temp);
725 726 727 728 729 730
}


void StoreIC::GenerateMiss(MacroAssembler* masm) {
  // Return address is on the stack.
  StoreIC_PushArgs(masm);
731 732

  // Perform tail call to the entry.
733
  __ TailCallRuntime(Runtime::kStoreIC_Miss);
734 735 736
}


737
void StoreIC::GenerateNormal(MacroAssembler* masm) {
738 739 740
  Register receiver = StoreDescriptor::ReceiverRegister();
  Register name = StoreDescriptor::NameRegister();
  Register value = StoreDescriptor::ValueRegister();
741
  Register dictionary = r11;
742 743
  DCHECK(!AreAliased(dictionary, StoreWithVectorDescriptor::VectorRegister(),
                     StoreWithVectorDescriptor::SlotRegister()));
744

745
  Label miss;
746

747 748
  __ movp(dictionary, FieldOperand(receiver, JSObject::kPropertiesOffset));
  GenerateDictionaryStore(masm, &miss, dictionary, name, value, r8, r9);
749
  Counters* counters = masm->isolate()->counters();
cbruni's avatar
cbruni committed
750
  __ IncrementCounter(counters->ic_store_normal_hit(), 1);
751 752 753
  __ ret(0);

  __ bind(&miss);
cbruni's avatar
cbruni committed
754
  __ IncrementCounter(counters->ic_store_normal_miss(), 1);
755 756 757 758
  GenerateMiss(masm);
}


759
void KeyedStoreIC::GenerateMiss(MacroAssembler* masm) {
760 761
  // Return address is on the stack.
  StoreIC_PushArgs(masm);
danno@chromium.org's avatar
danno@chromium.org committed
762 763

  // Do tail-call to runtime routine.
764
  __ TailCallRuntime(Runtime::kKeyedStoreIC_Miss);
765 766 767
}


768 769 770
#undef __


771 772 773 774 775 776 777 778
Condition CompareIC::ComputeCondition(Token::Value op) {
  switch (op) {
    case Token::EQ_STRICT:
    case Token::EQ:
      return equal;
    case Token::LT:
      return less;
    case Token::GT:
779
      return greater;
780
    case Token::LTE:
781
      return less_equal;
782 783 784 785 786 787 788 789 790
    case Token::GTE:
      return greater_equal;
    default:
      UNREACHABLE();
      return no_condition;
  }
}


791
bool CompareIC::HasInlinedSmiCode(Address address) {
792 793 794 795 796 797 798 799 800 801
  // The address of the instruction following the call.
  Address test_instruction_address =
      address + Assembler::kCallTargetAddressOffset;

  // If the instruction following the call is not a test al, nothing
  // was inlined.
  return *test_instruction_address == Assembler::kTestAlByte;
}


802 803
void PatchInlinedSmiCode(Isolate* isolate, Address address,
                         InlinedSmiCheck check) {
804 805 806 807 808 809 810
  // The address of the instruction following the call.
  Address test_instruction_address =
      address + Assembler::kCallTargetAddressOffset;

  // If the instruction following the call is not a test al, nothing
  // was inlined.
  if (*test_instruction_address != Assembler::kTestAlByte) {
811
    DCHECK(*test_instruction_address == Assembler::kNopByte);
812 813 814 815 816 817
    return;
  }

  Address delta_address = test_instruction_address + 1;
  // The delta to the start of the map check instruction and the
  // condition code uses at the patched jump.
818
  uint8_t delta = *reinterpret_cast<uint8_t*>(delta_address);
819
  if (FLAG_trace_ic) {
820 821 822
    PrintF("[  patching ic at %p, test=%p, delta=%d\n",
           static_cast<void*>(address),
           static_cast<void*>(test_instruction_address), delta);
823 824
  }

825 826 827
  // Patch with a short conditional jump. Enabling means switching from a short
  // jump-if-carry/not-carry to jump-if-zero/not-zero, whereas disabling is the
  // reverse operation of that.
828
  Address jmp_address = test_instruction_address - delta;
829
  DCHECK((check == ENABLE_INLINED_SMI_CHECK)
830 831 832 833 834 835 836 837
             ? (*jmp_address == Assembler::kJncShortOpcode ||
                *jmp_address == Assembler::kJcShortOpcode)
             : (*jmp_address == Assembler::kJnzShortOpcode ||
                *jmp_address == Assembler::kJzShortOpcode));
  Condition cc =
      (check == ENABLE_INLINED_SMI_CHECK)
          ? (*jmp_address == Assembler::kJncShortOpcode ? not_zero : zero)
          : (*jmp_address == Assembler::kJnzShortOpcode ? not_carry : carry);
838
  *jmp_address = static_cast<byte>(Assembler::kJccShortPrefix | cc);
839
}
840 841
}  // namespace internal
}  // namespace v8
842 843

#endif  // V8_TARGET_ARCH_X64