blob: 4d31d4988272ca0aac98ea4c4e2e7656ae400a83 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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#include "src/v8.h"
6
7#if V8_TARGET_ARCH_ARM64
8
9#include "src/codegen.h"
10#include "src/ic/stub-cache.h"
11
12namespace v8 {
13namespace 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.
25static void ProbeTable(Isolate* isolate, MacroAssembler* masm,
26 Code::Flags flags, bool leave_frame,
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 if (leave_frame) __ LeaveFrame(StackFrame::INTERNAL);
83
84 // Jump to the first instruction in the code stub.
85 __ Add(scratch, scratch, Code::kHeaderSize - kHeapObjectTag);
86 __ Br(scratch);
87
88 // Miss: fall through.
89 __ Bind(&miss);
90}
91
92
93void StubCache::GenerateProbe(MacroAssembler* masm, Code::Flags flags,
94 bool leave_frame, Register receiver,
95 Register name, Register scratch, Register extra,
96 Register extra2, Register extra3) {
97 Isolate* isolate = masm->isolate();
98 Label miss;
99
100 // Make sure the flags does not name a specific type.
101 DCHECK(Code::ExtractTypeFromFlags(flags) == 0);
102
103 // Make sure that there are no register conflicts.
104 DCHECK(!AreAliased(receiver, name, scratch, extra, extra2, extra3));
105
106 // Make sure extra and extra2 registers are valid.
107 DCHECK(!extra.is(no_reg));
108 DCHECK(!extra2.is(no_reg));
109 DCHECK(!extra3.is(no_reg));
110
111 Counters* counters = masm->isolate()->counters();
112 __ IncrementCounter(counters->megamorphic_stub_cache_probes(), 1, extra2,
113 extra3);
114
115 // Check that the receiver isn't a smi.
116 __ JumpIfSmi(receiver, &miss);
117
118 // Compute the hash for primary table.
119 __ Ldr(scratch, FieldMemOperand(name, Name::kHashFieldOffset));
120 __ Ldr(extra, FieldMemOperand(receiver, HeapObject::kMapOffset));
121 __ Add(scratch, scratch, extra);
122 __ Eor(scratch, scratch, flags);
123 // We shift out the last two bits because they are not part of the hash.
124 __ Ubfx(scratch, scratch, kCacheIndexShift,
125 CountTrailingZeros(kPrimaryTableSize, 64));
126
127 // Probe the primary table.
128 ProbeTable(isolate, masm, flags, leave_frame, kPrimary, receiver, name,
129 scratch, extra, extra2, extra3);
130
131 // Primary miss: Compute hash for secondary table.
132 __ Sub(scratch, scratch, Operand(name, LSR, kCacheIndexShift));
133 __ Add(scratch, scratch, flags >> kCacheIndexShift);
134 __ And(scratch, scratch, kSecondaryTableSize - 1);
135
136 // Probe the secondary table.
137 ProbeTable(isolate, masm, flags, leave_frame, kSecondary, receiver, name,
138 scratch, extra, extra2, extra3);
139
140 // Cache miss: Fall-through and let caller handle the miss by
141 // entering the runtime system.
142 __ Bind(&miss);
143 __ IncrementCounter(counters->megamorphic_stub_cache_misses(), 1, extra2,
144 extra3);
145}
146}
147} // namespace v8::internal
148
149#endif // V8_TARGET_ARCH_ARM64