Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

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

Issue 2663803002: [string] Migrate String.prototype.{split,replace} to TF (Closed)
Patch Set: Rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« 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...) Expand 10 before | Expand all | Expand 10 after
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...) Expand 10 before | Expand all | Expand 10 after
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));
Igor Sheludko 2017/02/01 12:55:30 CSA will generate a code that always compute both
jgruber 2017/02/01 14:03:15 The reason I went with this form is that I'm expec
1070 }
1071
1072 void StringBuiltinsAssembler::RequireObjectCoercible(Node* const context,
1073 Node* const value,
1074 const char* method_name) {
1075 Label out(this);
Igor Sheludko 2017/02/01 12:55:30 Definitely a slow path, can probably be a deferred
jgruber 2017/02/01 14:03:14 Added a deferred label for the slow path. I suppos
1076 GotoUnless(IsNullOrUndefined(value), &out);
1077
1078 // {value} is null or undefined, throw exception.
1079
1080 TailCallRuntime(
1081 Runtime::kThrowCalledOnNullOrUndefined, context,
1082 HeapConstant(factory()->NewStringFromAsciiChecked(method_name, TENURED)));
1083
1084 Bind(&out);
1085 }
1086
1087 void StringBuiltinsAssembler::MaybeCallFunctionAtSymbol(
1088 Node* const context, Node* const object, Handle<Symbol> symbol,
1089 const NodeFunction0& regexp_call, const NodeFunction1& generic_call) {
1090 Label out(this);
1091
1092 // Smis definitely don't have an attached symbol.
1093 GotoIf(TaggedIsSmi(object), &out);
1094
1095 Node* const object_map = LoadMap(object);
1096
1097 // Skip the slow lookup for Strings.
1098 {
1099 Label next(this);
1100
1101 GotoUnless(IsStringInstanceType(LoadMapInstanceType(object_map)), &next);
1102
1103 Node* const native_context = LoadNativeContext(context);
1104 Node* const initial_proto_initial_map = LoadContextElement(
1105 native_context, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX);
1106
1107 Node* const string_fun =
1108 LoadContextElement(native_context, Context::STRING_FUNCTION_INDEX);
1109 Node* const initial_map =
1110 LoadObjectField(string_fun, JSFunction::kPrototypeOrInitialMapOffset);
1111 Node* const proto_map = LoadMap(LoadMapPrototype(initial_map));
1112
1113 Branch(WordEqual(proto_map, initial_proto_initial_map), &out, &next);
1114
1115 Bind(&next);
1116 }
1117
1118 // Take the fast path for RegExps.
1119 if (regexp_call != nullptr) {
1120 Label stub_call(this), slow_lookup(this);
1121
1122 RegExpBuiltinsAssembler regexp_asm(state());
1123 regexp_asm.BranchIfFastRegExp(context, object_map, &stub_call,
1124 &slow_lookup);
1125
1126 Bind(&stub_call);
1127 Return(regexp_call());
1128
1129 Bind(&slow_lookup);
1130 }
1131
1132 GotoIf(IsNullOrUndefined(object), &out);
1133
1134 // Fall back to a slow lookup of {object[symbol]}.
1135
1136 Callable getproperty_callable = CodeFactory::GetProperty(isolate());
1137 Node* const key = HeapConstant(symbol);
1138 Node* const maybe_func = CallStub(getproperty_callable, context, object, key);
1139
1140 GotoIf(IsUndefined(maybe_func), &out);
1141
1142 // Attempt to call the function.
1143
1144 Node* const result = generic_call(maybe_func);
1145 Return(result);
1146
1147 Bind(&out);
1148 }
1149
1150 // ES6 section 21.1.3.16 String.prototype.replace ( search, replace )
1151 TF_BUILTIN(StringPrototypeReplace, StringBuiltinsAssembler) {
1152 Label out(this);
1153
1154 Node* const receiver = Parameter(0);
1155 Node* const search = Parameter(1);
1156 Node* const replace = Parameter(2);
1157 Node* const context = Parameter(5);
1158
1159 Node* const smi_zero = SmiConstant(0);
1160
1161 RequireObjectCoercible(context, receiver, "String.prototype.replace");
1162
1163 // Redirect to replacer method if {search[@@replace]} is not undefined.
1164 // TODO(jgruber): Call RegExp.p.replace stub for fast path.
1165
1166 MaybeCallFunctionAtSymbol(
1167 context, search, isolate()->factory()->replace_symbol(), nullptr,
1168 [this, context, search, receiver, replace](Node* fn) {
Igor Sheludko 2017/02/01 12:55:30 I think it's fine to let it capture all that's nec
jgruber 2017/02/01 14:03:14 Done.
1169 Callable call_callable = CodeFactory::Call(isolate());
1170 return CallJS(call_callable, context, fn, search, receiver, replace);
1171 });
1172
1173 // Convert {receiver} and {search} to strings.
1174
1175 Callable tostring_callable = CodeFactory::ToString(isolate());
1176 Node* const subject_string = CallStub(tostring_callable, context, receiver);
Igor Sheludko 2017/02/01 12:55:30 Suggestion for another CL: how about having CallTo
jgruber 2017/02/01 14:03:14 I like it.
1177 Node* const search_string = CallStub(tostring_callable, context, search);
1178
1179 Node* const subject_length = LoadStringLength(subject_string);
1180 Node* const search_length = LoadStringLength(search_string);
1181
1182 // Fast-path single-char {search}, long {receiver}, and simple string
1183 // {replace}.
1184 {
1185 Label next(this);
1186
1187 GotoUnless(SmiEqual(search_length, SmiConstant(1)), &next);
1188 GotoUnless(SmiGreaterThan(subject_length, SmiConstant(0xFF)), &next);
1189 GotoIf(TaggedIsSmi(replace), &next);
1190 GotoUnless(IsString(replace), &next);
1191
1192 Node* const dollar_char = Int32Constant('$');
1193 Node* const index_of_dollar =
1194 StringIndexOfChar(context, replace, dollar_char, smi_zero);
1195 GotoUnless(SmiIsNegative(index_of_dollar), &next);
1196
1197 // Searching by traversing a cons string tree and replace with cons of
1198 // slices works only when the replaced string is a single character, being
1199 // replaced by a simple string and only pays off for long strings.
1200 // TODO(jgruber): Reevaluate if this is still beneficial.
1201 TailCallRuntime(Runtime::kStringReplaceOneCharWithString, context,
1202 subject_string, search_string, replace);
1203
1204 Bind(&next);
1205 }
1206
1207 // TODO(jgruber): Extend StringIndexOfChar to handle two-byte strings and
1208 // longer substrings - we can handle up to 8 chars (one-byte) / 4 chars
1209 // (2-byte).
1210
1211 Node* const match_start_index =
1212 CallRuntime(Runtime::kStringIndexOfUnchecked, context, subject_string,
Igor Sheludko 2017/02/01 12:55:30 Why not just StringBuiltinsAssembler::StringIndexO
jgruber 2017/02/01 14:03:14 Replaced with CodeFactory::StringIndexOf.
1213 search_string, smi_zero);
1214 CSA_ASSERT(this, TaggedIsSmi(match_start_index));
1215
1216 // Early exit if no match found.
1217 {
1218 Label next(this);
1219
1220 GotoUnless(SmiIsNegative(match_start_index), &next);
1221 Return(subject_string);
Igor Sheludko 2017/02/01 12:55:30 1) Maybe we should introduce ReturnIf(condition, v
jgruber 2017/02/01 14:03:15 ReturnIf sounds good! Ugh - yes that looks correc
1222
1223 Bind(&next);
1224 }
1225
1226 Node* const match_end_index = SmiAdd(match_start_index, search_length);
1227
1228 Callable substring_callable = CodeFactory::SubString(isolate());
1229 Callable stringadd_callable =
1230 CodeFactory::StringAdd(isolate(), STRING_ADD_CHECK_NONE, NOT_TENURED);
1231
1232 Variable var_result(this, MachineRepresentation::kTagged,
1233 EmptyStringConstant());
1234
1235 // Compute the prefix.
1236 {
1237 Label next(this);
1238
1239 GotoIf(SmiEqual(match_start_index, smi_zero), &next);
1240 Node* const prefix = CallStub(substring_callable, context, subject_string,
1241 smi_zero, match_start_index);
1242 var_result.Bind(prefix);
1243
1244 Goto(&next);
1245 Bind(&next);
1246 }
1247
1248 // Compute the string to replace with.
1249
1250 Label if_iscallablereplace(this), if_notcallablereplace(this);
1251 GotoIf(TaggedIsSmi(replace), &if_notcallablereplace);
1252 Branch(IsCallableMap(LoadMap(replace)), &if_iscallablereplace,
1253 &if_notcallablereplace);
1254
1255 Bind(&if_iscallablereplace);
1256 {
1257 Callable call_callable = CodeFactory::Call(isolate());
1258 Node* const replacement =
1259 CallJS(call_callable, context, replace, UndefinedConstant(),
1260 search_string, match_start_index, subject_string);
1261 Node* const replacement_string =
1262 CallStub(tostring_callable, context, replacement);
1263 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(),
1264 replacement_string));
1265 Goto(&out);
1266 }
1267
1268 Bind(&if_notcallablereplace);
1269 {
1270 Node* const replace_string = CallStub(tostring_callable, context, replace);
1271
1272 // TODO(jgruber): Simplified GetSubstitution implementation in CSA.
1273 Node* const matched = CallStub(substring_callable, context, subject_string,
1274 match_start_index, match_end_index);
1275 Node* const replacement_string =
1276 CallRuntime(Runtime::kGetSubstitution, context, matched, subject_string,
1277 match_start_index, replace_string);
1278 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(),
1279 replacement_string));
1280 Goto(&out);
1281 }
1282
1283 Bind(&out);
1284 {
1285 Node* const suffix = CallStub(substring_callable, context, subject_string,
1286 match_end_index, subject_length);
1287 Node* const result =
1288 CallStub(stringadd_callable, context, var_result.value(), suffix);
1289 Return(result);
1290 }
1291 }
1292
1293 // ES6 section 21.1.3.19 String.prototype.split ( separator, limit )
1294 TF_BUILTIN(StringPrototypeSplit, StringBuiltinsAssembler) {
1295 Label out(this);
1296
1297 Node* const receiver = Parameter(0);
1298 Node* const separator = Parameter(1);
1299 Node* const limit = Parameter(2);
1300 Node* const context = Parameter(5);
1301
1302 Node* const smi_zero = SmiConstant(0);
1303
1304 RequireObjectCoercible(context, receiver, "String.prototype.split");
1305
1306 // Redirect to splitter method if {separator[@@split]} is not undefined.
1307 // TODO(jgruber): Call RegExp.p.split stub for fast path.
1308
1309 MaybeCallFunctionAtSymbol(
1310 context, separator, isolate()->factory()->split_symbol(), nullptr,
1311 [this, context, separator, receiver, limit](Node* fn) {
Igor Sheludko 2017/02/01 12:55:30 [=]
jgruber 2017/02/01 14:03:15 Done.
1312 Callable call_callable = CodeFactory::Call(isolate());
1313 return CallJS(call_callable, context, fn, separator, receiver, limit);
1314 });
1315
1316 // String and integer conversions.
1317 // TODO(jgruber): The old implementation used Uint32Max instead of SmiMax -
1318 // but AFAIK there should not be a difference since arrays are capped at Smi
1319 // lengths.
1320
1321 Callable tostring_callable = CodeFactory::ToString(isolate());
1322 Node* const subject_string = CallStub(tostring_callable, context, receiver);
1323 Node* const limit_number = Select(
1324 IsUndefined(limit), [this]() { return SmiConstant(Smi::kMaxValue); },
Igor Sheludko 2017/02/01 12:55:30 [=]
jgruber 2017/02/01 14:03:14 Done.
1325 [this, context, limit]() { return ToUint32(context, limit); },
Igor Sheludko 2017/02/01 12:55:30 [=]
jgruber 2017/02/01 14:03:14 Done.
1326 MachineRepresentation::kTagged);
1327 Node* const separator_string =
1328 CallStub(tostring_callable, context, separator);
1329
1330 // Shortcut for {limit} == 0.
1331 {
1332 Label next(this);
1333 GotoUnless(SmiEqual(limit_number, smi_zero), &next);
1334
1335 const ElementsKind kind = FAST_ELEMENTS;
1336 const ParameterMode mode = CodeStubAssembler::INTPTR_PARAMETERS;
Igor Sheludko 2017/02/01 12:55:30 INTPTR_PARAMETERS is default, you can just skip pa
jgruber 2017/02/01 14:03:14 Done. I was thinking of refactoring common array a
1337
1338 Node* const allocation_site = nullptr;
Igor Sheludko 2017/02/01 12:55:30 This parameter is also nullptr by default, you can
jgruber 2017/02/01 14:03:14 Done.
1339 Node* const native_context = LoadNativeContext(context);
1340 Node* const array_map = LoadJSArrayElementsMap(kind, native_context);
1341
1342 Node* const length = smi_zero;
1343 Node* const capacity = IntPtrConstant(0);
1344 Node* const result = AllocateJSArray(kind, array_map, capacity, length,
1345 allocation_site, mode);
1346
1347 Return(result);
1348
1349 Bind(&next);
1350 }
1351
1352 // ECMA-262 says that if {separator} is undefined, the result should
1353 // be an array of size 1 containing the entire string.
1354 {
1355 Label next(this);
1356 GotoUnless(IsUndefined(separator), &next);
1357
1358 const ElementsKind kind = FAST_ELEMENTS;
1359 const ParameterMode mode = CodeStubAssembler::INTPTR_PARAMETERS;
Igor Sheludko 2017/02/01 12:55:30 Same here.
jgruber 2017/02/01 14:03:14 Done.
1360
1361 Node* const allocation_site = nullptr;
Igor Sheludko 2017/02/01 12:55:30 Same here.
jgruber 2017/02/01 14:03:14 Done.
1362 Node* const native_context = LoadNativeContext(context);
1363 Node* const array_map = LoadJSArrayElementsMap(kind, native_context);
1364
1365 Node* const length = SmiConstant(1);
1366 Node* const capacity = IntPtrConstant(1);
1367 Node* const result = AllocateJSArray(kind, array_map, capacity, length,
1368 allocation_site, mode);
1369
1370 Node* const fixed_array = LoadElements(result);
1371 StoreFixedArrayElement(fixed_array, 0, subject_string);
1372
1373 Return(result);
1374
1375 Bind(&next);
1376 }
1377
1378 // If the separator string is empty then return the elements in the subject.
1379 {
1380 Label next(this);
1381 GotoUnless(SmiEqual(LoadStringLength(separator_string), smi_zero), &next);
1382
1383 Node* const result = CallRuntime(Runtime::kStringToArray, context,
1384 subject_string, limit_number);
1385 Return(result);
1386
1387 Bind(&next);
1388 }
1389
1390 Node* const result =
1391 CallRuntime(Runtime::kStringSplit, context, subject_string,
1392 separator_string, limit_number);
1393 Return(result);
1394 }
1395
1041 // ES6 section B.2.3.1 String.prototype.substr ( start, length ) 1396 // ES6 section B.2.3.1 String.prototype.substr ( start, length )
1042 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) { 1397 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) {
1043 Label out(this), handle_length(this); 1398 Label out(this), handle_length(this);
1044 1399
1045 Variable var_start(this, MachineRepresentation::kTagged); 1400 Variable var_start(this, MachineRepresentation::kTagged);
1046 Variable var_length(this, MachineRepresentation::kTagged); 1401 Variable var_length(this, MachineRepresentation::kTagged);
1047 1402
1048 Node* const receiver = Parameter(0); 1403 Node* const receiver = Parameter(0);
1049 Node* const start = Parameter(1); 1404 Node* const start = Parameter(1);
1050 Node* const length = Parameter(2); 1405 Node* const length = Parameter(2);
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context, 1850 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context,
1496 HeapConstant(factory()->NewStringFromAsciiChecked( 1851 HeapConstant(factory()->NewStringFromAsciiChecked(
1497 "String Iterator.prototype.next", TENURED)), 1852 "String Iterator.prototype.next", TENURED)),
1498 iterator); 1853 iterator);
1499 Return(result); // Never reached. 1854 Return(result); // Never reached.
1500 } 1855 }
1501 } 1856 }
1502 1857
1503 } // namespace internal 1858 } // namespace internal
1504 } // namespace v8 1859 } // 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
This is Rietveld 408576698