OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/builtins/builtins-utils.h" |
| 6 #include "src/builtins/builtins.h" |
| 7 |
| 8 #include "src/code-factory.h" |
| 9 #include "src/regexp/jsregexp.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 |
| 14 namespace { |
| 15 |
| 16 compiler::Node* LoadLastIndex(CodeStubAssembler* a, compiler::Node* context, |
| 17 compiler::Node* has_initialmap, |
| 18 compiler::Node* regexp) { |
| 19 typedef CodeStubAssembler::Variable Variable; |
| 20 typedef CodeStubAssembler::Label Label; |
| 21 typedef compiler::Node Node; |
| 22 |
| 23 Variable var_value(a, MachineRepresentation::kTagged); |
| 24 |
| 25 Label out(a), if_unmodified(a), if_modified(a, Label::kDeferred); |
| 26 a->Branch(has_initialmap, &if_unmodified, &if_modified); |
| 27 |
| 28 a->Bind(&if_unmodified); |
| 29 { |
| 30 // Load the in-object field. |
| 31 static const int field_offset = |
| 32 JSRegExp::kSize + JSRegExp::kLastIndexFieldIndex * kPointerSize; |
| 33 var_value.Bind(a->LoadObjectField(regexp, field_offset)); |
| 34 a->Goto(&out); |
| 35 } |
| 36 |
| 37 a->Bind(&if_modified); |
| 38 { |
| 39 // Load through the GetProperty stub. |
| 40 Node* const name = |
| 41 a->HeapConstant(a->isolate()->factory()->last_index_string()); |
| 42 Callable getproperty_callable = CodeFactory::GetProperty(a->isolate()); |
| 43 var_value.Bind(a->CallStub(getproperty_callable, context, regexp, name)); |
| 44 a->Goto(&out); |
| 45 } |
| 46 |
| 47 a->Bind(&out); |
| 48 return var_value.value(); |
| 49 } |
| 50 |
| 51 void StoreLastIndex(CodeStubAssembler* a, compiler::Node* context, |
| 52 compiler::Node* has_initialmap, compiler::Node* regexp, |
| 53 compiler::Node* value) { |
| 54 typedef CodeStubAssembler::Label Label; |
| 55 typedef compiler::Node Node; |
| 56 |
| 57 Label out(a), if_unmodified(a), if_modified(a, Label::kDeferred); |
| 58 a->Branch(has_initialmap, &if_unmodified, &if_modified); |
| 59 |
| 60 a->Bind(&if_unmodified); |
| 61 { |
| 62 // Store the in-object field. |
| 63 static const int field_offset = |
| 64 JSRegExp::kSize + JSRegExp::kLastIndexFieldIndex * kPointerSize; |
| 65 a->StoreObjectField(regexp, field_offset, value); |
| 66 a->Goto(&out); |
| 67 } |
| 68 |
| 69 a->Bind(&if_modified); |
| 70 { |
| 71 // Store through runtime. |
| 72 // TODO(ishell): Use SetPropertyStub here once available. |
| 73 Node* const name = |
| 74 a->HeapConstant(a->isolate()->factory()->last_index_string()); |
| 75 Node* const language_mode = a->SmiConstant(Smi::FromInt(STRICT)); |
| 76 a->CallRuntime(Runtime::kSetProperty, context, regexp, name, value, |
| 77 language_mode); |
| 78 a->Goto(&out); |
| 79 } |
| 80 |
| 81 a->Bind(&out); |
| 82 } |
| 83 |
| 84 compiler::Node* ConstructNewResultFromMatchInfo(Isolate* isolate, |
| 85 CodeStubAssembler* a, |
| 86 compiler::Node* context, |
| 87 compiler::Node* match_elements, |
| 88 compiler::Node* string) { |
| 89 typedef CodeStubAssembler::Variable Variable; |
| 90 typedef CodeStubAssembler::Label Label; |
| 91 typedef compiler::Node Node; |
| 92 |
| 93 Label out(a); |
| 94 |
| 95 Callable constructresult_callable = |
| 96 CodeFactory::RegExpConstructResult(isolate); |
| 97 |
| 98 CodeStubAssembler::ParameterMode mode = CodeStubAssembler::INTPTR_PARAMETERS; |
| 99 Node* const num_indices = a->SmiUntag(a->LoadFixedArrayElement( |
| 100 match_elements, a->IntPtrConstant(RegExpImpl::kLastCaptureCount), 0, |
| 101 mode)); |
| 102 Node* const num_results = a->SmiTag(a->WordShr(num_indices, 1)); |
| 103 Node* const start = a->LoadFixedArrayElement( |
| 104 match_elements, a->IntPtrConstant(RegExpImpl::kFirstCapture), 0, mode); |
| 105 Node* const end = a->LoadFixedArrayElement( |
| 106 match_elements, a->IntPtrConstant(RegExpImpl::kFirstCapture + 1), 0, |
| 107 mode); |
| 108 |
| 109 // Calculate the substring of the first match before creating the result array |
| 110 // to avoid an unnecessary write barrier storing the first result. |
| 111 Node* const first = a->SubString(context, string, start, end); |
| 112 |
| 113 Node* const result = a->CallStub(constructresult_callable, context, |
| 114 num_results, start, string); |
| 115 Node* const result_elements = a->LoadElements(result); |
| 116 |
| 117 a->StoreFixedArrayElement(result_elements, a->IntPtrConstant(0), first, |
| 118 SKIP_WRITE_BARRIER); |
| 119 |
| 120 a->GotoIf(a->SmiEqual(num_results, a->SmiConstant(Smi::FromInt(1))), &out); |
| 121 |
| 122 // Store all remaining captures. |
| 123 Node* const limit = |
| 124 a->IntPtrAdd(a->IntPtrConstant(RegExpImpl::kFirstCapture), num_indices); |
| 125 |
| 126 Variable var_from_cursor(a, MachineType::PointerRepresentation()); |
| 127 Variable var_to_cursor(a, MachineType::PointerRepresentation()); |
| 128 |
| 129 var_from_cursor.Bind(a->IntPtrConstant(RegExpImpl::kFirstCapture + 2)); |
| 130 var_to_cursor.Bind(a->IntPtrConstant(1)); |
| 131 |
| 132 Variable* vars[] = {&var_from_cursor, &var_to_cursor}; |
| 133 Label loop(a, 2, vars); |
| 134 |
| 135 a->Goto(&loop); |
| 136 a->Bind(&loop); |
| 137 { |
| 138 Node* const from_cursor = var_from_cursor.value(); |
| 139 Node* const to_cursor = var_to_cursor.value(); |
| 140 Node* const start = a->LoadFixedArrayElement(match_elements, from_cursor); |
| 141 |
| 142 Label next_iter(a); |
| 143 a->GotoIf(a->SmiEqual(start, a->SmiConstant(Smi::FromInt(-1))), &next_iter); |
| 144 |
| 145 Node* const from_cursor_plus1 = |
| 146 a->IntPtrAdd(from_cursor, a->IntPtrConstant(1)); |
| 147 Node* const end = |
| 148 a->LoadFixedArrayElement(match_elements, from_cursor_plus1); |
| 149 |
| 150 Node* const capture = a->SubString(context, string, start, end); |
| 151 a->StoreFixedArrayElement(result_elements, to_cursor, capture); |
| 152 a->Goto(&next_iter); |
| 153 |
| 154 a->Bind(&next_iter); |
| 155 var_from_cursor.Bind(a->IntPtrAdd(from_cursor, a->IntPtrConstant(2))); |
| 156 var_to_cursor.Bind(a->IntPtrAdd(to_cursor, a->IntPtrConstant(1))); |
| 157 a->Branch(a->UintPtrLessThan(var_from_cursor.value(), limit), &loop, &out); |
| 158 } |
| 159 |
| 160 a->Bind(&out); |
| 161 return result; |
| 162 } |
| 163 |
| 164 } // namespace |
| 165 |
| 166 // ES#sec-regexp.prototype.exec |
| 167 // RegExp.prototype.exec ( string ) |
| 168 void Builtins::Generate_RegExpPrototypeExec(CodeStubAssembler* a) { |
| 169 typedef CodeStubAssembler::Variable Variable; |
| 170 typedef CodeStubAssembler::Label Label; |
| 171 typedef compiler::Node Node; |
| 172 |
| 173 Isolate* const isolate = a->isolate(); |
| 174 |
| 175 Node* const receiver = a->Parameter(0); |
| 176 Node* const maybe_string = a->Parameter(1); |
| 177 Node* const context = a->Parameter(4); |
| 178 |
| 179 Node* const null = a->NullConstant(); |
| 180 Node* const int_zero = a->IntPtrConstant(0); |
| 181 Node* const smi_zero = a->SmiConstant(Smi::FromInt(0)); |
| 182 |
| 183 // Ensure {receiver} is a JSRegExp. |
| 184 Node* const regexp_map = a->ThrowIfNotInstanceType( |
| 185 context, receiver, JS_REGEXP_TYPE, "RegExp.prototype.exec"); |
| 186 Node* const regexp = receiver; |
| 187 |
| 188 // Check whether the regexp instance is unmodified. |
| 189 Node* const native_context = a->LoadNativeContext(context); |
| 190 Node* const regexp_fun = |
| 191 a->LoadContextElement(native_context, Context::REGEXP_FUNCTION_INDEX); |
| 192 Node* const initial_map = |
| 193 a->LoadObjectField(regexp_fun, JSFunction::kPrototypeOrInitialMapOffset); |
| 194 Node* const has_initialmap = a->WordEqual(regexp_map, initial_map); |
| 195 |
| 196 // Convert {maybe_string} to a string. |
| 197 Callable tostring_callable = CodeFactory::ToString(isolate); |
| 198 Node* const string = a->CallStub(tostring_callable, context, maybe_string); |
| 199 Node* const string_length = a->LoadStringLength(string); |
| 200 |
| 201 // Check whether the regexp is global or sticky, which determines whether we |
| 202 // update last index later on. |
| 203 Node* const flags = a->LoadObjectField(regexp, JSRegExp::kFlagsOffset); |
| 204 Node* const is_global_or_sticky = |
| 205 a->WordAnd(a->SmiUntag(flags), |
| 206 a->IntPtrConstant(JSRegExp::kGlobal | JSRegExp::kSticky)); |
| 207 Node* const should_update_last_index = |
| 208 a->WordNotEqual(is_global_or_sticky, int_zero); |
| 209 |
| 210 // Grab and possibly update last index. |
| 211 Label run_exec(a); |
| 212 Variable var_lastindex(a, MachineRepresentation::kTagged); |
| 213 { |
| 214 Label if_doupdate(a), if_dontupdate(a); |
| 215 a->Branch(should_update_last_index, &if_doupdate, &if_dontupdate); |
| 216 |
| 217 a->Bind(&if_doupdate); |
| 218 { |
| 219 Node* const regexp_lastindex = |
| 220 LoadLastIndex(a, context, has_initialmap, regexp); |
| 221 |
| 222 Callable tolength_callable = CodeFactory::ToLength(isolate); |
| 223 Node* const lastindex = |
| 224 a->CallStub(tolength_callable, context, regexp_lastindex); |
| 225 var_lastindex.Bind(lastindex); |
| 226 |
| 227 Label if_isoob(a, Label::kDeferred); |
| 228 a->GotoUnless(a->WordIsSmi(lastindex), &if_isoob); |
| 229 a->GotoUnless(a->SmiLessThanOrEqual(lastindex, string_length), &if_isoob); |
| 230 a->Goto(&run_exec); |
| 231 |
| 232 a->Bind(&if_isoob); |
| 233 { |
| 234 StoreLastIndex(a, context, has_initialmap, regexp, smi_zero); |
| 235 a->Return(null); |
| 236 } |
| 237 } |
| 238 |
| 239 a->Bind(&if_dontupdate); |
| 240 { |
| 241 var_lastindex.Bind(smi_zero); |
| 242 a->Goto(&run_exec); |
| 243 } |
| 244 } |
| 245 |
| 246 Node* match_indices; |
| 247 Label successful_match(a); |
| 248 a->Bind(&run_exec); |
| 249 { |
| 250 // Get last match info from the context. |
| 251 Node* const last_match_info = a->LoadContextElement( |
| 252 native_context, Context::REGEXP_LAST_MATCH_INFO_INDEX); |
| 253 |
| 254 // Call the exec stub. |
| 255 Callable exec_callable = CodeFactory::RegExpExec(isolate); |
| 256 match_indices = a->CallStub(exec_callable, context, regexp, string, |
| 257 var_lastindex.value(), last_match_info); |
| 258 |
| 259 // {match_indices} is either null or the RegExpLastMatchInfo array. |
| 260 // Return early if exec failed, possibly updating last index. |
| 261 a->GotoUnless(a->WordEqual(match_indices, null), &successful_match); |
| 262 |
| 263 Label return_null(a); |
| 264 a->GotoUnless(should_update_last_index, &return_null); |
| 265 |
| 266 StoreLastIndex(a, context, has_initialmap, regexp, smi_zero); |
| 267 a->Goto(&return_null); |
| 268 |
| 269 a->Bind(&return_null); |
| 270 a->Return(null); |
| 271 } |
| 272 |
| 273 Label construct_result(a); |
| 274 a->Bind(&successful_match); |
| 275 { |
| 276 Node* const match_elements = a->LoadElements(match_indices); |
| 277 |
| 278 a->GotoUnless(should_update_last_index, &construct_result); |
| 279 |
| 280 // Update the new last index from {match_indices}. |
| 281 Node* const new_lastindex = a->LoadFixedArrayElement( |
| 282 match_elements, a->IntPtrConstant(RegExpImpl::kFirstCapture + 1)); |
| 283 |
| 284 StoreLastIndex(a, context, has_initialmap, regexp, new_lastindex); |
| 285 a->Goto(&construct_result); |
| 286 |
| 287 a->Bind(&construct_result); |
| 288 { |
| 289 Node* result = ConstructNewResultFromMatchInfo(isolate, a, context, |
| 290 match_elements, string); |
| 291 a->Return(result); |
| 292 } |
| 293 } |
| 294 } |
| 295 |
| 296 } // namespace internal |
| 297 } // namespace v8 |
OLD | NEW |