Index: src/x64/code-stubs-x64.cc |
diff --git a/src/x64/code-stubs-x64.cc b/src/x64/code-stubs-x64.cc |
index 73cfc0d7d72a2bea1ab6d985f54b5f7cb9d96ba1..102265149555ae7ce245725c8cbf5a58e2f4defa 100644 |
--- a/src/x64/code-stubs-x64.cc |
+++ b/src/x64/code-stubs-x64.cc |
@@ -2009,6 +2009,227 @@ |
__ j(not_zero, &loop); |
__ bind(&done); |
+} |
+ |
+ |
+void SubStringStub::Generate(MacroAssembler* masm) { |
+ Label runtime; |
+ |
+ // Stack frame on entry. |
+ // rsp[0] : return address |
+ // rsp[8] : to |
+ // rsp[16] : from |
+ // rsp[24] : string |
+ |
+ enum SubStringStubArgumentIndices { |
+ STRING_ARGUMENT_INDEX, |
+ FROM_ARGUMENT_INDEX, |
+ TO_ARGUMENT_INDEX, |
+ SUB_STRING_ARGUMENT_COUNT |
+ }; |
+ |
+ StackArgumentsAccessor args(rsp, SUB_STRING_ARGUMENT_COUNT, |
+ ARGUMENTS_DONT_CONTAIN_RECEIVER); |
+ |
+ // Make sure first argument is a string. |
+ __ movp(rax, args.GetArgumentOperand(STRING_ARGUMENT_INDEX)); |
+ STATIC_ASSERT(kSmiTag == 0); |
+ __ testl(rax, Immediate(kSmiTagMask)); |
+ __ j(zero, &runtime); |
+ Condition is_string = masm->IsObjectStringType(rax, rbx, rbx); |
+ __ j(NegateCondition(is_string), &runtime); |
+ |
+ // rax: string |
+ // rbx: instance type |
+ // Calculate length of sub string using the smi values. |
+ __ movp(rcx, args.GetArgumentOperand(TO_ARGUMENT_INDEX)); |
+ __ movp(rdx, args.GetArgumentOperand(FROM_ARGUMENT_INDEX)); |
+ __ JumpUnlessBothNonNegativeSmi(rcx, rdx, &runtime); |
+ |
+ __ SmiSub(rcx, rcx, rdx); // Overflow doesn't happen. |
+ __ cmpp(rcx, FieldOperand(rax, String::kLengthOffset)); |
+ Label not_original_string; |
+ // Shorter than original string's length: an actual substring. |
+ __ j(below, ¬_original_string, Label::kNear); |
+ // Longer than original string's length or negative: unsafe arguments. |
+ __ j(above, &runtime); |
+ // Return original string. |
+ Counters* counters = isolate()->counters(); |
+ __ IncrementCounter(counters->sub_string_native(), 1); |
+ __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize); |
+ __ bind(¬_original_string); |
+ |
+ Label single_char; |
+ __ SmiCompare(rcx, Smi::FromInt(1)); |
+ __ j(equal, &single_char); |
+ |
+ __ SmiToInteger32(rcx, rcx); |
+ |
+ // rax: string |
+ // rbx: instance type |
+ // rcx: sub string length |
+ // rdx: from index (smi) |
+ // Deal with different string types: update the index if necessary |
+ // and put the underlying string into edi. |
+ Label underlying_unpacked, sliced_string, seq_or_external_string; |
+ // If the string is not indirect, it can only be sequential or external. |
+ STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag)); |
+ STATIC_ASSERT(kIsIndirectStringMask != 0); |
+ __ testb(rbx, Immediate(kIsIndirectStringMask)); |
+ __ j(zero, &seq_or_external_string, Label::kNear); |
+ |
+ __ testb(rbx, Immediate(kSlicedNotConsMask)); |
+ __ j(not_zero, &sliced_string, Label::kNear); |
+ // Cons string. Check whether it is flat, then fetch first part. |
+ // Flat cons strings have an empty second part. |
+ __ CompareRoot(FieldOperand(rax, ConsString::kSecondOffset), |
+ Heap::kempty_stringRootIndex); |
+ __ j(not_equal, &runtime); |
+ __ movp(rdi, FieldOperand(rax, ConsString::kFirstOffset)); |
+ // Update instance type. |
+ __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset)); |
+ __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset)); |
+ __ jmp(&underlying_unpacked, Label::kNear); |
+ |
+ __ bind(&sliced_string); |
+ // Sliced string. Fetch parent and correct start index by offset. |
+ __ addp(rdx, FieldOperand(rax, SlicedString::kOffsetOffset)); |
+ __ movp(rdi, FieldOperand(rax, SlicedString::kParentOffset)); |
+ // Update instance type. |
+ __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset)); |
+ __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset)); |
+ __ jmp(&underlying_unpacked, Label::kNear); |
+ |
+ __ bind(&seq_or_external_string); |
+ // Sequential or external string. Just move string to the correct register. |
+ __ movp(rdi, rax); |
+ |
+ __ bind(&underlying_unpacked); |
+ |
+ if (FLAG_string_slices) { |
+ Label copy_routine; |
+ // rdi: underlying subject string |
+ // rbx: instance type of underlying subject string |
+ // rdx: adjusted start index (smi) |
+ // rcx: length |
+ // If coming from the make_two_character_string path, the string |
+ // is too short to be sliced anyways. |
+ __ cmpp(rcx, Immediate(SlicedString::kMinLength)); |
+ // Short slice. Copy instead of slicing. |
+ __ j(less, ©_routine); |
+ // Allocate new sliced string. At this point we do not reload the instance |
+ // type including the string encoding because we simply rely on the info |
+ // provided by the original string. It does not matter if the original |
+ // string's encoding is wrong because we always have to recheck encoding of |
+ // the newly created string's parent anyways due to externalized strings. |
+ Label two_byte_slice, set_slice_header; |
+ STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0); |
+ STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0); |
+ __ testb(rbx, Immediate(kStringEncodingMask)); |
+ __ j(zero, &two_byte_slice, Label::kNear); |
+ __ AllocateOneByteSlicedString(rax, rbx, r14, &runtime); |
+ __ jmp(&set_slice_header, Label::kNear); |
+ __ bind(&two_byte_slice); |
+ __ AllocateTwoByteSlicedString(rax, rbx, r14, &runtime); |
+ __ bind(&set_slice_header); |
+ __ Integer32ToSmi(rcx, rcx); |
+ __ movp(FieldOperand(rax, SlicedString::kLengthOffset), rcx); |
+ __ movp(FieldOperand(rax, SlicedString::kHashFieldOffset), |
+ Immediate(String::kEmptyHashField)); |
+ __ movp(FieldOperand(rax, SlicedString::kParentOffset), rdi); |
+ __ movp(FieldOperand(rax, SlicedString::kOffsetOffset), rdx); |
+ __ IncrementCounter(counters->sub_string_native(), 1); |
+ __ ret(3 * kPointerSize); |
+ |
+ __ bind(©_routine); |
+ } |
+ |
+ // rdi: underlying subject string |
+ // rbx: instance type of underlying subject string |
+ // rdx: adjusted start index (smi) |
+ // rcx: length |
+ // The subject string can only be external or sequential string of either |
+ // encoding at this point. |
+ Label two_byte_sequential, sequential_string; |
+ STATIC_ASSERT(kExternalStringTag != 0); |
+ STATIC_ASSERT(kSeqStringTag == 0); |
+ __ testb(rbx, Immediate(kExternalStringTag)); |
+ __ j(zero, &sequential_string); |
+ |
+ // Handle external string. |
+ // Rule out short external strings. |
+ STATIC_ASSERT(kShortExternalStringTag != 0); |
+ __ testb(rbx, Immediate(kShortExternalStringMask)); |
+ __ j(not_zero, &runtime); |
+ __ movp(rdi, FieldOperand(rdi, ExternalString::kResourceDataOffset)); |
+ // Move the pointer so that offset-wise, it looks like a sequential string. |
+ STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize); |
+ __ subp(rdi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
+ |
+ __ bind(&sequential_string); |
+ STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0); |
+ __ testb(rbx, Immediate(kStringEncodingMask)); |
+ __ j(zero, &two_byte_sequential); |
+ |
+ // Allocate the result. |
+ __ AllocateOneByteString(rax, rcx, r11, r14, r15, &runtime); |
+ |
+ // rax: result string |
+ // rcx: result string length |
+ { // Locate character of sub string start. |
+ SmiIndex smi_as_index = masm->SmiToIndex(rdx, rdx, times_1); |
+ __ leap(r14, Operand(rdi, smi_as_index.reg, smi_as_index.scale, |
+ SeqOneByteString::kHeaderSize - kHeapObjectTag)); |
+ } |
+ // Locate first character of result. |
+ __ leap(rdi, FieldOperand(rax, SeqOneByteString::kHeaderSize)); |
+ |
+ // rax: result string |
+ // rcx: result length |
+ // r14: first character of result |
+ // rsi: character of sub string start |
+ StringHelper::GenerateCopyCharacters( |
+ masm, rdi, r14, rcx, String::ONE_BYTE_ENCODING); |
+ __ IncrementCounter(counters->sub_string_native(), 1); |
+ __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize); |
+ |
+ __ bind(&two_byte_sequential); |
+ // Allocate the result. |
+ __ AllocateTwoByteString(rax, rcx, r11, r14, r15, &runtime); |
+ |
+ // rax: result string |
+ // rcx: result string length |
+ { // Locate character of sub string start. |
+ SmiIndex smi_as_index = masm->SmiToIndex(rdx, rdx, times_2); |
+ __ leap(r14, Operand(rdi, smi_as_index.reg, smi_as_index.scale, |
+ SeqOneByteString::kHeaderSize - kHeapObjectTag)); |
+ } |
+ // Locate first character of result. |
+ __ leap(rdi, FieldOperand(rax, SeqTwoByteString::kHeaderSize)); |
+ |
+ // rax: result string |
+ // rcx: result length |
+ // rdi: first character of result |
+ // r14: character of sub string start |
+ StringHelper::GenerateCopyCharacters( |
+ masm, rdi, r14, rcx, String::TWO_BYTE_ENCODING); |
+ __ IncrementCounter(counters->sub_string_native(), 1); |
+ __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize); |
+ |
+ // Just jump to runtime to create the sub string. |
+ __ bind(&runtime); |
+ __ TailCallRuntime(Runtime::kSubString); |
+ |
+ __ bind(&single_char); |
+ // rax: string |
+ // rbx: instance type |
+ // rcx: sub string length (smi) |
+ // rdx: from index (smi) |
+ StringCharAtGenerator generator(rax, rdx, rcx, rax, &runtime, &runtime, |
+ &runtime, RECEIVER_IS_STRING); |
+ generator.GenerateFast(masm); |
+ __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize); |
+ generator.SkipSlow(masm, &runtime); |
} |
void ToStringStub::Generate(MacroAssembler* masm) { |