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