blob: fa0a0b32a0fc0bcb258c1151a238a56ec016162e [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
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#if V8_TARGET_ARCH_X64
6
7#include "src/codegen.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include "src/ic/ic.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/ic/stub-cache.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010#include "src/interface-descriptors.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011
12namespace v8 {
13namespace internal {
14
15#define __ ACCESS_MASM(masm)
16
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017static void ProbeTable(Isolate* isolate, MacroAssembler* masm,
Ben Murdochc5610432016-08-08 18:44:38 +010018 Code::Flags flags, StubCache::Table table,
19 Register receiver, Register name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020 // 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 Murdoch4a90d5f2016-03-22 12:00:34 +000030 DCHECK_EQ(3u * kPointerSize, sizeof(StubCache::Entry));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 // 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 Bernierd0a1eb72015-03-24 16:35:39 -040043 __ cmpp(name, Operand(kScratchRegister, offset, scale_factor, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044 __ 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 Bernierd0a1eb72015-03-24 16:35:39 -040048 DCHECK(isolate->stub_cache()->map_reference(table).address() -
49 isolate->stub_cache()->key_reference(table).address() ==
50 kPointerSize * 2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051 __ 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 Murdochb8a8cc12014-11-26 15:28:44 +000074 // 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 Bernierd0a1eb72015-03-24 16:35:39 -040082void StubCache::GenerateProbe(MacroAssembler* masm, Code::Kind ic_kind,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000083 Code::Flags flags, Register receiver,
84 Register name, Register scratch, Register extra,
85 Register extra2, Register extra3) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 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 Murdochb8a8cc12014-11-26 15:28:44 +000095 // 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 Bernierd0a1eb72015-03-24 16:35:39 -0400104#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 Murdoch61f157c2016-09-16 13:49:30 +0100109 if (ic_kind == Code::LOAD_IC || ic_kind == Code::LOAD_GLOBAL_IC ||
110 ic_kind == Code::KEYED_LOAD_IC) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111 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 Bernierd0a1eb72015-03-24 16:35:39 -0400120 }
121#endif
122
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 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 Murdochc5610432016-08-08 18:44:38 +0100139 ProbeTable(isolate, masm, flags, kPrimary, receiver, name, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140
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 Murdochc5610432016-08-08 18:44:38 +0100151 ProbeTable(isolate, masm, flags, kSecondary, receiver, name, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152
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 Murdoch4a90d5f2016-03-22 12:00:34 +0000161} // namespace internal
162} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163
164#endif // V8_TARGET_ARCH_X64