OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #ifndef VM_REGEXP_H_ |
| 6 #define VM_REGEXP_H_ |
| 7 |
| 8 #include "vm/assembler.h" |
| 9 #include "vm/intermediate_language.h" |
| 10 #include "vm/object.h" |
| 11 #include "vm/regexp_assembler.h" |
| 12 |
| 13 namespace dart { |
| 14 |
| 15 class NodeVisitor; |
| 16 class RegExpCompiler; |
| 17 class RegExpMacroAssembler; |
| 18 class RegExpNode; |
| 19 class RegExpTree; |
| 20 class BoyerMooreLookahead; |
| 21 |
| 22 |
| 23 // Represents code units in the range from from_ to to_, both ends are |
| 24 // inclusive. |
| 25 class CharacterRange { |
| 26 public: |
| 27 CharacterRange() : from_(0), to_(0) { } |
| 28 CharacterRange(uint16_t from, uint16_t to) : from_(from), to_(to) { } |
| 29 |
| 30 static void AddClassEscape(uint16_t type, |
| 31 ZoneGrowableArray<CharacterRange>* ranges); |
| 32 static GrowableArray<const intptr_t> GetWordBounds(); |
| 33 static inline CharacterRange Singleton(uint16_t value) { |
| 34 return CharacterRange(value, value); |
| 35 } |
| 36 static inline CharacterRange Range(uint16_t from, uint16_t to) { |
| 37 ASSERT(from <= to); |
| 38 return CharacterRange(from, to); |
| 39 } |
| 40 static inline CharacterRange Everything() { |
| 41 return CharacterRange(0, 0xFFFF); |
| 42 } |
| 43 bool Contains(uint16_t i) const { return from_ <= i && i <= to_; } |
| 44 uint16_t from() const { return from_; } |
| 45 void set_from(uint16_t value) { from_ = value; } |
| 46 uint16_t to() const { return to_; } |
| 47 void set_to(uint16_t value) { to_ = value; } |
| 48 bool is_valid() const { return from_ <= to_; } |
| 49 bool IsEverything(uint16_t max) const { return from_ == 0 && to_ >= max; } |
| 50 bool IsSingleton() const { return (from_ == to_); } |
| 51 void AddCaseEquivalents(ZoneGrowableArray<CharacterRange>* ranges, |
| 52 bool is_one_byte, |
| 53 Isolate* isolate); |
| 54 static void Split(ZoneGrowableArray<CharacterRange>* base, |
| 55 GrowableArray<const intptr_t> overlay, |
| 56 ZoneGrowableArray<CharacterRange>** included, |
| 57 ZoneGrowableArray<CharacterRange>** excluded, |
| 58 Isolate* isolate); |
| 59 // Whether a range list is in canonical form: Ranges ordered by from value, |
| 60 // and ranges non-overlapping and non-adjacent. |
| 61 static bool IsCanonical(ZoneGrowableArray<CharacterRange>* ranges); |
| 62 // Convert range list to canonical form. The characters covered by the ranges |
| 63 // will still be the same, but no character is in more than one range, and |
| 64 // adjacent ranges are merged. The resulting list may be shorter than the |
| 65 // original, but cannot be longer. |
| 66 static void Canonicalize(ZoneGrowableArray<CharacterRange>* ranges); |
| 67 // Negate the contents of a character range in canonical form. |
| 68 static void Negate(ZoneGrowableArray<CharacterRange>* src, |
| 69 ZoneGrowableArray<CharacterRange>* dst); |
| 70 static const intptr_t kStartMarker = (1 << 24); |
| 71 static const intptr_t kPayloadMask = (1 << 24) - 1; |
| 72 |
| 73 private: |
| 74 uint16_t from_; |
| 75 uint16_t to_; |
| 76 |
| 77 DISALLOW_ALLOCATION(); |
| 78 }; |
| 79 |
| 80 |
| 81 // A set of unsigned integers that behaves especially well on small |
| 82 // integers (< 32). May do zone-allocation. |
| 83 class OutSet: public ZoneAllocated { |
| 84 public: |
| 85 OutSet() : first_(0), remaining_(NULL), successors_(NULL) { } |
| 86 OutSet* Extend(unsigned value, Isolate* isolate); |
| 87 bool Get(unsigned value) const; |
| 88 static const unsigned kFirstLimit = 32; |
| 89 |
| 90 private: |
| 91 // Destructively set a value in this set. In most cases you want |
| 92 // to use Extend instead to ensure that only one instance exists |
| 93 // that contains the same values. |
| 94 void Set(unsigned value, Isolate* isolate); |
| 95 |
| 96 // The successors are a list of sets that contain the same values |
| 97 // as this set and the one more value that is not present in this |
| 98 // set. |
| 99 ZoneGrowableArray<OutSet*>* successors() { return successors_; } |
| 100 |
| 101 OutSet(uint32_t first, ZoneGrowableArray<unsigned>* remaining) |
| 102 : first_(first), remaining_(remaining), successors_(NULL) { } |
| 103 uint32_t first_; |
| 104 ZoneGrowableArray<unsigned>* remaining_; |
| 105 ZoneGrowableArray<OutSet*>* successors_; |
| 106 friend class Trace; |
| 107 }; |
| 108 |
| 109 |
| 110 #define FOR_EACH_NODE_TYPE(VISIT) \ |
| 111 VISIT(End) \ |
| 112 VISIT(Action) \ |
| 113 VISIT(Choice) \ |
| 114 VISIT(BackReference) \ |
| 115 VISIT(Assertion) \ |
| 116 VISIT(Text) |
| 117 |
| 118 |
| 119 #define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \ |
| 120 VISIT(Disjunction) \ |
| 121 VISIT(Alternative) \ |
| 122 VISIT(Assertion) \ |
| 123 VISIT(CharacterClass) \ |
| 124 VISIT(Atom) \ |
| 125 VISIT(Quantifier) \ |
| 126 VISIT(Capture) \ |
| 127 VISIT(Lookahead) \ |
| 128 VISIT(BackReference) \ |
| 129 VISIT(Empty) \ |
| 130 VISIT(Text) |
| 131 |
| 132 |
| 133 #define FORWARD_DECLARE(Name) class RegExp##Name; |
| 134 FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE) |
| 135 #undef FORWARD_DECLARE |
| 136 |
| 137 |
| 138 class TextElement { |
| 139 public: |
| 140 enum TextType { |
| 141 ATOM, |
| 142 CHAR_CLASS |
| 143 }; |
| 144 |
| 145 static TextElement Atom(RegExpAtom* atom); |
| 146 static TextElement CharClass(RegExpCharacterClass* char_class); |
| 147 |
| 148 intptr_t cp_offset() const { return cp_offset_; } |
| 149 void set_cp_offset(intptr_t cp_offset) { cp_offset_ = cp_offset; } |
| 150 intptr_t length() const; |
| 151 |
| 152 TextType text_type() const { return text_type_; } |
| 153 |
| 154 RegExpTree* tree() const { return tree_; } |
| 155 |
| 156 RegExpAtom* atom() const { |
| 157 ASSERT(text_type() == ATOM); |
| 158 return reinterpret_cast<RegExpAtom*>(tree()); |
| 159 } |
| 160 |
| 161 RegExpCharacterClass* char_class() const { |
| 162 ASSERT(text_type() == CHAR_CLASS); |
| 163 return reinterpret_cast<RegExpCharacterClass*>(tree()); |
| 164 } |
| 165 |
| 166 private: |
| 167 TextElement(TextType text_type, RegExpTree* tree) |
| 168 : cp_offset_(-1), text_type_(text_type), tree_(tree) {} |
| 169 |
| 170 intptr_t cp_offset_; |
| 171 TextType text_type_; |
| 172 RegExpTree* tree_; |
| 173 |
| 174 DISALLOW_ALLOCATION(); |
| 175 }; |
| 176 |
| 177 |
| 178 class Trace; |
| 179 struct PreloadState; |
| 180 class GreedyLoopState; |
| 181 class AlternativeGenerationList; |
| 182 |
| 183 struct NodeInfo { |
| 184 NodeInfo() |
| 185 : being_analyzed(false), |
| 186 been_analyzed(false), |
| 187 follows_word_interest(false), |
| 188 follows_newline_interest(false), |
| 189 follows_start_interest(false), |
| 190 at_end(false), |
| 191 visited(false), |
| 192 replacement_calculated(false) { } |
| 193 |
| 194 // Returns true if the interests and assumptions of this node |
| 195 // matches the given one. |
| 196 bool Matches(NodeInfo* that) { |
| 197 return (at_end == that->at_end) && |
| 198 (follows_word_interest == that->follows_word_interest) && |
| 199 (follows_newline_interest == that->follows_newline_interest) && |
| 200 (follows_start_interest == that->follows_start_interest); |
| 201 } |
| 202 |
| 203 // Updates the interests of this node given the interests of the |
| 204 // node preceding it. |
| 205 void AddFromPreceding(NodeInfo* that) { |
| 206 at_end |= that->at_end; |
| 207 follows_word_interest |= that->follows_word_interest; |
| 208 follows_newline_interest |= that->follows_newline_interest; |
| 209 follows_start_interest |= that->follows_start_interest; |
| 210 } |
| 211 |
| 212 bool HasLookbehind() { |
| 213 return follows_word_interest || |
| 214 follows_newline_interest || |
| 215 follows_start_interest; |
| 216 } |
| 217 |
| 218 // Sets the interests of this node to include the interests of the |
| 219 // following node. |
| 220 void AddFromFollowing(NodeInfo* that) { |
| 221 follows_word_interest |= that->follows_word_interest; |
| 222 follows_newline_interest |= that->follows_newline_interest; |
| 223 follows_start_interest |= that->follows_start_interest; |
| 224 } |
| 225 |
| 226 void ResetCompilationState() { |
| 227 being_analyzed = false; |
| 228 been_analyzed = false; |
| 229 } |
| 230 |
| 231 bool being_analyzed: 1; |
| 232 bool been_analyzed: 1; |
| 233 |
| 234 // These bits are set of this node has to know what the preceding |
| 235 // character was. |
| 236 bool follows_word_interest: 1; |
| 237 bool follows_newline_interest: 1; |
| 238 bool follows_start_interest: 1; |
| 239 |
| 240 bool at_end: 1; |
| 241 bool visited: 1; |
| 242 bool replacement_calculated: 1; |
| 243 }; |
| 244 |
| 245 |
| 246 // Details of a quick mask-compare check that can look ahead in the |
| 247 // input stream. |
| 248 class QuickCheckDetails { |
| 249 public: |
| 250 QuickCheckDetails() |
| 251 : characters_(0), |
| 252 mask_(0), |
| 253 value_(0), |
| 254 cannot_match_(false) { } |
| 255 explicit QuickCheckDetails(intptr_t characters) |
| 256 : characters_(characters), |
| 257 mask_(0), |
| 258 value_(0), |
| 259 cannot_match_(false) { } |
| 260 bool Rationalize(bool one_byte); |
| 261 // Merge in the information from another branch of an alternation. |
| 262 void Merge(QuickCheckDetails* other, intptr_t from_index); |
| 263 // Advance the current position by some amount. |
| 264 void Advance(intptr_t by, bool one_byte); |
| 265 void Clear(); |
| 266 bool cannot_match() { return cannot_match_; } |
| 267 void set_cannot_match() { cannot_match_ = true; } |
| 268 struct Position { |
| 269 Position() : mask(0), value(0), determines_perfectly(false) { } |
| 270 uint16_t mask; |
| 271 uint16_t value; |
| 272 bool determines_perfectly; |
| 273 }; |
| 274 intptr_t characters() { return characters_; } |
| 275 void set_characters(intptr_t characters) { characters_ = characters; } |
| 276 Position* positions(intptr_t index) { |
| 277 ASSERT(index >= 0); |
| 278 ASSERT(index < characters_); |
| 279 return positions_ + index; |
| 280 } |
| 281 uint32_t mask() { return mask_; } |
| 282 uint32_t value() { return value_; } |
| 283 |
| 284 private: |
| 285 // How many characters do we have quick check information from. This is |
| 286 // the same for all branches of a choice node. |
| 287 intptr_t characters_; |
| 288 Position positions_[4]; |
| 289 // These values are the condensate of the above array after Rationalize(). |
| 290 uint32_t mask_; |
| 291 uint32_t value_; |
| 292 // If set to true, there is no way this quick check can match at all. |
| 293 // E.g., if it requires to be at the start of the input, and isn't. |
| 294 bool cannot_match_; |
| 295 |
| 296 DISALLOW_ALLOCATION(); |
| 297 }; |
| 298 |
| 299 |
| 300 class RegExpNode: public ZoneAllocated { |
| 301 public: |
| 302 explicit RegExpNode(Isolate* isolate) |
| 303 : replacement_(NULL), trace_count_(0), isolate_(isolate) { |
| 304 bm_info_[0] = bm_info_[1] = NULL; |
| 305 } |
| 306 virtual ~RegExpNode(); |
| 307 virtual void Accept(NodeVisitor* visitor) = 0; |
| 308 // Generates a goto to this node or actually generates the code at this point. |
| 309 virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0; |
| 310 // How many characters must this node consume at a minimum in order to |
| 311 // succeed. If we have found at least 'still_to_find' characters that |
| 312 // must be consumed there is no need to ask any following nodes whether |
| 313 // they are sure to eat any more characters. The not_at_start argument is |
| 314 // used to indicate that we know we are not at the start of the input. In |
| 315 // this case anchored branches will always fail and can be ignored when |
| 316 // determining how many characters are consumed on success. |
| 317 virtual intptr_t EatsAtLeast(intptr_t still_to_find, intptr_t budget, |
| 318 bool not_at_start) = 0; |
| 319 // Emits some quick code that checks whether the preloaded characters match. |
| 320 // Falls through on certain failure, jumps to the label on possible success. |
| 321 // If the node cannot make a quick check it does nothing and returns false. |
| 322 bool EmitQuickCheck(RegExpCompiler* compiler, |
| 323 Trace* bounds_check_trace, |
| 324 Trace* trace, |
| 325 bool preload_has_checked_bounds, |
| 326 BlockLabel* on_possible_success, |
| 327 QuickCheckDetails* details_return, |
| 328 bool fall_through_on_failure); |
| 329 // For a given number of characters this returns a mask and a value. The |
| 330 // next n characters are anded with the mask and compared with the value. |
| 331 // A comparison failure indicates the node cannot match the next n characters. |
| 332 // A comparison success indicates the node may match. |
| 333 virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 334 RegExpCompiler* compiler, |
| 335 intptr_t characters_filled_in, |
| 336 bool not_at_start) = 0; |
| 337 static const intptr_t kNodeIsTooComplexForGreedyLoops = -1; |
| 338 virtual intptr_t GreedyLoopTextLength() { |
| 339 return kNodeIsTooComplexForGreedyLoops; |
| 340 } |
| 341 // Only returns the successor for a text node of length 1 that matches any |
| 342 // character and that has no guards on it. |
| 343 virtual RegExpNode* GetSuccessorOfOmnivorousTextNode( |
| 344 RegExpCompiler* compiler) { |
| 345 return NULL; |
| 346 } |
| 347 |
| 348 // Collects information on the possible code units (mod 128) that can match if |
| 349 // we look forward. This is used for a Boyer-Moore-like string searching |
| 350 // implementation. TODO(erikcorry): This should share more code with |
| 351 // EatsAtLeast, GetQuickCheckDetails. The budget argument is used to limit |
| 352 // the number of nodes we are willing to look at in order to create this data. |
| 353 static const intptr_t kRecursionBudget = 200; |
| 354 virtual void FillInBMInfo(intptr_t offset, |
| 355 intptr_t budget, |
| 356 BoyerMooreLookahead* bm, |
| 357 bool not_at_start) { |
| 358 UNREACHABLE(); |
| 359 } |
| 360 |
| 361 // If we know that the input is one-byte then there are some nodes that can |
| 362 // never match. This method returns a node that can be substituted for |
| 363 // itself, or NULL if the node can never match. |
| 364 virtual RegExpNode* FilterOneByte(intptr_t depth, bool ignore_case) { |
| 365 return this; |
| 366 } |
| 367 // Helper for FilterOneByte. |
| 368 RegExpNode* replacement() { |
| 369 ASSERT(info()->replacement_calculated); |
| 370 return replacement_; |
| 371 } |
| 372 RegExpNode* set_replacement(RegExpNode* replacement) { |
| 373 info()->replacement_calculated = true; |
| 374 replacement_ = replacement; |
| 375 return replacement; // For convenience. |
| 376 } |
| 377 |
| 378 // We want to avoid recalculating the lookahead info, so we store it on the |
| 379 // node. Only info that is for this node is stored. We can tell that the |
| 380 // info is for this node when offset == 0, so the information is calculated |
| 381 // relative to this node. |
| 382 void SaveBMInfo(BoyerMooreLookahead* bm, bool not_at_start, intptr_t offset) { |
| 383 if (offset == 0) set_bm_info(not_at_start, bm); |
| 384 } |
| 385 |
| 386 BlockLabel* label() { return &label_; } |
| 387 // If non-generic code is generated for a node (i.e. the node is not at the |
| 388 // start of the trace) then it cannot be reused. This variable sets a limit |
| 389 // on how often we allow that to happen before we insist on starting a new |
| 390 // trace and generating generic code for a node that can be reused by flushing |
| 391 // the deferred actions in the current trace and generating a goto. |
| 392 static const intptr_t kMaxCopiesCodeGenerated = 10; |
| 393 |
| 394 NodeInfo* info() { return &info_; } |
| 395 |
| 396 BoyerMooreLookahead* bm_info(bool not_at_start) { |
| 397 return bm_info_[not_at_start ? 1 : 0]; |
| 398 } |
| 399 |
| 400 Isolate* isolate() const { return isolate_; } |
| 401 |
| 402 protected: |
| 403 enum LimitResult { DONE, CONTINUE }; |
| 404 RegExpNode* replacement_; |
| 405 |
| 406 LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace); |
| 407 |
| 408 void set_bm_info(bool not_at_start, BoyerMooreLookahead* bm) { |
| 409 bm_info_[not_at_start ? 1 : 0] = bm; |
| 410 } |
| 411 |
| 412 private: |
| 413 static const intptr_t kFirstCharBudget = 10; |
| 414 BlockLabel label_; |
| 415 NodeInfo info_; |
| 416 // This variable keeps track of how many times code has been generated for |
| 417 // this node (in different traces). We don't keep track of where the |
| 418 // generated code is located unless the code is generated at the start of |
| 419 // a trace, in which case it is generic and can be reused by flushing the |
| 420 // deferred operations in the current trace and generating a goto. |
| 421 intptr_t trace_count_; |
| 422 BoyerMooreLookahead* bm_info_[2]; |
| 423 Isolate* isolate_; |
| 424 }; |
| 425 |
| 426 |
| 427 // A simple closed interval. |
| 428 class Interval { |
| 429 public: |
| 430 Interval() : from_(kNone), to_(kNone) { } |
| 431 Interval(intptr_t from, intptr_t to) : from_(from), to_(to) { } |
| 432 |
| 433 Interval Union(Interval that) { |
| 434 if (that.from_ == kNone) |
| 435 return *this; |
| 436 else if (from_ == kNone) |
| 437 return that; |
| 438 else |
| 439 return Interval(Utils::Minimum(from_, that.from_), |
| 440 Utils::Maximum(to_, that.to_)); |
| 441 } |
| 442 bool Contains(intptr_t value) const { |
| 443 return (from_ <= value) && (value <= to_); |
| 444 } |
| 445 bool is_empty() const { return from_ == kNone; } |
| 446 intptr_t from() const { return from_; } |
| 447 intptr_t to() const { return to_; } |
| 448 static Interval Empty() { return Interval(); } |
| 449 static const intptr_t kNone = -1; |
| 450 |
| 451 private: |
| 452 intptr_t from_; |
| 453 intptr_t to_; |
| 454 |
| 455 DISALLOW_ALLOCATION(); |
| 456 }; |
| 457 |
| 458 |
| 459 class SeqRegExpNode: public RegExpNode { |
| 460 public: |
| 461 explicit SeqRegExpNode(RegExpNode* on_success) |
| 462 : RegExpNode(on_success->isolate()), on_success_(on_success) { } |
| 463 RegExpNode* on_success() { return on_success_; } |
| 464 void set_on_success(RegExpNode* node) { on_success_ = node; } |
| 465 virtual RegExpNode* FilterOneByte(intptr_t depth, bool ignore_case); |
| 466 virtual void FillInBMInfo(intptr_t offset, |
| 467 intptr_t budget, |
| 468 BoyerMooreLookahead* bm, |
| 469 bool not_at_start) { |
| 470 on_success_->FillInBMInfo(offset, budget - 1, bm, not_at_start); |
| 471 if (offset == 0) set_bm_info(not_at_start, bm); |
| 472 } |
| 473 |
| 474 protected: |
| 475 RegExpNode* FilterSuccessor(intptr_t depth, bool ignore_case); |
| 476 |
| 477 private: |
| 478 RegExpNode* on_success_; |
| 479 }; |
| 480 |
| 481 |
| 482 class ActionNode: public SeqRegExpNode { |
| 483 public: |
| 484 enum ActionType { |
| 485 SET_REGISTER, |
| 486 INCREMENT_REGISTER, |
| 487 STORE_POSITION, |
| 488 BEGIN_SUBMATCH, |
| 489 POSITIVE_SUBMATCH_SUCCESS, |
| 490 EMPTY_MATCH_CHECK, |
| 491 CLEAR_CAPTURES |
| 492 }; |
| 493 static ActionNode* SetRegister(intptr_t reg, intptr_t val, |
| 494 RegExpNode* on_success); |
| 495 static ActionNode* IncrementRegister(intptr_t reg, RegExpNode* on_success); |
| 496 static ActionNode* StorePosition(intptr_t reg, |
| 497 bool is_capture, |
| 498 RegExpNode* on_success); |
| 499 static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success); |
| 500 static ActionNode* BeginSubmatch(intptr_t stack_pointer_reg, |
| 501 intptr_t position_reg, |
| 502 RegExpNode* on_success); |
| 503 static ActionNode* PositiveSubmatchSuccess(intptr_t stack_pointer_reg, |
| 504 intptr_t restore_reg, |
| 505 intptr_t clear_capture_count, |
| 506 intptr_t clear_capture_from, |
| 507 RegExpNode* on_success); |
| 508 static ActionNode* EmptyMatchCheck(intptr_t start_register, |
| 509 intptr_t repetition_register, |
| 510 intptr_t repetition_limit, |
| 511 RegExpNode* on_success); |
| 512 virtual void Accept(NodeVisitor* visitor); |
| 513 virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
| 514 virtual intptr_t EatsAtLeast(intptr_t still_to_find, intptr_t budget, |
| 515 bool not_at_start); |
| 516 virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 517 RegExpCompiler* compiler, |
| 518 intptr_t filled_in, |
| 519 bool not_at_start) { |
| 520 return on_success()->GetQuickCheckDetails( |
| 521 details, compiler, filled_in, not_at_start); |
| 522 } |
| 523 virtual void FillInBMInfo(intptr_t offset, |
| 524 intptr_t budget, |
| 525 BoyerMooreLookahead* bm, |
| 526 bool not_at_start); |
| 527 ActionType action_type() { return action_type_; } |
| 528 // TODO(erikcorry): We should allow some action nodes in greedy loops. |
| 529 virtual intptr_t GreedyLoopTextLength() { |
| 530 return kNodeIsTooComplexForGreedyLoops; |
| 531 } |
| 532 |
| 533 private: |
| 534 union { |
| 535 struct { |
| 536 intptr_t reg; |
| 537 intptr_t value; |
| 538 } u_store_register; |
| 539 struct { |
| 540 intptr_t reg; |
| 541 } u_increment_register; |
| 542 struct { |
| 543 intptr_t reg; |
| 544 bool is_capture; |
| 545 } u_position_register; |
| 546 struct { |
| 547 intptr_t stack_pointer_register; |
| 548 intptr_t current_position_register; |
| 549 intptr_t clear_register_count; |
| 550 intptr_t clear_register_from; |
| 551 } u_submatch; |
| 552 struct { |
| 553 intptr_t start_register; |
| 554 intptr_t repetition_register; |
| 555 intptr_t repetition_limit; |
| 556 } u_empty_match_check; |
| 557 struct { |
| 558 intptr_t range_from; |
| 559 intptr_t range_to; |
| 560 } u_clear_captures; |
| 561 } data_; |
| 562 ActionNode(ActionType action_type, RegExpNode* on_success) |
| 563 : SeqRegExpNode(on_success), |
| 564 action_type_(action_type) { } |
| 565 ActionType action_type_; |
| 566 friend class DotPrinter; |
| 567 }; |
| 568 |
| 569 |
| 570 class TextNode: public SeqRegExpNode { |
| 571 public: |
| 572 TextNode(ZoneGrowableArray<TextElement>* elms, |
| 573 RegExpNode* on_success) |
| 574 : SeqRegExpNode(on_success), |
| 575 elms_(elms) { } |
| 576 TextNode(RegExpCharacterClass* that, |
| 577 RegExpNode* on_success) |
| 578 : SeqRegExpNode(on_success), |
| 579 elms_(new(isolate()) ZoneGrowableArray<TextElement>(1)) { |
| 580 elms_->Add(TextElement::CharClass(that)); |
| 581 } |
| 582 virtual void Accept(NodeVisitor* visitor); |
| 583 virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
| 584 virtual intptr_t EatsAtLeast(intptr_t still_to_find, intptr_t budget, |
| 585 bool not_at_start); |
| 586 virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 587 RegExpCompiler* compiler, |
| 588 intptr_t characters_filled_in, |
| 589 bool not_at_start); |
| 590 ZoneGrowableArray<TextElement>* elements() { return elms_; } |
| 591 void MakeCaseIndependent(bool is_one_byte); |
| 592 virtual intptr_t GreedyLoopTextLength(); |
| 593 virtual RegExpNode* GetSuccessorOfOmnivorousTextNode( |
| 594 RegExpCompiler* compiler); |
| 595 virtual void FillInBMInfo(intptr_t offset, |
| 596 intptr_t budget, |
| 597 BoyerMooreLookahead* bm, |
| 598 bool not_at_start); |
| 599 void CalculateOffsets(); |
| 600 virtual RegExpNode* FilterOneByte(intptr_t depth, bool ignore_case); |
| 601 |
| 602 private: |
| 603 enum TextEmitPassType { |
| 604 NON_LATIN1_MATCH, // Check for characters that can't match. |
| 605 SIMPLE_CHARACTER_MATCH, // Case-dependent single character check. |
| 606 NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs. |
| 607 CASE_CHARACTER_MATCH, // Case-independent single character check. |
| 608 CHARACTER_CLASS_MATCH // Character class. |
| 609 }; |
| 610 static bool SkipPass(intptr_t pass, bool ignore_case); |
| 611 static const intptr_t kFirstRealPass = SIMPLE_CHARACTER_MATCH; |
| 612 static const intptr_t kLastPass = CHARACTER_CLASS_MATCH; |
| 613 void TextEmitPass(RegExpCompiler* compiler, |
| 614 TextEmitPassType pass, |
| 615 bool preloaded, |
| 616 Trace* trace, |
| 617 bool first_element_checked, |
| 618 intptr_t* checked_up_to); |
| 619 intptr_t Length(); |
| 620 ZoneGrowableArray<TextElement>* elms_; |
| 621 }; |
| 622 |
| 623 |
| 624 class AssertionNode: public SeqRegExpNode { |
| 625 public: |
| 626 enum AssertionType { |
| 627 AT_END, |
| 628 AT_START, |
| 629 AT_BOUNDARY, |
| 630 AT_NON_BOUNDARY, |
| 631 AFTER_NEWLINE |
| 632 }; |
| 633 static AssertionNode* AtEnd(RegExpNode* on_success) { |
| 634 return new(on_success->isolate()) AssertionNode(AT_END, on_success); |
| 635 } |
| 636 static AssertionNode* AtStart(RegExpNode* on_success) { |
| 637 return new(on_success->isolate()) AssertionNode(AT_START, on_success); |
| 638 } |
| 639 static AssertionNode* AtBoundary(RegExpNode* on_success) { |
| 640 return new(on_success->isolate()) AssertionNode(AT_BOUNDARY, on_success); |
| 641 } |
| 642 static AssertionNode* AtNonBoundary(RegExpNode* on_success) { |
| 643 return new(on_success->isolate()) AssertionNode(AT_NON_BOUNDARY, |
| 644 on_success); |
| 645 } |
| 646 static AssertionNode* AfterNewline(RegExpNode* on_success) { |
| 647 return new(on_success->isolate()) AssertionNode(AFTER_NEWLINE, on_success); |
| 648 } |
| 649 virtual void Accept(NodeVisitor* visitor); |
| 650 virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
| 651 virtual intptr_t EatsAtLeast(intptr_t still_to_find, intptr_t budget, |
| 652 bool not_at_start); |
| 653 virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 654 RegExpCompiler* compiler, |
| 655 intptr_t filled_in, |
| 656 bool not_at_start); |
| 657 virtual void FillInBMInfo(intptr_t offset, |
| 658 intptr_t budget, |
| 659 BoyerMooreLookahead* bm, |
| 660 bool not_at_start); |
| 661 AssertionType assertion_type() { return assertion_type_; } |
| 662 |
| 663 private: |
| 664 void EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace); |
| 665 enum IfPrevious { kIsNonWord, kIsWord }; |
| 666 void BacktrackIfPrevious(RegExpCompiler* compiler, |
| 667 Trace* trace, |
| 668 IfPrevious backtrack_if_previous); |
| 669 AssertionNode(AssertionType t, RegExpNode* on_success) |
| 670 : SeqRegExpNode(on_success), assertion_type_(t) { } |
| 671 AssertionType assertion_type_; |
| 672 }; |
| 673 |
| 674 |
| 675 class BackReferenceNode: public SeqRegExpNode { |
| 676 public: |
| 677 BackReferenceNode(intptr_t start_reg, |
| 678 intptr_t end_reg, |
| 679 RegExpNode* on_success) |
| 680 : SeqRegExpNode(on_success), |
| 681 start_reg_(start_reg), |
| 682 end_reg_(end_reg) { } |
| 683 virtual void Accept(NodeVisitor* visitor); |
| 684 intptr_t start_register() { return start_reg_; } |
| 685 intptr_t end_register() { return end_reg_; } |
| 686 virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
| 687 virtual intptr_t EatsAtLeast(intptr_t still_to_find, |
| 688 intptr_t recursion_depth, |
| 689 bool not_at_start); |
| 690 virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 691 RegExpCompiler* compiler, |
| 692 intptr_t characters_filled_in, |
| 693 bool not_at_start) { |
| 694 return; |
| 695 } |
| 696 virtual void FillInBMInfo(intptr_t offset, |
| 697 intptr_t budget, |
| 698 BoyerMooreLookahead* bm, |
| 699 bool not_at_start); |
| 700 |
| 701 private: |
| 702 intptr_t start_reg_; |
| 703 intptr_t end_reg_; |
| 704 }; |
| 705 |
| 706 |
| 707 class EndNode: public RegExpNode { |
| 708 public: |
| 709 enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS }; |
| 710 explicit EndNode(Action action, Isolate* isolate) |
| 711 : RegExpNode(isolate), action_(action) { } |
| 712 virtual void Accept(NodeVisitor* visitor); |
| 713 virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
| 714 virtual intptr_t EatsAtLeast(intptr_t still_to_find, |
| 715 intptr_t recursion_depth, |
| 716 bool not_at_start) { return 0; } |
| 717 virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 718 RegExpCompiler* compiler, |
| 719 intptr_t characters_filled_in, |
| 720 bool not_at_start) { |
| 721 // Returning 0 from EatsAtLeast should ensure we never get here. |
| 722 UNREACHABLE(); |
| 723 } |
| 724 virtual void FillInBMInfo(intptr_t offset, |
| 725 intptr_t budget, |
| 726 BoyerMooreLookahead* bm, |
| 727 bool not_at_start) { |
| 728 // Returning 0 from EatsAtLeast should ensure we never get here. |
| 729 UNREACHABLE(); |
| 730 } |
| 731 |
| 732 private: |
| 733 Action action_; |
| 734 }; |
| 735 |
| 736 |
| 737 class NegativeSubmatchSuccess: public EndNode { |
| 738 public: |
| 739 NegativeSubmatchSuccess(intptr_t stack_pointer_reg, |
| 740 intptr_t position_reg, |
| 741 intptr_t clear_capture_count, |
| 742 intptr_t clear_capture_start, |
| 743 Isolate* isolate) |
| 744 : EndNode(NEGATIVE_SUBMATCH_SUCCESS, isolate), |
| 745 stack_pointer_register_(stack_pointer_reg), |
| 746 current_position_register_(position_reg), |
| 747 clear_capture_count_(clear_capture_count), |
| 748 clear_capture_start_(clear_capture_start) { } |
| 749 virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
| 750 |
| 751 private: |
| 752 intptr_t stack_pointer_register_; |
| 753 intptr_t current_position_register_; |
| 754 intptr_t clear_capture_count_; |
| 755 intptr_t clear_capture_start_; |
| 756 }; |
| 757 |
| 758 |
| 759 class Guard: public ZoneAllocated { |
| 760 public: |
| 761 enum Relation { LT, GEQ }; |
| 762 Guard(intptr_t reg, Relation op, intptr_t value) |
| 763 : reg_(reg), |
| 764 op_(op), |
| 765 value_(value) { } |
| 766 intptr_t reg() { return reg_; } |
| 767 Relation op() { return op_; } |
| 768 intptr_t value() { return value_; } |
| 769 |
| 770 private: |
| 771 intptr_t reg_; |
| 772 Relation op_; |
| 773 intptr_t value_; |
| 774 }; |
| 775 |
| 776 |
| 777 class GuardedAlternative { |
| 778 public: |
| 779 explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { } |
| 780 void AddGuard(Guard* guard, Isolate* isolate); |
| 781 RegExpNode* node() { return node_; } |
| 782 void set_node(RegExpNode* node) { node_ = node; } |
| 783 ZoneGrowableArray<Guard*>* guards() { return guards_; } |
| 784 |
| 785 private: |
| 786 RegExpNode* node_; |
| 787 ZoneGrowableArray<Guard*>* guards_; |
| 788 |
| 789 DISALLOW_ALLOCATION(); |
| 790 }; |
| 791 |
| 792 |
| 793 class AlternativeGeneration; |
| 794 |
| 795 |
| 796 class ChoiceNode: public RegExpNode { |
| 797 public: |
| 798 explicit ChoiceNode(intptr_t expected_size, Isolate* isolate) |
| 799 : RegExpNode(isolate), |
| 800 alternatives_(new(isolate) |
| 801 ZoneGrowableArray<GuardedAlternative>(expected_size)), |
| 802 not_at_start_(false), |
| 803 being_calculated_(false) { } |
| 804 virtual void Accept(NodeVisitor* visitor); |
| 805 void AddAlternative(GuardedAlternative node) { |
| 806 alternatives()->Add(node); |
| 807 } |
| 808 ZoneGrowableArray<GuardedAlternative>* alternatives() { |
| 809 return alternatives_; |
| 810 } |
| 811 virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
| 812 virtual intptr_t EatsAtLeast(intptr_t still_to_find, intptr_t budget, |
| 813 bool not_at_start); |
| 814 intptr_t EatsAtLeastHelper(intptr_t still_to_find, |
| 815 intptr_t budget, |
| 816 RegExpNode* ignore_this_node, |
| 817 bool not_at_start); |
| 818 virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 819 RegExpCompiler* compiler, |
| 820 intptr_t characters_filled_in, |
| 821 bool not_at_start); |
| 822 virtual void FillInBMInfo(intptr_t offset, |
| 823 intptr_t budget, |
| 824 BoyerMooreLookahead* bm, |
| 825 bool not_at_start); |
| 826 |
| 827 bool being_calculated() { return being_calculated_; } |
| 828 bool not_at_start() { return not_at_start_; } |
| 829 void set_not_at_start() { not_at_start_ = true; } |
| 830 void set_being_calculated(bool b) { being_calculated_ = b; } |
| 831 virtual bool try_to_emit_quick_check_for_alternative(bool is_first) { |
| 832 return true; |
| 833 } |
| 834 virtual RegExpNode* FilterOneByte(intptr_t depth, bool ignore_case); |
| 835 |
| 836 protected: |
| 837 intptr_t GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative); |
| 838 ZoneGrowableArray<GuardedAlternative>* alternatives_; |
| 839 |
| 840 private: |
| 841 friend class Analysis; |
| 842 void GenerateGuard(RegExpMacroAssembler* macro_assembler, |
| 843 Guard* guard, |
| 844 Trace* trace); |
| 845 intptr_t CalculatePreloadCharacters(RegExpCompiler* compiler, |
| 846 intptr_t eats_at_least); |
| 847 void EmitOutOfLineContinuation(RegExpCompiler* compiler, |
| 848 Trace* trace, |
| 849 GuardedAlternative alternative, |
| 850 AlternativeGeneration* alt_gen, |
| 851 intptr_t preload_characters, |
| 852 bool next_expects_preload); |
| 853 void SetUpPreLoad(RegExpCompiler* compiler, |
| 854 Trace* current_trace, |
| 855 PreloadState* preloads); |
| 856 void AssertGuardsMentionRegisters(Trace* trace); |
| 857 intptr_t EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler, |
| 858 Trace* trace); |
| 859 Trace* EmitGreedyLoop(RegExpCompiler* compiler, |
| 860 Trace* trace, |
| 861 AlternativeGenerationList* alt_gens, |
| 862 PreloadState* preloads, |
| 863 GreedyLoopState* greedy_loop_state, |
| 864 intptr_t text_length); |
| 865 void EmitChoices(RegExpCompiler* compiler, |
| 866 AlternativeGenerationList* alt_gens, |
| 867 intptr_t first_choice, |
| 868 Trace* trace, |
| 869 PreloadState* preloads); |
| 870 // If true, this node is never checked at the start of the input. |
| 871 // Allows a new trace to start with at_start() set to false. |
| 872 bool not_at_start_; |
| 873 bool being_calculated_; |
| 874 }; |
| 875 |
| 876 |
| 877 class NegativeLookaheadChoiceNode: public ChoiceNode { |
| 878 public: |
| 879 explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail, |
| 880 GuardedAlternative then_do_this, |
| 881 Isolate* isolate) |
| 882 : ChoiceNode(2, isolate) { |
| 883 AddAlternative(this_must_fail); |
| 884 AddAlternative(then_do_this); |
| 885 } |
| 886 virtual intptr_t EatsAtLeast(intptr_t still_to_find, intptr_t budget, |
| 887 bool not_at_start); |
| 888 virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 889 RegExpCompiler* compiler, |
| 890 intptr_t characters_filled_in, |
| 891 bool not_at_start); |
| 892 virtual void FillInBMInfo(intptr_t offset, |
| 893 intptr_t budget, |
| 894 BoyerMooreLookahead* bm, |
| 895 bool not_at_start) { |
| 896 (*alternatives_)[1].node()->FillInBMInfo( |
| 897 offset, budget - 1, bm, not_at_start); |
| 898 if (offset == 0) set_bm_info(not_at_start, bm); |
| 899 } |
| 900 // For a negative lookahead we don't emit the quick check for the |
| 901 // alternative that is expected to fail. This is because quick check code |
| 902 // starts by loading enough characters for the alternative that takes fewest |
| 903 // characters, but on a negative lookahead the negative branch did not take |
| 904 // part in that calculation (EatsAtLeast) so the assumptions don't hold. |
| 905 virtual bool try_to_emit_quick_check_for_alternative(bool is_first) { |
| 906 return !is_first; |
| 907 } |
| 908 virtual RegExpNode* FilterOneByte(intptr_t depth, bool ignore_case); |
| 909 }; |
| 910 |
| 911 |
| 912 class LoopChoiceNode: public ChoiceNode { |
| 913 public: |
| 914 explicit LoopChoiceNode(bool body_can_be_zero_length, Isolate* isolate) |
| 915 : ChoiceNode(2, isolate), |
| 916 loop_node_(NULL), |
| 917 continue_node_(NULL), |
| 918 body_can_be_zero_length_(body_can_be_zero_length) { } |
| 919 void AddLoopAlternative(GuardedAlternative alt); |
| 920 void AddContinueAlternative(GuardedAlternative alt); |
| 921 virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
| 922 virtual intptr_t EatsAtLeast(intptr_t still_to_find, intptr_t budget, |
| 923 bool not_at_start); |
| 924 virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 925 RegExpCompiler* compiler, |
| 926 intptr_t characters_filled_in, |
| 927 bool not_at_start); |
| 928 virtual void FillInBMInfo(intptr_t offset, |
| 929 intptr_t budget, |
| 930 BoyerMooreLookahead* bm, |
| 931 bool not_at_start); |
| 932 RegExpNode* loop_node() { return loop_node_; } |
| 933 RegExpNode* continue_node() { return continue_node_; } |
| 934 bool body_can_be_zero_length() { return body_can_be_zero_length_; } |
| 935 virtual void Accept(NodeVisitor* visitor); |
| 936 virtual RegExpNode* FilterOneByte(intptr_t depth, bool ignore_case); |
| 937 |
| 938 private: |
| 939 // AddAlternative is made private for loop nodes because alternatives |
| 940 // should not be added freely, we need to keep track of which node |
| 941 // goes back to the node itself. |
| 942 void AddAlternative(GuardedAlternative node) { |
| 943 ChoiceNode::AddAlternative(node); |
| 944 } |
| 945 |
| 946 RegExpNode* loop_node_; |
| 947 RegExpNode* continue_node_; |
| 948 bool body_can_be_zero_length_; |
| 949 }; |
| 950 |
| 951 |
| 952 // Improve the speed that we scan for an initial point where a non-anchored |
| 953 // regexp can match by using a Boyer-Moore-like table. This is done by |
| 954 // identifying non-greedy non-capturing loops in the nodes that eat any |
| 955 // character one at a time. For example in the middle of the regexp |
| 956 // /foo[\s\S]*?bar/ we find such a loop. There is also such a loop implicitly |
| 957 // inserted at the start of any non-anchored regexp. |
| 958 // |
| 959 // When we have found such a loop we look ahead in the nodes to find the set of |
| 960 // characters that can come at given distances. For example for the regexp |
| 961 // /.?foo/ we know that there are at least 3 characters ahead of us, and the |
| 962 // sets of characters that can occur are [any, [f, o], [o]]. We find a range in |
| 963 // the lookahead info where the set of characters is reasonably constrained. In |
| 964 // our example this is from index 1 to 2 (0 is not constrained). We can now |
| 965 // look 3 characters ahead and if we don't find one of [f, o] (the union of |
| 966 // [f, o] and [o]) then we can skip forwards by the range size (in this case 2). |
| 967 // |
| 968 // For Unicode input strings we do the same, but modulo 128. |
| 969 // |
| 970 // We also look at the first string fed to the regexp and use that to get a hint |
| 971 // of the character frequencies in the inputs. This affects the assessment of |
| 972 // whether the set of characters is 'reasonably constrained'. |
| 973 // |
| 974 // We also have another lookahead mechanism (called quick check in the code), |
| 975 // which uses a wide load of multiple characters followed by a mask and compare |
| 976 // to determine whether a match is possible at this point. |
| 977 enum ContainedInLattice { |
| 978 kNotYet = 0, |
| 979 kLatticeIn = 1, |
| 980 kLatticeOut = 2, |
| 981 kLatticeUnknown = 3 // Can also mean both in and out. |
| 982 }; |
| 983 |
| 984 |
| 985 inline ContainedInLattice Combine(ContainedInLattice a, ContainedInLattice b) { |
| 986 return static_cast<ContainedInLattice>(a | b); |
| 987 } |
| 988 |
| 989 |
| 990 ContainedInLattice AddRange(ContainedInLattice a, |
| 991 const intptr_t* ranges, |
| 992 intptr_t ranges_size, |
| 993 Interval new_range); |
| 994 |
| 995 |
| 996 class BoyerMoorePositionInfo : public ZoneAllocated { |
| 997 public: |
| 998 explicit BoyerMoorePositionInfo(Isolate* isolate) |
| 999 : map_(new(isolate) ZoneGrowableArray<bool>(kMapSize)), |
| 1000 map_count_(0), |
| 1001 w_(kNotYet), |
| 1002 s_(kNotYet), |
| 1003 d_(kNotYet), |
| 1004 surrogate_(kNotYet) { |
| 1005 for (intptr_t i = 0; i < kMapSize; i++) { |
| 1006 map_->Add(false); |
| 1007 } |
| 1008 } |
| 1009 |
| 1010 bool& at(intptr_t i) { return (*map_)[i]; } |
| 1011 |
| 1012 static const intptr_t kMapSize = 128; |
| 1013 static const intptr_t kMask = kMapSize - 1; |
| 1014 |
| 1015 intptr_t map_count() const { return map_count_; } |
| 1016 |
| 1017 void Set(intptr_t character); |
| 1018 void SetInterval(const Interval& interval); |
| 1019 void SetAll(); |
| 1020 bool is_non_word() { return w_ == kLatticeOut; } |
| 1021 bool is_word() { return w_ == kLatticeIn; } |
| 1022 |
| 1023 private: |
| 1024 ZoneGrowableArray<bool>* map_; |
| 1025 intptr_t map_count_; // Number of set bits in the map. |
| 1026 ContainedInLattice w_; // The \w character class. |
| 1027 ContainedInLattice s_; // The \s character class. |
| 1028 ContainedInLattice d_; // The \d character class. |
| 1029 ContainedInLattice surrogate_; // Surrogate UTF-16 code units. |
| 1030 }; |
| 1031 |
| 1032 |
| 1033 class BoyerMooreLookahead : public ZoneAllocated{ |
| 1034 public: |
| 1035 BoyerMooreLookahead(intptr_t length, RegExpCompiler* compiler, |
| 1036 Isolate* Isolate); |
| 1037 |
| 1038 intptr_t length() { return length_; } |
| 1039 intptr_t max_char() { return max_char_; } |
| 1040 RegExpCompiler* compiler() { return compiler_; } |
| 1041 |
| 1042 intptr_t Count(intptr_t map_number) { |
| 1043 return bitmaps_->At(map_number)->map_count(); |
| 1044 } |
| 1045 |
| 1046 BoyerMoorePositionInfo* at(intptr_t i) { return bitmaps_->At(i); } |
| 1047 |
| 1048 void Set(intptr_t map_number, intptr_t character) { |
| 1049 if (character > max_char_) return; |
| 1050 BoyerMoorePositionInfo* info = bitmaps_->At(map_number); |
| 1051 info->Set(character); |
| 1052 } |
| 1053 |
| 1054 void SetInterval(intptr_t map_number, const Interval& interval) { |
| 1055 if (interval.from() > max_char_) return; |
| 1056 BoyerMoorePositionInfo* info = bitmaps_->At(map_number); |
| 1057 if (interval.to() > max_char_) { |
| 1058 info->SetInterval(Interval(interval.from(), max_char_)); |
| 1059 } else { |
| 1060 info->SetInterval(interval); |
| 1061 } |
| 1062 } |
| 1063 |
| 1064 void SetAll(intptr_t map_number) { |
| 1065 bitmaps_->At(map_number)->SetAll(); |
| 1066 } |
| 1067 |
| 1068 void SetRest(intptr_t from_map) { |
| 1069 for (intptr_t i = from_map; i < length_; i++) SetAll(i); |
| 1070 } |
| 1071 void EmitSkipInstructions(RegExpMacroAssembler* masm); |
| 1072 |
| 1073 private: |
| 1074 // This is the value obtained by EatsAtLeast. If we do not have at least this |
| 1075 // many characters left in the sample string then the match is bound to fail. |
| 1076 // Therefore it is OK to read a character this far ahead of the current match |
| 1077 // point. |
| 1078 intptr_t length_; |
| 1079 RegExpCompiler* compiler_; |
| 1080 // 0xff for Latin1, 0xffff for UTF-16. |
| 1081 intptr_t max_char_; |
| 1082 ZoneGrowableArray<BoyerMoorePositionInfo*>* bitmaps_; |
| 1083 |
| 1084 intptr_t GetSkipTable(intptr_t min_lookahead, |
| 1085 intptr_t max_lookahead, |
| 1086 const TypedData& boolean_skip_table); |
| 1087 bool FindWorthwhileInterval(intptr_t* from, intptr_t* to); |
| 1088 intptr_t FindBestInterval( |
| 1089 intptr_t max_number_of_chars, |
| 1090 intptr_t old_biggest_points, |
| 1091 intptr_t* from, intptr_t* to); |
| 1092 }; |
| 1093 |
| 1094 |
| 1095 // There are many ways to generate code for a node. This class encapsulates |
| 1096 // the current way we should be generating. In other words it encapsulates |
| 1097 // the current state of the code generator. The effect of this is that we |
| 1098 // generate code for paths that the matcher can take through the regular |
| 1099 // expression. A given node in the regexp can be code-generated several times |
| 1100 // as it can be part of several traces. For example for the regexp: |
| 1101 // /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part |
| 1102 // of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code |
| 1103 // to match foo is generated only once (the traces have a common prefix). The |
| 1104 // code to store the capture is deferred and generated (twice) after the places |
| 1105 // where baz has been matched. |
| 1106 class Trace { |
| 1107 public: |
| 1108 // A value for a property that is either known to be true, know to be false, |
| 1109 // or not known. |
| 1110 enum TriBool { |
| 1111 UNKNOWN = -1, FALSE_VALUE = 0, TRUE_VALUE = 1 |
| 1112 }; |
| 1113 |
| 1114 class DeferredAction { |
| 1115 public: |
| 1116 DeferredAction(ActionNode::ActionType action_type, intptr_t reg) |
| 1117 : action_type_(action_type), reg_(reg), next_(NULL) { } |
| 1118 DeferredAction* next() { return next_; } |
| 1119 bool Mentions(intptr_t reg); |
| 1120 intptr_t reg() { return reg_; } |
| 1121 ActionNode::ActionType action_type() { return action_type_; } |
| 1122 private: |
| 1123 ActionNode::ActionType action_type_; |
| 1124 intptr_t reg_; |
| 1125 DeferredAction* next_; |
| 1126 friend class Trace; |
| 1127 |
| 1128 DISALLOW_ALLOCATION(); |
| 1129 }; |
| 1130 |
| 1131 class DeferredCapture : public DeferredAction { |
| 1132 public: |
| 1133 DeferredCapture(intptr_t reg, bool is_capture, Trace* trace) |
| 1134 : DeferredAction(ActionNode::STORE_POSITION, reg), |
| 1135 cp_offset_(trace->cp_offset()), |
| 1136 is_capture_(is_capture) { } |
| 1137 intptr_t cp_offset() { return cp_offset_; } |
| 1138 bool is_capture() { return is_capture_; } |
| 1139 private: |
| 1140 intptr_t cp_offset_; |
| 1141 bool is_capture_; |
| 1142 void set_cp_offset(intptr_t cp_offset) { cp_offset_ = cp_offset; } |
| 1143 }; |
| 1144 |
| 1145 class DeferredSetRegister : public DeferredAction { |
| 1146 public: |
| 1147 DeferredSetRegister(intptr_t reg, intptr_t value) |
| 1148 : DeferredAction(ActionNode::SET_REGISTER, reg), |
| 1149 value_(value) { } |
| 1150 intptr_t value() { return value_; } |
| 1151 private: |
| 1152 intptr_t value_; |
| 1153 }; |
| 1154 |
| 1155 class DeferredClearCaptures : public DeferredAction { |
| 1156 public: |
| 1157 explicit DeferredClearCaptures(Interval range) |
| 1158 : DeferredAction(ActionNode::CLEAR_CAPTURES, -1), |
| 1159 range_(range) { } |
| 1160 Interval range() { return range_; } |
| 1161 private: |
| 1162 Interval range_; |
| 1163 }; |
| 1164 |
| 1165 class DeferredIncrementRegister : public DeferredAction { |
| 1166 public: |
| 1167 explicit DeferredIncrementRegister(intptr_t reg) |
| 1168 : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { } |
| 1169 }; |
| 1170 |
| 1171 Trace() |
| 1172 : cp_offset_(0), |
| 1173 actions_(NULL), |
| 1174 backtrack_(NULL), |
| 1175 stop_node_(NULL), |
| 1176 loop_label_(NULL), |
| 1177 characters_preloaded_(0), |
| 1178 bound_checked_up_to_(0), |
| 1179 flush_budget_(100), |
| 1180 at_start_(UNKNOWN) { } |
| 1181 |
| 1182 // End the trace. This involves flushing the deferred actions in the trace |
| 1183 // and pushing a backtrack location onto the backtrack stack. Once this is |
| 1184 // done we can start a new trace or go to one that has already been |
| 1185 // generated. |
| 1186 void Flush(RegExpCompiler* compiler, RegExpNode* successor); |
| 1187 intptr_t cp_offset() { return cp_offset_; } |
| 1188 DeferredAction* actions() { return actions_; } |
| 1189 // A trivial trace is one that has no deferred actions or other state that |
| 1190 // affects the assumptions used when generating code. There is no recorded |
| 1191 // backtrack location in a trivial trace, so with a trivial trace we will |
| 1192 // generate code that, on a failure to match, gets the backtrack location |
| 1193 // from the backtrack stack rather than using a direct jump instruction. We |
| 1194 // always start code generation with a trivial trace and non-trivial traces |
| 1195 // are created as we emit code for nodes or add to the list of deferred |
| 1196 // actions in the trace. The location of the code generated for a node using |
| 1197 // a trivial trace is recorded in a label in the node so that gotos can be |
| 1198 // generated to that code. |
| 1199 bool is_trivial() { |
| 1200 return backtrack_ == NULL && |
| 1201 actions_ == NULL && |
| 1202 cp_offset_ == 0 && |
| 1203 characters_preloaded_ == 0 && |
| 1204 bound_checked_up_to_ == 0 && |
| 1205 quick_check_performed_.characters() == 0 && |
| 1206 at_start_ == UNKNOWN; |
| 1207 } |
| 1208 TriBool at_start() { return at_start_; } |
| 1209 void set_at_start(bool at_start) { |
| 1210 at_start_ = at_start ? TRUE_VALUE : FALSE_VALUE; |
| 1211 } |
| 1212 BlockLabel* backtrack() { return backtrack_; } |
| 1213 BlockLabel* loop_label() { return loop_label_; } |
| 1214 RegExpNode* stop_node() { return stop_node_; } |
| 1215 intptr_t characters_preloaded() { return characters_preloaded_; } |
| 1216 intptr_t bound_checked_up_to() { return bound_checked_up_to_; } |
| 1217 intptr_t flush_budget() { return flush_budget_; } |
| 1218 QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; } |
| 1219 bool mentions_reg(intptr_t reg); |
| 1220 // Returns true if a deferred position store exists to the specified |
| 1221 // register and stores the offset in the out-parameter. Otherwise |
| 1222 // returns false. |
| 1223 bool GetStoredPosition(intptr_t reg, intptr_t* cp_offset); |
| 1224 // These set methods and AdvanceCurrentPositionInTrace should be used only on |
| 1225 // new traces - the intention is that traces are immutable after creation. |
| 1226 void add_action(DeferredAction* new_action) { |
| 1227 ASSERT(new_action->next_ == NULL); |
| 1228 new_action->next_ = actions_; |
| 1229 actions_ = new_action; |
| 1230 } |
| 1231 void set_backtrack(BlockLabel* backtrack) { backtrack_ = backtrack; } |
| 1232 void set_stop_node(RegExpNode* node) { stop_node_ = node; } |
| 1233 void set_loop_label(BlockLabel* label) { loop_label_ = label; } |
| 1234 void set_characters_preloaded(intptr_t count) { |
| 1235 characters_preloaded_ = count; |
| 1236 } |
| 1237 void set_bound_checked_up_to(intptr_t to) { bound_checked_up_to_ = to; } |
| 1238 void set_flush_budget(intptr_t to) { flush_budget_ = to; } |
| 1239 void set_quick_check_performed(QuickCheckDetails* d) { |
| 1240 quick_check_performed_ = *d; |
| 1241 } |
| 1242 void InvalidateCurrentCharacter(); |
| 1243 void AdvanceCurrentPositionInTrace(intptr_t by, RegExpCompiler* compiler); |
| 1244 |
| 1245 private: |
| 1246 intptr_t FindAffectedRegisters(OutSet* affected_registers, Isolate* isolate); |
| 1247 void PerformDeferredActions(RegExpMacroAssembler* macro, |
| 1248 intptr_t max_register, |
| 1249 const OutSet& affected_registers, |
| 1250 OutSet* registers_to_pop, |
| 1251 OutSet* registers_to_clear, |
| 1252 Isolate* isolate); |
| 1253 void RestoreAffectedRegisters(RegExpMacroAssembler* macro, |
| 1254 intptr_t max_register, |
| 1255 const OutSet& registers_to_pop, |
| 1256 const OutSet& registers_to_clear); |
| 1257 intptr_t cp_offset_; |
| 1258 DeferredAction* actions_; |
| 1259 BlockLabel* backtrack_; |
| 1260 RegExpNode* stop_node_; |
| 1261 BlockLabel* loop_label_; |
| 1262 intptr_t characters_preloaded_; |
| 1263 intptr_t bound_checked_up_to_; |
| 1264 QuickCheckDetails quick_check_performed_; |
| 1265 intptr_t flush_budget_; |
| 1266 TriBool at_start_; |
| 1267 |
| 1268 DISALLOW_ALLOCATION(); |
| 1269 }; |
| 1270 |
| 1271 |
| 1272 class GreedyLoopState { |
| 1273 public: |
| 1274 explicit GreedyLoopState(bool not_at_start); |
| 1275 |
| 1276 BlockLabel* label() { return &label_; } |
| 1277 Trace* counter_backtrack_trace() { return &counter_backtrack_trace_; } |
| 1278 |
| 1279 private: |
| 1280 BlockLabel label_; |
| 1281 Trace counter_backtrack_trace_; |
| 1282 }; |
| 1283 |
| 1284 |
| 1285 struct PreloadState { |
| 1286 static const intptr_t kEatsAtLeastNotYetInitialized = -1; |
| 1287 bool preload_is_current_; |
| 1288 bool preload_has_checked_bounds_; |
| 1289 intptr_t preload_characters_; |
| 1290 intptr_t eats_at_least_; |
| 1291 void init() { |
| 1292 eats_at_least_ = kEatsAtLeastNotYetInitialized; |
| 1293 } |
| 1294 |
| 1295 DISALLOW_ALLOCATION(); |
| 1296 }; |
| 1297 |
| 1298 |
| 1299 class NodeVisitor : public ValueObject { |
| 1300 public: |
| 1301 virtual ~NodeVisitor() { } |
| 1302 #define DECLARE_VISIT(Type) \ |
| 1303 virtual void Visit##Type(Type##Node* that) = 0; |
| 1304 FOR_EACH_NODE_TYPE(DECLARE_VISIT) |
| 1305 #undef DECLARE_VISIT |
| 1306 virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); } |
| 1307 }; |
| 1308 |
| 1309 |
| 1310 // Assertion propagation moves information about assertions such as |
| 1311 // \b to the affected nodes. For instance, in /.\b./ information must |
| 1312 // be propagated to the first '.' that whatever follows needs to know |
| 1313 // if it matched a word or a non-word, and to the second '.' that it |
| 1314 // has to check if it succeeds a word or non-word. In this case the |
| 1315 // result will be something like: |
| 1316 // |
| 1317 // +-------+ +------------+ |
| 1318 // | . | | . | |
| 1319 // +-------+ ---> +------------+ |
| 1320 // | word? | | check word | |
| 1321 // +-------+ +------------+ |
| 1322 class Analysis: public NodeVisitor { |
| 1323 public: |
| 1324 Analysis(bool ignore_case, bool is_one_byte) |
| 1325 : ignore_case_(ignore_case), |
| 1326 is_one_byte_(is_one_byte), |
| 1327 error_message_(NULL) { } |
| 1328 void EnsureAnalyzed(RegExpNode* node); |
| 1329 |
| 1330 #define DECLARE_VISIT(Type) \ |
| 1331 virtual void Visit##Type(Type##Node* that); |
| 1332 FOR_EACH_NODE_TYPE(DECLARE_VISIT) |
| 1333 #undef DECLARE_VISIT |
| 1334 virtual void VisitLoopChoice(LoopChoiceNode* that); |
| 1335 |
| 1336 bool has_failed() { return error_message_ != NULL; } |
| 1337 const char* error_message() { |
| 1338 ASSERT(error_message_ != NULL); |
| 1339 return error_message_; |
| 1340 } |
| 1341 void fail(const char* error_message) { |
| 1342 error_message_ = error_message; |
| 1343 } |
| 1344 |
| 1345 private: |
| 1346 bool ignore_case_; |
| 1347 bool is_one_byte_; |
| 1348 const char* error_message_; |
| 1349 |
| 1350 DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis); |
| 1351 }; |
| 1352 |
| 1353 |
| 1354 struct RegExpCompileData : public ZoneAllocated { |
| 1355 RegExpCompileData() |
| 1356 : tree(NULL), |
| 1357 node(NULL), |
| 1358 simple(true), |
| 1359 contains_anchor(false), |
| 1360 error(String::Handle(String::null())), |
| 1361 capture_count(0) { } |
| 1362 RegExpTree* tree; |
| 1363 RegExpNode* node; |
| 1364 bool simple; |
| 1365 bool contains_anchor; |
| 1366 String& error; |
| 1367 intptr_t capture_count; |
| 1368 }; |
| 1369 |
| 1370 |
| 1371 class RegExpEngine: public AllStatic { |
| 1372 public: |
| 1373 struct CompilationResult { |
| 1374 explicit CompilationResult(const char* error_message) |
| 1375 : macro_assembler(NULL), |
| 1376 graph_entry(NULL), |
| 1377 num_blocks(-1), |
| 1378 num_stack_locals(-1), |
| 1379 error_message(error_message) {} |
| 1380 CompilationResult(IRRegExpMacroAssembler* macro_assembler, |
| 1381 GraphEntryInstr* graph_entry, |
| 1382 intptr_t num_blocks, |
| 1383 intptr_t num_stack_locals) |
| 1384 : macro_assembler(macro_assembler), |
| 1385 graph_entry(graph_entry), |
| 1386 num_blocks(num_blocks), |
| 1387 num_stack_locals(num_stack_locals), |
| 1388 error_message(NULL) {} |
| 1389 |
| 1390 IRRegExpMacroAssembler* macro_assembler; |
| 1391 GraphEntryInstr* graph_entry; |
| 1392 const intptr_t num_blocks; |
| 1393 const intptr_t num_stack_locals; |
| 1394 |
| 1395 const char* error_message; |
| 1396 }; |
| 1397 |
| 1398 static CompilationResult Compile( |
| 1399 RegExpCompileData* input, |
| 1400 const ParsedFunction* parsed_function, |
| 1401 const ZoneGrowableArray<const ICData*>& ic_data_array); |
| 1402 |
| 1403 static RawJSRegExp* CreateJSRegExp(Isolate* isolate, |
| 1404 const String& pattern, |
| 1405 bool multi_line, |
| 1406 bool ignore_case); |
| 1407 |
| 1408 static void DotPrint(const char* label, RegExpNode* node, bool ignore_case); |
| 1409 }; |
| 1410 |
| 1411 } // namespace dart |
| 1412 |
| 1413 #endif // VM_REGEXP_H_ |
OLD | NEW |