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_
8 #include "src/code-stubs.h"
14 void ArrayNativeCode(MacroAssembler* masm, Label* call_generic_code);
16 class StoreBufferOverflowStub: public PlatformCodeStub {
18 StoreBufferOverflowStub(Isolate* isolate, SaveFPRegsMode save_fp)
19 : PlatformCodeStub(isolate), save_doubles_(save_fp) { }
21 void Generate(MacroAssembler* masm);
23 static void GenerateFixedRegStubsAheadOfTime(Isolate* isolate);
24 virtual bool SometimesSetsUpAFrame() { return false; }
27 SaveFPRegsMode save_doubles_;
29 Major MajorKey() const { return StoreBufferOverflow; }
30 int MinorKey() const { return (save_doubles_ == kSaveFPRegs) ? 1 : 0; }
34 class StringHelper : public AllStatic {
36 // Generate code for copying characters using the rep movs instruction.
37 // Copies rcx characters from rsi to rdi. Copying of overlapping regions is
39 static void GenerateCopyCharacters(MacroAssembler* masm,
43 String::Encoding encoding);
46 // Generate string hash.
47 static void GenerateHashInit(MacroAssembler* masm,
51 static void GenerateHashAddCharacter(MacroAssembler* masm,
55 static void GenerateHashGetHash(MacroAssembler* masm,
60 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
64 class SubStringStub: public PlatformCodeStub {
66 explicit SubStringStub(Isolate* isolate) : PlatformCodeStub(isolate) {}
69 Major MajorKey() const { return SubString; }
70 int MinorKey() const { return 0; }
72 void Generate(MacroAssembler* masm);
76 class StringCompareStub: public PlatformCodeStub {
78 explicit StringCompareStub(Isolate* isolate) : PlatformCodeStub(isolate) {}
80 // Compares two flat ASCII strings and returns result in rax.
81 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
89 // Compares two flat ASCII strings for equality and returns result
91 static void GenerateFlatAsciiStringEquals(MacroAssembler* masm,
98 virtual Major MajorKey() const { return StringCompare; }
99 virtual int MinorKey() const { return 0; }
100 virtual void Generate(MacroAssembler* masm);
102 static void GenerateAsciiCharsCompareLoop(
103 MacroAssembler* masm,
108 Label* chars_not_equal,
109 Label::Distance near_jump = Label::kFar);
113 class NameDictionaryLookupStub: public PlatformCodeStub {
115 enum LookupMode { POSITIVE_LOOKUP, NEGATIVE_LOOKUP };
117 NameDictionaryLookupStub(Isolate* isolate,
122 : PlatformCodeStub(isolate),
123 dictionary_(dictionary),
128 void Generate(MacroAssembler* masm);
130 static void GenerateNegativeLookup(MacroAssembler* masm,
137 static void GeneratePositiveLookup(MacroAssembler* masm,
145 virtual bool SometimesSetsUpAFrame() { return false; }
148 static const int kInlinedProbes = 4;
149 static const int kTotalProbes = 20;
151 static const int kCapacityOffset =
152 NameDictionary::kHeaderSize +
153 NameDictionary::kCapacityIndex * kPointerSize;
155 static const int kElementsStartOffset =
156 NameDictionary::kHeaderSize +
157 NameDictionary::kElementsStartIndex * kPointerSize;
159 Major MajorKey() const { return NameDictionaryLookup; }
161 int MinorKey() const {
162 return DictionaryBits::encode(dictionary_.code()) |
163 ResultBits::encode(result_.code()) |
164 IndexBits::encode(index_.code()) |
165 LookupModeBits::encode(mode_);
168 class DictionaryBits: public BitField<int, 0, 4> {};
169 class ResultBits: public BitField<int, 4, 4> {};
170 class IndexBits: public BitField<int, 8, 4> {};
171 class LookupModeBits: public BitField<LookupMode, 12, 1> {};
173 Register dictionary_;
180 class RecordWriteStub: public PlatformCodeStub {
182 RecordWriteStub(Isolate* isolate,
186 RememberedSetAction remembered_set_action,
187 SaveFPRegsMode fp_mode)
188 : PlatformCodeStub(isolate),
192 remembered_set_action_(remembered_set_action),
193 save_fp_regs_mode_(fp_mode),
194 regs_(object, // An input reg.
195 address, // An input reg.
196 value) { // One scratch reg.
202 INCREMENTAL_COMPACTION
205 virtual bool SometimesSetsUpAFrame() { return false; }
207 static const byte kTwoByteNopInstruction = 0x3c; // Cmpb al, #imm8.
208 static const byte kTwoByteJumpInstruction = 0xeb; // Jmp #imm8.
210 static const byte kFiveByteNopInstruction = 0x3d; // Cmpl eax, #imm32.
211 static const byte kFiveByteJumpInstruction = 0xe9; // Jmp #imm32.
213 static Mode GetMode(Code* stub) {
214 byte first_instruction = stub->instruction_start()[0];
215 byte second_instruction = stub->instruction_start()[2];
217 if (first_instruction == kTwoByteJumpInstruction) {
221 DCHECK(first_instruction == kTwoByteNopInstruction);
223 if (second_instruction == kFiveByteJumpInstruction) {
224 return INCREMENTAL_COMPACTION;
227 DCHECK(second_instruction == kFiveByteNopInstruction);
229 return STORE_BUFFER_ONLY;
232 static void Patch(Code* stub, Mode mode) {
234 case STORE_BUFFER_ONLY:
235 DCHECK(GetMode(stub) == INCREMENTAL ||
236 GetMode(stub) == INCREMENTAL_COMPACTION);
237 stub->instruction_start()[0] = kTwoByteNopInstruction;
238 stub->instruction_start()[2] = kFiveByteNopInstruction;
241 DCHECK(GetMode(stub) == STORE_BUFFER_ONLY);
242 stub->instruction_start()[0] = kTwoByteJumpInstruction;
244 case INCREMENTAL_COMPACTION:
245 DCHECK(GetMode(stub) == STORE_BUFFER_ONLY);
246 stub->instruction_start()[0] = kTwoByteNopInstruction;
247 stub->instruction_start()[2] = kFiveByteJumpInstruction;
250 DCHECK(GetMode(stub) == mode);
251 CpuFeatures::FlushICache(stub->instruction_start(), 7);
255 // This is a helper class for freeing up 3 scratch registers, where the third
256 // is always rcx (needed for shift operations). The input is two registers
257 // that must be preserved and one scratch register provided by the caller.
258 class RegisterAllocation {
260 RegisterAllocation(Register object,
263 : object_orig_(object),
264 address_orig_(address),
265 scratch0_orig_(scratch0),
268 scratch0_(scratch0) {
269 DCHECK(!AreAliased(scratch0, object, address, no_reg));
270 scratch1_ = GetRegThatIsNotRcxOr(object_, address_, scratch0_);
271 if (scratch0.is(rcx)) {
272 scratch0_ = GetRegThatIsNotRcxOr(object_, address_, scratch1_);
274 if (object.is(rcx)) {
275 object_ = GetRegThatIsNotRcxOr(address_, scratch0_, scratch1_);
277 if (address.is(rcx)) {
278 address_ = GetRegThatIsNotRcxOr(object_, scratch0_, scratch1_);
280 DCHECK(!AreAliased(scratch0_, object_, address_, rcx));
283 void Save(MacroAssembler* masm) {
284 DCHECK(!address_orig_.is(object_));
285 DCHECK(object_.is(object_orig_) || address_.is(address_orig_));
286 DCHECK(!AreAliased(object_, address_, scratch1_, scratch0_));
287 DCHECK(!AreAliased(object_orig_, address_, scratch1_, scratch0_));
288 DCHECK(!AreAliased(object_, address_orig_, scratch1_, scratch0_));
289 // We don't have to save scratch0_orig_ because it was given to us as
290 // a scratch register. But if we had to switch to a different reg then
291 // we should save the new scratch0_.
292 if (!scratch0_.is(scratch0_orig_)) masm->Push(scratch0_);
293 if (!rcx.is(scratch0_orig_) &&
294 !rcx.is(object_orig_) &&
295 !rcx.is(address_orig_)) {
298 masm->Push(scratch1_);
299 if (!address_.is(address_orig_)) {
300 masm->Push(address_);
301 masm->movp(address_, address_orig_);
303 if (!object_.is(object_orig_)) {
305 masm->movp(object_, object_orig_);
309 void Restore(MacroAssembler* masm) {
310 // These will have been preserved the entire time, so we just need to move
311 // them back. Only in one case is the orig_ reg different from the plain
312 // one, since only one of them can alias with rcx.
313 if (!object_.is(object_orig_)) {
314 masm->movp(object_orig_, object_);
317 if (!address_.is(address_orig_)) {
318 masm->movp(address_orig_, address_);
321 masm->Pop(scratch1_);
322 if (!rcx.is(scratch0_orig_) &&
323 !rcx.is(object_orig_) &&
324 !rcx.is(address_orig_)) {
327 if (!scratch0_.is(scratch0_orig_)) masm->Pop(scratch0_);
330 // If we have to call into C then we need to save and restore all caller-
331 // saved registers that were not already preserved.
333 // The three scratch registers (incl. rcx) will be restored by other means
334 // so we don't bother pushing them here. Rbx, rbp and r12-15 are callee
335 // save and don't need to be preserved.
336 void SaveCallerSaveRegisters(MacroAssembler* masm, SaveFPRegsMode mode) {
337 masm->PushCallerSaved(mode, scratch0_, scratch1_, rcx);
340 inline void RestoreCallerSaveRegisters(MacroAssembler*masm,
341 SaveFPRegsMode mode) {
342 masm->PopCallerSaved(mode, scratch0_, scratch1_, rcx);
345 inline Register object() { return object_; }
346 inline Register address() { return address_; }
347 inline Register scratch0() { return scratch0_; }
348 inline Register scratch1() { return scratch1_; }
351 Register object_orig_;
352 Register address_orig_;
353 Register scratch0_orig_;
358 // Third scratch register is always rcx.
360 Register GetRegThatIsNotRcxOr(Register r1,
363 for (int i = 0; i < Register::NumAllocatableRegisters(); i++) {
364 Register candidate = Register::FromAllocationIndex(i);
365 if (candidate.is(rcx)) continue;
366 if (candidate.is(r1)) continue;
367 if (candidate.is(r2)) continue;
368 if (candidate.is(r3)) continue;
374 friend class RecordWriteStub;
377 enum OnNoNeedToInformIncrementalMarker {
378 kReturnOnNoNeedToInformIncrementalMarker,
379 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker
382 void Generate(MacroAssembler* masm);
383 void GenerateIncremental(MacroAssembler* masm, Mode mode);
384 void CheckNeedsToInformIncrementalMarker(
385 MacroAssembler* masm,
386 OnNoNeedToInformIncrementalMarker on_no_need,
388 void InformIncrementalMarker(MacroAssembler* masm);
390 Major MajorKey() const { return RecordWrite; }
392 int MinorKey() const {
393 return ObjectBits::encode(object_.code()) |
394 ValueBits::encode(value_.code()) |
395 AddressBits::encode(address_.code()) |
396 RememberedSetActionBits::encode(remembered_set_action_) |
397 SaveFPRegsModeBits::encode(save_fp_regs_mode_);
400 void Activate(Code* code) {
401 code->GetHeap()->incremental_marking()->ActivateGeneratedStub(code);
404 class ObjectBits: public BitField<int, 0, 4> {};
405 class ValueBits: public BitField<int, 4, 4> {};
406 class AddressBits: public BitField<int, 8, 4> {};
407 class RememberedSetActionBits: public BitField<RememberedSetAction, 12, 1> {};
408 class SaveFPRegsModeBits: public BitField<SaveFPRegsMode, 13, 1> {};
413 RememberedSetAction remembered_set_action_;
414 SaveFPRegsMode save_fp_regs_mode_;
416 RegisterAllocation regs_;
420 } } // namespace v8::internal
422 #endif // V8_X64_CODE_STUBS_X64_H_