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.
9 #include "src/codegen.h"
10 #include "src/ic/stub-cache.h"
15 #define __ ACCESS_MASM(masm)
18 static void ProbeTable(Isolate* isolate, MacroAssembler* masm,
19 Code::Flags flags, StubCache::Table table,
20 Register receiver, Register name,
21 // The offset is scaled by 4, based on
22 // kCacheIndexShift, which is two bits
24 // We need to scale up the pointer by 2 when the offset is scaled by less
25 // than the pointer size.
26 DCHECK(kPointerSize == kInt64Size
27 ? kPointerSizeLog2 == StubCache::kCacheIndexShift + 1
28 : kPointerSizeLog2 == StubCache::kCacheIndexShift);
29 ScaleFactor scale_factor = kPointerSize == kInt64Size ? times_2 : times_1;
31 DCHECK_EQ(3 * kPointerSize, sizeof(StubCache::Entry));
32 // The offset register holds the entry offset times four (due to masking
33 // and shifting optimizations).
34 ExternalReference key_offset(isolate->stub_cache()->key_reference(table));
35 ExternalReference value_offset(isolate->stub_cache()->value_reference(table));
38 // Multiply by 3 because there are 3 fields per entry (name, code, map).
39 __ leap(offset, Operand(offset, offset, times_2, 0));
41 __ LoadAddress(kScratchRegister, key_offset);
43 // Check that the key in the entry matches the name.
44 // Multiply entry offset by 16 to get the entry address. Since the
45 // offset register already holds the entry offset times four, multiply
47 __ cmpl(name, Operand(kScratchRegister, offset, scale_factor, 0));
48 __ j(not_equal, &miss);
50 // Get the map entry from the cache.
51 // Use key_offset + kPointerSize * 2, rather than loading map_offset.
52 __ movp(kScratchRegister,
53 Operand(kScratchRegister, offset, scale_factor, kPointerSize * 2));
54 __ cmpp(kScratchRegister, FieldOperand(receiver, HeapObject::kMapOffset));
55 __ j(not_equal, &miss);
57 // Get the code entry from the cache.
58 __ LoadAddress(kScratchRegister, value_offset);
59 __ movp(kScratchRegister, Operand(kScratchRegister, offset, scale_factor, 0));
61 // Check that the flags match what we're looking for.
62 __ movl(offset, FieldOperand(kScratchRegister, Code::kFlagsOffset));
63 __ andp(offset, Immediate(~Code::kFlagsNotUsedInLookup));
64 __ cmpl(offset, Immediate(flags));
65 __ j(not_equal, &miss);
68 if (FLAG_test_secondary_stub_cache && table == StubCache::kPrimary) {
70 } else if (FLAG_test_primary_stub_cache && table == StubCache::kSecondary) {
75 // Jump to the first instruction in the code stub.
76 __ addp(kScratchRegister, Immediate(Code::kHeaderSize - kHeapObjectTag));
77 __ jmp(kScratchRegister);
83 void StubCache::GenerateProbe(MacroAssembler* masm, Code::Flags flags,
84 Register receiver, Register name,
85 Register scratch, Register extra, Register extra2,
87 Isolate* isolate = masm->isolate();
89 USE(extra); // The register extra is not used on the X64 platform.
90 USE(extra2); // The register extra2 is not used on the X64 platform.
91 USE(extra3); // The register extra2 is not used on the X64 platform.
92 // Make sure that code is valid. The multiplying code relies on the
93 // entry size being 3 * kPointerSize.
94 DCHECK(sizeof(Entry) == 3 * kPointerSize);
96 // Make sure the flags do not name a specific type.
97 DCHECK(Code::ExtractTypeFromFlags(flags) == 0);
99 // Make sure that there are no register conflicts.
100 DCHECK(!scratch.is(receiver));
101 DCHECK(!scratch.is(name));
103 // Check scratch register is valid, extra and extra2 are unused.
104 DCHECK(!scratch.is(no_reg));
105 DCHECK(extra2.is(no_reg));
106 DCHECK(extra3.is(no_reg));
108 Counters* counters = masm->isolate()->counters();
109 __ IncrementCounter(counters->megamorphic_stub_cache_probes(), 1);
111 // Check that the receiver isn't a smi.
112 __ JumpIfSmi(receiver, &miss);
114 // Get the map of the receiver and compute the hash.
115 __ movl(scratch, FieldOperand(name, Name::kHashFieldOffset));
116 // Use only the low 32 bits of the map pointer.
117 __ addl(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
118 __ xorp(scratch, Immediate(flags));
119 // We mask out the last two bits because they are not part of the hash and
120 // they are always 01 for maps. Also in the two 'and' instructions below.
121 __ andp(scratch, Immediate((kPrimaryTableSize - 1) << kCacheIndexShift));
123 // Probe the primary table.
124 ProbeTable(isolate, masm, flags, kPrimary, receiver, name, scratch);
126 // Primary miss: Compute hash for secondary probe.
127 __ movl(scratch, FieldOperand(name, Name::kHashFieldOffset));
128 __ addl(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
129 __ xorp(scratch, Immediate(flags));
130 __ andp(scratch, Immediate((kPrimaryTableSize - 1) << kCacheIndexShift));
131 __ subl(scratch, name);
132 __ addl(scratch, Immediate(flags));
133 __ andp(scratch, Immediate((kSecondaryTableSize - 1) << kCacheIndexShift));
135 // Probe the secondary table.
136 ProbeTable(isolate, masm, flags, kSecondary, receiver, name, scratch);
138 // Cache miss: Fall-through and let caller handle the miss by
139 // entering the runtime system.
141 __ IncrementCounter(counters->megamorphic_stub_cache_misses(), 1);
147 } // namespace v8::internal
149 #endif // V8_TARGET_ARCH_X64