blob: 6290cfed1c3827e84a34d5de618fa744ef11928e [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2011 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#ifndef V8_X87_CODE_STUBS_X87_H_
6#define V8_X87_CODE_STUBS_X87_H_
7
8namespace v8 {
9namespace internal {
10
11
12void ArrayNativeCode(MacroAssembler* masm,
13 bool construct_call,
14 Label* call_generic_code);
15
16
17class StringHelper : public AllStatic {
18 public:
19 // Generate code for copying characters using the rep movs instruction.
20 // Copies ecx characters from esi to edi. Copying of overlapping regions is
21 // not supported.
22 static void GenerateCopyCharacters(MacroAssembler* masm,
23 Register dest,
24 Register src,
25 Register count,
26 Register scratch,
27 String::Encoding encoding);
28
29 // Compares two flat one byte strings and returns result in eax.
30 static void GenerateCompareFlatOneByteStrings(MacroAssembler* masm,
31 Register left, Register right,
32 Register scratch1,
33 Register scratch2,
34 Register scratch3);
35
36 // Compares two flat one byte strings for equality and returns result in eax.
37 static void GenerateFlatOneByteStringEquals(MacroAssembler* masm,
38 Register left, Register right,
39 Register scratch1,
40 Register scratch2);
41
42 private:
43 static void GenerateOneByteCharsCompareLoop(
44 MacroAssembler* masm, Register left, Register right, Register length,
45 Register scratch, Label* chars_not_equal,
46 Label::Distance chars_not_equal_near = Label::kFar);
47
48 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
49};
50
51
52class NameDictionaryLookupStub: public PlatformCodeStub {
53 public:
54 enum LookupMode { POSITIVE_LOOKUP, NEGATIVE_LOOKUP };
55
56 NameDictionaryLookupStub(Isolate* isolate, Register dictionary,
57 Register result, Register index, LookupMode mode)
58 : PlatformCodeStub(isolate) {
59 minor_key_ = DictionaryBits::encode(dictionary.code()) |
60 ResultBits::encode(result.code()) |
61 IndexBits::encode(index.code()) | LookupModeBits::encode(mode);
62 }
63
64 static void GenerateNegativeLookup(MacroAssembler* masm,
65 Label* miss,
66 Label* done,
67 Register properties,
68 Handle<Name> name,
69 Register r0);
70
71 static void GeneratePositiveLookup(MacroAssembler* masm,
72 Label* miss,
73 Label* done,
74 Register elements,
75 Register name,
76 Register r0,
77 Register r1);
78
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079 bool SometimesSetsUpAFrame() override { return false; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080
81 private:
82 static const int kInlinedProbes = 4;
83 static const int kTotalProbes = 20;
84
85 static const int kCapacityOffset =
86 NameDictionary::kHeaderSize +
87 NameDictionary::kCapacityIndex * kPointerSize;
88
89 static const int kElementsStartOffset =
90 NameDictionary::kHeaderSize +
91 NameDictionary::kElementsStartIndex * kPointerSize;
92
93 Register dictionary() const {
94 return Register::from_code(DictionaryBits::decode(minor_key_));
95 }
96
97 Register result() const {
98 return Register::from_code(ResultBits::decode(minor_key_));
99 }
100
101 Register index() const {
102 return Register::from_code(IndexBits::decode(minor_key_));
103 }
104
105 LookupMode mode() const { return LookupModeBits::decode(minor_key_); }
106
107 class DictionaryBits: public BitField<int, 0, 3> {};
108 class ResultBits: public BitField<int, 3, 3> {};
109 class IndexBits: public BitField<int, 6, 3> {};
110 class LookupModeBits: public BitField<LookupMode, 9, 1> {};
111
112 DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
113 DEFINE_PLATFORM_CODE_STUB(NameDictionaryLookup, PlatformCodeStub);
114};
115
116
117class RecordWriteStub: public PlatformCodeStub {
118 public:
119 RecordWriteStub(Isolate* isolate, Register object, Register value,
120 Register address, RememberedSetAction remembered_set_action,
121 SaveFPRegsMode fp_mode)
122 : PlatformCodeStub(isolate),
123 regs_(object, // An input reg.
124 address, // An input reg.
125 value) { // One scratch reg.
126 minor_key_ = ObjectBits::encode(object.code()) |
127 ValueBits::encode(value.code()) |
128 AddressBits::encode(address.code()) |
129 RememberedSetActionBits::encode(remembered_set_action) |
130 SaveFPRegsModeBits::encode(fp_mode);
131 }
132
133 RecordWriteStub(uint32_t key, Isolate* isolate)
134 : PlatformCodeStub(key, isolate), regs_(object(), address(), value()) {}
135
136 enum Mode {
137 STORE_BUFFER_ONLY,
138 INCREMENTAL,
139 INCREMENTAL_COMPACTION
140 };
141
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142 bool SometimesSetsUpAFrame() override { return false; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143
144 static const byte kTwoByteNopInstruction = 0x3c; // Cmpb al, #imm8.
145 static const byte kTwoByteJumpInstruction = 0xeb; // Jmp #imm8.
146
147 static const byte kFiveByteNopInstruction = 0x3d; // Cmpl eax, #imm32.
148 static const byte kFiveByteJumpInstruction = 0xe9; // Jmp #imm32.
149
150 static Mode GetMode(Code* stub) {
151 byte first_instruction = stub->instruction_start()[0];
152 byte second_instruction = stub->instruction_start()[2];
153
154 if (first_instruction == kTwoByteJumpInstruction) {
155 return INCREMENTAL;
156 }
157
158 DCHECK(first_instruction == kTwoByteNopInstruction);
159
160 if (second_instruction == kFiveByteJumpInstruction) {
161 return INCREMENTAL_COMPACTION;
162 }
163
164 DCHECK(second_instruction == kFiveByteNopInstruction);
165
166 return STORE_BUFFER_ONLY;
167 }
168
169 static void Patch(Code* stub, Mode mode) {
170 switch (mode) {
171 case STORE_BUFFER_ONLY:
172 DCHECK(GetMode(stub) == INCREMENTAL ||
173 GetMode(stub) == INCREMENTAL_COMPACTION);
174 stub->instruction_start()[0] = kTwoByteNopInstruction;
175 stub->instruction_start()[2] = kFiveByteNopInstruction;
176 break;
177 case INCREMENTAL:
178 DCHECK(GetMode(stub) == STORE_BUFFER_ONLY);
179 stub->instruction_start()[0] = kTwoByteJumpInstruction;
180 break;
181 case INCREMENTAL_COMPACTION:
182 DCHECK(GetMode(stub) == STORE_BUFFER_ONLY);
183 stub->instruction_start()[0] = kTwoByteNopInstruction;
184 stub->instruction_start()[2] = kFiveByteJumpInstruction;
185 break;
186 }
187 DCHECK(GetMode(stub) == mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000188 Assembler::FlushICache(stub->GetIsolate(), stub->instruction_start(), 7);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189 }
190
191 DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
192
193 private:
194 // This is a helper class for freeing up 3 scratch registers, where the third
195 // is always ecx (needed for shift operations). The input is two registers
196 // that must be preserved and one scratch register provided by the caller.
197 class RegisterAllocation {
198 public:
199 RegisterAllocation(Register object,
200 Register address,
201 Register scratch0)
202 : object_orig_(object),
203 address_orig_(address),
204 scratch0_orig_(scratch0),
205 object_(object),
206 address_(address),
207 scratch0_(scratch0) {
208 DCHECK(!AreAliased(scratch0, object, address, no_reg));
209 scratch1_ = GetRegThatIsNotEcxOr(object_, address_, scratch0_);
210 if (scratch0.is(ecx)) {
211 scratch0_ = GetRegThatIsNotEcxOr(object_, address_, scratch1_);
212 }
213 if (object.is(ecx)) {
214 object_ = GetRegThatIsNotEcxOr(address_, scratch0_, scratch1_);
215 }
216 if (address.is(ecx)) {
217 address_ = GetRegThatIsNotEcxOr(object_, scratch0_, scratch1_);
218 }
219 DCHECK(!AreAliased(scratch0_, object_, address_, ecx));
220 }
221
222 void Save(MacroAssembler* masm) {
223 DCHECK(!address_orig_.is(object_));
224 DCHECK(object_.is(object_orig_) || address_.is(address_orig_));
225 DCHECK(!AreAliased(object_, address_, scratch1_, scratch0_));
226 DCHECK(!AreAliased(object_orig_, address_, scratch1_, scratch0_));
227 DCHECK(!AreAliased(object_, address_orig_, scratch1_, scratch0_));
228 // We don't have to save scratch0_orig_ because it was given to us as
229 // a scratch register. But if we had to switch to a different reg then
230 // we should save the new scratch0_.
231 if (!scratch0_.is(scratch0_orig_)) masm->push(scratch0_);
232 if (!ecx.is(scratch0_orig_) &&
233 !ecx.is(object_orig_) &&
234 !ecx.is(address_orig_)) {
235 masm->push(ecx);
236 }
237 masm->push(scratch1_);
238 if (!address_.is(address_orig_)) {
239 masm->push(address_);
240 masm->mov(address_, address_orig_);
241 }
242 if (!object_.is(object_orig_)) {
243 masm->push(object_);
244 masm->mov(object_, object_orig_);
245 }
246 }
247
248 void Restore(MacroAssembler* masm) {
249 // These will have been preserved the entire time, so we just need to move
250 // them back. Only in one case is the orig_ reg different from the plain
251 // one, since only one of them can alias with ecx.
252 if (!object_.is(object_orig_)) {
253 masm->mov(object_orig_, object_);
254 masm->pop(object_);
255 }
256 if (!address_.is(address_orig_)) {
257 masm->mov(address_orig_, address_);
258 masm->pop(address_);
259 }
260 masm->pop(scratch1_);
261 if (!ecx.is(scratch0_orig_) &&
262 !ecx.is(object_orig_) &&
263 !ecx.is(address_orig_)) {
264 masm->pop(ecx);
265 }
266 if (!scratch0_.is(scratch0_orig_)) masm->pop(scratch0_);
267 }
268
269 // If we have to call into C then we need to save and restore all caller-
270 // saved registers that were not already preserved. The caller saved
271 // registers are eax, ecx and edx. The three scratch registers (incl. ecx)
272 // will be restored by other means so we don't bother pushing them here.
273 void SaveCallerSaveRegisters(MacroAssembler* masm, SaveFPRegsMode mode) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100274 masm->PushCallerSaved(mode, ecx, scratch0_, scratch1_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275 }
276
277 inline void RestoreCallerSaveRegisters(MacroAssembler* masm,
278 SaveFPRegsMode mode) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100279 masm->PopCallerSaved(mode, ecx, scratch0_, scratch1_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 }
281
282 inline Register object() { return object_; }
283 inline Register address() { return address_; }
284 inline Register scratch0() { return scratch0_; }
285 inline Register scratch1() { return scratch1_; }
286
287 private:
288 Register object_orig_;
289 Register address_orig_;
290 Register scratch0_orig_;
291 Register object_;
292 Register address_;
293 Register scratch0_;
294 Register scratch1_;
295 // Third scratch register is always ecx.
296
297 Register GetRegThatIsNotEcxOr(Register r1,
298 Register r2,
299 Register r3) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000300 for (int i = 0; i < Register::kNumRegisters; i++) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100301 if (RegisterConfiguration::Crankshaft()->IsAllocatableGeneralCode(i)) {
302 Register candidate = Register::from_code(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000303 if (candidate.is(ecx)) continue;
304 if (candidate.is(r1)) continue;
305 if (candidate.is(r2)) continue;
306 if (candidate.is(r3)) continue;
307 return candidate;
308 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309 }
310 UNREACHABLE();
311 return no_reg;
312 }
313 friend class RecordWriteStub;
314 };
315
316 enum OnNoNeedToInformIncrementalMarker {
317 kReturnOnNoNeedToInformIncrementalMarker,
318 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker
319 };
320
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000321 inline Major MajorKey() const final { return RecordWrite; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000323 void Generate(MacroAssembler* masm) override;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000324 void GenerateIncremental(MacroAssembler* masm, Mode mode);
325 void CheckNeedsToInformIncrementalMarker(
326 MacroAssembler* masm,
327 OnNoNeedToInformIncrementalMarker on_no_need,
328 Mode mode);
329 void InformIncrementalMarker(MacroAssembler* masm);
330
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000331 void Activate(Code* code) override {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 code->GetHeap()->incremental_marking()->ActivateGeneratedStub(code);
333 }
334
335 Register object() const {
336 return Register::from_code(ObjectBits::decode(minor_key_));
337 }
338
339 Register value() const {
340 return Register::from_code(ValueBits::decode(minor_key_));
341 }
342
343 Register address() const {
344 return Register::from_code(AddressBits::decode(minor_key_));
345 }
346
347 RememberedSetAction remembered_set_action() const {
348 return RememberedSetActionBits::decode(minor_key_);
349 }
350
351 SaveFPRegsMode save_fp_regs_mode() const {
352 return SaveFPRegsModeBits::decode(minor_key_);
353 }
354
355 class ObjectBits: public BitField<int, 0, 3> {};
356 class ValueBits: public BitField<int, 3, 3> {};
357 class AddressBits: public BitField<int, 6, 3> {};
358 class RememberedSetActionBits: public BitField<RememberedSetAction, 9, 1> {};
359 class SaveFPRegsModeBits : public BitField<SaveFPRegsMode, 10, 1> {};
360
361 RegisterAllocation regs_;
362
363 DISALLOW_COPY_AND_ASSIGN(RecordWriteStub);
364};
365
366
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000367} // namespace internal
368} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000369
370#endif // V8_X87_CODE_STUBS_X87_H_