OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_INTL_SUPPORT |
| 6 #error Internationalization is expected to be enabled. |
| 7 #endif // V8_INTL_SUPPORT |
| 8 |
| 9 #include "src/builtins/builtins-utils-gen.h" |
| 10 #include "src/code-stub-assembler.h" |
| 11 |
| 12 namespace v8 { |
| 13 namespace internal { |
| 14 |
| 15 class IntlBuiltinsAssembler : public CodeStubAssembler { |
| 16 public: |
| 17 explicit IntlBuiltinsAssembler(compiler::CodeAssemblerState* state) |
| 18 : CodeStubAssembler(state) {} |
| 19 }; |
| 20 |
| 21 TF_BUILTIN(StringPrototypeToLowerCaseIntl, IntlBuiltinsAssembler) { |
| 22 Node* const maybe_string = Parameter(Descriptor::kReceiver); |
| 23 Node* const context = Parameter(Descriptor::kContext); |
| 24 |
| 25 Node* const string = |
| 26 ToThisString(context, maybe_string, "String.prototype.toLowerCase"); |
| 27 |
| 28 Label call_c(this), return_string(this), runtime(this, Label::kDeferred); |
| 29 |
| 30 // Early exit on empty strings. |
| 31 Node* const length = SmiUntag(LoadStringLength(string)); |
| 32 GotoIf(IntPtrEqual(length, IntPtrConstant(0)), &return_string); |
| 33 |
| 34 // Unpack strings if possible, and bail to runtime unless we get a one-byte |
| 35 // flat string. |
| 36 ToDirectStringAssembler to_direct( |
| 37 state(), string, ToDirectStringAssembler::kDontUnpackSlicedStrings); |
| 38 to_direct.TryToDirect(&runtime); |
| 39 |
| 40 Node* const instance_type = to_direct.instance_type(); |
| 41 CSA_ASSERT(this, |
| 42 Word32BinaryNot(IsIndirectStringInstanceType(instance_type))); |
| 43 GotoIfNot(IsOneByteStringInstanceType(instance_type), &runtime); |
| 44 |
| 45 // For short strings, do the conversion in CSA through the lookup table. |
| 46 |
| 47 Node* const dst = AllocateSeqOneByteString(context, length); |
| 48 |
| 49 const int kMaxShortStringLength = 24; // Determined empirically. |
| 50 GotoIf(IntPtrGreaterThan(length, IntPtrConstant(kMaxShortStringLength)), |
| 51 &call_c); |
| 52 |
| 53 { |
| 54 Node* const dst_ptr = PointerToSeqStringData(dst); |
| 55 VARIABLE(var_cursor, MachineType::PointerRepresentation(), |
| 56 IntPtrConstant(0)); |
| 57 |
| 58 Node* const start_address = to_direct.PointerToData(&call_c); |
| 59 Node* const end_address = IntPtrAdd(start_address, length); |
| 60 |
| 61 Node* const to_lower_table_addr = ExternalConstant( |
| 62 ExternalReference::intl_to_latin1_lower_table(isolate())); |
| 63 |
| 64 VariableList push_vars({&var_cursor}, zone()); |
| 65 BuildFastLoop( |
| 66 push_vars, start_address, end_address, |
| 67 [=, &var_cursor](Node* current) { |
| 68 Node* c = ChangeInt32ToIntPtr(Load(MachineType::Uint8(), current)); |
| 69 Node* lower = Load(MachineType::Uint8(), to_lower_table_addr, c); |
| 70 StoreNoWriteBarrier(MachineRepresentation::kWord8, dst_ptr, |
| 71 var_cursor.value(), lower); |
| 72 Increment(var_cursor); |
| 73 }, |
| 74 kCharSize, INTPTR_PARAMETERS, IndexAdvanceMode::kPost); |
| 75 |
| 76 // All lower-case. |
| 77 Return(dst); |
| 78 } |
| 79 |
| 80 // Call into C for case conversion. The signature is: |
| 81 // Object* ConvertOneByteToLower(String* src, String* dst, Isolate* isolate); |
| 82 BIND(&call_c); |
| 83 { |
| 84 Node* const src = to_direct.string(); |
| 85 |
| 86 Node* const function_addr = ExternalConstant( |
| 87 ExternalReference::intl_convert_one_byte_to_lower(isolate())); |
| 88 Node* const isolate_ptr = |
| 89 ExternalConstant(ExternalReference::isolate_address(isolate())); |
| 90 |
| 91 MachineType type_ptr = MachineType::Pointer(); |
| 92 MachineType type_tagged = MachineType::AnyTagged(); |
| 93 |
| 94 Node* const result = |
| 95 CallCFunction3(type_tagged, type_tagged, type_tagged, type_ptr, |
| 96 function_addr, src, dst, isolate_ptr); |
| 97 |
| 98 Return(result); |
| 99 } |
| 100 |
| 101 BIND(&return_string); |
| 102 Return(string); |
| 103 |
| 104 BIND(&runtime); |
| 105 { |
| 106 Node* const result = |
| 107 CallRuntime(Runtime::kStringToLowerCaseIntl, context, string); |
| 108 Return(result); |
| 109 } |
| 110 } |
| 111 |
| 112 } // namespace internal |
| 113 } // namespace v8 |
OLD | NEW |