1 // 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.
5 #ifndef V8_X64_CODE_STUBS_X64_H_
6 #define V8_X64_CODE_STUBS_X64_H_
12 void ArrayNativeCode(MacroAssembler* masm, Label* call_generic_code);
15 class StringHelper : public AllStatic {
17 // Generate code for copying characters using the rep movs instruction.
18 // Copies rcx characters from rsi to rdi. Copying of overlapping regions is
20 static void GenerateCopyCharacters(MacroAssembler* masm,
24 String::Encoding encoding);
26 // Compares two flat one-byte strings and returns result in rax.
27 static void GenerateCompareFlatOneByteStrings(
28 MacroAssembler* masm, Register left, Register right, Register scratch1,
29 Register scratch2, Register scratch3, Register scratch4);
31 // Compares two flat one-byte strings for equality and returns result in rax.
32 static void GenerateFlatOneByteStringEquals(MacroAssembler* masm,
33 Register left, Register right,
38 static void GenerateOneByteCharsCompareLoop(
39 MacroAssembler* masm, Register left, Register right, Register length,
40 Register scratch, Label* chars_not_equal,
41 Label::Distance near_jump = Label::kFar);
43 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
47 class NameDictionaryLookupStub: public PlatformCodeStub {
49 enum LookupMode { POSITIVE_LOOKUP, NEGATIVE_LOOKUP };
51 NameDictionaryLookupStub(Isolate* isolate, Register dictionary,
52 Register result, Register index, LookupMode mode)
53 : PlatformCodeStub(isolate) {
54 minor_key_ = DictionaryBits::encode(dictionary.code()) |
55 ResultBits::encode(result.code()) |
56 IndexBits::encode(index.code()) | LookupModeBits::encode(mode);
59 static void GenerateNegativeLookup(MacroAssembler* masm,
66 static void GeneratePositiveLookup(MacroAssembler* masm,
74 bool SometimesSetsUpAFrame() override { return false; }
77 static const int kInlinedProbes = 4;
78 static const int kTotalProbes = 20;
80 static const int kCapacityOffset =
81 NameDictionary::kHeaderSize +
82 NameDictionary::kCapacityIndex * kPointerSize;
84 static const int kElementsStartOffset =
85 NameDictionary::kHeaderSize +
86 NameDictionary::kElementsStartIndex * kPointerSize;
88 Register dictionary() const {
89 return Register::from_code(DictionaryBits::decode(minor_key_));
92 Register result() const {
93 return Register::from_code(ResultBits::decode(minor_key_));
96 Register index() const {
97 return Register::from_code(IndexBits::decode(minor_key_));
100 LookupMode mode() const { return LookupModeBits::decode(minor_key_); }
102 class DictionaryBits: public BitField<int, 0, 4> {};
103 class ResultBits: public BitField<int, 4, 4> {};
104 class IndexBits: public BitField<int, 8, 4> {};
105 class LookupModeBits: public BitField<LookupMode, 12, 1> {};
107 DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
108 DEFINE_PLATFORM_CODE_STUB(NameDictionaryLookup, PlatformCodeStub);
112 class RecordWriteStub: public PlatformCodeStub {
114 RecordWriteStub(Isolate* isolate, Register object, Register value,
115 Register address, RememberedSetAction remembered_set_action,
116 SaveFPRegsMode fp_mode)
117 : PlatformCodeStub(isolate),
118 regs_(object, // An input reg.
119 address, // An input reg.
120 value) { // One scratch reg.
121 minor_key_ = ObjectBits::encode(object.code()) |
122 ValueBits::encode(value.code()) |
123 AddressBits::encode(address.code()) |
124 RememberedSetActionBits::encode(remembered_set_action) |
125 SaveFPRegsModeBits::encode(fp_mode);
128 RecordWriteStub(uint32_t key, Isolate* isolate)
129 : PlatformCodeStub(key, isolate), regs_(object(), address(), value()) {}
134 INCREMENTAL_COMPACTION
137 bool SometimesSetsUpAFrame() override { return false; }
139 static const byte kTwoByteNopInstruction = 0x3c; // Cmpb al, #imm8.
140 static const byte kTwoByteJumpInstruction = 0xeb; // Jmp #imm8.
142 static const byte kFiveByteNopInstruction = 0x3d; // Cmpl eax, #imm32.
143 static const byte kFiveByteJumpInstruction = 0xe9; // Jmp #imm32.
145 static Mode GetMode(Code* stub) {
146 byte first_instruction = stub->instruction_start()[0];
147 byte second_instruction = stub->instruction_start()[2];
149 if (first_instruction == kTwoByteJumpInstruction) {
153 DCHECK(first_instruction == kTwoByteNopInstruction);
155 if (second_instruction == kFiveByteJumpInstruction) {
156 return INCREMENTAL_COMPACTION;
159 DCHECK(second_instruction == kFiveByteNopInstruction);
161 return STORE_BUFFER_ONLY;
164 static void Patch(Code* stub, Mode mode) {
166 case STORE_BUFFER_ONLY:
167 DCHECK(GetMode(stub) == INCREMENTAL ||
168 GetMode(stub) == INCREMENTAL_COMPACTION);
169 stub->instruction_start()[0] = kTwoByteNopInstruction;
170 stub->instruction_start()[2] = kFiveByteNopInstruction;
173 DCHECK(GetMode(stub) == STORE_BUFFER_ONLY);
174 stub->instruction_start()[0] = kTwoByteJumpInstruction;
176 case INCREMENTAL_COMPACTION:
177 DCHECK(GetMode(stub) == STORE_BUFFER_ONLY);
178 stub->instruction_start()[0] = kTwoByteNopInstruction;
179 stub->instruction_start()[2] = kFiveByteJumpInstruction;
182 DCHECK(GetMode(stub) == mode);
183 Assembler::FlushICache(stub->GetIsolate(), stub->instruction_start(), 7);
186 DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
189 // This is a helper class for freeing up 3 scratch registers, where the third
190 // is always rcx (needed for shift operations). The input is two registers
191 // that must be preserved and one scratch register provided by the caller.
192 class RegisterAllocation {
194 RegisterAllocation(Register object,
197 : object_orig_(object),
198 address_orig_(address),
199 scratch0_orig_(scratch0),
202 scratch0_(scratch0) {
203 DCHECK(!AreAliased(scratch0, object, address, no_reg));
204 scratch1_ = GetRegThatIsNotRcxOr(object_, address_, scratch0_);
205 if (scratch0.is(rcx)) {
206 scratch0_ = GetRegThatIsNotRcxOr(object_, address_, scratch1_);
208 if (object.is(rcx)) {
209 object_ = GetRegThatIsNotRcxOr(address_, scratch0_, scratch1_);
211 if (address.is(rcx)) {
212 address_ = GetRegThatIsNotRcxOr(object_, scratch0_, scratch1_);
214 DCHECK(!AreAliased(scratch0_, object_, address_, rcx));
217 void Save(MacroAssembler* masm) {
218 DCHECK(!address_orig_.is(object_));
219 DCHECK(object_.is(object_orig_) || address_.is(address_orig_));
220 DCHECK(!AreAliased(object_, address_, scratch1_, scratch0_));
221 DCHECK(!AreAliased(object_orig_, address_, scratch1_, scratch0_));
222 DCHECK(!AreAliased(object_, address_orig_, scratch1_, scratch0_));
223 // We don't have to save scratch0_orig_ because it was given to us as
224 // a scratch register. But if we had to switch to a different reg then
225 // we should save the new scratch0_.
226 if (!scratch0_.is(scratch0_orig_)) masm->Push(scratch0_);
227 if (!rcx.is(scratch0_orig_) &&
228 !rcx.is(object_orig_) &&
229 !rcx.is(address_orig_)) {
232 masm->Push(scratch1_);
233 if (!address_.is(address_orig_)) {
234 masm->Push(address_);
235 masm->movp(address_, address_orig_);
237 if (!object_.is(object_orig_)) {
239 masm->movp(object_, object_orig_);
243 void Restore(MacroAssembler* masm) {
244 // These will have been preserved the entire time, so we just need to move
245 // them back. Only in one case is the orig_ reg different from the plain
246 // one, since only one of them can alias with rcx.
247 if (!object_.is(object_orig_)) {
248 masm->movp(object_orig_, object_);
251 if (!address_.is(address_orig_)) {
252 masm->movp(address_orig_, address_);
255 masm->Pop(scratch1_);
256 if (!rcx.is(scratch0_orig_) &&
257 !rcx.is(object_orig_) &&
258 !rcx.is(address_orig_)) {
261 if (!scratch0_.is(scratch0_orig_)) masm->Pop(scratch0_);
264 // If we have to call into C then we need to save and restore all caller-
265 // saved registers that were not already preserved.
267 // The three scratch registers (incl. rcx) will be restored by other means
268 // so we don't bother pushing them here. Rbx, rbp and r12-15 are callee
269 // save and don't need to be preserved.
270 void SaveCallerSaveRegisters(MacroAssembler* masm, SaveFPRegsMode mode) {
271 masm->PushCallerSaved(mode, scratch0_, scratch1_, rcx);
274 inline void RestoreCallerSaveRegisters(MacroAssembler*masm,
275 SaveFPRegsMode mode) {
276 masm->PopCallerSaved(mode, scratch0_, scratch1_, rcx);
279 inline Register object() { return object_; }
280 inline Register address() { return address_; }
281 inline Register scratch0() { return scratch0_; }
282 inline Register scratch1() { return scratch1_; }
285 Register object_orig_;
286 Register address_orig_;
287 Register scratch0_orig_;
292 // Third scratch register is always rcx.
294 Register GetRegThatIsNotRcxOr(Register r1,
297 for (int i = 0; i < Register::NumAllocatableRegisters(); i++) {
298 Register candidate = Register::FromAllocationIndex(i);
299 if (candidate.is(rcx)) continue;
300 if (candidate.is(r1)) continue;
301 if (candidate.is(r2)) continue;
302 if (candidate.is(r3)) continue;
308 friend class RecordWriteStub;
311 enum OnNoNeedToInformIncrementalMarker {
312 kReturnOnNoNeedToInformIncrementalMarker,
313 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker
316 Major MajorKey() const final { return RecordWrite; }
318 void Generate(MacroAssembler* masm) override;
319 void GenerateIncremental(MacroAssembler* masm, Mode mode);
320 void CheckNeedsToInformIncrementalMarker(
321 MacroAssembler* masm,
322 OnNoNeedToInformIncrementalMarker on_no_need,
324 void InformIncrementalMarker(MacroAssembler* masm);
326 void Activate(Code* code) override {
327 code->GetHeap()->incremental_marking()->ActivateGeneratedStub(code);
330 Register object() const {
331 return Register::from_code(ObjectBits::decode(minor_key_));
334 Register value() const {
335 return Register::from_code(ValueBits::decode(minor_key_));
338 Register address() const {
339 return Register::from_code(AddressBits::decode(minor_key_));
342 RememberedSetAction remembered_set_action() const {
343 return RememberedSetActionBits::decode(minor_key_);
346 SaveFPRegsMode save_fp_regs_mode() const {
347 return SaveFPRegsModeBits::decode(minor_key_);
350 class ObjectBits: public BitField<int, 0, 4> {};
351 class ValueBits: public BitField<int, 4, 4> {};
352 class AddressBits: public BitField<int, 8, 4> {};
353 class RememberedSetActionBits: public BitField<RememberedSetAction, 12, 1> {};
354 class SaveFPRegsModeBits: public BitField<SaveFPRegsMode, 13, 1> {};
357 RegisterAllocation regs_;
359 DISALLOW_COPY_AND_ASSIGN(RecordWriteStub);
363 } // namespace internal
366 #endif // V8_X64_CODE_STUBS_X64_H_