• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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_ARM64
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 
16 #define __ ACCESS_MASM(masm)
17 
18 
19 // Probe primary or secondary table.
20 // If the entry is found in the cache, the generated code jump to the first
21 // instruction of the stub in the cache.
22 // If there is a miss the code fall trough.
23 //
24 // 'receiver', 'name' and 'offset' registers are preserved on miss.
ProbeTable(Isolate * isolate,MacroAssembler * masm,Code::Kind ic_kind,Code::Flags flags,StubCache::Table table,Register receiver,Register name,Register offset,Register scratch,Register scratch2,Register scratch3)25 static void ProbeTable(Isolate* isolate, MacroAssembler* masm,
26                        Code::Kind ic_kind, Code::Flags flags,
27                        StubCache::Table table, Register receiver, Register name,
28                        Register offset, Register scratch, Register scratch2,
29                        Register scratch3) {
30   // Some code below relies on the fact that the Entry struct contains
31   // 3 pointers (name, code, map).
32   STATIC_ASSERT(sizeof(StubCache::Entry) == (3 * kPointerSize));
33 
34   ExternalReference key_offset(isolate->stub_cache()->key_reference(table));
35   ExternalReference value_offset(isolate->stub_cache()->value_reference(table));
36   ExternalReference map_offset(isolate->stub_cache()->map_reference(table));
37 
38   uintptr_t key_off_addr = reinterpret_cast<uintptr_t>(key_offset.address());
39   uintptr_t value_off_addr =
40       reinterpret_cast<uintptr_t>(value_offset.address());
41   uintptr_t map_off_addr = reinterpret_cast<uintptr_t>(map_offset.address());
42 
43   Label miss;
44 
45   DCHECK(!AreAliased(name, offset, scratch, scratch2, scratch3));
46 
47   // Multiply by 3 because there are 3 fields per entry.
48   __ Add(scratch3, offset, Operand(offset, LSL, 1));
49 
50   // Calculate the base address of the entry.
51   __ Mov(scratch, key_offset);
52   __ Add(scratch, scratch, Operand(scratch3, LSL, kPointerSizeLog2));
53 
54   // Check that the key in the entry matches the name.
55   __ Ldr(scratch2, MemOperand(scratch));
56   __ Cmp(name, scratch2);
57   __ B(ne, &miss);
58 
59   // Check the map matches.
60   __ Ldr(scratch2, MemOperand(scratch, map_off_addr - key_off_addr));
61   __ Ldr(scratch3, FieldMemOperand(receiver, HeapObject::kMapOffset));
62   __ Cmp(scratch2, scratch3);
63   __ B(ne, &miss);
64 
65   // Get the code entry from the cache.
66   __ Ldr(scratch, MemOperand(scratch, value_off_addr - key_off_addr));
67 
68   // Check that the flags match what we're looking for.
69   __ Ldr(scratch2.W(), FieldMemOperand(scratch, Code::kFlagsOffset));
70   __ Bic(scratch2.W(), scratch2.W(), Code::kFlagsNotUsedInLookup);
71   __ Cmp(scratch2.W(), flags);
72   __ B(ne, &miss);
73 
74 #ifdef DEBUG
75   if (FLAG_test_secondary_stub_cache && table == StubCache::kPrimary) {
76     __ B(&miss);
77   } else if (FLAG_test_primary_stub_cache && table == StubCache::kSecondary) {
78     __ B(&miss);
79   }
80 #endif
81 
82   // Jump to the first instruction in the code stub.
83   __ Add(scratch, scratch, Code::kHeaderSize - kHeapObjectTag);
84   __ Br(scratch);
85 
86   // Miss: fall through.
87   __ Bind(&miss);
88 }
89 
90 
GenerateProbe(MacroAssembler * masm,Code::Kind ic_kind,Code::Flags flags,Register receiver,Register name,Register scratch,Register extra,Register extra2,Register extra3)91 void StubCache::GenerateProbe(MacroAssembler* masm, Code::Kind ic_kind,
92                               Code::Flags flags, Register receiver,
93                               Register name, Register scratch, Register extra,
94                               Register extra2, Register extra3) {
95   Isolate* isolate = masm->isolate();
96   Label miss;
97 
98   // Make sure the flags does not name a specific type.
99   DCHECK(Code::ExtractTypeFromFlags(flags) == 0);
100 
101   // Make sure that there are no register conflicts.
102   DCHECK(!AreAliased(receiver, name, scratch, extra, extra2, extra3));
103 
104   // Make sure extra and extra2 registers are valid.
105   DCHECK(!extra.is(no_reg));
106   DCHECK(!extra2.is(no_reg));
107   DCHECK(!extra3.is(no_reg));
108 
109 #ifdef DEBUG
110   // If vector-based ics are in use, ensure that scratch, extra, extra2 and
111   // extra3 don't conflict with the vector and slot registers, which need
112   // to be preserved for a handler call or miss.
113   if (IC::ICUseVector(ic_kind)) {
114     Register vector, slot;
115     if (ic_kind == Code::STORE_IC || ic_kind == Code::KEYED_STORE_IC) {
116       vector = VectorStoreICDescriptor::VectorRegister();
117       slot = VectorStoreICDescriptor::SlotRegister();
118     } else {
119       vector = LoadWithVectorDescriptor::VectorRegister();
120       slot = LoadWithVectorDescriptor::SlotRegister();
121     }
122     DCHECK(!AreAliased(vector, slot, scratch, extra, extra2, extra3));
123   }
124 #endif
125 
126   Counters* counters = masm->isolate()->counters();
127   __ IncrementCounter(counters->megamorphic_stub_cache_probes(), 1, extra2,
128                       extra3);
129 
130   // Check that the receiver isn't a smi.
131   __ JumpIfSmi(receiver, &miss);
132 
133   // Compute the hash for primary table.
134   __ Ldr(scratch, FieldMemOperand(name, Name::kHashFieldOffset));
135   __ Ldr(extra, FieldMemOperand(receiver, HeapObject::kMapOffset));
136   __ Add(scratch, scratch, extra);
137   __ Eor(scratch, scratch, flags);
138   // We shift out the last two bits because they are not part of the hash.
139   __ Ubfx(scratch, scratch, kCacheIndexShift,
140           CountTrailingZeros(kPrimaryTableSize, 64));
141 
142   // Probe the primary table.
143   ProbeTable(isolate, masm, ic_kind, flags, kPrimary, receiver, name, scratch,
144              extra, extra2, extra3);
145 
146   // Primary miss: Compute hash for secondary table.
147   __ Sub(scratch, scratch, Operand(name, LSR, kCacheIndexShift));
148   __ Add(scratch, scratch, flags >> kCacheIndexShift);
149   __ And(scratch, scratch, kSecondaryTableSize - 1);
150 
151   // Probe the secondary table.
152   ProbeTable(isolate, masm, ic_kind, flags, kSecondary, receiver, name, scratch,
153              extra, extra2, extra3);
154 
155   // Cache miss: Fall-through and let caller handle the miss by
156   // entering the runtime system.
157   __ Bind(&miss);
158   __ IncrementCounter(counters->megamorphic_stub_cache_misses(), 1, extra2,
159                       extra3);
160 }
161 }  // namespace internal
162 }  // namespace v8
163 
164 #endif  // V8_TARGET_ARCH_ARM64
165