ic-ia32.cc 35.7 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
#include "src/v8.h"
6

7
#if V8_TARGET_ARCH_IA32
8

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

14 15
namespace v8 {
namespace internal {
16 17 18 19 20

// ----------------------------------------------------------------------------
// Static IC stub generators.
//

21
#define __ ACCESS_MASM(masm)
22 23


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


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

61 62 63
  Label done;

  // Probe the dictionary.
64 65
  NameDictionaryLookupStub::GeneratePositiveLookup(masm, miss_label, &done,
                                                   elements, name, r0, r1);
66 67 68 69

  // If probing finds an entry in the dictionary, r0 contains the
  // index into the dictionary. Check that the value is a normal
  // property.
70
  __ bind(&done);
71
  const int kElementsStartOffset =
72 73
      NameDictionary::kHeaderSize +
      NameDictionary::kElementsStartIndex * kPointerSize;
74
  const int kDetailsOffset = kElementsStartOffset + 2 * kPointerSize;
75
  __ test(Operand(elements, r0, times_4, kDetailsOffset - kHeapObjectTag),
76
          Immediate(PropertyDetails::TypeField::kMask << kSmiTagSize));
77
  __ j(not_zero, miss_label);
78 79 80

  // Get the value at the masked, scaled index.
  const int kValueOffset = kElementsStartOffset + kPointerSize;
81
  __ mov(result, Operand(elements, r0, times_4, kValueOffset - kHeapObjectTag));
82 83 84
}


85 86 87 88
// Helper function used to store a property to a dictionary backing
// storage. This function may fail to store a property eventhough 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
89
// call if name is not internalized, and will jump to the miss_label in
90 91
// that case. The generated code assumes that the receiver has slow
// properties, is not a global object and does not have interceptors.
92 93 94
static void GenerateDictionaryStore(MacroAssembler* masm, Label* miss_label,
                                    Register elements, Register name,
                                    Register value, Register r0, Register r1) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  // 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.
  //
  // r0 - used for index into the property dictionary and is clobbered.
  //
  // r1 - used to hold the capacity of the property dictionary and is clobbered.
  Label done;


  // Probe the dictionary.
110 111
  NameDictionaryLookupStub::GeneratePositiveLookup(masm, miss_label, &done,
                                                   elements, name, r0, r1);
112 113 114 115 116 117

  // If probing finds an entry in the dictionary, r0 contains the
  // index into the dictionary. Check that the value is a normal
  // property that is not read only.
  __ bind(&done);
  const int kElementsStartOffset =
118 119
      NameDictionary::kHeaderSize +
      NameDictionary::kElementsStartIndex * kPointerSize;
120
  const int kDetailsOffset = kElementsStartOffset + 2 * kPointerSize;
121 122
  const int kTypeAndReadOnlyMask =
      (PropertyDetails::TypeField::kMask |
123 124
       PropertyDetails::AttributesField::encode(READ_ONLY))
      << kSmiTagSize;
125 126
  __ test(Operand(elements, r0, times_4, kDetailsOffset - kHeapObjectTag),
          Immediate(kTypeAndReadOnlyMask));
127
  __ j(not_zero, miss_label);
128 129 130 131 132 133 134 135

  // Store the value at the masked, scaled index.
  const int kValueOffset = kElementsStartOffset + kPointerSize;
  __ lea(r0, Operand(elements, r0, times_4, kValueOffset - kHeapObjectTag));
  __ mov(Operand(r0, 0), value);

  // Update write barrier. Make sure not to clobber the value.
  __ mov(r1, value);
136
  __ RecordWrite(elements, r0, r1, kDontSaveFPRegs);
137 138 139
}


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

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

  // Get the map of the receiver.
154
  __ mov(map, FieldOperand(receiver, HeapObject::kMapOffset));
155 156

  // Check bit field.
157
  __ test_b(FieldOperand(map, Map::kBitFieldOffset),
158
            (1 << Map::kIsAccessCheckNeeded) | (1 << interceptor_bit));
159
  __ j(not_zero, slow);
160 161 162
  // 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
163
  // into string objects works as intended.
164
  DCHECK(JS_OBJECT_TYPE > JS_VALUE_TYPE);
165

166
  __ CmpInstanceType(map, JS_OBJECT_TYPE);
167
  __ j(below, slow);
168 169 170 171
}


// Loads an indexed element from a fast case array.
172
// If not_fast_array is NULL, doesn't perform the elements map check.
173 174 175
static void GenerateFastArrayLoad(MacroAssembler* masm, Register receiver,
                                  Register key, Register scratch,
                                  Register result, Label* not_fast_array,
176 177 178 179 180 181 182 183 184 185
                                  Label* out_of_range) {
  // Register use:
  //   receiver - holds the receiver and is unchanged.
  //   key - holds the key and is unchanged (must be a smi).
  // Scratch registers:
  //   scratch - used to hold elements of the receiver and the loaded value.
  //   result - holds the result on exit if the load succeeds and
  //            we fall through.

  __ mov(scratch, FieldOperand(receiver, JSObject::kElementsOffset));
186 187
  if (not_fast_array != NULL) {
    // Check that the object is in fast mode and writable.
188 189
    __ CheckMap(scratch, masm->isolate()->factory()->fixed_array_map(),
                not_fast_array, DONT_DO_SMI_CHECK);
190 191 192
  } else {
    __ AssertFastElements(scratch);
  }
193
  // Check that the key (index) is within bounds.
194 195
  __ cmp(key, FieldOperand(scratch, FixedArray::kLengthOffset));
  __ j(above_equal, out_of_range);
196
  // Fast case: Do the load.
197
  STATIC_ASSERT((kPointerSize == 4) && (kSmiTagSize == 1) && (kSmiTag == 0));
198
  __ mov(scratch, FieldOperand(scratch, key, times_2, FixedArray::kHeaderSize));
199
  __ cmp(scratch, Immediate(masm->isolate()->factory()->the_hole_value()));
200 201
  // In case the loaded value is the_hole we have to consult GetProperty
  // to ensure the prototype chain is searched.
202 203 204 205 206 207 208
  __ j(equal, out_of_range);
  if (!result.is(scratch)) {
    __ mov(result, scratch);
  }
}


209 210
// Checks whether a key is an array index string or a unique name.
// Falls through if the key is a unique name.
211 212 213
static void GenerateKeyNameCheck(MacroAssembler* masm, Register key,
                                 Register map, Register hash,
                                 Label* index_string, Label* not_unique) {
214 215 216 217 218
  // 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.
219 220 221 222 223
  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);
224 225

  // Is the string an array index, with cached numeric value?
226 227
  __ mov(hash, FieldOperand(key, Name::kHashFieldOffset));
  __ test(hash, Immediate(Name::kContainsCachedArrayIndexMask));
228
  __ j(zero, index_string);
229

230 231
  // Is the string internalized? We already know it's a string so a single
  // bit test is enough.
232 233 234 235
  STATIC_ASSERT(kNotInternalizedTag != 0);
  __ test_b(FieldOperand(map, Map::kInstanceTypeOffset),
            kIsNotInternalizedMask);
  __ j(not_zero, not_unique);
236 237

  __ bind(&unique);
238 239 240
}


241 242 243
static Operand GenerateMappedArgumentsLookup(
    MacroAssembler* masm, Register object, Register key, Register scratch1,
    Register scratch2, Label* unmapped_case, Label* slow_case) {
244 245 246
  Heap* heap = masm->isolate()->heap();
  Factory* factory = masm->isolate()->factory();

247 248 249
  // Check that the receiver is a JSObject. Because of the elements
  // map check later, we do not need to check for interceptors or
  // whether it requires access checks.
250
  __ JumpIfSmi(object, slow_case);
251 252 253
  // Check that the object is some kind of JSObject.
  __ CmpObjectType(object, FIRST_JS_RECEIVER_TYPE, scratch1);
  __ j(below, slow_case);
254 255

  // Check that the key is a positive smi.
256
  __ test(key, Immediate(0x80000001));
257 258 259
  __ j(not_zero, slow_case);

  // Load the elements into scratch1 and check its map.
260
  Handle<Map> arguments_map(heap->sloppy_arguments_elements_map());
261 262 263 264 265 266
  __ mov(scratch1, FieldOperand(object, JSObject::kElementsOffset));
  __ CheckMap(scratch1, arguments_map, slow_case, DONT_DO_SMI_CHECK);

  // Check if element is in the range of mapped arguments. If not, jump
  // to the unmapped lookup with the parameter map in scratch1.
  __ mov(scratch2, FieldOperand(scratch1, FixedArray::kLengthOffset));
267 268
  __ sub(scratch2, Immediate(Smi::FromInt(2)));
  __ cmp(key, scratch2);
269
  __ j(above_equal, unmapped_case);
270 271 272

  // Load element index and check whether it is the hole.
  const int kHeaderSize = FixedArray::kHeaderSize + 2 * kPointerSize;
273 274
  __ mov(scratch2,
         FieldOperand(scratch1, key, times_half_pointer_size, kHeaderSize));
275 276 277 278 279 280 281 282
  __ cmp(scratch2, factory->the_hole_value());
  __ j(equal, unmapped_case);

  // Load value from context and return it. We can reuse scratch1 because
  // we do not jump to the unmapped lookup (which requires the parameter
  // map in scratch1).
  const int kContextOffset = FixedArray::kHeaderSize;
  __ mov(scratch1, FieldOperand(scratch1, kContextOffset));
283
  return FieldOperand(scratch1, scratch2, times_half_pointer_size,
284 285 286 287 288 289 290 291 292 293 294 295 296 297
                      Context::kHeaderSize);
}


static Operand GenerateUnmappedArgumentsLookup(MacroAssembler* masm,
                                               Register key,
                                               Register parameter_map,
                                               Register scratch,
                                               Label* slow_case) {
  // Element is in arguments backing store, which is referenced by the
  // second element of the parameter_map.
  const int kBackingStoreOffset = FixedArray::kHeaderSize + kPointerSize;
  Register backing_store = parameter_map;
  __ mov(backing_store, FieldOperand(parameter_map, kBackingStoreOffset));
298 299
  Handle<Map> fixed_array_map(masm->isolate()->heap()->fixed_array_map());
  __ CheckMap(backing_store, fixed_array_map, slow_case, DONT_DO_SMI_CHECK);
300
  __ mov(scratch, FieldOperand(backing_store, FixedArray::kLengthOffset));
301
  __ cmp(key, scratch);
302
  __ j(greater_equal, slow_case);
303
  return FieldOperand(backing_store, key, times_half_pointer_size,
304 305 306 307
                      FixedArray::kHeaderSize);
}


308
void KeyedLoadIC::GenerateGeneric(MacroAssembler* masm) {
309
  // The return address is on the stack.
310
  Label slow, check_name, index_smi, index_name, property_array_property;
311
  Label probe_dictionary, check_number_dictionary;
312

313 314
  Register receiver = LoadDescriptor::ReceiverRegister();
  Register key = LoadDescriptor::NameRegister();
315 316
  DCHECK(receiver.is(edx));
  DCHECK(key.is(ecx));
317

318
  // Check that the key is a smi.
319
  __ JumpIfNotSmi(key, &check_name);
320 321 322 323
  __ bind(&index_smi);
  // Now the key is known to be a smi. This place is also jumped to from
  // where a numeric string is converted to a smi.

324 325
  GenerateKeyedLoadReceiverCheck(masm, receiver, eax,
                                 Map::kHasIndexedInterceptor, &slow);
326

327
  // Check the receiver's map to see if it has fast elements.
328 329
  __ CheckFastElements(eax, &check_number_dictionary);

330
  GenerateFastArrayLoad(masm, receiver, key, eax, eax, NULL, &slow);
331 332 333
  Isolate* isolate = masm->isolate();
  Counters* counters = isolate->counters();
  __ IncrementCounter(counters->keyed_load_generic_smi(), 1);
334
  __ ret(0);
335

336
  __ bind(&check_number_dictionary);
337
  __ mov(ebx, key);
338
  __ SmiUntag(ebx);
339
  __ mov(eax, FieldOperand(receiver, JSObject::kElementsOffset));
340

341 342
  // Check whether the elements is a number dictionary.
  // ebx: untagged index
343
  // eax: elements
344
  __ CheckMap(eax, isolate->factory()->hash_table_map(), &slow,
345
              DONT_DO_SMI_CHECK);
346 347 348
  Label slow_pop_receiver;
  // Push receiver on the stack to free up a register for the dictionary
  // probing.
349 350
  __ push(receiver);
  __ LoadFromNumberDictionary(&slow_pop_receiver, eax, key, ebx, edx, edi, eax);
351
  // Pop receiver before returning.
352
  __ pop(receiver);
353 354 355 356
  __ ret(0);

  __ bind(&slow_pop_receiver);
  // Pop the receiver from the stack and jump to runtime.
357
  __ pop(receiver);
358

359
  __ bind(&slow);
360
  // Slow case: jump to runtime.
361
  __ IncrementCounter(counters->keyed_load_generic_slow(), 1);
362
  GenerateRuntimeGetProperty(masm);
363

364
  __ bind(&check_name);
365
  GenerateKeyNameCheck(masm, key, eax, ebx, &index_name, &slow);
366

367 368
  GenerateKeyedLoadReceiverCheck(masm, receiver, eax, Map::kHasNamedInterceptor,
                                 &slow);
369

370
  // If the receiver is a fast-case object, check the keyed lookup
371
  // cache. Otherwise probe the dictionary.
372
  __ mov(ebx, FieldOperand(receiver, JSObject::kPropertiesOffset));
373
  __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
374
         Immediate(isolate->factory()->hash_table_map()));
375 376
  __ j(equal, &probe_dictionary);

377
  // The receiver's map is still in eax, compute the keyed lookup cache hash
378
  // based on 32 bits of the map pointer and the string hash.
379
  if (FLAG_debug_code) {
380
    __ cmp(eax, FieldOperand(receiver, HeapObject::kMapOffset));
381
    __ Check(equal, kMapIsNoLongerInEax);
382 383 384
  }
  __ mov(ebx, eax);  // Keep the map around for later.
  __ shr(eax, KeyedLookupCache::kMapHashShift);
385
  __ mov(edi, FieldOperand(key, String::kHashFieldOffset));
386
  __ shr(edi, String::kHashShift);
387 388
  __ xor_(eax, edi);
  __ and_(eax, KeyedLookupCache::kCapacityMask & KeyedLookupCache::kHashMask);
389

390
  // Load the key (consisting of map and internalized string) from the cache and
391
  // check for match.
392 393 394
  Label load_in_object_property;
  static const int kEntriesPerBucket = KeyedLookupCache::kEntriesPerBucket;
  Label hit_on_nth_entry[kEntriesPerBucket];
395 396
  ExternalReference cache_keys =
      ExternalReference::keyed_lookup_cache_keys(masm->isolate());
397

398 399
  for (int i = 0; i < kEntriesPerBucket - 1; i++) {
    Label try_next_entry;
400
    __ mov(edi, eax);
401 402 403 404 405 406 407
    __ shl(edi, kPointerSizeLog2 + 1);
    if (i != 0) {
      __ add(edi, Immediate(kPointerSize * i * 2));
    }
    __ cmp(ebx, Operand::StaticArray(edi, times_1, cache_keys));
    __ j(not_equal, &try_next_entry);
    __ add(edi, Immediate(kPointerSize));
408
    __ cmp(key, Operand::StaticArray(edi, times_1, cache_keys));
409 410 411 412
    __ j(equal, &hit_on_nth_entry[i]);
    __ bind(&try_next_entry);
  }

413
  __ lea(edi, Operand(eax, 1));
414
  __ shl(edi, kPointerSizeLog2 + 1);
415
  __ add(edi, Immediate(kPointerSize * (kEntriesPerBucket - 1) * 2));
416
  __ cmp(ebx, Operand::StaticArray(edi, times_1, cache_keys));
417
  __ j(not_equal, &slow);
418
  __ add(edi, Immediate(kPointerSize));
419
  __ cmp(key, Operand::StaticArray(edi, times_1, cache_keys));
420 421
  __ j(not_equal, &slow);

422
  // Get field offset.
423 424
  // ebx      : receiver's map
  // eax      : lookup cache index
425 426
  ExternalReference cache_field_offsets =
      ExternalReference::keyed_lookup_cache_field_offsets(masm->isolate());
427

428 429 430 431
  // Hit on nth entry.
  for (int i = kEntriesPerBucket - 1; i >= 0; i--) {
    __ bind(&hit_on_nth_entry[i]);
    if (i != 0) {
432
      __ add(eax, Immediate(i));
433 434
    }
    __ mov(edi,
435 436 437
           Operand::StaticArray(eax, times_pointer_size, cache_field_offsets));
    __ movzx_b(eax, FieldOperand(ebx, Map::kInObjectPropertiesOffset));
    __ sub(edi, eax);
438 439 440 441 442
    __ j(above_equal, &property_array_property);
    if (i != 0) {
      __ jmp(&load_in_object_property);
    }
  }
443 444

  // Load in-object property.
445
  __ bind(&load_in_object_property);
446 447
  __ movzx_b(eax, FieldOperand(ebx, Map::kInstanceSizeOffset));
  __ add(eax, edi);
448
  __ mov(eax, FieldOperand(receiver, eax, times_pointer_size, 0));
449
  __ IncrementCounter(counters->keyed_load_generic_lookup_cache(), 1);
450 451
  __ ret(0);

452 453
  // Load property array property.
  __ bind(&property_array_property);
454
  __ mov(eax, FieldOperand(receiver, JSObject::kPropertiesOffset));
455 456
  __ mov(eax,
         FieldOperand(eax, edi, times_pointer_size, FixedArray::kHeaderSize));
457
  __ IncrementCounter(counters->keyed_load_generic_lookup_cache(), 1);
458 459
  __ ret(0);

460 461 462
  // Do a quick inline probe of the receiver's dictionary, if it
  // exists.
  __ bind(&probe_dictionary);
463

464
  __ mov(eax, FieldOperand(receiver, JSObject::kMapOffset));
465 466
  __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
  GenerateGlobalInstanceTypeCheck(masm, eax, &slow);
467

468
  GenerateDictionaryLoad(masm, &slow, ebx, key, eax, edi, eax);
469
  __ IncrementCounter(counters->keyed_load_generic_symbol(), 1);
470
  __ ret(0);
471

472
  __ bind(&index_name);
473
  __ IndexFromHash(ebx, key);
474 475
  // Now jump to the place where smi keys are handled.
  __ jmp(&index_smi);
476 477 478
}


479
void KeyedStoreIC::GenerateSloppyArguments(MacroAssembler* masm) {
480
  // Return address is on the stack.
481
  Label slow, notin;
482 483 484
  Register receiver = StoreDescriptor::ReceiverRegister();
  Register name = StoreDescriptor::NameRegister();
  Register value = StoreDescriptor::ValueRegister();
485 486 487
  DCHECK(receiver.is(edx));
  DCHECK(name.is(ecx));
  DCHECK(value.is(eax));
488

489 490
  Operand mapped_location = GenerateMappedArgumentsLookup(
      masm, receiver, name, ebx, edi, &notin, &slow);
491
  __ mov(mapped_location, value);
492
  __ lea(ecx, mapped_location);
493
  __ mov(edx, value);
494
  __ RecordWrite(ebx, ecx, edx, kDontSaveFPRegs);
495 496 497 498
  __ Ret();
  __ bind(&notin);
  // The unmapped lookup expects that the parameter map is in ebx.
  Operand unmapped_location =
499 500
      GenerateUnmappedArgumentsLookup(masm, name, ebx, edi, &slow);
  __ mov(unmapped_location, value);
501
  __ lea(edi, unmapped_location);
502
  __ mov(edx, value);
503
  __ RecordWrite(ebx, edi, edx, kDontSaveFPRegs);
504 505
  __ Ret();
  __ bind(&slow);
506
  GenerateMiss(masm);
507 508 509
}


510
static void KeyedStoreGenerateMegamorphicHelper(
511 512
    MacroAssembler* masm, Label* fast_object, Label* fast_double, Label* slow,
    KeyedStoreCheckMap check_map, KeyedStoreIncrementLength increment_length) {
513 514 515
  Label transition_smi_elements;
  Label finish_object_store, non_double_value, transition_double_elements;
  Label fast_double_without_map_check;
516 517 518
  Register receiver = StoreDescriptor::ReceiverRegister();
  Register key = StoreDescriptor::NameRegister();
  Register value = StoreDescriptor::ValueRegister();
519 520 521
  DCHECK(receiver.is(edx));
  DCHECK(key.is(ecx));
  DCHECK(value.is(eax));
522
  // key is a smi.
523 524 525 526
  // ebx: FixedArray receiver->elements
  // edi: receiver map
  // Fast case: Do the store, could either Object or double.
  __ bind(fast_object);
527
  if (check_map == kCheckMap) {
528 529 530 531
    __ mov(edi, FieldOperand(ebx, HeapObject::kMapOffset));
    __ cmp(edi, masm->isolate()->factory()->fixed_array_map());
    __ j(not_equal, fast_double);
  }
532 533 534 535 536

  // 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;
537
  __ cmp(FixedArrayElementOperand(ebx, key),
538 539
         masm->isolate()->factory()->the_hole_value());
  __ j(not_equal, &holecheck_passed1);
540 541
  __ JumpIfDictionaryInPrototypeChain(receiver, ebx, edi, slow);
  __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
542 543 544

  __ bind(&holecheck_passed1);

545 546
  // Smi stores don't require further checks.
  Label non_smi_value;
547
  __ JumpIfNotSmi(value, &non_smi_value);
548
  if (increment_length == kIncrementLength) {
549
    // Add 1 to receiver->length.
550
    __ add(FieldOperand(receiver, JSArray::kLengthOffset),
551 552 553
           Immediate(Smi::FromInt(1)));
  }
  // It's irrelevant whether array is smi-only or not when writing a smi.
554
  __ mov(FixedArrayElementOperand(ebx, key), value);
555 556 557 558
  __ ret(0);

  __ bind(&non_smi_value);
  // Escape to elements kind transition case.
559
  __ mov(edi, FieldOperand(receiver, HeapObject::kMapOffset));
560 561 562 563
  __ CheckFastObjectElements(edi, &transition_smi_elements);

  // Fast elements array, store the value to the elements backing store.
  __ bind(&finish_object_store);
564
  if (increment_length == kIncrementLength) {
565
    // Add 1 to receiver->length.
566
    __ add(FieldOperand(receiver, JSArray::kLengthOffset),
567 568
           Immediate(Smi::FromInt(1)));
  }
569
  __ mov(FixedArrayElementOperand(ebx, key), value);
570
  // Update write barrier for the elements array address.
571
  __ mov(edx, value);  // Preserve the value which is returned.
572 573
  __ RecordWriteArray(ebx, edx, key, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
                      OMIT_SMI_CHECK);
574 575 576
  __ ret(0);

  __ bind(fast_double);
577
  if (check_map == kCheckMap) {
578 579 580 581 582 583 584
    // Check for fast double array case. If this fails, call through to the
    // runtime.
    __ cmp(edi, masm->isolate()->factory()->fixed_double_array_map());
    __ j(not_equal, slow);
    // If the value is a number, store it as a double in the FastDoubleElements
    // array.
  }
585 586 587 588 589

  // 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);
590
  __ cmp(FieldOperand(ebx, key, times_4, offset), Immediate(kHoleNanUpper32));
591
  __ j(not_equal, &fast_double_without_map_check);
592 593
  __ JumpIfDictionaryInPrototypeChain(receiver, ebx, edi, slow);
  __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
594

595
  __ bind(&fast_double_without_map_check);
596
  __ StoreNumberToDoubleElements(value, ebx, key, edi, xmm0,
597
                                 &transition_double_elements);
598
  if (increment_length == kIncrementLength) {
599
    // Add 1 to receiver->length.
600
    __ add(FieldOperand(receiver, JSArray::kLengthOffset),
601 602 603 604 605
           Immediate(Smi::FromInt(1)));
  }
  __ ret(0);

  __ bind(&transition_smi_elements);
606
  __ mov(ebx, FieldOperand(receiver, HeapObject::kMapOffset));
607 608

  // Transition the array appropriately depending on the value type.
609 610
  __ CheckMap(value, masm->isolate()->factory()->heap_number_map(),
              &non_double_value, DONT_DO_SMI_CHECK);
611 612 613 614

  // Value is a double. Transition FAST_SMI_ELEMENTS -> FAST_DOUBLE_ELEMENTS
  // and complete the store.
  __ LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS,
615 616 617 618 619
                                         FAST_DOUBLE_ELEMENTS, ebx, edi, slow);
  AllocationSiteMode mode =
      AllocationSite::GetMode(FAST_SMI_ELEMENTS, FAST_DOUBLE_ELEMENTS);
  ElementsTransitionGenerator::GenerateSmiToDouble(masm, receiver, key, value,
                                                   ebx, mode, slow);
620
  __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
621 622 623 624
  __ jmp(&fast_double_without_map_check);

  __ bind(&non_double_value);
  // Value is not a double, FAST_SMI_ELEMENTS -> FAST_ELEMENTS
625 626
  __ LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS, FAST_ELEMENTS, ebx,
                                         edi, slow);
627
  mode = AllocationSite::GetMode(FAST_SMI_ELEMENTS, FAST_ELEMENTS);
628 629
  ElementsTransitionGenerator::GenerateMapChangeElementsTransition(
      masm, receiver, key, value, ebx, mode, slow);
630
  __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
631 632 633 634 635 636
  __ 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
637
  __ mov(ebx, FieldOperand(receiver, HeapObject::kMapOffset));
638 639
  __ LoadTransitionedArrayMapConditional(FAST_DOUBLE_ELEMENTS, FAST_ELEMENTS,
                                         ebx, edi, slow);
640
  mode = AllocationSite::GetMode(FAST_DOUBLE_ELEMENTS, FAST_ELEMENTS);
641 642
  ElementsTransitionGenerator::GenerateDoubleToObject(masm, receiver, key,
                                                      value, ebx, mode, slow);
643
  __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
644 645 646 647
  __ jmp(&finish_object_store);
}


648 649
void KeyedStoreIC::GenerateMegamorphic(MacroAssembler* masm,
                                       StrictMode strict_mode) {
650
  // Return address is on the stack.
651 652
  Label slow, fast_object, fast_object_grow;
  Label fast_double, fast_double_grow;
653
  Label array, extra, check_if_double_array, maybe_name_key, miss;
654 655
  Register receiver = StoreDescriptor::ReceiverRegister();
  Register key = StoreDescriptor::NameRegister();
656 657
  DCHECK(receiver.is(edx));
  DCHECK(key.is(ecx));
658

659
  // Check that the object isn't a smi.
660
  __ JumpIfSmi(receiver, &slow);
661
  // Get the map from the receiver.
662
  __ mov(edi, FieldOperand(receiver, HeapObject::kMapOffset));
663 664
  // Check that the receiver does not require access checks and is not observed.
  // The generic stub does not perform map checks or handle observed objects.
665
  __ test_b(FieldOperand(edi, Map::kBitFieldOffset),
666
            1 << Map::kIsAccessCheckNeeded | 1 << Map::kIsObserved);
667
  __ j(not_zero, &slow);
668
  // Check that the key is a smi.
669
  __ JumpIfNotSmi(key, &maybe_name_key);
670
  __ CmpInstanceType(edi, JS_ARRAY_TYPE);
671
  __ j(equal, &array);
672
  // Check that the object is some kind of JSObject.
673
  __ CmpInstanceType(edi, FIRST_JS_OBJECT_TYPE);
674
  __ j(below, &slow);
675 676

  // Object case: Check key against length in the elements array.
677
  // Key is a smi.
678
  // edi: receiver map
679
  __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
680
  // Check array bounds. Both the key and the length of FixedArray are smis.
681
  __ cmp(key, FieldOperand(ebx, FixedArray::kLengthOffset));
682
  __ j(below, &fast_object);
683

684
  // Slow case: call runtime.
685
  __ bind(&slow);
686
  PropertyICCompiler::GenerateRuntimeSetProperty(masm, strict_mode);
687 688 689 690 691 692 693 694 695 696 697
  // Never returns to here.

  __ bind(&maybe_name_key);
  __ mov(ebx, FieldOperand(key, HeapObject::kMapOffset));
  __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
  __ JumpIfNotUniqueNameInstanceType(ebx, &slow);
  Code::Flags flags = Code::RemoveTypeAndHolderFromFlags(
      Code::ComputeHandlerFlags(Code::STORE_IC));
  masm->isolate()->stub_cache()->GenerateProbe(masm, flags, false, receiver,
                                               key, ebx, no_reg);
  // Cache miss.
698
  __ jmp(&miss);
699 700 701 702 703

  // 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);
704 705
  // receiver is a JSArray.
  // key is a smi.
706 707
  // ebx: receiver->elements, a FixedArray
  // edi: receiver map
708
  // flags: compare (key, receiver.length())
709
  // do not leave holes in the array:
710
  __ j(not_equal, &slow);
711
  __ cmp(key, FieldOperand(ebx, FixedArray::kLengthOffset));
712
  __ j(above_equal, &slow);
713 714 715
  __ mov(edi, FieldOperand(ebx, HeapObject::kMapOffset));
  __ cmp(edi, masm->isolate()->factory()->fixed_array_map());
  __ j(not_equal, &check_if_double_array);
716
  __ jmp(&fast_object_grow);
717 718

  __ bind(&check_if_double_array);
719 720
  __ cmp(edi, masm->isolate()->factory()->fixed_double_array_map());
  __ j(not_equal, &slow);
721
  __ jmp(&fast_double_grow);
722 723

  // Array case: Get the length and the elements array from the JS
724 725
  // array. Check that the array is in fast mode (and writable); if it
  // is the length is always a smi.
726
  __ bind(&array);
727 728
  // receiver is a JSArray.
  // key is a smi.
729
  // edi: receiver map
730
  __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
731

732 733
  // Check the key against the length in the array and fall through to the
  // common store code.
734
  __ cmp(key, FieldOperand(receiver, JSArray::kLengthOffset));  // Compare smis.
735
  __ j(above_equal, &extra);
736

737 738 739 740 741
  KeyedStoreGenerateMegamorphicHelper(masm, &fast_object, &fast_double, &slow,
                                      kCheckMap, kDontIncrementLength);
  KeyedStoreGenerateMegamorphicHelper(masm, &fast_object_grow,
                                      &fast_double_grow, &slow, kDontCheckMap,
                                      kIncrementLength);
742

743 744
  __ bind(&miss);
  GenerateMiss(masm);
745 746 747 748
}


void LoadIC::GenerateNormal(MacroAssembler* masm) {
749
  Register dictionary = eax;
750 751
  DCHECK(!dictionary.is(LoadDescriptor::ReceiverRegister()));
  DCHECK(!dictionary.is(LoadDescriptor::NameRegister()));
752

753
  Label slow;
754

755
  __ mov(dictionary, FieldOperand(LoadDescriptor::ReceiverRegister(),
756 757
                                  JSObject::kPropertiesOffset));
  GenerateDictionaryLoad(masm, &slow, dictionary,
758
                         LoadDescriptor::NameRegister(), edi, ebx, eax);
759 760
  __ ret(0);

761 762 763
  // Dictionary load failed, go slow (but don't miss).
  __ bind(&slow);
  GenerateRuntimeGetProperty(masm);
764 765 766
}


767
static void LoadIC_PushArgs(MacroAssembler* masm) {
768 769
  Register receiver = LoadDescriptor::ReceiverRegister();
  Register name = LoadDescriptor::NameRegister();
770
  DCHECK(!ebx.is(receiver) && !ebx.is(name));
771 772 773 774 775 776 777 778

  __ pop(ebx);
  __ push(receiver);
  __ push(name);
  __ push(ebx);
}


779
void LoadIC::GenerateMiss(MacroAssembler* masm) {
780
  // Return address is on the stack.
781
  __ IncrementCounter(masm->isolate()->counters()->load_miss(), 1);
782

783
  LoadIC_PushArgs(masm);
784

785
  // Perform tail call to the entry.
786 787
  ExternalReference ref =
      ExternalReference(IC_Utility(kLoadIC_Miss), masm->isolate());
serya@chromium.org's avatar
serya@chromium.org committed
788
  __ TailCallExternalReference(ref, 2, 1);
789 790 791
}


792
void LoadIC::GenerateRuntimeGetProperty(MacroAssembler* masm) {
793
  // Return address is on the stack.
794
  LoadIC_PushArgs(masm);
795 796 797 798 799 800

  // Perform tail call to the entry.
  __ TailCallRuntime(Runtime::kGetProperty, 2, 1);
}


801
void KeyedLoadIC::GenerateMiss(MacroAssembler* masm) {
802
  // Return address is on the stack.
803
  __ IncrementCounter(masm->isolate()->counters()->keyed_load_miss(), 1);
804

805
  LoadIC_PushArgs(masm);
806 807

  // Perform tail call to the entry.
808 809
  ExternalReference ref =
      ExternalReference(IC_Utility(kKeyedLoadIC_Miss), masm->isolate());
serya@chromium.org's avatar
serya@chromium.org committed
810
  __ TailCallExternalReference(ref, 2, 1);
811 812 813
}


814
void KeyedLoadIC::GenerateRuntimeGetProperty(MacroAssembler* masm) {
815
  // Return address is on the stack.
816
  LoadIC_PushArgs(masm);
817

818
  // Perform tail call to the entry.
serya@chromium.org's avatar
serya@chromium.org committed
819
  __ TailCallRuntime(Runtime::kKeyedGetProperty, 2, 1);
820 821 822
}


823
void StoreIC::GenerateMegamorphic(MacroAssembler* masm) {
824
  // Return address is on the stack.
825 826
  Code::Flags flags = Code::RemoveTypeAndHolderFromFlags(
      Code::ComputeHandlerFlags(Code::STORE_IC));
827
  masm->isolate()->stub_cache()->GenerateProbe(
828
      masm, flags, false, StoreDescriptor::ReceiverRegister(),
829
      StoreDescriptor::NameRegister(), ebx, no_reg);
830 831

  // Cache miss: Jump to runtime.
832
  GenerateMiss(masm);
833 834 835
}


836
static void StoreIC_PushArgs(MacroAssembler* masm) {
837 838 839
  Register receiver = StoreDescriptor::ReceiverRegister();
  Register name = StoreDescriptor::NameRegister();
  Register value = StoreDescriptor::ValueRegister();
840

841
  DCHECK(!ebx.is(receiver) && !ebx.is(name) && !ebx.is(value));
842 843

  __ pop(ebx);
844 845 846
  __ push(receiver);
  __ push(name);
  __ push(value);
847
  __ push(ebx);
848 849 850 851 852 853
}


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

855
  // Perform tail call to the entry.
856 857
  ExternalReference ref =
      ExternalReference(IC_Utility(kStoreIC_Miss), masm->isolate());
serya@chromium.org's avatar
serya@chromium.org committed
858
  __ TailCallExternalReference(ref, 3, 1);
859 860 861
}


862
void StoreIC::GenerateNormal(MacroAssembler* masm) {
863
  Label restore_miss;
864 865 866
  Register receiver = StoreDescriptor::ReceiverRegister();
  Register name = StoreDescriptor::NameRegister();
  Register value = StoreDescriptor::ValueRegister();
867
  Register dictionary = ebx;
868

869
  __ mov(dictionary, FieldOperand(receiver, JSObject::kPropertiesOffset));
870 871 872 873

  // A lot of registers are needed for storing to slow case
  // objects. Push and restore receiver but rely on
  // GenerateDictionaryStore preserving the value and name.
874
  __ push(receiver);
875 876
  GenerateDictionaryStore(masm, &restore_miss, dictionary, name, value,
                          receiver, edi);
877
  __ Drop(1);
878 879
  Counters* counters = masm->isolate()->counters();
  __ IncrementCounter(counters->store_normal_hit(), 1);
880 881 882
  __ ret(0);

  __ bind(&restore_miss);
883
  __ pop(receiver);
884
  __ IncrementCounter(counters->store_normal_miss(), 1);
885 886 887 888
  GenerateMiss(masm);
}


889
void KeyedStoreIC::GenerateMiss(MacroAssembler* masm) {
890 891
  // Return address is on the stack.
  StoreIC_PushArgs(masm);
892 893

  // Do tail-call to runtime routine.
894 895
  ExternalReference ref =
      ExternalReference(IC_Utility(kKeyedStoreIC_Miss), masm->isolate());
danno@chromium.org's avatar
danno@chromium.org committed
896 897 898 899
  __ TailCallExternalReference(ref, 3, 1);
}


900 901 902
#undef __


903 904 905 906 907 908 909 910
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:
911
      return greater;
912
    case Token::LTE:
913
      return less_equal;
914 915 916 917 918 919 920 921 922
    case Token::GTE:
      return greater_equal;
    default:
      UNREACHABLE();
      return no_condition;
  }
}


923
bool CompareIC::HasInlinedSmiCode(Address address) {
924 925 926 927 928 929 930 931 932 933
  // 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;
}


934
void PatchInlinedSmiCode(Address address, InlinedSmiCheck check) {
935 936 937 938 939 940 941
  // 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) {
942
    DCHECK(*test_instruction_address == Assembler::kNopByte);
943 944 945 946 947 948
    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.
949
  uint8_t delta = *reinterpret_cast<uint8_t*>(delta_address);
950
  if (FLAG_trace_ic) {
951 952
    PrintF("[  patching ic at %p, test=%p, delta=%d\n", address,
           test_instruction_address, delta);
953 954
  }

955 956 957
  // 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.
958
  Address jmp_address = test_instruction_address - delta;
959
  DCHECK((check == ENABLE_INLINED_SMI_CHECK)
960 961 962 963 964 965 966 967
             ? (*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);
968
  *jmp_address = static_cast<byte>(Assembler::kJccShortPrefix | cc);
969
}
970 971
}
}  // namespace v8::internal
972 973

#endif  // V8_TARGET_ARCH_IA32