1a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org// Copyright 2012 the V8 project authors. All rights reserved.
2a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org// Use of this source code is governed by a BSD-style license that can be
3a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org// found in the LICENSE file.
4a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
5a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#include "src/v8.h"
6a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
7a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#if V8_TARGET_ARCH_IA32
8a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
9a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#include "src/codegen.h"
10a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#include "src/ic/stub-cache.h"
11a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
12a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.orgnamespace v8 {
13a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.orgnamespace internal {
14a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
15a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#define __ ACCESS_MASM(masm)
16a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
17a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
18a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.orgstatic void ProbeTable(Isolate* isolate, MacroAssembler* masm,
19e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org                       Code::Flags flags, bool leave_frame,
20e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org                       StubCache::Table table, Register name, Register receiver,
21a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org                       // Number of the cache entry pointer-size scaled.
22a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org                       Register offset, Register extra) {
23a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  ExternalReference key_offset(isolate->stub_cache()->key_reference(table));
24a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  ExternalReference value_offset(isolate->stub_cache()->value_reference(table));
25a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  ExternalReference map_offset(isolate->stub_cache()->map_reference(table));
26a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
27a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  Label miss;
28a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
29a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Multiply by 3 because there are 3 fields per entry (name, code, map).
30a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ lea(offset, Operand(offset, offset, times_2, 0));
31a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
32a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  if (extra.is_valid()) {
33a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Get the code entry from the cache.
34a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ mov(extra, Operand::StaticArray(offset, times_1, value_offset));
35a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
36a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Check that the key in the entry matches the name.
37a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ cmp(name, Operand::StaticArray(offset, times_1, key_offset));
38a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ j(not_equal, &miss);
39a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
40a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Check the map matches.
41a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ mov(offset, Operand::StaticArray(offset, times_1, map_offset));
42a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ cmp(offset, FieldOperand(receiver, HeapObject::kMapOffset));
43a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ j(not_equal, &miss);
44a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
45a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Check that the flags match what we're looking for.
46a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ mov(offset, FieldOperand(extra, Code::kFlagsOffset));
47a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ and_(offset, ~Code::kFlagsNotUsedInLookup);
48a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ cmp(offset, flags);
49a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ j(not_equal, &miss);
50a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
51a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#ifdef DEBUG
52a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    if (FLAG_test_secondary_stub_cache && table == StubCache::kPrimary) {
53a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org      __ jmp(&miss);
54a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    } else if (FLAG_test_primary_stub_cache && table == StubCache::kSecondary) {
55a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org      __ jmp(&miss);
56a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    }
57a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#endif
58a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
59e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org    if (leave_frame) __ leave();
60e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org
61a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Jump to the first instruction in the code stub.
62a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ add(extra, Immediate(Code::kHeaderSize - kHeapObjectTag));
63a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ jmp(extra);
64a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
65a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ bind(&miss);
66a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  } else {
67a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Save the offset on the stack.
68a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ push(offset);
69a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
70a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Check that the key in the entry matches the name.
71a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ cmp(name, Operand::StaticArray(offset, times_1, key_offset));
72a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ j(not_equal, &miss);
73a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
74a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Check the map matches.
75a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ mov(offset, Operand::StaticArray(offset, times_1, map_offset));
76a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ cmp(offset, FieldOperand(receiver, HeapObject::kMapOffset));
77a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ j(not_equal, &miss);
78a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
79a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Restore offset register.
80a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ mov(offset, Operand(esp, 0));
81a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
82a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Get the code entry from the cache.
83a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ mov(offset, Operand::StaticArray(offset, times_1, value_offset));
84a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
85a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Check that the flags match what we're looking for.
86a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ mov(offset, FieldOperand(offset, Code::kFlagsOffset));
87a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ and_(offset, ~Code::kFlagsNotUsedInLookup);
88a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ cmp(offset, flags);
89a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ j(not_equal, &miss);
90a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
91a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#ifdef DEBUG
92a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    if (FLAG_test_secondary_stub_cache && table == StubCache::kPrimary) {
93a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org      __ jmp(&miss);
94a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    } else if (FLAG_test_primary_stub_cache && table == StubCache::kSecondary) {
95a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org      __ jmp(&miss);
96a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    }
97a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#endif
98a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
99a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Restore offset and re-load code entry from cache.
100a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ pop(offset);
101a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ mov(offset, Operand::StaticArray(offset, times_1, value_offset));
102a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
103e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org    if (leave_frame) __ leave();
104e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org
105a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Jump to the first instruction in the code stub.
106a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ add(offset, Immediate(Code::kHeaderSize - kHeapObjectTag));
107a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ jmp(offset);
108a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
109a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    // Pop at miss.
110a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ bind(&miss);
111a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org    __ pop(offset);
112a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  }
113a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org}
114a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
115a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
116a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.orgvoid StubCache::GenerateProbe(MacroAssembler* masm, Code::Flags flags,
117e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org                              bool leave_frame, Register receiver,
118e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org                              Register name, Register scratch, Register extra,
119e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org                              Register extra2, Register extra3) {
120a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  Label miss;
121a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
122a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Assert that code is valid.  The multiplying code relies on the entry size
123a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // being 12.
124a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(sizeof(Entry) == 12);
125a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
126a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Assert the flags do not name a specific type.
127a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(Code::ExtractTypeFromFlags(flags) == 0);
128a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
129a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Assert that there are no register conflicts.
130a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(!scratch.is(receiver));
131a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(!scratch.is(name));
132a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(!extra.is(receiver));
133a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(!extra.is(name));
134a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(!extra.is(scratch));
135a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
136a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Assert scratch and extra registers are valid, and extra2/3 are unused.
137a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(!scratch.is(no_reg));
138a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(extra2.is(no_reg));
139a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(extra3.is(no_reg));
140a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
141a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  Register offset = scratch;
142a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  scratch = no_reg;
143a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
144a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  Counters* counters = masm->isolate()->counters();
145a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ IncrementCounter(counters->megamorphic_stub_cache_probes(), 1);
146a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
147a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Check that the receiver isn't a smi.
148a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ JumpIfSmi(receiver, &miss);
149a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
150a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Get the map of the receiver and compute the hash.
151a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ mov(offset, FieldOperand(name, Name::kHashFieldOffset));
152a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ add(offset, FieldOperand(receiver, HeapObject::kMapOffset));
153a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ xor_(offset, flags);
154a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // We mask out the last two bits because they are not part of the hash and
155a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // they are always 01 for maps.  Also in the two 'and' instructions below.
156a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ and_(offset, (kPrimaryTableSize - 1) << kCacheIndexShift);
157a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // ProbeTable expects the offset to be pointer scaled, which it is, because
158a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // the heap object tag size is 2 and the pointer size log 2 is also 2.
159a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  DCHECK(kCacheIndexShift == kPointerSizeLog2);
160a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
161a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Probe the primary table.
162e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org  ProbeTable(isolate(), masm, flags, leave_frame, kPrimary, name, receiver,
163e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org             offset, extra);
164a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
165a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Primary miss: Compute hash for secondary probe.
166a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ mov(offset, FieldOperand(name, Name::kHashFieldOffset));
167a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ add(offset, FieldOperand(receiver, HeapObject::kMapOffset));
168a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ xor_(offset, flags);
169a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ and_(offset, (kPrimaryTableSize - 1) << kCacheIndexShift);
170a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ sub(offset, name);
171a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ add(offset, Immediate(flags));
172a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ and_(offset, (kSecondaryTableSize - 1) << kCacheIndexShift);
173a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
174a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Probe the secondary table.
175e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org  ProbeTable(isolate(), masm, flags, leave_frame, kSecondary, name, receiver,
176e20e19efeef112c26d0e63b1e5118e695b42d855machenbach@chromium.org             offset, extra);
177a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
178a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // Cache miss: Fall-through and let caller handle the miss by
179a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  // entering the runtime system.
180a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ bind(&miss);
181a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org  __ IncrementCounter(counters->megamorphic_stub_cache_misses(), 1);
182a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org}
183a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
184a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
185a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#undef __
186a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org}
187a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org}  // namespace v8::internal
188a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org
189a8702c210b949f35c64d8e4aa01bb6d525086c85machenbach@chromium.org#endif  // V8_TARGET_ARCH_IA32
190