| 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 |
#ifndef V8_REGEXP_REGEXP_NODES_H_ |
| 6 |
#define V8_REGEXP_REGEXP_NODES_H_ |
| 7 |
|
| 8 |
#include "irregexp/imported/regexp-macro-assembler.h" |
| 9 |
|
| 10 |
namespace v8 { |
| 11 |
namespace internal { |
| 12 |
|
| 13 |
class AlternativeGenerationList; |
| 14 |
class BoyerMooreLookahead; |
| 15 |
class GreedyLoopState; |
| 16 |
class NodeVisitor; |
| 17 |
class QuickCheckDetails; |
| 18 |
class RegExpCompiler; |
| 19 |
class SeqRegExpNode; |
| 20 |
class Trace; |
| 21 |
struct PreloadState; |
| 22 |
|
| 23 |
#define FOR_EACH_NODE_TYPE(VISIT) \ |
| 24 |
VISIT(End) \ |
| 25 |
VISIT(Action) \ |
| 26 |
VISIT(Choice) \ |
| 27 |
VISIT(LoopChoice) \ |
| 28 |
VISIT(NegativeLookaroundChoice) \ |
| 29 |
VISIT(BackReference) \ |
| 30 |
VISIT(Assertion) \ |
| 31 |
VISIT(Text) |
| 32 |
|
| 33 |
#define FORWARD_DECLARE(type) class type##Node; |
| 34 |
FOR_EACH_NODE_TYPE(FORWARD_DECLARE) |
| 35 |
#undef FORWARD_DECLARE |
| 36 |
|
| 37 |
struct NodeInfo final { |
| 38 |
NodeInfo() |
| 39 |
: being_analyzed(false), |
| 40 |
been_analyzed(false), |
| 41 |
follows_word_interest(false), |
| 42 |
follows_newline_interest(false), |
| 43 |
follows_start_interest(false), |
| 44 |
at_end(false), |
| 45 |
visited(false), |
| 46 |
replacement_calculated(false) {} |
| 47 |
|
| 48 |
// Returns true if the interests and assumptions of this node |
| 49 |
// matches the given one. |
| 50 |
bool Matches(NodeInfo* that) { |
| 51 |
return (at_end == that->at_end) && |
| 52 |
(follows_word_interest == that->follows_word_interest) && |
| 53 |
(follows_newline_interest == that->follows_newline_interest) && |
| 54 |
(follows_start_interest == that->follows_start_interest); |
| 55 |
} |
| 56 |
|
| 57 |
// Updates the interests of this node given the interests of the |
| 58 |
// node preceding it. |
| 59 |
void AddFromPreceding(NodeInfo* that) { |
| 60 |
at_end |= that->at_end; |
| 61 |
follows_word_interest |= that->follows_word_interest; |
| 62 |
follows_newline_interest |= that->follows_newline_interest; |
| 63 |
follows_start_interest |= that->follows_start_interest; |
| 64 |
} |
| 65 |
|
| 66 |
bool HasLookbehind() { |
| 67 |
return follows_word_interest || follows_newline_interest || |
| 68 |
follows_start_interest; |
| 69 |
} |
| 70 |
|
| 71 |
// Sets the interests of this node to include the interests of the |
| 72 |
// following node. |
| 73 |
void AddFromFollowing(NodeInfo* that) { |
| 74 |
follows_word_interest |= that->follows_word_interest; |
| 75 |
follows_newline_interest |= that->follows_newline_interest; |
| 76 |
follows_start_interest |= that->follows_start_interest; |
| 77 |
} |
| 78 |
|
| 79 |
void ResetCompilationState() { |
| 80 |
being_analyzed = false; |
| 81 |
been_analyzed = false; |
| 82 |
} |
| 83 |
|
| 84 |
bool being_analyzed : 1; |
| 85 |
bool been_analyzed : 1; |
| 86 |
|
| 87 |
// These bits are set of this node has to know what the preceding |
| 88 |
// character was. |
| 89 |
bool follows_word_interest : 1; |
| 90 |
bool follows_newline_interest : 1; |
| 91 |
bool follows_start_interest : 1; |
| 92 |
|
| 93 |
bool at_end : 1; |
| 94 |
bool visited : 1; |
| 95 |
bool replacement_calculated : 1; |
| 96 |
}; |
| 97 |
|
| 98 |
struct EatsAtLeastInfo final { |
| 99 |
EatsAtLeastInfo() : EatsAtLeastInfo(0) {} |
| 100 |
explicit EatsAtLeastInfo(uint8_t eats) |
| 101 |
: eats_at_least_from_possibly_start(eats), |
| 102 |
eats_at_least_from_not_start(eats) {} |
| 103 |
void SetMin(const EatsAtLeastInfo& other) { |
| 104 |
if (other.eats_at_least_from_possibly_start < |
| 105 |
eats_at_least_from_possibly_start) { |
| 106 |
eats_at_least_from_possibly_start = |
| 107 |
other.eats_at_least_from_possibly_start; |
| 108 |
} |
| 109 |
if (other.eats_at_least_from_not_start < eats_at_least_from_not_start) { |
| 110 |
eats_at_least_from_not_start = other.eats_at_least_from_not_start; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
bool IsZero() const { |
| 115 |
return eats_at_least_from_possibly_start == 0 && |
| 116 |
eats_at_least_from_not_start == 0; |
| 117 |
} |
| 118 |
|
| 119 |
// Any successful match starting from the current node will consume at least |
| 120 |
// this many characters. This does not necessarily mean that there is a |
| 121 |
// possible match with exactly this many characters, but we generally try to |
| 122 |
// get this number as high as possible to allow for early exit on failure. |
| 123 |
uint8_t eats_at_least_from_possibly_start; |
| 124 |
|
| 125 |
// Like eats_at_least_from_possibly_start, but with the additional assumption |
| 126 |
// that start-of-string assertions (^) can't match. This value is greater than |
| 127 |
// or equal to eats_at_least_from_possibly_start. |
| 128 |
uint8_t eats_at_least_from_not_start; |
| 129 |
}; |
| 130 |
|
| 131 |
class RegExpNode : public ZoneObject { |
| 132 |
public: |
| 133 |
explicit RegExpNode(Zone* zone) |
| 134 |
: replacement_(nullptr), |
| 135 |
on_work_list_(false), |
| 136 |
trace_count_(0), |
| 137 |
zone_(zone) { |
| 138 |
bm_info_[0] = bm_info_[1] = nullptr; |
| 139 |
} |
| 140 |
virtual ~RegExpNode(); |
| 141 |
virtual void Accept(NodeVisitor* visitor) = 0; |
| 142 |
// Generates a goto to this node or actually generates the code at this point. |
| 143 |
virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0; |
| 144 |
// How many characters must this node consume at a minimum in order to |
| 145 |
// succeed. The not_at_start argument is used to indicate that we know we are |
| 146 |
// not at the start of the input. In this case anchored branches will always |
| 147 |
// fail and can be ignored when determining how many characters are consumed |
| 148 |
// on success. If this node has not been analyzed yet, EatsAtLeast returns 0. |
| 149 |
uint32_t EatsAtLeast(bool not_at_start); |
| 150 |
// Returns how many characters this node must consume in order to succeed, |
| 151 |
// given that this is a LoopChoiceNode whose counter register is in a |
| 152 |
// newly-initialized state at the current position in the generated code. For |
| 153 |
// example, consider /a{6,8}/. Absent any extra information, the |
| 154 |
// LoopChoiceNode for the repetition must report that it consumes at least |
| 155 |
// zero characters, because it may have already looped several times. However, |
| 156 |
// with a newly-initialized counter, it can report that it consumes at least |
| 157 |
// six characters. |
| 158 |
virtual EatsAtLeastInfo EatsAtLeastFromLoopEntry(); |
| 159 |
// Emits some quick code that checks whether the preloaded characters match. |
| 160 |
// Falls through on certain failure, jumps to the label on possible success. |
| 161 |
// If the node cannot make a quick check it does nothing and returns false. |
| 162 |
bool EmitQuickCheck(RegExpCompiler* compiler, Trace* bounds_check_trace, |
| 163 |
Trace* trace, bool preload_has_checked_bounds, |
| 164 |
Label* on_possible_success, |
| 165 |
QuickCheckDetails* details_return, |
| 166 |
bool fall_through_on_failure, ChoiceNode* predecessor); |
| 167 |
// For a given number of characters this returns a mask and a value. The |
| 168 |
// next n characters are anded with the mask and compared with the value. |
| 169 |
// A comparison failure indicates the node cannot match the next n characters. |
| 170 |
// A comparison success indicates the node may match. |
| 171 |
virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 172 |
RegExpCompiler* compiler, |
| 173 |
int characters_filled_in, |
| 174 |
bool not_at_start) = 0; |
| 175 |
// Fills in quick check details for this node, given that this is a |
| 176 |
// LoopChoiceNode whose counter register is in a newly-initialized state at |
| 177 |
// the current position in the generated code. For example, consider /a{6,8}/. |
| 178 |
// Absent any extra information, the LoopChoiceNode for the repetition cannot |
| 179 |
// generate any useful quick check because a match might be the (empty) |
| 180 |
// continuation node. However, with a newly-initialized counter, it can |
| 181 |
// generate a quick check for several 'a' characters at once. |
| 182 |
virtual void GetQuickCheckDetailsFromLoopEntry(QuickCheckDetails* details, |
| 183 |
RegExpCompiler* compiler, |
| 184 |
int characters_filled_in, |
| 185 |
bool not_at_start); |
| 186 |
static const int kNodeIsTooComplexForGreedyLoops = kMinInt; |
| 187 |
virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; } |
| 188 |
// Only returns the successor for a text node of length 1 that matches any |
| 189 |
// character and that has no guards on it. |
| 190 |
virtual RegExpNode* GetSuccessorOfOmnivorousTextNode( |
| 191 |
RegExpCompiler* compiler) { |
| 192 |
return nullptr; |
| 193 |
} |
| 194 |
|
| 195 |
// Collects information on the possible code units (mod 128) that can match if |
| 196 |
// we look forward. This is used for a Boyer-Moore-like string searching |
| 197 |
// implementation. TODO(erikcorry): This should share more code with |
| 198 |
// EatsAtLeast, GetQuickCheckDetails. The budget argument is used to limit |
| 199 |
// the number of nodes we are willing to look at in order to create this data. |
| 200 |
static const int kRecursionBudget = 200; |
| 201 |
bool KeepRecursing(RegExpCompiler* compiler); |
| 202 |
virtual void FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 203 |
BoyerMooreLookahead* bm, bool not_at_start) { |
| 204 |
UNREACHABLE(); |
| 205 |
} |
| 206 |
|
| 207 |
// If we know that the input is one-byte then there are some nodes that can |
| 208 |
// never match. This method returns a node that can be substituted for |
| 209 |
// itself, or nullptr if the node can never match. |
| 210 |
virtual RegExpNode* FilterOneByte(int depth, RegExpCompiler* compiler) { |
| 211 |
return this; |
| 212 |
} |
| 213 |
// Helper for FilterOneByte. |
| 214 |
RegExpNode* replacement() { |
| 215 |
DCHECK(info()->replacement_calculated); |
| 216 |
return replacement_; |
| 217 |
} |
| 218 |
RegExpNode* set_replacement(RegExpNode* replacement) { |
| 219 |
info()->replacement_calculated = true; |
| 220 |
replacement_ = replacement; |
| 221 |
return replacement; // For convenience. |
| 222 |
} |
| 223 |
|
| 224 |
// We want to avoid recalculating the lookahead info, so we store it on the |
| 225 |
// node. Only info that is for this node is stored. We can tell that the |
| 226 |
// info is for this node when offset == 0, so the information is calculated |
| 227 |
// relative to this node. |
| 228 |
void SaveBMInfo(BoyerMooreLookahead* bm, bool not_at_start, int offset) { |
| 229 |
if (offset == 0) set_bm_info(not_at_start, bm); |
| 230 |
} |
| 231 |
|
| 232 |
Label* label() { return &label_; } |
| 233 |
// If non-generic code is generated for a node (i.e. the node is not at the |
| 234 |
// start of the trace) then it cannot be reused. This variable sets a limit |
| 235 |
// on how often we allow that to happen before we insist on starting a new |
| 236 |
// trace and generating generic code for a node that can be reused by flushing |
| 237 |
// the deferred actions in the current trace and generating a goto. |
| 238 |
static const int kMaxCopiesCodeGenerated = 10; |
| 239 |
|
| 240 |
bool on_work_list() { return on_work_list_; } |
| 241 |
void set_on_work_list(bool value) { on_work_list_ = value; } |
| 242 |
|
| 243 |
NodeInfo* info() { return &info_; } |
| 244 |
const EatsAtLeastInfo* eats_at_least_info() const { return &eats_at_least_; } |
| 245 |
void set_eats_at_least_info(const EatsAtLeastInfo& eats_at_least) { |
| 246 |
eats_at_least_ = eats_at_least; |
| 247 |
} |
| 248 |
|
| 249 |
// TODO(v8:10441): This is a hacky way to avoid exponential code size growth |
| 250 |
// for very large choice nodes that can be generated by unicode property |
| 251 |
// escapes. In order to avoid inlining (i.e. trace recursion), we pretend to |
| 252 |
// have generated the maximum count of code copies already. |
| 253 |
// We should instead fix this properly, e.g. by using the code size budget |
| 254 |
// (flush_budget) or by generating property escape matches as calls to a C |
| 255 |
// function. |
| 256 |
void SetDoNotInline() { trace_count_ = kMaxCopiesCodeGenerated; } |
| 257 |
|
| 258 |
BoyerMooreLookahead* bm_info(bool not_at_start) { |
| 259 |
return bm_info_[not_at_start ? 1 : 0]; |
| 260 |
} |
| 261 |
|
| 262 |
#define DECLARE_CAST(type) \ |
| 263 |
virtual type##Node* As##type##Node() { return nullptr; } |
| 264 |
FOR_EACH_NODE_TYPE(DECLARE_CAST) |
| 265 |
#undef DECLARE_CAST |
| 266 |
|
| 267 |
virtual SeqRegExpNode* AsSeqRegExpNode() { return nullptr; } |
| 268 |
|
| 269 |
Zone* zone() const { return zone_; } |
| 270 |
|
| 271 |
protected: |
| 272 |
enum LimitResult { DONE, CONTINUE }; |
| 273 |
RegExpNode* replacement_; |
| 274 |
|
| 275 |
LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace); |
| 276 |
|
| 277 |
void set_bm_info(bool not_at_start, BoyerMooreLookahead* bm) { |
| 278 |
bm_info_[not_at_start ? 1 : 0] = bm; |
| 279 |
} |
| 280 |
|
| 281 |
private: |
| 282 |
static const int kFirstCharBudget = 10; |
| 283 |
Label label_; |
| 284 |
bool on_work_list_; |
| 285 |
NodeInfo info_; |
| 286 |
|
| 287 |
// Saved values for EatsAtLeast results, to avoid recomputation. Filled in |
| 288 |
// during analysis (valid if info_.been_analyzed is true). |
| 289 |
EatsAtLeastInfo eats_at_least_; |
| 290 |
|
| 291 |
// This variable keeps track of how many times code has been generated for |
| 292 |
// this node (in different traces). We don't keep track of where the |
| 293 |
// generated code is located unless the code is generated at the start of |
| 294 |
// a trace, in which case it is generic and can be reused by flushing the |
| 295 |
// deferred operations in the current trace and generating a goto. |
| 296 |
int trace_count_; |
| 297 |
BoyerMooreLookahead* bm_info_[2]; |
| 298 |
|
| 299 |
Zone* zone_; |
| 300 |
}; |
| 301 |
|
| 302 |
class SeqRegExpNode : public RegExpNode { |
| 303 |
public: |
| 304 |
explicit SeqRegExpNode(RegExpNode* on_success) |
| 305 |
: RegExpNode(on_success->zone()), on_success_(on_success) {} |
| 306 |
RegExpNode* on_success() { return on_success_; } |
| 307 |
void set_on_success(RegExpNode* node) { on_success_ = node; } |
| 308 |
RegExpNode* FilterOneByte(int depth, RegExpCompiler* compiler) override; |
| 309 |
void FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 310 |
BoyerMooreLookahead* bm, bool not_at_start) override { |
| 311 |
on_success_->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); |
| 312 |
if (offset == 0) set_bm_info(not_at_start, bm); |
| 313 |
} |
| 314 |
SeqRegExpNode* AsSeqRegExpNode() override { return this; } |
| 315 |
|
| 316 |
protected: |
| 317 |
RegExpNode* FilterSuccessor(int depth, RegExpCompiler* compiler); |
| 318 |
|
| 319 |
private: |
| 320 |
RegExpNode* on_success_; |
| 321 |
}; |
| 322 |
|
| 323 |
class ActionNode : public SeqRegExpNode { |
| 324 |
public: |
| 325 |
enum ActionType { |
| 326 |
SET_REGISTER_FOR_LOOP, |
| 327 |
INCREMENT_REGISTER, |
| 328 |
STORE_POSITION, |
| 329 |
BEGIN_POSITIVE_SUBMATCH, |
| 330 |
BEGIN_NEGATIVE_SUBMATCH, |
| 331 |
POSITIVE_SUBMATCH_SUCCESS, |
| 332 |
EMPTY_MATCH_CHECK, |
| 333 |
CLEAR_CAPTURES, |
| 334 |
MODIFY_FLAGS |
| 335 |
}; |
| 336 |
static ActionNode* SetRegisterForLoop(int reg, int val, |
| 337 |
RegExpNode* on_success); |
| 338 |
static ActionNode* IncrementRegister(int reg, RegExpNode* on_success); |
| 339 |
static ActionNode* StorePosition(int reg, bool is_capture, |
| 340 |
RegExpNode* on_success); |
| 341 |
static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success); |
| 342 |
static ActionNode* BeginPositiveSubmatch(int stack_pointer_reg, |
| 343 |
int position_reg, RegExpNode* body, |
| 344 |
ActionNode* success_node); |
| 345 |
static ActionNode* BeginNegativeSubmatch(int stack_pointer_reg, |
| 346 |
int position_reg, |
| 347 |
RegExpNode* on_success); |
| 348 |
static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg, |
| 349 |
int restore_reg, |
| 350 |
int clear_capture_count, |
| 351 |
int clear_capture_from, |
| 352 |
RegExpNode* on_success); |
| 353 |
static ActionNode* EmptyMatchCheck(int start_register, |
| 354 |
int repetition_register, |
| 355 |
int repetition_limit, |
| 356 |
RegExpNode* on_success); |
| 357 |
static ActionNode* ModifyFlags(RegExpFlags flags, RegExpNode* on_success); |
| 358 |
ActionNode* AsActionNode() override { return this; } |
| 359 |
void Accept(NodeVisitor* visitor) override; |
| 360 |
void Emit(RegExpCompiler* compiler, Trace* trace) override; |
| 361 |
void GetQuickCheckDetails(QuickCheckDetails* details, |
| 362 |
RegExpCompiler* compiler, int filled_in, |
| 363 |
bool not_at_start) override; |
| 364 |
void FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 365 |
BoyerMooreLookahead* bm, bool not_at_start) override; |
| 366 |
ActionType action_type() const { return action_type_; } |
| 367 |
// TODO(erikcorry): We should allow some action nodes in greedy loops. |
| 368 |
int GreedyLoopTextLength() override { |
| 369 |
return kNodeIsTooComplexForGreedyLoops; |
| 370 |
} |
| 371 |
RegExpFlags flags() const { |
| 372 |
DCHECK_EQ(action_type(), MODIFY_FLAGS); |
| 373 |
return RegExpFlags{data_.u_modify_flags.flags}; |
| 374 |
} |
| 375 |
ActionNode* success_node() const { |
| 376 |
DCHECK_EQ(action_type(), BEGIN_POSITIVE_SUBMATCH); |
| 377 |
return data_.u_submatch.success_node; |
| 378 |
} |
| 379 |
|
| 380 |
protected: |
| 381 |
ActionNode(ActionType action_type, RegExpNode* on_success) |
| 382 |
: SeqRegExpNode(on_success), action_type_(action_type) {} |
| 383 |
|
| 384 |
private: |
| 385 |
union { |
| 386 |
struct { |
| 387 |
int reg; |
| 388 |
int value; |
| 389 |
} u_store_register; |
| 390 |
struct { |
| 391 |
int reg; |
| 392 |
} u_increment_register; |
| 393 |
struct { |
| 394 |
int reg; |
| 395 |
bool is_capture; |
| 396 |
} u_position_register; |
| 397 |
struct { |
| 398 |
int stack_pointer_register; |
| 399 |
int current_position_register; |
| 400 |
int clear_register_count; |
| 401 |
int clear_register_from; |
| 402 |
ActionNode* success_node; // Only used for positive submatch. |
| 403 |
} u_submatch; |
| 404 |
struct { |
| 405 |
int start_register; |
| 406 |
int repetition_register; |
| 407 |
int repetition_limit; |
| 408 |
} u_empty_match_check; |
| 409 |
struct { |
| 410 |
int range_from; |
| 411 |
int range_to; |
| 412 |
} u_clear_captures; |
| 413 |
struct { |
| 414 |
int flags; |
| 415 |
} u_modify_flags; |
| 416 |
} data_; |
| 417 |
|
| 418 |
ActionType action_type_; |
| 419 |
friend class DotPrinterImpl; |
| 420 |
friend Zone; |
| 421 |
}; |
| 422 |
|
| 423 |
class TextNode : public SeqRegExpNode { |
| 424 |
public: |
| 425 |
TextNode(ZoneList<TextElement>* elms, bool read_backward, |
| 426 |
RegExpNode* on_success) |
| 427 |
: SeqRegExpNode(on_success), elms_(elms), read_backward_(read_backward) {} |
| 428 |
TextNode(RegExpClassRanges* that, bool read_backward, RegExpNode* on_success) |
| 429 |
: SeqRegExpNode(on_success), |
| 430 |
elms_(zone()->New<ZoneList<TextElement>>(1, zone())), |
| 431 |
read_backward_(read_backward) { |
| 432 |
elms_->Add(TextElement::ClassRanges(that), zone()); |
| 433 |
} |
| 434 |
// Create TextNode for a single character class for the given ranges. |
| 435 |
static TextNode* CreateForCharacterRanges(Zone* zone, |
| 436 |
ZoneList<CharacterRange>* ranges, |
| 437 |
bool read_backward, |
| 438 |
RegExpNode* on_success); |
| 439 |
// Create TextNode for a surrogate pair (i.e. match a sequence of two uc16 |
| 440 |
// code unit ranges). |
| 441 |
static TextNode* CreateForSurrogatePair( |
| 442 |
Zone* zone, CharacterRange lead, ZoneList<CharacterRange>* trail_ranges, |
| 443 |
bool read_backward, RegExpNode* on_success); |
| 444 |
static TextNode* CreateForSurrogatePair(Zone* zone, |
| 445 |
ZoneList<CharacterRange>* lead_ranges, |
| 446 |
CharacterRange trail, |
| 447 |
bool read_backward, |
| 448 |
RegExpNode* on_success); |
| 449 |
TextNode* AsTextNode() override { return this; } |
| 450 |
void Accept(NodeVisitor* visitor) override; |
| 451 |
void Emit(RegExpCompiler* compiler, Trace* trace) override; |
| 452 |
void GetQuickCheckDetails(QuickCheckDetails* details, |
| 453 |
RegExpCompiler* compiler, int characters_filled_in, |
| 454 |
bool not_at_start) override; |
| 455 |
ZoneList<TextElement>* elements() { return elms_; } |
| 456 |
bool read_backward() { return read_backward_; } |
| 457 |
void MakeCaseIndependent(Isolate* isolate, bool is_one_byte, |
| 458 |
RegExpFlags flags); |
| 459 |
int GreedyLoopTextLength() override; |
| 460 |
RegExpNode* GetSuccessorOfOmnivorousTextNode( |
| 461 |
RegExpCompiler* compiler) override; |
| 462 |
void FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 463 |
BoyerMooreLookahead* bm, bool not_at_start) override; |
| 464 |
void CalculateOffsets(); |
| 465 |
RegExpNode* FilterOneByte(int depth, RegExpCompiler* compiler) override; |
| 466 |
int Length(); |
| 467 |
|
| 468 |
private: |
| 469 |
enum TextEmitPassType { |
| 470 |
NON_LATIN1_MATCH, // Check for characters that can never match. |
| 471 |
SIMPLE_CHARACTER_MATCH, // Case-dependent single character check. |
| 472 |
NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs. |
| 473 |
CASE_CHARACTER_MATCH, // Case-independent single character check. |
| 474 |
CHARACTER_CLASS_MATCH // Character class. |
| 475 |
}; |
| 476 |
void TextEmitPass(RegExpCompiler* compiler, TextEmitPassType pass, |
| 477 |
bool preloaded, Trace* trace, bool first_element_checked, |
| 478 |
int* checked_up_to); |
| 479 |
ZoneList<TextElement>* elms_; |
| 480 |
bool read_backward_; |
| 481 |
}; |
| 482 |
|
| 483 |
class AssertionNode : public SeqRegExpNode { |
| 484 |
public: |
| 485 |
enum AssertionType { |
| 486 |
AT_END, |
| 487 |
AT_START, |
| 488 |
AT_BOUNDARY, |
| 489 |
AT_NON_BOUNDARY, |
| 490 |
AFTER_NEWLINE |
| 491 |
}; |
| 492 |
static AssertionNode* AtEnd(RegExpNode* on_success) { |
| 493 |
return on_success->zone()->New<AssertionNode>(AT_END, on_success); |
| 494 |
} |
| 495 |
static AssertionNode* AtStart(RegExpNode* on_success) { |
| 496 |
return on_success->zone()->New<AssertionNode>(AT_START, on_success); |
| 497 |
} |
| 498 |
static AssertionNode* AtBoundary(RegExpNode* on_success) { |
| 499 |
return on_success->zone()->New<AssertionNode>(AT_BOUNDARY, on_success); |
| 500 |
} |
| 501 |
static AssertionNode* AtNonBoundary(RegExpNode* on_success) { |
| 502 |
return on_success->zone()->New<AssertionNode>(AT_NON_BOUNDARY, on_success); |
| 503 |
} |
| 504 |
static AssertionNode* AfterNewline(RegExpNode* on_success) { |
| 505 |
return on_success->zone()->New<AssertionNode>(AFTER_NEWLINE, on_success); |
| 506 |
} |
| 507 |
AssertionNode* AsAssertionNode() override { return this; } |
| 508 |
void Accept(NodeVisitor* visitor) override; |
| 509 |
void Emit(RegExpCompiler* compiler, Trace* trace) override; |
| 510 |
void GetQuickCheckDetails(QuickCheckDetails* details, |
| 511 |
RegExpCompiler* compiler, int filled_in, |
| 512 |
bool not_at_start) override; |
| 513 |
void FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 514 |
BoyerMooreLookahead* bm, bool not_at_start) override; |
| 515 |
AssertionType assertion_type() { return assertion_type_; } |
| 516 |
|
| 517 |
private: |
| 518 |
friend Zone; |
| 519 |
|
| 520 |
void EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace); |
| 521 |
enum IfPrevious { kIsNonWord, kIsWord }; |
| 522 |
void BacktrackIfPrevious(RegExpCompiler* compiler, Trace* trace, |
| 523 |
IfPrevious backtrack_if_previous); |
| 524 |
AssertionNode(AssertionType t, RegExpNode* on_success) |
| 525 |
: SeqRegExpNode(on_success), assertion_type_(t) {} |
| 526 |
AssertionType assertion_type_; |
| 527 |
}; |
| 528 |
|
| 529 |
class BackReferenceNode : public SeqRegExpNode { |
| 530 |
public: |
| 531 |
BackReferenceNode(int start_reg, int end_reg, bool read_backward, |
| 532 |
RegExpNode* on_success) |
| 533 |
: SeqRegExpNode(on_success), |
| 534 |
start_reg_(start_reg), |
| 535 |
end_reg_(end_reg), |
| 536 |
read_backward_(read_backward) {} |
| 537 |
BackReferenceNode* AsBackReferenceNode() override { return this; } |
| 538 |
void Accept(NodeVisitor* visitor) override; |
| 539 |
int start_register() { return start_reg_; } |
| 540 |
int end_register() { return end_reg_; } |
| 541 |
bool read_backward() { return read_backward_; } |
| 542 |
void Emit(RegExpCompiler* compiler, Trace* trace) override; |
| 543 |
void GetQuickCheckDetails(QuickCheckDetails* details, |
| 544 |
RegExpCompiler* compiler, int characters_filled_in, |
| 545 |
bool not_at_start) override { |
| 546 |
return; |
| 547 |
} |
| 548 |
void FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 549 |
BoyerMooreLookahead* bm, bool not_at_start) override; |
| 550 |
|
| 551 |
private: |
| 552 |
int start_reg_; |
| 553 |
int end_reg_; |
| 554 |
bool read_backward_; |
| 555 |
}; |
| 556 |
|
| 557 |
class EndNode : public RegExpNode { |
| 558 |
public: |
| 559 |
enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS }; |
| 560 |
EndNode(Action action, Zone* zone) : RegExpNode(zone), action_(action) {} |
| 561 |
EndNode* AsEndNode() override { return this; } |
| 562 |
void Accept(NodeVisitor* visitor) override; |
| 563 |
void Emit(RegExpCompiler* compiler, Trace* trace) override; |
| 564 |
void GetQuickCheckDetails(QuickCheckDetails* details, |
| 565 |
RegExpCompiler* compiler, int characters_filled_in, |
| 566 |
bool not_at_start) override { |
| 567 |
// Returning 0 from EatsAtLeast should ensure we never get here. |
| 568 |
UNREACHABLE(); |
| 569 |
} |
| 570 |
void FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 571 |
BoyerMooreLookahead* bm, bool not_at_start) override { |
| 572 |
// Returning 0 from EatsAtLeast should ensure we never get here. |
| 573 |
UNREACHABLE(); |
| 574 |
} |
| 575 |
|
| 576 |
private: |
| 577 |
Action action_; |
| 578 |
}; |
| 579 |
|
| 580 |
class NegativeSubmatchSuccess : public EndNode { |
| 581 |
public: |
| 582 |
NegativeSubmatchSuccess(int stack_pointer_reg, int position_reg, |
| 583 |
int clear_capture_count, int clear_capture_start, |
| 584 |
Zone* zone) |
| 585 |
: EndNode(NEGATIVE_SUBMATCH_SUCCESS, zone), |
| 586 |
stack_pointer_register_(stack_pointer_reg), |
| 587 |
current_position_register_(position_reg), |
| 588 |
clear_capture_count_(clear_capture_count), |
| 589 |
clear_capture_start_(clear_capture_start) {} |
| 590 |
void Emit(RegExpCompiler* compiler, Trace* trace) override; |
| 591 |
|
| 592 |
private: |
| 593 |
int stack_pointer_register_; |
| 594 |
int current_position_register_; |
| 595 |
int clear_capture_count_; |
| 596 |
int clear_capture_start_; |
| 597 |
}; |
| 598 |
|
| 599 |
class Guard : public ZoneObject { |
| 600 |
public: |
| 601 |
enum Relation { LT, GEQ }; |
| 602 |
Guard(int reg, Relation op, int value) : reg_(reg), op_(op), value_(value) {} |
| 603 |
int reg() { return reg_; } |
| 604 |
Relation op() { return op_; } |
| 605 |
int value() { return value_; } |
| 606 |
|
| 607 |
private: |
| 608 |
int reg_; |
| 609 |
Relation op_; |
| 610 |
int value_; |
| 611 |
}; |
| 612 |
|
| 613 |
class GuardedAlternative { |
| 614 |
public: |
| 615 |
explicit GuardedAlternative(RegExpNode* node) |
| 616 |
: node_(node), guards_(nullptr) {} |
| 617 |
void AddGuard(Guard* guard, Zone* zone); |
| 618 |
RegExpNode* node() { return node_; } |
| 619 |
void set_node(RegExpNode* node) { node_ = node; } |
| 620 |
ZoneList<Guard*>* guards() { return guards_; } |
| 621 |
|
| 622 |
private: |
| 623 |
RegExpNode* node_; |
| 624 |
ZoneList<Guard*>* guards_; |
| 625 |
}; |
| 626 |
|
| 627 |
class AlternativeGeneration; |
| 628 |
|
| 629 |
class ChoiceNode : public RegExpNode { |
| 630 |
public: |
| 631 |
explicit ChoiceNode(int expected_size, Zone* zone) |
| 632 |
: RegExpNode(zone), |
| 633 |
alternatives_( |
| 634 |
zone->New<ZoneList<GuardedAlternative>>(expected_size, zone)), |
| 635 |
not_at_start_(false), |
| 636 |
being_calculated_(false) {} |
| 637 |
ChoiceNode* AsChoiceNode() override { return this; } |
| 638 |
void Accept(NodeVisitor* visitor) override; |
| 639 |
void AddAlternative(GuardedAlternative node) { |
| 640 |
alternatives()->Add(node, zone()); |
| 641 |
} |
| 642 |
ZoneList<GuardedAlternative>* alternatives() { return alternatives_; } |
| 643 |
void Emit(RegExpCompiler* compiler, Trace* trace) override; |
| 644 |
void GetQuickCheckDetails(QuickCheckDetails* details, |
| 645 |
RegExpCompiler* compiler, int characters_filled_in, |
| 646 |
bool not_at_start) override; |
| 647 |
void FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 648 |
BoyerMooreLookahead* bm, bool not_at_start) override; |
| 649 |
|
| 650 |
bool being_calculated() { return being_calculated_; } |
| 651 |
bool not_at_start() { return not_at_start_; } |
| 652 |
void set_not_at_start() { not_at_start_ = true; } |
| 653 |
void set_being_calculated(bool b) { being_calculated_ = b; } |
| 654 |
virtual bool try_to_emit_quick_check_for_alternative(bool is_first) { |
| 655 |
return true; |
| 656 |
} |
| 657 |
RegExpNode* FilterOneByte(int depth, RegExpCompiler* compiler) override; |
| 658 |
virtual bool read_backward() { return false; } |
| 659 |
|
| 660 |
protected: |
| 661 |
int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative); |
| 662 |
ZoneList<GuardedAlternative>* alternatives_; |
| 663 |
|
| 664 |
private: |
| 665 |
template <typename...> |
| 666 |
friend class Analysis; |
| 667 |
|
| 668 |
void GenerateGuard(RegExpMacroAssembler* macro_assembler, Guard* guard, |
| 669 |
Trace* trace); |
| 670 |
int CalculatePreloadCharacters(RegExpCompiler* compiler, int eats_at_least); |
| 671 |
void EmitOutOfLineContinuation(RegExpCompiler* compiler, Trace* trace, |
| 672 |
GuardedAlternative alternative, |
| 673 |
AlternativeGeneration* alt_gen, |
| 674 |
int preload_characters, |
| 675 |
bool next_expects_preload); |
| 676 |
void SetUpPreLoad(RegExpCompiler* compiler, Trace* current_trace, |
| 677 |
PreloadState* preloads); |
| 678 |
void AssertGuardsMentionRegisters(Trace* trace); |
| 679 |
int EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler, Trace* trace); |
| 680 |
Trace* EmitGreedyLoop(RegExpCompiler* compiler, Trace* trace, |
| 681 |
AlternativeGenerationList* alt_gens, |
| 682 |
PreloadState* preloads, |
| 683 |
GreedyLoopState* greedy_loop_state, int text_length); |
| 684 |
void EmitChoices(RegExpCompiler* compiler, |
| 685 |
AlternativeGenerationList* alt_gens, int first_choice, |
| 686 |
Trace* trace, PreloadState* preloads); |
| 687 |
|
| 688 |
// If true, this node is never checked at the start of the input. |
| 689 |
// Allows a new trace to start with at_start() set to false. |
| 690 |
bool not_at_start_; |
| 691 |
bool being_calculated_; |
| 692 |
}; |
| 693 |
|
| 694 |
class NegativeLookaroundChoiceNode : public ChoiceNode { |
| 695 |
public: |
| 696 |
explicit NegativeLookaroundChoiceNode(GuardedAlternative this_must_fail, |
| 697 |
GuardedAlternative then_do_this, |
| 698 |
Zone* zone) |
| 699 |
: ChoiceNode(2, zone) { |
| 700 |
AddAlternative(this_must_fail); |
| 701 |
AddAlternative(then_do_this); |
| 702 |
} |
| 703 |
void GetQuickCheckDetails(QuickCheckDetails* details, |
| 704 |
RegExpCompiler* compiler, int characters_filled_in, |
| 705 |
bool not_at_start) override; |
| 706 |
void FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 707 |
BoyerMooreLookahead* bm, bool not_at_start) override { |
| 708 |
continue_node()->FillInBMInfo(isolate, offset, budget - 1, bm, |
| 709 |
not_at_start); |
| 710 |
if (offset == 0) set_bm_info(not_at_start, bm); |
| 711 |
} |
| 712 |
static constexpr int kLookaroundIndex = 0; |
| 713 |
static constexpr int kContinueIndex = 1; |
| 714 |
RegExpNode* lookaround_node() { |
| 715 |
return alternatives()->at(kLookaroundIndex).node(); |
| 716 |
} |
| 717 |
RegExpNode* continue_node() { |
| 718 |
return alternatives()->at(kContinueIndex).node(); |
| 719 |
} |
| 720 |
// For a negative lookahead we don't emit the quick check for the |
| 721 |
// alternative that is expected to fail. This is because quick check code |
| 722 |
// starts by loading enough characters for the alternative that takes fewest |
| 723 |
// characters, but on a negative lookahead the negative branch did not take |
| 724 |
// part in that calculation (EatsAtLeast) so the assumptions don't hold. |
| 725 |
bool try_to_emit_quick_check_for_alternative(bool is_first) override { |
| 726 |
return !is_first; |
| 727 |
} |
| 728 |
NegativeLookaroundChoiceNode* AsNegativeLookaroundChoiceNode() override { |
| 729 |
return this; |
| 730 |
} |
| 731 |
void Accept(NodeVisitor* visitor) override; |
| 732 |
RegExpNode* FilterOneByte(int depth, RegExpCompiler* compiler) override; |
| 733 |
}; |
| 734 |
|
| 735 |
class LoopChoiceNode : public ChoiceNode { |
| 736 |
public: |
| 737 |
LoopChoiceNode(bool body_can_be_zero_length, bool read_backward, |
| 738 |
int min_loop_iterations, Zone* zone) |
| 739 |
: ChoiceNode(2, zone), |
| 740 |
loop_node_(nullptr), |
| 741 |
continue_node_(nullptr), |
| 742 |
body_can_be_zero_length_(body_can_be_zero_length), |
| 743 |
read_backward_(read_backward), |
| 744 |
traversed_loop_initialization_node_(false), |
| 745 |
min_loop_iterations_(min_loop_iterations) {} |
| 746 |
void AddLoopAlternative(GuardedAlternative alt); |
| 747 |
void AddContinueAlternative(GuardedAlternative alt); |
| 748 |
void Emit(RegExpCompiler* compiler, Trace* trace) override; |
| 749 |
void GetQuickCheckDetails(QuickCheckDetails* details, |
| 750 |
RegExpCompiler* compiler, int characters_filled_in, |
| 751 |
bool not_at_start) override; |
| 752 |
void GetQuickCheckDetailsFromLoopEntry(QuickCheckDetails* details, |
| 753 |
RegExpCompiler* compiler, |
| 754 |
int characters_filled_in, |
| 755 |
bool not_at_start) override; |
| 756 |
void FillInBMInfo(Isolate* isolate, int offset, int budget, |
| 757 |
BoyerMooreLookahead* bm, bool not_at_start) override; |
| 758 |
EatsAtLeastInfo EatsAtLeastFromLoopEntry() override; |
| 759 |
RegExpNode* loop_node() { return loop_node_; } |
| 760 |
RegExpNode* continue_node() { return continue_node_; } |
| 761 |
bool body_can_be_zero_length() { return body_can_be_zero_length_; } |
| 762 |
int min_loop_iterations() const { return min_loop_iterations_; } |
| 763 |
bool read_backward() override { return read_backward_; } |
| 764 |
LoopChoiceNode* AsLoopChoiceNode() override { return this; } |
| 765 |
void Accept(NodeVisitor* visitor) override; |
| 766 |
RegExpNode* FilterOneByte(int depth, RegExpCompiler* compiler) override; |
| 767 |
|
| 768 |
private: |
| 769 |
// AddAlternative is made private for loop nodes because alternatives |
| 770 |
// should not be added freely, we need to keep track of which node |
| 771 |
// goes back to the node itself. |
| 772 |
void AddAlternative(GuardedAlternative node) { |
| 773 |
ChoiceNode::AddAlternative(node); |
| 774 |
} |
| 775 |
|
| 776 |
RegExpNode* loop_node_; |
| 777 |
RegExpNode* continue_node_; |
| 778 |
bool body_can_be_zero_length_; |
| 779 |
bool read_backward_; |
| 780 |
|
| 781 |
// Temporary marker set only while generating quick check details. Represents |
| 782 |
// whether GetQuickCheckDetails traversed the initialization node for this |
| 783 |
// loop's counter. If so, we may be able to generate stricter quick checks |
| 784 |
// because we know the loop node must match at least min_loop_iterations_ |
| 785 |
// times before the continuation node can match. |
| 786 |
bool traversed_loop_initialization_node_; |
| 787 |
|
| 788 |
// The minimum number of times the loop_node_ must match before the |
| 789 |
// continue_node_ might be considered. This value can be temporarily decreased |
| 790 |
// while generating quick check details, to represent the remaining iterations |
| 791 |
// after the completed portion of the quick check details. |
| 792 |
int min_loop_iterations_; |
| 793 |
|
| 794 |
friend class IterationDecrementer; |
| 795 |
friend class LoopInitializationMarker; |
| 796 |
}; |
| 797 |
|
| 798 |
class NodeVisitor { |
| 799 |
public: |
| 800 |
virtual ~NodeVisitor() = default; |
| 801 |
#define DECLARE_VISIT(Type) virtual void Visit##Type(Type##Node* that) = 0; |
| 802 |
FOR_EACH_NODE_TYPE(DECLARE_VISIT) |
| 803 |
#undef DECLARE_VISIT |
| 804 |
}; |
| 805 |
|
| 806 |
} // namespace internal |
| 807 |
} // namespace v8 |
| 808 |
|
| 809 |
#endif // V8_REGEXP_REGEXP_NODES_H_ |