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