Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #if V8_TARGET_ARCH_X64 |
| 6 | |
| 7 | #include "src/codegen.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8 | #include "src/ic/ic.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/ic/stub-cache.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10 | #include "src/interface-descriptors.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
| 15 | #define __ ACCESS_MASM(masm) |
| 16 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17 | static void ProbeTable(Isolate* isolate, MacroAssembler* masm, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 18 | Code::Flags flags, StubCache::Table table, |
| 19 | Register receiver, Register name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | // The offset is scaled by 4, based on |
| 21 | // kCacheIndexShift, which is two bits |
| 22 | Register offset) { |
| 23 | // We need to scale up the pointer by 2 when the offset is scaled by less |
| 24 | // than the pointer size. |
| 25 | DCHECK(kPointerSize == kInt64Size |
| 26 | ? kPointerSizeLog2 == StubCache::kCacheIndexShift + 1 |
| 27 | : kPointerSizeLog2 == StubCache::kCacheIndexShift); |
| 28 | ScaleFactor scale_factor = kPointerSize == kInt64Size ? times_2 : times_1; |
| 29 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 30 | DCHECK_EQ(3u * kPointerSize, sizeof(StubCache::Entry)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | // The offset register holds the entry offset times four (due to masking |
| 32 | // and shifting optimizations). |
| 33 | ExternalReference key_offset(isolate->stub_cache()->key_reference(table)); |
| 34 | ExternalReference value_offset(isolate->stub_cache()->value_reference(table)); |
| 35 | Label miss; |
| 36 | |
| 37 | // Multiply by 3 because there are 3 fields per entry (name, code, map). |
| 38 | __ leap(offset, Operand(offset, offset, times_2, 0)); |
| 39 | |
| 40 | __ LoadAddress(kScratchRegister, key_offset); |
| 41 | |
| 42 | // Check that the key in the entry matches the name. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 43 | __ cmpp(name, Operand(kScratchRegister, offset, scale_factor, 0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | __ j(not_equal, &miss); |
| 45 | |
| 46 | // Get the map entry from the cache. |
| 47 | // Use key_offset + kPointerSize * 2, rather than loading map_offset. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 48 | DCHECK(isolate->stub_cache()->map_reference(table).address() - |
| 49 | isolate->stub_cache()->key_reference(table).address() == |
| 50 | kPointerSize * 2); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | __ movp(kScratchRegister, |
| 52 | Operand(kScratchRegister, offset, scale_factor, kPointerSize * 2)); |
| 53 | __ cmpp(kScratchRegister, FieldOperand(receiver, HeapObject::kMapOffset)); |
| 54 | __ j(not_equal, &miss); |
| 55 | |
| 56 | // Get the code entry from the cache. |
| 57 | __ LoadAddress(kScratchRegister, value_offset); |
| 58 | __ movp(kScratchRegister, Operand(kScratchRegister, offset, scale_factor, 0)); |
| 59 | |
| 60 | // Check that the flags match what we're looking for. |
| 61 | __ movl(offset, FieldOperand(kScratchRegister, Code::kFlagsOffset)); |
| 62 | __ andp(offset, Immediate(~Code::kFlagsNotUsedInLookup)); |
| 63 | __ cmpl(offset, Immediate(flags)); |
| 64 | __ j(not_equal, &miss); |
| 65 | |
| 66 | #ifdef DEBUG |
| 67 | if (FLAG_test_secondary_stub_cache && table == StubCache::kPrimary) { |
| 68 | __ jmp(&miss); |
| 69 | } else if (FLAG_test_primary_stub_cache && table == StubCache::kSecondary) { |
| 70 | __ jmp(&miss); |
| 71 | } |
| 72 | #endif |
| 73 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | // Jump to the first instruction in the code stub. |
| 75 | __ addp(kScratchRegister, Immediate(Code::kHeaderSize - kHeapObjectTag)); |
| 76 | __ jmp(kScratchRegister); |
| 77 | |
| 78 | __ bind(&miss); |
| 79 | } |
| 80 | |
| 81 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 82 | void StubCache::GenerateProbe(MacroAssembler* masm, Code::Kind ic_kind, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 83 | Code::Flags flags, Register receiver, |
| 84 | Register name, Register scratch, Register extra, |
| 85 | Register extra2, Register extra3) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | Isolate* isolate = masm->isolate(); |
| 87 | Label miss; |
| 88 | USE(extra); // The register extra is not used on the X64 platform. |
| 89 | USE(extra2); // The register extra2 is not used on the X64 platform. |
| 90 | USE(extra3); // The register extra2 is not used on the X64 platform. |
| 91 | // Make sure that code is valid. The multiplying code relies on the |
| 92 | // entry size being 3 * kPointerSize. |
| 93 | DCHECK(sizeof(Entry) == 3 * kPointerSize); |
| 94 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 95 | // Make sure that there are no register conflicts. |
| 96 | DCHECK(!scratch.is(receiver)); |
| 97 | DCHECK(!scratch.is(name)); |
| 98 | |
| 99 | // Check scratch register is valid, extra and extra2 are unused. |
| 100 | DCHECK(!scratch.is(no_reg)); |
| 101 | DCHECK(extra2.is(no_reg)); |
| 102 | DCHECK(extra3.is(no_reg)); |
| 103 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 104 | #ifdef DEBUG |
| 105 | // If vector-based ics are in use, ensure that scratch doesn't conflict with |
| 106 | // the vector and slot registers, which need to be preserved for a handler |
| 107 | // call or miss. |
| 108 | if (IC::ICUseVector(ic_kind)) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 109 | if (ic_kind == Code::LOAD_IC || ic_kind == Code::LOAD_GLOBAL_IC || |
| 110 | ic_kind == Code::KEYED_LOAD_IC) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 111 | Register vector = LoadWithVectorDescriptor::VectorRegister(); |
| 112 | Register slot = LoadDescriptor::SlotRegister(); |
| 113 | DCHECK(!AreAliased(vector, slot, scratch)); |
| 114 | } else { |
| 115 | DCHECK(ic_kind == Code::STORE_IC || ic_kind == Code::KEYED_STORE_IC); |
| 116 | Register vector = VectorStoreICDescriptor::VectorRegister(); |
| 117 | Register slot = VectorStoreICDescriptor::SlotRegister(); |
| 118 | DCHECK(!AreAliased(vector, slot, scratch)); |
| 119 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 120 | } |
| 121 | #endif |
| 122 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | Counters* counters = masm->isolate()->counters(); |
| 124 | __ IncrementCounter(counters->megamorphic_stub_cache_probes(), 1); |
| 125 | |
| 126 | // Check that the receiver isn't a smi. |
| 127 | __ JumpIfSmi(receiver, &miss); |
| 128 | |
| 129 | // Get the map of the receiver and compute the hash. |
| 130 | __ movl(scratch, FieldOperand(name, Name::kHashFieldOffset)); |
| 131 | // Use only the low 32 bits of the map pointer. |
| 132 | __ addl(scratch, FieldOperand(receiver, HeapObject::kMapOffset)); |
| 133 | __ xorp(scratch, Immediate(flags)); |
| 134 | // We mask out the last two bits because they are not part of the hash and |
| 135 | // they are always 01 for maps. Also in the two 'and' instructions below. |
| 136 | __ andp(scratch, Immediate((kPrimaryTableSize - 1) << kCacheIndexShift)); |
| 137 | |
| 138 | // Probe the primary table. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 139 | ProbeTable(isolate, masm, flags, kPrimary, receiver, name, scratch); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 140 | |
| 141 | // Primary miss: Compute hash for secondary probe. |
| 142 | __ movl(scratch, FieldOperand(name, Name::kHashFieldOffset)); |
| 143 | __ addl(scratch, FieldOperand(receiver, HeapObject::kMapOffset)); |
| 144 | __ xorp(scratch, Immediate(flags)); |
| 145 | __ andp(scratch, Immediate((kPrimaryTableSize - 1) << kCacheIndexShift)); |
| 146 | __ subl(scratch, name); |
| 147 | __ addl(scratch, Immediate(flags)); |
| 148 | __ andp(scratch, Immediate((kSecondaryTableSize - 1) << kCacheIndexShift)); |
| 149 | |
| 150 | // Probe the secondary table. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 151 | ProbeTable(isolate, masm, flags, kSecondary, receiver, name, scratch); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 152 | |
| 153 | // Cache miss: Fall-through and let caller handle the miss by |
| 154 | // entering the runtime system. |
| 155 | __ bind(&miss); |
| 156 | __ IncrementCounter(counters->megamorphic_stub_cache_misses(), 1); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | #undef __ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 161 | } // namespace internal |
| 162 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 163 | |
| 164 | #endif // V8_TARGET_ARCH_X64 |