| /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| * vim: set ts=8 sts=4 et sw=4 tw=99: */ |
| |
| // Copyright 2012 the V8 project authors. All rights reserved. |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are |
| // met: |
| // |
| // * Redistributions of source code must retain the above copyright |
| // notice, this list of conditions and the following disclaimer. |
| // * Redistributions in binary form must reproduce the above |
| // copyright notice, this list of conditions and the following |
| // disclaimer in the documentation and/or other materials provided |
| // with the distribution. |
| // * Neither the name of Google Inc. nor the names of its |
| // contributors may be used to endorse or promote products derived |
| // from this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| #include "irregexp/RegExpEngine.h" |
| |
| #include "irregexp/NativeRegExpMacroAssembler.h" |
| #include "irregexp/RegExpMacroAssembler.h" |
| #include "jit/JitCommon.h" |
| |
| using namespace js; |
| using namespace js::irregexp; |
| |
| using mozilla::ArrayLength; |
| using mozilla::DebugOnly; |
| using mozilla::Maybe; |
| |
| #define DEFINE_ACCEPT(Type) \ |
| void Type##Node::Accept(NodeVisitor* visitor) { \ |
| visitor->Visit##Type(this); \ |
| } |
| FOR_EACH_NODE_TYPE(DEFINE_ACCEPT) |
| #undef DEFINE_ACCEPT |
| |
| void LoopChoiceNode::Accept(NodeVisitor* visitor) { |
| visitor->VisitLoopChoice(this); |
| } |
| |
| static const int kMaxLookaheadForBoyerMoore = 8; |
| |
| RegExpNode::RegExpNode(LifoAlloc* alloc) |
| : replacement_(nullptr), trace_count_(0), alloc_(alloc) |
| { |
| bm_info_[0] = bm_info_[1] = nullptr; |
| } |
| |
| // ------------------------------------------------------------------- |
| // CharacterRange |
| |
| // The '2' variant has inclusive from and exclusive to. |
| // This covers \s as defined in ECMA-262 5.1, 15.10.2.12, |
| // which include WhiteSpace (7.2) or LineTerminator (7.3) values. |
| static const int kSpaceRanges[] = { '\t', '\r' + 1, ' ', ' ' + 1, |
| 0x00A0, 0x00A1, 0x1680, 0x1681, 0x180E, 0x180F, 0x2000, 0x200B, |
| 0x2028, 0x202A, 0x202F, 0x2030, 0x205F, 0x2060, 0x3000, 0x3001, |
| 0xFEFF, 0xFF00, 0x10000 }; |
| static const int kSpaceRangeCount = ArrayLength(kSpaceRanges); |
| |
| static const int kWordRanges[] = { |
| '0', '9' + 1, 'A', 'Z' + 1, '_', '_' + 1, 'a', 'z' + 1, 0x10000 }; |
| static const int kWordRangeCount = ArrayLength(kWordRanges); |
| static const int kDigitRanges[] = { '0', '9' + 1, 0x10000 }; |
| static const int kDigitRangeCount = ArrayLength(kDigitRanges); |
| static const int kSurrogateRanges[] = { 0xd800, 0xe000, 0x10000 }; |
| static const int kSurrogateRangeCount = ArrayLength(kSurrogateRanges); |
| static const int kLineTerminatorRanges[] = { 0x000A, 0x000B, 0x000D, 0x000E, |
| 0x2028, 0x202A, 0x10000 }; |
| static const int kLineTerminatorRangeCount = ArrayLength(kLineTerminatorRanges); |
| static const int kMaxOneByteCharCode = 0xff; |
| static const int kMaxUtf16CodeUnit = 0xffff; |
| |
| static char16_t |
| MaximumCharacter(bool ascii) |
| { |
| return ascii ? kMaxOneByteCharCode : kMaxUtf16CodeUnit; |
| } |
| |
| static void |
| AddClass(const int* elmv, int elmc, |
| CharacterRangeVector* ranges) |
| { |
| elmc--; |
| MOZ_ASSERT(elmv[elmc] == 0x10000); |
| for (int i = 0; i < elmc; i += 2) { |
| MOZ_ASSERT(elmv[i] < elmv[i + 1]); |
| ranges->append(CharacterRange(elmv[i], elmv[i + 1] - 1)); |
| } |
| } |
| |
| static void |
| AddClassNegated(const int* elmv, |
| int elmc, |
| CharacterRangeVector* ranges) |
| { |
| elmc--; |
| MOZ_ASSERT(elmv[elmc] == 0x10000); |
| MOZ_ASSERT(elmv[0] != 0x0000); |
| MOZ_ASSERT(elmv[elmc-1] != kMaxUtf16CodeUnit); |
| char16_t last = 0x0000; |
| for (int i = 0; i < elmc; i += 2) { |
| MOZ_ASSERT(last <= elmv[i] - 1); |
| MOZ_ASSERT(elmv[i] < elmv[i + 1]); |
| ranges->append(CharacterRange(last, elmv[i] - 1)); |
| last = elmv[i + 1]; |
| } |
| ranges->append(CharacterRange(last, kMaxUtf16CodeUnit)); |
| } |
| |
| void |
| CharacterRange::AddClassEscape(LifoAlloc* alloc, char16_t type, |
| CharacterRangeVector* ranges) |
| { |
| switch (type) { |
| case 's': |
| AddClass(kSpaceRanges, kSpaceRangeCount, ranges); |
| break; |
| case 'S': |
| AddClassNegated(kSpaceRanges, kSpaceRangeCount, ranges); |
| break; |
| case 'w': |
| AddClass(kWordRanges, kWordRangeCount, ranges); |
| break; |
| case 'W': |
| AddClassNegated(kWordRanges, kWordRangeCount, ranges); |
| break; |
| case 'd': |
| AddClass(kDigitRanges, kDigitRangeCount, ranges); |
| break; |
| case 'D': |
| AddClassNegated(kDigitRanges, kDigitRangeCount, ranges); |
| break; |
| case '.': |
| AddClassNegated(kLineTerminatorRanges, kLineTerminatorRangeCount, ranges); |
| break; |
| // This is not a character range as defined by the spec but a |
| // convenient shorthand for a character class that matches any |
| // character. |
| case '*': |
| ranges->append(CharacterRange::Everything()); |
| break; |
| // This is the set of characters matched by the $ and ^ symbols |
| // in multiline mode. |
| case 'n': |
| AddClass(kLineTerminatorRanges, kLineTerminatorRangeCount, ranges); |
| break; |
| default: |
| MOZ_CRASH("Bad character class escape"); |
| } |
| } |
| |
| // We need to check for the following characters: 0x39c 0x3bc 0x178. |
| static inline bool |
| RangeContainsLatin1Equivalents(CharacterRange range) |
| { |
| // TODO(dcarney): this could be a lot more efficient. |
| return range.Contains(0x39c) || range.Contains(0x3bc) || range.Contains(0x178); |
| } |
| |
| static bool |
| RangesContainLatin1Equivalents(const CharacterRangeVector& ranges) |
| { |
| for (size_t i = 0; i < ranges.length(); i++) { |
| // TODO(dcarney): this could be a lot more efficient. |
| if (RangeContainsLatin1Equivalents(ranges[i])) |
| return true; |
| } |
| return false; |
| } |
| |
| static const size_t kEcma262UnCanonicalizeMaxWidth = 4; |
| |
| // Returns the number of characters in the equivalence class, omitting those |
| // that cannot occur in the source string if it is a one byte string. |
| static int |
| GetCaseIndependentLetters(char16_t character, |
| bool ascii_subject, |
| char16_t* letters) |
| { |
| const char16_t choices[] = { |
| character, |
| unicode::ToLowerCase(character), |
| unicode::ToUpperCase(character) |
| }; |
| |
| size_t count = 0; |
| for (size_t i = 0; i < ArrayLength(choices); i++) { |
| char16_t c = choices[i]; |
| |
| // The standard requires that non-ASCII characters cannot have ASCII |
| // character codes in their equivalence class, even though this |
| // situation occurs multiple times in the unicode tables. |
| static const unsigned kMaxAsciiCharCode = 127; |
| if (character > kMaxAsciiCharCode && c <= kMaxAsciiCharCode) |
| continue; |
| |
| // Skip characters that can't appear in one byte strings. |
| if (ascii_subject && c > kMaxOneByteCharCode) |
| continue; |
| |
| // Watch for duplicates. |
| bool found = false; |
| for (size_t j = 0; j < count; j++) { |
| if (letters[j] == c) { |
| found = true; |
| break; |
| } |
| } |
| if (found) |
| continue; |
| |
| letters[count++] = c; |
| } |
| |
| return count; |
| } |
| |
| static char16_t |
| ConvertNonLatin1ToLatin1(char16_t c) |
| { |
| MOZ_ASSERT(c > kMaxOneByteCharCode); |
| switch (c) { |
| // This are equivalent characters in unicode. |
| case 0x39c: |
| case 0x3bc: |
| return 0xb5; |
| // This is an uppercase of a Latin-1 character |
| // outside of Latin-1. |
| case 0x178: |
| return 0xff; |
| } |
| return 0; |
| } |
| |
| void |
| CharacterRange::AddCaseEquivalents(bool is_ascii, CharacterRangeVector* ranges) |
| { |
| char16_t bottom = from(); |
| char16_t top = to(); |
| |
| if (is_ascii && !RangeContainsLatin1Equivalents(*this)) { |
| if (bottom > kMaxOneByteCharCode) |
| return; |
| if (top > kMaxOneByteCharCode) |
| top = kMaxOneByteCharCode; |
| } |
| |
| for (char16_t c = bottom;; c++) { |
| char16_t chars[kEcma262UnCanonicalizeMaxWidth]; |
| size_t length = GetCaseIndependentLetters(c, is_ascii, chars); |
| |
| for (size_t i = 0; i < length; i++) { |
| char16_t other = chars[i]; |
| if (other == c) |
| continue; |
| |
| // Try to combine with an existing range. |
| bool found = false; |
| for (size_t i = 0; i < ranges->length(); i++) { |
| CharacterRange& range = (*ranges)[i]; |
| if (range.Contains(other)) { |
| found = true; |
| break; |
| } else if (other == range.from() - 1) { |
| range.set_from(other); |
| found = true; |
| break; |
| } else if (other == range.to() + 1) { |
| range.set_to(other); |
| found = true; |
| break; |
| } |
| } |
| |
| if (!found) |
| ranges->append(CharacterRange::Singleton(other)); |
| } |
| |
| if (c == top) |
| break; |
| } |
| } |
| |
| static bool |
| CompareInverseRanges(const CharacterRangeVector& ranges, const int* special_class, size_t length) |
| { |
| length--; // Remove final 0x10000. |
| MOZ_ASSERT(special_class[length] == 0x10000); |
| MOZ_ASSERT(ranges.length() != 0); |
| MOZ_ASSERT(length != 0); |
| MOZ_ASSERT(special_class[0] != 0); |
| if (ranges.length() != (length >> 1) + 1) |
| return false; |
| CharacterRange range = ranges[0]; |
| if (range.from() != 0) |
| return false; |
| for (size_t i = 0; i < length; i += 2) { |
| if (special_class[i] != (range.to() + 1)) |
| return false; |
| range = ranges[(i >> 1) + 1]; |
| if (special_class[i+1] != range.from()) |
| return false; |
| } |
| if (range.to() != 0xffff) |
| return false; |
| return true; |
| } |
| |
| static bool |
| CompareRanges(const CharacterRangeVector& ranges, const int* special_class, size_t length) |
| { |
| length--; // Remove final 0x10000. |
| MOZ_ASSERT(special_class[length] == 0x10000); |
| if (ranges.length() * 2 != length) |
| return false; |
| for (size_t i = 0; i < length; i += 2) { |
| CharacterRange range = ranges[i >> 1]; |
| if (range.from() != special_class[i] || range.to() != special_class[i + 1] - 1) |
| return false; |
| } |
| return true; |
| } |
| |
| bool |
| RegExpCharacterClass::is_standard(LifoAlloc* alloc) |
| { |
| // TODO(lrn): Remove need for this function, by not throwing away information |
| // along the way. |
| if (is_negated_) |
| return false; |
| if (set_.is_standard()) |
| return true; |
| if (CompareRanges(set_.ranges(alloc), kSpaceRanges, kSpaceRangeCount)) { |
| set_.set_standard_set_type('s'); |
| return true; |
| } |
| if (CompareInverseRanges(set_.ranges(alloc), kSpaceRanges, kSpaceRangeCount)) { |
| set_.set_standard_set_type('S'); |
| return true; |
| } |
| if (CompareInverseRanges(set_.ranges(alloc), |
| kLineTerminatorRanges, |
| kLineTerminatorRangeCount)) { |
| set_.set_standard_set_type('.'); |
| return true; |
| } |
| if (CompareRanges(set_.ranges(alloc), |
| kLineTerminatorRanges, |
| kLineTerminatorRangeCount)) { |
| set_.set_standard_set_type('n'); |
| return true; |
| } |
| if (CompareRanges(set_.ranges(alloc), kWordRanges, kWordRangeCount)) { |
| set_.set_standard_set_type('w'); |
| return true; |
| } |
| if (CompareInverseRanges(set_.ranges(alloc), kWordRanges, kWordRangeCount)) { |
| set_.set_standard_set_type('W'); |
| return true; |
| } |
| return false; |
| } |
| |
| bool |
| CharacterRange::IsCanonical(const CharacterRangeVector& ranges) |
| { |
| int n = ranges.length(); |
| if (n <= 1) |
| return true; |
| |
| int max = ranges[0].to(); |
| for (int i = 1; i < n; i++) { |
| CharacterRange next_range = ranges[i]; |
| if (next_range.from() <= max + 1) |
| return false; |
| max = next_range.to(); |
| } |
| return true; |
| } |
| |
| // Move a number of elements in a zonelist to another position |
| // in the same list. Handles overlapping source and target areas. |
| static |
| void MoveRanges(CharacterRangeVector& list, int from, int to, int count) |
| { |
| // Ranges are potentially overlapping. |
| if (from < to) { |
| for (int i = count - 1; i >= 0; i--) |
| list[to + i] = list[from + i]; |
| } else { |
| for (int i = 0; i < count; i++) |
| list[to + i] = list[from + i]; |
| } |
| } |
| |
| static int |
| InsertRangeInCanonicalList(CharacterRangeVector& list, |
| int count, |
| CharacterRange insert) |
| { |
| // Inserts a range into list[0..count[, which must be sorted |
| // by from value and non-overlapping and non-adjacent, using at most |
| // list[0..count] for the result. Returns the number of resulting |
| // canonicalized ranges. Inserting a range may collapse existing ranges into |
| // fewer ranges, so the return value can be anything in the range 1..count+1. |
| char16_t from = insert.from(); |
| char16_t to = insert.to(); |
| int start_pos = 0; |
| int end_pos = count; |
| for (int i = count - 1; i >= 0; i--) { |
| CharacterRange current = list[i]; |
| if (current.from() > to + 1) { |
| end_pos = i; |
| } else if (current.to() + 1 < from) { |
| start_pos = i + 1; |
| break; |
| } |
| } |
| |
| // Inserted range overlaps, or is adjacent to, ranges at positions |
| // [start_pos..end_pos[. Ranges before start_pos or at or after end_pos are |
| // not affected by the insertion. |
| // If start_pos == end_pos, the range must be inserted before start_pos. |
| // if start_pos < end_pos, the entire range from start_pos to end_pos |
| // must be merged with the insert range. |
| |
| if (start_pos == end_pos) { |
| // Insert between existing ranges at position start_pos. |
| if (start_pos < count) { |
| MoveRanges(list, start_pos, start_pos + 1, count - start_pos); |
| } |
| list[start_pos] = insert; |
| return count + 1; |
| } |
| if (start_pos + 1 == end_pos) { |
| // Replace single existing range at position start_pos. |
| CharacterRange to_replace = list[start_pos]; |
| int new_from = Min(to_replace.from(), from); |
| int new_to = Max(to_replace.to(), to); |
| list[start_pos] = CharacterRange(new_from, new_to); |
| return count; |
| } |
| // Replace a number of existing ranges from start_pos to end_pos - 1. |
| // Move the remaining ranges down. |
| |
| int new_from = Min(list[start_pos].from(), from); |
| int new_to = Max(list[end_pos - 1].to(), to); |
| if (end_pos < count) { |
| MoveRanges(list, end_pos, start_pos + 1, count - end_pos); |
| } |
| list[start_pos] = CharacterRange(new_from, new_to); |
| return count - (end_pos - start_pos) + 1; |
| } |
| |
| void |
| CharacterRange::Canonicalize(CharacterRangeVector& character_ranges) |
| { |
| if (character_ranges.length() <= 1) return; |
| // Check whether ranges are already canonical (increasing, non-overlapping, |
| // non-adjacent). |
| int n = character_ranges.length(); |
| int max = character_ranges[0].to(); |
| int i = 1; |
| while (i < n) { |
| CharacterRange current = character_ranges[i]; |
| if (current.from() <= max + 1) { |
| break; |
| } |
| max = current.to(); |
| i++; |
| } |
| // Canonical until the i'th range. If that's all of them, we are done. |
| if (i == n) return; |
| |
| // The ranges at index i and forward are not canonicalized. Make them so by |
| // doing the equivalent of insertion sort (inserting each into the previous |
| // list, in order). |
| // Notice that inserting a range can reduce the number of ranges in the |
| // result due to combining of adjacent and overlapping ranges. |
| int read = i; // Range to insert. |
| size_t num_canonical = i; // Length of canonicalized part of list. |
| do { |
| num_canonical = InsertRangeInCanonicalList(character_ranges, |
| num_canonical, |
| character_ranges[read]); |
| read++; |
| } while (read < n); |
| |
| while (character_ranges.length() > num_canonical) |
| character_ranges.popBack(); |
| |
| MOZ_ASSERT(CharacterRange::IsCanonical(character_ranges)); |
| } |
| |
| // ------------------------------------------------------------------- |
| // SeqRegExpNode |
| |
| class VisitMarker |
| { |
| public: |
| explicit VisitMarker(NodeInfo* info) |
| : info_(info) |
| { |
| MOZ_ASSERT(!info->visited); |
| info->visited = true; |
| } |
| ~VisitMarker() { |
| info_->visited = false; |
| } |
| private: |
| NodeInfo* info_; |
| }; |
| |
| bool |
| SeqRegExpNode::FillInBMInfo(int offset, |
| int budget, |
| BoyerMooreLookahead* bm, |
| bool not_at_start) |
| { |
| if (!bm->CheckOverRecursed()) |
| return false; |
| if (!on_success_->FillInBMInfo(offset, budget - 1, bm, not_at_start)) |
| return false; |
| if (offset == 0) |
| set_bm_info(not_at_start, bm); |
| return true; |
| } |
| |
| RegExpNode* |
| SeqRegExpNode::FilterASCII(int depth, bool ignore_case) |
| { |
| if (info()->replacement_calculated) |
| return replacement(); |
| |
| if (depth < 0) |
| return this; |
| |
| MOZ_ASSERT(!info()->visited); |
| VisitMarker marker(info()); |
| return FilterSuccessor(depth - 1, ignore_case); |
| } |
| |
| RegExpNode* |
| SeqRegExpNode::FilterSuccessor(int depth, bool ignore_case) |
| { |
| RegExpNode* next = on_success_->FilterASCII(depth - 1, ignore_case); |
| if (next == nullptr) |
| return set_replacement(nullptr); |
| |
| on_success_ = next; |
| return set_replacement(this); |
| } |
| |
| // ------------------------------------------------------------------- |
| // ActionNode |
| |
| int |
| ActionNode::EatsAtLeast(int still_to_find, int budget, bool not_at_start) |
| { |
| if (budget <= 0) |
| return 0; |
| if (action_type_ == POSITIVE_SUBMATCH_SUCCESS) |
| return 0; // Rewinds input! |
| return on_success()->EatsAtLeast(still_to_find, |
| budget - 1, |
| not_at_start); |
| } |
| |
| bool |
| ActionNode::FillInBMInfo(int offset, |
| int budget, |
| BoyerMooreLookahead* bm, |
| bool not_at_start) |
| { |
| if (!bm->CheckOverRecursed()) |
| return false; |
| |
| if (action_type_ == BEGIN_SUBMATCH) { |
| bm->SetRest(offset); |
| } else if (action_type_ != POSITIVE_SUBMATCH_SUCCESS) { |
| if (!on_success()->FillInBMInfo(offset, budget - 1, bm, not_at_start)) |
| return false; |
| } |
| SaveBMInfo(bm, not_at_start, offset); |
| |
| return true; |
| } |
| |
| /* static */ ActionNode* |
| ActionNode::SetRegister(int reg, |
| int val, |
| RegExpNode* on_success) |
| { |
| ActionNode* result = on_success->alloc()->newInfallible<ActionNode>(SET_REGISTER, on_success); |
| result->data_.u_store_register.reg = reg; |
| result->data_.u_store_register.value = val; |
| return result; |
| } |
| |
| /* static */ ActionNode* |
| ActionNode::IncrementRegister(int reg, RegExpNode* on_success) |
| { |
| ActionNode* result = on_success->alloc()->newInfallible<ActionNode>(INCREMENT_REGISTER, on_success); |
| result->data_.u_increment_register.reg = reg; |
| return result; |
| } |
| |
| /* static */ ActionNode* |
| ActionNode::StorePosition(int reg, bool is_capture, RegExpNode* on_success) |
| { |
| ActionNode* result = on_success->alloc()->newInfallible<ActionNode>(STORE_POSITION, on_success); |
| result->data_.u_position_register.reg = reg; |
| result->data_.u_position_register.is_capture = is_capture; |
| return result; |
| } |
| |
| /* static */ ActionNode* |
| ActionNode::ClearCaptures(Interval range, RegExpNode* on_success) |
| { |
| ActionNode* result = on_success->alloc()->newInfallible<ActionNode>(CLEAR_CAPTURES, on_success); |
| result->data_.u_clear_captures.range_from = range.from(); |
| result->data_.u_clear_captures.range_to = range.to(); |
| return result; |
| } |
| |
| /* static */ ActionNode* |
| ActionNode::BeginSubmatch(int stack_pointer_reg, int position_reg, RegExpNode* on_success) |
| { |
| ActionNode* result = on_success->alloc()->newInfallible<ActionNode>(BEGIN_SUBMATCH, on_success); |
| result->data_.u_submatch.stack_pointer_register = stack_pointer_reg; |
| result->data_.u_submatch.current_position_register = position_reg; |
| return result; |
| } |
| |
| /* static */ ActionNode* |
| ActionNode::PositiveSubmatchSuccess(int stack_pointer_reg, |
| int restore_reg, |
| int clear_capture_count, |
| int clear_capture_from, |
| RegExpNode* on_success) |
| { |
| ActionNode* result = on_success->alloc()->newInfallible<ActionNode>(POSITIVE_SUBMATCH_SUCCESS, on_success); |
| result->data_.u_submatch.stack_pointer_register = stack_pointer_reg; |
| result->data_.u_submatch.current_position_register = restore_reg; |
| result->data_.u_submatch.clear_register_count = clear_capture_count; |
| result->data_.u_submatch.clear_register_from = clear_capture_from; |
| return result; |
| } |
| |
| /* static */ ActionNode* |
| ActionNode::EmptyMatchCheck(int start_register, |
| int repetition_register, |
| int repetition_limit, |
| RegExpNode* on_success) |
| { |
| ActionNode* result = on_success->alloc()->newInfallible<ActionNode>(EMPTY_MATCH_CHECK, on_success); |
| result->data_.u_empty_match_check.start_register = start_register; |
| result->data_.u_empty_match_check.repetition_register = repetition_register; |
| result->data_.u_empty_match_check.repetition_limit = repetition_limit; |
| return result; |
| } |
| |
| // ------------------------------------------------------------------- |
| // TextNode |
| |
| int |
| TextNode::EatsAtLeast(int still_to_find, int budget, bool not_at_start) |
| { |
| int answer = Length(); |
| if (answer >= still_to_find) |
| return answer; |
| if (budget <= 0) |
| return answer; |
| |
| // We are not at start after this node so we set the last argument to 'true'. |
| return answer + on_success()->EatsAtLeast(still_to_find - answer, |
| budget - 1, |
| true); |
| } |
| |
| int |
| TextNode::GreedyLoopTextLength() |
| { |
| TextElement elm = elements()[elements().length() - 1]; |
| return elm.cp_offset() + elm.length(); |
| } |
| |
| RegExpNode* |
| TextNode::FilterASCII(int depth, bool ignore_case) |
| { |
| if (info()->replacement_calculated) |
| return replacement(); |
| |
| if (depth < 0) |
| return this; |
| |
| MOZ_ASSERT(!info()->visited); |
| VisitMarker marker(info()); |
| int element_count = elements().length(); |
| for (int i = 0; i < element_count; i++) { |
| TextElement elm = elements()[i]; |
| if (elm.text_type() == TextElement::ATOM) { |
| CharacterVector& quarks = const_cast<CharacterVector&>(elm.atom()->data()); |
| for (size_t j = 0; j < quarks.length(); j++) { |
| uint16_t c = quarks[j]; |
| if (c <= kMaxOneByteCharCode) |
| continue; |
| if (!ignore_case) |
| return set_replacement(nullptr); |
| |
| // Here, we need to check for characters whose upper and lower cases |
| // are outside the Latin-1 range. |
| char16_t converted = ConvertNonLatin1ToLatin1(c); |
| if (converted == 0) { |
| // Character is outside Latin-1 completely |
| return set_replacement(nullptr); |
| } |
| |
| // Convert quark to Latin-1 in place. |
| quarks[j] = converted; |
| } |
| } else { |
| MOZ_ASSERT(elm.text_type() == TextElement::CHAR_CLASS); |
| RegExpCharacterClass* cc = elm.char_class(); |
| |
| CharacterRangeVector& ranges = cc->ranges(alloc()); |
| if (!CharacterRange::IsCanonical(ranges)) |
| CharacterRange::Canonicalize(ranges); |
| |
| // Now they are in order so we only need to look at the first. |
| int range_count = ranges.length(); |
| if (cc->is_negated()) { |
| if (range_count != 0 && |
| ranges[0].from() == 0 && |
| ranges[0].to() >= kMaxOneByteCharCode) |
| { |
| // This will be handled in a later filter. |
| if (ignore_case && RangesContainLatin1Equivalents(ranges)) |
| continue; |
| return set_replacement(nullptr); |
| } |
| } else { |
| if (range_count == 0 || |
| ranges[0].from() > kMaxOneByteCharCode) |
| { |
| // This will be handled in a later filter. |
| if (ignore_case && RangesContainLatin1Equivalents(ranges)) |
| continue; |
| return set_replacement(nullptr); |
| } |
| } |
| } |
| } |
| return FilterSuccessor(depth - 1, ignore_case); |
| } |
| |
| void |
| TextNode::CalculateOffsets() |
| { |
| int element_count = elements().length(); |
| |
| // Set up the offsets of the elements relative to the start. This is a fixed |
| // quantity since a TextNode can only contain fixed-width things. |
| int cp_offset = 0; |
| for (int i = 0; i < element_count; i++) { |
| TextElement& elm = elements()[i]; |
| elm.set_cp_offset(cp_offset); |
| cp_offset += elm.length(); |
| } |
| } |
| |
| void TextNode::MakeCaseIndependent(bool is_ascii) |
| { |
| int element_count = elements().length(); |
| for (int i = 0; i < element_count; i++) { |
| TextElement elm = elements()[i]; |
| if (elm.text_type() == TextElement::CHAR_CLASS) { |
| RegExpCharacterClass* cc = elm.char_class(); |
| |
| // None of the standard character classes is different in the case |
| // independent case and it slows us down if we don't know that. |
| if (cc->is_standard(alloc())) |
| continue; |
| |
| CharacterRangeVector& ranges = cc->ranges(alloc()); |
| int range_count = ranges.length(); |
| for (int j = 0; j < range_count; j++) |
| ranges[j].AddCaseEquivalents(is_ascii, &ranges); |
| } |
| } |
| } |
| |
| // ------------------------------------------------------------------- |
| // AssertionNode |
| |
| int |
| AssertionNode::EatsAtLeast(int still_to_find, int budget, bool not_at_start) |
| { |
| if (budget <= 0) |
| return 0; |
| |
| // If we know we are not at the start and we are asked "how many characters |
| // will you match if you succeed?" then we can answer anything since false |
| // implies false. So lets just return the max answer (still_to_find) since |
| // that won't prevent us from preloading a lot of characters for the other |
| // branches in the node graph. |
| if (assertion_type() == AT_START && not_at_start) |
| return still_to_find; |
| |
| return on_success()->EatsAtLeast(still_to_find, budget - 1, not_at_start); |
| } |
| |
| bool |
| AssertionNode::FillInBMInfo(int offset, int budget, BoyerMooreLookahead* bm, bool not_at_start) |
| { |
| if (!bm->CheckOverRecursed()) |
| return false; |
| |
| // Match the behaviour of EatsAtLeast on this node. |
| if (assertion_type() == AT_START && not_at_start) |
| return true; |
| |
| if (!on_success()->FillInBMInfo(offset, budget - 1, bm, not_at_start)) |
| return false; |
| SaveBMInfo(bm, not_at_start, offset); |
| return true; |
| } |
| |
| // ------------------------------------------------------------------- |
| // BackReferenceNode |
| |
| int |
| BackReferenceNode::EatsAtLeast(int still_to_find, int budget, bool not_at_start) |
| { |
| if (budget <= 0) |
| return 0; |
| return on_success()->EatsAtLeast(still_to_find, budget - 1, not_at_start); |
| } |
| |
| bool |
| BackReferenceNode::FillInBMInfo(int offset, int budget, BoyerMooreLookahead* bm, bool not_at_start) |
| { |
| // Working out the set of characters that a backreference can match is too |
| // hard, so we just say that any character can match. |
| bm->SetRest(offset); |
| SaveBMInfo(bm, not_at_start, offset); |
| return true; |
| } |
| |
| // ------------------------------------------------------------------- |
| // ChoiceNode |
| |
| int |
| ChoiceNode::EatsAtLeastHelper(int still_to_find, |
| int budget, |
| RegExpNode* ignore_this_node, |
| bool not_at_start) |
| { |
| if (budget <= 0) |
| return 0; |
| |
| int min = 100; |
| size_t choice_count = alternatives().length(); |
| budget = (budget - 1) / choice_count; |
| for (size_t i = 0; i < choice_count; i++) { |
| RegExpNode* node = alternatives()[i].node(); |
| if (node == ignore_this_node) continue; |
| int node_eats_at_least = |
| node->EatsAtLeast(still_to_find, budget, not_at_start); |
| if (node_eats_at_least < min) |
| min = node_eats_at_least; |
| if (min == 0) |
| return 0; |
| } |
| return min; |
| } |
| |
| int |
| ChoiceNode::EatsAtLeast(int still_to_find, int budget, bool not_at_start) |
| { |
| return EatsAtLeastHelper(still_to_find, |
| budget, |
| nullptr, |
| not_at_start); |
| } |
| |
| void |
| ChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| RegExpCompiler* compiler, |
| int characters_filled_in, |
| bool not_at_start) |
| { |
| not_at_start = (not_at_start || not_at_start_); |
| int choice_count = alternatives().length(); |
| MOZ_ASSERT(choice_count > 0); |
| alternatives()[0].node()->GetQuickCheckDetails(details, |
| compiler, |
| characters_filled_in, |
| not_at_start); |
| for (int i = 1; i < choice_count; i++) { |
| QuickCheckDetails new_details(details->characters()); |
| RegExpNode* node = alternatives()[i].node(); |
| node->GetQuickCheckDetails(&new_details, compiler, |
| characters_filled_in, |
| not_at_start); |
| // Here we merge the quick match details of the two branches. |
| details->Merge(&new_details, characters_filled_in); |
| } |
| } |
| |
| bool |
| ChoiceNode::FillInBMInfo(int offset, |
| int budget, |
| BoyerMooreLookahead* bm, |
| bool not_at_start) |
| { |
| if (!bm->CheckOverRecursed()) |
| return false; |
| |
| const GuardedAlternativeVector& alts = alternatives(); |
| budget = (budget - 1) / alts.length(); |
| for (size_t i = 0; i < alts.length(); i++) { |
| const GuardedAlternative& alt = alts[i]; |
| if (alt.guards() != nullptr && alt.guards()->length() != 0) { |
| bm->SetRest(offset); // Give up trying to fill in info. |
| SaveBMInfo(bm, not_at_start, offset); |
| return true; |
| } |
| if (!alt.node()->FillInBMInfo(offset, budget, bm, not_at_start)) |
| return false; |
| } |
| SaveBMInfo(bm, not_at_start, offset); |
| return true; |
| } |
| |
| RegExpNode* |
| ChoiceNode::FilterASCII(int depth, bool ignore_case) |
| { |
| if (info()->replacement_calculated) |
| return replacement(); |
| if (depth < 0) |
| return this; |
| if (info()->visited) |
| return this; |
| VisitMarker marker(info()); |
| int choice_count = alternatives().length(); |
| |
| for (int i = 0; i < choice_count; i++) { |
| const GuardedAlternative alternative = alternatives()[i]; |
| if (alternative.guards() != nullptr && alternative.guards()->length() != 0) { |
| set_replacement(this); |
| return this; |
| } |
| } |
| |
| int surviving = 0; |
| RegExpNode* survivor = nullptr; |
| for (int i = 0; i < choice_count; i++) { |
| GuardedAlternative alternative = alternatives()[i]; |
| RegExpNode* replacement = |
| alternative.node()->FilterASCII(depth - 1, ignore_case); |
| MOZ_ASSERT(replacement != this); // No missing EMPTY_MATCH_CHECK. |
| if (replacement != nullptr) { |
| alternatives()[i].set_node(replacement); |
| surviving++; |
| survivor = replacement; |
| } |
| } |
| if (surviving < 2) |
| return set_replacement(survivor); |
| |
| set_replacement(this); |
| if (surviving == choice_count) |
| return this; |
| |
| // Only some of the nodes survived the filtering. We need to rebuild the |
| // alternatives list. |
| GuardedAlternativeVector new_alternatives(*alloc()); |
| new_alternatives.reserve(surviving); |
| for (int i = 0; i < choice_count; i++) { |
| RegExpNode* replacement = |
| alternatives()[i].node()->FilterASCII(depth - 1, ignore_case); |
| if (replacement != nullptr) { |
| alternatives()[i].set_node(replacement); |
| AutoEnterOOMUnsafeRegion oomUnsafe; |
| if (!new_alternatives.append(alternatives()[i])) |
| oomUnsafe.crash("ChoiceNode::FilterASCII"); |
| } |
| } |
| |
| alternatives_ = Move(new_alternatives); |
| return this; |
| } |
| |
| // ------------------------------------------------------------------- |
| // NegativeLookaheadChoiceNode |
| |
| bool |
| NegativeLookaheadChoiceNode::FillInBMInfo(int offset, |
| int budget, |
| BoyerMooreLookahead* bm, |
| bool not_at_start) |
| { |
| if (!bm->CheckOverRecursed()) |
| return false; |
| |
| if (!alternatives()[1].node()->FillInBMInfo(offset, budget - 1, bm, not_at_start)) |
| return false; |
| if (offset == 0) |
| set_bm_info(not_at_start, bm); |
| return true; |
| } |
| |
| int |
| NegativeLookaheadChoiceNode::EatsAtLeast(int still_to_find, int budget, bool not_at_start) |
| { |
| if (budget <= 0) |
| return 0; |
| |
| // Alternative 0 is the negative lookahead, alternative 1 is what comes |
| // afterwards. |
| RegExpNode* node = alternatives()[1].node(); |
| return node->EatsAtLeast(still_to_find, budget - 1, not_at_start); |
| } |
| |
| void |
| NegativeLookaheadChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| RegExpCompiler* compiler, |
| int filled_in, |
| bool not_at_start) |
| { |
| // Alternative 0 is the negative lookahead, alternative 1 is what comes |
| // afterwards. |
| RegExpNode* node = alternatives()[1].node(); |
| return node->GetQuickCheckDetails(details, compiler, filled_in, not_at_start); |
| } |
| |
| RegExpNode* |
| NegativeLookaheadChoiceNode::FilterASCII(int depth, bool ignore_case) |
| { |
| if (info()->replacement_calculated) |
| return replacement(); |
| if (depth < 0) |
| return this; |
| if (info()->visited) |
| return this; |
| |
| VisitMarker marker(info()); |
| |
| // Alternative 0 is the negative lookahead, alternative 1 is what comes |
| // afterwards. |
| RegExpNode* node = alternatives()[1].node(); |
| RegExpNode* replacement = node->FilterASCII(depth - 1, ignore_case); |
| |
| if (replacement == nullptr) |
| return set_replacement(nullptr); |
| alternatives()[1].set_node(replacement); |
| |
| RegExpNode* neg_node = alternatives()[0].node(); |
| RegExpNode* neg_replacement = neg_node->FilterASCII(depth - 1, ignore_case); |
| |
| // If the negative lookahead is always going to fail then |
| // we don't need to check it. |
| if (neg_replacement == nullptr) |
| return set_replacement(replacement); |
| |
| alternatives()[0].set_node(neg_replacement); |
| return set_replacement(this); |
| } |
| |
| // ------------------------------------------------------------------- |
| // LoopChoiceNode |
| |
| void |
| GuardedAlternative::AddGuard(LifoAlloc* alloc, Guard* guard) |
| { |
| if (guards_ == nullptr) |
| guards_ = alloc->newInfallible<GuardVector>(*alloc); |
| guards_->append(guard); |
| } |
| |
| void |
| LoopChoiceNode::AddLoopAlternative(GuardedAlternative alt) |
| { |
| MOZ_ASSERT(loop_node_ == nullptr); |
| AddAlternative(alt); |
| loop_node_ = alt.node(); |
| } |
| |
| |
| void |
| LoopChoiceNode::AddContinueAlternative(GuardedAlternative alt) |
| { |
| MOZ_ASSERT(continue_node_ == nullptr); |
| AddAlternative(alt); |
| continue_node_ = alt.node(); |
| } |
| |
| int |
| LoopChoiceNode::EatsAtLeast(int still_to_find, int budget, bool not_at_start) |
| { |
| return EatsAtLeastHelper(still_to_find, |
| budget - 1, |
| loop_node_, |
| not_at_start); |
| } |
| |
| void |
| LoopChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| RegExpCompiler* compiler, |
| int characters_filled_in, |
| bool not_at_start) |
| { |
| if (body_can_be_zero_length_ || info()->visited) |
| return; |
| VisitMarker marker(info()); |
| return ChoiceNode::GetQuickCheckDetails(details, |
| compiler, |
| characters_filled_in, |
| not_at_start); |
| } |
| |
| bool |
| LoopChoiceNode::FillInBMInfo(int offset, |
| int budget, |
| BoyerMooreLookahead* bm, |
| bool not_at_start) |
| { |
| if (body_can_be_zero_length_ || budget <= 0) { |
| bm->SetRest(offset); |
| SaveBMInfo(bm, not_at_start, offset); |
| return true; |
| } |
| if (!ChoiceNode::FillInBMInfo(offset, budget - 1, bm, not_at_start)) |
| return false; |
| SaveBMInfo(bm, not_at_start, offset); |
| return true; |
| } |
| |
| RegExpNode* |
| LoopChoiceNode::FilterASCII(int depth, bool ignore_case) |
| { |
| if (info()->replacement_calculated) |
| return replacement(); |
| if (depth < 0) |
| return this; |
| if (info()->visited) |
| return this; |
| |
| { |
| VisitMarker marker(info()); |
| |
| RegExpNode* continue_replacement = |
| continue_node_->FilterASCII(depth - 1, ignore_case); |
| |
| // If we can't continue after the loop then there is no sense in doing the |
| // loop. |
| if (continue_replacement == nullptr) |
| return set_replacement(nullptr); |
| } |
| |
| return ChoiceNode::FilterASCII(depth - 1, ignore_case); |
| } |
| |
| // ------------------------------------------------------------------- |
| // Analysis |
| |
| void |
| Analysis::EnsureAnalyzed(RegExpNode* that) |
| { |
| JS_CHECK_RECURSION(cx, fail("Stack overflow"); return); |
| |
| if (that->info()->been_analyzed || that->info()->being_analyzed) |
| return; |
| that->info()->being_analyzed = true; |
| that->Accept(this); |
| that->info()->being_analyzed = false; |
| that->info()->been_analyzed = true; |
| } |
| |
| void |
| Analysis::VisitEnd(EndNode* that) |
| { |
| // nothing to do |
| } |
| |
| void |
| Analysis::VisitText(TextNode* that) |
| { |
| if (ignore_case_) |
| that->MakeCaseIndependent(is_ascii_); |
| EnsureAnalyzed(that->on_success()); |
| if (!has_failed()) { |
| that->CalculateOffsets(); |
| } |
| } |
| |
| void |
| Analysis::VisitAction(ActionNode* that) |
| { |
| RegExpNode* target = that->on_success(); |
| EnsureAnalyzed(target); |
| |
| if (!has_failed()) { |
| // If the next node is interested in what it follows then this node |
| // has to be interested too so it can pass the information on. |
| that->info()->AddFromFollowing(target->info()); |
| } |
| } |
| |
| void |
| Analysis::VisitChoice(ChoiceNode* that) |
| { |
| NodeInfo* info = that->info(); |
| |
| for (size_t i = 0; i < that->alternatives().length(); i++) { |
| RegExpNode* node = that->alternatives()[i].node(); |
| EnsureAnalyzed(node); |
| if (has_failed()) return; |
| |
| // Anything the following nodes need to know has to be known by |
| // this node also, so it can pass it on. |
| info->AddFromFollowing(node->info()); |
| } |
| } |
| |
| void |
| Analysis::VisitLoopChoice(LoopChoiceNode* that) |
| { |
| NodeInfo* info = that->info(); |
| for (size_t i = 0; i < that->alternatives().length(); i++) { |
| RegExpNode* node = that->alternatives()[i].node(); |
| if (node != that->loop_node()) { |
| EnsureAnalyzed(node); |
| if (has_failed()) return; |
| info->AddFromFollowing(node->info()); |
| } |
| } |
| |
| // Check the loop last since it may need the value of this node |
| // to get a correct result. |
| EnsureAnalyzed(that->loop_node()); |
| if (!has_failed()) |
| info->AddFromFollowing(that->loop_node()->info()); |
| } |
| |
| void |
| Analysis::VisitBackReference(BackReferenceNode* that) |
| { |
| EnsureAnalyzed(that->on_success()); |
| } |
| |
| void |
| Analysis::VisitAssertion(AssertionNode* that) |
| { |
| EnsureAnalyzed(that->on_success()); |
| } |
| |
| // ------------------------------------------------------------------- |
| // Implementation of the Irregexp regular expression engine. |
| // |
| // The Irregexp regular expression engine is intended to be a complete |
| // implementation of ECMAScript regular expressions. It generates either |
| // bytecodes or native code. |
| |
| // The Irregexp regexp engine is structured in three steps. |
| // 1) The parser generates an abstract syntax tree. See RegExpAST.cpp. |
| // 2) From the AST a node network is created. The nodes are all |
| // subclasses of RegExpNode. The nodes represent states when |
| // executing a regular expression. Several optimizations are |
| // performed on the node network. |
| // 3) From the nodes we generate either byte codes or native code |
| // that can actually execute the regular expression (perform |
| // the search). The code generation step is described in more |
| // detail below. |
| |
| // Code generation. |
| // |
| // The nodes are divided into four main categories. |
| // * Choice nodes |
| // These represent places where the regular expression can |
| // match in more than one way. For example on entry to an |
| // alternation (foo|bar) or a repetition (*, +, ? or {}). |
| // * Action nodes |
| // These represent places where some action should be |
| // performed. Examples include recording the current position |
| // in the input string to a register (in order to implement |
| // captures) or other actions on register for example in order |
| // to implement the counters needed for {} repetitions. |
| // * Matching nodes |
| // These attempt to match some element part of the input string. |
| // Examples of elements include character classes, plain strings |
| // or back references. |
| // * End nodes |
| // These are used to implement the actions required on finding |
| // a successful match or failing to find a match. |
| // |
| // The code generated (whether as byte codes or native code) maintains |
| // some state as it runs. This consists of the following elements: |
| // |
| // * The capture registers. Used for string captures. |
| // * Other registers. Used for counters etc. |
| // * The current position. |
| // * The stack of backtracking information. Used when a matching node |
| // fails to find a match and needs to try an alternative. |
| // |
| // Conceptual regular expression execution model: |
| // |
| // There is a simple conceptual model of regular expression execution |
| // which will be presented first. The actual code generated is a more |
| // efficient simulation of the simple conceptual model: |
| // |
| // * Choice nodes are implemented as follows: |
| // For each choice except the last { |
| // push current position |
| // push backtrack code location |
| // <generate code to test for choice> |
| // backtrack code location: |
| // pop current position |
| // } |
| // <generate code to test for last choice> |
| // |
| // * Actions nodes are generated as follows |
| // <push affected registers on backtrack stack> |
| // <generate code to perform action> |
| // push backtrack code location |
| // <generate code to test for following nodes> |
| // backtrack code location: |
| // <pop affected registers to restore their state> |
| // <pop backtrack location from stack and go to it> |
| // |
| // * Matching nodes are generated as follows: |
| // if input string matches at current position |
| // update current position |
| // <generate code to test for following nodes> |
| // else |
| // <pop backtrack location from stack and go to it> |
| // |
| // Thus it can be seen that the current position is saved and restored |
| // by the choice nodes, whereas the registers are saved and restored by |
| // by the action nodes that manipulate them. |
| // |
| // The other interesting aspect of this model is that nodes are generated |
| // at the point where they are needed by a recursive call to Emit(). If |
| // the node has already been code generated then the Emit() call will |
| // generate a jump to the previously generated code instead. In order to |
| // limit recursion it is possible for the Emit() function to put the node |
| // on a work list for later generation and instead generate a jump. The |
| // destination of the jump is resolved later when the code is generated. |
| // |
| // Actual regular expression code generation. |
| // |
| // Code generation is actually more complicated than the above. In order |
| // to improve the efficiency of the generated code some optimizations are |
| // performed |
| // |
| // * Choice nodes have 1-character lookahead. |
| // A choice node looks at the following character and eliminates some of |
| // the choices immediately based on that character. This is not yet |
| // implemented. |
| // * Simple greedy loops store reduced backtracking information. |
| // A quantifier like /.*foo/m will greedily match the whole input. It will |
| // then need to backtrack to a point where it can match "foo". The naive |
| // implementation of this would push each character position onto the |
| // backtracking stack, then pop them off one by one. This would use space |
| // proportional to the length of the input string. However since the "." |
| // can only match in one way and always has a constant length (in this case |
| // of 1) it suffices to store the current position on the top of the stack |
| // once. Matching now becomes merely incrementing the current position and |
| // backtracking becomes decrementing the current position and checking the |
| // result against the stored current position. This is faster and saves |
| // space. |
| // * The current state is virtualized. |
| // This is used to defer expensive operations until it is clear that they |
| // are needed and to generate code for a node more than once, allowing |
| // specialized an efficient versions of the code to be created. This is |
| // explained in the section below. |
| // |
| // Execution state virtualization. |
| // |
| // Instead of emitting code, nodes that manipulate the state can record their |
| // manipulation in an object called the Trace. The Trace object can record a |
| // current position offset, an optional backtrack code location on the top of |
| // the virtualized backtrack stack and some register changes. When a node is |
| // to be emitted it can flush the Trace or update it. Flushing the Trace |
| // will emit code to bring the actual state into line with the virtual state. |
| // Avoiding flushing the state can postpone some work (e.g. updates of capture |
| // registers). Postponing work can save time when executing the regular |
| // expression since it may be found that the work never has to be done as a |
| // failure to match can occur. In addition it is much faster to jump to a |
| // known backtrack code location than it is to pop an unknown backtrack |
| // location from the stack and jump there. |
| // |
| // The virtual state found in the Trace affects code generation. For example |
| // the virtual state contains the difference between the actual current |
| // position and the virtual current position, and matching code needs to use |
| // this offset to attempt a match in the correct location of the input |
| // string. Therefore code generated for a non-trivial trace is specialized |
| // to that trace. The code generator therefore has the ability to generate |
| // code for each node several times. In order to limit the size of the |
| // generated code there is an arbitrary limit on how many specialized sets of |
| // code may be generated for a given node. If the limit is reached, the |
| // trace is flushed and a generic version of the code for a node is emitted. |
| // This is subsequently used for that node. The code emitted for non-generic |
| // trace is not recorded in the node and so it cannot currently be reused in |
| // the event that code generation is requested for an identical trace. |
| |
| /* static */ TextElement |
| TextElement::Atom(RegExpAtom* atom) |
| { |
| return TextElement(ATOM, atom); |
| } |
| |
| /* static */ TextElement |
| TextElement::CharClass(RegExpCharacterClass* char_class) |
| { |
| return TextElement(CHAR_CLASS, char_class); |
| } |
| |
| int |
| TextElement::length() const |
| { |
| switch (text_type()) { |
| case ATOM: |
| return atom()->length(); |
| case CHAR_CLASS: |
| return 1; |
| } |
| MOZ_CRASH("Bad text type"); |
| } |
| |
| class FrequencyCollator |
| { |
| public: |
| FrequencyCollator() : total_samples_(0) { |
| for (int i = 0; i < RegExpMacroAssembler::kTableSize; i++) { |
| frequencies_[i] = CharacterFrequency(i); |
| } |
| } |
| |
| void CountCharacter(int character) { |
| int index = (character & RegExpMacroAssembler::kTableMask); |
| frequencies_[index].Increment(); |
| total_samples_++; |
| } |
| |
| // Does not measure in percent, but rather per-128 (the table size from the |
| // regexp macro assembler). |
| int Frequency(int in_character) { |
| MOZ_ASSERT((in_character & RegExpMacroAssembler::kTableMask) == in_character); |
| if (total_samples_ < 1) return 1; // Division by zero. |
| int freq_in_per128 = |
| (frequencies_[in_character].counter() * 128) / total_samples_; |
| return freq_in_per128; |
| } |
| |
| private: |
| class CharacterFrequency { |
| public: |
| CharacterFrequency() : counter_(0), character_(-1) { } |
| explicit CharacterFrequency(int character) |
| : counter_(0), character_(character) |
| {} |
| |
| void Increment() { counter_++; } |
| int counter() { return counter_; } |
| int character() { return character_; } |
| |
| private: |
| int counter_; |
| int character_; |
| }; |
| |
| private: |
| CharacterFrequency frequencies_[RegExpMacroAssembler::kTableSize]; |
| int total_samples_; |
| }; |
| |
| class irregexp::RegExpCompiler |
| { |
| public: |
| RegExpCompiler(JSContext* cx, LifoAlloc* alloc, int capture_count, |
| bool ignore_case, bool is_ascii, bool match_only); |
| |
| int AllocateRegister() { |
| if (next_register_ >= RegExpMacroAssembler::kMaxRegister) { |
| reg_exp_too_big_ = true; |
| return next_register_; |
| } |
| return next_register_++; |
| } |
| |
| RegExpCode Assemble(JSContext* cx, |
| RegExpMacroAssembler* assembler, |
| RegExpNode* start, |
| int capture_count); |
| |
| inline void AddWork(RegExpNode* node) { |
| AutoEnterOOMUnsafeRegion oomUnsafe; |
| if (!work_list_.append(node)) |
| oomUnsafe.crash("AddWork"); |
| } |
| |
| static const int kImplementationOffset = 0; |
| static const int kNumberOfRegistersOffset = 0; |
| static const int kCodeOffset = 1; |
| |
| RegExpMacroAssembler* macro_assembler() { return macro_assembler_; } |
| EndNode* accept() { return accept_; } |
| |
| static const int kMaxRecursion = 100; |
| inline int recursion_depth() { return recursion_depth_; } |
| inline void IncrementRecursionDepth() { recursion_depth_++; } |
| inline void DecrementRecursionDepth() { recursion_depth_--; } |
| |
| void SetRegExpTooBig() { reg_exp_too_big_ = true; } |
| |
| inline bool ignore_case() { return ignore_case_; } |
| inline bool ascii() { return ascii_; } |
| FrequencyCollator* frequency_collator() { return &frequency_collator_; } |
| |
| int current_expansion_factor() { return current_expansion_factor_; } |
| void set_current_expansion_factor(int value) { |
| current_expansion_factor_ = value; |
| } |
| |
| JSContext* cx() const { return cx_; } |
| LifoAlloc* alloc() const { return alloc_; } |
| |
| static const int kNoRegister = -1; |
| |
| private: |
| EndNode* accept_; |
| int next_register_; |
| Vector<RegExpNode*, 4, SystemAllocPolicy> work_list_; |
| int recursion_depth_; |
| RegExpMacroAssembler* macro_assembler_; |
| bool ignore_case_; |
| bool ascii_; |
| bool match_only_; |
| bool reg_exp_too_big_; |
| int current_expansion_factor_; |
| FrequencyCollator frequency_collator_; |
| JSContext* cx_; |
| LifoAlloc* alloc_; |
| }; |
| |
| class RecursionCheck |
| { |
| public: |
| explicit RecursionCheck(RegExpCompiler* compiler) : compiler_(compiler) { |
| compiler->IncrementRecursionDepth(); |
| } |
| ~RecursionCheck() { compiler_->DecrementRecursionDepth(); } |
| |
| private: |
| RegExpCompiler* compiler_; |
| }; |
| |
| // Attempts to compile the regexp using an Irregexp code generator. Returns |
| // a fixed array or a null handle depending on whether it succeeded. |
| RegExpCompiler::RegExpCompiler(JSContext* cx, LifoAlloc* alloc, int capture_count, |
| bool ignore_case, bool ascii, bool match_only) |
| : next_register_(2 * (capture_count + 1)), |
| recursion_depth_(0), |
| ignore_case_(ignore_case), |
| ascii_(ascii), |
| match_only_(match_only), |
| reg_exp_too_big_(false), |
| current_expansion_factor_(1), |
| frequency_collator_(), |
| cx_(cx), |
| alloc_(alloc) |
| { |
| accept_ = alloc->newInfallible<EndNode>(alloc, EndNode::ACCEPT); |
| MOZ_ASSERT(next_register_ - 1 <= RegExpMacroAssembler::kMaxRegister); |
| } |
| |
| RegExpCode |
| RegExpCompiler::Assemble(JSContext* cx, |
| RegExpMacroAssembler* assembler, |
| RegExpNode* start, |
| int capture_count) |
| { |
| macro_assembler_ = assembler; |
| macro_assembler_->set_slow_safe(false); |
| |
| jit::Label fail; |
| macro_assembler_->PushBacktrack(&fail); |
| Trace new_trace; |
| start->Emit(this, &new_trace); |
| macro_assembler_->BindBacktrack(&fail); |
| macro_assembler_->Fail(); |
| |
| while (!work_list_.empty()) |
| work_list_.popCopy()->Emit(this, &new_trace); |
| |
| RegExpCode code = macro_assembler_->GenerateCode(cx, match_only_); |
| if (code.empty()) |
| return RegExpCode(); |
| |
| if (reg_exp_too_big_) { |
| code.destroy(); |
| JS_ReportError(cx, "regexp too big"); |
| return RegExpCode(); |
| } |
| |
| return code; |
| } |
| |
| template <typename CharT> |
| static void |
| SampleChars(FrequencyCollator* collator, const CharT* chars, size_t length) |
| { |
| // Sample some characters from the middle of the string. |
| static const int kSampleSize = 128; |
| |
| int chars_sampled = 0; |
| int half_way = (int(length) - kSampleSize) / 2; |
| for (size_t i = Max(0, half_way); |
| i < length && chars_sampled < kSampleSize; |
| i++, chars_sampled++) |
| { |
| collator->CountCharacter(chars[i]); |
| } |
| } |
| |
| static bool |
| IsNativeRegExpEnabled(JSContext* cx) |
| { |
| #if defined(JS_CODEGEN_NONE) || defined(COBALT_DISABLE_JIT) |
| return false; |
| #else |
| return cx->runtime()->options().nativeRegExp(); |
| #endif |
| } |
| |
| RegExpCode |
| irregexp::CompilePattern(JSContext* cx, RegExpShared* shared, RegExpCompileData* data, |
| HandleLinearString sample, bool is_global, bool ignore_case, |
| bool is_ascii, bool match_only, bool force_bytecode, bool sticky) |
| { |
| if ((data->capture_count + 1) * 2 - 1 > RegExpMacroAssembler::kMaxRegister) { |
| JS_ReportError(cx, "regexp too big"); |
| return RegExpCode(); |
| } |
| |
| LifoAlloc& alloc = cx->tempLifoAlloc(); |
| RegExpCompiler compiler(cx, &alloc, data->capture_count, ignore_case, is_ascii, match_only); |
| |
| // Sample some characters from the middle of the string. |
| if (sample->hasLatin1Chars()) { |
| JS::AutoCheckCannotGC nogc; |
| SampleChars(compiler.frequency_collator(), sample->latin1Chars(nogc), sample->length()); |
| } else { |
| JS::AutoCheckCannotGC nogc; |
| SampleChars(compiler.frequency_collator(), sample->twoByteChars(nogc), sample->length()); |
| } |
| |
| // Wrap the body of the regexp in capture #0. |
| RegExpNode* captured_body = RegExpCapture::ToNode(data->tree, |
| 0, |
| &compiler, |
| compiler.accept()); |
| RegExpNode* node = captured_body; |
| bool is_end_anchored = data->tree->IsAnchoredAtEnd(); |
| bool is_start_anchored = sticky || data->tree->IsAnchoredAtStart(); |
| int max_length = data->tree->max_match(); |
| if (!is_start_anchored) { |
| // Add a .*? at the beginning, outside the body capture, unless |
| // this expression is anchored at the beginning. |
| RegExpNode* loop_node = |
| RegExpQuantifier::ToNode(0, |
| RegExpTree::kInfinity, |
| false, |
| alloc.newInfallible<RegExpCharacterClass>('*'), |
| &compiler, |
| captured_body, |
| data->contains_anchor); |
| |
| if (data->contains_anchor) { |
| // Unroll loop once, to take care of the case that might start |
| // at the start of input. |
| ChoiceNode* first_step_node = alloc.newInfallible<ChoiceNode>(&alloc, 2); |
| RegExpNode* char_class = |
| alloc.newInfallible<TextNode>(alloc.newInfallible<RegExpCharacterClass>('*'), loop_node); |
| first_step_node->AddAlternative(GuardedAlternative(captured_body)); |
| first_step_node->AddAlternative(GuardedAlternative(char_class)); |
| node = first_step_node; |
| } else { |
| node = loop_node; |
| } |
| } |
| if (is_ascii) { |
| node = node->FilterASCII(RegExpCompiler::kMaxRecursion, ignore_case); |
| // Do it again to propagate the new nodes to places where they were not |
| // put because they had not been calculated yet. |
| if (node != nullptr) { |
| node = node->FilterASCII(RegExpCompiler::kMaxRecursion, ignore_case); |
| } |
| } |
| |
| if (node == nullptr) |
| node = alloc.newInfallible<EndNode>(&alloc, EndNode::BACKTRACK); |
| |
| Analysis analysis(cx, ignore_case, is_ascii); |
| analysis.EnsureAnalyzed(node); |
| if (analysis.has_failed()) { |
| JS_ReportError(cx, analysis.errorMessage()); |
| return RegExpCode(); |
| } |
| |
| Maybe<jit::JitContext> ctx; |
| Maybe<NativeRegExpMacroAssembler> native_assembler; |
| Maybe<InterpretedRegExpMacroAssembler> interpreted_assembler; |
| |
| RegExpMacroAssembler* assembler; |
| if (IsNativeRegExpEnabled(cx) && !force_bytecode) { |
| NativeRegExpMacroAssembler::Mode mode = |
| is_ascii ? NativeRegExpMacroAssembler::ASCII |
| : NativeRegExpMacroAssembler::CHAR16; |
| |
| ctx.emplace(cx, (jit::TempAllocator*) nullptr); |
| native_assembler.emplace(&alloc, shared, cx->runtime(), mode, (data->capture_count + 1) * 2); |
| assembler = native_assembler.ptr(); |
| } else { |
| interpreted_assembler.emplace(&alloc, shared, (data->capture_count + 1) * 2); |
| assembler = interpreted_assembler.ptr(); |
| } |
| |
| // Inserted here, instead of in Assembler, because it depends on information |
| // in the AST that isn't replicated in the Node structure. |
| static const int kMaxBacksearchLimit = 1024; |
| if (is_end_anchored && |
| !is_start_anchored && |
| max_length < kMaxBacksearchLimit) { |
| assembler->SetCurrentPositionFromEnd(max_length); |
| } |
| |
| if (is_global) { |
| assembler->set_global_mode((data->tree->min_match() > 0) |
| ? RegExpMacroAssembler::GLOBAL_NO_ZERO_LENGTH_CHECK |
| : RegExpMacroAssembler::GLOBAL); |
| } |
| |
| return compiler.Assemble(cx, assembler, node, data->capture_count); |
| } |
| |
| template <typename CharT> |
| RegExpRunStatus |
| irregexp::ExecuteCode(JSContext* cx, jit::JitCode* codeBlock, const CharT* chars, size_t start, |
| size_t length, MatchPairs* matches) |
| { |
| typedef void (*RegExpCodeSignature)(InputOutputData*); |
| |
| InputOutputData data(chars, chars + length, start, matches); |
| |
| RegExpCodeSignature function = reinterpret_cast<RegExpCodeSignature>(codeBlock->raw()); |
| |
| { |
| JS::AutoSuppressGCAnalysis nogc; |
| CALL_GENERATED_1(function, &data); |
| } |
| |
| return (RegExpRunStatus) data.result; |
| } |
| |
| template RegExpRunStatus |
| irregexp::ExecuteCode(JSContext* cx, jit::JitCode* codeBlock, const Latin1Char* chars, size_t start, |
| size_t length, MatchPairs* matches); |
| |
| template RegExpRunStatus |
| irregexp::ExecuteCode(JSContext* cx, jit::JitCode* codeBlock, const char16_t* chars, size_t start, |
| size_t length, MatchPairs* matches); |
| |
| // ------------------------------------------------------------------- |
| // Tree to graph conversion |
| |
| RegExpNode* |
| RegExpAtom::ToNode(RegExpCompiler* compiler, RegExpNode* on_success) |
| { |
| TextElementVector* elms = |
| compiler->alloc()->newInfallible<TextElementVector>(*compiler->alloc()); |
| elms->append(TextElement::Atom(this)); |
| return compiler->alloc()->newInfallible<TextNode>(elms, on_success); |
| } |
| |
| RegExpNode* |
| RegExpText::ToNode(RegExpCompiler* compiler, RegExpNode* on_success) |
| { |
| return compiler->alloc()->newInfallible<TextNode>(&elements_, on_success); |
| } |
| |
| RegExpNode* |
| RegExpCharacterClass::ToNode(RegExpCompiler* compiler, RegExpNode* on_success) |
| { |
| return compiler->alloc()->newInfallible<TextNode>(this, on_success); |
| } |
| |
| RegExpNode* |
| RegExpDisjunction::ToNode(RegExpCompiler* compiler, RegExpNode* on_success) |
| { |
| const RegExpTreeVector& alternatives = this->alternatives(); |
| size_t length = alternatives.length(); |
| ChoiceNode* result = compiler->alloc()->newInfallible<ChoiceNode>(compiler->alloc(), length); |
| for (size_t i = 0; i < length; i++) { |
| GuardedAlternative alternative(alternatives[i]->ToNode(compiler, on_success)); |
| result->AddAlternative(alternative); |
| } |
| return result; |
| } |
| |
| RegExpNode* |
| RegExpQuantifier::ToNode(RegExpCompiler* compiler, RegExpNode* on_success) |
| { |
| return ToNode(min(), |
| max(), |
| is_greedy(), |
| body(), |
| compiler, |
| on_success); |
| } |
| |
| // Scoped object to keep track of how much we unroll quantifier loops in the |
| // regexp graph generator. |
| class RegExpExpansionLimiter |
| { |
| public: |
| static const int kMaxExpansionFactor = 6; |
| RegExpExpansionLimiter(RegExpCompiler* compiler, int factor) |
| : compiler_(compiler), |
| saved_expansion_factor_(compiler->current_expansion_factor()), |
| ok_to_expand_(saved_expansion_factor_ <= kMaxExpansionFactor) |
| { |
| MOZ_ASSERT(factor > 0); |
| if (ok_to_expand_) { |
| if (factor > kMaxExpansionFactor) { |
| // Avoid integer overflow of the current expansion factor. |
| ok_to_expand_ = false; |
| compiler->set_current_expansion_factor(kMaxExpansionFactor + 1); |
| } else { |
| int new_factor = saved_expansion_factor_ * factor; |
| ok_to_expand_ = (new_factor <= kMaxExpansionFactor); |
| compiler->set_current_expansion_factor(new_factor); |
| } |
| } |
| } |
| |
| ~RegExpExpansionLimiter() { |
| compiler_->set_current_expansion_factor(saved_expansion_factor_); |
| } |
| |
| bool ok_to_expand() { return ok_to_expand_; } |
| |
| private: |
| RegExpCompiler* compiler_; |
| int saved_expansion_factor_; |
| bool ok_to_expand_; |
| }; |
| |
| /* static */ RegExpNode* |
| RegExpQuantifier::ToNode(int min, |
| int max, |
| bool is_greedy, |
| RegExpTree* body, |
| RegExpCompiler* compiler, |
| RegExpNode* on_success, |
| bool not_at_start /* = false */) |
| { |
| // x{f, t} becomes this: |
| // |
| // (r++)<-. |
| // | ` |
| // | (x) |
| // v ^ |
| // (r=0)-->(?)---/ [if r < t] |
| // | |
| // [if r >= f] \----> ... |
| // |
| |
| // 15.10.2.5 RepeatMatcher algorithm. |
| // The parser has already eliminated the case where max is 0. In the case |
| // where max_match is zero the parser has removed the quantifier if min was |
| // > 0 and removed the atom if min was 0. See AddQuantifierToAtom. |
| |
| // If we know that we cannot match zero length then things are a little |
| // simpler since we don't need to make the special zero length match check |
| // from step 2.1. If the min and max are small we can unroll a little in |
| // this case. |
| static const int kMaxUnrolledMinMatches = 3; // Unroll (foo)+ and (foo){3,} |
| static const int kMaxUnrolledMaxMatches = 3; // Unroll (foo)? and (foo){x,3} |
| |
| if (max == 0) |
| return on_success; // This can happen due to recursion. |
| |
| bool body_can_be_empty = (body->min_match() == 0); |
| int body_start_reg = RegExpCompiler::kNoRegister; |
| Interval capture_registers = body->CaptureRegisters(); |
| bool needs_capture_clearing = !capture_registers.is_empty(); |
| LifoAlloc* alloc = compiler->alloc(); |
| |
| if (body_can_be_empty) { |
| body_start_reg = compiler->AllocateRegister(); |
| } else if (!needs_capture_clearing) { |
| // Only unroll if there are no captures and the body can't be |
| // empty. |
| { |
| RegExpExpansionLimiter limiter(compiler, min + ((max != min) ? 1 : 0)); |
| if (min > 0 && min <= kMaxUnrolledMinMatches && limiter.ok_to_expand()) { |
| int new_max = (max == kInfinity) ? max : max - min; |
| // Recurse once to get the loop or optional matches after the fixed |
| // ones. |
| RegExpNode* answer = ToNode(0, new_max, is_greedy, body, compiler, on_success, true); |
| // Unroll the forced matches from 0 to min. This can cause chains of |
| // TextNodes (which the parser does not generate). These should be |
| // combined if it turns out they hinder good code generation. |
| for (int i = 0; i < min; i++) |
| answer = body->ToNode(compiler, answer); |
| return answer; |
| } |
| } |
| if (max <= kMaxUnrolledMaxMatches && min == 0) { |
| MOZ_ASSERT(max > 0); // Due to the 'if' above. |
| RegExpExpansionLimiter limiter(compiler, max); |
| if (limiter.ok_to_expand()) { |
| // Unroll the optional matches up to max. |
| RegExpNode* answer = on_success; |
| for (int i = 0; i < max; i++) { |
| ChoiceNode* alternation = alloc->newInfallible<ChoiceNode>(alloc, 2); |
| if (is_greedy) { |
| alternation->AddAlternative(GuardedAlternative(body->ToNode(compiler, answer))); |
| alternation->AddAlternative(GuardedAlternative(on_success)); |
| } else { |
| alternation->AddAlternative(GuardedAlternative(on_success)); |
| alternation->AddAlternative(GuardedAlternative(body->ToNode(compiler, answer))); |
| } |
| answer = alternation; |
| if (not_at_start) alternation->set_not_at_start(); |
| } |
| return answer; |
| } |
| } |
| } |
| bool has_min = min > 0; |
| bool has_max = max < RegExpTree::kInfinity; |
| bool needs_counter = has_min || has_max; |
| int reg_ctr = needs_counter |
| ? compiler->AllocateRegister() |
| : RegExpCompiler::kNoRegister; |
| LoopChoiceNode* center = alloc->newInfallible<LoopChoiceNode>(alloc, body->min_match() == 0); |
| if (not_at_start) |
| center->set_not_at_start(); |
| RegExpNode* loop_return = needs_counter |
| ? static_cast<RegExpNode*>(ActionNode::IncrementRegister(reg_ctr, center)) |
| : static_cast<RegExpNode*>(center); |
| if (body_can_be_empty) { |
| // If the body can be empty we need to check if it was and then |
| // backtrack. |
| loop_return = ActionNode::EmptyMatchCheck(body_start_reg, |
| reg_ctr, |
| min, |
| loop_return); |
| } |
| RegExpNode* body_node = body->ToNode(compiler, loop_return); |
| if (body_can_be_empty) { |
| // If the body can be empty we need to store the start position |
| // so we can bail out if it was empty. |
| body_node = ActionNode::StorePosition(body_start_reg, false, body_node); |
| } |
| if (needs_capture_clearing) { |
| // Before entering the body of this loop we need to clear captures. |
| body_node = ActionNode::ClearCaptures(capture_registers, body_node); |
| } |
| GuardedAlternative body_alt(body_node); |
| if (has_max) { |
| Guard* body_guard = alloc->newInfallible<Guard>(reg_ctr, Guard::LT, max); |
| body_alt.AddGuard(alloc, body_guard); |
| } |
| GuardedAlternative rest_alt(on_success); |
| if (has_min) { |
| Guard* rest_guard = alloc->newInfallible<Guard>(reg_ctr, Guard::GEQ, min); |
| rest_alt.AddGuard(alloc, rest_guard); |
| } |
| if (is_greedy) { |
| center->AddLoopAlternative(body_alt); |
| center->AddContinueAlternative(rest_alt); |
| } else { |
| center->AddContinueAlternative(rest_alt); |
| center->AddLoopAlternative(body_alt); |
| } |
| if (needs_counter) |
| return ActionNode::SetRegister(reg_ctr, 0, center); |
| return center; |
| } |
| |
| RegExpNode* |
| RegExpAssertion::ToNode(RegExpCompiler* compiler, |
| RegExpNode* on_success) |
| { |
| NodeInfo info; |
| LifoAlloc* alloc = compiler->alloc(); |
| |
| switch (assertion_type()) { |
| case START_OF_LINE: |
| return AssertionNode::AfterNewline(on_success); |
| case START_OF_INPUT: |
| return AssertionNode::AtStart(on_success); |
| case BOUNDARY: |
| return AssertionNode::AtBoundary(on_success); |
| case NON_BOUNDARY: |
| return AssertionNode::AtNonBoundary(on_success); |
| case END_OF_INPUT: |
| return AssertionNode::AtEnd(on_success); |
| case END_OF_LINE: { |
| // Compile $ in multiline regexps as an alternation with a positive |
| // lookahead in one side and an end-of-input on the other side. |
| // We need two registers for the lookahead. |
| int stack_pointer_register = compiler->AllocateRegister(); |
| int position_register = compiler->AllocateRegister(); |
| // The ChoiceNode to distinguish between a newline and end-of-input. |
| ChoiceNode* result = alloc->newInfallible<ChoiceNode>(alloc, 2); |
| // Create a newline atom. |
| CharacterRangeVector* newline_ranges = alloc->newInfallible<CharacterRangeVector>(*alloc); |
| CharacterRange::AddClassEscape(alloc, 'n', newline_ranges); |
| RegExpCharacterClass* newline_atom = alloc->newInfallible<RegExpCharacterClass>('n'); |
| TextNode* newline_matcher = |
| alloc->newInfallible<TextNode>(newline_atom, |
| ActionNode::PositiveSubmatchSuccess(stack_pointer_register, |
| position_register, |
| 0, // No captures inside. |
| -1, // Ignored if no captures. |
| on_success)); |
| // Create an end-of-input matcher. |
| RegExpNode* end_of_line = |
| ActionNode::BeginSubmatch(stack_pointer_register, position_register, newline_matcher); |
| |
| // Add the two alternatives to the ChoiceNode. |
| GuardedAlternative eol_alternative(end_of_line); |
| result->AddAlternative(eol_alternative); |
| GuardedAlternative end_alternative(AssertionNode::AtEnd(on_success)); |
| result->AddAlternative(end_alternative); |
| return result; |
| } |
| default: |
| MOZ_CRASH("Bad assertion type"); |
| } |
| return on_success; |
| } |
| |
| RegExpNode* |
| RegExpBackReference::ToNode(RegExpCompiler* compiler, RegExpNode* on_success) |
| { |
| return compiler->alloc()->newInfallible<BackReferenceNode>(RegExpCapture::StartRegister(index()), |
| RegExpCapture::EndRegister(index()), |
| on_success); |
| } |
| |
| RegExpNode* |
| RegExpEmpty::ToNode(RegExpCompiler* compiler, RegExpNode* on_success) |
| { |
| return on_success; |
| } |
| |
| RegExpNode* |
| RegExpLookahead::ToNode(RegExpCompiler* compiler, RegExpNode* on_success) |
| { |
| int stack_pointer_register = compiler->AllocateRegister(); |
| int position_register = compiler->AllocateRegister(); |
| |
| const int registers_per_capture = 2; |
| const int register_of_first_capture = 2; |
| int register_count = capture_count_ * registers_per_capture; |
| int register_start = |
| register_of_first_capture + capture_from_ * registers_per_capture; |
| |
| if (is_positive()) { |
| RegExpNode* bodyNode = |
| body()->ToNode(compiler, |
| ActionNode::PositiveSubmatchSuccess(stack_pointer_register, |
| position_register, |
| register_count, |
| register_start, |
| on_success)); |
| return ActionNode::BeginSubmatch(stack_pointer_register, |
| position_register, |
| bodyNode); |
| } |
| |
| // We use a ChoiceNode for a negative lookahead because it has most of |
| // the characteristics we need. It has the body of the lookahead as its |
| // first alternative and the expression after the lookahead of the second |
| // alternative. If the first alternative succeeds then the |
| // NegativeSubmatchSuccess will unwind the stack including everything the |
| // choice node set up and backtrack. If the first alternative fails then |
| // the second alternative is tried, which is exactly the desired result |
| // for a negative lookahead. The NegativeLookaheadChoiceNode is a special |
| // ChoiceNode that knows to ignore the first exit when calculating quick |
| // checks. |
| LifoAlloc* alloc = compiler->alloc(); |
| |
| RegExpNode* success = |
| alloc->newInfallible<NegativeSubmatchSuccess>(alloc, |
| stack_pointer_register, |
| position_register, |
| register_count, |
| register_start); |
| GuardedAlternative body_alt(body()->ToNode(compiler, success)); |
| |
| ChoiceNode* choice_node = |
| alloc->newInfallible<NegativeLookaheadChoiceNode>(alloc, body_alt, GuardedAlternative(on_success)); |
| |
| return ActionNode::BeginSubmatch(stack_pointer_register, |
| position_register, |
| choice_node); |
| } |
| |
| RegExpNode* |
| RegExpCapture::ToNode(RegExpCompiler* compiler, RegExpNode* on_success) |
| { |
| return ToNode(body(), index(), compiler, on_success); |
| } |
| |
| /* static */ RegExpNode* |
| RegExpCapture::ToNode(RegExpTree* body, |
| int index, |
| RegExpCompiler* compiler, |
| RegExpNode* on_success) |
| { |
| int start_reg = RegExpCapture::StartRegister(index); |
| int end_reg = RegExpCapture::EndRegister(index); |
| RegExpNode* store_end = ActionNode::StorePosition(end_reg, true, on_success); |
| RegExpNode* body_node = body->ToNode(compiler, store_end); |
| return ActionNode::StorePosition(start_reg, true, body_node); |
| } |
| |
| RegExpNode* |
| RegExpAlternative::ToNode(RegExpCompiler* compiler, RegExpNode* on_success) |
| { |
| const RegExpTreeVector& children = nodes(); |
| RegExpNode* current = on_success; |
| for (int i = children.length() - 1; i >= 0; i--) |
| current = children[i]->ToNode(compiler, current); |
| return current; |
| } |
| |
| // ------------------------------------------------------------------- |
| // BoyerMooreLookahead |
| |
| ContainedInLattice |
| irregexp::AddRange(ContainedInLattice containment, |
| const int* ranges, |
| int ranges_length, |
| Interval new_range) |
| { |
| MOZ_ASSERT((ranges_length & 1) == 1); |
| MOZ_ASSERT(ranges[ranges_length - 1] == kMaxUtf16CodeUnit + 1); |
| if (containment == kLatticeUnknown) return containment; |
| bool inside = false; |
| int last = 0; |
| for (int i = 0; i < ranges_length; inside = !inside, last = ranges[i], i++) { |
| // Consider the range from last to ranges[i]. |
| // We haven't got to the new range yet. |
| if (ranges[i] <= new_range.from()) |
| continue; |
| |
| // New range is wholly inside last-ranges[i]. Note that new_range.to() is |
| // inclusive, but the values in ranges are not. |
| if (last <= new_range.from() && new_range.to() < ranges[i]) |
| return Combine(containment, inside ? kLatticeIn : kLatticeOut); |
| |
| return kLatticeUnknown; |
| } |
| return containment; |
| } |
| |
| void |
| BoyerMoorePositionInfo::Set(int character) |
| { |
| SetInterval(Interval(character, character)); |
| } |
| |
| void |
| BoyerMoorePositionInfo::SetInterval(const Interval& interval) |
| { |
| s_ = AddRange(s_, kSpaceRanges, kSpaceRangeCount, interval); |
| w_ = AddRange(w_, kWordRanges, kWordRangeCount, interval); |
| d_ = AddRange(d_, kDigitRanges, kDigitRangeCount, interval); |
| surrogate_ = |
| AddRange(surrogate_, kSurrogateRanges, kSurrogateRangeCount, interval); |
| if (interval.to() - interval.from() >= kMapSize - 1) { |
| if (map_count_ != kMapSize) { |
| map_count_ = kMapSize; |
| for (int i = 0; i < kMapSize; i++) |
| map_[i] = true; |
| } |
| return; |
| } |
| for (int i = interval.from(); i <= interval.to(); i++) { |
| int mod_character = (i & kMask); |
| if (!map_[mod_character]) { |
| map_count_++; |
| map_[mod_character] = true; |
| } |
| if (map_count_ == kMapSize) |
| return; |
| } |
| } |
| |
| void |
| BoyerMoorePositionInfo::SetAll() |
| { |
| s_ = w_ = d_ = kLatticeUnknown; |
| if (map_count_ != kMapSize) { |
| map_count_ = kMapSize; |
| for (int i = 0; i < kMapSize; i++) |
| map_[i] = true; |
| } |
| } |
| |
| BoyerMooreLookahead::BoyerMooreLookahead(LifoAlloc* alloc, size_t length, RegExpCompiler* compiler) |
| : length_(length), compiler_(compiler), bitmaps_(*alloc) |
| { |
| max_char_ = MaximumCharacter(compiler->ascii()); |
| |
| bitmaps_.reserve(length); |
| for (size_t i = 0; i < length; i++) |
| bitmaps_.append(alloc->newInfallible<BoyerMoorePositionInfo>(alloc)); |
| } |
| |
| // Find the longest range of lookahead that has the fewest number of different |
| // characters that can occur at a given position. Since we are optimizing two |
| // different parameters at once this is a tradeoff. |
| bool BoyerMooreLookahead::FindWorthwhileInterval(int* from, int* to) { |
| int biggest_points = 0; |
| // If more than 32 characters out of 128 can occur it is unlikely that we can |
| // be lucky enough to step forwards much of the time. |
| const int kMaxMax = 32; |
| for (int max_number_of_chars = 4; |
| max_number_of_chars < kMaxMax; |
| max_number_of_chars *= 2) { |
| biggest_points = |
| FindBestInterval(max_number_of_chars, biggest_points, from, to); |
| } |
| if (biggest_points == 0) return false; |
| return true; |
| } |
| |
| // Find the highest-points range between 0 and length_ where the character |
| // information is not too vague. 'Too vague' means that there are more than |
| // max_number_of_chars that can occur at this position. Calculates the number |
| // of points as the product of width-of-the-range and |
| // probability-of-finding-one-of-the-characters, where the probability is |
| // calculated using the frequency distribution of the sample subject string. |
| int |
| BoyerMooreLookahead::FindBestInterval(int max_number_of_chars, int old_biggest_points, |
| int* from, int* to) |
| { |
| int biggest_points = old_biggest_points; |
| static const int kSize = RegExpMacroAssembler::kTableSize; |
| for (int i = 0; i < length_; ) { |
| while (i < length_ && Count(i) > max_number_of_chars) i++; |
| if (i == length_) break; |
| int remembered_from = i; |
| bool union_map[kSize]; |
| for (int j = 0; j < kSize; j++) union_map[j] = false; |
| while (i < length_ && Count(i) <= max_number_of_chars) { |
| BoyerMoorePositionInfo* map = bitmaps_[i]; |
| for (int j = 0; j < kSize; j++) union_map[j] |= map->at(j); |
| i++; |
| } |
| int frequency = 0; |
| for (int j = 0; j < kSize; j++) { |
| if (union_map[j]) { |
| // Add 1 to the frequency to give a small per-character boost for |
| // the cases where our sampling is not good enough and many |
| // characters have a frequency of zero. This means the frequency |
| // can theoretically be up to 2*kSize though we treat it mostly as |
| // a fraction of kSize. |
| frequency += compiler_->frequency_collator()->Frequency(j) + 1; |
| } |
| } |
| // We use the probability of skipping times the distance we are skipping to |
| // judge the effectiveness of this. Actually we have a cut-off: By |
| // dividing by 2 we switch off the skipping if the probability of skipping |
| // is less than 50%. This is because the multibyte mask-and-compare |
| // skipping in quickcheck is more likely to do well on this case. |
| bool in_quickcheck_range = ((i - remembered_from < 4) || |
| (compiler_->ascii() ? remembered_from <= 4 : remembered_from <= 2)); |
| // Called 'probability' but it is only a rough estimate and can actually |
| // be outside the 0-kSize range. |
| int probability = (in_quickcheck_range ? kSize / 2 : kSize) - frequency; |
| int points = (i - remembered_from) * probability; |
| if (points > biggest_points) { |
| *from = remembered_from; |
| *to = i - 1; |
| biggest_points = points; |
| } |
| } |
| return biggest_points; |
| } |
| |
| // Take all the characters that will not prevent a successful match if they |
| // occur in the subject string in the range between min_lookahead and |
| // max_lookahead (inclusive) measured from the current position. If the |
| // character at max_lookahead offset is not one of these characters, then we |
| // can safely skip forwards by the number of characters in the range. |
| int BoyerMooreLookahead::GetSkipTable(int min_lookahead, |
| int max_lookahead, |
| uint8_t* boolean_skip_table) |
| { |
| const int kSize = RegExpMacroAssembler::kTableSize; |
| |
| const int kSkipArrayEntry = 0; |
| const int kDontSkipArrayEntry = 1; |
| |
| for (int i = 0; i < kSize; i++) |
| boolean_skip_table[i] = kSkipArrayEntry; |
| int skip = max_lookahead + 1 - min_lookahead; |
| |
| for (int i = max_lookahead; i >= min_lookahead; i--) { |
| BoyerMoorePositionInfo* map = bitmaps_[i]; |
| for (int j = 0; j < kSize; j++) { |
| if (map->at(j)) |
| boolean_skip_table[j] = kDontSkipArrayEntry; |
| } |
| } |
| |
| return skip; |
| } |
| |
| // See comment on the implementation of GetSkipTable. |
| bool |
| BoyerMooreLookahead::EmitSkipInstructions(RegExpMacroAssembler* masm) |
| { |
| const int kSize = RegExpMacroAssembler::kTableSize; |
| |
| int min_lookahead = 0; |
| int max_lookahead = 0; |
| |
| if (!FindWorthwhileInterval(&min_lookahead, &max_lookahead)) |
| return false; |
| |
| bool found_single_character = false; |
| int single_character = 0; |
| for (int i = max_lookahead; i >= min_lookahead; i--) { |
| BoyerMoorePositionInfo* map = bitmaps_[i]; |
| if (map->map_count() > 1 || |
| (found_single_character && map->map_count() != 0)) { |
| found_single_character = false; |
| break; |
| } |
| for (int j = 0; j < kSize; j++) { |
| if (map->at(j)) { |
| found_single_character = true; |
| single_character = j; |
| break; |
| } |
| } |
| } |
| |
| int lookahead_width = max_lookahead + 1 - min_lookahead; |
| |
| if (found_single_character && lookahead_width == 1 && max_lookahead < 3) { |
| // The mask-compare can probably handle this better. |
| return false; |
| } |
| |
| if (found_single_character) { |
| jit::Label cont, again; |
| masm->Bind(&again); |
| masm->LoadCurrentCharacter(max_lookahead, &cont, true); |
| if (max_char_ > kSize) { |
| masm->CheckCharacterAfterAnd(single_character, |
| RegExpMacroAssembler::kTableMask, |
| &cont); |
| } else { |
| masm->CheckCharacter(single_character, &cont); |
| } |
| masm->AdvanceCurrentPosition(lookahead_width); |
| masm->JumpOrBacktrack(&again); |
| masm->Bind(&cont); |
| return true; |
| } |
| |
| uint8_t* boolean_skip_table; |
| { |
| AutoEnterOOMUnsafeRegion oomUnsafe; |
| boolean_skip_table = static_cast<uint8_t*>(js_malloc(kSize)); |
| if (!boolean_skip_table || !masm->shared->addTable(boolean_skip_table)) |
| oomUnsafe.crash("Table malloc"); |
| } |
| |
| int skip_distance = GetSkipTable(min_lookahead, max_lookahead, boolean_skip_table); |
| MOZ_ASSERT(skip_distance != 0); |
| |
| jit::Label cont, again; |
| masm->Bind(&again); |
| masm->LoadCurrentCharacter(max_lookahead, &cont, true); |
| masm->CheckBitInTable(boolean_skip_table, &cont); |
| masm->AdvanceCurrentPosition(skip_distance); |
| masm->JumpOrBacktrack(&again); |
| masm->Bind(&cont); |
| |
| return true; |
| } |
| |
| bool |
| BoyerMooreLookahead::CheckOverRecursed() |
| { |
| JS_CHECK_RECURSION(compiler()->cx(), compiler()->SetRegExpTooBig(); return false); |
| return true; |
| } |
| |
| // ------------------------------------------------------------------- |
| // Trace |
| |
| bool Trace::DeferredAction::Mentions(int that) |
| { |
| if (action_type() == ActionNode::CLEAR_CAPTURES) { |
| Interval range = static_cast<DeferredClearCaptures*>(this)->range(); |
| return range.Contains(that); |
| } |
| return reg() == that; |
| } |
| |
| bool Trace::mentions_reg(int reg) |
| { |
| for (DeferredAction* action = actions_; action != nullptr; action = action->next()) { |
| if (action->Mentions(reg)) |
| return true; |
| } |
| return false; |
| } |
| |
| bool |
| Trace::GetStoredPosition(int reg, int* cp_offset) |
| { |
| MOZ_ASSERT(0 == *cp_offset); |
| for (DeferredAction* action = actions_; action != nullptr; action = action->next()) { |
| if (action->Mentions(reg)) { |
| if (action->action_type() == ActionNode::STORE_POSITION) { |
| *cp_offset = static_cast<DeferredCapture*>(action)->cp_offset(); |
| return true; |
| } |
| return false; |
| } |
| } |
| return false; |
| } |
| |
| int |
| Trace::FindAffectedRegisters(LifoAlloc* alloc, OutSet* affected_registers) |
| { |
| int max_register = RegExpCompiler::kNoRegister; |
| for (DeferredAction* action = actions_; action != nullptr; action = action->next()) { |
| if (action->action_type() == ActionNode::CLEAR_CAPTURES) { |
| Interval range = static_cast<DeferredClearCaptures*>(action)->range(); |
| for (int i = range.from(); i <= range.to(); i++) |
| affected_registers->Set(alloc, i); |
| if (range.to() > max_register) max_register = range.to(); |
| } else { |
| affected_registers->Set(alloc, action->reg()); |
| if (action->reg() > max_register) max_register = action->reg(); |
| } |
| } |
| return max_register; |
| } |
| |
| void |
| Trace::RestoreAffectedRegisters(RegExpMacroAssembler* assembler, |
| int max_register, |
| OutSet& registers_to_pop, |
| OutSet& registers_to_clear) |
| { |
| for (int reg = max_register; reg >= 0; reg--) { |
| if (registers_to_pop.Get(reg)) assembler->PopRegister(reg); |
| else if (registers_to_clear.Get(reg)) { |
| int clear_to = reg; |
| while (reg > 0 && registers_to_clear.Get(reg - 1)) |
| reg--; |
| assembler->ClearRegisters(reg, clear_to); |
| } |
| } |
| } |
| |
| enum DeferredActionUndoType { |
| DEFER_IGNORE, |
| DEFER_RESTORE, |
| DEFER_CLEAR |
| }; |
| |
| void |
| Trace::PerformDeferredActions(LifoAlloc* alloc, |
| RegExpMacroAssembler* assembler, |
| int max_register, |
| OutSet& affected_registers, |
| OutSet* registers_to_pop, |
| OutSet* registers_to_clear) |
| { |
| // The "+1" is to avoid a push_limit of zero if stack_limit_slack() is 1. |
| const int push_limit = (assembler->stack_limit_slack() + 1) / 2; |
| |
| // Count pushes performed to force a stack limit check occasionally. |
| int pushes = 0; |
| |
| for (int reg = 0; reg <= max_register; reg++) { |
| if (!affected_registers.Get(reg)) |
| continue; |
| |
| // The chronologically first deferred action in the trace |
| // is used to infer the action needed to restore a register |
| // to its previous state (or not, if it's safe to ignore it). |
| DeferredActionUndoType undo_action = DEFER_IGNORE; |
| |
| int value = 0; |
| bool absolute = false; |
| bool clear = false; |
| int store_position = -1; |
| // This is a little tricky because we are scanning the actions in reverse |
| // historical order (newest first). |
| for (DeferredAction* action = actions_; |
| action != nullptr; |
| action = action->next()) { |
| if (action->Mentions(reg)) { |
| switch (action->action_type()) { |
| case ActionNode::SET_REGISTER: { |
| Trace::DeferredSetRegister* psr = |
| static_cast<Trace::DeferredSetRegister*>(action); |
| if (!absolute) { |
| value += psr->value(); |
| absolute = true; |
| } |
| // SET_REGISTER is currently only used for newly introduced loop |
| // counters. They can have a significant previous value if they |
| // occour in a loop. TODO(lrn): Propagate this information, so |
| // we can set undo_action to IGNORE if we know there is no value to |
| // restore. |
| undo_action = DEFER_RESTORE; |
| MOZ_ASSERT(store_position == -1); |
| MOZ_ASSERT(!clear); |
| break; |
| } |
| case ActionNode::INCREMENT_REGISTER: |
| if (!absolute) { |
| value++; |
| } |
| MOZ_ASSERT(store_position == -1); |
| MOZ_ASSERT(!clear); |
| undo_action = DEFER_RESTORE; |
| break; |
| case ActionNode::STORE_POSITION: { |
| Trace::DeferredCapture* pc = |
| static_cast<Trace::DeferredCapture*>(action); |
| if (!clear && store_position == -1) { |
| store_position = pc->cp_offset(); |
| } |
| |
| // For captures we know that stores and clears alternate. |
| // Other register, are never cleared, and if the occur |
| // inside a loop, they might be assigned more than once. |
| if (reg <= 1) { |
| // Registers zero and one, aka "capture zero", is |
| // always set correctly if we succeed. There is no |
| // need to undo a setting on backtrack, because we |
| // will set it again or fail. |
| undo_action = DEFER_IGNORE; |
| } else { |
| undo_action = pc->is_capture() ? DEFER_CLEAR : DEFER_RESTORE; |
| } |
| MOZ_ASSERT(!absolute); |
| MOZ_ASSERT(value == 0); |
| break; |
| } |
| case ActionNode::CLEAR_CAPTURES: { |
| // Since we're scanning in reverse order, if we've already |
| // set the position we have to ignore historically earlier |
| // clearing operations. |
| if (store_position == -1) { |
| clear = true; |
| } |
| undo_action = DEFER_RESTORE; |
| MOZ_ASSERT(!absolute); |
| MOZ_ASSERT(value == 0); |
| break; |
| } |
| default: |
| MOZ_CRASH("Bad action"); |
| } |
| } |
| } |
| // Prepare for the undo-action (e.g., push if it's going to be popped). |
| if (undo_action == DEFER_RESTORE) { |
| pushes++; |
| RegExpMacroAssembler::StackCheckFlag stack_check = |
| RegExpMacroAssembler::kNoStackLimitCheck; |
| if (pushes == push_limit) { |
| stack_check = RegExpMacroAssembler::kCheckStackLimit; |
| pushes = 0; |
| } |
| |
| assembler->PushRegister(reg, stack_check); |
| registers_to_pop->Set(alloc, reg); |
| } else if (undo_action == DEFER_CLEAR) { |
| registers_to_clear->Set(alloc, reg); |
| } |
| // Perform the chronologically last action (or accumulated increment) |
| // for the register. |
| if (store_position != -1) { |
| assembler->WriteCurrentPositionToRegister(reg, store_position); |
| } else if (clear) { |
| assembler->ClearRegisters(reg, reg); |
| } else if (absolute) { |
| assembler->SetRegister(reg, value); |
| } else if (value != 0) { |
| assembler->AdvanceRegister(reg, value); |
| } |
| } |
| } |
| |
| // This is called as we come into a loop choice node and some other tricky |
| // nodes. It normalizes the state of the code generator to ensure we can |
| // generate generic code. |
| void Trace::Flush(RegExpCompiler* compiler, RegExpNode* successor) |
| { |
| RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| |
| MOZ_ASSERT(!is_trivial()); |
| |
| if (actions_ == nullptr && backtrack() == nullptr) { |
| // Here we just have some deferred cp advances to fix and we are back to |
| // a normal situation. We may also have to forget some information gained |
| // through a quick check that was already performed. |
| if (cp_offset_ != 0) assembler->AdvanceCurrentPosition(cp_offset_); |
| // Create a new trivial state and generate the node with that. |
| Trace new_state; |
| successor->Emit(compiler, &new_state); |
| return; |
| } |
| |
| // Generate deferred actions here along with code to undo them again. |
| OutSet affected_registers; |
| |
| if (backtrack() != nullptr) { |
| // Here we have a concrete backtrack location. These are set up by choice |
| // nodes and so they indicate that we have a deferred save of the current |
| // position which we may need to emit here. |
| assembler->PushCurrentPosition(); |
| } |
| |
| int max_register = FindAffectedRegisters(compiler->alloc(), &affected_registers); |
| OutSet registers_to_pop; |
| OutSet registers_to_clear; |
| PerformDeferredActions(compiler->alloc(), |
| assembler, |
| max_register, |
| affected_registers, |
| ®isters_to_pop, |
| ®isters_to_clear); |
| if (cp_offset_ != 0) |
| assembler->AdvanceCurrentPosition(cp_offset_); |
| |
| // Create a new trivial state and generate the node with that. |
| jit::Label undo; |
| assembler->PushBacktrack(&undo); |
| Trace new_state; |
| successor->Emit(compiler, &new_state); |
| |
| // On backtrack we need to restore state. |
| assembler->BindBacktrack(&undo); |
| RestoreAffectedRegisters(assembler, |
| max_register, |
| registers_to_pop, |
| registers_to_clear); |
| if (backtrack() == nullptr) { |
| assembler->Backtrack(); |
| } else { |
| assembler->PopCurrentPosition(); |
| assembler->JumpOrBacktrack(backtrack()); |
| } |
| } |
| |
| void |
| Trace::InvalidateCurrentCharacter() |
| { |
| characters_preloaded_ = 0; |
| } |
| |
| void |
| Trace::AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler) |
| { |
| MOZ_ASSERT(by > 0); |
| // We don't have an instruction for shifting the current character register |
| // down or for using a shifted value for anything so lets just forget that |
| // we preloaded any characters into it. |
| characters_preloaded_ = 0; |
| // Adjust the offsets of the quick check performed information. This |
| // information is used to find out what we already determined about the |
| // characters by means of mask and compare. |
| quick_check_performed_.Advance(by, compiler->ascii()); |
| cp_offset_ += by; |
| if (cp_offset_ > RegExpMacroAssembler::kMaxCPOffset) { |
| compiler->SetRegExpTooBig(); |
| cp_offset_ = 0; |
| } |
| bound_checked_up_to_ = Max(0, bound_checked_up_to_ - by); |
| } |
| |
| void |
| OutSet::Set(LifoAlloc* alloc, unsigned value) |
| { |
| if (value < kFirstLimit) { |
| first_ |= (1 << value); |
| } else { |
| if (remaining_ == nullptr) |
| remaining_ = alloc->newInfallible<RemainingVector>(*alloc); |
| |
| for (size_t i = 0; i < remaining().length(); i++) { |
| if (remaining()[i] == value) |
| return; |
| } |
| remaining().append(value); |
| } |
| } |
| |
| bool |
| OutSet::Get(unsigned value) |
| { |
| if (value < kFirstLimit) |
| return (first_ & (1 << value)) != 0; |
| if (remaining_ == nullptr) |
| return false; |
| for (size_t i = 0; i < remaining().length(); i++) { |
| if (remaining()[i] == value) |
| return true; |
| } |
| return false; |
| } |
| |
| // ------------------------------------------------------------------- |
| // Graph emitting |
| |
| void |
| NegativeSubmatchSuccess::Emit(RegExpCompiler* compiler, Trace* trace) |
| { |
| RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| |
| // Omit flushing the trace. We discard the entire stack frame anyway. |
| |
| if (!label()->bound()) { |
| // We are completely independent of the trace, since we ignore it, |
| // so this code can be used as the generic version. |
| assembler->Bind(label()); |
| } |
| |
| // Throw away everything on the backtrack stack since the start |
| // of the negative submatch and restore the character position. |
| assembler->ReadCurrentPositionFromRegister(current_position_register_); |
| assembler->ReadBacktrackStackPointerFromRegister(stack_pointer_register_); |
| |
| if (clear_capture_count_ > 0) { |
| // Clear any captures that might have been performed during the success |
| // of the body of the negative look-ahead. |
| int clear_capture_end = clear_capture_start_ + clear_capture_count_ - 1; |
| assembler->ClearRegisters(clear_capture_start_, clear_capture_end); |
| } |
| |
| // Now that we have unwound the stack we find at the top of the stack the |
| // backtrack that the BeginSubmatch node got. |
| assembler->Backtrack(); |
| } |
| |
| void |
| EndNode::Emit(RegExpCompiler* compiler, Trace* trace) |
| { |
| if (!trace->is_trivial()) { |
| trace->Flush(compiler, this); |
| return; |
| } |
| RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| if (!label()->bound()) { |
| assembler->Bind(label()); |
| } |
| switch (action_) { |
| case ACCEPT: |
| assembler->Succeed(); |
| return; |
| case BACKTRACK: |
| assembler->JumpOrBacktrack(trace->backtrack()); |
| return; |
| case NEGATIVE_SUBMATCH_SUCCESS: |
| // This case is handled in a different virtual method. |
| MOZ_CRASH("Bad action: NEGATIVE_SUBMATCH_SUCCESS"); |
| } |
| MOZ_CRASH("Bad action"); |
| } |
| |
| // Emit the code to check for a ^ in multiline mode (1-character lookbehind |
| // that matches newline or the start of input). |
| static void |
| EmitHat(RegExpCompiler* compiler, RegExpNode* on_success, Trace* trace) |
| { |
| RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| |
| // We will be loading the previous character into the current character |
| // register. |
| Trace new_trace(*trace); |
| new_trace.InvalidateCurrentCharacter(); |
| |
| jit::Label ok; |
| if (new_trace.cp_offset() == 0) { |
| // The start of input counts as a newline in this context, so skip to |
| // ok if we are at the start. |
| assembler->CheckAtStart(&ok); |
| } |
| |
| // We already checked that we are not at the start of input so it must be |
| // OK to load the previous character. |
| assembler->LoadCurrentCharacter(new_trace.cp_offset() -1, new_trace.backtrack(), false); |
| |
| if (!assembler->CheckSpecialCharacterClass('n', new_trace.backtrack())) { |
| // Newline means \n, \r, 0x2028 or 0x2029. |
| if (!compiler->ascii()) |
| assembler->CheckCharacterAfterAnd(0x2028, 0xfffe, &ok); |
| assembler->CheckCharacter('\n', &ok); |
| assembler->CheckNotCharacter('\r', new_trace.backtrack()); |
| } |
| assembler->Bind(&ok); |
| on_success->Emit(compiler, &new_trace); |
| } |
| |
| // Check for [0-9A-Z_a-z]. |
| static void |
| EmitWordCheck(RegExpMacroAssembler* assembler, |
| jit::Label* word, jit::Label* non_word, bool fall_through_on_word) |
| { |
| if (assembler->CheckSpecialCharacterClass(fall_through_on_word ? 'w' : 'W', |
| fall_through_on_word ? non_word : word)) |
| { |
| // Optimized implementation available. |
| return; |
| } |
| |
| assembler->CheckCharacterGT('z', non_word); |
| assembler->CheckCharacterLT('0', non_word); |
| assembler->CheckCharacterGT('a' - 1, word); |
| assembler->CheckCharacterLT('9' + 1, word); |
| assembler->CheckCharacterLT('A', non_word); |
| assembler->CheckCharacterLT('Z' + 1, word); |
| |
| if (fall_through_on_word) |
| assembler->CheckNotCharacter('_', non_word); |
| else |
| assembler->CheckCharacter('_', word); |
| } |
| |
| // Emit the code to handle \b and \B (word-boundary or non-word-boundary). |
| void |
| AssertionNode::EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace) |
| { |
| RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| Trace::TriBool next_is_word_character = Trace::UNKNOWN; |
| bool not_at_start = (trace->at_start() == Trace::FALSE_VALUE); |
| BoyerMooreLookahead* lookahead = bm_info(not_at_start); |
| if (lookahead == nullptr) { |
| int eats_at_least = |
| Min(kMaxLookaheadForBoyerMoore, EatsAtLeast(kMaxLookaheadForBoyerMoore, |
| kRecursionBudget, |
| not_at_start)); |
| if (eats_at_least >= 1) { |
| BoyerMooreLookahead* bm = |
| alloc()->newInfallible<BoyerMooreLookahead>(alloc(), eats_at_least, compiler); |
| FillInBMInfo(0, kRecursionBudget, bm, not_at_start); |
| if (bm->at(0)->is_non_word()) |
| next_is_word_character = Trace::FALSE_VALUE; |
| if (bm->at(0)->is_word()) next_is_word_character = Trace::TRUE_VALUE; |
| } |
| } else { |
| if (lookahead->at(0)->is_non_word()) |
| next_is_word_character = Trace::FALSE_VALUE; |
| if (lookahead->at(0)->is_word()) |
| next_is_word_character = Trace::TRUE_VALUE; |
| } |
| bool at_boundary = (assertion_type_ == AssertionNode::AT_BOUNDARY); |
| if (next_is_word_character == Trace::UNKNOWN) { |
| jit::Label before_non_word; |
| jit::Label before_word; |
| if (trace->characters_preloaded() != 1) { |
| assembler->LoadCurrentCharacter(trace->cp_offset(), &before_non_word); |
| } |
| // Fall through on non-word. |
| EmitWordCheck(assembler, &before_word, &before_non_word, false); |
| // Next character is not a word character. |
| assembler->Bind(&before_non_word); |
| jit::Label ok; |
| BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord); |
| assembler->JumpOrBacktrack(&ok); |
| |
| assembler->Bind(&before_word); |
| BacktrackIfPrevious(compiler, trace, at_boundary ? kIsWord : kIsNonWord); |
| assembler->Bind(&ok); |
| } else if (next_is_word_character == Trace::TRUE_VALUE) { |
| BacktrackIfPrevious(compiler, trace, at_boundary ? kIsWord : kIsNonWord); |
| } else { |
| MOZ_ASSERT(next_is_word_character == Trace::FALSE_VALUE); |
|