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