• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #if V8_TARGET_ARCH_X64
6 
7 #include "src/codegen.h"
8 #include "src/ic/ic.h"
9 #include "src/ic/stub-cache.h"
10 #include "src/interface-descriptors.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 #define __ ACCESS_MASM(masm)
16 
17 
ProbeTable(Isolate * isolate,MacroAssembler * masm,Code::Kind ic_kind,Code::Flags flags,StubCache::Table table,Register receiver,Register name,Register offset)18 static void ProbeTable(Isolate* isolate, MacroAssembler* masm,
19                        Code::Kind ic_kind, Code::Flags flags,
20                        StubCache::Table table, Register receiver, Register name,
21                        // The offset is scaled by 4, based on
22                        // kCacheIndexShift, which is two bits
23                        Register offset) {
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;
30 
31   DCHECK_EQ(3u * 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));
36   Label miss;
37 
38   // Multiply by 3 because there are 3 fields per entry (name, code, map).
39   __ leap(offset, Operand(offset, offset, times_2, 0));
40 
41   __ LoadAddress(kScratchRegister, key_offset);
42 
43   // Check that the key in the entry matches the name.
44   __ cmpp(name, Operand(kScratchRegister, offset, scale_factor, 0));
45   __ j(not_equal, &miss);
46 
47   // Get the map entry from the cache.
48   // Use key_offset + kPointerSize * 2, rather than loading map_offset.
49   DCHECK(isolate->stub_cache()->map_reference(table).address() -
50              isolate->stub_cache()->key_reference(table).address() ==
51          kPointerSize * 2);
52   __ movp(kScratchRegister,
53           Operand(kScratchRegister, offset, scale_factor, kPointerSize * 2));
54   __ cmpp(kScratchRegister, FieldOperand(receiver, HeapObject::kMapOffset));
55   __ j(not_equal, &miss);
56 
57   // Get the code entry from the cache.
58   __ LoadAddress(kScratchRegister, value_offset);
59   __ movp(kScratchRegister, Operand(kScratchRegister, offset, scale_factor, 0));
60 
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);
66 
67 #ifdef DEBUG
68   if (FLAG_test_secondary_stub_cache && table == StubCache::kPrimary) {
69     __ jmp(&miss);
70   } else if (FLAG_test_primary_stub_cache && table == StubCache::kSecondary) {
71     __ jmp(&miss);
72   }
73 #endif
74 
75   // Jump to the first instruction in the code stub.
76   __ addp(kScratchRegister, Immediate(Code::kHeaderSize - kHeapObjectTag));
77   __ jmp(kScratchRegister);
78 
79   __ bind(&miss);
80 }
81 
82 
GenerateProbe(MacroAssembler * masm,Code::Kind ic_kind,Code::Flags flags,Register receiver,Register name,Register scratch,Register extra,Register extra2,Register extra3)83 void StubCache::GenerateProbe(MacroAssembler* masm, Code::Kind ic_kind,
84                               Code::Flags flags, Register receiver,
85                               Register name, Register scratch, Register extra,
86                               Register extra2, Register extra3) {
87   Isolate* isolate = masm->isolate();
88   Label miss;
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);
95 
96   // Make sure the flags do not name a specific type.
97   DCHECK(Code::ExtractTypeFromFlags(flags) == 0);
98 
99   // Make sure that there are no register conflicts.
100   DCHECK(!scratch.is(receiver));
101   DCHECK(!scratch.is(name));
102 
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));
107 
108 #ifdef DEBUG
109   // If vector-based ics are in use, ensure that scratch doesn't conflict with
110   // the vector and slot registers, which need to be preserved for a handler
111   // call or miss.
112   if (IC::ICUseVector(ic_kind)) {
113     if (ic_kind == Code::LOAD_IC || ic_kind == Code::KEYED_LOAD_IC) {
114       Register vector = LoadWithVectorDescriptor::VectorRegister();
115       Register slot = LoadDescriptor::SlotRegister();
116       DCHECK(!AreAliased(vector, slot, scratch));
117     } else {
118       DCHECK(ic_kind == Code::STORE_IC || ic_kind == Code::KEYED_STORE_IC);
119       Register vector = VectorStoreICDescriptor::VectorRegister();
120       Register slot = VectorStoreICDescriptor::SlotRegister();
121       DCHECK(!AreAliased(vector, slot, scratch));
122     }
123   }
124 #endif
125 
126   Counters* counters = masm->isolate()->counters();
127   __ IncrementCounter(counters->megamorphic_stub_cache_probes(), 1);
128 
129   // Check that the receiver isn't a smi.
130   __ JumpIfSmi(receiver, &miss);
131 
132   // Get the map of the receiver and compute the hash.
133   __ movl(scratch, FieldOperand(name, Name::kHashFieldOffset));
134   // Use only the low 32 bits of the map pointer.
135   __ addl(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
136   __ xorp(scratch, Immediate(flags));
137   // We mask out the last two bits because they are not part of the hash and
138   // they are always 01 for maps.  Also in the two 'and' instructions below.
139   __ andp(scratch, Immediate((kPrimaryTableSize - 1) << kCacheIndexShift));
140 
141   // Probe the primary table.
142   ProbeTable(isolate, masm, ic_kind, flags, kPrimary, receiver, name, scratch);
143 
144   // Primary miss: Compute hash for secondary probe.
145   __ movl(scratch, FieldOperand(name, Name::kHashFieldOffset));
146   __ addl(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
147   __ xorp(scratch, Immediate(flags));
148   __ andp(scratch, Immediate((kPrimaryTableSize - 1) << kCacheIndexShift));
149   __ subl(scratch, name);
150   __ addl(scratch, Immediate(flags));
151   __ andp(scratch, Immediate((kSecondaryTableSize - 1) << kCacheIndexShift));
152 
153   // Probe the secondary table.
154   ProbeTable(isolate, masm, ic_kind, flags, kSecondary, receiver, name,
155              scratch);
156 
157   // Cache miss: Fall-through and let caller handle the miss by
158   // entering the runtime system.
159   __ bind(&miss);
160   __ IncrementCounter(counters->megamorphic_stub_cache_misses(), 1);
161 }
162 
163 
164 #undef __
165 }  // namespace internal
166 }  // namespace v8
167 
168 #endif  // V8_TARGET_ARCH_X64
169