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