| 1 |
// Copyright 2019 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 "irregexp/imported/regexp-compiler.h" |
| 6 |
|
| 7 |
#include <optional> |
| 8 |
|
| 9 |
#include "irregexp/imported/regexp-macro-assembler-arch.h" |
| 10 |
|
| 11 |
#ifdef V8_INTL_SUPPORT |
| 12 |
#include "irregexp/imported/special-case.h" |
| 13 |
#include "unicode/locid.h" |
| 14 |
#include "unicode/uniset.h" |
| 15 |
#include "unicode/utypes.h" |
| 16 |
#endif // V8_INTL_SUPPORT |
| 17 |
|
| 18 |
namespace v8::internal { |
| 19 |
|
| 20 |
using namespace regexp_compiler_constants; // NOLINT(build/namespaces) |
| 21 |
|
| 22 |
// ------------------------------------------------------------------- |
| 23 |
// Implementation of the Irregexp regular expression engine. |
| 24 |
// |
| 25 |
// The Irregexp regular expression engine is intended to be a complete |
| 26 |
// implementation of ECMAScript regular expressions. It generates either |
| 27 |
// bytecodes or native code. |
| 28 |
|
| 29 |
// The Irregexp regexp engine is structured in three steps. |
| 30 |
// 1) The parser generates an abstract syntax tree. See ast.cc. |
| 31 |
// 2) From the AST a node network is created. The nodes are all |
| 32 |
// subclasses of RegExpNode. The nodes represent states when |
| 33 |
// executing a regular expression. Several optimizations are |
| 34 |
// performed on the node network. |
| 35 |
// 3) From the nodes we generate either byte codes or native code |
| 36 |
// that can actually execute the regular expression (perform |
| 37 |
// the search). The code generation step is described in more |
| 38 |
// detail below. |
| 39 |
|
| 40 |
// Code generation. |
| 41 |
// |
| 42 |
// The nodes are divided into four main categories. |
| 43 |
// * Choice nodes |
| 44 |
// These represent places where the regular expression can |
| 45 |
// match in more than one way. For example on entry to an |
| 46 |
// alternation (foo|bar) or a repetition (*, +, ? or {}). |
| 47 |
// * Action nodes |
| 48 |
// These represent places where some action should be |
| 49 |
// performed. Examples include recording the current position |
| 50 |
// in the input string to a register (in order to implement |
| 51 |
// captures) or other actions on register for example in order |
| 52 |
// to implement the counters needed for {} repetitions. |
| 53 |
// * Matching nodes |
| 54 |
// These attempt to match some element part of the input string. |
| 55 |
// Examples of elements include character classes, plain strings |
| 56 |
// or back references. |
| 57 |
// * End nodes |
| 58 |
// These are used to implement the actions required on finding |
| 59 |
// a successful match or failing to find a match. |
| 60 |
// |
| 61 |
// The code generated (whether as byte codes or native code) maintains |
| 62 |
// some state as it runs. This consists of the following elements: |
| 63 |
// |
| 64 |
// * The capture registers. Used for string captures. |
| 65 |
// * Other registers. Used for counters etc. |
| 66 |
// * The current position. |
| 67 |
// * The stack of backtracking information. Used when a matching node |
| 68 |
// fails to find a match and needs to try an alternative. |
| 69 |
// |
| 70 |
// Conceptual regular expression execution model: |
| 71 |
// |
| 72 |
// There is a simple conceptual model of regular expression execution |
| 73 |
// which will be presented first. The actual code generated is a more |
| 74 |
// efficient simulation of the simple conceptual model: |
| 75 |
// |
| 76 |
// * Choice nodes are implemented as follows: |
| 77 |
// For each choice except the last { |
| 78 |
// push current position |
| 79 |
// push backtrack code location |
| 80 |
// <generate code to test for choice> |
| 81 |
// backtrack code location: |
| 82 |
// pop current position |
| 83 |
// } |
| 84 |
// <generate code to test for last choice> |
| 85 |
// |
| 86 |
// * Actions nodes are generated as follows |
| 87 |
// <push affected registers on backtrack stack> |
| 88 |
// <generate code to perform action> |
| 89 |
// push backtrack code location |
| 90 |
// <generate code to test for following nodes> |
| 91 |
// backtrack code location: |
| 92 |
// <pop affected registers to restore their state> |
| 93 |
// <pop backtrack location from stack and go to it> |
| 94 |
// |
| 95 |
// * Matching nodes are generated as follows: |
| 96 |
// if input string matches at current position |
| 97 |
// update current position |
| 98 |
// <generate code to test for following nodes> |
| 99 |
// else |
| 100 |
// <pop backtrack location from stack and go to it> |
| 101 |
// |
| 102 |
// Thus it can be seen that the current position is saved and restored |
| 103 |
// by the choice nodes, whereas the registers are saved and restored by |
| 104 |
// by the action nodes that manipulate them. |
| 105 |
// |
| 106 |
// The other interesting aspect of this model is that nodes are generated |
| 107 |
// at the point where they are needed by a recursive call to Emit(). If |
| 108 |
// the node has already been code generated then the Emit() call will |
| 109 |
// generate a jump to the previously generated code instead. In order to |
| 110 |
// limit recursion it is possible for the Emit() function to put the node |
| 111 |
// on a work list for later generation and instead generate a jump. The |
| 112 |
// destination of the jump is resolved later when the code is generated. |
| 113 |
// |
| 114 |
// Actual regular expression code generation. |
| 115 |
// |
| 116 |
// Code generation is actually more complicated than the above. In order to |
| 117 |
// improve the efficiency of the generated code some optimizations are |
| 118 |
// performed |
| 119 |
// |
| 120 |
// * Choice nodes have 1-character lookahead. |
| 121 |
// A choice node looks at the following character and eliminates some of |
| 122 |
// the choices immediately based on that character. This is not yet |
| 123 |
// implemented. |
| 124 |
// * Simple greedy loops store reduced backtracking information. |
| 125 |
// A quantifier like /.*foo/m will greedily match the whole input. It will |
| 126 |
// then need to backtrack to a point where it can match "foo". The naive |
| 127 |
// implementation of this would push each character position onto the |
| 128 |
// backtracking stack, then pop them off one by one. This would use space |
| 129 |
// proportional to the length of the input string. However since the "." |
| 130 |
// can only match in one way and always has a constant length (in this case |
| 131 |
// of 1) it suffices to store the current position on the top of the stack |
| 132 |
// once. Matching now becomes merely incrementing the current position and |
| 133 |
// backtracking becomes decrementing the current position and checking the |
| 134 |
// result against the stored current position. This is faster and saves |
| 135 |
// space. |
| 136 |
// * The current state is virtualized. |
| 137 |
// This is used to defer expensive operations until it is clear that they |
| 138 |
// are needed and to generate code for a node more than once, allowing |
| 139 |
// specialized an efficient versions of the code to be created. This is |
| 140 |
// explained in the section below. |
| 141 |
// |
| 142 |
// Execution state virtualization. |
| 143 |
// |
| 144 |
// Instead of emitting code, nodes that manipulate the state can record their |
| 145 |
// manipulation in an object called the Trace. The Trace object can record a |
| 146 |
// current position offset, an optional backtrack code location on the top of |
| 147 |
// the virtualized backtrack stack and some register changes. When a node is |
| 148 |
// to be emitted it can flush the Trace or update it. Flushing the Trace |
| 149 |
// will emit code to bring the actual state into line with the virtual state. |
| 150 |
// Avoiding flushing the state can postpone some work (e.g. updates of capture |
| 151 |
// registers). Postponing work can save time when executing the regular |
| 152 |
// expression since it may be found that the work never has to be done as a |
| 153 |
// failure to match can occur. In addition it is much faster to jump to a |
| 154 |
// known backtrack code location than it is to pop an unknown backtrack |
| 155 |
// location from the stack and jump there. |
| 156 |
// |
| 157 |
// The virtual state found in the Trace affects code generation. For example |
| 158 |
// the virtual state contains the difference between the actual current |
| 159 |
// position and the virtual current position, and matching code needs to use |
| 160 |
// this offset to attempt a match in the correct location of the input |
| 161 |
// string. Therefore code generated for a non-trivial trace is specialized |
| 162 |
// to that trace. The code generator therefore has the ability to generate |
| 163 |
// code for each node several times. In order to limit the size of the |
| 164 |
// generated code there is an arbitrary limit on how many specialized sets of |
| 165 |
// code may be generated for a given node. If the limit is reached, the |
| 166 |
// trace is flushed and a generic version of the code for a node is emitted. |
| 167 |
// This is subsequently used for that node. The code emitted for non-generic |
| 168 |
// trace is not recorded in the node and so it cannot currently be reused in |
| 169 |
// the event that code generation is requested for an identical trace. |
| 170 |
|
| 171 |
namespace { |
| 172 |
|
| 173 |
constexpr base::uc32 MaxCodeUnit(const bool one_byte) { |
| 174 |
static_assert(String::kMaxOneByteCharCodeU <= |
| 175 |
std::numeric_limits<uint16_t>::max()); |
| 176 |
static_assert(String::kMaxUtf16CodeUnitU <= |
| 177 |
std::numeric_limits<uint16_t>::max()); |
| 178 |
return one_byte ? String::kMaxOneByteCharCodeU : String::kMaxUtf16CodeUnitU; |
| 179 |
} |
| 180 |
|
| 181 |
constexpr uint32_t CharMask(const bool one_byte) { |
| 182 |
static_assert(base::bits::IsPowerOfTwo(String::kMaxOneByteCharCodeU + 1)); |
| 183 |
static_assert(base::bits::IsPowerOfTwo(String::kMaxUtf16CodeUnitU + 1)); |
| 184 |
return MaxCodeUnit(one_byte); |
| 185 |
} |
| 186 |
|
| 187 |
} // namespace |
| 188 |
|
| 189 |
void RegExpTree::AppendToText(RegExpText* text, Zone* zone) { UNREACHABLE(); } |
| 190 |
|
| 191 |
void RegExpAtom::AppendToText(RegExpText* text, Zone* zone) { |
| 192 |
text->AddElement(TextElement::Atom(this), zone); |
| 193 |
} |
| 194 |
|
| 195 |
void RegExpClassRanges::AppendToText(RegExpText* text, Zone* zone) { |
| 196 |
text->AddElement(TextElement::ClassRanges(this), zone); |
| 197 |
} |
| 198 |
|
| 199 |
void RegExpText::AppendToText(RegExpText* text, Zone* zone) { |
| 200 |
for (int i = 0; i < elements()->length(); i++) |
| 201 |
text->AddElement(elements()->at(i), zone); |
| 202 |
} |
| 203 |
|
| 204 |
TextElement TextElement::Atom(RegExpAtom* atom) { |
| 205 |
return TextElement(ATOM, atom); |
| 206 |
} |
| 207 |
|
| 208 |
TextElement TextElement::ClassRanges(RegExpClassRanges* class_ranges) { |
| 209 |
return TextElement(CLASS_RANGES, class_ranges); |
| 210 |
} |
| 211 |
|
| 212 |
int TextElement::length() const { |
| 213 |
switch (text_type()) { |
| 214 |
case ATOM: |
| 215 |
return atom()->length(); |
| 216 |
|
| 217 |
case CLASS_RANGES: |
| 218 |
return 1; |
| 219 |
} |
| 220 |
UNREACHABLE(); |
| 221 |
} |
| 222 |
|
| 223 |
class RecursionCheck { |
| 224 |
public: |
| 225 |
explicit RecursionCheck(RegExpCompiler* compiler) : compiler_(compiler) { |
| 226 |
compiler->IncrementRecursionDepth(); |
| 227 |
} |
| 228 |
~RecursionCheck() { compiler_->DecrementRecursionDepth(); } |
| 229 |
|
| 230 |
private: |
| 231 |
RegExpCompiler* compiler_; |
| 232 |
}; |
| 233 |
|
| 234 |
// Attempts to compile the regexp using an Irregexp code generator. Returns |
| 235 |
// a fixed array or a null handle depending on whether it succeeded. |
| 236 |
RegExpCompiler::RegExpCompiler(Isolate* isolate, Zone* zone, int capture_count, |
| 237 |
RegExpFlags flags, bool one_byte) |
| 238 |
: next_register_(JSRegExp::RegistersForCaptureCount(capture_count)), |
| 239 |
unicode_lookaround_stack_register_(kNoRegister), |
| 240 |
unicode_lookaround_position_register_(kNoRegister), |
| 241 |
work_list_(nullptr), |
| 242 |
recursion_depth_(0), |
| 243 |
flags_(flags), |
| 244 |
one_byte_(one_byte), |
| 245 |
reg_exp_too_big_(false), |
| 246 |
limiting_recursion_(false), |
| 247 |
optimize_(v8_flags.regexp_optimization), |
| 248 |
read_backward_(false), |
| 249 |
current_expansion_factor_(1), |
| 250 |
frequency_collator_(), |
| 251 |
isolate_(isolate), |
| 252 |
zone_(zone) { |
| 253 |
accept_ = zone->New<EndNode>(EndNode::ACCEPT, zone); |
| 254 |
DCHECK_GE(RegExpMacroAssembler::kMaxRegister, next_register_ - 1); |
| 255 |
} |
| 256 |
|
| 257 |
RegExpCompiler::CompilationResult RegExpCompiler::Assemble( |
| 258 |
Isolate* isolate, RegExpMacroAssembler* macro_assembler, RegExpNode* start, |
| 259 |
int capture_count, DirectHandle<String> pattern) { |
| 260 |
macro_assembler_ = macro_assembler; |
| 261 |
|
| 262 |
ZoneVector<RegExpNode*> work_list(zone()); |
| 263 |
work_list_ = &work_list; |
| 264 |
Label fail; |
| 265 |
macro_assembler_->PushBacktrack(&fail); |
| 266 |
Trace new_trace; |
| 267 |
start->Emit(this, &new_trace); |
| 268 |
macro_assembler_->BindJumpTarget(&fail); |
| 269 |
macro_assembler_->Fail(); |
| 270 |
while (!work_list.empty()) { |
| 271 |
RegExpNode* node = work_list.back(); |
| 272 |
work_list.pop_back(); |
| 273 |
node->set_on_work_list(false); |
| 274 |
if (!node->label()->is_bound()) node->Emit(this, &new_trace); |
| 275 |
} |
| 276 |
if (reg_exp_too_big_) { |
| 277 |
if (v8_flags.correctness_fuzzer_suppressions) { |
| 278 |
FATAL("Aborting on excess zone allocation"); |
| 279 |
} |
| 280 |
macro_assembler_->AbortedCodeGeneration(); |
| 281 |
return CompilationResult::RegExpTooBig(); |
| 282 |
} |
| 283 |
|
| 284 |
DirectHandle<HeapObject> code = macro_assembler_->GetCode(pattern, flags_); |
| 285 |
isolate->IncreaseTotalRegexpCodeGenerated(code); |
| 286 |
work_list_ = nullptr; |
| 287 |
|
| 288 |
return {code, next_register_}; |
| 289 |
} |
| 290 |
|
| 291 |
bool Trace::DeferredAction::Mentions(int that) { |
| 292 |
if (action_type() == ActionNode::CLEAR_CAPTURES) { |
| 293 |
Interval range = static_cast<DeferredClearCaptures*>(this)->range(); |
| 294 |
return range.Contains(that); |
| 295 |
} else { |
| 296 |
return reg() == that; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
bool Trace::mentions_reg(int reg) { |
| 301 |
for (DeferredAction* action = actions_; action != nullptr; |
| 302 |
action = action->next()) { |
| 303 |
if (action->Mentions(reg)) return true; |
| 304 |
} |
| 305 |
return false; |
| 306 |
} |
| 307 |
|
| 308 |
bool Trace::GetStoredPosition(int reg, int* cp_offset) { |
| 309 |
DCHECK_EQ(0, *cp_offset); |
| 310 |
for (DeferredAction* action = actions_; action != nullptr; |
| 311 |
action = action->next()) { |
| 312 |
if (action->Mentions(reg)) { |
| 313 |
if (action->action_type() == ActionNode::STORE_POSITION) { |
| 314 |
*cp_offset = static_cast<DeferredCapture*>(action)->cp_offset(); |
| 315 |
return true; |
| 316 |
} else { |
| 317 |
return false; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
// A (dynamically-sized) set of unsigned integers that behaves especially well |
| 325 |
// on small integers (< kFirstLimit). May do zone-allocation. |
| 326 |
class DynamicBitSet : public ZoneObject { |
| 327 |
public: |
| 328 |
V8_EXPORT_PRIVATE bool Get(unsigned value) const { |
| 329 |
if (value < kFirstLimit) { |
| 330 |
return (first_ & (1 << value)) != 0; |
| 331 |
} else if (remaining_ == nullptr) { |
| 332 |
return false; |
| 333 |
} else { |
| 334 |
return remaining_->Contains(value); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
// Destructively set a value in this set. |
| 339 |
void Set(unsigned value, Zone* zone) { |
| 340 |
if (value < kFirstLimit) { |
| 341 |
first_ |= (1 << value); |
| 342 |
} else { |
| 343 |
if (remaining_ == nullptr) |
| 344 |
remaining_ = zone->New<ZoneList<unsigned>>(1, zone); |
| 345 |
if (remaining_->is_empty() || !remaining_->Contains(value)) |
| 346 |
remaining_->Add(value, zone); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
private: |
| 351 |
static constexpr unsigned kFirstLimit = 32; |
| 352 |
|
| 353 |
uint32_t first_ = 0; |
| 354 |
ZoneList<unsigned>* remaining_ = nullptr; |
| 355 |
}; |
| 356 |
|
| 357 |
int Trace::FindAffectedRegisters(DynamicBitSet* affected_registers, |
| 358 |
Zone* zone) { |
| 359 |
int max_register = RegExpCompiler::kNoRegister; |
| 360 |
for (DeferredAction* action = actions_; action != nullptr; |
| 361 |
action = action->next()) { |
| 362 |
if (action->action_type() == ActionNode::CLEAR_CAPTURES) { |
| 363 |
Interval range = static_cast<DeferredClearCaptures*>(action)->range(); |
| 364 |
for (int i = range.from(); i <= range.to(); i++) |
| 365 |
affected_registers->Set(i, zone); |
| 366 |
if (range.to() > max_register) max_register = range.to(); |
| 367 |
} else { |
| 368 |
affected_registers->Set(action->reg(), zone); |
| 369 |
if (action->reg() > max_register) max_register = action->reg(); |
| 370 |
} |
| 371 |
} |
| 372 |
return max_register; |
| 373 |
} |
| 374 |
|
| 375 |
void Trace::RestoreAffectedRegisters(RegExpMacroAssembler* assembler, |
| 376 |
int max_register, |
| 377 |
const DynamicBitSet& registers_to_pop, |
| 378 |
const DynamicBitSet& registers_to_clear) { |
| 379 |
for (int reg = max_register; reg >= 0; reg--) { |
| 380 |
if (registers_to_pop.Get(reg)) { |
| 381 |
assembler->PopRegister(reg); |
| 382 |
} else if (registers_to_clear.Get(reg)) { |
| 383 |
int clear_to = reg; |
| 384 |
while (reg > 0 && registers_to_clear.Get(reg - 1)) { |
| 385 |
reg--; |
| 386 |
} |
| 387 |
assembler->ClearRegisters(reg, clear_to); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
void Trace::PerformDeferredActions(RegExpMacroAssembler* assembler, |
| 393 |
int max_register, |
| 394 |
const DynamicBitSet& affected_registers, |
| 395 |
DynamicBitSet* registers_to_pop, |
| 396 |
DynamicBitSet* registers_to_clear, |
| 397 |
Zone* zone) { |
| 398 |
// Count pushes performed to force a stack limit check occasionally. |
| 399 |
int pushes = 0; |
| 400 |
|
| 401 |
for (int reg = 0; reg <= max_register; reg++) { |
| 402 |
if (!affected_registers.Get(reg)) continue; |
| 403 |
|
| 404 |
// The chronologically first deferred action in the trace |
| 405 |
// is used to infer the action needed to restore a register |
| 406 |
// to its previous state (or not, if it's safe to ignore it). |
| 407 |
enum DeferredActionUndoType { IGNORE, RESTORE, CLEAR }; |
| 408 |
DeferredActionUndoType undo_action = IGNORE; |
| 409 |
|
| 410 |
int value = 0; |
| 411 |
bool absolute = false; |
| 412 |
bool clear = false; |
| 413 |
static const int kNoStore = kMinInt; |
| 414 |
int store_position = kNoStore; |
| 415 |
// This is a little tricky because we are scanning the actions in reverse |
| 416 |
// historical order (newest first). |
| 417 |
for (DeferredAction* action = actions_; action != nullptr; |
| 418 |
action = action->next()) { |
| 419 |
if (action->Mentions(reg)) { |
| 420 |
switch (action->action_type()) { |
| 421 |
case ActionNode::SET_REGISTER_FOR_LOOP: { |
| 422 |
Trace::DeferredSetRegisterForLoop* psr = |
| 423 |
static_cast<Trace::DeferredSetRegisterForLoop*>(action); |
| 424 |
if (!absolute) { |
| 425 |
value += psr->value(); |
| 426 |
absolute = true; |
| 427 |
} |
| 428 |
// SET_REGISTER_FOR_LOOP is only used for newly introduced loop |
| 429 |
// counters. They can have a significant previous value if they |
| 430 |
// occur in a loop. TODO(lrn): Propagate this information, so |
| 431 |
// we can set undo_action to IGNORE if we know there is no value to |
| 432 |
// restore. |
| 433 |
undo_action = RESTORE; |
| 434 |
DCHECK_EQ(store_position, kNoStore); |
| 435 |
DCHECK(!clear); |
| 436 |
break; |
| 437 |
} |
| 438 |
case ActionNode::INCREMENT_REGISTER: |
| 439 |
if (!absolute) { |
| 440 |
value++; |
| 441 |
} |
| 442 |
DCHECK_EQ(store_position, kNoStore); |
| 443 |
DCHECK(!clear); |
| 444 |
undo_action = RESTORE; |
| 445 |
break; |
| 446 |
case ActionNode::STORE_POSITION: { |
| 447 |
Trace::DeferredCapture* pc = |
| 448 |
static_cast<Trace::DeferredCapture*>(action); |
| 449 |
if (!clear && store_position == kNoStore) { |
| 450 |
store_position = pc->cp_offset(); |
| 451 |
} |
| 452 |
|
| 453 |
// For captures we know that stores and clears alternate. |
| 454 |
// Other register, are never cleared, and if the occur |
| 455 |
// inside a loop, they might be assigned more than once. |
| 456 |
if (reg <= 1) { |
| 457 |
// Registers zero and one, aka "capture zero", is |
| 458 |
// always set correctly if we succeed. There is no |
| 459 |
// need to undo a setting on backtrack, because we |
| 460 |
// will set it again or fail. |
| 461 |
undo_action = IGNORE; |
| 462 |
} else { |
| 463 |
undo_action = pc->is_capture() ? CLEAR : RESTORE; |
| 464 |
} |
| 465 |
DCHECK(!absolute); |
| 466 |
DCHECK_EQ(value, 0); |
| 467 |
break; |
| 468 |
} |
| 469 |
case ActionNode::CLEAR_CAPTURES: { |
| 470 |
// Since we're scanning in reverse order, if we've already |
| 471 |
// set the position we have to ignore historically earlier |
| 472 |
// clearing operations. |
| 473 |
if (store_position == kNoStore) { |
| 474 |
clear = true; |
| 475 |
} |
| 476 |
undo_action = RESTORE; |
| 477 |
DCHECK(!absolute); |
| 478 |
DCHECK_EQ(value, 0); |
| 479 |
break; |
| 480 |
} |
| 481 |
default: |
| 482 |
UNREACHABLE(); |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
// Prepare for the undo-action (e.g., push if it's going to be popped). |
| 487 |
if (undo_action == RESTORE) { |
| 488 |
pushes++; |
| 489 |
RegExpMacroAssembler::StackCheckFlag stack_check = |
| 490 |
RegExpMacroAssembler::kNoStackLimitCheck; |
| 491 |
DCHECK_GT(assembler->stack_limit_slack_slot_count(), 0); |
| 492 |
if (pushes == assembler->stack_limit_slack_slot_count()) { |
| 493 |
stack_check = RegExpMacroAssembler::kCheckStackLimit; |
| 494 |
pushes = 0; |
| 495 |
} |
| 496 |
|
| 497 |
assembler->PushRegister(reg, stack_check); |
| 498 |
registers_to_pop->Set(reg, zone); |
| 499 |
} else if (undo_action == CLEAR) { |
| 500 |
registers_to_clear->Set(reg, zone); |
| 501 |
} |
| 502 |
// Perform the chronologically last action (or accumulated increment) |
| 503 |
// for the register. |
| 504 |
if (store_position != kNoStore) { |
| 505 |
assembler->WriteCurrentPositionToRegister(reg, store_position); |
| 506 |
} else if (clear) { |
| 507 |
assembler->ClearRegisters(reg, reg); |
| 508 |
} else if (absolute) { |
| 509 |
assembler->SetRegister(reg, value); |
| 510 |
} else if (value != 0) { |
| 511 |
assembler->AdvanceRegister(reg, value); |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
// This is called as we come into a loop choice node and some other tricky |
| 517 |
// nodes. It normalizes the state of the code generator to ensure we can |
| 518 |
// generate generic code. |
| 519 |
void Trace::Flush(RegExpCompiler* compiler, RegExpNode* successor) { |
| 520 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 521 |
|
| 522 |
DCHECK(!is_trivial()); |
| 523 |
|
| 524 |
if (actions_ == nullptr && backtrack() == nullptr) { |
| 525 |
// Here we just have some deferred cp advances to fix and we are back to |
| 526 |
// a normal situation. We may also have to forget some information gained |
| 527 |
// through a quick check that was already performed. |
| 528 |
if (cp_offset_ != 0) assembler->AdvanceCurrentPosition(cp_offset_); |
| 529 |
// Create a new trivial state and generate the node with that. |
| 530 |
Trace new_state; |
| 531 |
successor->Emit(compiler, &new_state); |
| 532 |
return; |
| 533 |
} |
| 534 |
|
| 535 |
// Generate deferred actions here along with code to undo them again. |
| 536 |
DynamicBitSet affected_registers; |
| 537 |
|
| 538 |
if (backtrack() != nullptr) { |
| 539 |
// Here we have a concrete backtrack location. These are set up by choice |
| 540 |
// nodes and so they indicate that we have a deferred save of the current |
| 541 |
// position which we may need to emit here. |
| 542 |
assembler->PushCurrentPosition(); |
| 543 |
} |
| 544 |
|
| 545 |
int max_register = |
| 546 |
FindAffectedRegisters(&affected_registers, compiler->zone()); |
| 547 |
DynamicBitSet registers_to_pop; |
| 548 |
DynamicBitSet registers_to_clear; |
| 549 |
PerformDeferredActions(assembler, max_register, affected_registers, |
| 550 |
®isters_to_pop, ®isters_to_clear, |
| 551 |
compiler->zone()); |
| 552 |
if (cp_offset_ != 0) { |
| 553 |
assembler->AdvanceCurrentPosition(cp_offset_); |
| 554 |
} |
| 555 |
|
| 556 |
// Create a new trivial state and generate the node with that. |
| 557 |
Label undo; |
| 558 |
assembler->PushBacktrack(&undo); |
| 559 |
if (successor->KeepRecursing(compiler)) { |
| 560 |
Trace new_state; |
| 561 |
successor->Emit(compiler, &new_state); |
| 562 |
} else { |
| 563 |
compiler->AddWork(successor); |
| 564 |
assembler->GoTo(successor->label()); |
| 565 |
} |
| 566 |
|
| 567 |
// On backtrack we need to restore state. |
| 568 |
assembler->BindJumpTarget(&undo); |
| 569 |
RestoreAffectedRegisters(assembler, max_register, registers_to_pop, |
| 570 |
registers_to_clear); |
| 571 |
if (backtrack() == nullptr) { |
| 572 |
assembler->Backtrack(); |
| 573 |
} else { |
| 574 |
assembler->PopCurrentPosition(); |
| 575 |
assembler->GoTo(backtrack()); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
void NegativeSubmatchSuccess::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 580 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 581 |
|
| 582 |
// Omit flushing the trace. We discard the entire stack frame anyway. |
| 583 |
|
| 584 |
if (!label()->is_bound()) { |
| 585 |
// We are completely independent of the trace, since we ignore it, |
| 586 |
// so this code can be used as the generic version. |
| 587 |
assembler->Bind(label()); |
| 588 |
} |
| 589 |
|
| 590 |
// Throw away everything on the backtrack stack since the start |
| 591 |
// of the negative submatch and restore the character position. |
| 592 |
assembler->ReadCurrentPositionFromRegister(current_position_register_); |
| 593 |
assembler->ReadStackPointerFromRegister(stack_pointer_register_); |
| 594 |
if (clear_capture_count_ > 0) { |
| 595 |
// Clear any captures that might have been performed during the success |
| 596 |
// of the body of the negative look-ahead. |
| 597 |
int clear_capture_end = clear_capture_start_ + clear_capture_count_ - 1; |
| 598 |
assembler->ClearRegisters(clear_capture_start_, clear_capture_end); |
| 599 |
} |
| 600 |
// Now that we have unwound the stack we find at the top of the stack the |
| 601 |
// backtrack that the BeginNegativeSubmatch node got. |
| 602 |
assembler->Backtrack(); |
| 603 |
} |
| 604 |
|
| 605 |
void EndNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 606 |
if (!trace->is_trivial()) { |
| 607 |
trace->Flush(compiler, this); |
| 608 |
return; |
| 609 |
} |
| 610 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 611 |
if (!label()->is_bound()) { |
| 612 |
assembler->Bind(label()); |
| 613 |
} |
| 614 |
switch (action_) { |
| 615 |
case ACCEPT: |
| 616 |
assembler->Succeed(); |
| 617 |
return; |
| 618 |
case BACKTRACK: |
| 619 |
assembler->GoTo(trace->backtrack()); |
| 620 |
return; |
| 621 |
case NEGATIVE_SUBMATCH_SUCCESS: |
| 622 |
// This case is handled in a different virtual method. |
| 623 |
UNREACHABLE(); |
| 624 |
} |
| 625 |
UNIMPLEMENTED(); |
| 626 |
} |
| 627 |
|
| 628 |
void GuardedAlternative::AddGuard(Guard* guard, Zone* zone) { |
| 629 |
if (guards_ == nullptr) guards_ = zone->New<ZoneList<Guard*>>(1, zone); |
| 630 |
guards_->Add(guard, zone); |
| 631 |
} |
| 632 |
|
| 633 |
ActionNode* ActionNode::SetRegisterForLoop(int reg, int val, |
| 634 |
RegExpNode* on_success) { |
| 635 |
ActionNode* result = |
| 636 |
on_success->zone()->New<ActionNode>(SET_REGISTER_FOR_LOOP, on_success); |
| 637 |
result->data_.u_store_register.reg = reg; |
| 638 |
result->data_.u_store_register.value = val; |
| 639 |
return result; |
| 640 |
} |
| 641 |
|
| 642 |
ActionNode* ActionNode::IncrementRegister(int reg, RegExpNode* on_success) { |
| 643 |
ActionNode* result = |
| 644 |
on_success->zone()->New<ActionNode>(INCREMENT_REGISTER, on_success); |
| 645 |
result->data_.u_increment_register.reg = reg; |
| 646 |
return result; |
| 647 |
} |
| 648 |
|
| 649 |
ActionNode* ActionNode::StorePosition(int reg, bool is_capture, |
| 650 |
RegExpNode* on_success) { |
| 651 |
ActionNode* result = |
| 652 |
on_success->zone()->New<ActionNode>(STORE_POSITION, on_success); |
| 653 |
result->data_.u_position_register.reg = reg; |
| 654 |
result->data_.u_position_register.is_capture = is_capture; |
| 655 |
return result; |
| 656 |
} |
| 657 |
|
| 658 |
ActionNode* ActionNode::ClearCaptures(Interval range, RegExpNode* on_success) { |
| 659 |
ActionNode* result = |
| 660 |
on_success->zone()->New<ActionNode>(CLEAR_CAPTURES, on_success); |
| 661 |
result->data_.u_clear_captures.range_from = range.from(); |
| 662 |
result->data_.u_clear_captures.range_to = range.to(); |
| 663 |
return result; |
| 664 |
} |
| 665 |
|
| 666 |
ActionNode* ActionNode::BeginPositiveSubmatch(int stack_reg, int position_reg, |
| 667 |
RegExpNode* body, |
| 668 |
ActionNode* success_node) { |
| 669 |
ActionNode* result = |
| 670 |
body->zone()->New<ActionNode>(BEGIN_POSITIVE_SUBMATCH, body); |
| 671 |
result->data_.u_submatch.stack_pointer_register = stack_reg; |
| 672 |
result->data_.u_submatch.current_position_register = position_reg; |
| 673 |
result->data_.u_submatch.success_node = success_node; |
| 674 |
return result; |
| 675 |
} |
| 676 |
|
| 677 |
ActionNode* ActionNode::BeginNegativeSubmatch(int stack_reg, int position_reg, |
| 678 |
RegExpNode* on_success) { |
| 679 |
ActionNode* result = |
| 680 |
on_success->zone()->New<ActionNode>(BEGIN_NEGATIVE_SUBMATCH, on_success); |
| 681 |
result->data_.u_submatch.stack_pointer_register = stack_reg; |
| 682 |
result->data_.u_submatch.current_position_register = position_reg; |
| 683 |
return result; |
| 684 |
} |
| 685 |
|
| 686 |
ActionNode* ActionNode::PositiveSubmatchSuccess(int stack_reg, int position_reg, |
| 687 |
int clear_register_count, |
| 688 |
int clear_register_from, |
| 689 |
RegExpNode* on_success) { |
| 690 |
ActionNode* result = on_success->zone()->New<ActionNode>( |
| 691 |
POSITIVE_SUBMATCH_SUCCESS, on_success); |
| 692 |
result->data_.u_submatch.stack_pointer_register = stack_reg; |
| 693 |
result->data_.u_submatch.current_position_register = position_reg; |
| 694 |
result->data_.u_submatch.clear_register_count = clear_register_count; |
| 695 |
result->data_.u_submatch.clear_register_from = clear_register_from; |
| 696 |
return result; |
| 697 |
} |
| 698 |
|
| 699 |
ActionNode* ActionNode::EmptyMatchCheck(int start_register, |
| 700 |
int repetition_register, |
| 701 |
int repetition_limit, |
| 702 |
RegExpNode* on_success) { |
| 703 |
ActionNode* result = |
| 704 |
on_success->zone()->New<ActionNode>(EMPTY_MATCH_CHECK, on_success); |
| 705 |
result->data_.u_empty_match_check.start_register = start_register; |
| 706 |
result->data_.u_empty_match_check.repetition_register = repetition_register; |
| 707 |
result->data_.u_empty_match_check.repetition_limit = repetition_limit; |
| 708 |
return result; |
| 709 |
} |
| 710 |
|
| 711 |
ActionNode* ActionNode::ModifyFlags(RegExpFlags flags, RegExpNode* on_success) { |
| 712 |
ActionNode* result = |
| 713 |
on_success->zone()->New<ActionNode>(MODIFY_FLAGS, on_success); |
| 714 |
result->data_.u_modify_flags.flags = flags; |
| 715 |
return result; |
| 716 |
} |
| 717 |
|
| 718 |
#define DEFINE_ACCEPT(Type) \ |
| 719 |
void Type##Node::Accept(NodeVisitor* visitor) { visitor->Visit##Type(this); } |
| 720 |
FOR_EACH_NODE_TYPE(DEFINE_ACCEPT) |
| 721 |
#undef DEFINE_ACCEPT |
| 722 |
|
| 723 |
// ------------------------------------------------------------------- |
| 724 |
// Emit code. |
| 725 |
|
| 726 |
void ChoiceNode::GenerateGuard(RegExpMacroAssembler* macro_assembler, |
| 727 |
Guard* guard, Trace* trace) { |
| 728 |
switch (guard->op()) { |
| 729 |
case Guard::LT: |
| 730 |
DCHECK(!trace->mentions_reg(guard->reg())); |
| 731 |
macro_assembler->IfRegisterGE(guard->reg(), guard->value(), |
| 732 |
trace->backtrack()); |
| 733 |
break; |
| 734 |
case Guard::GEQ: |
| 735 |
DCHECK(!trace->mentions_reg(guard->reg())); |
| 736 |
macro_assembler->IfRegisterLT(guard->reg(), guard->value(), |
| 737 |
trace->backtrack()); |
| 738 |
break; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
namespace { |
| 743 |
|
| 744 |
#ifdef DEBUG |
| 745 |
bool ContainsOnlyUtf16CodeUnits(unibrow::uchar* chars, int length) { |
| 746 |
static_assert(sizeof(unibrow::uchar) == 4); |
| 747 |
for (int i = 0; i < length; i++) { |
| 748 |
if (chars[i] > String::kMaxUtf16CodeUnit) return false; |
| 749 |
} |
| 750 |
return true; |
| 751 |
} |
| 752 |
#endif // DEBUG |
| 753 |
|
| 754 |
// Returns the number of characters in the equivalence class, omitting those |
| 755 |
// that cannot occur in the source string because it is Latin1. This is called |
| 756 |
// both for unicode modes /ui and /vi, and also for legacy case independent |
| 757 |
// mode /i. In the case of Unicode modes we handled surrogate pair expansions |
| 758 |
// earlier so at this point it's all about single-code-unit expansions. |
| 759 |
int GetCaseIndependentLetters(Isolate* isolate, base::uc16 character, |
| 760 |
RegExpCompiler* compiler, unibrow::uchar* letters, |
| 761 |
int letter_length) { |
| 762 |
bool one_byte_subject = compiler->one_byte(); |
| 763 |
bool unicode = IsEitherUnicode(compiler->flags()); |
| 764 |
static const base::uc16 kMaxAscii = 0x7f; |
| 765 |
if (!unicode && character <= kMaxAscii) { |
| 766 |
// Fast case for common characters. |
| 767 |
base::uc16 upper = character & ~0x20; |
| 768 |
if ('A' <= upper && upper <= 'Z') { |
| 769 |
letters[0] = upper; |
| 770 |
letters[1] = upper | 0x20; |
| 771 |
return 2; |
| 772 |
} |
| 773 |
letters[0] = character; |
| 774 |
return 1; |
| 775 |
} |
| 776 |
#ifdef V8_INTL_SUPPORT |
| 777 |
|
| 778 |
if (!unicode && RegExpCaseFolding::IgnoreSet().contains(character)) { |
| 779 |
if (one_byte_subject && character > String::kMaxOneByteCharCode) { |
| 780 |
// This function promises not to return a character that is impossible |
| 781 |
// for the subject encoding. |
| 782 |
return 0; |
| 783 |
} |
| 784 |
letters[0] = character; |
| 785 |
DCHECK(ContainsOnlyUtf16CodeUnits(letters, 1)); |
| 786 |
return 1; |
| 787 |
} |
| 788 |
bool in_special_add_set = |
| 789 |
RegExpCaseFolding::SpecialAddSet().contains(character); |
| 790 |
|
| 791 |
icu::UnicodeSet set; |
| 792 |
set.add(character); |
| 793 |
set = set.closeOver(unicode ? USET_SIMPLE_CASE_INSENSITIVE |
| 794 |
: USET_CASE_INSENSITIVE); |
| 795 |
|
| 796 |
UChar32 canon = 0; |
| 797 |
if (in_special_add_set && !unicode) { |
| 798 |
canon = RegExpCaseFolding::Canonicalize(character); |
| 799 |
} |
| 800 |
|
| 801 |
int32_t range_count = set.getRangeCount(); |
| 802 |
int items = 0; |
| 803 |
for (int32_t i = 0; i < range_count; i++) { |
| 804 |
UChar32 start = set.getRangeStart(i); |
| 805 |
UChar32 end = set.getRangeEnd(i); |
| 806 |
CHECK(end - start + items <= letter_length); |
| 807 |
for (UChar32 cu = start; cu <= end; cu++) { |
| 808 |
if (one_byte_subject && cu > String::kMaxOneByteCharCode) continue; |
| 809 |
if (!unicode && in_special_add_set && |
| 810 |
RegExpCaseFolding::Canonicalize(cu) != canon) { |
| 811 |
continue; |
| 812 |
} |
| 813 |
letters[items++] = static_cast<unibrow::uchar>(cu); |
| 814 |
} |
| 815 |
} |
| 816 |
DCHECK(ContainsOnlyUtf16CodeUnits(letters, items)); |
| 817 |
return items; |
| 818 |
#else |
| 819 |
int length = |
| 820 |
isolate->jsregexp_uncanonicalize()->get(character, '\0', letters); |
| 821 |
// Unibrow returns 0 or 1 for characters where case independence is |
| 822 |
// trivial. |
| 823 |
if (length == 0) { |
| 824 |
letters[0] = character; |
| 825 |
length = 1; |
| 826 |
} |
| 827 |
|
| 828 |
if (one_byte_subject) { |
| 829 |
int new_length = 0; |
| 830 |
for (int i = 0; i < length; i++) { |
| 831 |
if (letters[i] <= String::kMaxOneByteCharCode) { |
| 832 |
letters[new_length++] = letters[i]; |
| 833 |
} |
| 834 |
} |
| 835 |
length = new_length; |
| 836 |
} |
| 837 |
|
| 838 |
DCHECK(ContainsOnlyUtf16CodeUnits(letters, length)); |
| 839 |
return length; |
| 840 |
#endif // V8_INTL_SUPPORT |
| 841 |
} |
| 842 |
|
| 843 |
inline bool EmitSimpleCharacter(Isolate* isolate, RegExpCompiler* compiler, |
| 844 |
base::uc16 c, Label* on_failure, int cp_offset, |
| 845 |
bool check, bool preloaded) { |
| 846 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 847 |
bool bound_checked = false; |
| 848 |
if (!preloaded) { |
| 849 |
assembler->LoadCurrentCharacter(cp_offset, on_failure, check); |
| 850 |
bound_checked = true; |
| 851 |
} |
| 852 |
assembler->CheckNotCharacter(c, on_failure); |
| 853 |
return bound_checked; |
| 854 |
} |
| 855 |
|
| 856 |
// Only emits non-letters (things that don't have case). Only used for case |
| 857 |
// independent matches. |
| 858 |
inline bool EmitAtomNonLetter(Isolate* isolate, RegExpCompiler* compiler, |
| 859 |
base::uc16 c, Label* on_failure, int cp_offset, |
| 860 |
bool check, bool preloaded) { |
| 861 |
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 862 |
bool one_byte = compiler->one_byte(); |
| 863 |
unibrow::uchar chars[4]; |
| 864 |
int length = GetCaseIndependentLetters(isolate, c, compiler, chars, 4); |
| 865 |
if (length < 1) { |
| 866 |
// This can't match. Must be an one-byte subject and a non-one-byte |
| 867 |
// character. We do not need to do anything since the one-byte pass |
| 868 |
// already handled this. |
| 869 |
CHECK(one_byte); |
| 870 |
return false; // Bounds not checked. |
| 871 |
} |
| 872 |
bool checked = false; |
| 873 |
// We handle the length > 1 case in a later pass. |
| 874 |
if (length == 1) { |
| 875 |
// GetCaseIndependentLetters promises not to return characters that can't |
| 876 |
// match because of the subject encoding. This case is already handled by |
| 877 |
// the one-byte pass. |
| 878 |
CHECK_IMPLIES(one_byte, chars[0] <= String::kMaxOneByteCharCodeU); |
| 879 |
if (!preloaded) { |
| 880 |
macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check); |
| 881 |
checked = check; |
| 882 |
} |
| 883 |
macro_assembler->CheckNotCharacter(chars[0], on_failure); |
| 884 |
} |
| 885 |
return checked; |
| 886 |
} |
| 887 |
|
| 888 |
bool ShortCutEmitCharacterPair(RegExpMacroAssembler* macro_assembler, |
| 889 |
bool one_byte, base::uc16 c1, base::uc16 c2, |
| 890 |
Label* on_failure) { |
| 891 |
const uint32_t char_mask = CharMask(one_byte); |
| 892 |
base::uc16 exor = c1 ^ c2; |
| 893 |
// Check whether exor has only one bit set. |
| 894 |
if (((exor - 1) & exor) == 0) { |
| 895 |
// If c1 and c2 differ only by one bit. |
| 896 |
// Ecma262UnCanonicalize always gives the highest number last. |
| 897 |
DCHECK(c2 > c1); |
| 898 |
base::uc16 mask = char_mask ^ exor; |
| 899 |
macro_assembler->CheckNotCharacterAfterAnd(c1, mask, on_failure); |
| 900 |
return true; |
| 901 |
} |
| 902 |
DCHECK(c2 > c1); |
| 903 |
base::uc16 diff = c2 - c1; |
| 904 |
if (((diff - 1) & diff) == 0 && c1 >= diff) { |
| 905 |
// If the characters differ by 2^n but don't differ by one bit then |
| 906 |
// subtract the difference from the found character, then do the or |
| 907 |
// trick. We avoid the theoretical case where negative numbers are |
| 908 |
// involved in order to simplify code generation. |
| 909 |
base::uc16 mask = char_mask ^ diff; |
| 910 |
macro_assembler->CheckNotCharacterAfterMinusAnd(c1 - diff, diff, mask, |
| 911 |
on_failure); |
| 912 |
return true; |
| 913 |
} |
| 914 |
return false; |
| 915 |
} |
| 916 |
|
| 917 |
// Only emits letters (things that have case). Only used for case independent |
| 918 |
// matches. |
| 919 |
inline bool EmitAtomLetter(Isolate* isolate, RegExpCompiler* compiler, |
| 920 |
base::uc16 c, Label* on_failure, int cp_offset, |
| 921 |
bool check, bool preloaded) { |
| 922 |
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 923 |
bool one_byte = compiler->one_byte(); |
| 924 |
unibrow::uchar chars[4]; |
| 925 |
int length = GetCaseIndependentLetters(isolate, c, compiler, chars, 4); |
| 926 |
// The 0 and 1 case are handled by earlier passes. |
| 927 |
if (length <= 1) return false; |
| 928 |
// We may not need to check against the end of the input string |
| 929 |
// if this character lies before a character that matched. |
| 930 |
if (!preloaded) { |
| 931 |
macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check); |
| 932 |
} |
| 933 |
Label ok; |
| 934 |
switch (length) { |
| 935 |
case 2: { |
| 936 |
if (ShortCutEmitCharacterPair(macro_assembler, one_byte, chars[0], |
| 937 |
chars[1], on_failure)) { |
| 938 |
} else { |
| 939 |
macro_assembler->CheckCharacter(chars[0], &ok); |
| 940 |
macro_assembler->CheckNotCharacter(chars[1], on_failure); |
| 941 |
macro_assembler->Bind(&ok); |
| 942 |
} |
| 943 |
break; |
| 944 |
} |
| 945 |
case 4: |
| 946 |
macro_assembler->CheckCharacter(chars[3], &ok); |
| 947 |
[[fallthrough]]; |
| 948 |
case 3: |
| 949 |
macro_assembler->CheckCharacter(chars[0], &ok); |
| 950 |
macro_assembler->CheckCharacter(chars[1], &ok); |
| 951 |
macro_assembler->CheckNotCharacter(chars[2], on_failure); |
| 952 |
macro_assembler->Bind(&ok); |
| 953 |
break; |
| 954 |
default: |
| 955 |
UNREACHABLE(); |
| 956 |
} |
| 957 |
return true; |
| 958 |
} |
| 959 |
|
| 960 |
void EmitBoundaryTest(RegExpMacroAssembler* masm, int border, |
| 961 |
Label* fall_through, Label* above_or_equal, |
| 962 |
Label* below) { |
| 963 |
if (below != fall_through) { |
| 964 |
masm->CheckCharacterLT(border, below); |
| 965 |
if (above_or_equal != fall_through) masm->GoTo(above_or_equal); |
| 966 |
} else { |
| 967 |
masm->CheckCharacterGT(border - 1, above_or_equal); |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
void EmitDoubleBoundaryTest(RegExpMacroAssembler* masm, int first, int last, |
| 972 |
Label* fall_through, Label* in_range, |
| 973 |
Label* out_of_range) { |
| 974 |
if (in_range == fall_through) { |
| 975 |
if (first == last) { |
| 976 |
masm->CheckNotCharacter(first, out_of_range); |
| 977 |
} else { |
| 978 |
masm->CheckCharacterNotInRange(first, last, out_of_range); |
| 979 |
} |
| 980 |
} else { |
| 981 |
if (first == last) { |
| 982 |
masm->CheckCharacter(first, in_range); |
| 983 |
} else { |
| 984 |
masm->CheckCharacterInRange(first, last, in_range); |
| 985 |
} |
| 986 |
if (out_of_range != fall_through) masm->GoTo(out_of_range); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
// even_label is for ranges[i] to ranges[i + 1] where i - start_index is even. |
| 991 |
// odd_label is for ranges[i] to ranges[i + 1] where i - start_index is odd. |
| 992 |
void EmitUseLookupTable(RegExpMacroAssembler* masm, |
| 993 |
ZoneList<base::uc32>* ranges, uint32_t start_index, |
| 994 |
uint32_t end_index, base::uc32 min_char, |
| 995 |
Label* fall_through, Label* even_label, |
| 996 |
Label* odd_label) { |
| 997 |
static const uint32_t kSize = RegExpMacroAssembler::kTableSize; |
| 998 |
static const uint32_t kMask = RegExpMacroAssembler::kTableMask; |
| 999 |
|
| 1000 |
base::uc32 base = (min_char & ~kMask); |
| 1001 |
USE(base); |
| 1002 |
|
| 1003 |
// Assert that everything is on one kTableSize page. |
| 1004 |
for (uint32_t i = start_index; i <= end_index; i++) { |
| 1005 |
DCHECK_EQ(ranges->at(i) & ~kMask, base); |
| 1006 |
} |
| 1007 |
DCHECK(start_index == 0 || (ranges->at(start_index - 1) & ~kMask) <= base); |
| 1008 |
|
| 1009 |
char templ[kSize]; |
| 1010 |
Label* on_bit_set; |
| 1011 |
Label* on_bit_clear; |
| 1012 |
int bit; |
| 1013 |
if (even_label == fall_through) { |
| 1014 |
on_bit_set = odd_label; |
| 1015 |
on_bit_clear = even_label; |
| 1016 |
bit = 1; |
| 1017 |
} else { |
| 1018 |
on_bit_set = even_label; |
| 1019 |
on_bit_clear = odd_label; |
| 1020 |
bit = 0; |
| 1021 |
} |
| 1022 |
for (uint32_t i = 0; i < (ranges->at(start_index) & kMask) && i < kSize; |
| 1023 |
i++) { |
| 1024 |
templ[i] = bit; |
| 1025 |
} |
| 1026 |
uint32_t j = 0; |
| 1027 |
bit ^= 1; |
| 1028 |
for (uint32_t i = start_index; i < end_index; i++) { |
| 1029 |
for (j = (ranges->at(i) & kMask); j < (ranges->at(i + 1) & kMask); j++) { |
| 1030 |
templ[j] = bit; |
| 1031 |
} |
| 1032 |
bit ^= 1; |
| 1033 |
} |
| 1034 |
for (uint32_t i = j; i < kSize; i++) { |
| 1035 |
templ[i] = bit; |
| 1036 |
} |
| 1037 |
Factory* factory = masm->isolate()->factory(); |
| 1038 |
// TODO(erikcorry): Cache these. |
| 1039 |
Handle<ByteArray> ba = factory->NewByteArray(kSize, AllocationType::kOld); |
| 1040 |
for (uint32_t i = 0; i < kSize; i++) { |
| 1041 |
ba->set(i, templ[i]); |
| 1042 |
} |
| 1043 |
masm->CheckBitInTable(ba, on_bit_set); |
| 1044 |
if (on_bit_clear != fall_through) masm->GoTo(on_bit_clear); |
| 1045 |
} |
| 1046 |
|
| 1047 |
void CutOutRange(RegExpMacroAssembler* masm, ZoneList<base::uc32>* ranges, |
| 1048 |
uint32_t start_index, uint32_t end_index, uint32_t cut_index, |
| 1049 |
Label* even_label, Label* odd_label) { |
| 1050 |
bool odd = (((cut_index - start_index) & 1) == 1); |
| 1051 |
Label* in_range_label = odd ? odd_label : even_label; |
| 1052 |
Label dummy; |
| 1053 |
EmitDoubleBoundaryTest(masm, ranges->at(cut_index), |
| 1054 |
ranges->at(cut_index + 1) - 1, &dummy, in_range_label, |
| 1055 |
&dummy); |
| 1056 |
DCHECK(!dummy.is_linked()); |
| 1057 |
// Cut out the single range by rewriting the array. This creates a new |
| 1058 |
// range that is a merger of the two ranges on either side of the one we |
| 1059 |
// are cutting out. The oddity of the labels is preserved. |
| 1060 |
for (uint32_t j = cut_index; j > start_index; j--) { |
| 1061 |
ranges->at(j) = ranges->at(j - 1); |
| 1062 |
} |
| 1063 |
for (uint32_t j = cut_index + 1; j < end_index; j++) { |
| 1064 |
ranges->at(j) = ranges->at(j + 1); |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
// Unicode case. Split the search space into kSize spaces that are handled |
| 1069 |
// with recursion. |
| 1070 |
void SplitSearchSpace(ZoneList<base::uc32>* ranges, uint32_t start_index, |
| 1071 |
uint32_t end_index, uint32_t* new_start_index, |
| 1072 |
uint32_t* new_end_index, base::uc32* border) { |
| 1073 |
static const uint32_t kSize = RegExpMacroAssembler::kTableSize; |
| 1074 |
static const uint32_t kMask = RegExpMacroAssembler::kTableMask; |
| 1075 |
|
| 1076 |
base::uc32 first = ranges->at(start_index); |
| 1077 |
base::uc32 last = ranges->at(end_index) - 1; |
| 1078 |
|
| 1079 |
*new_start_index = start_index; |
| 1080 |
*border = (ranges->at(start_index) & ~kMask) + kSize; |
| 1081 |
while (*new_start_index < end_index) { |
| 1082 |
if (ranges->at(*new_start_index) > *border) break; |
| 1083 |
(*new_start_index)++; |
| 1084 |
} |
| 1085 |
// new_start_index is the index of the first edge that is beyond the |
| 1086 |
// current kSize space. |
| 1087 |
|
| 1088 |
// For very large search spaces we do a binary chop search of the non-Latin1 |
| 1089 |
// space instead of just going to the end of the current kSize space. The |
| 1090 |
// heuristics are complicated a little by the fact that any 128-character |
| 1091 |
// encoding space can be quickly tested with a table lookup, so we don't |
| 1092 |
// wish to do binary chop search at a smaller granularity than that. A |
| 1093 |
// 128-character space can take up a lot of space in the ranges array if, |
| 1094 |
// for example, we only want to match every second character (eg. the lower |
| 1095 |
// case characters on some Unicode pages). |
| 1096 |
uint32_t binary_chop_index = (end_index + start_index) / 2; |
| 1097 |
// The first test ensures that we get to the code that handles the Latin1 |
| 1098 |
// range with a single not-taken branch, speeding up this important |
| 1099 |
// character range (even non-Latin1 charset-based text has spaces and |
| 1100 |
// punctuation). |
| 1101 |
if (*border - 1 > String::kMaxOneByteCharCode && // Latin1 case. |
| 1102 |
end_index - start_index > (*new_start_index - start_index) * 2 && |
| 1103 |
last - first > kSize * 2 && binary_chop_index > *new_start_index && |
| 1104 |
ranges->at(binary_chop_index) >= first + 2 * kSize) { |
| 1105 |
uint32_t scan_forward_for_section_border = binary_chop_index; |
| 1106 |
uint32_t new_border = (ranges->at(binary_chop_index) | kMask) + 1; |
| 1107 |
|
| 1108 |
while (scan_forward_for_section_border < end_index) { |
| 1109 |
if (ranges->at(scan_forward_for_section_border) > new_border) { |
| 1110 |
*new_start_index = scan_forward_for_section_border; |
| 1111 |
*border = new_border; |
| 1112 |
break; |
| 1113 |
} |
| 1114 |
scan_forward_for_section_border++; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
DCHECK(*new_start_index > start_index); |
| 1119 |
*new_end_index = *new_start_index - 1; |
| 1120 |
if (ranges->at(*new_end_index) == *border) { |
| 1121 |
(*new_end_index)--; |
| 1122 |
} |
| 1123 |
if (*border >= ranges->at(end_index)) { |
| 1124 |
*border = ranges->at(end_index); |
| 1125 |
*new_start_index = end_index; // Won't be used. |
| 1126 |
*new_end_index = end_index - 1; |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
// Gets a series of segment boundaries representing a character class. If the |
| 1131 |
// character is in the range between an even and an odd boundary (counting from |
| 1132 |
// start_index) then go to even_label, otherwise go to odd_label. We already |
| 1133 |
// know that the character is in the range of min_char to max_char inclusive. |
| 1134 |
// Either label can be nullptr indicating backtracking. Either label can also |
| 1135 |
// be equal to the fall_through label. |
| 1136 |
void GenerateBranches(RegExpMacroAssembler* masm, ZoneList<base::uc32>* ranges, |
| 1137 |
uint32_t start_index, uint32_t end_index, |
| 1138 |
base::uc32 min_char, base::uc32 max_char, |
| 1139 |
Label* fall_through, Label* even_label, |
| 1140 |
Label* odd_label) { |
| 1141 |
DCHECK_LE(min_char, String::kMaxUtf16CodeUnit); |
| 1142 |
DCHECK_LE(max_char, String::kMaxUtf16CodeUnit); |
| 1143 |
|
| 1144 |
base::uc32 first = ranges->at(start_index); |
| 1145 |
base::uc32 last = ranges->at(end_index) - 1; |
| 1146 |
|
| 1147 |
DCHECK_LT(min_char, first); |
| 1148 |
|
| 1149 |
// Just need to test if the character is before or on-or-after |
| 1150 |
// a particular character. |
| 1151 |
if (start_index == end_index) { |
| 1152 |
EmitBoundaryTest(masm, first, fall_through, even_label, odd_label); |
| 1153 |
return; |
| 1154 |
} |
| 1155 |
|
| 1156 |
// Another almost trivial case: There is one interval in the middle that is |
| 1157 |
// different from the end intervals. |
| 1158 |
if (start_index + 1 == end_index) { |
| 1159 |
EmitDoubleBoundaryTest(masm, first, last, fall_through, even_label, |
| 1160 |
odd_label); |
| 1161 |
return; |
| 1162 |
} |
| 1163 |
|
| 1164 |
// It's not worth using table lookup if there are very few intervals in the |
| 1165 |
// character class. |
| 1166 |
if (end_index - start_index <= 6) { |
| 1167 |
// It is faster to test for individual characters, so we look for those |
| 1168 |
// first, then try arbitrary ranges in the second round. |
| 1169 |
static uint32_t kNoCutIndex = -1; |
| 1170 |
uint32_t cut = kNoCutIndex; |
| 1171 |
for (uint32_t i = start_index; i < end_index; i++) { |
| 1172 |
if (ranges->at(i) == ranges->at(i + 1) - 1) { |
| 1173 |
cut = i; |
| 1174 |
break; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
if (cut == kNoCutIndex) cut = start_index; |
| 1178 |
CutOutRange(masm, ranges, start_index, end_index, cut, even_label, |
| 1179 |
odd_label); |
| 1180 |
DCHECK_GE(end_index - start_index, 2); |
| 1181 |
GenerateBranches(masm, ranges, start_index + 1, end_index - 1, min_char, |
| 1182 |
max_char, fall_through, even_label, odd_label); |
| 1183 |
return; |
| 1184 |
} |
| 1185 |
|
| 1186 |
// If there are a lot of intervals in the regexp, then we will use tables to |
| 1187 |
// determine whether the character is inside or outside the character class. |
| 1188 |
static const int kBits = RegExpMacroAssembler::kTableSizeBits; |
| 1189 |
|
| 1190 |
if ((max_char >> kBits) == (min_char >> kBits)) { |
| 1191 |
EmitUseLookupTable(masm, ranges, start_index, end_index, min_char, |
| 1192 |
fall_through, even_label, odd_label); |
| 1193 |
return; |
| 1194 |
} |
| 1195 |
|
| 1196 |
if ((min_char >> kBits) != first >> kBits) { |
| 1197 |
masm->CheckCharacterLT(first, odd_label); |
| 1198 |
GenerateBranches(masm, ranges, start_index + 1, end_index, first, max_char, |
| 1199 |
fall_through, odd_label, even_label); |
| 1200 |
return; |
| 1201 |
} |
| 1202 |
|
| 1203 |
uint32_t new_start_index = 0; |
| 1204 |
uint32_t new_end_index = 0; |
| 1205 |
base::uc32 border = 0; |
| 1206 |
|
| 1207 |
SplitSearchSpace(ranges, start_index, end_index, &new_start_index, |
| 1208 |
&new_end_index, &border); |
| 1209 |
|
| 1210 |
Label handle_rest; |
| 1211 |
Label* above = &handle_rest; |
| 1212 |
if (border == last + 1) { |
| 1213 |
// We didn't find any section that started after the limit, so everything |
| 1214 |
// above the border is one of the terminal labels. |
| 1215 |
above = (end_index & 1) != (start_index & 1) ? odd_label : even_label; |
| 1216 |
DCHECK(new_end_index == end_index - 1); |
| 1217 |
} |
| 1218 |
|
| 1219 |
DCHECK_LE(start_index, new_end_index); |
| 1220 |
DCHECK_LE(new_start_index, end_index); |
| 1221 |
DCHECK_LT(start_index, new_start_index); |
| 1222 |
DCHECK_LT(new_end_index, end_index); |
| 1223 |
DCHECK(new_end_index + 1 == new_start_index || |
| 1224 |
(new_end_index + 2 == new_start_index && |
| 1225 |
border == ranges->at(new_end_index + 1))); |
| 1226 |
DCHECK_LT(min_char, border - 1); |
| 1227 |
DCHECK_LT(border, max_char); |
| 1228 |
DCHECK_LT(ranges->at(new_end_index), border); |
| 1229 |
DCHECK(border < ranges->at(new_start_index) || |
| 1230 |
(border == ranges->at(new_start_index) && |
| 1231 |
new_start_index == end_index && new_end_index == end_index - 1 && |
| 1232 |
border == last + 1)); |
| 1233 |
DCHECK(new_start_index == 0 || border >= ranges->at(new_start_index - 1)); |
| 1234 |
|
| 1235 |
masm->CheckCharacterGT(border - 1, above); |
| 1236 |
Label dummy; |
| 1237 |
GenerateBranches(masm, ranges, start_index, new_end_index, min_char, |
| 1238 |
border - 1, &dummy, even_label, odd_label); |
| 1239 |
if (handle_rest.is_linked()) { |
| 1240 |
masm->Bind(&handle_rest); |
| 1241 |
bool flip = (new_start_index & 1) != (start_index & 1); |
| 1242 |
GenerateBranches(masm, ranges, new_start_index, end_index, border, max_char, |
| 1243 |
&dummy, flip ? odd_label : even_label, |
| 1244 |
flip ? even_label : odd_label); |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
void EmitClassRanges(RegExpMacroAssembler* macro_assembler, |
| 1249 |
RegExpClassRanges* cr, bool one_byte, Label* on_failure, |
| 1250 |
int cp_offset, bool check_offset, bool preloaded, |
| 1251 |
Zone* zone) { |
| 1252 |
ZoneList<CharacterRange>* ranges = cr->ranges(zone); |
| 1253 |
CharacterRange::Canonicalize(ranges); |
| 1254 |
|
| 1255 |
// Now that all processing (like case-insensitivity) is done, clamp the |
| 1256 |
// ranges to the set of ranges that may actually occur in the subject string. |
| 1257 |
if (one_byte) CharacterRange::ClampToOneByte(ranges); |
| 1258 |
|
| 1259 |
const int ranges_length = ranges->length(); |
| 1260 |
if (ranges_length == 0) { |
| 1261 |
if (!cr->is_negated()) { |
| 1262 |
macro_assembler->GoTo(on_failure); |
| 1263 |
} |
| 1264 |
if (check_offset) { |
| 1265 |
macro_assembler->CheckPosition(cp_offset, on_failure); |
| 1266 |
} |
| 1267 |
return; |
| 1268 |
} |
| 1269 |
|
| 1270 |
const base::uc32 max_char = MaxCodeUnit(one_byte); |
| 1271 |
if (ranges_length == 1 && ranges->at(0).IsEverything(max_char)) { |
| 1272 |
if (cr->is_negated()) { |
| 1273 |
macro_assembler->GoTo(on_failure); |
| 1274 |
} else { |
| 1275 |
// This is a common case hit by non-anchored expressions. |
| 1276 |
if (check_offset) { |
| 1277 |
macro_assembler->CheckPosition(cp_offset, on_failure); |
| 1278 |
} |
| 1279 |
} |
| 1280 |
return; |
| 1281 |
} |
| 1282 |
|
| 1283 |
if (!preloaded) { |
| 1284 |
macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check_offset); |
| 1285 |
} |
| 1286 |
|
| 1287 |
if (cr->is_standard(zone) && macro_assembler->CheckSpecialClassRanges( |
| 1288 |
cr->standard_type(), on_failure)) { |
| 1289 |
return; |
| 1290 |
} |
| 1291 |
|
| 1292 |
static constexpr int kMaxRangesForInlineBranchGeneration = 16; |
| 1293 |
if (ranges_length > kMaxRangesForInlineBranchGeneration) { |
| 1294 |
// For large range sets, emit a more compact instruction sequence to avoid |
| 1295 |
// a potentially problematic increase in code size. |
| 1296 |
// Note the flipped logic below (we check InRange if negated, NotInRange if |
| 1297 |
// not negated); this is necessary since the method falls through on |
| 1298 |
// failure whereas we want to fall through on success. |
| 1299 |
if (cr->is_negated()) { |
| 1300 |
if (macro_assembler->CheckCharacterInRangeArray(ranges, on_failure)) { |
| 1301 |
return; |
| 1302 |
} |
| 1303 |
} else { |
| 1304 |
if (macro_assembler->CheckCharacterNotInRangeArray(ranges, on_failure)) { |
| 1305 |
return; |
| 1306 |
} |
| 1307 |
} |
| 1308 |
} |
| 1309 |
|
| 1310 |
// Generate a flat list of range boundaries for consumption by |
| 1311 |
// GenerateBranches. See the comment on that function for how the list should |
| 1312 |
// be structured |
| 1313 |
ZoneList<base::uc32>* range_boundaries = |
| 1314 |
zone->New<ZoneList<base::uc32>>(ranges_length * 2, zone); |
| 1315 |
|
| 1316 |
bool zeroth_entry_is_failure = !cr->is_negated(); |
| 1317 |
|
| 1318 |
for (int i = 0; i < ranges_length; i++) { |
| 1319 |
CharacterRange& range = ranges->at(i); |
| 1320 |
if (range.from() == 0) { |
| 1321 |
DCHECK_EQ(i, 0); |
| 1322 |
zeroth_entry_is_failure = !zeroth_entry_is_failure; |
| 1323 |
} else { |
| 1324 |
range_boundaries->Add(range.from(), zone); |
| 1325 |
} |
| 1326 |
// `+ 1` to convert from inclusive to exclusive `to`. |
| 1327 |
// [from, to] == [from, to+1[. |
| 1328 |
range_boundaries->Add(range.to() + 1, zone); |
| 1329 |
} |
| 1330 |
int end_index = range_boundaries->length() - 1; |
| 1331 |
if (range_boundaries->at(end_index) > max_char) { |
| 1332 |
end_index--; |
| 1333 |
} |
| 1334 |
|
| 1335 |
Label fall_through; |
| 1336 |
GenerateBranches(macro_assembler, range_boundaries, |
| 1337 |
0, // start_index. |
| 1338 |
end_index, |
| 1339 |
0, // min_char. |
| 1340 |
max_char, &fall_through, |
| 1341 |
zeroth_entry_is_failure ? &fall_through : on_failure, |
| 1342 |
zeroth_entry_is_failure ? on_failure : &fall_through); |
| 1343 |
macro_assembler->Bind(&fall_through); |
| 1344 |
} |
| 1345 |
|
| 1346 |
} // namespace |
| 1347 |
|
| 1348 |
RegExpNode::~RegExpNode() = default; |
| 1349 |
|
| 1350 |
RegExpNode::LimitResult RegExpNode::LimitVersions(RegExpCompiler* compiler, |
| 1351 |
Trace* trace) { |
| 1352 |
// If we are generating a greedy loop then don't stop and don't reuse code. |
| 1353 |
if (trace->stop_node() != nullptr) { |
| 1354 |
return CONTINUE; |
| 1355 |
} |
| 1356 |
|
| 1357 |
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 1358 |
if (trace->is_trivial()) { |
| 1359 |
if (label_.is_bound() || on_work_list() || !KeepRecursing(compiler)) { |
| 1360 |
// If a generic version is already scheduled to be generated or we have |
| 1361 |
// recursed too deeply then just generate a jump to that code. |
| 1362 |
macro_assembler->GoTo(&label_); |
| 1363 |
// This will queue it up for generation of a generic version if it hasn't |
| 1364 |
// already been queued. |
| 1365 |
compiler->AddWork(this); |
| 1366 |
return DONE; |
| 1367 |
} |
| 1368 |
// Generate generic version of the node and bind the label for later use. |
| 1369 |
macro_assembler->Bind(&label_); |
| 1370 |
return CONTINUE; |
| 1371 |
} |
| 1372 |
|
| 1373 |
// We are being asked to make a non-generic version. Keep track of how many |
| 1374 |
// non-generic versions we generate so as not to overdo it. |
| 1375 |
trace_count_++; |
| 1376 |
if (KeepRecursing(compiler) && compiler->optimize() && |
| 1377 |
trace_count_ < kMaxCopiesCodeGenerated) { |
| 1378 |
return CONTINUE; |
| 1379 |
} |
| 1380 |
|
| 1381 |
// If we get here code has been generated for this node too many times or |
| 1382 |
// recursion is too deep. Time to switch to a generic version. The code for |
| 1383 |
// generic versions above can handle deep recursion properly. |
| 1384 |
bool was_limiting = compiler->limiting_recursion(); |
| 1385 |
compiler->set_limiting_recursion(true); |
| 1386 |
trace->Flush(compiler, this); |
| 1387 |
compiler->set_limiting_recursion(was_limiting); |
| 1388 |
return DONE; |
| 1389 |
} |
| 1390 |
|
| 1391 |
bool RegExpNode::KeepRecursing(RegExpCompiler* compiler) { |
| 1392 |
return !compiler->limiting_recursion() && |
| 1393 |
compiler->recursion_depth() <= RegExpCompiler::kMaxRecursion; |
| 1394 |
} |
| 1395 |
|
| 1396 |
void ActionNode::FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 1397 |
BoyerMooreLookahead* bm, bool not_at_start) { |
| 1398 |
std::optional<RegExpFlags> old_flags; |
| 1399 |
if (action_type_ == MODIFY_FLAGS) { |
| 1400 |
// It is not guaranteed that we hit the resetting modify flags node, due to |
| 1401 |
// recursion budget limitation for filling in BMInfo. Therefore we reset the |
| 1402 |
// flags manually to the previous state after recursing. |
| 1403 |
old_flags = bm->compiler()->flags(); |
| 1404 |
bm->compiler()->set_flags(flags()); |
| 1405 |
} |
| 1406 |
if (action_type_ == BEGIN_POSITIVE_SUBMATCH) { |
| 1407 |
// We use the node after the lookaround to fill in the eats_at_least info |
| 1408 |
// so we have to use the same node to fill in the Boyer-Moore info. |
| 1409 |
success_node()->on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, |
| 1410 |
not_at_start); |
| 1411 |
} else if (action_type_ != POSITIVE_SUBMATCH_SUCCESS) { |
| 1412 |
// We don't use the node after a positive submatch success because it |
| 1413 |
// rewinds the position. Since we returned 0 as the eats_at_least value for |
| 1414 |
// this node, we don't need to fill in any data. |
| 1415 |
on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); |
| 1416 |
} |
| 1417 |
SaveBMInfo(bm, not_at_start, offset); |
| 1418 |
if (old_flags.has_value()) { |
| 1419 |
bm->compiler()->set_flags(*old_flags); |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
void ActionNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| 1424 |
RegExpCompiler* compiler, int filled_in, |
| 1425 |
bool not_at_start) { |
| 1426 |
if (action_type_ == SET_REGISTER_FOR_LOOP) { |
| 1427 |
on_success()->GetQuickCheckDetailsFromLoopEntry(details, compiler, |
| 1428 |
filled_in, not_at_start); |
| 1429 |
} else if (action_type_ == BEGIN_POSITIVE_SUBMATCH) { |
| 1430 |
// We use the node after the lookaround to fill in the eats_at_least info |
| 1431 |
// so we have to use the same node to fill in the QuickCheck info. |
| 1432 |
success_node()->on_success()->GetQuickCheckDetails(details, compiler, |
| 1433 |
filled_in, not_at_start); |
| 1434 |
} else if (action_type() != POSITIVE_SUBMATCH_SUCCESS) { |
| 1435 |
// We don't use the node after a positive submatch success because it |
| 1436 |
// rewinds the position. Since we returned 0 as the eats_at_least value |
| 1437 |
// for this node, we don't need to fill in any data. |
| 1438 |
std::optional<RegExpFlags> old_flags; |
| 1439 |
if (action_type() == MODIFY_FLAGS) { |
| 1440 |
// It is not guaranteed that we hit the resetting modify flags node, as |
| 1441 |
// GetQuickCheckDetails doesn't travers the whole graph. Therefore we |
| 1442 |
// reset the flags manually to the previous state after recursing. |
| 1443 |
old_flags = compiler->flags(); |
| 1444 |
compiler->set_flags(flags()); |
| 1445 |
} |
| 1446 |
on_success()->GetQuickCheckDetails(details, compiler, filled_in, |
| 1447 |
not_at_start); |
| 1448 |
if (old_flags.has_value()) { |
| 1449 |
compiler->set_flags(*old_flags); |
| 1450 |
} |
| 1451 |
} |
| 1452 |
} |
| 1453 |
|
| 1454 |
void AssertionNode::FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 1455 |
BoyerMooreLookahead* bm, bool not_at_start) { |
| 1456 |
// Match the behaviour of EatsAtLeast on this node. |
| 1457 |
if (assertion_type() == AT_START && not_at_start) return; |
| 1458 |
on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); |
| 1459 |
SaveBMInfo(bm, not_at_start, offset); |
| 1460 |
} |
| 1461 |
|
| 1462 |
void NegativeLookaroundChoiceNode::GetQuickCheckDetails( |
| 1463 |
QuickCheckDetails* details, RegExpCompiler* compiler, int filled_in, |
| 1464 |
bool not_at_start) { |
| 1465 |
RegExpNode* node = continue_node(); |
| 1466 |
return node->GetQuickCheckDetails(details, compiler, filled_in, not_at_start); |
| 1467 |
} |
| 1468 |
|
| 1469 |
namespace { |
| 1470 |
|
| 1471 |
// Takes the left-most 1-bit and smears it out, setting all bits to its right. |
| 1472 |
inline uint32_t SmearBitsRight(uint32_t v) { |
| 1473 |
v |= v >> 1; |
| 1474 |
v |= v >> 2; |
| 1475 |
v |= v >> 4; |
| 1476 |
v |= v >> 8; |
| 1477 |
v |= v >> 16; |
| 1478 |
return v; |
| 1479 |
} |
| 1480 |
|
| 1481 |
} // namespace |
| 1482 |
|
| 1483 |
bool QuickCheckDetails::Rationalize(bool asc) { |
| 1484 |
bool found_useful_op = false; |
| 1485 |
const uint32_t char_mask = CharMask(asc); |
| 1486 |
mask_ = 0; |
| 1487 |
value_ = 0; |
| 1488 |
int char_shift = 0; |
| 1489 |
for (int i = 0; i < characters_; i++) { |
| 1490 |
Position* pos = &positions_[i]; |
| 1491 |
if ((pos->mask & String::kMaxOneByteCharCode) != 0) { |
| 1492 |
found_useful_op = true; |
| 1493 |
} |
| 1494 |
mask_ |= (pos->mask & char_mask) << char_shift; |
| 1495 |
value_ |= (pos->value & char_mask) << char_shift; |
| 1496 |
char_shift += asc ? 8 : 16; |
| 1497 |
} |
| 1498 |
return found_useful_op; |
| 1499 |
} |
| 1500 |
|
| 1501 |
uint32_t RegExpNode::EatsAtLeast(bool not_at_start) { |
| 1502 |
return not_at_start ? eats_at_least_.eats_at_least_from_not_start |
| 1503 |
: eats_at_least_.eats_at_least_from_possibly_start; |
| 1504 |
} |
| 1505 |
|
| 1506 |
EatsAtLeastInfo RegExpNode::EatsAtLeastFromLoopEntry() { |
| 1507 |
// SET_REGISTER_FOR_LOOP is only used to initialize loop counters, and it |
| 1508 |
// implies that the following node must be a LoopChoiceNode. If we need to |
| 1509 |
// set registers to constant values for other reasons, we could introduce a |
| 1510 |
// new action type SET_REGISTER that doesn't imply anything about its |
| 1511 |
// successor. |
| 1512 |
UNREACHABLE(); |
| 1513 |
} |
| 1514 |
|
| 1515 |
void RegExpNode::GetQuickCheckDetailsFromLoopEntry(QuickCheckDetails* details, |
| 1516 |
RegExpCompiler* compiler, |
| 1517 |
int characters_filled_in, |
| 1518 |
bool not_at_start) { |
| 1519 |
// See comment in RegExpNode::EatsAtLeastFromLoopEntry. |
| 1520 |
UNREACHABLE(); |
| 1521 |
} |
| 1522 |
|
| 1523 |
EatsAtLeastInfo LoopChoiceNode::EatsAtLeastFromLoopEntry() { |
| 1524 |
DCHECK_EQ(alternatives_->length(), 2); // There's just loop and continue. |
| 1525 |
|
| 1526 |
if (read_backward()) { |
| 1527 |
// The eats_at_least value is not used if reading backward. The |
| 1528 |
// EatsAtLeastPropagator should've zeroed it as well. |
| 1529 |
DCHECK_EQ(eats_at_least_info()->eats_at_least_from_possibly_start, 0); |
| 1530 |
DCHECK_EQ(eats_at_least_info()->eats_at_least_from_not_start, 0); |
| 1531 |
return {}; |
| 1532 |
} |
| 1533 |
|
| 1534 |
// Figure out how much the loop body itself eats, not including anything in |
| 1535 |
// the continuation case. In general, the nodes in the loop body should report |
| 1536 |
// that they eat at least the number eaten by the continuation node, since any |
| 1537 |
// successful match in the loop body must also include the continuation node. |
| 1538 |
// However, in some cases involving positive lookaround, the loop body under- |
| 1539 |
// reports its appetite, so use saturated math here to avoid negative numbers. |
| 1540 |
// For this to work correctly, we explicitly need to use signed integers here. |
| 1541 |
uint8_t loop_body_from_not_start = base::saturated_cast<uint8_t>( |
| 1542 |
static_cast<int>(loop_node_->EatsAtLeast(true)) - |
| 1543 |
static_cast<int>(continue_node_->EatsAtLeast(true))); |
| 1544 |
uint8_t loop_body_from_possibly_start = base::saturated_cast<uint8_t>( |
| 1545 |
static_cast<int>(loop_node_->EatsAtLeast(false)) - |
| 1546 |
static_cast<int>(continue_node_->EatsAtLeast(true))); |
| 1547 |
|
| 1548 |
// Limit the number of loop iterations to avoid overflow in subsequent steps. |
| 1549 |
int loop_iterations = base::saturated_cast<uint8_t>(min_loop_iterations()); |
| 1550 |
|
| 1551 |
EatsAtLeastInfo result; |
| 1552 |
result.eats_at_least_from_not_start = |
| 1553 |
base::saturated_cast<uint8_t>(loop_iterations * loop_body_from_not_start + |
| 1554 |
continue_node_->EatsAtLeast(true)); |
| 1555 |
if (loop_iterations > 0 && loop_body_from_possibly_start > 0) { |
| 1556 |
// First loop iteration eats at least one, so all subsequent iterations |
| 1557 |
// and the after-loop chunk are guaranteed to not be at the start. |
| 1558 |
result.eats_at_least_from_possibly_start = base::saturated_cast<uint8_t>( |
| 1559 |
loop_body_from_possibly_start + |
| 1560 |
(loop_iterations - 1) * loop_body_from_not_start + |
| 1561 |
continue_node_->EatsAtLeast(true)); |
| 1562 |
} else { |
| 1563 |
// Loop body might eat nothing, so only continue node contributes. |
| 1564 |
result.eats_at_least_from_possibly_start = |
| 1565 |
continue_node_->EatsAtLeast(false); |
| 1566 |
} |
| 1567 |
return result; |
| 1568 |
} |
| 1569 |
|
| 1570 |
bool RegExpNode::EmitQuickCheck(RegExpCompiler* compiler, |
| 1571 |
Trace* bounds_check_trace, Trace* trace, |
| 1572 |
bool preload_has_checked_bounds, |
| 1573 |
Label* on_possible_success, |
| 1574 |
QuickCheckDetails* details, |
| 1575 |
bool fall_through_on_failure, |
| 1576 |
ChoiceNode* predecessor) { |
| 1577 |
DCHECK_NOT_NULL(predecessor); |
| 1578 |
if (details->characters() == 0) return false; |
| 1579 |
GetQuickCheckDetails(details, compiler, 0, |
| 1580 |
trace->at_start() == Trace::FALSE_VALUE); |
| 1581 |
if (details->cannot_match()) return false; |
| 1582 |
if (!details->Rationalize(compiler->one_byte())) return false; |
| 1583 |
DCHECK(details->characters() == 1 || |
| 1584 |
compiler->macro_assembler()->CanReadUnaligned()); |
| 1585 |
uint32_t mask = details->mask(); |
| 1586 |
uint32_t value = details->value(); |
| 1587 |
|
| 1588 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 1589 |
|
| 1590 |
if (trace->characters_preloaded() != details->characters()) { |
| 1591 |
DCHECK(trace->cp_offset() == bounds_check_trace->cp_offset()); |
| 1592 |
// The bounds check is performed using the minimum number of characters |
| 1593 |
// any choice would eat, so if the bounds check fails, then none of the |
| 1594 |
// choices can succeed, so we can just immediately backtrack, rather |
| 1595 |
// than go to the next choice. The number of characters preloaded may be |
| 1596 |
// less than the number used for the bounds check. |
| 1597 |
int eats_at_least = predecessor->EatsAtLeast( |
| 1598 |
bounds_check_trace->at_start() == Trace::FALSE_VALUE); |
| 1599 |
DCHECK_GE(eats_at_least, details->characters()); |
| 1600 |
assembler->LoadCurrentCharacter( |
| 1601 |
trace->cp_offset(), bounds_check_trace->backtrack(), |
| 1602 |
!preload_has_checked_bounds, details->characters(), eats_at_least); |
| 1603 |
} |
| 1604 |
|
| 1605 |
bool need_mask = true; |
| 1606 |
|
| 1607 |
if (details->characters() == 1) { |
| 1608 |
// If number of characters preloaded is 1 then we used a byte or 16 bit |
| 1609 |
// load so the value is already masked down. |
| 1610 |
const uint32_t char_mask = CharMask(compiler->one_byte()); |
| 1611 |
if ((mask & char_mask) == char_mask) need_mask = false; |
| 1612 |
mask &= char_mask; |
| 1613 |
} else { |
| 1614 |
// For 2-character preloads in one-byte mode or 1-character preloads in |
| 1615 |
// two-byte mode we also use a 16 bit load with zero extend. |
| 1616 |
static const uint32_t kTwoByteMask = 0xFFFF; |
| 1617 |
static const uint32_t kFourByteMask = 0xFFFFFFFF; |
| 1618 |
if (details->characters() == 2 && compiler->one_byte()) { |
| 1619 |
if ((mask & kTwoByteMask) == kTwoByteMask) need_mask = false; |
| 1620 |
} else if (details->characters() == 1 && !compiler->one_byte()) { |
| 1621 |
if ((mask & kTwoByteMask) == kTwoByteMask) need_mask = false; |
| 1622 |
} else { |
| 1623 |
if (mask == kFourByteMask) need_mask = false; |
| 1624 |
} |
| 1625 |
} |
| 1626 |
|
| 1627 |
if (fall_through_on_failure) { |
| 1628 |
if (need_mask) { |
| 1629 |
assembler->CheckCharacterAfterAnd(value, mask, on_possible_success); |
| 1630 |
} else { |
| 1631 |
assembler->CheckCharacter(value, on_possible_success); |
| 1632 |
} |
| 1633 |
} else { |
| 1634 |
if (need_mask) { |
| 1635 |
assembler->CheckNotCharacterAfterAnd(value, mask, trace->backtrack()); |
| 1636 |
} else { |
| 1637 |
assembler->CheckNotCharacter(value, trace->backtrack()); |
| 1638 |
} |
| 1639 |
} |
| 1640 |
return true; |
| 1641 |
} |
| 1642 |
|
| 1643 |
// Here is the meat of GetQuickCheckDetails (see also the comment on the |
| 1644 |
// super-class in the .h file). |
| 1645 |
// |
| 1646 |
// We iterate along the text object, building up for each character a |
| 1647 |
// mask and value that can be used to test for a quick failure to match. |
| 1648 |
// The masks and values for the positions will be combined into a single |
| 1649 |
// machine word for the current character width in order to be used in |
| 1650 |
// generating a quick check. |
| 1651 |
void TextNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| 1652 |
RegExpCompiler* compiler, |
| 1653 |
int characters_filled_in, |
| 1654 |
bool not_at_start) { |
| 1655 |
// Do not collect any quick check details if the text node reads backward, |
| 1656 |
// since it reads in the opposite direction than we use for quick checks. |
| 1657 |
if (read_backward()) return; |
| 1658 |
Isolate* isolate = compiler->macro_assembler()->isolate(); |
| 1659 |
DCHECK(characters_filled_in < details->characters()); |
| 1660 |
int characters = details->characters(); |
| 1661 |
const uint32_t char_mask = CharMask(compiler->one_byte()); |
| 1662 |
for (int k = 0; k < elements()->length(); k++) { |
| 1663 |
TextElement elm = elements()->at(k); |
| 1664 |
if (elm.text_type() == TextElement::ATOM) { |
| 1665 |
base::Vector<const base::uc16> quarks = elm.atom()->data(); |
| 1666 |
for (int i = 0; i < characters && i < quarks.length(); i++) { |
| 1667 |
QuickCheckDetails::Position* pos = |
| 1668 |
details->positions(characters_filled_in); |
| 1669 |
base::uc16 c = quarks[i]; |
| 1670 |
if (IsIgnoreCase(compiler->flags())) { |
| 1671 |
unibrow::uchar chars[4]; |
| 1672 |
int length = |
| 1673 |
GetCaseIndependentLetters(isolate, c, compiler, chars, 4); |
| 1674 |
if (length == 0) { |
| 1675 |
// This can happen because all case variants are non-Latin1, but we |
| 1676 |
// know the input is Latin1. |
| 1677 |
details->set_cannot_match(); |
| 1678 |
pos->determines_perfectly = false; |
| 1679 |
return; |
| 1680 |
} |
| 1681 |
if (length == 1) { |
| 1682 |
// This letter has no case equivalents, so it's nice and simple |
| 1683 |
// and the mask-compare will determine definitely whether we have |
| 1684 |
// a match at this character position. |
| 1685 |
pos->mask = char_mask; |
| 1686 |
pos->value = chars[0]; |
| 1687 |
pos->determines_perfectly = true; |
| 1688 |
} else { |
| 1689 |
uint32_t common_bits = char_mask; |
| 1690 |
uint32_t bits = chars[0]; |
| 1691 |
for (int j = 1; j < length; j++) { |
| 1692 |
uint32_t differing_bits = ((chars[j] & common_bits) ^ bits); |
| 1693 |
common_bits ^= differing_bits; |
| 1694 |
bits &= common_bits; |
| 1695 |
} |
| 1696 |
// If length is 2 and common bits has only one zero in it then |
| 1697 |
// our mask and compare instruction will determine definitely |
| 1698 |
// whether we have a match at this character position. Otherwise |
| 1699 |
// it can only be an approximate check. |
| 1700 |
uint32_t one_zero = (common_bits | ~char_mask); |
| 1701 |
if (length == 2 && ((~one_zero) & ((~one_zero) - 1)) == 0) { |
| 1702 |
pos->determines_perfectly = true; |
| 1703 |
} |
| 1704 |
pos->mask = common_bits; |
| 1705 |
pos->value = bits; |
| 1706 |
} |
| 1707 |
} else { |
| 1708 |
// Don't ignore case. Nice simple case where the mask-compare will |
| 1709 |
// determine definitely whether we have a match at this character |
| 1710 |
// position. |
| 1711 |
if (c > char_mask) { |
| 1712 |
details->set_cannot_match(); |
| 1713 |
pos->determines_perfectly = false; |
| 1714 |
return; |
| 1715 |
} |
| 1716 |
pos->mask = char_mask; |
| 1717 |
pos->value = c; |
| 1718 |
pos->determines_perfectly = true; |
| 1719 |
} |
| 1720 |
characters_filled_in++; |
| 1721 |
DCHECK(characters_filled_in <= details->characters()); |
| 1722 |
if (characters_filled_in == details->characters()) { |
| 1723 |
return; |
| 1724 |
} |
| 1725 |
} |
| 1726 |
} else { |
| 1727 |
QuickCheckDetails::Position* pos = |
| 1728 |
details->positions(characters_filled_in); |
| 1729 |
RegExpClassRanges* tree = elm.class_ranges(); |
| 1730 |
ZoneList<CharacterRange>* ranges = tree->ranges(zone()); |
| 1731 |
if (tree->is_negated() || ranges->is_empty()) { |
| 1732 |
// A quick check uses multi-character mask and compare. There is no |
| 1733 |
// useful way to incorporate a negative char class into this scheme |
| 1734 |
// so we just conservatively create a mask and value that will always |
| 1735 |
// succeed. |
| 1736 |
// Likewise for empty ranges (empty ranges can occur e.g. when |
| 1737 |
// compiling for one-byte subjects and impossible (non-one-byte) ranges |
| 1738 |
// have been removed). |
| 1739 |
pos->mask = 0; |
| 1740 |
pos->value = 0; |
| 1741 |
} else { |
| 1742 |
int first_range = 0; |
| 1743 |
while (ranges->at(first_range).from() > char_mask) { |
| 1744 |
first_range++; |
| 1745 |
if (first_range == ranges->length()) { |
| 1746 |
details->set_cannot_match(); |
| 1747 |
pos->determines_perfectly = false; |
| 1748 |
return; |
| 1749 |
} |
| 1750 |
} |
| 1751 |
CharacterRange range = ranges->at(first_range); |
| 1752 |
const base::uc32 first_from = range.from(); |
| 1753 |
const base::uc32 first_to = |
| 1754 |
(range.to() > char_mask) ? char_mask : range.to(); |
| 1755 |
const uint32_t differing_bits = (first_from ^ first_to); |
| 1756 |
// A mask and compare is only perfect if the differing bits form a |
| 1757 |
// number like 00011111 with one single block of trailing 1s. |
| 1758 |
if ((differing_bits & (differing_bits + 1)) == 0 && |
| 1759 |
first_from + differing_bits == first_to) { |
| 1760 |
pos->determines_perfectly = true; |
| 1761 |
} |
| 1762 |
uint32_t common_bits = ~SmearBitsRight(differing_bits); |
| 1763 |
uint32_t bits = (first_from & common_bits); |
| 1764 |
for (int i = first_range + 1; i < ranges->length(); i++) { |
| 1765 |
range = ranges->at(i); |
| 1766 |
const base::uc32 from = range.from(); |
| 1767 |
if (from > char_mask) continue; |
| 1768 |
const base::uc32 to = |
| 1769 |
(range.to() > char_mask) ? char_mask : range.to(); |
| 1770 |
// Here we are combining more ranges into the mask and compare |
| 1771 |
// value. With each new range the mask becomes more sparse and |
| 1772 |
// so the chances of a false positive rise. A character class |
| 1773 |
// with multiple ranges is assumed never to be equivalent to a |
| 1774 |
// mask and compare operation. |
| 1775 |
pos->determines_perfectly = false; |
| 1776 |
uint32_t new_common_bits = (from ^ to); |
| 1777 |
new_common_bits = ~SmearBitsRight(new_common_bits); |
| 1778 |
common_bits &= new_common_bits; |
| 1779 |
bits &= new_common_bits; |
| 1780 |
uint32_t new_differing_bits = (from & common_bits) ^ bits; |
| 1781 |
common_bits ^= new_differing_bits; |
| 1782 |
bits &= common_bits; |
| 1783 |
} |
| 1784 |
pos->mask = common_bits; |
| 1785 |
pos->value = bits; |
| 1786 |
} |
| 1787 |
characters_filled_in++; |
| 1788 |
DCHECK(characters_filled_in <= details->characters()); |
| 1789 |
if (characters_filled_in == details->characters()) return; |
| 1790 |
} |
| 1791 |
} |
| 1792 |
DCHECK(characters_filled_in != details->characters()); |
| 1793 |
if (!details->cannot_match()) { |
| 1794 |
on_success()->GetQuickCheckDetails(details, compiler, characters_filled_in, |
| 1795 |
true); |
| 1796 |
} |
| 1797 |
} |
| 1798 |
|
| 1799 |
void QuickCheckDetails::Clear() { |
| 1800 |
for (int i = 0; i < characters_; i++) { |
| 1801 |
positions_[i].mask = 0; |
| 1802 |
positions_[i].value = 0; |
| 1803 |
positions_[i].determines_perfectly = false; |
| 1804 |
} |
| 1805 |
characters_ = 0; |
| 1806 |
} |
| 1807 |
|
| 1808 |
void QuickCheckDetails::Advance(int by, bool one_byte) { |
| 1809 |
if (by >= characters_ || by < 0) { |
| 1810 |
DCHECK_IMPLIES(by < 0, characters_ == 0); |
| 1811 |
Clear(); |
| 1812 |
return; |
| 1813 |
} |
| 1814 |
DCHECK_LE(characters_ - by, 4); |
| 1815 |
DCHECK_LE(characters_, 4); |
| 1816 |
for (int i = 0; i < characters_ - by; i++) { |
| 1817 |
positions_[i] = positions_[by + i]; |
| 1818 |
} |
| 1819 |
for (int i = characters_ - by; i < characters_; i++) { |
| 1820 |
positions_[i].mask = 0; |
| 1821 |
positions_[i].value = 0; |
| 1822 |
positions_[i].determines_perfectly = false; |
| 1823 |
} |
| 1824 |
characters_ -= by; |
| 1825 |
// We could change mask_ and value_ here but we would never advance unless |
| 1826 |
// they had already been used in a check and they won't be used again because |
| 1827 |
// it would gain us nothing. So there's no point. |
| 1828 |
} |
| 1829 |
|
| 1830 |
void QuickCheckDetails::Merge(QuickCheckDetails* other, int from_index) { |
| 1831 |
DCHECK(characters_ == other->characters_); |
| 1832 |
if (other->cannot_match_) { |
| 1833 |
return; |
| 1834 |
} |
| 1835 |
if (cannot_match_) { |
| 1836 |
*this = *other; |
| 1837 |
return; |
| 1838 |
} |
| 1839 |
for (int i = from_index; i < characters_; i++) { |
| 1840 |
QuickCheckDetails::Position* pos = positions(i); |
| 1841 |
QuickCheckDetails::Position* other_pos = other->positions(i); |
| 1842 |
if (pos->mask != other_pos->mask || pos->value != other_pos->value || |
| 1843 |
!other_pos->determines_perfectly) { |
| 1844 |
// Our mask-compare operation will be approximate unless we have the |
| 1845 |
// exact same operation on both sides of the alternation. |
| 1846 |
pos->determines_perfectly = false; |
| 1847 |
} |
| 1848 |
pos->mask &= other_pos->mask; |
| 1849 |
pos->value &= pos->mask; |
| 1850 |
other_pos->value &= pos->mask; |
| 1851 |
uint32_t differing_bits = (pos->value ^ other_pos->value); |
| 1852 |
pos->mask &= ~differing_bits; |
| 1853 |
pos->value &= pos->mask; |
| 1854 |
} |
| 1855 |
} |
| 1856 |
|
| 1857 |
class VisitMarker { |
| 1858 |
public: |
| 1859 |
explicit VisitMarker(NodeInfo* info) : info_(info) { |
| 1860 |
DCHECK(!info->visited); |
| 1861 |
info->visited = true; |
| 1862 |
} |
| 1863 |
~VisitMarker() { info_->visited = false; } |
| 1864 |
|
| 1865 |
private: |
| 1866 |
NodeInfo* info_; |
| 1867 |
}; |
| 1868 |
|
| 1869 |
// Temporarily sets traversed_loop_initialization_node_. |
| 1870 |
class LoopInitializationMarker { |
| 1871 |
public: |
| 1872 |
explicit LoopInitializationMarker(LoopChoiceNode* node) : node_(node) { |
| 1873 |
DCHECK(!node_->traversed_loop_initialization_node_); |
| 1874 |
node_->traversed_loop_initialization_node_ = true; |
| 1875 |
} |
| 1876 |
~LoopInitializationMarker() { |
| 1877 |
DCHECK(node_->traversed_loop_initialization_node_); |
| 1878 |
node_->traversed_loop_initialization_node_ = false; |
| 1879 |
} |
| 1880 |
LoopInitializationMarker(const LoopInitializationMarker&) = delete; |
| 1881 |
LoopInitializationMarker& operator=(const LoopInitializationMarker&) = delete; |
| 1882 |
|
| 1883 |
private: |
| 1884 |
LoopChoiceNode* node_; |
| 1885 |
}; |
| 1886 |
|
| 1887 |
// Temporarily decrements min_loop_iterations_. |
| 1888 |
class IterationDecrementer { |
| 1889 |
public: |
| 1890 |
explicit IterationDecrementer(LoopChoiceNode* node) : node_(node) { |
| 1891 |
DCHECK_GT(node_->min_loop_iterations_, 0); |
| 1892 |
--node_->min_loop_iterations_; |
| 1893 |
} |
| 1894 |
~IterationDecrementer() { ++node_->min_loop_iterations_; } |
| 1895 |
IterationDecrementer(const IterationDecrementer&) = delete; |
| 1896 |
IterationDecrementer& operator=(const IterationDecrementer&) = delete; |
| 1897 |
|
| 1898 |
private: |
| 1899 |
LoopChoiceNode* node_; |
| 1900 |
}; |
| 1901 |
|
| 1902 |
RegExpNode* SeqRegExpNode::FilterOneByte(int depth, RegExpCompiler* compiler) { |
| 1903 |
if (info()->replacement_calculated) return replacement(); |
| 1904 |
if (depth < 0) return this; |
| 1905 |
DCHECK(!info()->visited); |
| 1906 |
VisitMarker marker(info()); |
| 1907 |
return FilterSuccessor(depth - 1, compiler); |
| 1908 |
} |
| 1909 |
|
| 1910 |
RegExpNode* SeqRegExpNode::FilterSuccessor(int depth, |
| 1911 |
RegExpCompiler* compiler) { |
| 1912 |
RegExpNode* next = on_success_->FilterOneByte(depth - 1, compiler); |
| 1913 |
if (next == nullptr) return set_replacement(nullptr); |
| 1914 |
on_success_ = next; |
| 1915 |
return set_replacement(this); |
| 1916 |
} |
| 1917 |
|
| 1918 |
// We need to check for the following characters: 0x39C 0x3BC 0x178. |
| 1919 |
bool RangeContainsLatin1Equivalents(CharacterRange range) { |
| 1920 |
// TODO(dcarney): this could be a lot more efficient. |
| 1921 |
return range.Contains(0x039C) || range.Contains(0x03BC) || |
| 1922 |
range.Contains(0x0178); |
| 1923 |
} |
| 1924 |
|
| 1925 |
namespace { |
| 1926 |
|
| 1927 |
bool RangesContainLatin1Equivalents(ZoneList<CharacterRange>* ranges) { |
| 1928 |
for (int i = 0; i < ranges->length(); i++) { |
| 1929 |
// TODO(dcarney): this could be a lot more efficient. |
| 1930 |
if (RangeContainsLatin1Equivalents(ranges->at(i))) return true; |
| 1931 |
} |
| 1932 |
return false; |
| 1933 |
} |
| 1934 |
|
| 1935 |
} // namespace |
| 1936 |
|
| 1937 |
RegExpNode* TextNode::FilterOneByte(int depth, RegExpCompiler* compiler) { |
| 1938 |
RegExpFlags flags = compiler->flags(); |
| 1939 |
if (info()->replacement_calculated) return replacement(); |
| 1940 |
if (depth < 0) return this; |
| 1941 |
DCHECK(!info()->visited); |
| 1942 |
VisitMarker marker(info()); |
| 1943 |
int element_count = elements()->length(); |
| 1944 |
for (int i = 0; i < element_count; i++) { |
| 1945 |
TextElement elm = elements()->at(i); |
| 1946 |
if (elm.text_type() == TextElement::ATOM) { |
| 1947 |
base::Vector<const base::uc16> quarks = elm.atom()->data(); |
| 1948 |
for (int j = 0; j < quarks.length(); j++) { |
| 1949 |
base::uc16 c = quarks[j]; |
| 1950 |
if (!IsIgnoreCase(flags)) { |
| 1951 |
if (c > String::kMaxOneByteCharCode) return set_replacement(nullptr); |
| 1952 |
} else { |
| 1953 |
unibrow::uchar chars[4]; |
| 1954 |
int length = GetCaseIndependentLetters(compiler->isolate(), c, |
| 1955 |
compiler, chars, 4); |
| 1956 |
if (length == 0 || chars[0] > String::kMaxOneByteCharCode) { |
| 1957 |
return set_replacement(nullptr); |
| 1958 |
} |
| 1959 |
} |
| 1960 |
} |
| 1961 |
} else { |
| 1962 |
// A character class can also be impossible to match in one-byte mode. |
| 1963 |
DCHECK(elm.text_type() == TextElement::CLASS_RANGES); |
| 1964 |
RegExpClassRanges* cr = elm.class_ranges(); |
| 1965 |
ZoneList<CharacterRange>* ranges = cr->ranges(zone()); |
| 1966 |
CharacterRange::Canonicalize(ranges); |
| 1967 |
// Now they are in order so we only need to look at the first. |
| 1968 |
// If we are in non-Unicode case independent mode then we need |
| 1969 |
// to be a bit careful here, because the character classes have |
| 1970 |
// not been case-desugared yet, but there are characters and ranges |
| 1971 |
// that can become Latin-1 when case is considered. |
| 1972 |
int range_count = ranges->length(); |
| 1973 |
if (cr->is_negated()) { |
| 1974 |
if (range_count != 0 && ranges->at(0).from() == 0 && |
| 1975 |
ranges->at(0).to() >= String::kMaxOneByteCharCode) { |
| 1976 |
bool case_complications = !IsEitherUnicode(flags) && |
| 1977 |
IsIgnoreCase(flags) && |
| 1978 |
RangesContainLatin1Equivalents(ranges); |
| 1979 |
if (!case_complications) { |
| 1980 |
return set_replacement(nullptr); |
| 1981 |
} |
| 1982 |
} |
| 1983 |
} else { |
| 1984 |
if (range_count == 0 || |
| 1985 |
ranges->at(0).from() > String::kMaxOneByteCharCode) { |
| 1986 |
bool case_complications = !IsEitherUnicode(flags) && |
| 1987 |
IsIgnoreCase(flags) && |
| 1988 |
RangesContainLatin1Equivalents(ranges); |
| 1989 |
if (!case_complications) { |
| 1990 |
return set_replacement(nullptr); |
| 1991 |
} |
| 1992 |
} |
| 1993 |
} |
| 1994 |
} |
| 1995 |
} |
| 1996 |
return FilterSuccessor(depth - 1, compiler); |
| 1997 |
} |
| 1998 |
|
| 1999 |
RegExpNode* LoopChoiceNode::FilterOneByte(int depth, RegExpCompiler* compiler) { |
| 2000 |
if (info()->replacement_calculated) return replacement(); |
| 2001 |
if (depth < 0) return this; |
| 2002 |
if (info()->visited) return this; |
| 2003 |
{ |
| 2004 |
VisitMarker marker(info()); |
| 2005 |
|
| 2006 |
RegExpNode* continue_replacement = |
| 2007 |
continue_node_->FilterOneByte(depth - 1, compiler); |
| 2008 |
// If we can't continue after the loop then there is no sense in doing the |
| 2009 |
// loop. |
| 2010 |
if (continue_replacement == nullptr) return set_replacement(nullptr); |
| 2011 |
} |
| 2012 |
|
| 2013 |
return ChoiceNode::FilterOneByte(depth - 1, compiler); |
| 2014 |
} |
| 2015 |
|
| 2016 |
RegExpNode* ChoiceNode::FilterOneByte(int depth, RegExpCompiler* compiler) { |
| 2017 |
if (info()->replacement_calculated) return replacement(); |
| 2018 |
if (depth < 0) return this; |
| 2019 |
if (info()->visited) return this; |
| 2020 |
VisitMarker marker(info()); |
| 2021 |
int choice_count = alternatives_->length(); |
| 2022 |
|
| 2023 |
for (int i = 0; i < choice_count; i++) { |
| 2024 |
GuardedAlternative alternative = alternatives_->at(i); |
| 2025 |
if (alternative.guards() != nullptr && |
| 2026 |
alternative.guards()->length() != 0) { |
| 2027 |
set_replacement(this); |
| 2028 |
return this; |
| 2029 |
} |
| 2030 |
} |
| 2031 |
|
| 2032 |
int surviving = 0; |
| 2033 |
RegExpNode* survivor = nullptr; |
| 2034 |
for (int i = 0; i < choice_count; i++) { |
| 2035 |
GuardedAlternative alternative = alternatives_->at(i); |
| 2036 |
RegExpNode* replacement = |
| 2037 |
alternative.node()->FilterOneByte(depth - 1, compiler); |
| 2038 |
DCHECK(replacement != this); // No missing EMPTY_MATCH_CHECK. |
| 2039 |
if (replacement != nullptr) { |
| 2040 |
alternatives_->at(i).set_node(replacement); |
| 2041 |
surviving++; |
| 2042 |
survivor = replacement; |
| 2043 |
} |
| 2044 |
} |
| 2045 |
if (surviving < 2) return set_replacement(survivor); |
| 2046 |
|
| 2047 |
set_replacement(this); |
| 2048 |
if (surviving == choice_count) { |
| 2049 |
return this; |
| 2050 |
} |
| 2051 |
// Only some of the nodes survived the filtering. We need to rebuild the |
| 2052 |
// alternatives list. |
| 2053 |
ZoneList<GuardedAlternative>* new_alternatives = |
| 2054 |
zone()->New<ZoneList<GuardedAlternative>>(surviving, zone()); |
| 2055 |
for (int i = 0; i < choice_count; i++) { |
| 2056 |
RegExpNode* replacement = |
| 2057 |
alternatives_->at(i).node()->FilterOneByte(depth - 1, compiler); |
| 2058 |
if (replacement != nullptr) { |
| 2059 |
alternatives_->at(i).set_node(replacement); |
| 2060 |
new_alternatives->Add(alternatives_->at(i), zone()); |
| 2061 |
} |
| 2062 |
} |
| 2063 |
alternatives_ = new_alternatives; |
| 2064 |
return this; |
| 2065 |
} |
| 2066 |
|
| 2067 |
RegExpNode* NegativeLookaroundChoiceNode::FilterOneByte( |
| 2068 |
int depth, RegExpCompiler* compiler) { |
| 2069 |
if (info()->replacement_calculated) return replacement(); |
| 2070 |
if (depth < 0) return this; |
| 2071 |
if (info()->visited) return this; |
| 2072 |
VisitMarker marker(info()); |
| 2073 |
// Alternative 0 is the negative lookahead, alternative 1 is what comes |
| 2074 |
// afterwards. |
| 2075 |
RegExpNode* node = continue_node(); |
| 2076 |
RegExpNode* replacement = node->FilterOneByte(depth - 1, compiler); |
| 2077 |
if (replacement == nullptr) return set_replacement(nullptr); |
| 2078 |
alternatives_->at(kContinueIndex).set_node(replacement); |
| 2079 |
|
| 2080 |
RegExpNode* neg_node = lookaround_node(); |
| 2081 |
RegExpNode* neg_replacement = neg_node->FilterOneByte(depth - 1, compiler); |
| 2082 |
// If the negative lookahead is always going to fail then |
| 2083 |
// we don't need to check it. |
| 2084 |
if (neg_replacement == nullptr) return set_replacement(replacement); |
| 2085 |
alternatives_->at(kLookaroundIndex).set_node(neg_replacement); |
| 2086 |
return set_replacement(this); |
| 2087 |
} |
| 2088 |
|
| 2089 |
void LoopChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| 2090 |
RegExpCompiler* compiler, |
| 2091 |
int characters_filled_in, |
| 2092 |
bool not_at_start) { |
| 2093 |
if (body_can_be_zero_length_ || info()->visited) return; |
| 2094 |
not_at_start = not_at_start || this->not_at_start(); |
| 2095 |
DCHECK_EQ(alternatives_->length(), 2); // There's just loop and continue. |
| 2096 |
if (traversed_loop_initialization_node_ && min_loop_iterations_ > 0 && |
| 2097 |
loop_node_->EatsAtLeast(not_at_start) > |
| 2098 |
continue_node_->EatsAtLeast(true)) { |
| 2099 |
// Loop body is guaranteed to execute at least once, and consume characters |
| 2100 |
// when it does, meaning the only possible quick checks from this point |
| 2101 |
// begin with the loop body. We may recursively visit this LoopChoiceNode, |
| 2102 |
// but we temporarily decrease its minimum iteration counter so we know when |
| 2103 |
// to check the continue case. |
| 2104 |
IterationDecrementer next_iteration(this); |
| 2105 |
loop_node_->GetQuickCheckDetails(details, compiler, characters_filled_in, |
| 2106 |
not_at_start); |
| 2107 |
} else { |
| 2108 |
// Might not consume anything in the loop body, so treat it like a normal |
| 2109 |
// ChoiceNode (and don't recursively visit this node again). |
| 2110 |
VisitMarker marker(info()); |
| 2111 |
ChoiceNode::GetQuickCheckDetails(details, compiler, characters_filled_in, |
| 2112 |
not_at_start); |
| 2113 |
} |
| 2114 |
} |
| 2115 |
|
| 2116 |
void LoopChoiceNode::GetQuickCheckDetailsFromLoopEntry( |
| 2117 |
QuickCheckDetails* details, RegExpCompiler* compiler, |
| 2118 |
int characters_filled_in, bool not_at_start) { |
| 2119 |
if (traversed_loop_initialization_node_) { |
| 2120 |
// We already entered this loop once, exited via its continuation node, and |
| 2121 |
// followed an outer loop's back-edge to before the loop entry point. We |
| 2122 |
// could try to reset the minimum iteration count to its starting value at |
| 2123 |
// this point, but that seems like more trouble than it's worth. It's safe |
| 2124 |
// to keep going with the current (possibly reduced) minimum iteration |
| 2125 |
// count. |
| 2126 |
GetQuickCheckDetails(details, compiler, characters_filled_in, not_at_start); |
| 2127 |
} else { |
| 2128 |
// We are entering a loop via its counter initialization action, meaning we |
| 2129 |
// are guaranteed to run the loop body at least some minimum number of times |
| 2130 |
// before running the continuation node. Set a flag so that this node knows |
| 2131 |
// (now and any times we visit it again recursively) that it was entered |
| 2132 |
// from the top. |
| 2133 |
LoopInitializationMarker marker(this); |
| 2134 |
GetQuickCheckDetails(details, compiler, characters_filled_in, not_at_start); |
| 2135 |
} |
| 2136 |
} |
| 2137 |
|
| 2138 |
void LoopChoiceNode::FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 2139 |
BoyerMooreLookahead* bm, bool not_at_start) { |
| 2140 |
if (body_can_be_zero_length_ || budget <= 0) { |
| 2141 |
bm->SetRest(offset); |
| 2142 |
SaveBMInfo(bm, not_at_start, offset); |
| 2143 |
return; |
| 2144 |
} |
| 2145 |
ChoiceNode::FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); |
| 2146 |
SaveBMInfo(bm, not_at_start, offset); |
| 2147 |
} |
| 2148 |
|
| 2149 |
void ChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| 2150 |
RegExpCompiler* compiler, |
| 2151 |
int characters_filled_in, |
| 2152 |
bool not_at_start) { |
| 2153 |
not_at_start = (not_at_start || not_at_start_); |
| 2154 |
int choice_count = alternatives_->length(); |
| 2155 |
DCHECK_LT(0, choice_count); |
| 2156 |
alternatives_->at(0).node()->GetQuickCheckDetails( |
| 2157 |
details, compiler, characters_filled_in, not_at_start); |
| 2158 |
for (int i = 1; i < choice_count; i++) { |
| 2159 |
QuickCheckDetails new_details(details->characters()); |
| 2160 |
RegExpNode* node = alternatives_->at(i).node(); |
| 2161 |
node->GetQuickCheckDetails(&new_details, compiler, characters_filled_in, |
| 2162 |
not_at_start); |
| 2163 |
// Here we merge the quick match details of the two branches. |
| 2164 |
details->Merge(&new_details, characters_filled_in); |
| 2165 |
} |
| 2166 |
} |
| 2167 |
|
| 2168 |
namespace { |
| 2169 |
|
| 2170 |
// Check for [0-9A-Z_a-z]. |
| 2171 |
void EmitWordCheck(RegExpMacroAssembler* assembler, Label* word, |
| 2172 |
Label* non_word, bool fall_through_on_word) { |
| 2173 |
if (assembler->CheckSpecialClassRanges( |
| 2174 |
fall_through_on_word ? StandardCharacterSet::kWord |
| 2175 |
: StandardCharacterSet::kNotWord, |
| 2176 |
fall_through_on_word ? non_word : word)) { |
| 2177 |
// Optimized implementation available. |
| 2178 |
return; |
| 2179 |
} |
| 2180 |
assembler->CheckCharacterGT('z', non_word); |
| 2181 |
assembler->CheckCharacterLT('0', non_word); |
| 2182 |
assembler->CheckCharacterGT('a' - 1, word); |
| 2183 |
assembler->CheckCharacterLT('9' + 1, word); |
| 2184 |
assembler->CheckCharacterLT('A', non_word); |
| 2185 |
assembler->CheckCharacterLT('Z' + 1, word); |
| 2186 |
if (fall_through_on_word) { |
| 2187 |
assembler->CheckNotCharacter('_', non_word); |
| 2188 |
} else { |
| 2189 |
assembler->CheckCharacter('_', word); |
| 2190 |
} |
| 2191 |
} |
| 2192 |
|
| 2193 |
// Emit the code to check for a ^ in multiline mode (1-character lookbehind |
| 2194 |
// that matches newline or the start of input). |
| 2195 |
void EmitHat(RegExpCompiler* compiler, RegExpNode* on_success, Trace* trace) { |
| 2196 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 2197 |
|
| 2198 |
// We will load the previous character into the current character register. |
| 2199 |
Trace new_trace(*trace); |
| 2200 |
new_trace.InvalidateCurrentCharacter(); |
| 2201 |
|
| 2202 |
// A positive (> 0) cp_offset means we've already successfully matched a |
| 2203 |
// non-empty-width part of the pattern, and thus cannot be at or before the |
| 2204 |
// start of the subject string. We can thus skip both at-start and |
| 2205 |
// bounds-checks when loading the one-character lookbehind. |
| 2206 |
const bool may_be_at_or_before_subject_string_start = |
| 2207 |
new_trace.cp_offset() <= 0; |
| 2208 |
|
| 2209 |
Label ok; |
| 2210 |
if (may_be_at_or_before_subject_string_start) { |
| 2211 |
// The start of input counts as a newline in this context, so skip to ok if |
| 2212 |
// we are at the start. |
| 2213 |
assembler->CheckAtStart(new_trace.cp_offset(), &ok); |
| 2214 |
} |
| 2215 |
|
| 2216 |
// If we've already checked that we are not at the start of input, it's okay |
| 2217 |
// to load the previous character without bounds checks. |
| 2218 |
const bool can_skip_bounds_check = !may_be_at_or_before_subject_string_start; |
| 2219 |
assembler->LoadCurrentCharacter(new_trace.cp_offset() - 1, |
| 2220 |
new_trace.backtrack(), can_skip_bounds_check); |
| 2221 |
if (!assembler->CheckSpecialClassRanges(StandardCharacterSet::kLineTerminator, |
| 2222 |
new_trace.backtrack())) { |
| 2223 |
// Newline means \n, \r, 0x2028 or 0x2029. |
| 2224 |
if (!compiler->one_byte()) { |
| 2225 |
assembler->CheckCharacterAfterAnd(0x2028, 0xFFFE, &ok); |
| 2226 |
} |
| 2227 |
assembler->CheckCharacter('\n', &ok); |
| 2228 |
assembler->CheckNotCharacter('\r', new_trace.backtrack()); |
| 2229 |
} |
| 2230 |
assembler->Bind(&ok); |
| 2231 |
on_success->Emit(compiler, &new_trace); |
| 2232 |
} |
| 2233 |
|
| 2234 |
} // namespace |
| 2235 |
|
| 2236 |
// Emit the code to handle \b and \B (word-boundary or non-word-boundary). |
| 2237 |
void AssertionNode::EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace) { |
| 2238 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 2239 |
Isolate* isolate = assembler->isolate(); |
| 2240 |
Trace::TriBool next_is_word_character = Trace::UNKNOWN; |
| 2241 |
bool not_at_start = (trace->at_start() == Trace::FALSE_VALUE); |
| 2242 |
BoyerMooreLookahead* lookahead = bm_info(not_at_start); |
| 2243 |
if (lookahead == nullptr) { |
| 2244 |
int eats_at_least = |
| 2245 |
std::min(kMaxLookaheadForBoyerMoore, EatsAtLeast(not_at_start)); |
| 2246 |
if (eats_at_least >= 1) { |
| 2247 |
BoyerMooreLookahead* bm = |
| 2248 |
zone()->New<BoyerMooreLookahead>(eats_at_least, compiler, zone()); |
| 2249 |
FillInBMInfo(isolate, 0, kRecursionBudget, bm, not_at_start); |
| 2250 |
if (bm->at(0)->is_non_word()) next_is_word_character = Trace::FALSE_VALUE; |
| 2251 |
if (bm->at(0)->is_word()) next_is_word_character = Trace::TRUE_VALUE; |
| 2252 |
} |
| 2253 |
} else { |
| 2254 |
if (lookahead->at(0)->is_non_word()) |
| 2255 |
next_is_word_character = Trace::FALSE_VALUE; |
| 2256 |
if (lookahead->at(0)->is_word()) next_is_word_character = Trace::TRUE_VALUE; |
| 2257 |
} |
| 2258 |
bool at_boundary = (assertion_type_ == AssertionNode::AT_BOUNDARY); |
| 2259 |
if (next_is_word_character == Trace::UNKNOWN) { |
| 2260 |
Label before_non_word; |
| 2261 |
Label before_word; |
| 2262 |
if (trace->characters_preloaded() != 1) { |
| 2263 |
assembler->LoadCurrentCharacter(trace->cp_offset(), &before_non_word); |
| 2264 |
} |
| 2265 |
// Fall through on non-word. |
| 2266 |
EmitWordCheck(assembler, &before_word, &before_non_word, false); |
| 2267 |
// Next character is not a word character. |
| 2268 |
assembler->Bind(&before_non_word); |
| 2269 |
Label ok; |
| 2270 |
BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord); |
| 2271 |
assembler->GoTo(&ok); |
| 2272 |
|
| 2273 |
assembler->Bind(&before_word); |
| 2274 |
BacktrackIfPrevious(compiler, trace, at_boundary ? kIsWord : kIsNonWord); |
| 2275 |
assembler->Bind(&ok); |
| 2276 |
} else if (next_is_word_character == Trace::TRUE_VALUE) { |
| 2277 |
BacktrackIfPrevious(compiler, trace, at_boundary ? kIsWord : kIsNonWord); |
| 2278 |
} else { |
| 2279 |
DCHECK(next_is_word_character == Trace::FALSE_VALUE); |
| 2280 |
BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord); |
| 2281 |
} |
| 2282 |
} |
| 2283 |
|
| 2284 |
void AssertionNode::BacktrackIfPrevious( |
| 2285 |
RegExpCompiler* compiler, Trace* trace, |
| 2286 |
AssertionNode::IfPrevious backtrack_if_previous) { |
| 2287 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 2288 |
Trace new_trace(*trace); |
| 2289 |
new_trace.InvalidateCurrentCharacter(); |
| 2290 |
|
| 2291 |
Label fall_through; |
| 2292 |
Label* non_word = backtrack_if_previous == kIsNonWord ? new_trace.backtrack() |
| 2293 |
: &fall_through; |
| 2294 |
Label* word = backtrack_if_previous == kIsNonWord ? &fall_through |
| 2295 |
: new_trace.backtrack(); |
| 2296 |
|
| 2297 |
// A positive (> 0) cp_offset means we've already successfully matched a |
| 2298 |
// non-empty-width part of the pattern, and thus cannot be at or before the |
| 2299 |
// start of the subject string. We can thus skip both at-start and |
| 2300 |
// bounds-checks when loading the one-character lookbehind. |
| 2301 |
const bool may_be_at_or_before_subject_string_start = |
| 2302 |
new_trace.cp_offset() <= 0; |
| 2303 |
|
| 2304 |
if (may_be_at_or_before_subject_string_start) { |
| 2305 |
// The start of input counts as a non-word character, so the question is |
| 2306 |
// decided if we are at the start. |
| 2307 |
assembler->CheckAtStart(new_trace.cp_offset(), non_word); |
| 2308 |
} |
| 2309 |
|
| 2310 |
// If we've already checked that we are not at the start of input, it's okay |
| 2311 |
// to load the previous character without bounds checks. |
| 2312 |
const bool can_skip_bounds_check = !may_be_at_or_before_subject_string_start; |
| 2313 |
assembler->LoadCurrentCharacter(new_trace.cp_offset() - 1, non_word, |
| 2314 |
can_skip_bounds_check); |
| 2315 |
EmitWordCheck(assembler, word, non_word, backtrack_if_previous == kIsNonWord); |
| 2316 |
|
| 2317 |
assembler->Bind(&fall_through); |
| 2318 |
on_success()->Emit(compiler, &new_trace); |
| 2319 |
} |
| 2320 |
|
| 2321 |
void AssertionNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| 2322 |
RegExpCompiler* compiler, |
| 2323 |
int filled_in, bool not_at_start) { |
| 2324 |
if (assertion_type_ == AT_START && not_at_start) { |
| 2325 |
details->set_cannot_match(); |
| 2326 |
return; |
| 2327 |
} |
| 2328 |
return on_success()->GetQuickCheckDetails(details, compiler, filled_in, |
| 2329 |
not_at_start); |
| 2330 |
} |
| 2331 |
|
| 2332 |
void AssertionNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 2333 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 2334 |
switch (assertion_type_) { |
| 2335 |
case AT_END: { |
| 2336 |
Label ok; |
| 2337 |
assembler->CheckPosition(trace->cp_offset(), &ok); |
| 2338 |
assembler->GoTo(trace->backtrack()); |
| 2339 |
assembler->Bind(&ok); |
| 2340 |
break; |
| 2341 |
} |
| 2342 |
case AT_START: { |
| 2343 |
if (trace->at_start() == Trace::FALSE_VALUE) { |
| 2344 |
assembler->GoTo(trace->backtrack()); |
| 2345 |
return; |
| 2346 |
} |
| 2347 |
if (trace->at_start() == Trace::UNKNOWN) { |
| 2348 |
assembler->CheckNotAtStart(trace->cp_offset(), trace->backtrack()); |
| 2349 |
Trace at_start_trace = *trace; |
| 2350 |
at_start_trace.set_at_start(Trace::TRUE_VALUE); |
| 2351 |
on_success()->Emit(compiler, &at_start_trace); |
| 2352 |
return; |
| 2353 |
} |
| 2354 |
} break; |
| 2355 |
case AFTER_NEWLINE: |
| 2356 |
EmitHat(compiler, on_success(), trace); |
| 2357 |
return; |
| 2358 |
case AT_BOUNDARY: |
| 2359 |
case AT_NON_BOUNDARY: { |
| 2360 |
EmitBoundaryCheck(compiler, trace); |
| 2361 |
return; |
| 2362 |
} |
| 2363 |
} |
| 2364 |
on_success()->Emit(compiler, trace); |
| 2365 |
} |
| 2366 |
|
| 2367 |
namespace { |
| 2368 |
|
| 2369 |
bool DeterminedAlready(QuickCheckDetails* quick_check, int offset) { |
| 2370 |
if (quick_check == nullptr) return false; |
| 2371 |
if (offset >= quick_check->characters()) return false; |
| 2372 |
return quick_check->positions(offset)->determines_perfectly; |
| 2373 |
} |
| 2374 |
|
| 2375 |
void UpdateBoundsCheck(int index, int* checked_up_to) { |
| 2376 |
if (index > *checked_up_to) { |
| 2377 |
*checked_up_to = index; |
| 2378 |
} |
| 2379 |
} |
| 2380 |
|
| 2381 |
} // namespace |
| 2382 |
|
| 2383 |
// We call this repeatedly to generate code for each pass over the text node. |
| 2384 |
// The passes are in increasing order of difficulty because we hope one |
| 2385 |
// of the first passes will fail in which case we are saved the work of the |
| 2386 |
// later passes. for example for the case independent regexp /%[asdfghjkl]a/ |
| 2387 |
// we will check the '%' in the first pass, the case independent 'a' in the |
| 2388 |
// second pass and the character class in the last pass. |
| 2389 |
// |
| 2390 |
// The passes are done from right to left, so for example to test for /bar/ |
| 2391 |
// we will first test for an 'r' with offset 2, then an 'a' with offset 1 |
| 2392 |
// and then a 'b' with offset 0. This means we can avoid the end-of-input |
| 2393 |
// bounds check most of the time. In the example we only need to check for |
| 2394 |
// end-of-input when loading the putative 'r'. |
| 2395 |
// |
| 2396 |
// A slight complication involves the fact that the first character may already |
| 2397 |
// be fetched into a register by the previous node. In this case we want to |
| 2398 |
// do the test for that character first. We do this in separate passes. The |
| 2399 |
// 'preloaded' argument indicates that we are doing such a 'pass'. If such a |
| 2400 |
// pass has been performed then subsequent passes will have true in |
| 2401 |
// first_element_checked to indicate that that character does not need to be |
| 2402 |
// checked again. |
| 2403 |
// |
| 2404 |
// In addition to all this we are passed a Trace, which can |
| 2405 |
// contain an AlternativeGeneration object. In this AlternativeGeneration |
| 2406 |
// object we can see details of any quick check that was already passed in |
| 2407 |
// order to get to the code we are now generating. The quick check can involve |
| 2408 |
// loading characters, which means we do not need to recheck the bounds |
| 2409 |
// up to the limit the quick check already checked. In addition the quick |
| 2410 |
// check can have involved a mask and compare operation which may simplify |
| 2411 |
// or obviate the need for further checks at some character positions. |
| 2412 |
void TextNode::TextEmitPass(RegExpCompiler* compiler, TextEmitPassType pass, |
| 2413 |
bool preloaded, Trace* trace, |
| 2414 |
bool first_element_checked, int* checked_up_to) { |
| 2415 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 2416 |
Isolate* isolate = assembler->isolate(); |
| 2417 |
bool one_byte = compiler->one_byte(); |
| 2418 |
Label* backtrack = trace->backtrack(); |
| 2419 |
QuickCheckDetails* quick_check = trace->quick_check_performed(); |
| 2420 |
int element_count = elements()->length(); |
| 2421 |
int backward_offset = read_backward() ? -Length() : 0; |
| 2422 |
for (int i = preloaded ? 0 : element_count - 1; i >= 0; i--) { |
| 2423 |
TextElement elm = elements()->at(i); |
| 2424 |
int cp_offset = trace->cp_offset() + elm.cp_offset() + backward_offset; |
| 2425 |
if (elm.text_type() == TextElement::ATOM) { |
| 2426 |
base::Vector<const base::uc16> quarks = elm.atom()->data(); |
| 2427 |
for (int j = preloaded ? 0 : quarks.length() - 1; j >= 0; j--) { |
| 2428 |
if (first_element_checked && i == 0 && j == 0) continue; |
| 2429 |
if (DeterminedAlready(quick_check, elm.cp_offset() + j)) continue; |
| 2430 |
base::uc16 quark = quarks[j]; |
| 2431 |
bool needs_bounds_check = |
| 2432 |
*checked_up_to < cp_offset + j || read_backward(); |
| 2433 |
bool bounds_checked = false; |
| 2434 |
switch (pass) { |
| 2435 |
case NON_LATIN1_MATCH: { |
| 2436 |
DCHECK(one_byte); // This pass is only done in one-byte mode. |
| 2437 |
if (IsIgnoreCase(compiler->flags())) { |
| 2438 |
// We are compiling for a one-byte subject, case independent mode. |
| 2439 |
// We have to check whether any of the case alternatives are in |
| 2440 |
// the one-byte range. |
| 2441 |
unibrow::uchar chars[4]; |
| 2442 |
// Only returns characters that are in the one-byte range. |
| 2443 |
int length = |
| 2444 |
GetCaseIndependentLetters(isolate, quark, compiler, chars, 4); |
| 2445 |
if (length == 0) { |
| 2446 |
assembler->GoTo(backtrack); |
| 2447 |
return; |
| 2448 |
} |
| 2449 |
} else { |
| 2450 |
// Case-dependent mode. |
| 2451 |
if (quark > String::kMaxOneByteCharCode) { |
| 2452 |
assembler->GoTo(backtrack); |
| 2453 |
return; |
| 2454 |
} |
| 2455 |
} |
| 2456 |
break; |
| 2457 |
} |
| 2458 |
case NON_LETTER_CHARACTER_MATCH: |
| 2459 |
bounds_checked = |
| 2460 |
EmitAtomNonLetter(isolate, compiler, quark, backtrack, |
| 2461 |
cp_offset + j, needs_bounds_check, preloaded); |
| 2462 |
break; |
| 2463 |
case SIMPLE_CHARACTER_MATCH: |
| 2464 |
bounds_checked = EmitSimpleCharacter(isolate, compiler, quark, |
| 2465 |
backtrack, cp_offset + j, |
| 2466 |
needs_bounds_check, preloaded); |
| 2467 |
break; |
| 2468 |
case CASE_CHARACTER_MATCH: |
| 2469 |
bounds_checked = |
| 2470 |
EmitAtomLetter(isolate, compiler, quark, backtrack, |
| 2471 |
cp_offset + j, needs_bounds_check, preloaded); |
| 2472 |
break; |
| 2473 |
default: |
| 2474 |
break; |
| 2475 |
} |
| 2476 |
if (bounds_checked) UpdateBoundsCheck(cp_offset + j, checked_up_to); |
| 2477 |
} |
| 2478 |
} else { |
| 2479 |
DCHECK_EQ(TextElement::CLASS_RANGES, elm.text_type()); |
| 2480 |
if (pass == CHARACTER_CLASS_MATCH) { |
| 2481 |
if (first_element_checked && i == 0) continue; |
| 2482 |
if (DeterminedAlready(quick_check, elm.cp_offset())) continue; |
| 2483 |
RegExpClassRanges* cr = elm.class_ranges(); |
| 2484 |
bool bounds_check = *checked_up_to < cp_offset || read_backward(); |
| 2485 |
EmitClassRanges(assembler, cr, one_byte, backtrack, cp_offset, |
| 2486 |
bounds_check, preloaded, zone()); |
| 2487 |
UpdateBoundsCheck(cp_offset, checked_up_to); |
| 2488 |
} |
| 2489 |
} |
| 2490 |
} |
| 2491 |
} |
| 2492 |
|
| 2493 |
int TextNode::Length() { |
| 2494 |
TextElement elm = elements()->last(); |
| 2495 |
DCHECK_LE(0, elm.cp_offset()); |
| 2496 |
return elm.cp_offset() + elm.length(); |
| 2497 |
} |
| 2498 |
|
| 2499 |
TextNode* TextNode::CreateForCharacterRanges(Zone* zone, |
| 2500 |
ZoneList<CharacterRange>* ranges, |
| 2501 |
bool read_backward, |
| 2502 |
RegExpNode* on_success) { |
| 2503 |
DCHECK_NOT_NULL(ranges); |
| 2504 |
// TODO(jgruber): There's no fundamental need to create this |
| 2505 |
// RegExpClassRanges; we could refactor to avoid the allocation. |
| 2506 |
return zone->New<TextNode>(zone->New<RegExpClassRanges>(zone, ranges), |
| 2507 |
read_backward, on_success); |
| 2508 |
} |
| 2509 |
|
| 2510 |
TextNode* TextNode::CreateForSurrogatePair( |
| 2511 |
Zone* zone, CharacterRange lead, ZoneList<CharacterRange>* trail_ranges, |
| 2512 |
bool read_backward, RegExpNode* on_success) { |
| 2513 |
ZoneList<TextElement>* elms = zone->New<ZoneList<TextElement>>(2, zone); |
| 2514 |
if (lead.from() == lead.to()) { |
| 2515 |
ZoneList<base::uc16> lead_surrogate(1, zone); |
| 2516 |
lead_surrogate.Add(lead.from(), zone); |
| 2517 |
RegExpAtom* atom = zone->New<RegExpAtom>(lead_surrogate.ToConstVector()); |
| 2518 |
elms->Add(TextElement::Atom(atom), zone); |
| 2519 |
} else { |
| 2520 |
ZoneList<CharacterRange>* lead_ranges = CharacterRange::List(zone, lead); |
| 2521 |
elms->Add(TextElement::ClassRanges( |
| 2522 |
zone->New<RegExpClassRanges>(zone, lead_ranges)), |
| 2523 |
zone); |
| 2524 |
} |
| 2525 |
elms->Add(TextElement::ClassRanges( |
| 2526 |
zone->New<RegExpClassRanges>(zone, trail_ranges)), |
| 2527 |
zone); |
| 2528 |
return zone->New<TextNode>(elms, read_backward, on_success); |
| 2529 |
} |
| 2530 |
|
| 2531 |
TextNode* TextNode::CreateForSurrogatePair( |
| 2532 |
Zone* zone, ZoneList<CharacterRange>* lead_ranges, CharacterRange trail, |
| 2533 |
bool read_backward, RegExpNode* on_success) { |
| 2534 |
ZoneList<CharacterRange>* trail_ranges = CharacterRange::List(zone, trail); |
| 2535 |
ZoneList<TextElement>* elms = zone->New<ZoneList<TextElement>>(2, zone); |
| 2536 |
elms->Add( |
| 2537 |
TextElement::ClassRanges(zone->New<RegExpClassRanges>(zone, lead_ranges)), |
| 2538 |
zone); |
| 2539 |
elms->Add(TextElement::ClassRanges( |
| 2540 |
zone->New<RegExpClassRanges>(zone, trail_ranges)), |
| 2541 |
zone); |
| 2542 |
return zone->New<TextNode>(elms, read_backward, on_success); |
| 2543 |
} |
| 2544 |
|
| 2545 |
// This generates the code to match a text node. A text node can contain |
| 2546 |
// straight character sequences (possibly to be matched in a case-independent |
| 2547 |
// way) and character classes. For efficiency we do not do this in a single |
| 2548 |
// pass from left to right. Instead we pass over the text node several times, |
| 2549 |
// emitting code for some character positions every time. See the comment on |
| 2550 |
// TextEmitPass for details. |
| 2551 |
void TextNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 2552 |
LimitResult limit_result = LimitVersions(compiler, trace); |
| 2553 |
if (limit_result == DONE) return; |
| 2554 |
DCHECK(limit_result == CONTINUE); |
| 2555 |
|
| 2556 |
if (trace->cp_offset() + Length() > RegExpMacroAssembler::kMaxCPOffset) { |
| 2557 |
compiler->SetRegExpTooBig(); |
| 2558 |
return; |
| 2559 |
} |
| 2560 |
|
| 2561 |
if (compiler->one_byte()) { |
| 2562 |
int dummy = 0; |
| 2563 |
TextEmitPass(compiler, NON_LATIN1_MATCH, false, trace, false, &dummy); |
| 2564 |
} |
| 2565 |
|
| 2566 |
bool first_elt_done = false; |
| 2567 |
int bound_checked_to = trace->cp_offset() - 1; |
| 2568 |
bound_checked_to += trace->bound_checked_up_to(); |
| 2569 |
|
| 2570 |
// If a character is preloaded into the current character register then |
| 2571 |
// check that first to save reloading it. |
| 2572 |
for (int twice = 0; twice < 2; twice++) { |
| 2573 |
bool is_preloaded_pass = twice == 0; |
| 2574 |
if (is_preloaded_pass && trace->characters_preloaded() != 1) continue; |
| 2575 |
if (IsIgnoreCase(compiler->flags())) { |
| 2576 |
TextEmitPass(compiler, NON_LETTER_CHARACTER_MATCH, is_preloaded_pass, |
| 2577 |
trace, first_elt_done, &bound_checked_to); |
| 2578 |
TextEmitPass(compiler, CASE_CHARACTER_MATCH, is_preloaded_pass, trace, |
| 2579 |
first_elt_done, &bound_checked_to); |
| 2580 |
} else { |
| 2581 |
TextEmitPass(compiler, SIMPLE_CHARACTER_MATCH, is_preloaded_pass, trace, |
| 2582 |
first_elt_done, &bound_checked_to); |
| 2583 |
} |
| 2584 |
TextEmitPass(compiler, CHARACTER_CLASS_MATCH, is_preloaded_pass, trace, |
| 2585 |
first_elt_done, &bound_checked_to); |
| 2586 |
first_elt_done = true; |
| 2587 |
} |
| 2588 |
|
| 2589 |
Trace successor_trace(*trace); |
| 2590 |
// If we advance backward, we may end up at the start. |
| 2591 |
successor_trace.AdvanceCurrentPositionInTrace( |
| 2592 |
read_backward() ? -Length() : Length(), compiler); |
| 2593 |
successor_trace.set_at_start(read_backward() ? Trace::UNKNOWN |
| 2594 |
: Trace::FALSE_VALUE); |
| 2595 |
RecursionCheck rc(compiler); |
| 2596 |
on_success()->Emit(compiler, &successor_trace); |
| 2597 |
} |
| 2598 |
|
| 2599 |
void Trace::InvalidateCurrentCharacter() { characters_preloaded_ = 0; } |
| 2600 |
|
| 2601 |
void Trace::AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler) { |
| 2602 |
// We don't have an instruction for shifting the current character register |
| 2603 |
// down or for using a shifted value for anything so lets just forget that |
| 2604 |
// we preloaded any characters into it. |
| 2605 |
characters_preloaded_ = 0; |
| 2606 |
// Adjust the offsets of the quick check performed information. This |
| 2607 |
// information is used to find out what we already determined about the |
| 2608 |
// characters by means of mask and compare. |
| 2609 |
quick_check_performed_.Advance(by, compiler->one_byte()); |
| 2610 |
cp_offset_ += by; |
| 2611 |
if (cp_offset_ > RegExpMacroAssembler::kMaxCPOffset) { |
| 2612 |
compiler->SetRegExpTooBig(); |
| 2613 |
cp_offset_ = 0; |
| 2614 |
} |
| 2615 |
bound_checked_up_to_ = std::max(0, bound_checked_up_to_ - by); |
| 2616 |
} |
| 2617 |
|
| 2618 |
void TextNode::MakeCaseIndependent(Isolate* isolate, bool is_one_byte, |
| 2619 |
RegExpFlags flags) { |
| 2620 |
if (!IsIgnoreCase(flags)) return; |
| 2621 |
#ifdef V8_INTL_SUPPORT |
| 2622 |
// This is done in an earlier step when generating the nodes from the AST |
| 2623 |
// because we may have to split up into separate nodes. |
| 2624 |
if (NeedsUnicodeCaseEquivalents(flags)) return; |
| 2625 |
#endif |
| 2626 |
|
| 2627 |
int element_count = elements()->length(); |
| 2628 |
for (int i = 0; i < element_count; i++) { |
| 2629 |
TextElement elm = elements()->at(i); |
| 2630 |
if (elm.text_type() == TextElement::CLASS_RANGES) { |
| 2631 |
RegExpClassRanges* cr = elm.class_ranges(); |
| 2632 |
// None of the standard character classes is different in the case |
| 2633 |
// independent case and it slows us down if we don't know that. |
| 2634 |
if (cr->is_standard(zone())) continue; |
| 2635 |
ZoneList<CharacterRange>* ranges = cr->ranges(zone()); |
| 2636 |
CharacterRange::AddCaseEquivalents(isolate, zone(), ranges, is_one_byte); |
| 2637 |
} |
| 2638 |
} |
| 2639 |
} |
| 2640 |
|
| 2641 |
int TextNode::GreedyLoopTextLength() { return Length(); } |
| 2642 |
|
| 2643 |
RegExpNode* TextNode::GetSuccessorOfOmnivorousTextNode( |
| 2644 |
RegExpCompiler* compiler) { |
| 2645 |
if (read_backward()) return nullptr; |
| 2646 |
if (elements()->length() != 1) return nullptr; |
| 2647 |
TextElement elm = elements()->at(0); |
| 2648 |
if (elm.text_type() != TextElement::CLASS_RANGES) return nullptr; |
| 2649 |
RegExpClassRanges* node = elm.class_ranges(); |
| 2650 |
ZoneList<CharacterRange>* ranges = node->ranges(zone()); |
| 2651 |
CharacterRange::Canonicalize(ranges); |
| 2652 |
if (node->is_negated()) { |
| 2653 |
return ranges->length() == 0 ? on_success() : nullptr; |
| 2654 |
} |
| 2655 |
if (ranges->length() != 1) return nullptr; |
| 2656 |
const base::uc32 max_char = MaxCodeUnit(compiler->one_byte()); |
| 2657 |
return ranges->at(0).IsEverything(max_char) ? on_success() : nullptr; |
| 2658 |
} |
| 2659 |
|
| 2660 |
// Finds the fixed match length of a sequence of nodes that goes from |
| 2661 |
// this alternative and back to this choice node. If there are variable |
| 2662 |
// length nodes or other complications in the way then return a sentinel |
| 2663 |
// value indicating that a greedy loop cannot be constructed. |
| 2664 |
int ChoiceNode::GreedyLoopTextLengthForAlternative( |
| 2665 |
GuardedAlternative* alternative) { |
| 2666 |
int length = 0; |
| 2667 |
RegExpNode* node = alternative->node(); |
| 2668 |
// Later we will generate code for all these text nodes using recursion |
| 2669 |
// so we have to limit the max number. |
| 2670 |
int recursion_depth = 0; |
| 2671 |
while (node != this) { |
| 2672 |
if (recursion_depth++ > RegExpCompiler::kMaxRecursion) { |
| 2673 |
return kNodeIsTooComplexForGreedyLoops; |
| 2674 |
} |
| 2675 |
int node_length = node->GreedyLoopTextLength(); |
| 2676 |
if (node_length == kNodeIsTooComplexForGreedyLoops) { |
| 2677 |
return kNodeIsTooComplexForGreedyLoops; |
| 2678 |
} |
| 2679 |
length += node_length; |
| 2680 |
node = node->AsSeqRegExpNode()->on_success(); |
| 2681 |
} |
| 2682 |
if (read_backward()) { |
| 2683 |
length = -length; |
| 2684 |
} |
| 2685 |
// Check that we can jump by the whole text length. If not, return sentinel |
| 2686 |
// to indicate the we can't construct a greedy loop. |
| 2687 |
if (length < RegExpMacroAssembler::kMinCPOffset || |
| 2688 |
length > RegExpMacroAssembler::kMaxCPOffset) { |
| 2689 |
return kNodeIsTooComplexForGreedyLoops; |
| 2690 |
} |
| 2691 |
return length; |
| 2692 |
} |
| 2693 |
|
| 2694 |
void LoopChoiceNode::AddLoopAlternative(GuardedAlternative alt) { |
| 2695 |
DCHECK_NULL(loop_node_); |
| 2696 |
AddAlternative(alt); |
| 2697 |
loop_node_ = alt.node(); |
| 2698 |
} |
| 2699 |
|
| 2700 |
void LoopChoiceNode::AddContinueAlternative(GuardedAlternative alt) { |
| 2701 |
DCHECK_NULL(continue_node_); |
| 2702 |
AddAlternative(alt); |
| 2703 |
continue_node_ = alt.node(); |
| 2704 |
} |
| 2705 |
|
| 2706 |
void LoopChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 2707 |
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 2708 |
if (trace->stop_node() == this) { |
| 2709 |
// Back edge of greedy optimized loop node graph. |
| 2710 |
int text_length = |
| 2711 |
GreedyLoopTextLengthForAlternative(&(alternatives_->at(0))); |
| 2712 |
DCHECK_NE(kNodeIsTooComplexForGreedyLoops, text_length); |
| 2713 |
// Update the counter-based backtracking info on the stack. This is an |
| 2714 |
// optimization for greedy loops (see below). |
| 2715 |
DCHECK(trace->cp_offset() == text_length); |
| 2716 |
macro_assembler->AdvanceCurrentPosition(text_length); |
| 2717 |
macro_assembler->GoTo(trace->loop_label()); |
| 2718 |
return; |
| 2719 |
} |
| 2720 |
DCHECK_NULL(trace->stop_node()); |
| 2721 |
if (!trace->is_trivial()) { |
| 2722 |
trace->Flush(compiler, this); |
| 2723 |
return; |
| 2724 |
} |
| 2725 |
ChoiceNode::Emit(compiler, trace); |
| 2726 |
} |
| 2727 |
|
| 2728 |
int ChoiceNode::CalculatePreloadCharacters(RegExpCompiler* compiler, |
| 2729 |
int eats_at_least) { |
| 2730 |
int preload_characters = std::min(4, eats_at_least); |
| 2731 |
DCHECK_LE(preload_characters, 4); |
| 2732 |
if (compiler->macro_assembler()->CanReadUnaligned()) { |
| 2733 |
bool one_byte = compiler->one_byte(); |
| 2734 |
if (one_byte) { |
| 2735 |
// We can't preload 3 characters because there is no machine instruction |
| 2736 |
// to do that. We can't just load 4 because we could be reading |
| 2737 |
// beyond the end of the string, which could cause a memory fault. |
| 2738 |
if (preload_characters == 3) preload_characters = 2; |
| 2739 |
} else { |
| 2740 |
if (preload_characters > 2) preload_characters = 2; |
| 2741 |
} |
| 2742 |
} else { |
| 2743 |
if (preload_characters > 1) preload_characters = 1; |
| 2744 |
} |
| 2745 |
return preload_characters; |
| 2746 |
} |
| 2747 |
|
| 2748 |
// This class is used when generating the alternatives in a choice node. It |
| 2749 |
// records the way the alternative is being code generated. |
| 2750 |
class AlternativeGeneration : public Malloced { |
| 2751 |
public: |
| 2752 |
AlternativeGeneration() |
| 2753 |
: possible_success(), |
| 2754 |
expects_preload(false), |
| 2755 |
after(), |
| 2756 |
quick_check_details() {} |
| 2757 |
Label possible_success; |
| 2758 |
bool expects_preload; |
| 2759 |
Label after; |
| 2760 |
QuickCheckDetails quick_check_details; |
| 2761 |
}; |
| 2762 |
|
| 2763 |
// Creates a list of AlternativeGenerations. If the list has a reasonable |
| 2764 |
// size then it is on the stack, otherwise the excess is on the heap. |
| 2765 |
class AlternativeGenerationList { |
| 2766 |
public: |
| 2767 |
AlternativeGenerationList(int count, Zone* zone) : alt_gens_(count, zone) { |
| 2768 |
for (int i = 0; i < count && i < kAFew; i++) { |
| 2769 |
alt_gens_.Add(a_few_alt_gens_ + i, zone); |
| 2770 |
} |
| 2771 |
for (int i = kAFew; i < count; i++) { |
| 2772 |
alt_gens_.Add(new AlternativeGeneration(), zone); |
| 2773 |
} |
| 2774 |
} |
| 2775 |
~AlternativeGenerationList() { |
| 2776 |
for (int i = kAFew; i < alt_gens_.length(); i++) { |
| 2777 |
delete alt_gens_[i]; |
| 2778 |
alt_gens_[i] = nullptr; |
| 2779 |
} |
| 2780 |
} |
| 2781 |
|
| 2782 |
AlternativeGeneration* at(int i) { return alt_gens_[i]; } |
| 2783 |
|
| 2784 |
private: |
| 2785 |
static const int kAFew = 10; |
| 2786 |
ZoneList<AlternativeGeneration*> alt_gens_; |
| 2787 |
AlternativeGeneration a_few_alt_gens_[kAFew]; |
| 2788 |
}; |
| 2789 |
|
| 2790 |
void BoyerMoorePositionInfo::Set(int character) { |
| 2791 |
SetInterval(Interval(character, character)); |
| 2792 |
} |
| 2793 |
|
| 2794 |
namespace { |
| 2795 |
|
| 2796 |
ContainedInLattice AddRange(ContainedInLattice containment, const int* ranges, |
| 2797 |
int ranges_length, Interval new_range) { |
| 2798 |
DCHECK_EQ(1, ranges_length & 1); |
| 2799 |
DCHECK_EQ(String::kMaxCodePoint + 1, ranges[ranges_length - 1]); |
| 2800 |
if (containment == kLatticeUnknown) return containment; |
| 2801 |
bool inside = false; |
| 2802 |
int last = 0; |
| 2803 |
for (int i = 0; i < ranges_length; inside = !inside, last = ranges[i], i++) { |
| 2804 |
// Consider the range from last to ranges[i]. |
| 2805 |
// We haven't got to the new range yet. |
| 2806 |
if (ranges[i] <= new_range.from()) continue; |
| 2807 |
// New range is wholly inside last-ranges[i]. Note that new_range.to() is |
| 2808 |
// inclusive, but the values in ranges are not. |
| 2809 |
if (last <= new_range.from() && new_range.to() < ranges[i]) { |
| 2810 |
return Combine(containment, inside ? kLatticeIn : kLatticeOut); |
| 2811 |
} |
| 2812 |
return kLatticeUnknown; |
| 2813 |
} |
| 2814 |
return containment; |
| 2815 |
} |
| 2816 |
|
| 2817 |
int BitsetFirstSetBit(BoyerMoorePositionInfo::Bitset bitset) { |
| 2818 |
static_assert(BoyerMoorePositionInfo::kMapSize == |
| 2819 |
2 * kInt64Size * kBitsPerByte); |
| 2820 |
|
| 2821 |
// Slight fiddling is needed here, since the bitset is of length 128 while |
| 2822 |
// CountTrailingZeros requires an integral type and std::bitset can only |
| 2823 |
// convert to unsigned long long. So we handle the most- and least-significant |
| 2824 |
// bits separately. |
| 2825 |
|
| 2826 |
{ |
| 2827 |
static constexpr BoyerMoorePositionInfo::Bitset mask(~uint64_t{0}); |
| 2828 |
BoyerMoorePositionInfo::Bitset masked_bitset = bitset & mask; |
| 2829 |
static_assert(kInt64Size >= sizeof(decltype(masked_bitset.to_ullong()))); |
| 2830 |
uint64_t lsb = masked_bitset.to_ullong(); |
| 2831 |
if (lsb != 0) return base::bits::CountTrailingZeros(lsb); |
| 2832 |
} |
| 2833 |
|
| 2834 |
{ |
| 2835 |
BoyerMoorePositionInfo::Bitset masked_bitset = bitset >> 64; |
| 2836 |
uint64_t msb = masked_bitset.to_ullong(); |
| 2837 |
if (msb != 0) return 64 + base::bits::CountTrailingZeros(msb); |
| 2838 |
} |
| 2839 |
|
| 2840 |
return -1; |
| 2841 |
} |
| 2842 |
|
| 2843 |
} // namespace |
| 2844 |
|
| 2845 |
void BoyerMoorePositionInfo::SetInterval(const Interval& interval) { |
| 2846 |
w_ = AddRange(w_, kWordRanges, kWordRangeCount, interval); |
| 2847 |
|
| 2848 |
if (interval.size() >= kMapSize) { |
| 2849 |
map_count_ = kMapSize; |
| 2850 |
map_.set(); |
| 2851 |
return; |
| 2852 |
} |
| 2853 |
|
| 2854 |
for (int i = interval.from(); i <= interval.to(); i++) { |
| 2855 |
int mod_character = (i & kMask); |
| 2856 |
if (!map_[mod_character]) { |
| 2857 |
map_count_++; |
| 2858 |
map_.set(mod_character); |
| 2859 |
} |
| 2860 |
if (map_count_ == kMapSize) return; |
| 2861 |
} |
| 2862 |
} |
| 2863 |
|
| 2864 |
void BoyerMoorePositionInfo::SetAll() { |
| 2865 |
w_ = kLatticeUnknown; |
| 2866 |
if (map_count_ != kMapSize) { |
| 2867 |
map_count_ = kMapSize; |
| 2868 |
map_.set(); |
| 2869 |
} |
| 2870 |
} |
| 2871 |
|
| 2872 |
BoyerMooreLookahead::BoyerMooreLookahead(int length, RegExpCompiler* compiler, |
| 2873 |
Zone* zone) |
| 2874 |
: length_(length), |
| 2875 |
compiler_(compiler), |
| 2876 |
max_char_(MaxCodeUnit(compiler->one_byte())) { |
| 2877 |
bitmaps_ = zone->New<ZoneList<BoyerMoorePositionInfo*>>(length, zone); |
| 2878 |
for (int i = 0; i < length; i++) { |
| 2879 |
bitmaps_->Add(zone->New<BoyerMoorePositionInfo>(), zone); |
| 2880 |
} |
| 2881 |
} |
| 2882 |
|
| 2883 |
// Find the longest range of lookahead that has the fewest number of different |
| 2884 |
// characters that can occur at a given position. Since we are optimizing two |
| 2885 |
// different parameters at once this is a tradeoff. |
| 2886 |
bool BoyerMooreLookahead::FindWorthwhileInterval(int* from, int* to) { |
| 2887 |
int biggest_points = 0; |
| 2888 |
// If more than 32 characters out of 128 can occur it is unlikely that we can |
| 2889 |
// be lucky enough to step forwards much of the time. |
| 2890 |
const int kMaxMax = 32; |
| 2891 |
for (int max_number_of_chars = 4; max_number_of_chars < kMaxMax; |
| 2892 |
max_number_of_chars *= 2) { |
| 2893 |
biggest_points = |
| 2894 |
FindBestInterval(max_number_of_chars, biggest_points, from, to); |
| 2895 |
} |
| 2896 |
if (biggest_points == 0) return false; |
| 2897 |
return true; |
| 2898 |
} |
| 2899 |
|
| 2900 |
// Find the highest-points range between 0 and length_ where the character |
| 2901 |
// information is not too vague. 'Too vague' means that there are more than |
| 2902 |
// max_number_of_chars that can occur at this position. Calculates the number |
| 2903 |
// of points as the product of width-of-the-range and |
| 2904 |
// probability-of-finding-one-of-the-characters, where the probability is |
| 2905 |
// calculated using the frequency distribution of the sample subject string. |
| 2906 |
int BoyerMooreLookahead::FindBestInterval(int max_number_of_chars, |
| 2907 |
int old_biggest_points, int* from, |
| 2908 |
int* to) { |
| 2909 |
int biggest_points = old_biggest_points; |
| 2910 |
static const int kSize = RegExpMacroAssembler::kTableSize; |
| 2911 |
for (int i = 0; i < length_;) { |
| 2912 |
while (i < length_ && Count(i) > max_number_of_chars) i++; |
| 2913 |
if (i == length_) break; |
| 2914 |
int remembered_from = i; |
| 2915 |
|
| 2916 |
BoyerMoorePositionInfo::Bitset union_bitset; |
| 2917 |
for (; i < length_ && Count(i) <= max_number_of_chars; i++) { |
| 2918 |
union_bitset |= bitmaps_->at(i)->raw_bitset(); |
| 2919 |
} |
| 2920 |
|
| 2921 |
int frequency = 0; |
| 2922 |
|
| 2923 |
// Iterate only over set bits. |
| 2924 |
int j; |
| 2925 |
while ((j = BitsetFirstSetBit(union_bitset)) != -1) { |
| 2926 |
DCHECK(union_bitset[j]); // Sanity check. |
| 2927 |
// Add 1 to the frequency to give a small per-character boost for |
| 2928 |
// the cases where our sampling is not good enough and many |
| 2929 |
// characters have a frequency of zero. This means the frequency |
| 2930 |
// can theoretically be up to 2*kSize though we treat it mostly as |
| 2931 |
// a fraction of kSize. |
| 2932 |
frequency += compiler_->frequency_collator()->Frequency(j) + 1; |
| 2933 |
union_bitset.reset(j); |
| 2934 |
} |
| 2935 |
|
| 2936 |
// We use the probability of skipping times the distance we are skipping to |
| 2937 |
// judge the effectiveness of this. Actually we have a cut-off: By |
| 2938 |
// dividing by 2 we switch off the skipping if the probability of skipping |
| 2939 |
// is less than 50%. This is because the multibyte mask-and-compare |
| 2940 |
// skipping in quickcheck is more likely to do well on this case. |
| 2941 |
bool in_quickcheck_range = |
| 2942 |
((i - remembered_from < 4) || |
| 2943 |
(compiler_->one_byte() ? remembered_from <= 4 : remembered_from <= 2)); |
| 2944 |
// Called 'probability' but it is only a rough estimate and can actually |
| 2945 |
// be outside the 0-kSize range. |
| 2946 |
int probability = (in_quickcheck_range ? kSize / 2 : kSize) - frequency; |
| 2947 |
int points = (i - remembered_from) * probability; |
| 2948 |
if (points > biggest_points) { |
| 2949 |
*from = remembered_from; |
| 2950 |
*to = i - 1; |
| 2951 |
biggest_points = points; |
| 2952 |
} |
| 2953 |
} |
| 2954 |
return biggest_points; |
| 2955 |
} |
| 2956 |
|
| 2957 |
// Take all the characters that will not prevent a successful match if they |
| 2958 |
// occur in the subject string in the range between min_lookahead and |
| 2959 |
// max_lookahead (inclusive) measured from the current position. If the |
| 2960 |
// character at max_lookahead offset is not one of these characters, then we |
| 2961 |
// can safely skip forwards by the number of characters in the range. |
| 2962 |
// nibble_table is only used for SIMD variants and encodes the same information |
| 2963 |
// as boolean_skip_table but in only 128 bits. It contains 16 bytes where the |
| 2964 |
// index into the table represent low nibbles of a character, and the stored |
| 2965 |
// byte is a bitset representing matching high nibbles. E.g. to store the |
| 2966 |
// character 'b' (0x62) in the nibble table, we set the 6th bit in row 2. |
| 2967 |
int BoyerMooreLookahead::GetSkipTable( |
| 2968 |
int min_lookahead, int max_lookahead, |
| 2969 |
DirectHandle<ByteArray> boolean_skip_table, |
| 2970 |
DirectHandle<ByteArray> nibble_table) { |
| 2971 |
const int kSkipArrayEntry = 0; |
| 2972 |
const int kDontSkipArrayEntry = 1; |
| 2973 |
|
| 2974 |
std::memset(boolean_skip_table->begin(), kSkipArrayEntry, |
| 2975 |
boolean_skip_table->length()); |
| 2976 |
const bool fill_nibble_table = !nibble_table.is_null(); |
| 2977 |
if (fill_nibble_table) { |
| 2978 |
std::memset(nibble_table->begin(), 0, nibble_table->length()); |
| 2979 |
} |
| 2980 |
|
| 2981 |
for (int i = max_lookahead; i >= min_lookahead; i--) { |
| 2982 |
BoyerMoorePositionInfo::Bitset bitset = bitmaps_->at(i)->raw_bitset(); |
| 2983 |
|
| 2984 |
// Iterate only over set bits. |
| 2985 |
int j; |
| 2986 |
while ((j = BitsetFirstSetBit(bitset)) != -1) { |
| 2987 |
DCHECK(bitset[j]); // Sanity check. |
| 2988 |
boolean_skip_table->set(j, kDontSkipArrayEntry); |
| 2989 |
if (fill_nibble_table) { |
| 2990 |
int lo_nibble = j & 0x0f; |
| 2991 |
int hi_nibble = (j >> 4) & 0x07; |
| 2992 |
int row = nibble_table->get(lo_nibble); |
| 2993 |
row |= 1 << hi_nibble; |
| 2994 |
nibble_table->set(lo_nibble, row); |
| 2995 |
} |
| 2996 |
bitset.reset(j); |
| 2997 |
} |
| 2998 |
} |
| 2999 |
|
| 3000 |
const int skip = max_lookahead + 1 - min_lookahead; |
| 3001 |
return skip; |
| 3002 |
} |
| 3003 |
|
| 3004 |
// See comment above on the implementation of GetSkipTable. |
| 3005 |
void BoyerMooreLookahead::EmitSkipInstructions(RegExpMacroAssembler* masm) { |
| 3006 |
const int kSize = RegExpMacroAssembler::kTableSize; |
| 3007 |
|
| 3008 |
int min_lookahead = 0; |
| 3009 |
int max_lookahead = 0; |
| 3010 |
|
| 3011 |
if (!FindWorthwhileInterval(&min_lookahead, &max_lookahead)) return; |
| 3012 |
|
| 3013 |
// Check if we only have a single non-empty position info, and that info |
| 3014 |
// contains precisely one character. |
| 3015 |
bool found_single_character = false; |
| 3016 |
int single_character = 0; |
| 3017 |
for (int i = max_lookahead; i >= min_lookahead; i--) { |
| 3018 |
BoyerMoorePositionInfo* map = bitmaps_->at(i); |
| 3019 |
if (map->map_count() == 0) continue; |
| 3020 |
|
| 3021 |
if (found_single_character || map->map_count() > 1) { |
| 3022 |
found_single_character = false; |
| 3023 |
break; |
| 3024 |
} |
| 3025 |
|
| 3026 |
DCHECK(!found_single_character); |
| 3027 |
DCHECK_EQ(map->map_count(), 1); |
| 3028 |
|
| 3029 |
found_single_character = true; |
| 3030 |
single_character = BitsetFirstSetBit(map->raw_bitset()); |
| 3031 |
|
| 3032 |
DCHECK_NE(single_character, -1); |
| 3033 |
} |
| 3034 |
|
| 3035 |
int lookahead_width = max_lookahead + 1 - min_lookahead; |
| 3036 |
|
| 3037 |
if (found_single_character && lookahead_width == 1 && max_lookahead < 3) { |
| 3038 |
// The mask-compare can probably handle this better. |
| 3039 |
return; |
| 3040 |
} |
| 3041 |
|
| 3042 |
if (found_single_character) { |
| 3043 |
// TODO(pthier): Add vectorized version. |
| 3044 |
Label cont, again; |
| 3045 |
masm->Bind(&again); |
| 3046 |
masm->LoadCurrentCharacter(max_lookahead, &cont, true); |
| 3047 |
if (max_char_ > kSize) { |
| 3048 |
masm->CheckCharacterAfterAnd(single_character, |
| 3049 |
RegExpMacroAssembler::kTableMask, &cont); |
| 3050 |
} else { |
| 3051 |
masm->CheckCharacter(single_character, &cont); |
| 3052 |
} |
| 3053 |
masm->AdvanceCurrentPosition(lookahead_width); |
| 3054 |
masm->GoTo(&again); |
| 3055 |
masm->Bind(&cont); |
| 3056 |
return; |
| 3057 |
} |
| 3058 |
|
| 3059 |
Factory* factory = masm->isolate()->factory(); |
| 3060 |
Handle<ByteArray> boolean_skip_table = |
| 3061 |
factory->NewByteArray(kSize, AllocationType::kOld); |
| 3062 |
Handle<ByteArray> nibble_table; |
| 3063 |
const int skip_distance = max_lookahead + 1 - min_lookahead; |
| 3064 |
if (masm->SkipUntilBitInTableUseSimd(skip_distance)) { |
| 3065 |
// The current implementation is tailored specifically for 128-bit tables. |
| 3066 |
static_assert(kSize == 128); |
| 3067 |
nibble_table = |
| 3068 |
factory->NewByteArray(kSize / kBitsPerByte, AllocationType::kOld); |
| 3069 |
} |
| 3070 |
GetSkipTable(min_lookahead, max_lookahead, boolean_skip_table, nibble_table); |
| 3071 |
DCHECK_NE(0, skip_distance); |
| 3072 |
|
| 3073 |
masm->SkipUntilBitInTable(max_lookahead, boolean_skip_table, nibble_table, |
| 3074 |
skip_distance); |
| 3075 |
} |
| 3076 |
|
| 3077 |
/* Code generation for choice nodes. |
| 3078 |
* |
| 3079 |
* We generate quick checks that do a mask and compare to eliminate a |
| 3080 |
* choice. If the quick check succeeds then it jumps to the continuation to |
| 3081 |
* do slow checks and check subsequent nodes. If it fails (the common case) |
| 3082 |
* it falls through to the next choice. |
| 3083 |
* |
| 3084 |
* Here is the desired flow graph. Nodes directly below each other imply |
| 3085 |
* fallthrough. Alternatives 1 and 2 have quick checks. Alternative |
| 3086 |
* 3 doesn't have a quick check so we have to call the slow check. |
| 3087 |
* Nodes are marked Qn for quick checks and Sn for slow checks. The entire |
| 3088 |
* regexp continuation is generated directly after the Sn node, up to the |
| 3089 |
* next GoTo if we decide to reuse some already generated code. Some |
| 3090 |
* nodes expect preload_characters to be preloaded into the current |
| 3091 |
* character register. R nodes do this preloading. Vertices are marked |
| 3092 |
* F for failures and S for success (possible success in the case of quick |
| 3093 |
* nodes). L, V, < and > are used as arrow heads. |
| 3094 |
* |
| 3095 |
* ----------> R |
| 3096 |
* | |
| 3097 |
* V |
| 3098 |
* Q1 -----> S1 |
| 3099 |
* | S / |
| 3100 |
* F| / |
| 3101 |
* | F/ |
| 3102 |
* | / |
| 3103 |
* | R |
| 3104 |
* | / |
| 3105 |
* V L |
| 3106 |
* Q2 -----> S2 |
| 3107 |
* | S / |
| 3108 |
* F| / |
| 3109 |
* | F/ |
| 3110 |
* | / |
| 3111 |
* | R |
| 3112 |
* | / |
| 3113 |
* V L |
| 3114 |
* S3 |
| 3115 |
* | |
| 3116 |
* F| |
| 3117 |
* | |
| 3118 |
* R |
| 3119 |
* | |
| 3120 |
* backtrack V |
| 3121 |
* <----------Q4 |
| 3122 |
* \ F | |
| 3123 |
* \ |S |
| 3124 |
* \ F V |
| 3125 |
* \-----S4 |
| 3126 |
* |
| 3127 |
* For greedy loops we push the current position, then generate the code that |
| 3128 |
* eats the input specially in EmitGreedyLoop. The other choice (the |
| 3129 |
* continuation) is generated by the normal code in EmitChoices, and steps back |
| 3130 |
* in the input to the starting position when it fails to match. The loop code |
| 3131 |
* looks like this (U is the unwind code that steps back in the greedy loop). |
| 3132 |
* |
| 3133 |
* _____ |
| 3134 |
* / \ |
| 3135 |
* V | |
| 3136 |
* ----------> S1 | |
| 3137 |
* /| | |
| 3138 |
* / |S | |
| 3139 |
* F/ \_____/ |
| 3140 |
* / |
| 3141 |
* |<----- |
| 3142 |
* | \ |
| 3143 |
* V |S |
| 3144 |
* Q2 ---> U----->backtrack |
| 3145 |
* | F / |
| 3146 |
* S| / |
| 3147 |
* V F / |
| 3148 |
* S2--/ |
| 3149 |
*/ |
| 3150 |
|
| 3151 |
GreedyLoopState::GreedyLoopState(bool not_at_start) { |
| 3152 |
counter_backtrack_trace_.set_backtrack(&label_); |
| 3153 |
if (not_at_start) counter_backtrack_trace_.set_at_start(Trace::FALSE_VALUE); |
| 3154 |
} |
| 3155 |
|
| 3156 |
void ChoiceNode::AssertGuardsMentionRegisters(Trace* trace) { |
| 3157 |
#ifdef DEBUG |
| 3158 |
int choice_count = alternatives_->length(); |
| 3159 |
for (int i = 0; i < choice_count - 1; i++) { |
| 3160 |
GuardedAlternative alternative = alternatives_->at(i); |
| 3161 |
ZoneList<Guard*>* guards = alternative.guards(); |
| 3162 |
int guard_count = (guards == nullptr) ? 0 : guards->length(); |
| 3163 |
for (int j = 0; j < guard_count; j++) { |
| 3164 |
DCHECK(!trace->mentions_reg(guards->at(j)->reg())); |
| 3165 |
} |
| 3166 |
} |
| 3167 |
#endif |
| 3168 |
} |
| 3169 |
|
| 3170 |
void ChoiceNode::SetUpPreLoad(RegExpCompiler* compiler, Trace* current_trace, |
| 3171 |
PreloadState* state) { |
| 3172 |
if (state->eats_at_least_ == PreloadState::kEatsAtLeastNotYetInitialized) { |
| 3173 |
// Save some time by looking at most one machine word ahead. |
| 3174 |
state->eats_at_least_ = |
| 3175 |
EatsAtLeast(current_trace->at_start() == Trace::FALSE_VALUE); |
| 3176 |
} |
| 3177 |
state->preload_characters_ = |
| 3178 |
CalculatePreloadCharacters(compiler, state->eats_at_least_); |
| 3179 |
|
| 3180 |
state->preload_is_current_ = |
| 3181 |
(current_trace->characters_preloaded() == state->preload_characters_); |
| 3182 |
state->preload_has_checked_bounds_ = state->preload_is_current_; |
| 3183 |
} |
| 3184 |
|
| 3185 |
void ChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 3186 |
int choice_count = alternatives_->length(); |
| 3187 |
|
| 3188 |
if (choice_count == 1 && alternatives_->at(0).guards() == nullptr) { |
| 3189 |
alternatives_->at(0).node()->Emit(compiler, trace); |
| 3190 |
return; |
| 3191 |
} |
| 3192 |
|
| 3193 |
AssertGuardsMentionRegisters(trace); |
| 3194 |
|
| 3195 |
LimitResult limit_result = LimitVersions(compiler, trace); |
| 3196 |
if (limit_result == DONE) return; |
| 3197 |
DCHECK(limit_result == CONTINUE); |
| 3198 |
|
| 3199 |
// For loop nodes we already flushed (see LoopChoiceNode::Emit), but for |
| 3200 |
// other choice nodes we only flush if we are out of code size budget. |
| 3201 |
if (trace->flush_budget() == 0 && trace->actions() != nullptr) { |
| 3202 |
trace->Flush(compiler, this); |
| 3203 |
return; |
| 3204 |
} |
| 3205 |
|
| 3206 |
RecursionCheck rc(compiler); |
| 3207 |
|
| 3208 |
PreloadState preload; |
| 3209 |
preload.init(); |
| 3210 |
GreedyLoopState greedy_loop_state(not_at_start()); |
| 3211 |
|
| 3212 |
int text_length = GreedyLoopTextLengthForAlternative(&alternatives_->at(0)); |
| 3213 |
AlternativeGenerationList alt_gens(choice_count, zone()); |
| 3214 |
|
| 3215 |
if (choice_count > 1 && text_length != kNodeIsTooComplexForGreedyLoops) { |
| 3216 |
trace = EmitGreedyLoop(compiler, trace, &alt_gens, &preload, |
| 3217 |
&greedy_loop_state, text_length); |
| 3218 |
} else { |
| 3219 |
preload.eats_at_least_ = EmitOptimizedUnanchoredSearch(compiler, trace); |
| 3220 |
|
| 3221 |
EmitChoices(compiler, &alt_gens, 0, trace, &preload); |
| 3222 |
} |
| 3223 |
|
| 3224 |
// At this point we need to generate slow checks for the alternatives where |
| 3225 |
// the quick check was inlined. We can recognize these because the associated |
| 3226 |
// label was bound. |
| 3227 |
int new_flush_budget = trace->flush_budget() / choice_count; |
| 3228 |
for (int i = 0; i < choice_count; i++) { |
| 3229 |
AlternativeGeneration* alt_gen = alt_gens.at(i); |
| 3230 |
Trace new_trace(*trace); |
| 3231 |
// If there are actions to be flushed we have to limit how many times |
| 3232 |
// they are flushed. Take the budget of the parent trace and distribute |
| 3233 |
// it fairly amongst the children. |
| 3234 |
if (new_trace.actions() != nullptr) { |
| 3235 |
new_trace.set_flush_budget(new_flush_budget); |
| 3236 |
} |
| 3237 |
bool next_expects_preload = |
| 3238 |
i == choice_count - 1 ? false : alt_gens.at(i + 1)->expects_preload; |
| 3239 |
EmitOutOfLineContinuation(compiler, &new_trace, alternatives_->at(i), |
| 3240 |
alt_gen, preload.preload_characters_, |
| 3241 |
next_expects_preload); |
| 3242 |
} |
| 3243 |
} |
| 3244 |
|
| 3245 |
Trace* ChoiceNode::EmitGreedyLoop(RegExpCompiler* compiler, Trace* trace, |
| 3246 |
AlternativeGenerationList* alt_gens, |
| 3247 |
PreloadState* preload, |
| 3248 |
GreedyLoopState* greedy_loop_state, |
| 3249 |
int text_length) { |
| 3250 |
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 3251 |
// Here we have special handling for greedy loops containing only text nodes |
| 3252 |
// and other simple nodes. These are handled by pushing the current |
| 3253 |
// position on the stack and then incrementing the current position each |
| 3254 |
// time around the switch. On backtrack we decrement the current position |
| 3255 |
// and check it against the pushed value. This avoids pushing backtrack |
| 3256 |
// information for each iteration of the loop, which could take up a lot of |
| 3257 |
// space. |
| 3258 |
DCHECK(trace->stop_node() == nullptr); |
| 3259 |
macro_assembler->PushCurrentPosition(); |
| 3260 |
Label greedy_match_failed; |
| 3261 |
Trace greedy_match_trace; |
| 3262 |
if (not_at_start()) greedy_match_trace.set_at_start(Trace::FALSE_VALUE); |
| 3263 |
greedy_match_trace.set_backtrack(&greedy_match_failed); |
| 3264 |
Label loop_label; |
| 3265 |
macro_assembler->Bind(&loop_label); |
| 3266 |
greedy_match_trace.set_stop_node(this); |
| 3267 |
greedy_match_trace.set_loop_label(&loop_label); |
| 3268 |
alternatives_->at(0).node()->Emit(compiler, &greedy_match_trace); |
| 3269 |
macro_assembler->Bind(&greedy_match_failed); |
| 3270 |
|
| 3271 |
Label second_choice; // For use in greedy matches. |
| 3272 |
macro_assembler->Bind(&second_choice); |
| 3273 |
|
| 3274 |
Trace* new_trace = greedy_loop_state->counter_backtrack_trace(); |
| 3275 |
|
| 3276 |
EmitChoices(compiler, alt_gens, 1, new_trace, preload); |
| 3277 |
|
| 3278 |
macro_assembler->Bind(greedy_loop_state->label()); |
| 3279 |
// If we have unwound to the bottom then backtrack. |
| 3280 |
macro_assembler->CheckGreedyLoop(trace->backtrack()); |
| 3281 |
// Otherwise try the second priority at an earlier position. |
| 3282 |
macro_assembler->AdvanceCurrentPosition(-text_length); |
| 3283 |
macro_assembler->GoTo(&second_choice); |
| 3284 |
return new_trace; |
| 3285 |
} |
| 3286 |
|
| 3287 |
int ChoiceNode::EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler, |
| 3288 |
Trace* trace) { |
| 3289 |
int eats_at_least = PreloadState::kEatsAtLeastNotYetInitialized; |
| 3290 |
if (alternatives_->length() != 2) return eats_at_least; |
| 3291 |
|
| 3292 |
GuardedAlternative alt1 = alternatives_->at(1); |
| 3293 |
if (alt1.guards() != nullptr && alt1.guards()->length() != 0) { |
| 3294 |
return eats_at_least; |
| 3295 |
} |
| 3296 |
RegExpNode* eats_anything_node = alt1.node(); |
| 3297 |
if (eats_anything_node->GetSuccessorOfOmnivorousTextNode(compiler) != this) { |
| 3298 |
return eats_at_least; |
| 3299 |
} |
| 3300 |
|
| 3301 |
// Really we should be creating a new trace when we execute this function, |
| 3302 |
// but there is no need, because the code it generates cannot backtrack, and |
| 3303 |
// we always arrive here with a trivial trace (since it's the entry to a |
| 3304 |
// loop. That also implies that there are no preloaded characters, which is |
| 3305 |
// good, because it means we won't be violating any assumptions by |
| 3306 |
// overwriting those characters with new load instructions. |
| 3307 |
DCHECK(trace->is_trivial()); |
| 3308 |
|
| 3309 |
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 3310 |
Isolate* isolate = macro_assembler->isolate(); |
| 3311 |
// At this point we know that we are at a non-greedy loop that will eat |
| 3312 |
// any character one at a time. Any non-anchored regexp has such a |
| 3313 |
// loop prepended to it in order to find where it starts. We look for |
| 3314 |
// a pattern of the form ...abc... where we can look 6 characters ahead |
| 3315 |
// and step forwards 3 if the character is not one of abc. Abc need |
| 3316 |
// not be atoms, they can be any reasonably limited character class or |
| 3317 |
// small alternation. |
| 3318 |
BoyerMooreLookahead* bm = bm_info(false); |
| 3319 |
if (bm == nullptr) { |
| 3320 |
eats_at_least = std::min(kMaxLookaheadForBoyerMoore, EatsAtLeast(false)); |
| 3321 |
if (eats_at_least >= 1) { |
| 3322 |
bm = zone()->New<BoyerMooreLookahead>(eats_at_least, compiler, zone()); |
| 3323 |
GuardedAlternative alt0 = alternatives_->at(0); |
| 3324 |
alt0.node()->FillInBMInfo(isolate, 0, kRecursionBudget, bm, false); |
| 3325 |
} |
| 3326 |
} |
| 3327 |
if (bm != nullptr) { |
| 3328 |
bm->EmitSkipInstructions(macro_assembler); |
| 3329 |
} |
| 3330 |
return eats_at_least; |
| 3331 |
} |
| 3332 |
|
| 3333 |
void ChoiceNode::EmitChoices(RegExpCompiler* compiler, |
| 3334 |
AlternativeGenerationList* alt_gens, |
| 3335 |
int first_choice, Trace* trace, |
| 3336 |
PreloadState* preload) { |
| 3337 |
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 3338 |
SetUpPreLoad(compiler, trace, preload); |
| 3339 |
|
| 3340 |
// For now we just call all choices one after the other. The idea ultimately |
| 3341 |
// is to use the Dispatch table to try only the relevant ones. |
| 3342 |
int choice_count = alternatives_->length(); |
| 3343 |
|
| 3344 |
int new_flush_budget = trace->flush_budget() / choice_count; |
| 3345 |
|
| 3346 |
for (int i = first_choice; i < choice_count; i++) { |
| 3347 |
bool is_last = i == choice_count - 1; |
| 3348 |
bool fall_through_on_failure = !is_last; |
| 3349 |
GuardedAlternative alternative = alternatives_->at(i); |
| 3350 |
AlternativeGeneration* alt_gen = alt_gens->at(i); |
| 3351 |
alt_gen->quick_check_details.set_characters(preload->preload_characters_); |
| 3352 |
ZoneList<Guard*>* guards = alternative.guards(); |
| 3353 |
int guard_count = (guards == nullptr) ? 0 : guards->length(); |
| 3354 |
Trace new_trace(*trace); |
| 3355 |
new_trace.set_characters_preloaded( |
| 3356 |
preload->preload_is_current_ ? preload->preload_characters_ : 0); |
| 3357 |
if (preload->preload_has_checked_bounds_) { |
| 3358 |
new_trace.set_bound_checked_up_to(preload->preload_characters_); |
| 3359 |
} |
| 3360 |
new_trace.quick_check_performed()->Clear(); |
| 3361 |
if (not_at_start_) new_trace.set_at_start(Trace::FALSE_VALUE); |
| 3362 |
if (!is_last) { |
| 3363 |
new_trace.set_backtrack(&alt_gen->after); |
| 3364 |
} |
| 3365 |
alt_gen->expects_preload = preload->preload_is_current_; |
| 3366 |
bool generate_full_check_inline = false; |
| 3367 |
if (v8_flags.regexp_optimization && |
| 3368 |
try_to_emit_quick_check_for_alternative(i == 0) && |
| 3369 |
alternative.node()->EmitQuickCheck( |
| 3370 |
compiler, trace, &new_trace, preload->preload_has_checked_bounds_, |
| 3371 |
&alt_gen->possible_success, &alt_gen->quick_check_details, |
| 3372 |
fall_through_on_failure, this)) { |
| 3373 |
// Quick check was generated for this choice. |
| 3374 |
preload->preload_is_current_ = true; |
| 3375 |
preload->preload_has_checked_bounds_ = true; |
| 3376 |
// If we generated the quick check to fall through on possible success, |
| 3377 |
// we now need to generate the full check inline. |
| 3378 |
if (!fall_through_on_failure) { |
| 3379 |
macro_assembler->Bind(&alt_gen->possible_success); |
| 3380 |
new_trace.set_quick_check_performed(&alt_gen->quick_check_details); |
| 3381 |
new_trace.set_characters_preloaded(preload->preload_characters_); |
| 3382 |
new_trace.set_bound_checked_up_to(preload->preload_characters_); |
| 3383 |
generate_full_check_inline = true; |
| 3384 |
} |
| 3385 |
} else if (alt_gen->quick_check_details.cannot_match()) { |
| 3386 |
if (!fall_through_on_failure) { |
| 3387 |
macro_assembler->GoTo(trace->backtrack()); |
| 3388 |
} |
| 3389 |
continue; |
| 3390 |
} else { |
| 3391 |
// No quick check was generated. Put the full code here. |
| 3392 |
// If this is not the first choice then there could be slow checks from |
| 3393 |
// previous cases that go here when they fail. There's no reason to |
| 3394 |
// insist that they preload characters since the slow check we are about |
| 3395 |
// to generate probably can't use it. |
| 3396 |
if (i != first_choice) { |
| 3397 |
alt_gen->expects_preload = false; |
| 3398 |
new_trace.InvalidateCurrentCharacter(); |
| 3399 |
} |
| 3400 |
generate_full_check_inline = true; |
| 3401 |
} |
| 3402 |
if (generate_full_check_inline) { |
| 3403 |
if (new_trace.actions() != nullptr) { |
| 3404 |
new_trace.set_flush_budget(new_flush_budget); |
| 3405 |
} |
| 3406 |
for (int j = 0; j < guard_count; j++) { |
| 3407 |
GenerateGuard(macro_assembler, guards->at(j), &new_trace); |
| 3408 |
} |
| 3409 |
alternative.node()->Emit(compiler, &new_trace); |
| 3410 |
preload->preload_is_current_ = false; |
| 3411 |
} |
| 3412 |
macro_assembler->Bind(&alt_gen->after); |
| 3413 |
} |
| 3414 |
} |
| 3415 |
|
| 3416 |
void ChoiceNode::EmitOutOfLineContinuation(RegExpCompiler* compiler, |
| 3417 |
Trace* trace, |
| 3418 |
GuardedAlternative alternative, |
| 3419 |
AlternativeGeneration* alt_gen, |
| 3420 |
int preload_characters, |
| 3421 |
bool next_expects_preload) { |
| 3422 |
if (!alt_gen->possible_success.is_linked()) return; |
| 3423 |
|
| 3424 |
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 3425 |
macro_assembler->Bind(&alt_gen->possible_success); |
| 3426 |
Trace out_of_line_trace(*trace); |
| 3427 |
out_of_line_trace.set_characters_preloaded(preload_characters); |
| 3428 |
out_of_line_trace.set_quick_check_performed(&alt_gen->quick_check_details); |
| 3429 |
if (not_at_start_) out_of_line_trace.set_at_start(Trace::FALSE_VALUE); |
| 3430 |
ZoneList<Guard*>* guards = alternative.guards(); |
| 3431 |
int guard_count = (guards == nullptr) ? 0 : guards->length(); |
| 3432 |
if (next_expects_preload) { |
| 3433 |
Label reload_current_char; |
| 3434 |
out_of_line_trace.set_backtrack(&reload_current_char); |
| 3435 |
for (int j = 0; j < guard_count; j++) { |
| 3436 |
GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace); |
| 3437 |
} |
| 3438 |
alternative.node()->Emit(compiler, &out_of_line_trace); |
| 3439 |
macro_assembler->Bind(&reload_current_char); |
| 3440 |
// Reload the current character, since the next quick check expects that. |
| 3441 |
// We don't need to check bounds here because we only get into this |
| 3442 |
// code through a quick check which already did the checked load. |
| 3443 |
macro_assembler->LoadCurrentCharacter(trace->cp_offset(), nullptr, false, |
| 3444 |
preload_characters); |
| 3445 |
macro_assembler->GoTo(&(alt_gen->after)); |
| 3446 |
} else { |
| 3447 |
out_of_line_trace.set_backtrack(&(alt_gen->after)); |
| 3448 |
for (int j = 0; j < guard_count; j++) { |
| 3449 |
GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace); |
| 3450 |
} |
| 3451 |
alternative.node()->Emit(compiler, &out_of_line_trace); |
| 3452 |
} |
| 3453 |
} |
| 3454 |
|
| 3455 |
void ActionNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 3456 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 3457 |
LimitResult limit_result = LimitVersions(compiler, trace); |
| 3458 |
if (limit_result == DONE) return; |
| 3459 |
DCHECK(limit_result == CONTINUE); |
| 3460 |
|
| 3461 |
RecursionCheck rc(compiler); |
| 3462 |
|
| 3463 |
switch (action_type_) { |
| 3464 |
case STORE_POSITION: { |
| 3465 |
Trace::DeferredCapture new_capture(data_.u_position_register.reg, |
| 3466 |
data_.u_position_register.is_capture, |
| 3467 |
trace); |
| 3468 |
Trace new_trace = *trace; |
| 3469 |
new_trace.add_action(&new_capture); |
| 3470 |
on_success()->Emit(compiler, &new_trace); |
| 3471 |
break; |
| 3472 |
} |
| 3473 |
case INCREMENT_REGISTER: { |
| 3474 |
Trace::DeferredIncrementRegister new_increment( |
| 3475 |
data_.u_increment_register.reg); |
| 3476 |
Trace new_trace = *trace; |
| 3477 |
new_trace.add_action(&new_increment); |
| 3478 |
on_success()->Emit(compiler, &new_trace); |
| 3479 |
break; |
| 3480 |
} |
| 3481 |
case SET_REGISTER_FOR_LOOP: { |
| 3482 |
Trace::DeferredSetRegisterForLoop new_set(data_.u_store_register.reg, |
| 3483 |
data_.u_store_register.value); |
| 3484 |
Trace new_trace = *trace; |
| 3485 |
new_trace.add_action(&new_set); |
| 3486 |
on_success()->Emit(compiler, &new_trace); |
| 3487 |
break; |
| 3488 |
} |
| 3489 |
case CLEAR_CAPTURES: { |
| 3490 |
Trace::DeferredClearCaptures new_capture(Interval( |
| 3491 |
data_.u_clear_captures.range_from, data_.u_clear_captures.range_to)); |
| 3492 |
Trace new_trace = *trace; |
| 3493 |
new_trace.add_action(&new_capture); |
| 3494 |
on_success()->Emit(compiler, &new_trace); |
| 3495 |
break; |
| 3496 |
} |
| 3497 |
case BEGIN_POSITIVE_SUBMATCH: |
| 3498 |
case BEGIN_NEGATIVE_SUBMATCH: |
| 3499 |
if (!trace->is_trivial()) { |
| 3500 |
trace->Flush(compiler, this); |
| 3501 |
} else { |
| 3502 |
assembler->WriteCurrentPositionToRegister( |
| 3503 |
data_.u_submatch.current_position_register, 0); |
| 3504 |
assembler->WriteStackPointerToRegister( |
| 3505 |
data_.u_submatch.stack_pointer_register); |
| 3506 |
on_success()->Emit(compiler, trace); |
| 3507 |
} |
| 3508 |
break; |
| 3509 |
case EMPTY_MATCH_CHECK: { |
| 3510 |
int start_pos_reg = data_.u_empty_match_check.start_register; |
| 3511 |
int stored_pos = 0; |
| 3512 |
int rep_reg = data_.u_empty_match_check.repetition_register; |
| 3513 |
bool has_minimum = (rep_reg != RegExpCompiler::kNoRegister); |
| 3514 |
bool know_dist = trace->GetStoredPosition(start_pos_reg, &stored_pos); |
| 3515 |
if (know_dist && !has_minimum && stored_pos == trace->cp_offset()) { |
| 3516 |
// If we know we haven't advanced and there is no minimum we |
| 3517 |
// can just backtrack immediately. |
| 3518 |
assembler->GoTo(trace->backtrack()); |
| 3519 |
} else if (know_dist && stored_pos < trace->cp_offset()) { |
| 3520 |
// If we know we've advanced we can generate the continuation |
| 3521 |
// immediately. |
| 3522 |
on_success()->Emit(compiler, trace); |
| 3523 |
} else if (!trace->is_trivial()) { |
| 3524 |
trace->Flush(compiler, this); |
| 3525 |
} else { |
| 3526 |
Label skip_empty_check; |
| 3527 |
// If we have a minimum number of repetitions we check the current |
| 3528 |
// number first and skip the empty check if it's not enough. |
| 3529 |
if (has_minimum) { |
| 3530 |
int limit = data_.u_empty_match_check.repetition_limit; |
| 3531 |
assembler->IfRegisterLT(rep_reg, limit, &skip_empty_check); |
| 3532 |
} |
| 3533 |
// If the match is empty we bail out, otherwise we fall through |
| 3534 |
// to the on-success continuation. |
| 3535 |
assembler->IfRegisterEqPos(data_.u_empty_match_check.start_register, |
| 3536 |
trace->backtrack()); |
| 3537 |
assembler->Bind(&skip_empty_check); |
| 3538 |
on_success()->Emit(compiler, trace); |
| 3539 |
} |
| 3540 |
break; |
| 3541 |
} |
| 3542 |
case POSITIVE_SUBMATCH_SUCCESS: { |
| 3543 |
if (!trace->is_trivial()) { |
| 3544 |
trace->Flush(compiler, this); |
| 3545 |
return; |
| 3546 |
} |
| 3547 |
assembler->ReadCurrentPositionFromRegister( |
| 3548 |
data_.u_submatch.current_position_register); |
| 3549 |
assembler->ReadStackPointerFromRegister( |
| 3550 |
data_.u_submatch.stack_pointer_register); |
| 3551 |
int clear_register_count = data_.u_submatch.clear_register_count; |
| 3552 |
if (clear_register_count == 0) { |
| 3553 |
on_success()->Emit(compiler, trace); |
| 3554 |
return; |
| 3555 |
} |
| 3556 |
int clear_registers_from = data_.u_submatch.clear_register_from; |
| 3557 |
Label clear_registers_backtrack; |
| 3558 |
Trace new_trace = *trace; |
| 3559 |
new_trace.set_backtrack(&clear_registers_backtrack); |
| 3560 |
on_success()->Emit(compiler, &new_trace); |
| 3561 |
|
| 3562 |
assembler->Bind(&clear_registers_backtrack); |
| 3563 |
int clear_registers_to = clear_registers_from + clear_register_count - 1; |
| 3564 |
assembler->ClearRegisters(clear_registers_from, clear_registers_to); |
| 3565 |
|
| 3566 |
DCHECK(trace->backtrack() == nullptr); |
| 3567 |
assembler->Backtrack(); |
| 3568 |
return; |
| 3569 |
} |
| 3570 |
case MODIFY_FLAGS: { |
| 3571 |
compiler->set_flags(flags()); |
| 3572 |
on_success()->Emit(compiler, trace); |
| 3573 |
break; |
| 3574 |
} |
| 3575 |
default: |
| 3576 |
UNREACHABLE(); |
| 3577 |
} |
| 3578 |
} |
| 3579 |
|
| 3580 |
void BackReferenceNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 3581 |
RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 3582 |
if (!trace->is_trivial()) { |
| 3583 |
trace->Flush(compiler, this); |
| 3584 |
return; |
| 3585 |
} |
| 3586 |
|
| 3587 |
LimitResult limit_result = LimitVersions(compiler, trace); |
| 3588 |
if (limit_result == DONE) return; |
| 3589 |
DCHECK(limit_result == CONTINUE); |
| 3590 |
|
| 3591 |
RecursionCheck rc(compiler); |
| 3592 |
|
| 3593 |
DCHECK_EQ(start_reg_ + 1, end_reg_); |
| 3594 |
if (IsIgnoreCase(compiler->flags())) { |
| 3595 |
bool unicode = IsEitherUnicode(compiler->flags()); |
| 3596 |
assembler->CheckNotBackReferenceIgnoreCase(start_reg_, read_backward(), |
| 3597 |
unicode, trace->backtrack()); |
| 3598 |
} else { |
| 3599 |
assembler->CheckNotBackReference(start_reg_, read_backward(), |
| 3600 |
trace->backtrack()); |
| 3601 |
} |
| 3602 |
// We are going to advance backward, so we may end up at the start. |
| 3603 |
if (read_backward()) trace->set_at_start(Trace::UNKNOWN); |
| 3604 |
|
| 3605 |
// Check that the back reference does not end inside a surrogate pair. |
| 3606 |
if (IsEitherUnicode(compiler->flags()) && !compiler->one_byte()) { |
| 3607 |
assembler->CheckNotInSurrogatePair(trace->cp_offset(), trace->backtrack()); |
| 3608 |
} |
| 3609 |
on_success()->Emit(compiler, trace); |
| 3610 |
} |
| 3611 |
|
| 3612 |
void TextNode::CalculateOffsets() { |
| 3613 |
int element_count = elements()->length(); |
| 3614 |
// Set up the offsets of the elements relative to the start. This is a fixed |
| 3615 |
// quantity since a TextNode can only contain fixed-width things. |
| 3616 |
int cp_offset = 0; |
| 3617 |
for (int i = 0; i < element_count; i++) { |
| 3618 |
TextElement& elm = elements()->at(i); |
| 3619 |
elm.set_cp_offset(cp_offset); |
| 3620 |
cp_offset += elm.length(); |
| 3621 |
} |
| 3622 |
} |
| 3623 |
|
| 3624 |
namespace { |
| 3625 |
|
| 3626 |
// Assertion propagation moves information about assertions such as |
| 3627 |
// \b to the affected nodes. For instance, in /.\b./ information must |
| 3628 |
// be propagated to the first '.' that whatever follows needs to know |
| 3629 |
// if it matched a word or a non-word, and to the second '.' that it |
| 3630 |
// has to check if it succeeds a word or non-word. In this case the |
| 3631 |
// result will be something like: |
| 3632 |
// |
| 3633 |
// +-------+ +------------+ |
| 3634 |
// | . | | . | |
| 3635 |
// +-------+ ---> +------------+ |
| 3636 |
// | word? | | check word | |
| 3637 |
// +-------+ +------------+ |
| 3638 |
class AssertionPropagator : public AllStatic { |
| 3639 |
public: |
| 3640 |
static void VisitText(TextNode* that) {} |
| 3641 |
|
| 3642 |
static void VisitAction(ActionNode* that) { |
| 3643 |
// If the next node is interested in what it follows then this node |
| 3644 |
// has to be interested too so it can pass the information on. |
| 3645 |
that->info()->AddFromFollowing(that->on_success()->info()); |
| 3646 |
} |
| 3647 |
|
| 3648 |
static void VisitChoice(ChoiceNode* that, int i) { |
| 3649 |
// Anything the following nodes need to know has to be known by |
| 3650 |
// this node also, so it can pass it on. |
| 3651 |
that->info()->AddFromFollowing(that->alternatives()->at(i).node()->info()); |
| 3652 |
} |
| 3653 |
|
| 3654 |
static void VisitLoopChoiceContinueNode(LoopChoiceNode* that) { |
| 3655 |
that->info()->AddFromFollowing(that->continue_node()->info()); |
| 3656 |
} |
| 3657 |
|
| 3658 |
static void VisitLoopChoiceLoopNode(LoopChoiceNode* that) { |
| 3659 |
that->info()->AddFromFollowing(that->loop_node()->info()); |
| 3660 |
} |
| 3661 |
|
| 3662 |
static void VisitNegativeLookaroundChoiceLookaroundNode( |
| 3663 |
NegativeLookaroundChoiceNode* that) { |
| 3664 |
VisitChoice(that, NegativeLookaroundChoiceNode::kLookaroundIndex); |
| 3665 |
} |
| 3666 |
|
| 3667 |
static void VisitNegativeLookaroundChoiceContinueNode( |
| 3668 |
NegativeLookaroundChoiceNode* that) { |
| 3669 |
VisitChoice(that, NegativeLookaroundChoiceNode::kContinueIndex); |
| 3670 |
} |
| 3671 |
|
| 3672 |
static void VisitBackReference(BackReferenceNode* that) {} |
| 3673 |
|
| 3674 |
static void VisitAssertion(AssertionNode* that) {} |
| 3675 |
}; |
| 3676 |
|
| 3677 |
// Propagates information about the minimum size of successful matches from |
| 3678 |
// successor nodes to their predecessors. Note that all eats_at_least values |
| 3679 |
// are initialized to zero before analysis. |
| 3680 |
class EatsAtLeastPropagator : public AllStatic { |
| 3681 |
public: |
| 3682 |
static void VisitText(TextNode* that) { |
| 3683 |
// The eats_at_least value is not used if reading backward. |
| 3684 |
if (!that->read_backward()) { |
| 3685 |
// We are not at the start after this node, and thus we can use the |
| 3686 |
// successor's eats_at_least_from_not_start value. |
| 3687 |
uint8_t eats_at_least = base::saturated_cast<uint8_t>( |
| 3688 |
that->Length() + that->on_success() |
| 3689 |
->eats_at_least_info() |
| 3690 |
->eats_at_least_from_not_start); |
| 3691 |
that->set_eats_at_least_info(EatsAtLeastInfo(eats_at_least)); |
| 3692 |
} |
| 3693 |
} |
| 3694 |
|
| 3695 |
static void VisitAction(ActionNode* that) { |
| 3696 |
switch (that->action_type()) { |
| 3697 |
case ActionNode::BEGIN_POSITIVE_SUBMATCH: { |
| 3698 |
// For a begin positive submatch we propagate the eats_at_least |
| 3699 |
// data from the successor of the success node, ignoring the body of |
| 3700 |
// the lookahead, which eats nothing, since it is a zero-width |
| 3701 |
// assertion. |
| 3702 |
// TODO(chromium:42201836) This is better than discarding all |
| 3703 |
// information when there is a positive lookahead, but it loses some |
| 3704 |
// information that could be useful, since the body of the lookahead |
| 3705 |
// could tell us something about how close to the end of the string we |
| 3706 |
// are. |
| 3707 |
that->set_eats_at_least_info( |
| 3708 |
*that->success_node()->on_success()->eats_at_least_info()); |
| 3709 |
break; |
| 3710 |
} |
| 3711 |
case ActionNode::POSITIVE_SUBMATCH_SUCCESS: |
| 3712 |
// We do not propagate eats_at_least data through positive submatch |
| 3713 |
// success because it rewinds input. |
| 3714 |
DCHECK(that->eats_at_least_info()->IsZero()); |
| 3715 |
break; |
| 3716 |
case ActionNode::SET_REGISTER_FOR_LOOP: |
| 3717 |
// SET_REGISTER_FOR_LOOP indicates a loop entry point, which means the |
| 3718 |
// loop body will run at least the minimum number of times before the |
| 3719 |
// continuation case can run. |
| 3720 |
that->set_eats_at_least_info( |
| 3721 |
that->on_success()->EatsAtLeastFromLoopEntry()); |
| 3722 |
break; |
| 3723 |
case ActionNode::BEGIN_NEGATIVE_SUBMATCH: |
| 3724 |
default: |
| 3725 |
// Otherwise, the current node eats at least as much as its successor. |
| 3726 |
// Note: we can propagate eats_at_least data for BEGIN_NEGATIVE_SUBMATCH |
| 3727 |
// because NegativeLookaroundChoiceNode ignores its lookaround successor |
| 3728 |
// when computing eats-at-least and quick check information. |
| 3729 |
that->set_eats_at_least_info(*that->on_success()->eats_at_least_info()); |
| 3730 |
break; |
| 3731 |
} |
| 3732 |
} |
| 3733 |
|
| 3734 |
static void VisitChoice(ChoiceNode* that, int i) { |
| 3735 |
// The minimum possible match from a choice node is the minimum of its |
| 3736 |
// successors. |
| 3737 |
EatsAtLeastInfo eats_at_least = |
| 3738 |
i == 0 ? EatsAtLeastInfo(UINT8_MAX) : *that->eats_at_least_info(); |
| 3739 |
eats_at_least.SetMin( |
| 3740 |
*that->alternatives()->at(i).node()->eats_at_least_info()); |
| 3741 |
that->set_eats_at_least_info(eats_at_least); |
| 3742 |
} |
| 3743 |
|
| 3744 |
static void VisitLoopChoiceContinueNode(LoopChoiceNode* that) { |
| 3745 |
if (!that->read_backward()) { |
| 3746 |
that->set_eats_at_least_info( |
| 3747 |
*that->continue_node()->eats_at_least_info()); |
| 3748 |
} |
| 3749 |
} |
| 3750 |
|
| 3751 |
static void VisitLoopChoiceLoopNode(LoopChoiceNode* that) {} |
| 3752 |
|
| 3753 |
static void VisitNegativeLookaroundChoiceLookaroundNode( |
| 3754 |
NegativeLookaroundChoiceNode* that) {} |
| 3755 |
|
| 3756 |
static void VisitNegativeLookaroundChoiceContinueNode( |
| 3757 |
NegativeLookaroundChoiceNode* that) { |
| 3758 |
that->set_eats_at_least_info(*that->continue_node()->eats_at_least_info()); |
| 3759 |
} |
| 3760 |
|
| 3761 |
static void VisitBackReference(BackReferenceNode* that) { |
| 3762 |
if (!that->read_backward()) { |
| 3763 |
that->set_eats_at_least_info(*that->on_success()->eats_at_least_info()); |
| 3764 |
} |
| 3765 |
} |
| 3766 |
|
| 3767 |
static void VisitAssertion(AssertionNode* that) { |
| 3768 |
EatsAtLeastInfo eats_at_least = *that->on_success()->eats_at_least_info(); |
| 3769 |
if (that->assertion_type() == AssertionNode::AT_START) { |
| 3770 |
// If we know we are not at the start and we are asked "how many |
| 3771 |
// characters will you match if you succeed?" then we can answer anything |
| 3772 |
// since false implies false. So let's just set the max answer |
| 3773 |
// (UINT8_MAX) since that won't prevent us from preloading a lot of |
| 3774 |
// characters for the other branches in the node graph. |
| 3775 |
eats_at_least.eats_at_least_from_not_start = UINT8_MAX; |
| 3776 |
} |
| 3777 |
that->set_eats_at_least_info(eats_at_least); |
| 3778 |
} |
| 3779 |
}; |
| 3780 |
|
| 3781 |
} // namespace |
| 3782 |
|
| 3783 |
// ------------------------------------------------------------------- |
| 3784 |
// Analysis |
| 3785 |
|
| 3786 |
// Iterates the node graph and provides the opportunity for propagators to set |
| 3787 |
// values that depend on successor nodes. |
| 3788 |
template <typename... Propagators> |
| 3789 |
class Analysis : public NodeVisitor { |
| 3790 |
public: |
| 3791 |
Analysis(Isolate* isolate, bool is_one_byte, RegExpFlags flags) |
| 3792 |
: isolate_(isolate), |
| 3793 |
is_one_byte_(is_one_byte), |
| 3794 |
flags_(flags), |
| 3795 |
error_(RegExpError::kNone) {} |
| 3796 |
|
| 3797 |
void EnsureAnalyzed(RegExpNode* that) { |
| 3798 |
StackLimitCheck check(isolate()); |
| 3799 |
if (check.HasOverflowed()) { |
| 3800 |
if (v8_flags.correctness_fuzzer_suppressions) { |
| 3801 |
FATAL("Analysis: Aborting on stack overflow"); |
| 3802 |
} |
| 3803 |
fail(RegExpError::kAnalysisStackOverflow); |
| 3804 |
return; |
| 3805 |
} |
| 3806 |
if (that->info()->been_analyzed || that->info()->being_analyzed) return; |
| 3807 |
that->info()->being_analyzed = true; |
| 3808 |
that->Accept(this); |
| 3809 |
that->info()->being_analyzed = false; |
| 3810 |
that->info()->been_analyzed = true; |
| 3811 |
} |
| 3812 |
|
| 3813 |
bool has_failed() { return error_ != RegExpError::kNone; } |
| 3814 |
RegExpError error() { |
| 3815 |
DCHECK(error_ != RegExpError::kNone); |
| 3816 |
return error_; |
| 3817 |
} |
| 3818 |
void fail(RegExpError error) { error_ = error; } |
| 3819 |
|
| 3820 |
Isolate* isolate() const { return isolate_; } |
| 3821 |
|
| 3822 |
void VisitEnd(EndNode* that) override { |
| 3823 |
// nothing to do |
| 3824 |
} |
| 3825 |
|
| 3826 |
// Used to call the given static function on each propagator / variadic template |
| 3827 |
// argument. |
| 3828 |
#define STATIC_FOR_EACH(expr) \ |
| 3829 |
do { \ |
| 3830 |
int dummy[] = {((expr), 0)...}; \ |
| 3831 |
USE(dummy); \ |
| 3832 |
} while (false) |
| 3833 |
|
| 3834 |
void VisitText(TextNode* that) override { |
| 3835 |
that->MakeCaseIndependent(isolate(), is_one_byte_, flags()); |
| 3836 |
EnsureAnalyzed(that->on_success()); |
| 3837 |
if (has_failed()) return; |
| 3838 |
that->CalculateOffsets(); |
| 3839 |
STATIC_FOR_EACH(Propagators::VisitText(that)); |
| 3840 |
} |
| 3841 |
|
| 3842 |
void VisitAction(ActionNode* that) override { |
| 3843 |
if (that->action_type() == ActionNode::MODIFY_FLAGS) { |
| 3844 |
set_flags(that->flags()); |
| 3845 |
} |
| 3846 |
EnsureAnalyzed(that->on_success()); |
| 3847 |
if (has_failed()) return; |
| 3848 |
STATIC_FOR_EACH(Propagators::VisitAction(that)); |
| 3849 |
} |
| 3850 |
|
| 3851 |
void VisitChoice(ChoiceNode* that) override { |
| 3852 |
for (int i = 0; i < that->alternatives()->length(); i++) { |
| 3853 |
EnsureAnalyzed(that->alternatives()->at(i).node()); |
| 3854 |
if (has_failed()) return; |
| 3855 |
STATIC_FOR_EACH(Propagators::VisitChoice(that, i)); |
| 3856 |
} |
| 3857 |
} |
| 3858 |
|
| 3859 |
void VisitLoopChoice(LoopChoiceNode* that) override { |
| 3860 |
DCHECK_EQ(that->alternatives()->length(), 2); // Just loop and continue. |
| 3861 |
|
| 3862 |
// First propagate all information from the continuation node. |
| 3863 |
// Due to the unusual visitation order, we need to manage the flags manually |
| 3864 |
// as if we were visiting the loop node before visiting the continuation. |
| 3865 |
RegExpFlags orig_flags = flags(); |
| 3866 |
|
| 3867 |
EnsureAnalyzed(that->continue_node()); |
| 3868 |
if (has_failed()) return; |
| 3869 |
// Propagators don't access global state (including flags), so we don't need |
| 3870 |
// to reset them here. |
| 3871 |
STATIC_FOR_EACH(Propagators::VisitLoopChoiceContinueNode(that)); |
| 3872 |
|
| 3873 |
RegExpFlags continuation_flags = flags(); |
| 3874 |
|
| 3875 |
// Check the loop last since it may need the value of this node |
| 3876 |
// to get a correct result. |
| 3877 |
set_flags(orig_flags); |
| 3878 |
EnsureAnalyzed(that->loop_node()); |
| 3879 |
if (has_failed()) return; |
| 3880 |
// Propagators don't access global state (including flags), so we don't need |
| 3881 |
// to reset them here. |
| 3882 |
STATIC_FOR_EACH(Propagators::VisitLoopChoiceLoopNode(that)); |
| 3883 |
|
| 3884 |
set_flags(continuation_flags); |
| 3885 |
} |
| 3886 |
|
| 3887 |
void VisitNegativeLookaroundChoice( |
| 3888 |
NegativeLookaroundChoiceNode* that) override { |
| 3889 |
DCHECK_EQ(that->alternatives()->length(), 2); // Lookaround and continue. |
| 3890 |
|
| 3891 |
EnsureAnalyzed(that->lookaround_node()); |
| 3892 |
if (has_failed()) return; |
| 3893 |
STATIC_FOR_EACH( |
| 3894 |
Propagators::VisitNegativeLookaroundChoiceLookaroundNode(that)); |
| 3895 |
|
| 3896 |
EnsureAnalyzed(that->continue_node()); |
| 3897 |
if (has_failed()) return; |
| 3898 |
STATIC_FOR_EACH( |
| 3899 |
Propagators::VisitNegativeLookaroundChoiceContinueNode(that)); |
| 3900 |
} |
| 3901 |
|
| 3902 |
void VisitBackReference(BackReferenceNode* that) override { |
| 3903 |
EnsureAnalyzed(that->on_success()); |
| 3904 |
if (has_failed()) return; |
| 3905 |
STATIC_FOR_EACH(Propagators::VisitBackReference(that)); |
| 3906 |
} |
| 3907 |
|
| 3908 |
void VisitAssertion(AssertionNode* that) override { |
| 3909 |
EnsureAnalyzed(that->on_success()); |
| 3910 |
if (has_failed()) return; |
| 3911 |
STATIC_FOR_EACH(Propagators::VisitAssertion(that)); |
| 3912 |
} |
| 3913 |
|
| 3914 |
#undef STATIC_FOR_EACH |
| 3915 |
|
| 3916 |
private: |
| 3917 |
RegExpFlags flags() const { return flags_; } |
| 3918 |
void set_flags(RegExpFlags flags) { flags_ = flags; } |
| 3919 |
|
| 3920 |
Isolate* isolate_; |
| 3921 |
const bool is_one_byte_; |
| 3922 |
RegExpFlags flags_; |
| 3923 |
RegExpError error_; |
| 3924 |
|
| 3925 |
DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis); |
| 3926 |
}; |
| 3927 |
|
| 3928 |
RegExpError AnalyzeRegExp(Isolate* isolate, bool is_one_byte, RegExpFlags flags, |
| 3929 |
RegExpNode* node) { |
| 3930 |
Analysis<AssertionPropagator, EatsAtLeastPropagator> analysis( |
| 3931 |
isolate, is_one_byte, flags); |
| 3932 |
DCHECK_EQ(node->info()->been_analyzed, false); |
| 3933 |
analysis.EnsureAnalyzed(node); |
| 3934 |
DCHECK_IMPLIES(analysis.has_failed(), analysis.error() != RegExpError::kNone); |
| 3935 |
return analysis.has_failed() ? analysis.error() : RegExpError::kNone; |
| 3936 |
} |
| 3937 |
|
| 3938 |
void BackReferenceNode::FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 3939 |
BoyerMooreLookahead* bm, |
| 3940 |
bool not_at_start) { |
| 3941 |
// Working out the set of characters that a backreference can match is too |
| 3942 |
// hard, so we just say that any character can match. |
| 3943 |
bm->SetRest(offset); |
| 3944 |
SaveBMInfo(bm, not_at_start, offset); |
| 3945 |
} |
| 3946 |
|
| 3947 |
static_assert(BoyerMoorePositionInfo::kMapSize == |
| 3948 |
RegExpMacroAssembler::kTableSize); |
| 3949 |
|
| 3950 |
void ChoiceNode::FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 3951 |
BoyerMooreLookahead* bm, bool not_at_start) { |
| 3952 |
ZoneList<GuardedAlternative>* alts = alternatives(); |
| 3953 |
budget = (budget - 1) / alts->length(); |
| 3954 |
for (int i = 0; i < alts->length(); i++) { |
| 3955 |
GuardedAlternative& alt = alts->at(i); |
| 3956 |
if (alt.guards() != nullptr && alt.guards()->length() != 0) { |
| 3957 |
bm->SetRest(offset); // Give up trying to fill in info. |
| 3958 |
SaveBMInfo(bm, not_at_start, offset); |
| 3959 |
return; |
| 3960 |
} |
| 3961 |
alt.node()->FillInBMInfo(isolate, offset, budget, bm, not_at_start); |
| 3962 |
} |
| 3963 |
SaveBMInfo(bm, not_at_start, offset); |
| 3964 |
} |
| 3965 |
|
| 3966 |
void TextNode::FillInBMInfo(Isolate* isolate, int initial_offset, int budget, |
| 3967 |
BoyerMooreLookahead* bm, bool not_at_start) { |
| 3968 |
if (initial_offset >= bm->length()) return; |
| 3969 |
if (read_backward()) return; |
| 3970 |
int offset = initial_offset; |
| 3971 |
int max_char = bm->max_char(); |
| 3972 |
for (int i = 0; i < elements()->length(); i++) { |
| 3973 |
if (offset >= bm->length()) { |
| 3974 |
if (initial_offset == 0) set_bm_info(not_at_start, bm); |
| 3975 |
return; |
| 3976 |
} |
| 3977 |
TextElement text = elements()->at(i); |
| 3978 |
if (text.text_type() == TextElement::ATOM) { |
| 3979 |
RegExpAtom* atom = text.atom(); |
| 3980 |
for (int j = 0; j < atom->length(); j++, offset++) { |
| 3981 |
if (offset >= bm->length()) { |
| 3982 |
if (initial_offset == 0) set_bm_info(not_at_start, bm); |
| 3983 |
return; |
| 3984 |
} |
| 3985 |
base::uc16 character = atom->data()[j]; |
| 3986 |
if (IsIgnoreCase(bm->compiler()->flags())) { |
| 3987 |
unibrow::uchar chars[4]; |
| 3988 |
int length = GetCaseIndependentLetters(isolate, character, |
| 3989 |
bm->compiler(), chars, 4); |
| 3990 |
for (int k = 0; k < length; k++) { |
| 3991 |
bm->Set(offset, chars[k]); |
| 3992 |
} |
| 3993 |
} else { |
| 3994 |
if (character <= max_char) bm->Set(offset, character); |
| 3995 |
} |
| 3996 |
} |
| 3997 |
} else { |
| 3998 |
DCHECK_EQ(TextElement::CLASS_RANGES, text.text_type()); |
| 3999 |
RegExpClassRanges* class_ranges = text.class_ranges(); |
| 4000 |
ZoneList<CharacterRange>* ranges = class_ranges->ranges(zone()); |
| 4001 |
if (class_ranges->is_negated()) { |
| 4002 |
bm->SetAll(offset); |
| 4003 |
} else { |
| 4004 |
for (int k = 0; k < ranges->length(); k++) { |
| 4005 |
CharacterRange& range = ranges->at(k); |
| 4006 |
if (static_cast<int>(range.from()) > max_char) continue; |
| 4007 |
int to = std::min(max_char, static_cast<int>(range.to())); |
| 4008 |
bm->SetInterval(offset, Interval(range.from(), to)); |
| 4009 |
} |
| 4010 |
} |
| 4011 |
offset++; |
| 4012 |
} |
| 4013 |
} |
| 4014 |
if (offset >= bm->length()) { |
| 4015 |
if (initial_offset == 0) set_bm_info(not_at_start, bm); |
| 4016 |
return; |
| 4017 |
} |
| 4018 |
on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, |
| 4019 |
true); // Not at start after a text node. |
| 4020 |
if (initial_offset == 0) set_bm_info(not_at_start, bm); |
| 4021 |
} |
| 4022 |
|
| 4023 |
RegExpNode* RegExpCompiler::OptionallyStepBackToLeadSurrogate( |
| 4024 |
RegExpNode* on_success) { |
| 4025 |
DCHECK(!read_backward()); |
| 4026 |
ZoneList<CharacterRange>* lead_surrogates = CharacterRange::List( |
| 4027 |
zone(), CharacterRange::Range(kLeadSurrogateStart, kLeadSurrogateEnd)); |
| 4028 |
ZoneList<CharacterRange>* trail_surrogates = CharacterRange::List( |
| 4029 |
zone(), CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd)); |
| 4030 |
|
| 4031 |
ChoiceNode* optional_step_back = zone()->New<ChoiceNode>(2, zone()); |
| 4032 |
|
| 4033 |
int stack_register = UnicodeLookaroundStackRegister(); |
| 4034 |
int position_register = UnicodeLookaroundPositionRegister(); |
| 4035 |
RegExpNode* step_back = TextNode::CreateForCharacterRanges( |
| 4036 |
zone(), lead_surrogates, true, on_success); |
| 4037 |
RegExpLookaround::Builder builder(true, step_back, stack_register, |
| 4038 |
position_register); |
| 4039 |
RegExpNode* match_trail = TextNode::CreateForCharacterRanges( |
| 4040 |
zone(), trail_surrogates, false, builder.on_match_success()); |
| 4041 |
|
| 4042 |
optional_step_back->AddAlternative( |
| 4043 |
GuardedAlternative(builder.ForMatch(match_trail))); |
| 4044 |
optional_step_back->AddAlternative(GuardedAlternative(on_success)); |
| 4045 |
|
| 4046 |
return optional_step_back; |
| 4047 |
} |
| 4048 |
|
| 4049 |
RegExpNode* RegExpCompiler::PreprocessRegExp(RegExpCompileData* data, |
| 4050 |
bool is_one_byte) { |
| 4051 |
// Wrap the body of the regexp in capture #0. |
| 4052 |
RegExpNode* captured_body = |
| 4053 |
RegExpCapture::ToNode(data->tree, 0, this, accept()); |
| 4054 |
RegExpNode* node = captured_body; |
| 4055 |
if (!data->tree->IsAnchoredAtStart() && !IsSticky(flags())) { |
| 4056 |
// Add a .*? at the beginning, outside the body capture, unless |
| 4057 |
// this expression is anchored at the beginning or sticky. |
| 4058 |
RegExpNode* loop_node = RegExpQuantifier::ToNode( |
| 4059 |
0, RegExpTree::kInfinity, false, |
| 4060 |
zone()->New<RegExpClassRanges>(StandardCharacterSet::kEverything), this, |
| 4061 |
captured_body, data->contains_anchor); |
| 4062 |
|
| 4063 |
if (data->contains_anchor) { |
| 4064 |
// Unroll loop once, to take care of the case that might start |
| 4065 |
// at the start of input. |
| 4066 |
ChoiceNode* first_step_node = zone()->New<ChoiceNode>(2, zone()); |
| 4067 |
first_step_node->AddAlternative(GuardedAlternative(captured_body)); |
| 4068 |
first_step_node->AddAlternative(GuardedAlternative(zone()->New<TextNode>( |
| 4069 |
zone()->New<RegExpClassRanges>(StandardCharacterSet::kEverything), |
| 4070 |
false, loop_node))); |
| 4071 |
node = first_step_node; |
| 4072 |
} else { |
| 4073 |
node = loop_node; |
| 4074 |
} |
| 4075 |
} |
| 4076 |
if (is_one_byte) { |
| 4077 |
node = node->FilterOneByte(RegExpCompiler::kMaxRecursion, this); |
| 4078 |
// Do it again to propagate the new nodes to places where they were not |
| 4079 |
// put because they had not been calculated yet. |
| 4080 |
if (node != nullptr) { |
| 4081 |
node = node->FilterOneByte(RegExpCompiler::kMaxRecursion, this); |
| 4082 |
} |
| 4083 |
} else if (IsEitherUnicode(flags()) && |
| 4084 |
(IsGlobal(flags()) || IsSticky(flags()))) { |
| 4085 |
node = OptionallyStepBackToLeadSurrogate(node); |
| 4086 |
} |
| 4087 |
|
| 4088 |
if (node == nullptr) node = zone()->New<EndNode>(EndNode::BACKTRACK, zone()); |
| 4089 |
// We can run out of registers during preprocessing. Indicate an error in case |
| 4090 |
// we do. |
| 4091 |
if (reg_exp_too_big_) { |
| 4092 |
data->error = RegExpError::kTooLarge; |
| 4093 |
} |
| 4094 |
return node; |
| 4095 |
} |
| 4096 |
|
| 4097 |
void RegExpCompiler::ToNodeCheckForStackOverflow() { |
| 4098 |
if (StackLimitCheck{isolate()}.HasOverflowed()) { |
| 4099 |
V8::FatalProcessOutOfMemory(isolate(), "RegExpCompiler"); |
| 4100 |
} |
| 4101 |
} |
| 4102 |
|
| 4103 |
} // namespace v8::internal |