blob: 1004ac036b89c4b4e4dd7adb694c0340776effbc [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/v8.h"
6
7#if V8_TARGET_ARCH_X87
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
14namespace v8 {
15namespace internal {
16
17// ----------------------------------------------------------------------------
18// Static IC stub generators.
19//
20
21#define __ ACCESS_MASM(masm)
22
23
24static void GenerateGlobalInstanceTypeCheck(MacroAssembler* masm, Register type,
25 Label* global_object) {
26 // Register usage:
27 // type: holds the receiver instance type on entry.
28 __ 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// Helper function used to load a property from a dictionary backing
38// storage. This function may fail to load a property even though it is
39// in the dictionary, so code at miss_label must always call a backup
40// 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// case. The generated code assumes that the receiver has slow
43// properties, is not a global object and does not have interceptors.
44static void GenerateDictionaryLoad(MacroAssembler* masm, Label* miss_label,
45 Register elements, Register name,
46 Register r0, Register r1, Register result) {
47 // Register use:
48 //
49 // elements - holds the property dictionary on entry and is unchanged.
50 //
51 // name - holds the name of the property on entry and is unchanged.
52 //
53 // Scratch registers:
54 //
55 // r0 - used for the index into the property dictionary
56 //
57 // r1 - used to hold the capacity of the property dictionary.
58 //
59 // result - holds the result on exit.
60
61 Label done;
62
63 // Probe the dictionary.
64 NameDictionaryLookupStub::GeneratePositiveLookup(masm, miss_label, &done,
65 elements, name, r0, r1);
66
67 // If probing finds an entry in the dictionary, r0 contains the
68 // index into the dictionary. Check that the value is a normal
69 // property.
70 __ bind(&done);
71 const int kElementsStartOffset =
72 NameDictionary::kHeaderSize +
73 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 // Get the value at the masked, scaled index.
80 const int kValueOffset = kElementsStartOffset + kPointerSize;
81 __ mov(result, Operand(elements, r0, times_4, kValueOffset - kHeapObjectTag));
82}
83
84
85// Helper function used to store a property to a dictionary backing
86// storage. This function may fail to store a property eventhough it
87// is in the dictionary, so code at miss_label must always call a
88// 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// that case. The generated code assumes that the receiver has slow
91// properties, is not a global object and does not have interceptors.
92static void GenerateDictionaryStore(MacroAssembler* masm, Label* miss_label,
93 Register elements, Register name,
94 Register value, Register r0, Register r1) {
95 // Register use:
96 //
97 // elements - holds the property dictionary on entry and is clobbered.
98 //
99 // name - holds the name of the property on entry and is unchanged.
100 //
101 // value - holds the value to store and is unchanged.
102 //
103 // r0 - used for index into the property dictionary and is clobbered.
104 //
105 // r1 - used to hold the capacity of the property dictionary and is clobbered.
106 Label done;
107
108
109 // Probe the dictionary.
110 NameDictionaryLookupStub::GeneratePositiveLookup(masm, miss_label, &done,
111 elements, name, r0, r1);
112
113 // If probing finds an entry in the dictionary, r0 contains the
114 // index into the dictionary. Check that the value is a normal
115 // property that is not read only.
116 __ bind(&done);
117 const int kElementsStartOffset =
118 NameDictionary::kHeaderSize +
119 NameDictionary::kElementsStartIndex * kPointerSize;
120 const int kDetailsOffset = kElementsStartOffset + 2 * kPointerSize;
121 const int kTypeAndReadOnlyMask =
122 (PropertyDetails::TypeField::kMask |
123 PropertyDetails::AttributesField::encode(READ_ONLY))
124 << kSmiTagSize;
125 __ test(Operand(elements, r0, times_4, kDetailsOffset - kHeapObjectTag),
126 Immediate(kTypeAndReadOnlyMask));
127 __ j(not_zero, miss_label);
128
129 // Store the value at the masked, scaled index.
130 const int kValueOffset = kElementsStartOffset + kPointerSize;
131 __ lea(r0, Operand(elements, r0, times_4, kValueOffset - kHeapObjectTag));
132 __ mov(Operand(r0, 0), value);
133
134 // Update write barrier. Make sure not to clobber the value.
135 __ mov(r1, value);
136 __ RecordWrite(elements, r0, r1, kDontSaveFPRegs);
137}
138
139
140// Checks the receiver for special cases (value type, slow case bits).
141// Falls through for regular JS object.
142static void GenerateKeyedLoadReceiverCheck(MacroAssembler* masm,
143 Register receiver, Register map,
144 int interceptor_bit, Label* slow) {
145 // Register use:
146 // receiver - holds the receiver and is unchanged.
147 // 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 // Check that the object is some kind of JS object EXCEPT JS Value type.
161 // In the case that the object is a value-wrapper object,
162 // 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.
173static void GenerateFastArrayLoad(MacroAssembler* masm, Register receiver,
174 Register key, Register scratch,
175 Register result, Label* not_fast_array,
176 Label* out_of_range) {
177 // Register use:
178 // receiver - holds the receiver and is unchanged.
179 // key - holds the key and is unchanged (must be a smi).
180 // Scratch registers:
181 // scratch - used to hold elements of the receiver and the loaded value.
182 // result - holds the result on exit if the load succeeds and
183 // we fall through.
184
185 __ mov(scratch, FieldOperand(receiver, JSObject::kElementsOffset));
186 if (not_fast_array != NULL) {
187 // Check that the object is in fast mode and writable.
188 __ CheckMap(scratch,
189 masm->isolate()->factory()->fixed_array_map(),
190 not_fast_array,
191 DONT_DO_SMI_CHECK);
192 } else {
193 __ AssertFastElements(scratch);
194 }
195 // Check that the key (index) is within bounds.
196 __ cmp(key, FieldOperand(scratch, FixedArray::kLengthOffset));
197 __ j(above_equal, out_of_range);
198 // Fast case: Do the load.
199 STATIC_ASSERT((kPointerSize == 4) && (kSmiTagSize == 1) && (kSmiTag == 0));
200 __ mov(scratch, FieldOperand(scratch, key, times_2, FixedArray::kHeaderSize));
201 __ cmp(scratch, Immediate(masm->isolate()->factory()->the_hole_value()));
202 // In case the loaded value is the_hole we have to consult GetProperty
203 // to ensure the prototype chain is searched.
204 __ j(equal, out_of_range);
205 if (!result.is(scratch)) {
206 __ mov(result, scratch);
207 }
208}
209
210
211// Checks whether a key is an array index string or a unique name.
212// Falls through if the key is a unique name.
213static void GenerateKeyNameCheck(MacroAssembler* masm, Register key,
214 Register map, Register hash,
215 Label* index_string, Label* not_unique) {
216 // Register use:
217 // key - holds the key and is unchanged. Assumed to be non-smi.
218 // Scratch registers:
219 // map - used to hold the map of the key.
220 // hash - used to hold the hash of the key.
221 Label unique;
222 __ CmpObjectType(key, LAST_UNIQUE_NAME_TYPE, map);
223 __ j(above, not_unique);
224 STATIC_ASSERT(LAST_UNIQUE_NAME_TYPE == FIRST_NONSTRING_TYPE);
225 __ j(equal, &unique);
226
227 // Is the string an array index, with cached numeric value?
228 __ mov(hash, FieldOperand(key, Name::kHashFieldOffset));
229 __ test(hash, Immediate(Name::kContainsCachedArrayIndexMask));
230 __ j(zero, index_string);
231
232 // Is the string internalized? We already know it's a string so a single
233 // bit test is enough.
234 STATIC_ASSERT(kNotInternalizedTag != 0);
235 __ test_b(FieldOperand(map, Map::kInstanceTypeOffset),
236 kIsNotInternalizedMask);
237 __ j(not_zero, not_unique);
238
239 __ bind(&unique);
240}
241
242
243static Operand GenerateMappedArgumentsLookup(
244 MacroAssembler* masm, Register object, Register key, Register scratch1,
245 Register scratch2, Label* unmapped_case, Label* slow_case) {
246 Heap* heap = masm->isolate()->heap();
247 Factory* factory = masm->isolate()->factory();
248
249 // Check that the receiver is a JSObject. Because of the elements
250 // map check later, we do not need to check for interceptors or
251 // whether it requires access checks.
252 __ JumpIfSmi(object, slow_case);
253 // Check that the object is some kind of JSObject.
254 __ CmpObjectType(object, FIRST_JS_RECEIVER_TYPE, scratch1);
255 __ j(below, slow_case);
256
257 // Check that the key is a positive smi.
258 __ test(key, Immediate(0x80000001));
259 __ j(not_zero, slow_case);
260
261 // Load the elements into scratch1 and check its map.
262 Handle<Map> arguments_map(heap->sloppy_arguments_elements_map());
263 __ mov(scratch1, FieldOperand(object, JSObject::kElementsOffset));
264 __ CheckMap(scratch1, arguments_map, slow_case, DONT_DO_SMI_CHECK);
265
266 // Check if element is in the range of mapped arguments. If not, jump
267 // to the unmapped lookup with the parameter map in scratch1.
268 __ mov(scratch2, FieldOperand(scratch1, FixedArray::kLengthOffset));
269 __ sub(scratch2, Immediate(Smi::FromInt(2)));
270 __ cmp(key, scratch2);
271 __ j(above_equal, unmapped_case);
272
273 // Load element index and check whether it is the hole.
274 const int kHeaderSize = FixedArray::kHeaderSize + 2 * kPointerSize;
275 __ mov(scratch2,
276 FieldOperand(scratch1, key, times_half_pointer_size, kHeaderSize));
277 __ cmp(scratch2, factory->the_hole_value());
278 __ j(equal, unmapped_case);
279
280 // Load value from context and return it. We can reuse scratch1 because
281 // we do not jump to the unmapped lookup (which requires the parameter
282 // map in scratch1).
283 const int kContextOffset = FixedArray::kHeaderSize;
284 __ mov(scratch1, FieldOperand(scratch1, kContextOffset));
285 return FieldOperand(scratch1, scratch2, times_half_pointer_size,
286 Context::kHeaderSize);
287}
288
289
290static Operand GenerateUnmappedArgumentsLookup(MacroAssembler* masm,
291 Register key,
292 Register parameter_map,
293 Register scratch,
294 Label* slow_case) {
295 // Element is in arguments backing store, which is referenced by the
296 // second element of the parameter_map.
297 const int kBackingStoreOffset = FixedArray::kHeaderSize + kPointerSize;
298 Register backing_store = parameter_map;
299 __ mov(backing_store, FieldOperand(parameter_map, kBackingStoreOffset));
300 Handle<Map> fixed_array_map(masm->isolate()->heap()->fixed_array_map());
301 __ CheckMap(backing_store, fixed_array_map, slow_case, DONT_DO_SMI_CHECK);
302 __ mov(scratch, FieldOperand(backing_store, FixedArray::kLengthOffset));
303 __ cmp(key, scratch);
304 __ j(greater_equal, slow_case);
305 return FieldOperand(backing_store, key, times_half_pointer_size,
306 FixedArray::kHeaderSize);
307}
308
309
310void KeyedLoadIC::GenerateGeneric(MacroAssembler* masm) {
311 // The return address is on the stack.
312 Label slow, check_name, index_smi, index_name, property_array_property;
313 Label probe_dictionary, check_number_dictionary;
314
315 Register receiver = LoadDescriptor::ReceiverRegister();
316 Register key = LoadDescriptor::NameRegister();
317 DCHECK(receiver.is(edx));
318 DCHECK(key.is(ecx));
319
320 // Check that the key is a smi.
321 __ JumpIfNotSmi(key, &check_name);
322 __ bind(&index_smi);
323 // Now the key is known to be a smi. This place is also jumped to from
324 // where a numeric string is converted to a smi.
325
326 GenerateKeyedLoadReceiverCheck(masm, receiver, eax,
327 Map::kHasIndexedInterceptor, &slow);
328
329 // Check the receiver's map to see if it has fast elements.
330 __ CheckFastElements(eax, &check_number_dictionary);
331
332 GenerateFastArrayLoad(masm, receiver, key, eax, eax, NULL, &slow);
333 Isolate* isolate = masm->isolate();
334 Counters* counters = isolate->counters();
335 __ IncrementCounter(counters->keyed_load_generic_smi(), 1);
336 __ ret(0);
337
338 __ bind(&check_number_dictionary);
339 __ mov(ebx, key);
340 __ SmiUntag(ebx);
341 __ mov(eax, FieldOperand(receiver, JSObject::kElementsOffset));
342
343 // Check whether the elements is a number dictionary.
344 // ebx: untagged index
345 // eax: elements
346 __ CheckMap(eax, isolate->factory()->hash_table_map(), &slow,
347 DONT_DO_SMI_CHECK);
348 Label slow_pop_receiver;
349 // Push receiver on the stack to free up a register for the dictionary
350 // probing.
351 __ push(receiver);
352 __ LoadFromNumberDictionary(&slow_pop_receiver, eax, key, ebx, edx, edi, eax);
353 // Pop receiver before returning.
354 __ pop(receiver);
355 __ ret(0);
356
357 __ bind(&slow_pop_receiver);
358 // Pop the receiver from the stack and jump to runtime.
359 __ pop(receiver);
360
361 __ bind(&slow);
362 // Slow case: jump to runtime.
363 __ IncrementCounter(counters->keyed_load_generic_slow(), 1);
364 GenerateRuntimeGetProperty(masm);
365
366 __ bind(&check_name);
367 GenerateKeyNameCheck(masm, key, eax, ebx, &index_name, &slow);
368
369 GenerateKeyedLoadReceiverCheck(masm, receiver, eax, Map::kHasNamedInterceptor,
370 &slow);
371
372 // If the receiver is a fast-case object, check the keyed lookup
373 // cache. Otherwise probe the dictionary.
374 __ mov(ebx, FieldOperand(receiver, JSObject::kPropertiesOffset));
375 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
376 Immediate(isolate->factory()->hash_table_map()));
377 __ j(equal, &probe_dictionary);
378
379 // The receiver's map is still in eax, compute the keyed lookup cache hash
380 // based on 32 bits of the map pointer and the string hash.
381 if (FLAG_debug_code) {
382 __ cmp(eax, FieldOperand(receiver, HeapObject::kMapOffset));
383 __ Check(equal, kMapIsNoLongerInEax);
384 }
385 __ mov(ebx, eax); // Keep the map around for later.
386 __ shr(eax, KeyedLookupCache::kMapHashShift);
387 __ mov(edi, FieldOperand(key, String::kHashFieldOffset));
388 __ shr(edi, String::kHashShift);
389 __ xor_(eax, edi);
390 __ and_(eax, KeyedLookupCache::kCapacityMask & KeyedLookupCache::kHashMask);
391
392 // Load the key (consisting of map and internalized string) from the cache and
393 // check for match.
394 Label load_in_object_property;
395 static const int kEntriesPerBucket = KeyedLookupCache::kEntriesPerBucket;
396 Label hit_on_nth_entry[kEntriesPerBucket];
397 ExternalReference cache_keys =
398 ExternalReference::keyed_lookup_cache_keys(masm->isolate());
399
400 for (int i = 0; i < kEntriesPerBucket - 1; i++) {
401 Label try_next_entry;
402 __ mov(edi, eax);
403 __ shl(edi, kPointerSizeLog2 + 1);
404 if (i != 0) {
405 __ add(edi, Immediate(kPointerSize * i * 2));
406 }
407 __ cmp(ebx, Operand::StaticArray(edi, times_1, cache_keys));
408 __ j(not_equal, &try_next_entry);
409 __ add(edi, Immediate(kPointerSize));
410 __ cmp(key, Operand::StaticArray(edi, times_1, cache_keys));
411 __ j(equal, &hit_on_nth_entry[i]);
412 __ bind(&try_next_entry);
413 }
414
415 __ lea(edi, Operand(eax, 1));
416 __ shl(edi, kPointerSizeLog2 + 1);
417 __ add(edi, Immediate(kPointerSize * (kEntriesPerBucket - 1) * 2));
418 __ cmp(ebx, Operand::StaticArray(edi, times_1, cache_keys));
419 __ j(not_equal, &slow);
420 __ add(edi, Immediate(kPointerSize));
421 __ cmp(key, Operand::StaticArray(edi, times_1, cache_keys));
422 __ j(not_equal, &slow);
423
424 // Get field offset.
425 // ebx : receiver's map
426 // eax : lookup cache index
427 ExternalReference cache_field_offsets =
428 ExternalReference::keyed_lookup_cache_field_offsets(masm->isolate());
429
430 // Hit on nth entry.
431 for (int i = kEntriesPerBucket - 1; i >= 0; i--) {
432 __ bind(&hit_on_nth_entry[i]);
433 if (i != 0) {
434 __ add(eax, Immediate(i));
435 }
436 __ mov(edi,
437 Operand::StaticArray(eax, times_pointer_size, cache_field_offsets));
438 __ movzx_b(eax, FieldOperand(ebx, Map::kInObjectPropertiesOffset));
439 __ sub(edi, eax);
440 __ j(above_equal, &property_array_property);
441 if (i != 0) {
442 __ jmp(&load_in_object_property);
443 }
444 }
445
446 // Load in-object property.
447 __ bind(&load_in_object_property);
448 __ movzx_b(eax, FieldOperand(ebx, Map::kInstanceSizeOffset));
449 __ add(eax, edi);
450 __ mov(eax, FieldOperand(receiver, eax, times_pointer_size, 0));
451 __ IncrementCounter(counters->keyed_load_generic_lookup_cache(), 1);
452 __ ret(0);
453
454 // Load property array property.
455 __ bind(&property_array_property);
456 __ mov(eax, FieldOperand(receiver, JSObject::kPropertiesOffset));
457 __ mov(eax,
458 FieldOperand(eax, edi, times_pointer_size, FixedArray::kHeaderSize));
459 __ IncrementCounter(counters->keyed_load_generic_lookup_cache(), 1);
460 __ ret(0);
461
462 // Do a quick inline probe of the receiver's dictionary, if it
463 // exists.
464 __ bind(&probe_dictionary);
465
466 __ mov(eax, FieldOperand(receiver, JSObject::kMapOffset));
467 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
468 GenerateGlobalInstanceTypeCheck(masm, eax, &slow);
469
470 GenerateDictionaryLoad(masm, &slow, ebx, key, eax, edi, eax);
471 __ IncrementCounter(counters->keyed_load_generic_symbol(), 1);
472 __ ret(0);
473
474 __ bind(&index_name);
475 __ IndexFromHash(ebx, key);
476 // Now jump to the place where smi keys are handled.
477 __ jmp(&index_smi);
478}
479
480
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481void KeyedStoreIC::GenerateSloppyArguments(MacroAssembler* masm) {
482 // Return address is on the stack.
483 Label slow, notin;
484 Register receiver = StoreDescriptor::ReceiverRegister();
485 Register name = StoreDescriptor::NameRegister();
486 Register value = StoreDescriptor::ValueRegister();
487 DCHECK(receiver.is(edx));
488 DCHECK(name.is(ecx));
489 DCHECK(value.is(eax));
490
491 Operand mapped_location = GenerateMappedArgumentsLookup(
492 masm, receiver, name, ebx, edi, &notin, &slow);
493 __ mov(mapped_location, value);
494 __ lea(ecx, mapped_location);
495 __ mov(edx, value);
496 __ RecordWrite(ebx, ecx, edx, kDontSaveFPRegs);
497 __ Ret();
498 __ bind(&notin);
499 // The unmapped lookup expects that the parameter map is in ebx.
500 Operand unmapped_location =
501 GenerateUnmappedArgumentsLookup(masm, name, ebx, edi, &slow);
502 __ mov(unmapped_location, value);
503 __ lea(edi, unmapped_location);
504 __ mov(edx, value);
505 __ RecordWrite(ebx, edi, edx, kDontSaveFPRegs);
506 __ Ret();
507 __ bind(&slow);
508 GenerateMiss(masm);
509}
510
511
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400512static void KeyedStoreGenerateMegamorphicHelper(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000513 MacroAssembler* masm, Label* fast_object, Label* fast_double, Label* slow,
514 KeyedStoreCheckMap check_map, KeyedStoreIncrementLength increment_length) {
515 Label transition_smi_elements;
516 Label finish_object_store, non_double_value, transition_double_elements;
517 Label fast_double_without_map_check;
518 Register receiver = StoreDescriptor::ReceiverRegister();
519 Register key = StoreDescriptor::NameRegister();
520 Register value = StoreDescriptor::ValueRegister();
521 DCHECK(receiver.is(edx));
522 DCHECK(key.is(ecx));
523 DCHECK(value.is(eax));
524 // key is a smi.
525 // ebx: FixedArray receiver->elements
526 // edi: receiver map
527 // Fast case: Do the store, could either Object or double.
528 __ bind(fast_object);
529 if (check_map == kCheckMap) {
530 __ mov(edi, FieldOperand(ebx, HeapObject::kMapOffset));
531 __ cmp(edi, masm->isolate()->factory()->fixed_array_map());
532 __ j(not_equal, fast_double);
533 }
534
535 // HOLECHECK: guards "A[i] = V"
536 // We have to go to the runtime if the current value is the hole because
537 // there may be a callback on the element
538 Label holecheck_passed1;
539 __ cmp(FixedArrayElementOperand(ebx, key),
540 masm->isolate()->factory()->the_hole_value());
541 __ j(not_equal, &holecheck_passed1);
542 __ JumpIfDictionaryInPrototypeChain(receiver, ebx, edi, slow);
543 __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
544
545 __ bind(&holecheck_passed1);
546
547 // Smi stores don't require further checks.
548 Label non_smi_value;
549 __ JumpIfNotSmi(value, &non_smi_value);
550 if (increment_length == kIncrementLength) {
551 // Add 1 to receiver->length.
552 __ add(FieldOperand(receiver, JSArray::kLengthOffset),
553 Immediate(Smi::FromInt(1)));
554 }
555 // It's irrelevant whether array is smi-only or not when writing a smi.
556 __ mov(FixedArrayElementOperand(ebx, key), value);
557 __ ret(0);
558
559 __ bind(&non_smi_value);
560 // Escape to elements kind transition case.
561 __ mov(edi, FieldOperand(receiver, HeapObject::kMapOffset));
562 __ CheckFastObjectElements(edi, &transition_smi_elements);
563
564 // Fast elements array, store the value to the elements backing store.
565 __ bind(&finish_object_store);
566 if (increment_length == kIncrementLength) {
567 // Add 1 to receiver->length.
568 __ add(FieldOperand(receiver, JSArray::kLengthOffset),
569 Immediate(Smi::FromInt(1)));
570 }
571 __ mov(FixedArrayElementOperand(ebx, key), value);
572 // Update write barrier for the elements array address.
573 __ mov(edx, value); // Preserve the value which is returned.
574 __ RecordWriteArray(ebx, edx, key, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
575 OMIT_SMI_CHECK);
576 __ ret(0);
577
578 __ bind(fast_double);
579 if (check_map == kCheckMap) {
580 // Check for fast double array case. If this fails, call through to the
581 // runtime.
582 __ cmp(edi, masm->isolate()->factory()->fixed_double_array_map());
583 __ j(not_equal, slow);
584 // If the value is a number, store it as a double in the FastDoubleElements
585 // array.
586 }
587
588 // HOLECHECK: guards "A[i] double hole?"
589 // We have to see if the double version of the hole is present. If so
590 // go to the runtime.
591 uint32_t offset = FixedDoubleArray::kHeaderSize + sizeof(kHoleNanLower32);
592 __ cmp(FieldOperand(ebx, key, times_4, offset), Immediate(kHoleNanUpper32));
593 __ j(not_equal, &fast_double_without_map_check);
594 __ JumpIfDictionaryInPrototypeChain(receiver, ebx, edi, slow);
595 __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
596
597 __ bind(&fast_double_without_map_check);
598 __ StoreNumberToDoubleElements(value, ebx, key, edi,
599 &transition_double_elements, false);
600 if (increment_length == kIncrementLength) {
601 // Add 1 to receiver->length.
602 __ add(FieldOperand(receiver, JSArray::kLengthOffset),
603 Immediate(Smi::FromInt(1)));
604 }
605 __ ret(0);
606
607 __ bind(&transition_smi_elements);
608 __ mov(ebx, FieldOperand(receiver, HeapObject::kMapOffset));
609
610 // Transition the array appropriately depending on the value type.
611 __ CheckMap(value, masm->isolate()->factory()->heap_number_map(),
612 &non_double_value, DONT_DO_SMI_CHECK);
613
614 // Value is a double. Transition FAST_SMI_ELEMENTS -> FAST_DOUBLE_ELEMENTS
615 // and complete the store.
616 __ LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS,
617 FAST_DOUBLE_ELEMENTS, ebx, edi, slow);
618 AllocationSiteMode mode =
619 AllocationSite::GetMode(FAST_SMI_ELEMENTS, FAST_DOUBLE_ELEMENTS);
620 ElementsTransitionGenerator::GenerateSmiToDouble(masm, receiver, key, value,
621 ebx, mode, slow);
622 __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
623 __ jmp(&fast_double_without_map_check);
624
625 __ bind(&non_double_value);
626 // Value is not a double, FAST_SMI_ELEMENTS -> FAST_ELEMENTS
627 __ LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS, FAST_ELEMENTS, ebx,
628 edi, slow);
629 mode = AllocationSite::GetMode(FAST_SMI_ELEMENTS, FAST_ELEMENTS);
630 ElementsTransitionGenerator::GenerateMapChangeElementsTransition(
631 masm, receiver, key, value, ebx, mode, slow);
632 __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
633 __ jmp(&finish_object_store);
634
635 __ bind(&transition_double_elements);
636 // Elements are FAST_DOUBLE_ELEMENTS, but value is an Object that's not a
637 // HeapNumber. Make sure that the receiver is a Array with FAST_ELEMENTS and
638 // transition array from FAST_DOUBLE_ELEMENTS to FAST_ELEMENTS
639 __ mov(ebx, FieldOperand(receiver, HeapObject::kMapOffset));
640 __ LoadTransitionedArrayMapConditional(FAST_DOUBLE_ELEMENTS, FAST_ELEMENTS,
641 ebx, edi, slow);
642 mode = AllocationSite::GetMode(FAST_DOUBLE_ELEMENTS, FAST_ELEMENTS);
643 ElementsTransitionGenerator::GenerateDoubleToObject(masm, receiver, key,
644 value, ebx, mode, slow);
645 __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
646 __ jmp(&finish_object_store);
647}
648
649
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400650void KeyedStoreIC::GenerateMegamorphic(MacroAssembler* masm,
651 StrictMode strict_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000652 // Return address is on the stack.
653 Label slow, fast_object, fast_object_grow;
654 Label fast_double, fast_double_grow;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400655 Label array, extra, check_if_double_array, maybe_name_key, miss;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000656 Register receiver = StoreDescriptor::ReceiverRegister();
657 Register key = StoreDescriptor::NameRegister();
658 DCHECK(receiver.is(edx));
659 DCHECK(key.is(ecx));
660
661 // Check that the object isn't a smi.
662 __ JumpIfSmi(receiver, &slow);
663 // Get the map from the receiver.
664 __ mov(edi, FieldOperand(receiver, HeapObject::kMapOffset));
665 // Check that the receiver does not require access checks and is not observed.
666 // The generic stub does not perform map checks or handle observed objects.
667 __ test_b(FieldOperand(edi, Map::kBitFieldOffset),
668 1 << Map::kIsAccessCheckNeeded | 1 << Map::kIsObserved);
669 __ j(not_zero, &slow);
670 // Check that the key is a smi.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400671 __ JumpIfNotSmi(key, &maybe_name_key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000672 __ CmpInstanceType(edi, JS_ARRAY_TYPE);
673 __ j(equal, &array);
674 // Check that the object is some kind of JSObject.
675 __ CmpInstanceType(edi, FIRST_JS_OBJECT_TYPE);
676 __ j(below, &slow);
677
678 // Object case: Check key against length in the elements array.
679 // Key is a smi.
680 // edi: receiver map
681 __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
682 // Check array bounds. Both the key and the length of FixedArray are smis.
683 __ cmp(key, FieldOperand(ebx, FixedArray::kLengthOffset));
684 __ j(below, &fast_object);
685
686 // Slow case: call runtime.
687 __ bind(&slow);
688 PropertyICCompiler::GenerateRuntimeSetProperty(masm, strict_mode);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400689 // Never returns to here.
690
691 __ bind(&maybe_name_key);
692 __ mov(ebx, FieldOperand(key, HeapObject::kMapOffset));
693 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
694 __ JumpIfNotUniqueNameInstanceType(ebx, &slow);
695 Code::Flags flags = Code::RemoveTypeAndHolderFromFlags(
696 Code::ComputeHandlerFlags(Code::STORE_IC));
697 masm->isolate()->stub_cache()->GenerateProbe(
698 masm, Code::STORE_IC, flags, false, receiver, key, ebx, no_reg);
699 // Cache miss.
700 __ jmp(&miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000701
702 // Extra capacity case: Check if there is extra capacity to
703 // perform the store and update the length. Used for adding one
704 // element to the array by writing to array[array.length].
705 __ bind(&extra);
706 // receiver is a JSArray.
707 // key is a smi.
708 // ebx: receiver->elements, a FixedArray
709 // edi: receiver map
710 // flags: compare (key, receiver.length())
711 // do not leave holes in the array:
712 __ j(not_equal, &slow);
713 __ cmp(key, FieldOperand(ebx, FixedArray::kLengthOffset));
714 __ j(above_equal, &slow);
715 __ mov(edi, FieldOperand(ebx, HeapObject::kMapOffset));
716 __ cmp(edi, masm->isolate()->factory()->fixed_array_map());
717 __ j(not_equal, &check_if_double_array);
718 __ jmp(&fast_object_grow);
719
720 __ bind(&check_if_double_array);
721 __ cmp(edi, masm->isolate()->factory()->fixed_double_array_map());
722 __ j(not_equal, &slow);
723 __ jmp(&fast_double_grow);
724
725 // Array case: Get the length and the elements array from the JS
726 // array. Check that the array is in fast mode (and writable); if it
727 // is the length is always a smi.
728 __ bind(&array);
729 // receiver is a JSArray.
730 // key is a smi.
731 // edi: receiver map
732 __ mov(ebx, FieldOperand(receiver, JSObject::kElementsOffset));
733
734 // Check the key against the length in the array and fall through to the
735 // common store code.
736 __ cmp(key, FieldOperand(receiver, JSArray::kLengthOffset)); // Compare smis.
737 __ j(above_equal, &extra);
738
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400739 KeyedStoreGenerateMegamorphicHelper(masm, &fast_object, &fast_double, &slow,
740 kCheckMap, kDontIncrementLength);
741 KeyedStoreGenerateMegamorphicHelper(masm, &fast_object_grow,
742 &fast_double_grow, &slow, kDontCheckMap,
743 kIncrementLength);
744
745 __ bind(&miss);
746 GenerateMiss(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000747}
748
749
750void LoadIC::GenerateNormal(MacroAssembler* masm) {
751 Register dictionary = eax;
752 DCHECK(!dictionary.is(LoadDescriptor::ReceiverRegister()));
753 DCHECK(!dictionary.is(LoadDescriptor::NameRegister()));
754
755 Label slow;
756
757 __ mov(dictionary, FieldOperand(LoadDescriptor::ReceiverRegister(),
758 JSObject::kPropertiesOffset));
759 GenerateDictionaryLoad(masm, &slow, dictionary,
760 LoadDescriptor::NameRegister(), edi, ebx, eax);
761 __ ret(0);
762
763 // Dictionary load failed, go slow (but don't miss).
764 __ bind(&slow);
765 GenerateRuntimeGetProperty(masm);
766}
767
768
769static void LoadIC_PushArgs(MacroAssembler* masm) {
770 Register receiver = LoadDescriptor::ReceiverRegister();
771 Register name = LoadDescriptor::NameRegister();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400772 if (FLAG_vector_ics) {
773 Register slot = VectorLoadICDescriptor::SlotRegister();
774 Register vector = VectorLoadICDescriptor::VectorRegister();
775 DCHECK(!edi.is(receiver) && !edi.is(name) && !edi.is(slot) &&
776 !edi.is(vector));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000777
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400778 __ pop(edi);
779 __ push(receiver);
780 __ push(name);
781 __ push(slot);
782 __ push(vector);
783 __ push(edi);
784 } else {
785 DCHECK(!ebx.is(receiver) && !ebx.is(name));
786
787 __ pop(ebx);
788 __ push(receiver);
789 __ push(name);
790 __ push(ebx);
791 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000792}
793
794
795void LoadIC::GenerateMiss(MacroAssembler* masm) {
796 // Return address is on the stack.
797 __ IncrementCounter(masm->isolate()->counters()->load_miss(), 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000798 LoadIC_PushArgs(masm);
799
800 // Perform tail call to the entry.
801 ExternalReference ref =
802 ExternalReference(IC_Utility(kLoadIC_Miss), masm->isolate());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400803 int arg_count = FLAG_vector_ics ? 4 : 2;
804 __ TailCallExternalReference(ref, arg_count, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000805}
806
807
808void LoadIC::GenerateRuntimeGetProperty(MacroAssembler* masm) {
809 // Return address is on the stack.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400810 Register receiver = LoadDescriptor::ReceiverRegister();
811 Register name = LoadDescriptor::NameRegister();
812 DCHECK(!ebx.is(receiver) && !ebx.is(name));
813
814 __ pop(ebx);
815 __ push(receiver);
816 __ push(name);
817 __ push(ebx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818
819 // Perform tail call to the entry.
820 __ TailCallRuntime(Runtime::kGetProperty, 2, 1);
821}
822
823
824void KeyedLoadIC::GenerateMiss(MacroAssembler* masm) {
825 // Return address is on the stack.
826 __ IncrementCounter(masm->isolate()->counters()->keyed_load_miss(), 1);
827
828 LoadIC_PushArgs(masm);
829
830 // Perform tail call to the entry.
831 ExternalReference ref =
832 ExternalReference(IC_Utility(kKeyedLoadIC_Miss), masm->isolate());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400833 int arg_count = FLAG_vector_ics ? 4 : 2;
834 __ TailCallExternalReference(ref, arg_count, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000835}
836
837
838void KeyedLoadIC::GenerateRuntimeGetProperty(MacroAssembler* masm) {
839 // Return address is on the stack.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400840 Register receiver = LoadDescriptor::ReceiverRegister();
841 Register name = LoadDescriptor::NameRegister();
842 DCHECK(!ebx.is(receiver) && !ebx.is(name));
843
844 __ pop(ebx);
845 __ push(receiver);
846 __ push(name);
847 __ push(ebx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000848
849 // Perform tail call to the entry.
850 __ TailCallRuntime(Runtime::kKeyedGetProperty, 2, 1);
851}
852
853
854void StoreIC::GenerateMegamorphic(MacroAssembler* masm) {
855 // Return address is on the stack.
856 Code::Flags flags = Code::RemoveTypeAndHolderFromFlags(
857 Code::ComputeHandlerFlags(Code::STORE_IC));
858 masm->isolate()->stub_cache()->GenerateProbe(
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400859 masm, Code::STORE_IC, flags, false, StoreDescriptor::ReceiverRegister(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000860 StoreDescriptor::NameRegister(), ebx, no_reg);
861
862 // Cache miss: Jump to runtime.
863 GenerateMiss(masm);
864}
865
866
867static void StoreIC_PushArgs(MacroAssembler* masm) {
868 Register receiver = StoreDescriptor::ReceiverRegister();
869 Register name = StoreDescriptor::NameRegister();
870 Register value = StoreDescriptor::ValueRegister();
871
872 DCHECK(!ebx.is(receiver) && !ebx.is(name) && !ebx.is(value));
873
874 __ pop(ebx);
875 __ push(receiver);
876 __ push(name);
877 __ push(value);
878 __ push(ebx);
879}
880
881
882void StoreIC::GenerateMiss(MacroAssembler* masm) {
883 // Return address is on the stack.
884 StoreIC_PushArgs(masm);
885
886 // Perform tail call to the entry.
887 ExternalReference ref =
888 ExternalReference(IC_Utility(kStoreIC_Miss), masm->isolate());
889 __ TailCallExternalReference(ref, 3, 1);
890}
891
892
893void StoreIC::GenerateNormal(MacroAssembler* masm) {
894 Label restore_miss;
895 Register receiver = StoreDescriptor::ReceiverRegister();
896 Register name = StoreDescriptor::NameRegister();
897 Register value = StoreDescriptor::ValueRegister();
898 Register dictionary = ebx;
899
900 __ mov(dictionary, FieldOperand(receiver, JSObject::kPropertiesOffset));
901
902 // A lot of registers are needed for storing to slow case
903 // objects. Push and restore receiver but rely on
904 // GenerateDictionaryStore preserving the value and name.
905 __ push(receiver);
906 GenerateDictionaryStore(masm, &restore_miss, dictionary, name, value,
907 receiver, edi);
908 __ Drop(1);
909 Counters* counters = masm->isolate()->counters();
910 __ IncrementCounter(counters->store_normal_hit(), 1);
911 __ ret(0);
912
913 __ bind(&restore_miss);
914 __ pop(receiver);
915 __ IncrementCounter(counters->store_normal_miss(), 1);
916 GenerateMiss(masm);
917}
918
919
920void KeyedStoreIC::GenerateMiss(MacroAssembler* masm) {
921 // Return address is on the stack.
922 StoreIC_PushArgs(masm);
923
924 // Do tail-call to runtime routine.
925 ExternalReference ref =
926 ExternalReference(IC_Utility(kKeyedStoreIC_Miss), masm->isolate());
927 __ TailCallExternalReference(ref, 3, 1);
928}
929
930
931#undef __
932
933
934Condition CompareIC::ComputeCondition(Token::Value op) {
935 switch (op) {
936 case Token::EQ_STRICT:
937 case Token::EQ:
938 return equal;
939 case Token::LT:
940 return less;
941 case Token::GT:
942 return greater;
943 case Token::LTE:
944 return less_equal;
945 case Token::GTE:
946 return greater_equal;
947 default:
948 UNREACHABLE();
949 return no_condition;
950 }
951}
952
953
954bool CompareIC::HasInlinedSmiCode(Address address) {
955 // The address of the instruction following the call.
956 Address test_instruction_address =
957 address + Assembler::kCallTargetAddressOffset;
958
959 // If the instruction following the call is not a test al, nothing
960 // was inlined.
961 return *test_instruction_address == Assembler::kTestAlByte;
962}
963
964
965void PatchInlinedSmiCode(Address address, InlinedSmiCheck check) {
966 // The address of the instruction following the call.
967 Address test_instruction_address =
968 address + Assembler::kCallTargetAddressOffset;
969
970 // If the instruction following the call is not a test al, nothing
971 // was inlined.
972 if (*test_instruction_address != Assembler::kTestAlByte) {
973 DCHECK(*test_instruction_address == Assembler::kNopByte);
974 return;
975 }
976
977 Address delta_address = test_instruction_address + 1;
978 // The delta to the start of the map check instruction and the
979 // condition code uses at the patched jump.
980 uint8_t delta = *reinterpret_cast<uint8_t*>(delta_address);
981 if (FLAG_trace_ic) {
982 PrintF("[ patching ic at %p, test=%p, delta=%d\n", address,
983 test_instruction_address, delta);
984 }
985
986 // Patch with a short conditional jump. Enabling means switching from a short
987 // jump-if-carry/not-carry to jump-if-zero/not-zero, whereas disabling is the
988 // reverse operation of that.
989 Address jmp_address = test_instruction_address - delta;
990 DCHECK((check == ENABLE_INLINED_SMI_CHECK)
991 ? (*jmp_address == Assembler::kJncShortOpcode ||
992 *jmp_address == Assembler::kJcShortOpcode)
993 : (*jmp_address == Assembler::kJnzShortOpcode ||
994 *jmp_address == Assembler::kJzShortOpcode));
995 Condition cc =
996 (check == ENABLE_INLINED_SMI_CHECK)
997 ? (*jmp_address == Assembler::kJncShortOpcode ? not_zero : zero)
998 : (*jmp_address == Assembler::kJnzShortOpcode ? not_carry : carry);
999 *jmp_address = static_cast<byte>(Assembler::kJccShortPrefix | cc);
1000}
1001}
1002} // namespace v8::internal
1003
1004#endif // V8_TARGET_ARCH_X87