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

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

Issue 2663803002: [string] Migrate String.prototype.{split,replace} to TF (Closed)
Patch Set: Whitelist new builtins and fix fast-regexp check 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
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 // TODO(jgruber): Fast path for Strings.
89 typedef std::function<Node*()> NodeFunction0;
90 typedef std::function<Node*(Node* fn)> NodeFunction1;
91 void MaybeCallFunctionAtSymbol(Node* const context, Node* const object,
92 Handle<Symbol> symbol,
93 const NodeFunction0& regexp_call,
94 const NodeFunction1& generic_call);
67 }; 95 };
68 96
69 void StringBuiltinsAssembler::GenerateStringEqual(ResultMode mode) { 97 void StringBuiltinsAssembler::GenerateStringEqual(ResultMode mode) {
70 // Here's pseudo-code for the algorithm below in case of kDontNegateResult 98 // Here's pseudo-code for the algorithm below in case of kDontNegateResult
71 // mode; for kNegateResult mode we properly negate the result. 99 // mode; for kNegateResult mode we properly negate the result.
72 // 100 //
73 // if (lhs == rhs) return true; 101 // if (lhs == rhs) return true;
74 // if (lhs->length() != rhs->length()) return false; 102 // if (lhs->length() != rhs->length()) return false;
75 // if (lhs->IsInternalizedString() && rhs->IsInternalizedString()) { 103 // if (lhs->IsInternalizedString() && rhs->IsInternalizedString()) {
76 // return false; 104 // return false;
(...skipping 954 matching lines...) Expand 10 before | Expand all | Expand 10 after
1031 Handle<String> valid_forms = 1059 Handle<String> valid_forms =
1032 isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD"); 1060 isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD");
1033 THROW_NEW_ERROR_RETURN_FAILURE( 1061 THROW_NEW_ERROR_RETURN_FAILURE(
1034 isolate, 1062 isolate,
1035 NewRangeError(MessageTemplate::kNormalizationForm, valid_forms)); 1063 NewRangeError(MessageTemplate::kNormalizationForm, valid_forms));
1036 } 1064 }
1037 1065
1038 return *string; 1066 return *string;
1039 } 1067 }
1040 1068
1069 compiler::Node* StringBuiltinsAssembler::IsNullOrUndefined(Node* const value) {
1070 return Word32Or(IsUndefined(value), IsNull(value));
1071 }
1072
1073 void StringBuiltinsAssembler::RequireObjectCoercible(Node* const context,
1074 Node* const value,
1075 const char* method_name) {
1076 Label out(this);
1077 GotoUnless(IsNullOrUndefined(value), &out);
1078
1079 // {value} is null or undefined, throw exception.
1080
1081 TailCallRuntime(
1082 Runtime::kThrowCalledOnNullOrUndefined, context,
1083 HeapConstant(factory()->NewStringFromAsciiChecked(method_name, TENURED)));
1084
1085 Bind(&out);
1086 }
1087
1088 void StringBuiltinsAssembler::MaybeCallFunctionAtSymbol(
1089 Node* const context, Node* const object, Handle<Symbol> symbol,
1090 const NodeFunction0& regexp_call, const NodeFunction1& generic_call) {
1091 Label out(this);
1092
1093 // Smis definitely don't have an attached symbol.
1094 GotoIf(TaggedIsSmi(object), &out);
1095
1096 // Take the fast path for RegExps.
1097 if (regexp_call != nullptr) {
1098 Label stub_call(this), slow_lookup(this);
1099 Node* const object_map = LoadMap(object);
1100
1101 RegExpBuiltinsAssembler regexp_asm(state());
1102 regexp_asm.BranchIfFastRegExp(context, object_map, &stub_call,
1103 &slow_lookup);
1104
1105 Bind(&stub_call);
1106 Return(regexp_call());
1107
1108 Bind(&slow_lookup);
1109 }
1110
1111 // TODO(jgruber): Add a fast path for strings (String.prototype is currently
1112 // not completely set up in the bootstrapper, so we can't store its initial
1113 // map from there).
1114
1115 GotoIf(IsNullOrUndefined(object), &out);
1116
1117 // Fall back to a slow lookup of {object[symbol]}.
1118
1119 Callable getproperty_callable = CodeFactory::GetProperty(isolate());
1120 Node* const key = HeapConstant(symbol);
1121 Node* const maybe_func = CallStub(getproperty_callable, context, object, key);
1122
1123 GotoIf(IsUndefined(maybe_func), &out);
1124
1125 // Attempt to call the function.
1126
1127 Node* const result = generic_call(maybe_func);
1128 Return(result);
1129
1130 Bind(&out);
1131 }
1132
1133 // ES6 section 21.1.3.16 String.prototype.replace ( search, replace )
1134 TF_BUILTIN(StringPrototypeReplace, StringBuiltinsAssembler) {
1135 Label out(this);
1136
1137 Node* const receiver = Parameter(0);
1138 Node* const search = Parameter(1);
1139 Node* const replace = Parameter(2);
1140 Node* const context = Parameter(5);
1141
1142 Node* const smi_zero = SmiConstant(0);
1143
1144 RequireObjectCoercible(context, receiver, "String.prototype.replace");
1145
1146 // Redirect to replacer method if {search[@@replace]} is not undefined.
1147 // TODO(jgruber): Call RegExp.p.replace stub for fast path.
1148
1149 MaybeCallFunctionAtSymbol(
1150 context, search, isolate()->factory()->replace_symbol(), nullptr,
1151 [this, context, search, receiver, replace](Node* fn) {
1152 Callable call_callable = CodeFactory::Call(isolate());
1153 return CallJS(call_callable, context, fn, search, receiver, replace);
1154 });
1155
1156 // Convert {receiver} and {search} to strings.
1157
1158 Callable tostring_callable = CodeFactory::ToString(isolate());
1159 Node* const subject_string = CallStub(tostring_callable, context, receiver);
1160 Node* const search_string = CallStub(tostring_callable, context, search);
1161
1162 Node* const subject_length = LoadStringLength(subject_string);
1163 Node* const search_length = LoadStringLength(search_string);
1164
1165 // Fast-path single-char {search}, long {receiver}, and simple string
1166 // {replace}.
1167 {
1168 Label next(this);
1169
1170 GotoUnless(SmiEqual(search_length, SmiConstant(1)), &next);
1171 GotoUnless(SmiGreaterThan(subject_length, SmiConstant(0xFF)), &next);
1172 GotoIf(TaggedIsSmi(replace), &next);
1173 GotoUnless(IsString(replace), &next);
1174
1175 Node* const dollar_char = Int32Constant('$');
1176 Node* const index_of_dollar =
1177 StringIndexOfChar(context, replace, dollar_char, smi_zero);
1178 GotoUnless(SmiIsNegative(index_of_dollar), &next);
1179
1180 // Searching by traversing a cons string tree and replace with cons of
1181 // slices works only when the replaced string is a single character, being
1182 // replaced by a simple string and only pays off for long strings.
1183 // TODO(jgruber): Reevaluate if this is still beneficial.
1184 TailCallRuntime(Runtime::kStringReplaceOneCharWithString, context,
1185 subject_string, search_string, replace);
1186
1187 Bind(&next);
1188 }
1189
1190 // TODO(jgruber): Extend StringIndexOfChar to handle two-byte strings and
1191 // longer substrings - we can handle up to 8 chars (one-byte) / 4 chars
1192 // (2-byte).
1193
1194 Node* const match_start_index =
1195 CallRuntime(Runtime::kStringIndexOfUnchecked, context, subject_string,
1196 search_string, smi_zero);
1197 CSA_ASSERT(this, TaggedIsSmi(match_start_index));
1198
1199 // Early exit if no match found.
1200 {
1201 Label next(this);
1202
1203 GotoUnless(SmiIsNegative(match_start_index), &next);
1204 Return(subject_string);
1205
1206 Bind(&next);
1207 }
1208
1209 Node* const match_end_index = SmiAdd(match_start_index, search_length);
1210
1211 Callable substring_callable = CodeFactory::SubString(isolate());
1212 Callable stringadd_callable =
1213 CodeFactory::StringAdd(isolate(), STRING_ADD_CHECK_NONE, NOT_TENURED);
1214
1215 Variable var_result(this, MachineRepresentation::kTagged,
1216 EmptyStringConstant());
1217
1218 // Compute the prefix.
1219 {
1220 Label next(this);
1221
1222 GotoIf(SmiEqual(match_start_index, smi_zero), &next);
1223 Node* const prefix = CallStub(substring_callable, context, subject_string,
1224 smi_zero, match_start_index);
1225 var_result.Bind(prefix);
1226
1227 Goto(&next);
1228 Bind(&next);
1229 }
1230
1231 // Compute the string to replace with.
1232
1233 Label if_iscallablereplace(this), if_notcallablereplace(this);
1234 GotoIf(TaggedIsSmi(replace), &if_notcallablereplace);
1235 Branch(IsCallableMap(LoadMap(replace)), &if_iscallablereplace,
1236 &if_notcallablereplace);
1237
1238 Bind(&if_iscallablereplace);
1239 {
1240 Callable call_callable = CodeFactory::Call(isolate());
1241 Node* const replacement =
1242 CallJS(call_callable, context, replace, UndefinedConstant(),
1243 search_string, match_start_index, subject_string);
1244 Node* const replacement_string =
1245 CallStub(tostring_callable, context, replacement);
1246 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(),
1247 replacement_string));
1248 Goto(&out);
1249 }
1250
1251 Bind(&if_notcallablereplace);
1252 {
1253 Node* const replace_string = CallStub(tostring_callable, context, replace);
1254
1255 // TODO(jgruber): Simplified GetSubstitution implementation in CSA.
1256 Node* const matched = CallStub(substring_callable, context, subject_string,
1257 match_start_index, match_end_index);
1258 Node* const replacement_string =
1259 CallRuntime(Runtime::kGetSubstitution, context, matched, subject_string,
1260 match_start_index, replace_string);
1261 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(),
1262 replacement_string));
1263 Goto(&out);
1264 }
1265
1266 Bind(&out);
1267 {
1268 Node* const suffix = CallStub(substring_callable, context, subject_string,
1269 match_end_index, subject_length);
1270 Node* const result =
1271 CallStub(stringadd_callable, context, var_result.value(), suffix);
1272 Return(result);
1273 }
1274 }
1275
1276 // ES6 section 21.1.3.19 String.prototype.split ( separator, limit )
1277 TF_BUILTIN(StringPrototypeSplit, StringBuiltinsAssembler) {
1278 Label out(this);
1279
1280 Node* const receiver = Parameter(0);
1281 Node* const separator = Parameter(1);
1282 Node* const limit = Parameter(2);
1283 Node* const context = Parameter(5);
1284
1285 Node* const smi_zero = SmiConstant(0);
1286
1287 RequireObjectCoercible(context, receiver, "String.prototype.split");
1288
1289 // Redirect to splitter method if {separator[@@split]} is not undefined.
1290 // TODO(jgruber): Call RegExp.p.split stub for fast path.
1291
1292 MaybeCallFunctionAtSymbol(
1293 context, separator, isolate()->factory()->split_symbol(), nullptr,
1294 [this, context, separator, receiver, limit](Node* fn) {
1295 Callable call_callable = CodeFactory::Call(isolate());
1296 return CallJS(call_callable, context, fn, separator, receiver, limit);
1297 });
1298
1299 // String and integer conversions.
1300 // TODO(jgruber): The old implementation used Uint32Max instead of SmiMax -
1301 // but AFAIK there should not be a difference since arrays are capped at Smi
1302 // lengths.
1303
1304 Callable tostring_callable = CodeFactory::ToString(isolate());
1305 Node* const subject_string = CallStub(tostring_callable, context, receiver);
1306 Node* const limit_number = Select(
1307 IsUndefined(limit), [this]() { return SmiConstant(Smi::kMaxValue); },
1308 [this, context, limit]() { return ToUint32(context, limit); },
1309 MachineRepresentation::kTagged);
1310 Node* const separator_string =
1311 CallStub(tostring_callable, context, separator);
1312
1313 // Shortcut for {limit} == 0.
1314 {
1315 Label next(this);
1316 GotoUnless(SmiEqual(limit_number, smi_zero), &next);
1317
1318 const ElementsKind kind = FAST_ELEMENTS;
1319 const ParameterMode mode = CodeStubAssembler::INTPTR_PARAMETERS;
1320
1321 Node* const allocation_site = nullptr;
1322 Node* const native_context = LoadNativeContext(context);
1323 Node* const array_map = LoadJSArrayElementsMap(kind, native_context);
1324
1325 Node* const length = smi_zero;
1326 Node* const capacity = IntPtrConstant(0);
1327 Node* const result = AllocateJSArray(kind, array_map, capacity, length,
1328 allocation_site, mode);
1329
1330 Return(result);
1331
1332 Bind(&next);
1333 }
1334
1335 // ECMA-262 says that if {separator} is undefined, the result should
1336 // be an array of size 1 containing the entire string.
1337 {
1338 Label next(this);
1339 GotoUnless(IsUndefined(separator), &next);
1340
1341 const ElementsKind kind = FAST_ELEMENTS;
1342 const ParameterMode mode = CodeStubAssembler::INTPTR_PARAMETERS;
1343
1344 Node* const allocation_site = nullptr;
1345 Node* const native_context = LoadNativeContext(context);
1346 Node* const array_map = LoadJSArrayElementsMap(kind, native_context);
1347
1348 Node* const length = SmiConstant(1);
1349 Node* const capacity = IntPtrConstant(1);
1350 Node* const result = AllocateJSArray(kind, array_map, capacity, length,
1351 allocation_site, mode);
1352
1353 Node* const fixed_array = LoadElements(result);
1354 StoreFixedArrayElement(fixed_array, 0, subject_string);
1355
1356 Return(result);
1357
1358 Bind(&next);
1359 }
1360
1361 // If the separator string is empty then return the elements in the subject.
1362 {
1363 Label next(this);
1364 GotoUnless(SmiEqual(LoadStringLength(separator_string), smi_zero), &next);
1365
1366 Node* const result = CallRuntime(Runtime::kStringToArray, context,
1367 subject_string, limit_number);
1368 Return(result);
1369
1370 Bind(&next);
1371 }
1372
1373 Node* const result =
1374 CallRuntime(Runtime::kStringSplit, context, subject_string,
1375 separator_string, limit_number);
1376 Return(result);
1377 }
1378
1041 // ES6 section B.2.3.1 String.prototype.substr ( start, length ) 1379 // ES6 section B.2.3.1 String.prototype.substr ( start, length )
1042 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) { 1380 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) {
1043 Label out(this), handle_length(this); 1381 Label out(this), handle_length(this);
1044 1382
1045 Variable var_start(this, MachineRepresentation::kTagged); 1383 Variable var_start(this, MachineRepresentation::kTagged);
1046 Variable var_length(this, MachineRepresentation::kTagged); 1384 Variable var_length(this, MachineRepresentation::kTagged);
1047 1385
1048 Node* const receiver = Parameter(0); 1386 Node* const receiver = Parameter(0);
1049 Node* const start = Parameter(1); 1387 Node* const start = Parameter(1);
1050 Node* const length = Parameter(2); 1388 Node* const length = Parameter(2);
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context, 1833 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context,
1496 HeapConstant(factory()->NewStringFromAsciiChecked( 1834 HeapConstant(factory()->NewStringFromAsciiChecked(
1497 "String Iterator.prototype.next", TENURED)), 1835 "String Iterator.prototype.next", TENURED)),
1498 iterator); 1836 iterator);
1499 Return(result); // Never reached. 1837 Return(result); // Never reached.
1500 } 1838 }
1501 } 1839 }
1502 1840
1503 } // namespace internal 1841 } // namespace internal
1504 } // namespace v8 1842 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698