OLD | NEW |
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" | |
6 #include "src/builtins/builtins-utils.h" | 5 #include "src/builtins/builtins-utils.h" |
7 #include "src/builtins/builtins.h" | 6 #include "src/builtins/builtins.h" |
8 #include "src/code-factory.h" | 7 #include "src/code-factory.h" |
9 #include "src/code-stub-assembler.h" | 8 #include "src/code-stub-assembler.h" |
10 #include "src/regexp/regexp-utils.h" | 9 #include "src/regexp/regexp-utils.h" |
11 | 10 |
12 namespace v8 { | 11 namespace v8 { |
13 namespace internal { | 12 namespace internal { |
14 | 13 |
15 typedef CodeStubAssembler::ResultMode ResultMode; | 14 typedef CodeStubAssembler::ResultMode ResultMode; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 void GenerateStringRelationalComparison(RelationalComparisonMode mode); | 57 void GenerateStringRelationalComparison(RelationalComparisonMode mode); |
59 | 58 |
60 Node* ToSmiBetweenZeroAnd(Node* context, Node* value, Node* limit); | 59 Node* ToSmiBetweenZeroAnd(Node* context, Node* value, Node* limit); |
61 | 60 |
62 Node* LoadSurrogatePairAt(Node* string, Node* length, Node* index, | 61 Node* LoadSurrogatePairAt(Node* string, Node* length, Node* index, |
63 UnicodeEncoding encoding); | 62 UnicodeEncoding encoding); |
64 | 63 |
65 void StringIndexOf(Node* receiver, Node* instance_type, Node* search_string, | 64 void StringIndexOf(Node* receiver, Node* instance_type, Node* search_string, |
66 Node* search_string_instance_type, Node* position, | 65 Node* search_string_instance_type, Node* position, |
67 std::function<void(Node*)> f_return); | 66 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); | |
94 }; | 67 }; |
95 | 68 |
96 void StringBuiltinsAssembler::GenerateStringEqual(ResultMode mode) { | 69 void StringBuiltinsAssembler::GenerateStringEqual(ResultMode mode) { |
97 // Here's pseudo-code for the algorithm below in case of kDontNegateResult | 70 // Here's pseudo-code for the algorithm below in case of kDontNegateResult |
98 // mode; for kNegateResult mode we properly negate the result. | 71 // mode; for kNegateResult mode we properly negate the result. |
99 // | 72 // |
100 // if (lhs == rhs) return true; | 73 // if (lhs == rhs) return true; |
101 // if (lhs->length() != rhs->length()) return false; | 74 // if (lhs->length() != rhs->length()) return false; |
102 // if (lhs->IsInternalizedString() && rhs->IsInternalizedString()) { | 75 // if (lhs->IsInternalizedString() && rhs->IsInternalizedString()) { |
103 // return false; | 76 // return false; |
(...skipping 954 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 Handle<String> valid_forms = | 1031 Handle<String> valid_forms = |
1059 isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD"); | 1032 isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD"); |
1060 THROW_NEW_ERROR_RETURN_FAILURE( | 1033 THROW_NEW_ERROR_RETURN_FAILURE( |
1061 isolate, | 1034 isolate, |
1062 NewRangeError(MessageTemplate::kNormalizationForm, valid_forms)); | 1035 NewRangeError(MessageTemplate::kNormalizationForm, valid_forms)); |
1063 } | 1036 } |
1064 | 1037 |
1065 return *string; | 1038 return *string; |
1066 } | 1039 } |
1067 | 1040 |
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 // Early exit if no match found. | |
1216 { | |
1217 Label next(this), return_subject(this); | |
1218 | |
1219 GotoUnless(SmiIsNegative(match_start_index), &next); | |
1220 | |
1221 // The spec requires to perform ToString(replace) if the {replace} is not | |
1222 // callable even if we are going to exit here. | |
1223 // Since ToString() being applied to Smi does not have side effects for | |
1224 // numbers we can skip it. | |
1225 GotoIf(TaggedIsSmi(replace), &return_subject); | |
1226 GotoIf(IsCallableMap(LoadMap(replace)), &return_subject); | |
1227 | |
1228 // TODO(jgruber): Could introduce ToStringSideeffectsStub which only | |
1229 // performs observable parts of ToString. | |
1230 CallStub(tostring_callable, context, replace); | |
1231 Goto(&return_subject); | |
1232 | |
1233 Bind(&return_subject); | |
1234 Return(subject_string); | |
1235 | |
1236 Bind(&next); | |
1237 } | |
1238 | |
1239 Node* const match_end_index = SmiAdd(match_start_index, search_length); | |
1240 | |
1241 Callable substring_callable = CodeFactory::SubString(isolate()); | |
1242 Callable stringadd_callable = | |
1243 CodeFactory::StringAdd(isolate(), STRING_ADD_CHECK_NONE, NOT_TENURED); | |
1244 | |
1245 Variable var_result(this, MachineRepresentation::kTagged, | |
1246 EmptyStringConstant()); | |
1247 | |
1248 // Compute the prefix. | |
1249 { | |
1250 Label next(this); | |
1251 | |
1252 GotoIf(SmiEqual(match_start_index, smi_zero), &next); | |
1253 Node* const prefix = CallStub(substring_callable, context, subject_string, | |
1254 smi_zero, match_start_index); | |
1255 var_result.Bind(prefix); | |
1256 | |
1257 Goto(&next); | |
1258 Bind(&next); | |
1259 } | |
1260 | |
1261 // Compute the string to replace with. | |
1262 | |
1263 Label if_iscallablereplace(this), if_notcallablereplace(this); | |
1264 GotoIf(TaggedIsSmi(replace), &if_notcallablereplace); | |
1265 Branch(IsCallableMap(LoadMap(replace)), &if_iscallablereplace, | |
1266 &if_notcallablereplace); | |
1267 | |
1268 Bind(&if_iscallablereplace); | |
1269 { | |
1270 Callable call_callable = CodeFactory::Call(isolate()); | |
1271 Node* const replacement = | |
1272 CallJS(call_callable, context, replace, UndefinedConstant(), | |
1273 search_string, match_start_index, subject_string); | |
1274 Node* const replacement_string = | |
1275 CallStub(tostring_callable, context, replacement); | |
1276 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(), | |
1277 replacement_string)); | |
1278 Goto(&out); | |
1279 } | |
1280 | |
1281 Bind(&if_notcallablereplace); | |
1282 { | |
1283 Node* const replace_string = CallStub(tostring_callable, context, replace); | |
1284 | |
1285 // TODO(jgruber): Simplified GetSubstitution implementation in CSA. | |
1286 Node* const matched = CallStub(substring_callable, context, subject_string, | |
1287 match_start_index, match_end_index); | |
1288 Node* const replacement_string = | |
1289 CallRuntime(Runtime::kGetSubstitution, context, matched, subject_string, | |
1290 match_start_index, replace_string); | |
1291 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(), | |
1292 replacement_string)); | |
1293 Goto(&out); | |
1294 } | |
1295 | |
1296 Bind(&out); | |
1297 { | |
1298 Node* const suffix = CallStub(substring_callable, context, subject_string, | |
1299 match_end_index, subject_length); | |
1300 Node* const result = | |
1301 CallStub(stringadd_callable, context, var_result.value(), suffix); | |
1302 Return(result); | |
1303 } | |
1304 } | |
1305 | |
1306 // ES6 section 21.1.3.19 String.prototype.split ( separator, limit ) | |
1307 TF_BUILTIN(StringPrototypeSplit, StringBuiltinsAssembler) { | |
1308 Label out(this); | |
1309 | |
1310 Node* const receiver = Parameter(0); | |
1311 Node* const separator = Parameter(1); | |
1312 Node* const limit = Parameter(2); | |
1313 Node* const context = Parameter(5); | |
1314 | |
1315 Node* const smi_zero = SmiConstant(0); | |
1316 | |
1317 RequireObjectCoercible(context, receiver, "String.prototype.split"); | |
1318 | |
1319 // Redirect to splitter method if {separator[@@split]} is not undefined. | |
1320 // TODO(jgruber): Call RegExp.p.split stub for fast path. | |
1321 | |
1322 MaybeCallFunctionAtSymbol( | |
1323 context, separator, isolate()->factory()->split_symbol(), nullptr, | |
1324 [=](Node* fn) { | |
1325 Callable call_callable = CodeFactory::Call(isolate()); | |
1326 return CallJS(call_callable, context, fn, separator, receiver, limit); | |
1327 }); | |
1328 | |
1329 // String and integer conversions. | |
1330 // TODO(jgruber): The old implementation used Uint32Max instead of SmiMax - | |
1331 // but AFAIK there should not be a difference since arrays are capped at Smi | |
1332 // lengths. | |
1333 | |
1334 Callable tostring_callable = CodeFactory::ToString(isolate()); | |
1335 Node* const subject_string = CallStub(tostring_callable, context, receiver); | |
1336 Node* const limit_number = | |
1337 Select(IsUndefined(limit), [=]() { return SmiConstant(Smi::kMaxValue); }, | |
1338 [=]() { return ToUint32(context, limit); }, | |
1339 MachineRepresentation::kTagged); | |
1340 Node* const separator_string = | |
1341 CallStub(tostring_callable, context, separator); | |
1342 | |
1343 // Shortcut for {limit} == 0. | |
1344 { | |
1345 Label next(this); | |
1346 GotoUnless(SmiEqual(limit_number, smi_zero), &next); | |
1347 | |
1348 const ElementsKind kind = FAST_ELEMENTS; | |
1349 Node* const native_context = LoadNativeContext(context); | |
1350 Node* const array_map = LoadJSArrayElementsMap(kind, native_context); | |
1351 | |
1352 Node* const length = smi_zero; | |
1353 Node* const capacity = IntPtrConstant(0); | |
1354 Node* const result = AllocateJSArray(kind, array_map, capacity, length); | |
1355 | |
1356 Return(result); | |
1357 | |
1358 Bind(&next); | |
1359 } | |
1360 | |
1361 // ECMA-262 says that if {separator} is undefined, the result should | |
1362 // be an array of size 1 containing the entire string. | |
1363 { | |
1364 Label next(this); | |
1365 GotoUnless(IsUndefined(separator), &next); | |
1366 | |
1367 const ElementsKind kind = FAST_ELEMENTS; | |
1368 Node* const native_context = LoadNativeContext(context); | |
1369 Node* const array_map = LoadJSArrayElementsMap(kind, native_context); | |
1370 | |
1371 Node* const length = SmiConstant(1); | |
1372 Node* const capacity = IntPtrConstant(1); | |
1373 Node* const result = AllocateJSArray(kind, array_map, capacity, length); | |
1374 | |
1375 Node* const fixed_array = LoadElements(result); | |
1376 StoreFixedArrayElement(fixed_array, 0, subject_string); | |
1377 | |
1378 Return(result); | |
1379 | |
1380 Bind(&next); | |
1381 } | |
1382 | |
1383 // If the separator string is empty then return the elements in the subject. | |
1384 { | |
1385 Label next(this); | |
1386 GotoUnless(SmiEqual(LoadStringLength(separator_string), smi_zero), &next); | |
1387 | |
1388 Node* const result = CallRuntime(Runtime::kStringToArray, context, | |
1389 subject_string, limit_number); | |
1390 Return(result); | |
1391 | |
1392 Bind(&next); | |
1393 } | |
1394 | |
1395 Node* const result = | |
1396 CallRuntime(Runtime::kStringSplit, context, subject_string, | |
1397 separator_string, limit_number); | |
1398 Return(result); | |
1399 } | |
1400 | |
1401 // ES6 section B.2.3.1 String.prototype.substr ( start, length ) | 1041 // ES6 section B.2.3.1 String.prototype.substr ( start, length ) |
1402 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) { | 1042 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) { |
1403 Label out(this), handle_length(this); | 1043 Label out(this), handle_length(this); |
1404 | 1044 |
1405 Variable var_start(this, MachineRepresentation::kTagged); | 1045 Variable var_start(this, MachineRepresentation::kTagged); |
1406 Variable var_length(this, MachineRepresentation::kTagged); | 1046 Variable var_length(this, MachineRepresentation::kTagged); |
1407 | 1047 |
1408 Node* const receiver = Parameter(0); | 1048 Node* const receiver = Parameter(0); |
1409 Node* const start = Parameter(1); | 1049 Node* const start = Parameter(1); |
1410 Node* const length = Parameter(2); | 1050 Node* const length = Parameter(2); |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1855 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context, | 1495 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context, |
1856 HeapConstant(factory()->NewStringFromAsciiChecked( | 1496 HeapConstant(factory()->NewStringFromAsciiChecked( |
1857 "String Iterator.prototype.next", TENURED)), | 1497 "String Iterator.prototype.next", TENURED)), |
1858 iterator); | 1498 iterator); |
1859 Return(result); // Never reached. | 1499 Return(result); // Never reached. |
1860 } | 1500 } |
1861 } | 1501 } |
1862 | 1502 |
1863 } // namespace internal | 1503 } // namespace internal |
1864 } // namespace v8 | 1504 } // namespace v8 |
OLD | NEW |