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_X87
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
12namespace v8 {
13namespace internal {
14
15#define __ ACCESS_MASM(masm)
16
17static void ProbeTable(StubCache* stub_cache, MacroAssembler* masm,
18                       StubCache::Table table, Register name, Register receiver,
19                       // The offset is scaled by 4, based on
20                       // kCacheIndexShift, which is two bits
21                       Register offset, Register extra) {
22  ExternalReference key_offset(stub_cache->key_reference(table));
23  ExternalReference value_offset(stub_cache->value_reference(table));
24  ExternalReference map_offset(stub_cache->map_reference(table));
25
26  Label miss;
27  Code::Kind ic_kind = stub_cache->ic_kind();
28  bool is_vector_store =
29      IC::ICUseVector(ic_kind) &&
30      (ic_kind == Code::STORE_IC || ic_kind == Code::KEYED_STORE_IC);
31
32  // Multiply by 3 because there are 3 fields per entry (name, code, map).
33  __ lea(offset, Operand(offset, offset, times_2, 0));
34
35  if (extra.is_valid()) {
36    // Get the code entry from the cache.
37    __ mov(extra, Operand::StaticArray(offset, times_1, value_offset));
38
39    // Check that the key in the entry matches the name.
40    __ cmp(name, Operand::StaticArray(offset, times_1, key_offset));
41    __ j(not_equal, &miss);
42
43    // Check the map matches.
44    __ mov(offset, Operand::StaticArray(offset, times_1, map_offset));
45    __ cmp(offset, FieldOperand(receiver, HeapObject::kMapOffset));
46    __ j(not_equal, &miss);
47
48#ifdef DEBUG
49    if (FLAG_test_secondary_stub_cache && table == StubCache::kPrimary) {
50      __ jmp(&miss);
51    } else if (FLAG_test_primary_stub_cache && table == StubCache::kSecondary) {
52      __ jmp(&miss);
53    }
54#endif
55
56    if (is_vector_store) {
57      // The value, vector and slot were passed to the IC on the stack and
58      // they are still there. So we can just jump to the handler.
59      DCHECK(extra.is(StoreWithVectorDescriptor::SlotRegister()));
60      __ add(extra, Immediate(Code::kHeaderSize - kHeapObjectTag));
61      __ jmp(extra);
62    } else {
63      // The vector and slot were pushed onto the stack before starting the
64      // probe, and need to be dropped before calling the handler.
65      __ pop(LoadWithVectorDescriptor::VectorRegister());
66      __ pop(LoadDescriptor::SlotRegister());
67      __ add(extra, Immediate(Code::kHeaderSize - kHeapObjectTag));
68      __ jmp(extra);
69    }
70
71    __ bind(&miss);
72  } else {
73    DCHECK(ic_kind == Code::STORE_IC || ic_kind == Code::KEYED_STORE_IC);
74
75    // Save the offset on the stack.
76    __ push(offset);
77
78    // Check that the key in the entry matches the name.
79    __ cmp(name, Operand::StaticArray(offset, times_1, key_offset));
80    __ j(not_equal, &miss);
81
82    // Check the map matches.
83    __ mov(offset, Operand::StaticArray(offset, times_1, map_offset));
84    __ cmp(offset, FieldOperand(receiver, HeapObject::kMapOffset));
85    __ j(not_equal, &miss);
86
87    // Restore offset register.
88    __ mov(offset, Operand(esp, 0));
89
90    // Get the code entry from the cache.
91    __ mov(offset, Operand::StaticArray(offset, times_1, value_offset));
92
93#ifdef DEBUG
94    if (FLAG_test_secondary_stub_cache && table == StubCache::kPrimary) {
95      __ jmp(&miss);
96    } else if (FLAG_test_primary_stub_cache && table == StubCache::kSecondary) {
97      __ jmp(&miss);
98    }
99#endif
100
101    // Restore offset and re-load code entry from cache.
102    __ pop(offset);
103    __ mov(offset, Operand::StaticArray(offset, times_1, value_offset));
104
105    // Jump to the first instruction in the code stub.
106    if (is_vector_store) {
107      DCHECK(offset.is(StoreWithVectorDescriptor::SlotRegister()));
108    }
109    __ add(offset, Immediate(Code::kHeaderSize - kHeapObjectTag));
110    __ jmp(offset);
111
112    // Pop at miss.
113    __ bind(&miss);
114    __ pop(offset);
115  }
116}
117
118void StubCache::GenerateProbe(MacroAssembler* masm, Register receiver,
119                              Register name, Register scratch, Register extra,
120                              Register extra2, Register extra3) {
121  Label miss;
122
123  // Assert that code is valid.  The multiplying code relies on the entry size
124  // being 12.
125  DCHECK(sizeof(Entry) == 12);
126
127  // Assert that there are no register conflicts.
128  DCHECK(!scratch.is(receiver));
129  DCHECK(!scratch.is(name));
130  DCHECK(!extra.is(receiver));
131  DCHECK(!extra.is(name));
132  DCHECK(!extra.is(scratch));
133
134  // Assert scratch and extra registers are valid, and extra2/3 are unused.
135  DCHECK(!scratch.is(no_reg));
136  DCHECK(extra2.is(no_reg));
137  DCHECK(extra3.is(no_reg));
138
139  Register offset = scratch;
140  scratch = no_reg;
141
142  Counters* counters = masm->isolate()->counters();
143  __ IncrementCounter(counters->megamorphic_stub_cache_probes(), 1);
144
145  // Check that the receiver isn't a smi.
146  __ JumpIfSmi(receiver, &miss);
147
148  // Get the map of the receiver and compute the hash.
149  __ mov(offset, FieldOperand(name, Name::kHashFieldOffset));
150  __ add(offset, FieldOperand(receiver, HeapObject::kMapOffset));
151  __ xor_(offset, kPrimaryMagic);
152  // We mask out the last two bits because they are not part of the hash and
153  // they are always 01 for maps.  Also in the two 'and' instructions below.
154  __ and_(offset, (kPrimaryTableSize - 1) << kCacheIndexShift);
155  // ProbeTable expects the offset to be pointer scaled, which it is, because
156  // the heap object tag size is 2 and the pointer size log 2 is also 2.
157  DCHECK(kCacheIndexShift == kPointerSizeLog2);
158
159  // Probe the primary table.
160  ProbeTable(this, masm, kPrimary, name, receiver, offset, extra);
161
162  // Primary miss: Compute hash for secondary probe.
163  __ mov(offset, FieldOperand(name, Name::kHashFieldOffset));
164  __ add(offset, FieldOperand(receiver, HeapObject::kMapOffset));
165  __ xor_(offset, kPrimaryMagic);
166  __ and_(offset, (kPrimaryTableSize - 1) << kCacheIndexShift);
167  __ sub(offset, name);
168  __ add(offset, Immediate(kSecondaryMagic));
169  __ and_(offset, (kSecondaryTableSize - 1) << kCacheIndexShift);
170
171  // Probe the secondary table.
172  ProbeTable(this, masm, kSecondary, name, receiver, offset, extra);
173
174  // Cache miss: Fall-through and let caller handle the miss by
175  // entering the runtime system.
176  __ bind(&miss);
177  __ IncrementCounter(counters->megamorphic_stub_cache_misses(), 1);
178}
179
180
181#undef __
182}  // namespace internal
183}  // namespace v8
184
185#endif  // V8_TARGET_ARCH_X87
186