1 // Copyright 2012 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 #ifndef V8_JSREGEXP_H_ 29 #define V8_JSREGEXP_H_ 30 31 #include "allocation.h" 32 #include "assembler.h" 33 #include "zone-inl.h" 34 35 namespace v8 { 36 namespace internal { 37 38 class NodeVisitor; 39 class RegExpCompiler; 40 class RegExpMacroAssembler; 41 class RegExpNode; 42 class RegExpTree; 43 44 class RegExpImpl { 45 public: 46 // Whether V8 is compiled with native regexp support or not. 47 static bool UsesNativeRegExp() { 48 #ifdef V8_INTERPRETED_REGEXP 49 return false; 50 #else 51 return true; 52 #endif 53 } 54 55 // Creates a regular expression literal in the old space. 56 // This function calls the garbage collector if necessary. 57 static Handle<Object> CreateRegExpLiteral(Handle<JSFunction> constructor, 58 Handle<String> pattern, 59 Handle<String> flags, 60 bool* has_pending_exception); 61 62 // Returns a string representation of a regular expression. 63 // Implements RegExp.prototype.toString, see ECMA-262 section 15.10.6.4. 64 // This function calls the garbage collector if necessary. 65 static Handle<String> ToString(Handle<Object> value); 66 67 // Parses the RegExp pattern and prepares the JSRegExp object with 68 // generic data and choice of implementation - as well as what 69 // the implementation wants to store in the data field. 70 // Returns false if compilation fails. 71 static Handle<Object> Compile(Handle<JSRegExp> re, 72 Handle<String> pattern, 73 Handle<String> flags); 74 75 // See ECMA-262 section 15.10.6.2. 76 // This function calls the garbage collector if necessary. 77 static Handle<Object> Exec(Handle<JSRegExp> regexp, 78 Handle<String> subject, 79 int index, 80 Handle<JSArray> lastMatchInfo); 81 82 // Prepares a JSRegExp object with Irregexp-specific data. 83 static void IrregexpInitialize(Handle<JSRegExp> re, 84 Handle<String> pattern, 85 JSRegExp::Flags flags, 86 int capture_register_count); 87 88 89 static void AtomCompile(Handle<JSRegExp> re, 90 Handle<String> pattern, 91 JSRegExp::Flags flags, 92 Handle<String> match_pattern); 93 94 static Handle<Object> AtomExec(Handle<JSRegExp> regexp, 95 Handle<String> subject, 96 int index, 97 Handle<JSArray> lastMatchInfo); 98 99 enum IrregexpResult { RE_FAILURE = 0, RE_SUCCESS = 1, RE_EXCEPTION = -1 }; 100 101 // Prepare a RegExp for being executed one or more times (using 102 // IrregexpExecOnce) on the subject. 103 // This ensures that the regexp is compiled for the subject, and that 104 // the subject is flat. 105 // Returns the number of integer spaces required by IrregexpExecOnce 106 // as its "registers" argument. If the regexp cannot be compiled, 107 // an exception is set as pending, and this function returns negative. 108 static int IrregexpPrepare(Handle<JSRegExp> regexp, 109 Handle<String> subject); 110 111 // Execute a regular expression once on the subject, starting from 112 // character "index". 113 // If successful, returns RE_SUCCESS and set the capture positions 114 // in the first registers. 115 // If matching fails, returns RE_FAILURE. 116 // If execution fails, sets a pending exception and returns RE_EXCEPTION. 117 static IrregexpResult IrregexpExecOnce(Handle<JSRegExp> regexp, 118 Handle<String> subject, 119 int index, 120 Vector<int> registers); 121 122 // Execute an Irregexp bytecode pattern. 123 // On a successful match, the result is a JSArray containing 124 // captured positions. On a failure, the result is the null value. 125 // Returns an empty handle in case of an exception. 126 static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp, 127 Handle<String> subject, 128 int index, 129 Handle<JSArray> lastMatchInfo); 130 131 // Array index in the lastMatchInfo array. 132 static const int kLastCaptureCount = 0; 133 static const int kLastSubject = 1; 134 static const int kLastInput = 2; 135 static const int kFirstCapture = 3; 136 static const int kLastMatchOverhead = 3; 137 138 // Direct offset into the lastMatchInfo array. 139 static const int kLastCaptureCountOffset = 140 FixedArray::kHeaderSize + kLastCaptureCount * kPointerSize; 141 static const int kLastSubjectOffset = 142 FixedArray::kHeaderSize + kLastSubject * kPointerSize; 143 static const int kLastInputOffset = 144 FixedArray::kHeaderSize + kLastInput * kPointerSize; 145 static const int kFirstCaptureOffset = 146 FixedArray::kHeaderSize + kFirstCapture * kPointerSize; 147 148 // Used to access the lastMatchInfo array. 149 static int GetCapture(FixedArray* array, int index) { 150 return Smi::cast(array->get(index + kFirstCapture))->value(); 151 } 152 153 static void SetLastCaptureCount(FixedArray* array, int to) { 154 array->set(kLastCaptureCount, Smi::FromInt(to)); 155 } 156 157 static void SetLastSubject(FixedArray* array, String* to) { 158 array->set(kLastSubject, to); 159 } 160 161 static void SetLastInput(FixedArray* array, String* to) { 162 array->set(kLastInput, to); 163 } 164 165 static void SetCapture(FixedArray* array, int index, int to) { 166 array->set(index + kFirstCapture, Smi::FromInt(to)); 167 } 168 169 static int GetLastCaptureCount(FixedArray* array) { 170 return Smi::cast(array->get(kLastCaptureCount))->value(); 171 } 172 173 // For acting on the JSRegExp data FixedArray. 174 static int IrregexpMaxRegisterCount(FixedArray* re); 175 static void SetIrregexpMaxRegisterCount(FixedArray* re, int value); 176 static int IrregexpNumberOfCaptures(FixedArray* re); 177 static int IrregexpNumberOfRegisters(FixedArray* re); 178 static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii); 179 static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii); 180 181 // Limit the space regexps take up on the heap. In order to limit this we 182 // would like to keep track of the amount of regexp code on the heap. This 183 // is not tracked, however. As a conservative approximation we track the 184 // total regexp code compiled including code that has subsequently been freed 185 // and the total executable memory at any point. 186 static const int kRegExpExecutableMemoryLimit = 16 * MB; 187 static const int kRegWxpCompiledLimit = 1 * MB; 188 189 private: 190 static String* last_ascii_string_; 191 static String* two_byte_cached_string_; 192 193 static bool CompileIrregexp(Handle<JSRegExp> re, bool is_ascii); 194 static inline bool EnsureCompiledIrregexp(Handle<JSRegExp> re, bool is_ascii); 195 196 197 // Set the subject cache. The previous string buffer is not deleted, so the 198 // caller should ensure that it doesn't leak. 199 static void SetSubjectCache(String* subject, 200 char* utf8_subject, 201 int uft8_length, 202 int character_position, 203 int utf8_position); 204 205 // A one element cache of the last utf8_subject string and its length. The 206 // subject JS String object is cached in the heap. We also cache a 207 // translation between position and utf8 position. 208 static char* utf8_subject_cache_; 209 static int utf8_length_cache_; 210 static int utf8_position_; 211 static int character_position_; 212 }; 213 214 215 // Represents the location of one element relative to the intersection of 216 // two sets. Corresponds to the four areas of a Venn diagram. 217 enum ElementInSetsRelation { 218 kInsideNone = 0, 219 kInsideFirst = 1, 220 kInsideSecond = 2, 221 kInsideBoth = 3 222 }; 223 224 225 // Represents the relation of two sets. 226 // Sets can be either disjoint, partially or fully overlapping, or equal. 227 class SetRelation BASE_EMBEDDED { 228 public: 229 // Relation is represented by a bit saying whether there are elements in 230 // one set that is not in the other, and a bit saying that there are elements 231 // that are in both sets. 232 233 // Location of an element. Corresponds to the internal areas of 234 // a Venn diagram. 235 enum { 236 kInFirst = 1 << kInsideFirst, 237 kInSecond = 1 << kInsideSecond, 238 kInBoth = 1 << kInsideBoth 239 }; 240 SetRelation() : bits_(0) {} 241 ~SetRelation() {} 242 // Add the existence of objects in a particular 243 void SetElementsInFirstSet() { bits_ |= kInFirst; } 244 void SetElementsInSecondSet() { bits_ |= kInSecond; } 245 void SetElementsInBothSets() { bits_ |= kInBoth; } 246 // Check the currently known relation of the sets (common functions only, 247 // for other combinations, use value() to get the bits and check them 248 // manually). 249 // Sets are completely disjoint. 250 bool Disjoint() { return (bits_ & kInBoth) == 0; } 251 // Sets are equal. 252 bool Equals() { return (bits_ & (kInFirst | kInSecond)) == 0; } 253 // First set contains second. 254 bool Contains() { return (bits_ & kInSecond) == 0; } 255 // Second set contains first. 256 bool ContainedIn() { return (bits_ & kInFirst) == 0; } 257 bool NonTrivialIntersection() { 258 return (bits_ == (kInFirst | kInSecond | kInBoth)); 259 } 260 int value() { return bits_; } 261 262 private: 263 int bits_; 264 }; 265 266 267 class CharacterRange { 268 public: 269 CharacterRange() : from_(0), to_(0) { } 270 // For compatibility with the CHECK_OK macro 271 CharacterRange(void* null) { ASSERT_EQ(NULL, null); } //NOLINT 272 CharacterRange(uc16 from, uc16 to) : from_(from), to_(to) { } 273 static void AddClassEscape(uc16 type, ZoneList<CharacterRange>* ranges); 274 static Vector<const uc16> GetWordBounds(); 275 static inline CharacterRange Singleton(uc16 value) { 276 return CharacterRange(value, value); 277 } 278 static inline CharacterRange Range(uc16 from, uc16 to) { 279 ASSERT(from <= to); 280 return CharacterRange(from, to); 281 } 282 static inline CharacterRange Everything() { 283 return CharacterRange(0, 0xFFFF); 284 } 285 bool Contains(uc16 i) { return from_ <= i && i <= to_; } 286 uc16 from() const { return from_; } 287 void set_from(uc16 value) { from_ = value; } 288 uc16 to() const { return to_; } 289 void set_to(uc16 value) { to_ = value; } 290 bool is_valid() { return from_ <= to_; } 291 bool IsEverything(uc16 max) { return from_ == 0 && to_ >= max; } 292 bool IsSingleton() { return (from_ == to_); } 293 void AddCaseEquivalents(ZoneList<CharacterRange>* ranges, bool is_ascii); 294 static void Split(ZoneList<CharacterRange>* base, 295 Vector<const uc16> overlay, 296 ZoneList<CharacterRange>** included, 297 ZoneList<CharacterRange>** excluded); 298 // Whether a range list is in canonical form: Ranges ordered by from value, 299 // and ranges non-overlapping and non-adjacent. 300 static bool IsCanonical(ZoneList<CharacterRange>* ranges); 301 // Convert range list to canonical form. The characters covered by the ranges 302 // will still be the same, but no character is in more than one range, and 303 // adjacent ranges are merged. The resulting list may be shorter than the 304 // original, but cannot be longer. 305 static void Canonicalize(ZoneList<CharacterRange>* ranges); 306 // Check how the set of characters defined by a CharacterRange list relates 307 // to the set of word characters. List must be in canonical form. 308 static SetRelation WordCharacterRelation(ZoneList<CharacterRange>* ranges); 309 // Takes two character range lists (representing character sets) in canonical 310 // form and merges them. 311 // The characters that are only covered by the first set are added to 312 // first_set_only_out. the characters that are only in the second set are 313 // added to second_set_only_out, and the characters that are in both are 314 // added to both_sets_out. 315 // The pointers to first_set_only_out, second_set_only_out and both_sets_out 316 // should be to empty lists, but they need not be distinct, and may be NULL. 317 // If NULL, the characters are dropped, and if two arguments are the same 318 // pointer, the result is the union of the two sets that would be created 319 // if the pointers had been distinct. 320 // This way, the Merge function can compute all the usual set operations: 321 // union (all three out-sets are equal), intersection (only both_sets_out is 322 // non-NULL), and set difference (only first_set is non-NULL). 323 static void Merge(ZoneList<CharacterRange>* first_set, 324 ZoneList<CharacterRange>* second_set, 325 ZoneList<CharacterRange>* first_set_only_out, 326 ZoneList<CharacterRange>* second_set_only_out, 327 ZoneList<CharacterRange>* both_sets_out); 328 // Negate the contents of a character range in canonical form. 329 static void Negate(ZoneList<CharacterRange>* src, 330 ZoneList<CharacterRange>* dst); 331 static const int kStartMarker = (1 << 24); 332 static const int kPayloadMask = (1 << 24) - 1; 333 334 private: 335 uc16 from_; 336 uc16 to_; 337 }; 338 339 340 // A set of unsigned integers that behaves especially well on small 341 // integers (< 32). May do zone-allocation. 342 class OutSet: public ZoneObject { 343 public: 344 OutSet() : first_(0), remaining_(NULL), successors_(NULL) { } 345 OutSet* Extend(unsigned value); 346 bool Get(unsigned value); 347 static const unsigned kFirstLimit = 32; 348 349 private: 350 // Destructively set a value in this set. In most cases you want 351 // to use Extend instead to ensure that only one instance exists 352 // that contains the same values. 353 void Set(unsigned value); 354 355 // The successors are a list of sets that contain the same values 356 // as this set and the one more value that is not present in this 357 // set. 358 ZoneList<OutSet*>* successors() { return successors_; } 359 360 OutSet(uint32_t first, ZoneList<unsigned>* remaining) 361 : first_(first), remaining_(remaining), successors_(NULL) { } 362 uint32_t first_; 363 ZoneList<unsigned>* remaining_; 364 ZoneList<OutSet*>* successors_; 365 friend class Trace; 366 }; 367 368 369 // A mapping from integers, specified as ranges, to a set of integers. 370 // Used for mapping character ranges to choices. 371 class DispatchTable : public ZoneObject { 372 public: 373 class Entry { 374 public: 375 Entry() : from_(0), to_(0), out_set_(NULL) { } 376 Entry(uc16 from, uc16 to, OutSet* out_set) 377 : from_(from), to_(to), out_set_(out_set) { } 378 uc16 from() { return from_; } 379 uc16 to() { return to_; } 380 void set_to(uc16 value) { to_ = value; } 381 void AddValue(int value) { out_set_ = out_set_->Extend(value); } 382 OutSet* out_set() { return out_set_; } 383 private: 384 uc16 from_; 385 uc16 to_; 386 OutSet* out_set_; 387 }; 388 389 class Config { 390 public: 391 typedef uc16 Key; 392 typedef Entry Value; 393 static const uc16 kNoKey; 394 static const Entry NoValue() { return Value(); } 395 static inline int Compare(uc16 a, uc16 b) { 396 if (a == b) 397 return 0; 398 else if (a < b) 399 return -1; 400 else 401 return 1; 402 } 403 }; 404 405 void AddRange(CharacterRange range, int value); 406 OutSet* Get(uc16 value); 407 void Dump(); 408 409 template <typename Callback> 410 void ForEach(Callback* callback) { return tree()->ForEach(callback); } 411 412 private: 413 // There can't be a static empty set since it allocates its 414 // successors in a zone and caches them. 415 OutSet* empty() { return &empty_; } 416 OutSet empty_; 417 ZoneSplayTree<Config>* tree() { return &tree_; } 418 ZoneSplayTree<Config> tree_; 419 }; 420 421 422 #define FOR_EACH_NODE_TYPE(VISIT) \ 423 VISIT(End) \ 424 VISIT(Action) \ 425 VISIT(Choice) \ 426 VISIT(BackReference) \ 427 VISIT(Assertion) \ 428 VISIT(Text) 429 430 431 #define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \ 432 VISIT(Disjunction) \ 433 VISIT(Alternative) \ 434 VISIT(Assertion) \ 435 VISIT(CharacterClass) \ 436 VISIT(Atom) \ 437 VISIT(Quantifier) \ 438 VISIT(Capture) \ 439 VISIT(Lookahead) \ 440 VISIT(BackReference) \ 441 VISIT(Empty) \ 442 VISIT(Text) 443 444 445 #define FORWARD_DECLARE(Name) class RegExp##Name; 446 FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE) 447 #undef FORWARD_DECLARE 448 449 450 class TextElement { 451 public: 452 enum Type {UNINITIALIZED, ATOM, CHAR_CLASS}; 453 TextElement() : type(UNINITIALIZED) { } 454 explicit TextElement(Type t) : type(t), cp_offset(-1) { } 455 static TextElement Atom(RegExpAtom* atom); 456 static TextElement CharClass(RegExpCharacterClass* char_class); 457 int length(); 458 Type type; 459 union { 460 RegExpAtom* u_atom; 461 RegExpCharacterClass* u_char_class; 462 } data; 463 int cp_offset; 464 }; 465 466 467 class Trace; 468 469 470 struct NodeInfo { 471 NodeInfo() 472 : being_analyzed(false), 473 been_analyzed(false), 474 follows_word_interest(false), 475 follows_newline_interest(false), 476 follows_start_interest(false), 477 at_end(false), 478 visited(false) { } 479 480 // Returns true if the interests and assumptions of this node 481 // matches the given one. 482 bool Matches(NodeInfo* that) { 483 return (at_end == that->at_end) && 484 (follows_word_interest == that->follows_word_interest) && 485 (follows_newline_interest == that->follows_newline_interest) && 486 (follows_start_interest == that->follows_start_interest); 487 } 488 489 // Updates the interests of this node given the interests of the 490 // node preceding it. 491 void AddFromPreceding(NodeInfo* that) { 492 at_end |= that->at_end; 493 follows_word_interest |= that->follows_word_interest; 494 follows_newline_interest |= that->follows_newline_interest; 495 follows_start_interest |= that->follows_start_interest; 496 } 497 498 bool HasLookbehind() { 499 return follows_word_interest || 500 follows_newline_interest || 501 follows_start_interest; 502 } 503 504 // Sets the interests of this node to include the interests of the 505 // following node. 506 void AddFromFollowing(NodeInfo* that) { 507 follows_word_interest |= that->follows_word_interest; 508 follows_newline_interest |= that->follows_newline_interest; 509 follows_start_interest |= that->follows_start_interest; 510 } 511 512 void ResetCompilationState() { 513 being_analyzed = false; 514 been_analyzed = false; 515 } 516 517 bool being_analyzed: 1; 518 bool been_analyzed: 1; 519 520 // These bits are set of this node has to know what the preceding 521 // character was. 522 bool follows_word_interest: 1; 523 bool follows_newline_interest: 1; 524 bool follows_start_interest: 1; 525 526 bool at_end: 1; 527 bool visited: 1; 528 }; 529 530 531 class SiblingList { 532 public: 533 SiblingList() : list_(NULL) { } 534 int length() { 535 return list_ == NULL ? 0 : list_->length(); 536 } 537 void Ensure(RegExpNode* parent) { 538 if (list_ == NULL) { 539 list_ = new ZoneList<RegExpNode*>(2); 540 list_->Add(parent); 541 } 542 } 543 void Add(RegExpNode* node) { list_->Add(node); } 544 RegExpNode* Get(int index) { return list_->at(index); } 545 private: 546 ZoneList<RegExpNode*>* list_; 547 }; 548 549 550 // Details of a quick mask-compare check that can look ahead in the 551 // input stream. 552 class QuickCheckDetails { 553 public: 554 QuickCheckDetails() 555 : characters_(0), 556 mask_(0), 557 value_(0), 558 cannot_match_(false) { } 559 explicit QuickCheckDetails(int characters) 560 : characters_(characters), 561 mask_(0), 562 value_(0), 563 cannot_match_(false) { } 564 bool Rationalize(bool ascii); 565 // Merge in the information from another branch of an alternation. 566 void Merge(QuickCheckDetails* other, int from_index); 567 // Advance the current position by some amount. 568 void Advance(int by, bool ascii); 569 void Clear(); 570 bool cannot_match() { return cannot_match_; } 571 void set_cannot_match() { cannot_match_ = true; } 572 struct Position { 573 Position() : mask(0), value(0), determines_perfectly(false) { } 574 uc16 mask; 575 uc16 value; 576 bool determines_perfectly; 577 }; 578 int characters() { return characters_; } 579 void set_characters(int characters) { characters_ = characters; } 580 Position* positions(int index) { 581 ASSERT(index >= 0); 582 ASSERT(index < characters_); 583 return positions_ + index; 584 } 585 uint32_t mask() { return mask_; } 586 uint32_t value() { return value_; } 587 588 private: 589 // How many characters do we have quick check information from. This is 590 // the same for all branches of a choice node. 591 int characters_; 592 Position positions_[4]; 593 // These values are the condensate of the above array after Rationalize(). 594 uint32_t mask_; 595 uint32_t value_; 596 // If set to true, there is no way this quick check can match at all. 597 // E.g., if it requires to be at the start of the input, and isn't. 598 bool cannot_match_; 599 }; 600 601 602 class RegExpNode: public ZoneObject { 603 public: 604 RegExpNode() : first_character_set_(NULL), trace_count_(0) { } 605 virtual ~RegExpNode(); 606 virtual void Accept(NodeVisitor* visitor) = 0; 607 // Generates a goto to this node or actually generates the code at this point. 608 virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0; 609 // How many characters must this node consume at a minimum in order to 610 // succeed. If we have found at least 'still_to_find' characters that 611 // must be consumed there is no need to ask any following nodes whether 612 // they are sure to eat any more characters. The not_at_start argument is 613 // used to indicate that we know we are not at the start of the input. In 614 // this case anchored branches will always fail and can be ignored when 615 // determining how many characters are consumed on success. 616 virtual int EatsAtLeast(int still_to_find, 617 int recursion_depth, 618 bool not_at_start) = 0; 619 // Emits some quick code that checks whether the preloaded characters match. 620 // Falls through on certain failure, jumps to the label on possible success. 621 // If the node cannot make a quick check it does nothing and returns false. 622 bool EmitQuickCheck(RegExpCompiler* compiler, 623 Trace* trace, 624 bool preload_has_checked_bounds, 625 Label* on_possible_success, 626 QuickCheckDetails* details_return, 627 bool fall_through_on_failure); 628 // For a given number of characters this returns a mask and a value. The 629 // next n characters are anded with the mask and compared with the value. 630 // A comparison failure indicates the node cannot match the next n characters. 631 // A comparison success indicates the node may match. 632 virtual void GetQuickCheckDetails(QuickCheckDetails* details, 633 RegExpCompiler* compiler, 634 int characters_filled_in, 635 bool not_at_start) = 0; 636 static const int kNodeIsTooComplexForGreedyLoops = -1; 637 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; } 638 Label* label() { return &label_; } 639 // If non-generic code is generated for a node (i.e. the node is not at the 640 // start of the trace) then it cannot be reused. This variable sets a limit 641 // on how often we allow that to happen before we insist on starting a new 642 // trace and generating generic code for a node that can be reused by flushing 643 // the deferred actions in the current trace and generating a goto. 644 static const int kMaxCopiesCodeGenerated = 10; 645 646 NodeInfo* info() { return &info_; } 647 648 void AddSibling(RegExpNode* node) { siblings_.Add(node); } 649 650 // Static version of EnsureSibling that expresses the fact that the 651 // result has the same type as the input. 652 template <class C> 653 static C* EnsureSibling(C* node, NodeInfo* info, bool* cloned) { 654 return static_cast<C*>(node->EnsureSibling(info, cloned)); 655 } 656 657 SiblingList* siblings() { return &siblings_; } 658 void set_siblings(SiblingList* other) { siblings_ = *other; } 659 660 // Return the set of possible next characters recognized by the regexp 661 // (or a safe subset, potentially the set of all characters). 662 ZoneList<CharacterRange>* FirstCharacterSet(); 663 664 // Compute (if possible within the budget of traversed nodes) the 665 // possible first characters of the input matched by this node and 666 // its continuation. Returns the remaining budget after the computation. 667 // If the budget is spent, the result is negative, and the cached 668 // first_character_set_ value isn't set. 669 virtual int ComputeFirstCharacterSet(int budget); 670 671 // Get and set the cached first character set value. 672 ZoneList<CharacterRange>* first_character_set() { 673 return first_character_set_; 674 } 675 void set_first_character_set(ZoneList<CharacterRange>* character_set) { 676 first_character_set_ = character_set; 677 } 678 679 protected: 680 enum LimitResult { DONE, CONTINUE }; 681 static const int kComputeFirstCharacterSetFail = -1; 682 683 LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace); 684 685 // Returns a sibling of this node whose interests and assumptions 686 // match the ones in the given node info. If no sibling exists NULL 687 // is returned. 688 RegExpNode* TryGetSibling(NodeInfo* info); 689 690 // Returns a sibling of this node whose interests match the ones in 691 // the given node info. The info must not contain any assertions. 692 // If no node exists a new one will be created by cloning the current 693 // node. The result will always be an instance of the same concrete 694 // class as this node. 695 RegExpNode* EnsureSibling(NodeInfo* info, bool* cloned); 696 697 // Returns a clone of this node initialized using the copy constructor 698 // of its concrete class. Note that the node may have to be pre- 699 // processed before it is on a usable state. 700 virtual RegExpNode* Clone() = 0; 701 702 private: 703 static const int kFirstCharBudget = 10; 704 Label label_; 705 NodeInfo info_; 706 SiblingList siblings_; 707 ZoneList<CharacterRange>* first_character_set_; 708 // This variable keeps track of how many times code has been generated for 709 // this node (in different traces). We don't keep track of where the 710 // generated code is located unless the code is generated at the start of 711 // a trace, in which case it is generic and can be reused by flushing the 712 // deferred operations in the current trace and generating a goto. 713 int trace_count_; 714 }; 715 716 717 // A simple closed interval. 718 class Interval { 719 public: 720 Interval() : from_(kNone), to_(kNone) { } 721 Interval(int from, int to) : from_(from), to_(to) { } 722 Interval Union(Interval that) { 723 if (that.from_ == kNone) 724 return *this; 725 else if (from_ == kNone) 726 return that; 727 else 728 return Interval(Min(from_, that.from_), Max(to_, that.to_)); 729 } 730 bool Contains(int value) { 731 return (from_ <= value) && (value <= to_); 732 } 733 bool is_empty() { return from_ == kNone; } 734 int from() { return from_; } 735 int to() { return to_; } 736 static Interval Empty() { return Interval(); } 737 static const int kNone = -1; 738 private: 739 int from_; 740 int to_; 741 }; 742 743 744 class SeqRegExpNode: public RegExpNode { 745 public: 746 explicit SeqRegExpNode(RegExpNode* on_success) 747 : on_success_(on_success) { } 748 RegExpNode* on_success() { return on_success_; } 749 void set_on_success(RegExpNode* node) { on_success_ = node; } 750 private: 751 RegExpNode* on_success_; 752 }; 753 754 755 class ActionNode: public SeqRegExpNode { 756 public: 757 enum Type { 758 SET_REGISTER, 759 INCREMENT_REGISTER, 760 STORE_POSITION, 761 BEGIN_SUBMATCH, 762 POSITIVE_SUBMATCH_SUCCESS, 763 EMPTY_MATCH_CHECK, 764 CLEAR_CAPTURES 765 }; 766 static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success); 767 static ActionNode* IncrementRegister(int reg, RegExpNode* on_success); 768 static ActionNode* StorePosition(int reg, 769 bool is_capture, 770 RegExpNode* on_success); 771 static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success); 772 static ActionNode* BeginSubmatch(int stack_pointer_reg, 773 int position_reg, 774 RegExpNode* on_success); 775 static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg, 776 int restore_reg, 777 int clear_capture_count, 778 int clear_capture_from, 779 RegExpNode* on_success); 780 static ActionNode* EmptyMatchCheck(int start_register, 781 int repetition_register, 782 int repetition_limit, 783 RegExpNode* on_success); 784 virtual void Accept(NodeVisitor* visitor); 785 virtual void Emit(RegExpCompiler* compiler, Trace* trace); 786 virtual int EatsAtLeast(int still_to_find, 787 int recursion_depth, 788 bool not_at_start); 789 virtual void GetQuickCheckDetails(QuickCheckDetails* details, 790 RegExpCompiler* compiler, 791 int filled_in, 792 bool not_at_start) { 793 return on_success()->GetQuickCheckDetails( 794 details, compiler, filled_in, not_at_start); 795 } 796 Type type() { return type_; } 797 // TODO(erikcorry): We should allow some action nodes in greedy loops. 798 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; } 799 virtual ActionNode* Clone() { return new ActionNode(*this); } 800 virtual int ComputeFirstCharacterSet(int budget); 801 802 private: 803 union { 804 struct { 805 int reg; 806 int value; 807 } u_store_register; 808 struct { 809 int reg; 810 } u_increment_register; 811 struct { 812 int reg; 813 bool is_capture; 814 } u_position_register; 815 struct { 816 int stack_pointer_register; 817 int current_position_register; 818 int clear_register_count; 819 int clear_register_from; 820 } u_submatch; 821 struct { 822 int start_register; 823 int repetition_register; 824 int repetition_limit; 825 } u_empty_match_check; 826 struct { 827 int range_from; 828 int range_to; 829 } u_clear_captures; 830 } data_; 831 ActionNode(Type type, RegExpNode* on_success) 832 : SeqRegExpNode(on_success), 833 type_(type) { } 834 Type type_; 835 friend class DotPrinter; 836 }; 837 838 839 class TextNode: public SeqRegExpNode { 840 public: 841 TextNode(ZoneList<TextElement>* elms, 842 RegExpNode* on_success) 843 : SeqRegExpNode(on_success), 844 elms_(elms) { } 845 TextNode(RegExpCharacterClass* that, 846 RegExpNode* on_success) 847 : SeqRegExpNode(on_success), 848 elms_(new ZoneList<TextElement>(1)) { 849 elms_->Add(TextElement::CharClass(that)); 850 } 851 virtual void Accept(NodeVisitor* visitor); 852 virtual void Emit(RegExpCompiler* compiler, Trace* trace); 853 virtual int EatsAtLeast(int still_to_find, 854 int recursion_depth, 855 bool not_at_start); 856 virtual void GetQuickCheckDetails(QuickCheckDetails* details, 857 RegExpCompiler* compiler, 858 int characters_filled_in, 859 bool not_at_start); 860 ZoneList<TextElement>* elements() { return elms_; } 861 void MakeCaseIndependent(bool is_ascii); 862 virtual int GreedyLoopTextLength(); 863 virtual TextNode* Clone() { 864 TextNode* result = new TextNode(*this); 865 result->CalculateOffsets(); 866 return result; 867 } 868 void CalculateOffsets(); 869 virtual int ComputeFirstCharacterSet(int budget); 870 871 private: 872 enum TextEmitPassType { 873 NON_ASCII_MATCH, // Check for characters that can't match. 874 SIMPLE_CHARACTER_MATCH, // Case-dependent single character check. 875 NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs. 876 CASE_CHARACTER_MATCH, // Case-independent single character check. 877 CHARACTER_CLASS_MATCH // Character class. 878 }; 879 static bool SkipPass(int pass, bool ignore_case); 880 static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH; 881 static const int kLastPass = CHARACTER_CLASS_MATCH; 882 void TextEmitPass(RegExpCompiler* compiler, 883 TextEmitPassType pass, 884 bool preloaded, 885 Trace* trace, 886 bool first_element_checked, 887 int* checked_up_to); 888 int Length(); 889 ZoneList<TextElement>* elms_; 890 }; 891 892 893 class AssertionNode: public SeqRegExpNode { 894 public: 895 enum AssertionNodeType { 896 AT_END, 897 AT_START, 898 AT_BOUNDARY, 899 AT_NON_BOUNDARY, 900 AFTER_NEWLINE, 901 // Types not directly expressible in regexp syntax. 902 // Used for modifying a boundary node if its following character is 903 // known to be word and/or non-word. 904 AFTER_NONWORD_CHARACTER, 905 AFTER_WORD_CHARACTER 906 }; 907 static AssertionNode* AtEnd(RegExpNode* on_success) { 908 return new AssertionNode(AT_END, on_success); 909 } 910 static AssertionNode* AtStart(RegExpNode* on_success) { 911 return new AssertionNode(AT_START, on_success); 912 } 913 static AssertionNode* AtBoundary(RegExpNode* on_success) { 914 return new AssertionNode(AT_BOUNDARY, on_success); 915 } 916 static AssertionNode* AtNonBoundary(RegExpNode* on_success) { 917 return new AssertionNode(AT_NON_BOUNDARY, on_success); 918 } 919 static AssertionNode* AfterNewline(RegExpNode* on_success) { 920 return new AssertionNode(AFTER_NEWLINE, on_success); 921 } 922 virtual void Accept(NodeVisitor* visitor); 923 virtual void Emit(RegExpCompiler* compiler, Trace* trace); 924 virtual int EatsAtLeast(int still_to_find, 925 int recursion_depth, 926 bool not_at_start); 927 virtual void GetQuickCheckDetails(QuickCheckDetails* details, 928 RegExpCompiler* compiler, 929 int filled_in, 930 bool not_at_start); 931 virtual int ComputeFirstCharacterSet(int budget); 932 virtual AssertionNode* Clone() { return new AssertionNode(*this); } 933 AssertionNodeType type() { return type_; } 934 void set_type(AssertionNodeType type) { type_ = type; } 935 936 private: 937 AssertionNode(AssertionNodeType t, RegExpNode* on_success) 938 : SeqRegExpNode(on_success), type_(t) { } 939 AssertionNodeType type_; 940 }; 941 942 943 class BackReferenceNode: public SeqRegExpNode { 944 public: 945 BackReferenceNode(int start_reg, 946 int end_reg, 947 RegExpNode* on_success) 948 : SeqRegExpNode(on_success), 949 start_reg_(start_reg), 950 end_reg_(end_reg) { } 951 virtual void Accept(NodeVisitor* visitor); 952 int start_register() { return start_reg_; } 953 int end_register() { return end_reg_; } 954 virtual void Emit(RegExpCompiler* compiler, Trace* trace); 955 virtual int EatsAtLeast(int still_to_find, 956 int recursion_depth, 957 bool not_at_start); 958 virtual void GetQuickCheckDetails(QuickCheckDetails* details, 959 RegExpCompiler* compiler, 960 int characters_filled_in, 961 bool not_at_start) { 962 return; 963 } 964 virtual BackReferenceNode* Clone() { return new BackReferenceNode(*this); } 965 virtual int ComputeFirstCharacterSet(int budget); 966 967 private: 968 int start_reg_; 969 int end_reg_; 970 }; 971 972 973 class EndNode: public RegExpNode { 974 public: 975 enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS }; 976 explicit EndNode(Action action) : action_(action) { } 977 virtual void Accept(NodeVisitor* visitor); 978 virtual void Emit(RegExpCompiler* compiler, Trace* trace); 979 virtual int EatsAtLeast(int still_to_find, 980 int recursion_depth, 981 bool not_at_start) { return 0; } 982 virtual void GetQuickCheckDetails(QuickCheckDetails* details, 983 RegExpCompiler* compiler, 984 int characters_filled_in, 985 bool not_at_start) { 986 // Returning 0 from EatsAtLeast should ensure we never get here. 987 UNREACHABLE(); 988 } 989 virtual EndNode* Clone() { return new EndNode(*this); } 990 private: 991 Action action_; 992 }; 993 994 995 class NegativeSubmatchSuccess: public EndNode { 996 public: 997 NegativeSubmatchSuccess(int stack_pointer_reg, 998 int position_reg, 999 int clear_capture_count, 1000 int clear_capture_start) 1001 : EndNode(NEGATIVE_SUBMATCH_SUCCESS), 1002 stack_pointer_register_(stack_pointer_reg), 1003 current_position_register_(position_reg), 1004 clear_capture_count_(clear_capture_count), 1005 clear_capture_start_(clear_capture_start) { } 1006 virtual void Emit(RegExpCompiler* compiler, Trace* trace); 1007 1008 private: 1009 int stack_pointer_register_; 1010 int current_position_register_; 1011 int clear_capture_count_; 1012 int clear_capture_start_; 1013 }; 1014 1015 1016 class Guard: public ZoneObject { 1017 public: 1018 enum Relation { LT, GEQ }; 1019 Guard(int reg, Relation op, int value) 1020 : reg_(reg), 1021 op_(op), 1022 value_(value) { } 1023 int reg() { return reg_; } 1024 Relation op() { return op_; } 1025 int value() { return value_; } 1026 1027 private: 1028 int reg_; 1029 Relation op_; 1030 int value_; 1031 }; 1032 1033 1034 class GuardedAlternative { 1035 public: 1036 explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { } 1037 void AddGuard(Guard* guard); 1038 RegExpNode* node() { return node_; } 1039 void set_node(RegExpNode* node) { node_ = node; } 1040 ZoneList<Guard*>* guards() { return guards_; } 1041 1042 private: 1043 RegExpNode* node_; 1044 ZoneList<Guard*>* guards_; 1045 }; 1046 1047 1048 class AlternativeGeneration; 1049 1050 1051 class ChoiceNode: public RegExpNode { 1052 public: 1053 explicit ChoiceNode(int expected_size) 1054 : alternatives_(new ZoneList<GuardedAlternative>(expected_size)), 1055 table_(NULL), 1056 not_at_start_(false), 1057 being_calculated_(false) { } 1058 virtual void Accept(NodeVisitor* visitor); 1059 void AddAlternative(GuardedAlternative node) { alternatives()->Add(node); } 1060 ZoneList<GuardedAlternative>* alternatives() { return alternatives_; } 1061 DispatchTable* GetTable(bool ignore_case); 1062 virtual void Emit(RegExpCompiler* compiler, Trace* trace); 1063 virtual int EatsAtLeast(int still_to_find, 1064 int recursion_depth, 1065 bool not_at_start); 1066 int EatsAtLeastHelper(int still_to_find, 1067 int recursion_depth, 1068 RegExpNode* ignore_this_node, 1069 bool not_at_start); 1070 virtual void GetQuickCheckDetails(QuickCheckDetails* details, 1071 RegExpCompiler* compiler, 1072 int characters_filled_in, 1073 bool not_at_start); 1074 virtual ChoiceNode* Clone() { return new ChoiceNode(*this); } 1075 1076 bool being_calculated() { return being_calculated_; } 1077 bool not_at_start() { return not_at_start_; } 1078 void set_not_at_start() { not_at_start_ = true; } 1079 void set_being_calculated(bool b) { being_calculated_ = b; } 1080 virtual bool try_to_emit_quick_check_for_alternative(int i) { return true; } 1081 1082 protected: 1083 int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative); 1084 ZoneList<GuardedAlternative>* alternatives_; 1085 1086 private: 1087 friend class DispatchTableConstructor; 1088 friend class Analysis; 1089 void GenerateGuard(RegExpMacroAssembler* macro_assembler, 1090 Guard* guard, 1091 Trace* trace); 1092 int CalculatePreloadCharacters(RegExpCompiler* compiler, bool not_at_start); 1093 void EmitOutOfLineContinuation(RegExpCompiler* compiler, 1094 Trace* trace, 1095 GuardedAlternative alternative, 1096 AlternativeGeneration* alt_gen, 1097 int preload_characters, 1098 bool next_expects_preload); 1099 DispatchTable* table_; 1100 // If true, this node is never checked at the start of the input. 1101 // Allows a new trace to start with at_start() set to false. 1102 bool not_at_start_; 1103 bool being_calculated_; 1104 }; 1105 1106 1107 class NegativeLookaheadChoiceNode: public ChoiceNode { 1108 public: 1109 explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail, 1110 GuardedAlternative then_do_this) 1111 : ChoiceNode(2) { 1112 AddAlternative(this_must_fail); 1113 AddAlternative(then_do_this); 1114 } 1115 virtual int EatsAtLeast(int still_to_find, 1116 int recursion_depth, 1117 bool not_at_start); 1118 virtual void GetQuickCheckDetails(QuickCheckDetails* details, 1119 RegExpCompiler* compiler, 1120 int characters_filled_in, 1121 bool not_at_start); 1122 // For a negative lookahead we don't emit the quick check for the 1123 // alternative that is expected to fail. This is because quick check code 1124 // starts by loading enough characters for the alternative that takes fewest 1125 // characters, but on a negative lookahead the negative branch did not take 1126 // part in that calculation (EatsAtLeast) so the assumptions don't hold. 1127 virtual bool try_to_emit_quick_check_for_alternative(int i) { return i != 0; } 1128 virtual int ComputeFirstCharacterSet(int budget); 1129 }; 1130 1131 1132 class LoopChoiceNode: public ChoiceNode { 1133 public: 1134 explicit LoopChoiceNode(bool body_can_be_zero_length) 1135 : ChoiceNode(2), 1136 loop_node_(NULL), 1137 continue_node_(NULL), 1138 body_can_be_zero_length_(body_can_be_zero_length) { } 1139 void AddLoopAlternative(GuardedAlternative alt); 1140 void AddContinueAlternative(GuardedAlternative alt); 1141 virtual void Emit(RegExpCompiler* compiler, Trace* trace); 1142 virtual int EatsAtLeast(int still_to_find, 1143 int recursion_depth, 1144 bool not_at_start); 1145 virtual void GetQuickCheckDetails(QuickCheckDetails* details, 1146 RegExpCompiler* compiler, 1147 int characters_filled_in, 1148 bool not_at_start); 1149 virtual int ComputeFirstCharacterSet(int budget); 1150 virtual LoopChoiceNode* Clone() { return new LoopChoiceNode(*this); } 1151 RegExpNode* loop_node() { return loop_node_; } 1152 RegExpNode* continue_node() { return continue_node_; } 1153 bool body_can_be_zero_length() { return body_can_be_zero_length_; } 1154 virtual void Accept(NodeVisitor* visitor); 1155 1156 private: 1157 // AddAlternative is made private for loop nodes because alternatives 1158 // should not be added freely, we need to keep track of which node 1159 // goes back to the node itself. 1160 void AddAlternative(GuardedAlternative node) { 1161 ChoiceNode::AddAlternative(node); 1162 } 1163 1164 RegExpNode* loop_node_; 1165 RegExpNode* continue_node_; 1166 bool body_can_be_zero_length_; 1167 }; 1168 1169 1170 // There are many ways to generate code for a node. This class encapsulates 1171 // the current way we should be generating. In other words it encapsulates 1172 // the current state of the code generator. The effect of this is that we 1173 // generate code for paths that the matcher can take through the regular 1174 // expression. A given node in the regexp can be code-generated several times 1175 // as it can be part of several traces. For example for the regexp: 1176 // /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part 1177 // of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code 1178 // to match foo is generated only once (the traces have a common prefix). The 1179 // code to store the capture is deferred and generated (twice) after the places 1180 // where baz has been matched. 1181 class Trace { 1182 public: 1183 // A value for a property that is either known to be true, know to be false, 1184 // or not known. 1185 enum TriBool { 1186 UNKNOWN = -1, FALSE = 0, TRUE = 1 1187 }; 1188 1189 class DeferredAction { 1190 public: 1191 DeferredAction(ActionNode::Type type, int reg) 1192 : type_(type), reg_(reg), next_(NULL) { } 1193 DeferredAction* next() { return next_; } 1194 bool Mentions(int reg); 1195 int reg() { return reg_; } 1196 ActionNode::Type type() { return type_; } 1197 private: 1198 ActionNode::Type type_; 1199 int reg_; 1200 DeferredAction* next_; 1201 friend class Trace; 1202 }; 1203 1204 class DeferredCapture : public DeferredAction { 1205 public: 1206 DeferredCapture(int reg, bool is_capture, Trace* trace) 1207 : DeferredAction(ActionNode::STORE_POSITION, reg), 1208 cp_offset_(trace->cp_offset()), 1209 is_capture_(is_capture) { } 1210 int cp_offset() { return cp_offset_; } 1211 bool is_capture() { return is_capture_; } 1212 private: 1213 int cp_offset_; 1214 bool is_capture_; 1215 void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; } 1216 }; 1217 1218 class DeferredSetRegister : public DeferredAction { 1219 public: 1220 DeferredSetRegister(int reg, int value) 1221 : DeferredAction(ActionNode::SET_REGISTER, reg), 1222 value_(value) { } 1223 int value() { return value_; } 1224 private: 1225 int value_; 1226 }; 1227 1228 class DeferredClearCaptures : public DeferredAction { 1229 public: 1230 explicit DeferredClearCaptures(Interval range) 1231 : DeferredAction(ActionNode::CLEAR_CAPTURES, -1), 1232 range_(range) { } 1233 Interval range() { return range_; } 1234 private: 1235 Interval range_; 1236 }; 1237 1238 class DeferredIncrementRegister : public DeferredAction { 1239 public: 1240 explicit DeferredIncrementRegister(int reg) 1241 : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { } 1242 }; 1243 1244 Trace() 1245 : cp_offset_(0), 1246 actions_(NULL), 1247 backtrack_(NULL), 1248 stop_node_(NULL), 1249 loop_label_(NULL), 1250 characters_preloaded_(0), 1251 bound_checked_up_to_(0), 1252 flush_budget_(100), 1253 at_start_(UNKNOWN) { } 1254 1255 // End the trace. This involves flushing the deferred actions in the trace 1256 // and pushing a backtrack location onto the backtrack stack. Once this is 1257 // done we can start a new trace or go to one that has already been 1258 // generated. 1259 void Flush(RegExpCompiler* compiler, RegExpNode* successor); 1260 int cp_offset() { return cp_offset_; } 1261 DeferredAction* actions() { return actions_; } 1262 // A trivial trace is one that has no deferred actions or other state that 1263 // affects the assumptions used when generating code. There is no recorded 1264 // backtrack location in a trivial trace, so with a trivial trace we will 1265 // generate code that, on a failure to match, gets the backtrack location 1266 // from the backtrack stack rather than using a direct jump instruction. We 1267 // always start code generation with a trivial trace and non-trivial traces 1268 // are created as we emit code for nodes or add to the list of deferred 1269 // actions in the trace. The location of the code generated for a node using 1270 // a trivial trace is recorded in a label in the node so that gotos can be 1271 // generated to that code. 1272 bool is_trivial() { 1273 return backtrack_ == NULL && 1274 actions_ == NULL && 1275 cp_offset_ == 0 && 1276 characters_preloaded_ == 0 && 1277 bound_checked_up_to_ == 0 && 1278 quick_check_performed_.characters() == 0 && 1279 at_start_ == UNKNOWN; 1280 } 1281 TriBool at_start() { return at_start_; } 1282 void set_at_start(bool at_start) { at_start_ = at_start ? TRUE : FALSE; } 1283 Label* backtrack() { return backtrack_; } 1284 Label* loop_label() { return loop_label_; } 1285 RegExpNode* stop_node() { return stop_node_; } 1286 int characters_preloaded() { return characters_preloaded_; } 1287 int bound_checked_up_to() { return bound_checked_up_to_; } 1288 int flush_budget() { return flush_budget_; } 1289 QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; } 1290 bool mentions_reg(int reg); 1291 // Returns true if a deferred position store exists to the specified 1292 // register and stores the offset in the out-parameter. Otherwise 1293 // returns false. 1294 bool GetStoredPosition(int reg, int* cp_offset); 1295 // These set methods and AdvanceCurrentPositionInTrace should be used only on 1296 // new traces - the intention is that traces are immutable after creation. 1297 void add_action(DeferredAction* new_action) { 1298 ASSERT(new_action->next_ == NULL); 1299 new_action->next_ = actions_; 1300 actions_ = new_action; 1301 } 1302 void set_backtrack(Label* backtrack) { backtrack_ = backtrack; } 1303 void set_stop_node(RegExpNode* node) { stop_node_ = node; } 1304 void set_loop_label(Label* label) { loop_label_ = label; } 1305 void set_characters_preloaded(int count) { characters_preloaded_ = count; } 1306 void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; } 1307 void set_flush_budget(int to) { flush_budget_ = to; } 1308 void set_quick_check_performed(QuickCheckDetails* d) { 1309 quick_check_performed_ = *d; 1310 } 1311 void InvalidateCurrentCharacter(); 1312 void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler); 1313 1314 private: 1315 int FindAffectedRegisters(OutSet* affected_registers); 1316 void PerformDeferredActions(RegExpMacroAssembler* macro, 1317 int max_register, 1318 OutSet& affected_registers, 1319 OutSet* registers_to_pop, 1320 OutSet* registers_to_clear); 1321 void RestoreAffectedRegisters(RegExpMacroAssembler* macro, 1322 int max_register, 1323 OutSet& registers_to_pop, 1324 OutSet& registers_to_clear); 1325 int cp_offset_; 1326 DeferredAction* actions_; 1327 Label* backtrack_; 1328 RegExpNode* stop_node_; 1329 Label* loop_label_; 1330 int characters_preloaded_; 1331 int bound_checked_up_to_; 1332 QuickCheckDetails quick_check_performed_; 1333 int flush_budget_; 1334 TriBool at_start_; 1335 }; 1336 1337 1338 class NodeVisitor { 1339 public: 1340 virtual ~NodeVisitor() { } 1341 #define DECLARE_VISIT(Type) \ 1342 virtual void Visit##Type(Type##Node* that) = 0; 1343 FOR_EACH_NODE_TYPE(DECLARE_VISIT) 1344 #undef DECLARE_VISIT 1345 virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); } 1346 }; 1347 1348 1349 // Node visitor used to add the start set of the alternatives to the 1350 // dispatch table of a choice node. 1351 class DispatchTableConstructor: public NodeVisitor { 1352 public: 1353 DispatchTableConstructor(DispatchTable* table, bool ignore_case) 1354 : table_(table), 1355 choice_index_(-1), 1356 ignore_case_(ignore_case) { } 1357 1358 void BuildTable(ChoiceNode* node); 1359 1360 void AddRange(CharacterRange range) { 1361 table()->AddRange(range, choice_index_); 1362 } 1363 1364 void AddInverse(ZoneList<CharacterRange>* ranges); 1365 1366 #define DECLARE_VISIT(Type) \ 1367 virtual void Visit##Type(Type##Node* that); 1368 FOR_EACH_NODE_TYPE(DECLARE_VISIT) 1369 #undef DECLARE_VISIT 1370 1371 DispatchTable* table() { return table_; } 1372 void set_choice_index(int value) { choice_index_ = value; } 1373 1374 protected: 1375 DispatchTable* table_; 1376 int choice_index_; 1377 bool ignore_case_; 1378 }; 1379 1380 1381 // Assertion propagation moves information about assertions such as 1382 // \b to the affected nodes. For instance, in /.\b./ information must 1383 // be propagated to the first '.' that whatever follows needs to know 1384 // if it matched a word or a non-word, and to the second '.' that it 1385 // has to check if it succeeds a word or non-word. In this case the 1386 // result will be something like: 1387 // 1388 // +-------+ +------------+ 1389 // | . | | . | 1390 // +-------+ ---> +------------+ 1391 // | word? | | check word | 1392 // +-------+ +------------+ 1393 class Analysis: public NodeVisitor { 1394 public: 1395 Analysis(bool ignore_case, bool is_ascii) 1396 : ignore_case_(ignore_case), 1397 is_ascii_(is_ascii), 1398 error_message_(NULL) { } 1399 void EnsureAnalyzed(RegExpNode* node); 1400 1401 #define DECLARE_VISIT(Type) \ 1402 virtual void Visit##Type(Type##Node* that); 1403 FOR_EACH_NODE_TYPE(DECLARE_VISIT) 1404 #undef DECLARE_VISIT 1405 virtual void VisitLoopChoice(LoopChoiceNode* that); 1406 1407 bool has_failed() { return error_message_ != NULL; } 1408 const char* error_message() { 1409 ASSERT(error_message_ != NULL); 1410 return error_message_; 1411 } 1412 void fail(const char* error_message) { 1413 error_message_ = error_message; 1414 } 1415 1416 private: 1417 bool ignore_case_; 1418 bool is_ascii_; 1419 const char* error_message_; 1420 1421 DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis); 1422 }; 1423 1424 1425 struct RegExpCompileData { 1426 RegExpCompileData() 1427 : tree(NULL), 1428 node(NULL), 1429 simple(true), 1430 contains_anchor(false), 1431 capture_count(0) { } 1432 RegExpTree* tree; 1433 RegExpNode* node; 1434 bool simple; 1435 bool contains_anchor; 1436 Handle<String> error; 1437 int capture_count; 1438 }; 1439 1440 1441 class RegExpEngine: public AllStatic { 1442 public: 1443 struct CompilationResult { 1444 explicit CompilationResult(const char* error_message) 1445 : error_message(error_message), 1446 code(HEAP->the_hole_value()), 1447 num_registers(0) {} 1448 CompilationResult(Object* code, int registers) 1449 : error_message(NULL), 1450 code(code), 1451 num_registers(registers) {} 1452 const char* error_message; 1453 Object* code; 1454 int num_registers; 1455 }; 1456 1457 static CompilationResult Compile(RegExpCompileData* input, 1458 bool ignore_case, 1459 bool multiline, 1460 Handle<String> pattern, 1461 bool is_ascii); 1462 1463 static void DotPrint(const char* label, RegExpNode* node, bool ignore_case); 1464 }; 1465 1466 1467 class OffsetsVector { 1468 public: 1469 inline OffsetsVector(int num_registers, Isolate* isolate) 1470 : offsets_vector_length_(num_registers) { 1471 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) { 1472 vector_ = NewArray<int>(offsets_vector_length_); 1473 } else { 1474 vector_ = isolate->jsregexp_static_offsets_vector(); 1475 } 1476 } 1477 inline ~OffsetsVector() { 1478 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) { 1479 DeleteArray(vector_); 1480 vector_ = NULL; 1481 } 1482 } 1483 inline int* vector() { return vector_; } 1484 inline int length() { return offsets_vector_length_; } 1485 1486 static const int kStaticOffsetsVectorSize = 50; 1487 1488 private: 1489 static Address static_offsets_vector_address(Isolate* isolate) { 1490 return reinterpret_cast<Address>(isolate->jsregexp_static_offsets_vector()); 1491 } 1492 1493 int* vector_; 1494 int offsets_vector_length_; 1495 1496 friend class ExternalReference; 1497 }; 1498 1499 1500 } } // namespace v8::internal 1501 1502 #endif // V8_JSREGEXP_H_ 1503