Chromium Code Reviews

Side by Side Diff: src/builtins/builtins-string.cc

Issue 2663803002: [string] Migrate String.prototype.{split,replace} to TF (Closed)
Patch Set: Address comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « src/builtins/builtins-regexp.cc ('k') | src/debug/debug-evaluate.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/builtins/builtins-regexp.h"
5 #include "src/builtins/builtins-utils.h" 6 #include "src/builtins/builtins-utils.h"
6 #include "src/builtins/builtins.h" 7 #include "src/builtins/builtins.h"
7 #include "src/code-factory.h" 8 #include "src/code-factory.h"
8 #include "src/code-stub-assembler.h" 9 #include "src/code-stub-assembler.h"
9 #include "src/regexp/regexp-utils.h" 10 #include "src/regexp/regexp-utils.h"
10 11
11 namespace v8 { 12 namespace v8 {
12 namespace internal { 13 namespace internal {
13 14
14 typedef CodeStubAssembler::ResultMode ResultMode; 15 typedef CodeStubAssembler::ResultMode ResultMode;
(...skipping 42 matching lines...)
57 void GenerateStringRelationalComparison(RelationalComparisonMode mode); 58 void GenerateStringRelationalComparison(RelationalComparisonMode mode);
58 59
59 Node* ToSmiBetweenZeroAnd(Node* context, Node* value, Node* limit); 60 Node* ToSmiBetweenZeroAnd(Node* context, Node* value, Node* limit);
60 61
61 Node* LoadSurrogatePairAt(Node* string, Node* length, Node* index, 62 Node* LoadSurrogatePairAt(Node* string, Node* length, Node* index,
62 UnicodeEncoding encoding); 63 UnicodeEncoding encoding);
63 64
64 void StringIndexOf(Node* receiver, Node* instance_type, Node* search_string, 65 void StringIndexOf(Node* receiver, Node* instance_type, Node* search_string,
65 Node* search_string_instance_type, Node* position, 66 Node* search_string_instance_type, Node* position,
66 std::function<void(Node*)> f_return); 67 std::function<void(Node*)> f_return);
68
69 Node* IsNullOrUndefined(Node* const value);
70 void RequireObjectCoercible(Node* const context, Node* const value,
71 const char* method_name);
72
73 Node* SmiIsNegative(Node* const value) {
74 return SmiLessThan(value, SmiConstant(0));
75 }
76
77 // Implements boilerplate logic for {match, split, replace, search} of the
78 // form:
79 //
80 // if (!IS_NULL_OR_UNDEFINED(object)) {
81 // var maybe_function = object[symbol];
82 // if (!IS_UNDEFINED(maybe_function)) {
83 // return %_Call(maybe_function, ...);
84 // }
85 // }
86 //
87 // Contains fast paths for Smi and RegExp objects.
88 typedef std::function<Node*()> NodeFunction0;
89 typedef std::function<Node*(Node* fn)> NodeFunction1;
90 void MaybeCallFunctionAtSymbol(Node* const context, Node* const object,
91 Handle<Symbol> symbol,
92 const NodeFunction0& regexp_call,
93 const NodeFunction1& generic_call);
67 }; 94 };
68 95
69 void StringBuiltinsAssembler::GenerateStringEqual(ResultMode mode) { 96 void StringBuiltinsAssembler::GenerateStringEqual(ResultMode mode) {
70 // Here's pseudo-code for the algorithm below in case of kDontNegateResult 97 // Here's pseudo-code for the algorithm below in case of kDontNegateResult
71 // mode; for kNegateResult mode we properly negate the result. 98 // mode; for kNegateResult mode we properly negate the result.
72 // 99 //
73 // if (lhs == rhs) return true; 100 // if (lhs == rhs) return true;
74 // if (lhs->length() != rhs->length()) return false; 101 // if (lhs->length() != rhs->length()) return false;
75 // if (lhs->IsInternalizedString() && rhs->IsInternalizedString()) { 102 // if (lhs->IsInternalizedString() && rhs->IsInternalizedString()) {
76 // return false; 103 // return false;
(...skipping 954 matching lines...)
1031 Handle<String> valid_forms = 1058 Handle<String> valid_forms =
1032 isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD"); 1059 isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD");
1033 THROW_NEW_ERROR_RETURN_FAILURE( 1060 THROW_NEW_ERROR_RETURN_FAILURE(
1034 isolate, 1061 isolate,
1035 NewRangeError(MessageTemplate::kNormalizationForm, valid_forms)); 1062 NewRangeError(MessageTemplate::kNormalizationForm, valid_forms));
1036 } 1063 }
1037 1064
1038 return *string; 1065 return *string;
1039 } 1066 }
1040 1067
1068 compiler::Node* StringBuiltinsAssembler::IsNullOrUndefined(Node* const value) {
1069 return Word32Or(IsUndefined(value), IsNull(value));
1070 }
1071
1072 void StringBuiltinsAssembler::RequireObjectCoercible(Node* const context,
1073 Node* const value,
1074 const char* method_name) {
1075 Label out(this), throw_exception(this, Label::kDeferred);
1076 Branch(IsNullOrUndefined(value), &throw_exception, &out);
1077
1078 Bind(&throw_exception);
1079 TailCallRuntime(
1080 Runtime::kThrowCalledOnNullOrUndefined, context,
1081 HeapConstant(factory()->NewStringFromAsciiChecked(method_name, TENURED)));
1082
1083 Bind(&out);
1084 }
1085
1086 void StringBuiltinsAssembler::MaybeCallFunctionAtSymbol(
1087 Node* const context, Node* const object, Handle<Symbol> symbol,
1088 const NodeFunction0& regexp_call, const NodeFunction1& generic_call) {
1089 Label out(this);
1090
1091 // Smis definitely don't have an attached symbol.
1092 GotoIf(TaggedIsSmi(object), &out);
1093
1094 Node* const object_map = LoadMap(object);
1095
1096 // Skip the slow lookup for Strings.
1097 {
1098 Label next(this);
1099
1100 GotoUnless(IsStringInstanceType(LoadMapInstanceType(object_map)), &next);
1101
1102 Node* const native_context = LoadNativeContext(context);
1103 Node* const initial_proto_initial_map = LoadContextElement(
1104 native_context, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX);
1105
1106 Node* const string_fun =
1107 LoadContextElement(native_context, Context::STRING_FUNCTION_INDEX);
1108 Node* const initial_map =
1109 LoadObjectField(string_fun, JSFunction::kPrototypeOrInitialMapOffset);
1110 Node* const proto_map = LoadMap(LoadMapPrototype(initial_map));
1111
1112 Branch(WordEqual(proto_map, initial_proto_initial_map), &out, &next);
1113
1114 Bind(&next);
1115 }
1116
1117 // Take the fast path for RegExps.
1118 if (regexp_call != nullptr) {
1119 Label stub_call(this), slow_lookup(this);
1120
1121 RegExpBuiltinsAssembler regexp_asm(state());
1122 regexp_asm.BranchIfFastRegExp(context, object_map, &stub_call,
1123 &slow_lookup);
1124
1125 Bind(&stub_call);
1126 Return(regexp_call());
1127
1128 Bind(&slow_lookup);
1129 }
1130
1131 GotoIf(IsNullOrUndefined(object), &out);
1132
1133 // Fall back to a slow lookup of {object[symbol]}.
1134
1135 Callable getproperty_callable = CodeFactory::GetProperty(isolate());
1136 Node* const key = HeapConstant(symbol);
1137 Node* const maybe_func = CallStub(getproperty_callable, context, object, key);
1138
1139 GotoIf(IsUndefined(maybe_func), &out);
1140
1141 // Attempt to call the function.
1142
1143 Node* const result = generic_call(maybe_func);
1144 Return(result);
1145
1146 Bind(&out);
1147 }
1148
1149 // ES6 section 21.1.3.16 String.prototype.replace ( search, replace )
1150 TF_BUILTIN(StringPrototypeReplace, StringBuiltinsAssembler) {
1151 Label out(this);
1152
1153 Node* const receiver = Parameter(0);
1154 Node* const search = Parameter(1);
1155 Node* const replace = Parameter(2);
1156 Node* const context = Parameter(5);
1157
1158 Node* const smi_zero = SmiConstant(0);
1159
1160 RequireObjectCoercible(context, receiver, "String.prototype.replace");
1161
1162 // Redirect to replacer method if {search[@@replace]} is not undefined.
1163 // TODO(jgruber): Call RegExp.p.replace stub for fast path.
1164
1165 MaybeCallFunctionAtSymbol(
1166 context, search, isolate()->factory()->replace_symbol(), nullptr,
1167 [=](Node* fn) {
1168 Callable call_callable = CodeFactory::Call(isolate());
1169 return CallJS(call_callable, context, fn, search, receiver, replace);
1170 });
1171
1172 // Convert {receiver} and {search} to strings.
1173
1174 Callable tostring_callable = CodeFactory::ToString(isolate());
1175 Node* const subject_string = CallStub(tostring_callable, context, receiver);
1176 Node* const search_string = CallStub(tostring_callable, context, search);
1177
1178 Node* const subject_length = LoadStringLength(subject_string);
1179 Node* const search_length = LoadStringLength(search_string);
1180
1181 // Fast-path single-char {search}, long {receiver}, and simple string
1182 // {replace}.
1183 {
1184 Label next(this);
1185
1186 GotoUnless(SmiEqual(search_length, SmiConstant(1)), &next);
1187 GotoUnless(SmiGreaterThan(subject_length, SmiConstant(0xFF)), &next);
1188 GotoIf(TaggedIsSmi(replace), &next);
1189 GotoUnless(IsString(replace), &next);
1190
1191 Node* const dollar_char = Int32Constant('$');
1192 Node* const index_of_dollar =
1193 StringIndexOfChar(context, replace, dollar_char, smi_zero);
1194 GotoUnless(SmiIsNegative(index_of_dollar), &next);
1195
1196 // Searching by traversing a cons string tree and replace with cons of
1197 // slices works only when the replaced string is a single character, being
1198 // replaced by a simple string and only pays off for long strings.
1199 // TODO(jgruber): Reevaluate if this is still beneficial.
1200 TailCallRuntime(Runtime::kStringReplaceOneCharWithString, context,
1201 subject_string, search_string, replace);
1202
1203 Bind(&next);
1204 }
1205
1206 // TODO(jgruber): Extend StringIndexOfChar to handle two-byte strings and
1207 // longer substrings - we can handle up to 8 chars (one-byte) / 4 chars
1208 // (2-byte).
1209
1210 Callable indexof_stub = CodeFactory::StringIndexOf(isolate());
1211 Node* const match_start_index =
1212 CallStub(indexof_stub, context, subject_string, search_string, smi_zero);
1213 CSA_ASSERT(this, TaggedIsSmi(match_start_index));
1214
1215 Node* const replace_string = CallStub(tostring_callable, context, replace);
Igor Sheludko 2017/02/01 14:12:14 We should not call it in case of callable "replace
jgruber 2017/02/01 15:11:30 Good catch. I moved the ToString call into the ear
1216
1217 // Early exit if no match found.
1218 {
1219 Label next(this);
1220
1221 GotoUnless(SmiIsNegative(match_start_index), &next);
1222 Return(subject_string);
1223
1224 Bind(&next);
1225 }
1226
1227 Node* const match_end_index = SmiAdd(match_start_index, search_length);
1228
1229 Callable substring_callable = CodeFactory::SubString(isolate());
1230 Callable stringadd_callable =
1231 CodeFactory::StringAdd(isolate(), STRING_ADD_CHECK_NONE, NOT_TENURED);
1232
1233 Variable var_result(this, MachineRepresentation::kTagged,
1234 EmptyStringConstant());
1235
1236 // Compute the prefix.
1237 {
1238 Label next(this);
1239
1240 GotoIf(SmiEqual(match_start_index, smi_zero), &next);
1241 Node* const prefix = CallStub(substring_callable, context, subject_string,
1242 smi_zero, match_start_index);
1243 var_result.Bind(prefix);
1244
1245 Goto(&next);
1246 Bind(&next);
1247 }
1248
1249 // Compute the string to replace with.
1250
1251 Label if_iscallablereplace(this), if_notcallablereplace(this);
1252 GotoIf(TaggedIsSmi(replace), &if_notcallablereplace);
1253 Branch(IsCallableMap(LoadMap(replace)), &if_iscallablereplace,
1254 &if_notcallablereplace);
1255
1256 Bind(&if_iscallablereplace);
1257 {
1258 Callable call_callable = CodeFactory::Call(isolate());
1259 Node* const replacement =
1260 CallJS(call_callable, context, replace, UndefinedConstant(),
1261 search_string, match_start_index, subject_string);
1262 Node* const replacement_string =
1263 CallStub(tostring_callable, context, replacement);
1264 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(),
1265 replacement_string));
1266 Goto(&out);
1267 }
1268
1269 Bind(&if_notcallablereplace);
1270 {
1271 // TODO(jgruber): Simplified GetSubstitution implementation in CSA.
1272 Node* const matched = CallStub(substring_callable, context, subject_string,
1273 match_start_index, match_end_index);
1274 Node* const replacement_string =
1275 CallRuntime(Runtime::kGetSubstitution, context, matched, subject_string,
1276 match_start_index, replace_string);
1277 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(),
1278 replacement_string));
1279 Goto(&out);
1280 }
1281
1282 Bind(&out);
1283 {
1284 Node* const suffix = CallStub(substring_callable, context, subject_string,
1285 match_end_index, subject_length);
1286 Node* const result =
1287 CallStub(stringadd_callable, context, var_result.value(), suffix);
1288 Return(result);
1289 }
1290 }
1291
1292 // ES6 section 21.1.3.19 String.prototype.split ( separator, limit )
1293 TF_BUILTIN(StringPrototypeSplit, StringBuiltinsAssembler) {
1294 Label out(this);
1295
1296 Node* const receiver = Parameter(0);
1297 Node* const separator = Parameter(1);
1298 Node* const limit = Parameter(2);
1299 Node* const context = Parameter(5);
1300
1301 Node* const smi_zero = SmiConstant(0);
1302
1303 RequireObjectCoercible(context, receiver, "String.prototype.split");
1304
1305 // Redirect to splitter method if {separator[@@split]} is not undefined.
1306 // TODO(jgruber): Call RegExp.p.split stub for fast path.
1307
1308 MaybeCallFunctionAtSymbol(
1309 context, separator, isolate()->factory()->split_symbol(), nullptr,
1310 [=](Node* fn) {
1311 Callable call_callable = CodeFactory::Call(isolate());
1312 return CallJS(call_callable, context, fn, separator, receiver, limit);
1313 });
1314
1315 // String and integer conversions.
1316 // TODO(jgruber): The old implementation used Uint32Max instead of SmiMax -
1317 // but AFAIK there should not be a difference since arrays are capped at Smi
1318 // lengths.
1319
1320 Callable tostring_callable = CodeFactory::ToString(isolate());
1321 Node* const subject_string = CallStub(tostring_callable, context, receiver);
1322 Node* const limit_number =
1323 Select(IsUndefined(limit), [=]() { return SmiConstant(Smi::kMaxValue); },
1324 [=]() { return ToUint32(context, limit); },
1325 MachineRepresentation::kTagged);
1326 Node* const separator_string =
1327 CallStub(tostring_callable, context, separator);
1328
1329 // Shortcut for {limit} == 0.
1330 {
1331 Label next(this);
1332 GotoUnless(SmiEqual(limit_number, smi_zero), &next);
1333
1334 const ElementsKind kind = FAST_ELEMENTS;
1335 Node* const native_context = LoadNativeContext(context);
1336 Node* const array_map = LoadJSArrayElementsMap(kind, native_context);
1337
1338 Node* const length = smi_zero;
1339 Node* const capacity = IntPtrConstant(0);
1340 Node* const result = AllocateJSArray(kind, array_map, capacity, length);
1341
1342 Return(result);
1343
1344 Bind(&next);
1345 }
1346
1347 // ECMA-262 says that if {separator} is undefined, the result should
1348 // be an array of size 1 containing the entire string.
1349 {
1350 Label next(this);
1351 GotoUnless(IsUndefined(separator), &next);
1352
1353 const ElementsKind kind = FAST_ELEMENTS;
1354 Node* const native_context = LoadNativeContext(context);
1355 Node* const array_map = LoadJSArrayElementsMap(kind, native_context);
1356
1357 Node* const length = SmiConstant(1);
1358 Node* const capacity = IntPtrConstant(1);
1359 Node* const result = AllocateJSArray(kind, array_map, capacity, length);
1360
1361 Node* const fixed_array = LoadElements(result);
1362 StoreFixedArrayElement(fixed_array, 0, subject_string);
1363
1364 Return(result);
1365
1366 Bind(&next);
1367 }
1368
1369 // If the separator string is empty then return the elements in the subject.
1370 {
1371 Label next(this);
1372 GotoUnless(SmiEqual(LoadStringLength(separator_string), smi_zero), &next);
1373
1374 Node* const result = CallRuntime(Runtime::kStringToArray, context,
1375 subject_string, limit_number);
1376 Return(result);
1377
1378 Bind(&next);
1379 }
1380
1381 Node* const result =
1382 CallRuntime(Runtime::kStringSplit, context, subject_string,
1383 separator_string, limit_number);
1384 Return(result);
1385 }
1386
1041 // ES6 section B.2.3.1 String.prototype.substr ( start, length ) 1387 // ES6 section B.2.3.1 String.prototype.substr ( start, length )
1042 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) { 1388 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) {
1043 Label out(this), handle_length(this); 1389 Label out(this), handle_length(this);
1044 1390
1045 Variable var_start(this, MachineRepresentation::kTagged); 1391 Variable var_start(this, MachineRepresentation::kTagged);
1046 Variable var_length(this, MachineRepresentation::kTagged); 1392 Variable var_length(this, MachineRepresentation::kTagged);
1047 1393
1048 Node* const receiver = Parameter(0); 1394 Node* const receiver = Parameter(0);
1049 Node* const start = Parameter(1); 1395 Node* const start = Parameter(1);
1050 Node* const length = Parameter(2); 1396 Node* const length = Parameter(2);
(...skipping 444 matching lines...)
1495 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context, 1841 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context,
1496 HeapConstant(factory()->NewStringFromAsciiChecked( 1842 HeapConstant(factory()->NewStringFromAsciiChecked(
1497 "String Iterator.prototype.next", TENURED)), 1843 "String Iterator.prototype.next", TENURED)),
1498 iterator); 1844 iterator);
1499 Return(result); // Never reached. 1845 Return(result); // Never reached.
1500 } 1846 }
1501 } 1847 }
1502 1848
1503 } // namespace internal 1849 } // namespace internal
1504 } // namespace v8 1850 } // namespace v8
OLDNEW
« no previous file with comments | « src/builtins/builtins-regexp.cc ('k') | src/debug/debug-evaluate.cc » ('j') | no next file with comments »

Powered by Google App Engine