From 271a6c3487a14599023a9106329505597638d793 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Fri, 30 Aug 2019 10:22:43 +0200 Subject: BASELINE: Update Chromium to 77.0.3865.59 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I1e89a5f3b009a9519a6705102ad65c92fe736f21 Reviewed-by: Michael BrĂ¼ning --- chromium/v8/src/regexp/OWNERS | 2 - chromium/v8/src/regexp/bytecodes-irregexp.h | 87 - chromium/v8/src/regexp/interpreter-irregexp.cc | 693 -- chromium/v8/src/regexp/interpreter-irregexp.h | 31 - chromium/v8/src/regexp/jsregexp-inl.h | 86 - chromium/v8/src/regexp/jsregexp.cc | 7055 -------------------- chromium/v8/src/regexp/jsregexp.h | 1548 ----- chromium/v8/src/regexp/regexp-ast.h | 24 +- .../v8/src/regexp/regexp-bytecode-generator-inl.h | 56 + .../v8/src/regexp/regexp-bytecode-generator.cc | 378 ++ chromium/v8/src/regexp/regexp-bytecode-generator.h | 113 + chromium/v8/src/regexp/regexp-bytecodes.h | 84 + chromium/v8/src/regexp/regexp-compiler-tonode.cc | 1678 +++++ chromium/v8/src/regexp/regexp-compiler.cc | 3551 ++++++++++ chromium/v8/src/regexp/regexp-compiler.h | 657 ++ chromium/v8/src/regexp/regexp-dotprinter.cc | 244 + chromium/v8/src/regexp/regexp-dotprinter.h | 23 + chromium/v8/src/regexp/regexp-interpreter.cc | 680 ++ chromium/v8/src/regexp/regexp-interpreter.h | 33 + .../v8/src/regexp/regexp-macro-assembler-arch.h | 30 + .../regexp/regexp-macro-assembler-irregexp-inl.h | 60 - .../src/regexp/regexp-macro-assembler-irregexp.cc | 455 -- .../src/regexp/regexp-macro-assembler-irregexp.h | 124 - chromium/v8/src/regexp/regexp-macro-assembler.cc | 9 +- chromium/v8/src/regexp/regexp-macro-assembler.h | 8 +- chromium/v8/src/regexp/regexp-nodes.h | 675 ++ chromium/v8/src/regexp/regexp-parser.cc | 66 +- chromium/v8/src/regexp/regexp-parser.h | 10 +- chromium/v8/src/regexp/regexp-utils.cc | 6 +- chromium/v8/src/regexp/regexp.cc | 1018 +++ chromium/v8/src/regexp/regexp.h | 177 + 31 files changed, 9471 insertions(+), 10190 deletions(-) delete mode 100644 chromium/v8/src/regexp/bytecodes-irregexp.h delete mode 100644 chromium/v8/src/regexp/interpreter-irregexp.cc delete mode 100644 chromium/v8/src/regexp/interpreter-irregexp.h delete mode 100644 chromium/v8/src/regexp/jsregexp-inl.h delete mode 100644 chromium/v8/src/regexp/jsregexp.cc delete mode 100644 chromium/v8/src/regexp/jsregexp.h create mode 100644 chromium/v8/src/regexp/regexp-bytecode-generator-inl.h create mode 100644 chromium/v8/src/regexp/regexp-bytecode-generator.cc create mode 100644 chromium/v8/src/regexp/regexp-bytecode-generator.h create mode 100644 chromium/v8/src/regexp/regexp-bytecodes.h create mode 100644 chromium/v8/src/regexp/regexp-compiler-tonode.cc create mode 100644 chromium/v8/src/regexp/regexp-compiler.cc create mode 100644 chromium/v8/src/regexp/regexp-compiler.h create mode 100644 chromium/v8/src/regexp/regexp-dotprinter.cc create mode 100644 chromium/v8/src/regexp/regexp-dotprinter.h create mode 100644 chromium/v8/src/regexp/regexp-interpreter.cc create mode 100644 chromium/v8/src/regexp/regexp-interpreter.h create mode 100644 chromium/v8/src/regexp/regexp-macro-assembler-arch.h delete mode 100644 chromium/v8/src/regexp/regexp-macro-assembler-irregexp-inl.h delete mode 100644 chromium/v8/src/regexp/regexp-macro-assembler-irregexp.cc delete mode 100644 chromium/v8/src/regexp/regexp-macro-assembler-irregexp.h create mode 100644 chromium/v8/src/regexp/regexp-nodes.h create mode 100644 chromium/v8/src/regexp/regexp.cc create mode 100644 chromium/v8/src/regexp/regexp.h (limited to 'chromium/v8/src/regexp') diff --git a/chromium/v8/src/regexp/OWNERS b/chromium/v8/src/regexp/OWNERS index 7f916e12ea6..250c8c6b88b 100644 --- a/chromium/v8/src/regexp/OWNERS +++ b/chromium/v8/src/regexp/OWNERS @@ -1,5 +1,3 @@ -set noparent - jgruber@chromium.org yangguo@chromium.org diff --git a/chromium/v8/src/regexp/bytecodes-irregexp.h b/chromium/v8/src/regexp/bytecodes-irregexp.h deleted file mode 100644 index a27c9a0a2b5..00000000000 --- a/chromium/v8/src/regexp/bytecodes-irregexp.h +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2011 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - - -#ifndef V8_REGEXP_BYTECODES_IRREGEXP_H_ -#define V8_REGEXP_BYTECODES_IRREGEXP_H_ - -namespace v8 { -namespace internal { - - -const int BYTECODE_MASK = 0xff; -// The first argument is packed in with the byte code in one word, but so it -// has 24 bits, but it can be positive and negative so only use 23 bits for -// positive values. -const unsigned int MAX_FIRST_ARG = 0x7fffffu; -const int BYTECODE_SHIFT = 8; - -#define BYTECODE_ITERATOR(V) \ - V(BREAK, 0, 4) /* bc8 */ \ - V(PUSH_CP, 1, 4) /* bc8 pad24 */ \ - V(PUSH_BT, 2, 8) /* bc8 pad24 offset32 */ \ - V(PUSH_REGISTER, 3, 4) /* bc8 reg_idx24 */ \ - V(SET_REGISTER_TO_CP, 4, 8) /* bc8 reg_idx24 offset32 */ \ - V(SET_CP_TO_REGISTER, 5, 4) /* bc8 reg_idx24 */ \ - V(SET_REGISTER_TO_SP, 6, 4) /* bc8 reg_idx24 */ \ - V(SET_SP_TO_REGISTER, 7, 4) /* bc8 reg_idx24 */ \ - V(SET_REGISTER, 8, 8) /* bc8 reg_idx24 value32 */ \ - V(ADVANCE_REGISTER, 9, 8) /* bc8 reg_idx24 value32 */ \ - V(POP_CP, 10, 4) /* bc8 pad24 */ \ - V(POP_BT, 11, 4) /* bc8 pad24 */ \ - V(POP_REGISTER, 12, 4) /* bc8 reg_idx24 */ \ - V(FAIL, 13, 4) /* bc8 pad24 */ \ - V(SUCCEED, 14, 4) /* bc8 pad24 */ \ - V(ADVANCE_CP, 15, 4) /* bc8 offset24 */ \ - V(GOTO, 16, 8) /* bc8 pad24 addr32 */ \ - V(LOAD_CURRENT_CHAR, 17, 8) /* bc8 offset24 addr32 */ \ - V(LOAD_CURRENT_CHAR_UNCHECKED, 18, 4) /* bc8 offset24 */ \ - V(LOAD_2_CURRENT_CHARS, 19, 8) /* bc8 offset24 addr32 */ \ - V(LOAD_2_CURRENT_CHARS_UNCHECKED, 20, 4) /* bc8 offset24 */ \ - V(LOAD_4_CURRENT_CHARS, 21, 8) /* bc8 offset24 addr32 */ \ - V(LOAD_4_CURRENT_CHARS_UNCHECKED, 22, 4) /* bc8 offset24 */ \ - V(CHECK_4_CHARS, 23, 12) /* bc8 pad24 uint32 addr32 */ \ - V(CHECK_CHAR, 24, 8) /* bc8 pad8 uint16 addr32 */ \ - V(CHECK_NOT_4_CHARS, 25, 12) /* bc8 pad24 uint32 addr32 */ \ - V(CHECK_NOT_CHAR, 26, 8) /* bc8 pad8 uint16 addr32 */ \ - V(AND_CHECK_4_CHARS, 27, 16) /* bc8 pad24 uint32 uint32 addr32 */ \ - V(AND_CHECK_CHAR, 28, 12) /* bc8 pad8 uint16 uint32 addr32 */ \ - V(AND_CHECK_NOT_4_CHARS, 29, 16) /* bc8 pad24 uint32 uint32 addr32 */ \ - V(AND_CHECK_NOT_CHAR, 30, 12) /* bc8 pad8 uint16 uint32 addr32 */ \ - V(MINUS_AND_CHECK_NOT_CHAR, 31, 12) /* bc8 pad8 uc16 uc16 uc16 addr32 */ \ - V(CHECK_CHAR_IN_RANGE, 32, 12) /* bc8 pad24 uc16 uc16 addr32 */ \ - V(CHECK_CHAR_NOT_IN_RANGE, 33, 12) /* bc8 pad24 uc16 uc16 addr32 */ \ - V(CHECK_BIT_IN_TABLE, 34, 24) /* bc8 pad24 addr32 bits128 */ \ - V(CHECK_LT, 35, 8) /* bc8 pad8 uc16 addr32 */ \ - V(CHECK_GT, 36, 8) /* bc8 pad8 uc16 addr32 */ \ - V(CHECK_NOT_BACK_REF, 37, 8) /* bc8 reg_idx24 addr32 */ \ - V(CHECK_NOT_BACK_REF_NO_CASE, 38, 8) /* bc8 reg_idx24 addr32 */ \ - V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE, 39, 8) \ - V(CHECK_NOT_BACK_REF_BACKWARD, 40, 8) /* bc8 reg_idx24 addr32 */ \ - V(CHECK_NOT_BACK_REF_NO_CASE_BACKWARD, 41, 8) /* bc8 reg_idx24 addr32 */ \ - V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD, 42, 8) \ - V(CHECK_NOT_REGS_EQUAL, 43, 12) /* bc8 regidx24 reg_idx32 addr32 */ \ - V(CHECK_REGISTER_LT, 44, 12) /* bc8 reg_idx24 value32 addr32 */ \ - V(CHECK_REGISTER_GE, 45, 12) /* bc8 reg_idx24 value32 addr32 */ \ - V(CHECK_REGISTER_EQ_POS, 46, 8) /* bc8 reg_idx24 addr32 */ \ - V(CHECK_AT_START, 47, 8) /* bc8 pad24 addr32 */ \ - V(CHECK_NOT_AT_START, 48, 8) /* bc8 offset24 addr32 */ \ - V(CHECK_GREEDY, 49, 8) /* bc8 pad24 addr32 */ \ - V(ADVANCE_CP_AND_GOTO, 50, 8) /* bc8 offset24 addr32 */ \ - V(SET_CURRENT_POSITION_FROM_END, 51, 4) /* bc8 idx24 */ - -#define DECLARE_BYTECODES(name, code, length) \ - static const int BC_##name = code; -BYTECODE_ITERATOR(DECLARE_BYTECODES) -#undef DECLARE_BYTECODES - -#define DECLARE_BYTECODE_LENGTH(name, code, length) \ - static const int BC_##name##_LENGTH = length; -BYTECODE_ITERATOR(DECLARE_BYTECODE_LENGTH) -#undef DECLARE_BYTECODE_LENGTH - -} // namespace internal -} // namespace v8 - -#endif // V8_REGEXP_BYTECODES_IRREGEXP_H_ diff --git a/chromium/v8/src/regexp/interpreter-irregexp.cc b/chromium/v8/src/regexp/interpreter-irregexp.cc deleted file mode 100644 index 04bb63ee7ab..00000000000 --- a/chromium/v8/src/regexp/interpreter-irregexp.cc +++ /dev/null @@ -1,693 +0,0 @@ -// Copyright 2011 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// A simple interpreter for the Irregexp byte code. - -#include "src/regexp/interpreter-irregexp.h" - -#include "src/ast/ast.h" -#include "src/objects/objects-inl.h" -#include "src/regexp/bytecodes-irregexp.h" -#include "src/regexp/jsregexp.h" -#include "src/regexp/regexp-macro-assembler.h" -#include "src/strings/unicode.h" -#include "src/utils/utils.h" - -#ifdef V8_INTL_SUPPORT -#include "unicode/uchar.h" -#endif // V8_INTL_SUPPORT - -namespace v8 { -namespace internal { - -static bool BackRefMatchesNoCase(Isolate* isolate, int from, int current, - int len, Vector subject, - bool unicode) { - Address offset_a = - reinterpret_cast
(const_cast(&subject.at(from))); - Address offset_b = - reinterpret_cast
(const_cast(&subject.at(current))); - size_t length = len * kUC16Size; - return RegExpMacroAssembler::CaseInsensitiveCompareUC16( - offset_a, offset_b, length, unicode ? nullptr : isolate) == 1; -} - - -static bool BackRefMatchesNoCase(Isolate* isolate, int from, int current, - int len, Vector subject, - bool unicode) { - // For Latin1 characters the unicode flag makes no difference. - for (int i = 0; i < len; i++) { - unsigned int old_char = subject[from++]; - unsigned int new_char = subject[current++]; - if (old_char == new_char) continue; - // Convert both characters to lower case. - old_char |= 0x20; - new_char |= 0x20; - if (old_char != new_char) return false; - // Not letters in the ASCII range and Latin-1 range. - if (!(old_char - 'a' <= 'z' - 'a') && - !(old_char - 224 <= 254 - 224 && old_char != 247)) { - return false; - } - } - return true; -} - - -#ifdef DEBUG -static void TraceInterpreter(const byte* code_base, - const byte* pc, - int stack_depth, - int current_position, - uint32_t current_char, - int bytecode_length, - const char* bytecode_name) { - if (FLAG_trace_regexp_bytecodes) { - bool printable = (current_char < 127 && current_char >= 32); - const char* format = - printable ? - "pc = %02x, sp = %d, curpos = %d, curchar = %08x (%c), bc = %s" : - "pc = %02x, sp = %d, curpos = %d, curchar = %08x .%c., bc = %s"; - PrintF(format, - pc - code_base, - stack_depth, - current_position, - current_char, - printable ? current_char : '.', - bytecode_name); - for (int i = 0; i < bytecode_length; i++) { - printf(", %02x", pc[i]); - } - printf(" "); - for (int i = 1; i < bytecode_length; i++) { - unsigned char b = pc[i]; - if (b < 127 && b >= 32) { - printf("%c", b); - } else { - printf("."); - } - } - printf("\n"); - } -} - - -#define BYTECODE(name) \ - case BC_##name: \ - TraceInterpreter(code_base, \ - pc, \ - static_cast(backtrack_sp - backtrack_stack_base), \ - current, \ - current_char, \ - BC_##name##_LENGTH, \ - #name); -#else -#define BYTECODE(name) \ - case BC_##name: -#endif - - -static int32_t Load32Aligned(const byte* pc) { - DCHECK_EQ(0, reinterpret_cast(pc) & 3); - return *reinterpret_cast(pc); -} - - -static int32_t Load16Aligned(const byte* pc) { - DCHECK_EQ(0, reinterpret_cast(pc) & 1); - return *reinterpret_cast(pc); -} - - -// A simple abstraction over the backtracking stack used by the interpreter. -// This backtracking stack does not grow automatically, but it ensures that the -// the memory held by the stack is released or remembered in a cache if the -// matching terminates. -class BacktrackStack { - public: - BacktrackStack() { data_ = NewArray(kBacktrackStackSize); } - - ~BacktrackStack() { - DeleteArray(data_); - } - - int* data() const { return data_; } - - int max_size() const { return kBacktrackStackSize; } - - private: - static const int kBacktrackStackSize = 10000; - - int* data_; - - DISALLOW_COPY_AND_ASSIGN(BacktrackStack); -}; - -namespace { - -IrregexpInterpreter::Result StackOverflow(Isolate* isolate) { - // We abort interpreter execution after the stack overflow is thrown, and thus - // allow allocation here despite the outer DisallowHeapAllocationScope. - AllowHeapAllocation yes_gc; - isolate->StackOverflow(); - return IrregexpInterpreter::EXCEPTION; -} - -// Runs all pending interrupts. Callers must update unhandlified object -// references after this function completes. -IrregexpInterpreter::Result HandleInterrupts(Isolate* isolate, - Handle subject_string) { - DisallowHeapAllocation no_gc; - - StackLimitCheck check(isolate); - if (check.JsHasOverflowed()) { - // A real stack overflow. - return StackOverflow(isolate); - } - - const bool was_one_byte = - String::IsOneByteRepresentationUnderneath(*subject_string); - - Object result; - { - AllowHeapAllocation yes_gc; - result = isolate->stack_guard()->HandleInterrupts(); - } - - if (result.IsException(isolate)) { - return IrregexpInterpreter::EXCEPTION; - } - - // If we changed between a LATIN1 and a UC16 string, we need to restart - // regexp matching with the appropriate template instantiation of RawMatch. - if (String::IsOneByteRepresentationUnderneath(*subject_string) != - was_one_byte) { - return IrregexpInterpreter::RETRY; - } - - return IrregexpInterpreter::SUCCESS; -} - -template -void UpdateCodeAndSubjectReferences(Isolate* isolate, - Handle code_array, - Handle subject_string, - const byte** code_base_out, - const byte** pc_out, - Vector* subject_string_out) { - DisallowHeapAllocation no_gc; - - if (*code_base_out != code_array->GetDataStartAddress()) { - const intptr_t pc_offset = *pc_out - *code_base_out; - DCHECK_GT(pc_offset, 0); - *code_base_out = code_array->GetDataStartAddress(); - *pc_out = *code_base_out + pc_offset; - } - - DCHECK(subject_string->IsFlat()); - *subject_string_out = subject_string->GetCharVector(no_gc); -} - -template -IrregexpInterpreter::Result RawMatch(Isolate* isolate, - Handle code_array, - Handle subject_string, - Vector subject, int* registers, - int current, uint32_t current_char) { - DisallowHeapAllocation no_gc; - - const byte* pc = code_array->GetDataStartAddress(); - const byte* code_base = pc; - - // BacktrackStack ensures that the memory allocated for the backtracking stack - // is returned to the system or cached if there is no stack being cached at - // the moment. - BacktrackStack backtrack_stack; - int* backtrack_stack_base = backtrack_stack.data(); - int* backtrack_sp = backtrack_stack_base; - int backtrack_stack_space = backtrack_stack.max_size(); -#ifdef DEBUG - if (FLAG_trace_regexp_bytecodes) { - PrintF("\n\nStart bytecode interpreter\n\n"); - } -#endif - while (true) { - int32_t insn = Load32Aligned(pc); - switch (insn & BYTECODE_MASK) { - BYTECODE(BREAK) - UNREACHABLE(); - BYTECODE(PUSH_CP) - if (--backtrack_stack_space < 0) { - return StackOverflow(isolate); - } - *backtrack_sp++ = current; - pc += BC_PUSH_CP_LENGTH; - break; - BYTECODE(PUSH_BT) - if (--backtrack_stack_space < 0) { - return StackOverflow(isolate); - } - *backtrack_sp++ = Load32Aligned(pc + 4); - pc += BC_PUSH_BT_LENGTH; - break; - BYTECODE(PUSH_REGISTER) - if (--backtrack_stack_space < 0) { - return StackOverflow(isolate); - } - *backtrack_sp++ = registers[insn >> BYTECODE_SHIFT]; - pc += BC_PUSH_REGISTER_LENGTH; - break; - BYTECODE(SET_REGISTER) - registers[insn >> BYTECODE_SHIFT] = Load32Aligned(pc + 4); - pc += BC_SET_REGISTER_LENGTH; - break; - BYTECODE(ADVANCE_REGISTER) - registers[insn >> BYTECODE_SHIFT] += Load32Aligned(pc + 4); - pc += BC_ADVANCE_REGISTER_LENGTH; - break; - BYTECODE(SET_REGISTER_TO_CP) - registers[insn >> BYTECODE_SHIFT] = current + Load32Aligned(pc + 4); - pc += BC_SET_REGISTER_TO_CP_LENGTH; - break; - BYTECODE(SET_CP_TO_REGISTER) - current = registers[insn >> BYTECODE_SHIFT]; - pc += BC_SET_CP_TO_REGISTER_LENGTH; - break; - BYTECODE(SET_REGISTER_TO_SP) - registers[insn >> BYTECODE_SHIFT] = - static_cast(backtrack_sp - backtrack_stack_base); - pc += BC_SET_REGISTER_TO_SP_LENGTH; - break; - BYTECODE(SET_SP_TO_REGISTER) - backtrack_sp = backtrack_stack_base + registers[insn >> BYTECODE_SHIFT]; - backtrack_stack_space = backtrack_stack.max_size() - - static_cast(backtrack_sp - backtrack_stack_base); - pc += BC_SET_SP_TO_REGISTER_LENGTH; - break; - BYTECODE(POP_CP) - backtrack_stack_space++; - --backtrack_sp; - current = *backtrack_sp; - pc += BC_POP_CP_LENGTH; - break; - // clang-format off - BYTECODE(POP_BT) { - IrregexpInterpreter::Result return_code = HandleInterrupts( - isolate, subject_string); - if (return_code != IrregexpInterpreter::SUCCESS) return return_code; - - UpdateCodeAndSubjectReferences(isolate, code_array, subject_string, - &code_base, &pc, &subject); - - backtrack_stack_space++; - --backtrack_sp; - pc = code_base + *backtrack_sp; - break; - } - BYTECODE(POP_REGISTER) // clang-format on - backtrack_stack_space++; - --backtrack_sp; - registers[insn >> BYTECODE_SHIFT] = *backtrack_sp; - pc += BC_POP_REGISTER_LENGTH; - break; - BYTECODE(FAIL) - return IrregexpInterpreter::FAILURE; - BYTECODE(SUCCEED) - return IrregexpInterpreter::SUCCESS; - BYTECODE(ADVANCE_CP) - current += insn >> BYTECODE_SHIFT; - pc += BC_ADVANCE_CP_LENGTH; - break; - BYTECODE(GOTO) - pc = code_base + Load32Aligned(pc + 4); - break; - BYTECODE(ADVANCE_CP_AND_GOTO) - current += insn >> BYTECODE_SHIFT; - pc = code_base + Load32Aligned(pc + 4); - break; - BYTECODE(CHECK_GREEDY) - if (current == backtrack_sp[-1]) { - backtrack_sp--; - backtrack_stack_space++; - pc = code_base + Load32Aligned(pc + 4); - } else { - pc += BC_CHECK_GREEDY_LENGTH; - } - break; - BYTECODE(LOAD_CURRENT_CHAR) { - int pos = current + (insn >> BYTECODE_SHIFT); - if (pos >= subject.length() || pos < 0) { - pc = code_base + Load32Aligned(pc + 4); - } else { - current_char = subject[pos]; - pc += BC_LOAD_CURRENT_CHAR_LENGTH; - } - break; - } - BYTECODE(LOAD_CURRENT_CHAR_UNCHECKED) { - int pos = current + (insn >> BYTECODE_SHIFT); - current_char = subject[pos]; - pc += BC_LOAD_CURRENT_CHAR_UNCHECKED_LENGTH; - break; - } - BYTECODE(LOAD_2_CURRENT_CHARS) { - int pos = current + (insn >> BYTECODE_SHIFT); - if (pos + 2 > subject.length() || pos < 0) { - pc = code_base + Load32Aligned(pc + 4); - } else { - Char next = subject[pos + 1]; - current_char = - (subject[pos] | (next << (kBitsPerByte * sizeof(Char)))); - pc += BC_LOAD_2_CURRENT_CHARS_LENGTH; - } - break; - } - BYTECODE(LOAD_2_CURRENT_CHARS_UNCHECKED) { - int pos = current + (insn >> BYTECODE_SHIFT); - Char next = subject[pos + 1]; - current_char = (subject[pos] | (next << (kBitsPerByte * sizeof(Char)))); - pc += BC_LOAD_2_CURRENT_CHARS_UNCHECKED_LENGTH; - break; - } - BYTECODE(LOAD_4_CURRENT_CHARS) { - DCHECK_EQ(1, sizeof(Char)); - int pos = current + (insn >> BYTECODE_SHIFT); - if (pos + 4 > subject.length() || pos < 0) { - pc = code_base + Load32Aligned(pc + 4); - } else { - Char next1 = subject[pos + 1]; - Char next2 = subject[pos + 2]; - Char next3 = subject[pos + 3]; - current_char = (subject[pos] | - (next1 << 8) | - (next2 << 16) | - (next3 << 24)); - pc += BC_LOAD_4_CURRENT_CHARS_LENGTH; - } - break; - } - BYTECODE(LOAD_4_CURRENT_CHARS_UNCHECKED) { - DCHECK_EQ(1, sizeof(Char)); - int pos = current + (insn >> BYTECODE_SHIFT); - Char next1 = subject[pos + 1]; - Char next2 = subject[pos + 2]; - Char next3 = subject[pos + 3]; - current_char = (subject[pos] | - (next1 << 8) | - (next2 << 16) | - (next3 << 24)); - pc += BC_LOAD_4_CURRENT_CHARS_UNCHECKED_LENGTH; - break; - } - BYTECODE(CHECK_4_CHARS) { - uint32_t c = Load32Aligned(pc + 4); - if (c == current_char) { - pc = code_base + Load32Aligned(pc + 8); - } else { - pc += BC_CHECK_4_CHARS_LENGTH; - } - break; - } - BYTECODE(CHECK_CHAR) { - uint32_t c = (insn >> BYTECODE_SHIFT); - if (c == current_char) { - pc = code_base + Load32Aligned(pc + 4); - } else { - pc += BC_CHECK_CHAR_LENGTH; - } - break; - } - BYTECODE(CHECK_NOT_4_CHARS) { - uint32_t c = Load32Aligned(pc + 4); - if (c != current_char) { - pc = code_base + Load32Aligned(pc + 8); - } else { - pc += BC_CHECK_NOT_4_CHARS_LENGTH; - } - break; - } - BYTECODE(CHECK_NOT_CHAR) { - uint32_t c = (insn >> BYTECODE_SHIFT); - if (c != current_char) { - pc = code_base + Load32Aligned(pc + 4); - } else { - pc += BC_CHECK_NOT_CHAR_LENGTH; - } - break; - } - BYTECODE(AND_CHECK_4_CHARS) { - uint32_t c = Load32Aligned(pc + 4); - if (c == (current_char & Load32Aligned(pc + 8))) { - pc = code_base + Load32Aligned(pc + 12); - } else { - pc += BC_AND_CHECK_4_CHARS_LENGTH; - } - break; - } - BYTECODE(AND_CHECK_CHAR) { - uint32_t c = (insn >> BYTECODE_SHIFT); - if (c == (current_char & Load32Aligned(pc + 4))) { - pc = code_base + Load32Aligned(pc + 8); - } else { - pc += BC_AND_CHECK_CHAR_LENGTH; - } - break; - } - BYTECODE(AND_CHECK_NOT_4_CHARS) { - uint32_t c = Load32Aligned(pc + 4); - if (c != (current_char & Load32Aligned(pc + 8))) { - pc = code_base + Load32Aligned(pc + 12); - } else { - pc += BC_AND_CHECK_NOT_4_CHARS_LENGTH; - } - break; - } - BYTECODE(AND_CHECK_NOT_CHAR) { - uint32_t c = (insn >> BYTECODE_SHIFT); - if (c != (current_char & Load32Aligned(pc + 4))) { - pc = code_base + Load32Aligned(pc + 8); - } else { - pc += BC_AND_CHECK_NOT_CHAR_LENGTH; - } - break; - } - BYTECODE(MINUS_AND_CHECK_NOT_CHAR) { - uint32_t c = (insn >> BYTECODE_SHIFT); - uint32_t minus = Load16Aligned(pc + 4); - uint32_t mask = Load16Aligned(pc + 6); - if (c != ((current_char - minus) & mask)) { - pc = code_base + Load32Aligned(pc + 8); - } else { - pc += BC_MINUS_AND_CHECK_NOT_CHAR_LENGTH; - } - break; - } - BYTECODE(CHECK_CHAR_IN_RANGE) { - uint32_t from = Load16Aligned(pc + 4); - uint32_t to = Load16Aligned(pc + 6); - if (from <= current_char && current_char <= to) { - pc = code_base + Load32Aligned(pc + 8); - } else { - pc += BC_CHECK_CHAR_IN_RANGE_LENGTH; - } - break; - } - BYTECODE(CHECK_CHAR_NOT_IN_RANGE) { - uint32_t from = Load16Aligned(pc + 4); - uint32_t to = Load16Aligned(pc + 6); - if (from > current_char || current_char > to) { - pc = code_base + Load32Aligned(pc + 8); - } else { - pc += BC_CHECK_CHAR_NOT_IN_RANGE_LENGTH; - } - break; - } - BYTECODE(CHECK_BIT_IN_TABLE) { - int mask = RegExpMacroAssembler::kTableMask; - byte b = pc[8 + ((current_char & mask) >> kBitsPerByteLog2)]; - int bit = (current_char & (kBitsPerByte - 1)); - if ((b & (1 << bit)) != 0) { - pc = code_base + Load32Aligned(pc + 4); - } else { - pc += BC_CHECK_BIT_IN_TABLE_LENGTH; - } - break; - } - BYTECODE(CHECK_LT) { - uint32_t limit = (insn >> BYTECODE_SHIFT); - if (current_char < limit) { - pc = code_base + Load32Aligned(pc + 4); - } else { - pc += BC_CHECK_LT_LENGTH; - } - break; - } - BYTECODE(CHECK_GT) { - uint32_t limit = (insn >> BYTECODE_SHIFT); - if (current_char > limit) { - pc = code_base + Load32Aligned(pc + 4); - } else { - pc += BC_CHECK_GT_LENGTH; - } - break; - } - BYTECODE(CHECK_REGISTER_LT) - if (registers[insn >> BYTECODE_SHIFT] < Load32Aligned(pc + 4)) { - pc = code_base + Load32Aligned(pc + 8); - } else { - pc += BC_CHECK_REGISTER_LT_LENGTH; - } - break; - BYTECODE(CHECK_REGISTER_GE) - if (registers[insn >> BYTECODE_SHIFT] >= Load32Aligned(pc + 4)) { - pc = code_base + Load32Aligned(pc + 8); - } else { - pc += BC_CHECK_REGISTER_GE_LENGTH; - } - break; - BYTECODE(CHECK_REGISTER_EQ_POS) - if (registers[insn >> BYTECODE_SHIFT] == current) { - pc = code_base + Load32Aligned(pc + 4); - } else { - pc += BC_CHECK_REGISTER_EQ_POS_LENGTH; - } - break; - BYTECODE(CHECK_NOT_REGS_EQUAL) - if (registers[insn >> BYTECODE_SHIFT] == - registers[Load32Aligned(pc + 4)]) { - pc += BC_CHECK_NOT_REGS_EQUAL_LENGTH; - } else { - pc = code_base + Load32Aligned(pc + 8); - } - break; - BYTECODE(CHECK_NOT_BACK_REF) { - int from = registers[insn >> BYTECODE_SHIFT]; - int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; - if (from >= 0 && len > 0) { - if (current + len > subject.length() || - CompareChars(&subject[from], &subject[current], len) != 0) { - pc = code_base + Load32Aligned(pc + 4); - break; - } - current += len; - } - pc += BC_CHECK_NOT_BACK_REF_LENGTH; - break; - } - BYTECODE(CHECK_NOT_BACK_REF_BACKWARD) { - int from = registers[insn >> BYTECODE_SHIFT]; - int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; - if (from >= 0 && len > 0) { - if (current - len < 0 || - CompareChars(&subject[from], &subject[current - len], len) != 0) { - pc = code_base + Load32Aligned(pc + 4); - break; - } - current -= len; - } - pc += BC_CHECK_NOT_BACK_REF_BACKWARD_LENGTH; - break; - } - BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE) - V8_FALLTHROUGH; - BYTECODE(CHECK_NOT_BACK_REF_NO_CASE) { - bool unicode = - (insn & BYTECODE_MASK) == BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE; - int from = registers[insn >> BYTECODE_SHIFT]; - int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; - if (from >= 0 && len > 0) { - if (current + len > subject.length() || - !BackRefMatchesNoCase(isolate, from, current, len, subject, - unicode)) { - pc = code_base + Load32Aligned(pc + 4); - break; - } - current += len; - } - pc += BC_CHECK_NOT_BACK_REF_NO_CASE_LENGTH; - break; - } - BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD) - V8_FALLTHROUGH; - BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_BACKWARD) { - bool unicode = (insn & BYTECODE_MASK) == - BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD; - int from = registers[insn >> BYTECODE_SHIFT]; - int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; - if (from >= 0 && len > 0) { - if (current - len < 0 || - !BackRefMatchesNoCase(isolate, from, current - len, len, subject, - unicode)) { - pc = code_base + Load32Aligned(pc + 4); - break; - } - current -= len; - } - pc += BC_CHECK_NOT_BACK_REF_NO_CASE_BACKWARD_LENGTH; - break; - } - BYTECODE(CHECK_AT_START) - if (current == 0) { - pc = code_base + Load32Aligned(pc + 4); - } else { - pc += BC_CHECK_AT_START_LENGTH; - } - break; - BYTECODE(CHECK_NOT_AT_START) - if (current + (insn >> BYTECODE_SHIFT) == 0) { - pc += BC_CHECK_NOT_AT_START_LENGTH; - } else { - pc = code_base + Load32Aligned(pc + 4); - } - break; - BYTECODE(SET_CURRENT_POSITION_FROM_END) { - int by = static_cast(insn) >> BYTECODE_SHIFT; - if (subject.length() - current > by) { - current = subject.length() - by; - current_char = subject[current - 1]; - } - pc += BC_SET_CURRENT_POSITION_FROM_END_LENGTH; - break; - } - default: - UNREACHABLE(); - break; - } - } -} - -} // namespace - -// static -IrregexpInterpreter::Result IrregexpInterpreter::Match( - Isolate* isolate, Handle code_array, - Handle subject_string, int* registers, int start_position) { - DCHECK(subject_string->IsFlat()); - - // Note: Heap allocation *is* allowed in two situations: - // 1. When creating & throwing a stack overflow exception. The interpreter - // aborts afterwards, and thus possible-moved objects are never used. - // 2. When handling interrupts. We manually relocate unhandlified references - // after interrupts have run. - DisallowHeapAllocation no_gc; - - uc16 previous_char = '\n'; - String::FlatContent subject_content = subject_string->GetFlatContent(no_gc); - if (subject_content.IsOneByte()) { - Vector subject_vector = subject_content.ToOneByteVector(); - if (start_position != 0) previous_char = subject_vector[start_position - 1]; - return RawMatch(isolate, code_array, subject_string, subject_vector, - registers, start_position, previous_char); - } else { - DCHECK(subject_content.IsTwoByte()); - Vector subject_vector = subject_content.ToUC16Vector(); - if (start_position != 0) previous_char = subject_vector[start_position - 1]; - return RawMatch(isolate, code_array, subject_string, subject_vector, - registers, start_position, previous_char); - } -} - -} // namespace internal -} // namespace v8 diff --git a/chromium/v8/src/regexp/interpreter-irregexp.h b/chromium/v8/src/regexp/interpreter-irregexp.h deleted file mode 100644 index a57d40854ed..00000000000 --- a/chromium/v8/src/regexp/interpreter-irregexp.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2011 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// A simple interpreter for the Irregexp byte code. - -#ifndef V8_REGEXP_INTERPRETER_IRREGEXP_H_ -#define V8_REGEXP_INTERPRETER_IRREGEXP_H_ - -#include "src/regexp/jsregexp.h" - -namespace v8 { -namespace internal { - -class V8_EXPORT_PRIVATE IrregexpInterpreter { - public: - enum Result { RETRY = -2, EXCEPTION = -1, FAILURE = 0, SUCCESS = 1 }; - STATIC_ASSERT(EXCEPTION == static_cast(RegExpImpl::RE_EXCEPTION)); - STATIC_ASSERT(FAILURE == static_cast(RegExpImpl::RE_FAILURE)); - STATIC_ASSERT(SUCCESS == static_cast(RegExpImpl::RE_SUCCESS)); - - // The caller is responsible for initializing registers before each call. - static Result Match(Isolate* isolate, Handle code_array, - Handle subject_string, int* registers, - int start_position); -}; - -} // namespace internal -} // namespace v8 - -#endif // V8_REGEXP_INTERPRETER_IRREGEXP_H_ diff --git a/chromium/v8/src/regexp/jsregexp-inl.h b/chromium/v8/src/regexp/jsregexp-inl.h deleted file mode 100644 index b542add17b5..00000000000 --- a/chromium/v8/src/regexp/jsregexp-inl.h +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright 2013 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - - -#ifndef V8_REGEXP_JSREGEXP_INL_H_ -#define V8_REGEXP_JSREGEXP_INL_H_ - -#include "src/objects/js-regexp-inl.h" -#include "src/objects/objects.h" -#include "src/regexp/jsregexp.h" -#include "src/utils/allocation.h" - -namespace v8 { -namespace internal { - - -RegExpImpl::GlobalCache::~GlobalCache() { - // Deallocate the register array if we allocated it in the constructor - // (as opposed to using the existing jsregexp_static_offsets_vector). - if (register_array_size_ > Isolate::kJSRegexpStaticOffsetsVectorSize) { - DeleteArray(register_array_); - } -} - - -int32_t* RegExpImpl::GlobalCache::FetchNext() { - current_match_index_++; - if (current_match_index_ >= num_matches_) { - // Current batch of results exhausted. - // Fail if last batch was not even fully filled. - if (num_matches_ < max_matches_) { - num_matches_ = 0; // Signal failed match. - return nullptr; - } - - int32_t* last_match = - ®ister_array_[(current_match_index_ - 1) * registers_per_match_]; - int last_end_index = last_match[1]; - - if (regexp_->TypeTag() == JSRegExp::ATOM) { - num_matches_ = - RegExpImpl::AtomExecRaw(isolate_, regexp_, subject_, last_end_index, - register_array_, register_array_size_); - } else { - int last_start_index = last_match[0]; - if (last_start_index == last_end_index) { - // Zero-length match. Advance by one code point. - last_end_index = AdvanceZeroLength(last_end_index); - } - if (last_end_index > subject_->length()) { - num_matches_ = 0; // Signal failed match. - return nullptr; - } - num_matches_ = RegExpImpl::IrregexpExecRaw( - isolate_, regexp_, subject_, last_end_index, register_array_, - register_array_size_); - } - - if (num_matches_ <= 0) return nullptr; - current_match_index_ = 0; - return register_array_; - } else { - return ®ister_array_[current_match_index_ * registers_per_match_]; - } -} - - -int32_t* RegExpImpl::GlobalCache::LastSuccessfulMatch() { - int index = current_match_index_ * registers_per_match_; - if (num_matches_ == 0) { - // After a failed match we shift back by one result. - index -= registers_per_match_; - } - return ®ister_array_[index]; -} - -RegExpEngine::CompilationResult::CompilationResult(Isolate* isolate, - const char* error_message) - : error_message(error_message), - code(ReadOnlyRoots(isolate).the_hole_value()) {} - -} // namespace internal -} // namespace v8 - -#endif // V8_REGEXP_JSREGEXP_INL_H_ diff --git a/chromium/v8/src/regexp/jsregexp.cc b/chromium/v8/src/regexp/jsregexp.cc deleted file mode 100644 index a6f3a5ebcba..00000000000 --- a/chromium/v8/src/regexp/jsregexp.cc +++ /dev/null @@ -1,7055 +0,0 @@ -// Copyright 2012 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "src/regexp/jsregexp.h" - -#include -#include - -#include "src/base/platform/platform.h" -#include "src/codegen/compilation-cache.h" -#include "src/diagnostics/code-tracer.h" -#include "src/execution/execution.h" -#include "src/execution/isolate-inl.h" -#include "src/execution/message-template.h" -#include "src/heap/factory.h" -#include "src/heap/heap-inl.h" -#include "src/objects/elements.h" -#include "src/regexp/interpreter-irregexp.h" -#include "src/regexp/jsregexp-inl.h" -#include "src/regexp/regexp-macro-assembler-irregexp.h" -#include "src/regexp/regexp-macro-assembler-tracer.h" -#include "src/regexp/regexp-macro-assembler.h" -#include "src/regexp/regexp-parser.h" -#include "src/regexp/regexp-stack.h" -#ifdef V8_INTL_SUPPORT -#include "src/regexp/special-case.h" -#endif // V8_INTL_SUPPORT -#include "src/runtime/runtime.h" -#include "src/strings/string-search.h" -#include "src/strings/unicode-decoder.h" -#include "src/strings/unicode-inl.h" -#include "src/utils/ostreams.h" -#include "src/utils/splay-tree-inl.h" -#include "src/zone/zone-list-inl.h" - -#ifdef V8_INTL_SUPPORT -#include "unicode/locid.h" -#include "unicode/uniset.h" -#include "unicode/utypes.h" -#endif // V8_INTL_SUPPORT - -#if V8_TARGET_ARCH_IA32 -#include "src/regexp/ia32/regexp-macro-assembler-ia32.h" -#elif V8_TARGET_ARCH_X64 -#include "src/regexp/x64/regexp-macro-assembler-x64.h" -#elif V8_TARGET_ARCH_ARM64 -#include "src/regexp/arm64/regexp-macro-assembler-arm64.h" -#elif V8_TARGET_ARCH_ARM -#include "src/regexp/arm/regexp-macro-assembler-arm.h" -#elif V8_TARGET_ARCH_PPC -#include "src/regexp/ppc/regexp-macro-assembler-ppc.h" -#elif V8_TARGET_ARCH_S390 -#include "src/regexp/s390/regexp-macro-assembler-s390.h" -#elif V8_TARGET_ARCH_MIPS -#include "src/regexp/mips/regexp-macro-assembler-mips.h" -#elif V8_TARGET_ARCH_MIPS64 -#include "src/regexp/mips64/regexp-macro-assembler-mips64.h" -#else -#error Unsupported target architecture. -#endif - -namespace v8 { -namespace internal { - -V8_WARN_UNUSED_RESULT -static inline MaybeHandle ThrowRegExpException( - Isolate* isolate, Handle re, Handle pattern, - Handle error_text) { - THROW_NEW_ERROR(isolate, NewSyntaxError(MessageTemplate::kMalformedRegExp, - pattern, error_text), - Object); -} - -inline void ThrowRegExpException(Isolate* isolate, Handle re, - Handle error_text) { - USE(ThrowRegExpException(isolate, re, Handle(re->Pattern(), isolate), - error_text)); -} - - -ContainedInLattice AddRange(ContainedInLattice containment, - const int* ranges, - int ranges_length, - Interval new_range) { - DCHECK_EQ(1, ranges_length & 1); - DCHECK_EQ(String::kMaxCodePoint + 1, ranges[ranges_length - 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; -} - -// More makes code generation slower, less makes V8 benchmark score lower. -const int kMaxLookaheadForBoyerMoore = 8; -// In a 3-character pattern you can maximally step forwards 3 characters -// at a time, which is not always enough to pay for the extra logic. -const int kPatternTooShortForBoyerMoore = 2; - -// Identifies the sort of regexps where the regexp engine is faster -// than the code used for atom matches. -static bool HasFewDifferentCharacters(Handle pattern) { - int length = Min(kMaxLookaheadForBoyerMoore, pattern->length()); - if (length <= kPatternTooShortForBoyerMoore) return false; - const int kMod = 128; - bool character_found[kMod]; - int different = 0; - memset(&character_found[0], 0, sizeof(character_found)); - for (int i = 0; i < length; i++) { - int ch = (pattern->Get(i) & (kMod - 1)); - if (!character_found[ch]) { - character_found[ch] = true; - different++; - // We declare a regexp low-alphabet if it has at least 3 times as many - // characters as it has different characters. - if (different * 3 > length) return false; - } - } - return true; -} - -// Generic RegExp methods. Dispatches to implementation specific methods. - -MaybeHandle RegExpImpl::Compile(Isolate* isolate, Handle re, - Handle pattern, - JSRegExp::Flags flags) { - DCHECK(pattern->IsFlat()); - - Zone zone(isolate->allocator(), ZONE_NAME); - CompilationCache* compilation_cache = isolate->compilation_cache(); - MaybeHandle maybe_cached = - compilation_cache->LookupRegExp(pattern, flags); - Handle cached; - if (maybe_cached.ToHandle(&cached)) { - re->set_data(*cached); - return re; - } - - PostponeInterruptsScope postpone(isolate); - RegExpCompileData parse_result; - FlatStringReader reader(isolate, pattern); - DCHECK(!isolate->has_pending_exception()); - if (!RegExpParser::ParseRegExp(isolate, &zone, &reader, flags, - &parse_result)) { - // Throw an exception if we fail to parse the pattern. - return ThrowRegExpException(isolate, re, pattern, parse_result.error); - } - - bool has_been_compiled = false; - - if (parse_result.simple && !IgnoreCase(flags) && !IsSticky(flags) && - !HasFewDifferentCharacters(pattern)) { - // Parse-tree is a single atom that is equal to the pattern. - AtomCompile(isolate, re, pattern, flags, pattern); - has_been_compiled = true; - } else if (parse_result.tree->IsAtom() && !IsSticky(flags) && - parse_result.capture_count == 0) { - RegExpAtom* atom = parse_result.tree->AsAtom(); - Vector atom_pattern = atom->data(); - Handle atom_string; - ASSIGN_RETURN_ON_EXCEPTION( - isolate, atom_string, - isolate->factory()->NewStringFromTwoByte(atom_pattern), Object); - if (!IgnoreCase(atom->flags()) && !HasFewDifferentCharacters(atom_string)) { - AtomCompile(isolate, re, pattern, flags, atom_string); - has_been_compiled = true; - } - } - if (!has_been_compiled) { - IrregexpInitialize(isolate, re, pattern, flags, parse_result.capture_count); - } - DCHECK(re->data().IsFixedArray()); - // Compilation succeeded so the data is set on the regexp - // and we can store it in the cache. - Handle data(FixedArray::cast(re->data()), isolate); - compilation_cache->PutRegExp(pattern, flags, data); - - return re; -} - -MaybeHandle RegExpImpl::Exec(Isolate* isolate, Handle regexp, - Handle subject, int index, - Handle last_match_info) { - switch (regexp->TypeTag()) { - case JSRegExp::ATOM: - return AtomExec(isolate, regexp, subject, index, last_match_info); - case JSRegExp::IRREGEXP: { - return IrregexpExec(isolate, regexp, subject, index, last_match_info); - } - default: - UNREACHABLE(); - } -} - - -// RegExp Atom implementation: Simple string search using indexOf. - -void RegExpImpl::AtomCompile(Isolate* isolate, Handle re, - Handle pattern, JSRegExp::Flags flags, - Handle match_pattern) { - isolate->factory()->SetRegExpAtomData(re, JSRegExp::ATOM, pattern, flags, - match_pattern); -} - -static void SetAtomLastCapture(Isolate* isolate, - Handle last_match_info, - String subject, int from, int to) { - SealHandleScope shs(isolate); - last_match_info->SetNumberOfCaptureRegisters(2); - last_match_info->SetLastSubject(subject); - last_match_info->SetLastInput(subject); - last_match_info->SetCapture(0, from); - last_match_info->SetCapture(1, to); -} - -int RegExpImpl::AtomExecRaw(Isolate* isolate, Handle regexp, - Handle subject, int index, int32_t* output, - int output_size) { - DCHECK_LE(0, index); - DCHECK_LE(index, subject->length()); - - subject = String::Flatten(isolate, subject); - DisallowHeapAllocation no_gc; // ensure vectors stay valid - - String needle = String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex)); - int needle_len = needle.length(); - DCHECK(needle.IsFlat()); - DCHECK_LT(0, needle_len); - - if (index + needle_len > subject->length()) { - return RegExpImpl::RE_FAILURE; - } - - for (int i = 0; i < output_size; i += 2) { - String::FlatContent needle_content = needle.GetFlatContent(no_gc); - String::FlatContent subject_content = subject->GetFlatContent(no_gc); - DCHECK(needle_content.IsFlat()); - DCHECK(subject_content.IsFlat()); - // dispatch on type of strings - index = - (needle_content.IsOneByte() - ? (subject_content.IsOneByte() - ? SearchString(isolate, subject_content.ToOneByteVector(), - needle_content.ToOneByteVector(), index) - : SearchString(isolate, subject_content.ToUC16Vector(), - needle_content.ToOneByteVector(), index)) - : (subject_content.IsOneByte() - ? SearchString(isolate, subject_content.ToOneByteVector(), - needle_content.ToUC16Vector(), index) - : SearchString(isolate, subject_content.ToUC16Vector(), - needle_content.ToUC16Vector(), index))); - if (index == -1) { - return i / 2; // Return number of matches. - } else { - output[i] = index; - output[i+1] = index + needle_len; - index += needle_len; - } - } - return output_size / 2; -} - -Handle RegExpImpl::AtomExec(Isolate* isolate, Handle re, - Handle subject, int index, - Handle last_match_info) { - static const int kNumRegisters = 2; - STATIC_ASSERT(kNumRegisters <= Isolate::kJSRegexpStaticOffsetsVectorSize); - int32_t* output_registers = isolate->jsregexp_static_offsets_vector(); - - int res = - AtomExecRaw(isolate, re, subject, index, output_registers, kNumRegisters); - - if (res == RegExpImpl::RE_FAILURE) return isolate->factory()->null_value(); - - DCHECK_EQ(res, RegExpImpl::RE_SUCCESS); - SealHandleScope shs(isolate); - SetAtomLastCapture(isolate, last_match_info, *subject, output_registers[0], - output_registers[1]); - return last_match_info; -} - - -// Irregexp implementation. - -// Ensures that the regexp object contains a compiled version of the -// source for either one-byte or two-byte subject strings. -// If the compiled version doesn't already exist, it is compiled -// from the source pattern. -// If compilation fails, an exception is thrown and this function -// returns false. -bool RegExpImpl::EnsureCompiledIrregexp(Isolate* isolate, Handle re, - Handle sample_subject, - bool is_one_byte) { - Object compiled_code = re->DataAt(JSRegExp::code_index(is_one_byte)); - if (compiled_code != Smi::FromInt(JSRegExp::kUninitializedValue)) { - DCHECK(FLAG_regexp_interpret_all ? compiled_code.IsByteArray() - : compiled_code.IsCode()); - return true; - } - return CompileIrregexp(isolate, re, sample_subject, is_one_byte); -} - -bool RegExpImpl::CompileIrregexp(Isolate* isolate, Handle re, - Handle sample_subject, - bool is_one_byte) { - // Compile the RegExp. - Zone zone(isolate->allocator(), ZONE_NAME); - PostponeInterruptsScope postpone(isolate); -#ifdef DEBUG - Object entry = re->DataAt(JSRegExp::code_index(is_one_byte)); - // When arriving here entry can only be a smi representing an uncompiled - // regexp. - DCHECK(entry.IsSmi()); - int entry_value = Smi::ToInt(entry); - DCHECK_EQ(JSRegExp::kUninitializedValue, entry_value); -#endif - - JSRegExp::Flags flags = re->GetFlags(); - - Handle pattern(re->Pattern(), isolate); - pattern = String::Flatten(isolate, pattern); - RegExpCompileData compile_data; - FlatStringReader reader(isolate, pattern); - if (!RegExpParser::ParseRegExp(isolate, &zone, &reader, flags, - &compile_data)) { - // Throw an exception if we fail to parse the pattern. - // THIS SHOULD NOT HAPPEN. We already pre-parsed it successfully once. - USE(ThrowRegExpException(isolate, re, pattern, compile_data.error)); - return false; - } - RegExpEngine::CompilationResult result = - RegExpEngine::Compile(isolate, &zone, &compile_data, flags, pattern, - sample_subject, is_one_byte); - if (result.error_message != nullptr) { - // Unable to compile regexp. - if (FLAG_correctness_fuzzer_suppressions && - strncmp(result.error_message, "Stack overflow", 15) == 0) { - FATAL("Aborting on stack overflow"); - } - Handle error_message = isolate->factory()->NewStringFromUtf8( - CStrVector(result.error_message)).ToHandleChecked(); - ThrowRegExpException(isolate, re, error_message); - return false; - } - - Handle data = - Handle(FixedArray::cast(re->data()), isolate); - data->set(JSRegExp::code_index(is_one_byte), result.code); - SetIrregexpCaptureNameMap(*data, compile_data.capture_name_map); - int register_max = IrregexpMaxRegisterCount(*data); - if (result.num_registers > register_max) { - SetIrregexpMaxRegisterCount(*data, result.num_registers); - } - - return true; -} - -int RegExpImpl::IrregexpMaxRegisterCount(FixedArray re) { - return Smi::cast(re.get(JSRegExp::kIrregexpMaxRegisterCountIndex)).value(); -} - -void RegExpImpl::SetIrregexpMaxRegisterCount(FixedArray re, int value) { - re.set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(value)); -} - -void RegExpImpl::SetIrregexpCaptureNameMap(FixedArray re, - Handle value) { - if (value.is_null()) { - re.set(JSRegExp::kIrregexpCaptureNameMapIndex, Smi::kZero); - } else { - re.set(JSRegExp::kIrregexpCaptureNameMapIndex, *value); - } -} - -int RegExpImpl::IrregexpNumberOfCaptures(FixedArray re) { - return Smi::ToInt(re.get(JSRegExp::kIrregexpCaptureCountIndex)); -} - -int RegExpImpl::IrregexpNumberOfRegisters(FixedArray re) { - return Smi::ToInt(re.get(JSRegExp::kIrregexpMaxRegisterCountIndex)); -} - -ByteArray RegExpImpl::IrregexpByteCode(FixedArray re, bool is_one_byte) { - return ByteArray::cast(re.get(JSRegExp::code_index(is_one_byte))); -} - -Code RegExpImpl::IrregexpNativeCode(FixedArray re, bool is_one_byte) { - return Code::cast(re.get(JSRegExp::code_index(is_one_byte))); -} - -void RegExpImpl::IrregexpInitialize(Isolate* isolate, Handle re, - Handle pattern, - JSRegExp::Flags flags, int capture_count) { - // Initialize compiled code entries to null. - isolate->factory()->SetRegExpIrregexpData(re, JSRegExp::IRREGEXP, pattern, - flags, capture_count); -} - -int RegExpImpl::IrregexpPrepare(Isolate* isolate, Handle regexp, - Handle subject) { - DCHECK(subject->IsFlat()); - - // Check representation of the underlying storage. - bool is_one_byte = String::IsOneByteRepresentationUnderneath(*subject); - if (!EnsureCompiledIrregexp(isolate, regexp, subject, is_one_byte)) return -1; - - if (FLAG_regexp_interpret_all) { - // Byte-code regexp needs space allocated for all its registers. - // The result captures are copied to the start of the registers array - // if the match succeeds. This way those registers are not clobbered - // when we set the last match info from last successful match. - return IrregexpNumberOfRegisters(FixedArray::cast(regexp->data())) + - (IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())) + 1) * 2; - } else { - // Native regexp only needs room to output captures. Registers are handled - // internally. - return (IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())) + 1) * 2; - } -} - -int RegExpImpl::IrregexpExecRaw(Isolate* isolate, Handle regexp, - Handle subject, int index, - int32_t* output, int output_size) { - Handle irregexp(FixedArray::cast(regexp->data()), isolate); - - DCHECK_LE(0, index); - DCHECK_LE(index, subject->length()); - DCHECK(subject->IsFlat()); - - bool is_one_byte = String::IsOneByteRepresentationUnderneath(*subject); - - if (!FLAG_regexp_interpret_all) { - DCHECK(output_size >= (IrregexpNumberOfCaptures(*irregexp) + 1) * 2); - do { - EnsureCompiledIrregexp(isolate, regexp, subject, is_one_byte); - Handle code(IrregexpNativeCode(*irregexp, is_one_byte), isolate); - // The stack is used to allocate registers for the compiled regexp code. - // This means that in case of failure, the output registers array is left - // untouched and contains the capture results from the previous successful - // match. We can use that to set the last match info lazily. - int res = NativeRegExpMacroAssembler::Match(code, subject, output, - output_size, index, isolate); - if (res != NativeRegExpMacroAssembler::RETRY) { - DCHECK(res != NativeRegExpMacroAssembler::EXCEPTION || - isolate->has_pending_exception()); - STATIC_ASSERT(static_cast(NativeRegExpMacroAssembler::SUCCESS) == - RE_SUCCESS); - STATIC_ASSERT(static_cast(NativeRegExpMacroAssembler::FAILURE) == - RE_FAILURE); - STATIC_ASSERT(static_cast(NativeRegExpMacroAssembler::EXCEPTION) == - RE_EXCEPTION); - return res; - } - // If result is RETRY, the string has changed representation, and we - // must restart from scratch. - // In this case, it means we must make sure we are prepared to handle - // the, potentially, different subject (the string can switch between - // being internal and external, and even between being Latin1 and UC16, - // but the characters are always the same). - IrregexpPrepare(isolate, regexp, subject); - is_one_byte = String::IsOneByteRepresentationUnderneath(*subject); - } while (true); - UNREACHABLE(); - } else { - DCHECK(FLAG_regexp_interpret_all); - DCHECK(output_size >= IrregexpNumberOfRegisters(*irregexp)); - // We must have done EnsureCompiledIrregexp, so we can get the number of - // registers. - int number_of_capture_registers = - (IrregexpNumberOfCaptures(*irregexp) + 1) * 2; - int32_t* raw_output = &output[number_of_capture_registers]; - - do { - // We do not touch the actual capture result registers until we know there - // has been a match so that we can use those capture results to set the - // last match info. - for (int i = number_of_capture_registers - 1; i >= 0; i--) { - raw_output[i] = -1; - } - Handle byte_codes(IrregexpByteCode(*irregexp, is_one_byte), - isolate); - - IrregexpInterpreter::Result result = IrregexpInterpreter::Match( - isolate, byte_codes, subject, raw_output, index); - DCHECK_IMPLIES(result == IrregexpInterpreter::EXCEPTION, - isolate->has_pending_exception()); - - switch (result) { - case IrregexpInterpreter::SUCCESS: - // Copy capture results to the start of the registers array. - MemCopy(output, raw_output, - number_of_capture_registers * sizeof(int32_t)); - return result; - case IrregexpInterpreter::EXCEPTION: - case IrregexpInterpreter::FAILURE: - return result; - case IrregexpInterpreter::RETRY: - // The string has changed representation, and we must restart the - // match. - is_one_byte = String::IsOneByteRepresentationUnderneath(*subject); - EnsureCompiledIrregexp(isolate, regexp, subject, is_one_byte); - break; - } - } while (true); - UNREACHABLE(); - } -} - -MaybeHandle RegExpImpl::IrregexpExec( - Isolate* isolate, Handle regexp, Handle subject, - int previous_index, Handle last_match_info) { - DCHECK_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); - - subject = String::Flatten(isolate, subject); - - // Prepare space for the return values. -#ifdef DEBUG - if (FLAG_regexp_interpret_all && FLAG_trace_regexp_bytecodes) { - String pattern = regexp->Pattern(); - PrintF("\n\nRegexp match: /%s/\n\n", pattern.ToCString().get()); - PrintF("\n\nSubject string: '%s'\n\n", subject->ToCString().get()); - } -#endif - int required_registers = - RegExpImpl::IrregexpPrepare(isolate, regexp, subject); - if (required_registers < 0) { - // Compiling failed with an exception. - DCHECK(isolate->has_pending_exception()); - return MaybeHandle(); - } - - int32_t* output_registers = nullptr; - if (required_registers > Isolate::kJSRegexpStaticOffsetsVectorSize) { - output_registers = NewArray(required_registers); - } - std::unique_ptr auto_release(output_registers); - if (output_registers == nullptr) { - output_registers = isolate->jsregexp_static_offsets_vector(); - } - - int res = - RegExpImpl::IrregexpExecRaw(isolate, regexp, subject, previous_index, - output_registers, required_registers); - if (res == RE_SUCCESS) { - int capture_count = - IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())); - return SetLastMatchInfo(isolate, last_match_info, subject, capture_count, - output_registers); - } - if (res == RE_EXCEPTION) { - DCHECK(isolate->has_pending_exception()); - return MaybeHandle(); - } - DCHECK(res == RE_FAILURE); - return isolate->factory()->null_value(); -} - -Handle RegExpImpl::SetLastMatchInfo( - Isolate* isolate, Handle last_match_info, - Handle subject, int capture_count, int32_t* match) { - // This is the only place where match infos can grow. If, after executing the - // regexp, RegExpExecStub finds that the match info is too small, it restarts - // execution in RegExpImpl::Exec, which finally grows the match info right - // here. - - int capture_register_count = (capture_count + 1) * 2; - Handle result = RegExpMatchInfo::ReserveCaptures( - isolate, last_match_info, capture_register_count); - result->SetNumberOfCaptureRegisters(capture_register_count); - - if (*result != *last_match_info) { - if (*last_match_info == *isolate->regexp_last_match_info()) { - // This inner condition is only needed for special situations like the - // regexp fuzzer, where we pass our own custom RegExpMatchInfo to - // RegExpImpl::Exec; there actually want to bypass the Isolate's match - // info and execute the regexp without side effects. - isolate->native_context()->set_regexp_last_match_info(*result); - } - } - - DisallowHeapAllocation no_allocation; - if (match != nullptr) { - for (int i = 0; i < capture_register_count; i += 2) { - result->SetCapture(i, match[i]); - result->SetCapture(i + 1, match[i + 1]); - } - } - result->SetLastSubject(*subject); - result->SetLastInput(*subject); - return result; -} - -RegExpImpl::GlobalCache::GlobalCache(Handle regexp, - Handle subject, Isolate* isolate) - : register_array_(nullptr), - register_array_size_(0), - regexp_(regexp), - subject_(subject), - isolate_(isolate) { - bool interpreted = FLAG_regexp_interpret_all; - - if (regexp_->TypeTag() == JSRegExp::ATOM) { - static const int kAtomRegistersPerMatch = 2; - registers_per_match_ = kAtomRegistersPerMatch; - // There is no distinction between interpreted and native for atom regexps. - interpreted = false; - } else { - registers_per_match_ = - RegExpImpl::IrregexpPrepare(isolate_, regexp_, subject_); - if (registers_per_match_ < 0) { - num_matches_ = -1; // Signal exception. - return; - } - } - - DCHECK(IsGlobal(regexp->GetFlags())); - if (!interpreted) { - register_array_size_ = - Max(registers_per_match_, Isolate::kJSRegexpStaticOffsetsVectorSize); - max_matches_ = register_array_size_ / registers_per_match_; - } else { - // Global loop in interpreted regexp is not implemented. We choose - // the size of the offsets vector so that it can only store one match. - register_array_size_ = registers_per_match_; - max_matches_ = 1; - } - - if (register_array_size_ > Isolate::kJSRegexpStaticOffsetsVectorSize) { - register_array_ = NewArray(register_array_size_); - } else { - register_array_ = isolate->jsregexp_static_offsets_vector(); - } - - // Set state so that fetching the results the first time triggers a call - // to the compiled regexp. - current_match_index_ = max_matches_ - 1; - num_matches_ = max_matches_; - DCHECK_LE(2, registers_per_match_); // Each match has at least one capture. - DCHECK_GE(register_array_size_, registers_per_match_); - int32_t* last_match = - ®ister_array_[current_match_index_ * registers_per_match_]; - last_match[0] = -1; - last_match[1] = 0; -} - -int RegExpImpl::GlobalCache::AdvanceZeroLength(int last_index) { - if (IsUnicode(regexp_->GetFlags()) && last_index + 1 < subject_->length() && - unibrow::Utf16::IsLeadSurrogate(subject_->Get(last_index)) && - unibrow::Utf16::IsTrailSurrogate(subject_->Get(last_index + 1))) { - // Advance over the surrogate pair. - return last_index + 2; - } - return last_index + 1; -} - -// ------------------------------------------------------------------- -// 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 ast.cc. -// 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 -// -// backtrack code location: -// pop current position -// } -// -// -// * Actions nodes are generated as follows -// -// -// push backtrack code location -// -// backtrack code location: -// -// -// -// * Matching nodes are generated as follows: -// if input string matches at current position -// update current position -// -// else -// -// -// 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. - - -void RegExpTree::AppendToText(RegExpText* text, Zone* zone) { - UNREACHABLE(); -} - - -void RegExpAtom::AppendToText(RegExpText* text, Zone* zone) { - text->AddElement(TextElement::Atom(this), zone); -} - - -void RegExpCharacterClass::AppendToText(RegExpText* text, Zone* zone) { - text->AddElement(TextElement::CharClass(this), zone); -} - - -void RegExpText::AppendToText(RegExpText* text, Zone* zone) { - for (int i = 0; i < elements()->length(); i++) - text->AddElement(elements()->at(i), zone); -} - - -TextElement TextElement::Atom(RegExpAtom* atom) { - return TextElement(ATOM, atom); -} - - -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; - } - UNREACHABLE(); -} - - -DispatchTable* ChoiceNode::GetTable(bool ignore_case) { - if (table_ == nullptr) { - table_ = new(zone()) DispatchTable(zone()); - DispatchTableConstructor cons(table_, ignore_case, zone()); - cons.BuildTable(this); - } - return table_; -} - - -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) { - DCHECK((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 RegExpCompiler { - public: - RegExpCompiler(Isolate* isolate, Zone* zone, int capture_count, - bool is_one_byte); - - int AllocateRegister() { - if (next_register_ >= RegExpMacroAssembler::kMaxRegister) { - reg_exp_too_big_ = true; - return next_register_; - } - return next_register_++; - } - - // Lookarounds to match lone surrogates for unicode character class matches - // are never nested. We can therefore reuse registers. - int UnicodeLookaroundStackRegister() { - if (unicode_lookaround_stack_register_ == kNoRegister) { - unicode_lookaround_stack_register_ = AllocateRegister(); - } - return unicode_lookaround_stack_register_; - } - - int UnicodeLookaroundPositionRegister() { - if (unicode_lookaround_position_register_ == kNoRegister) { - unicode_lookaround_position_register_ = AllocateRegister(); - } - return unicode_lookaround_position_register_; - } - - RegExpEngine::CompilationResult Assemble(Isolate* isolate, - RegExpMacroAssembler* assembler, - RegExpNode* start, int capture_count, - Handle pattern); - - inline void AddWork(RegExpNode* node) { - if (!node->on_work_list() && !node->label()->is_bound()) { - node->set_on_work_list(true); - work_list_->push_back(node); - } - } - - 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 one_byte() { return one_byte_; } - inline bool optimize() { return optimize_; } - inline void set_optimize(bool value) { optimize_ = value; } - inline bool limiting_recursion() { return limiting_recursion_; } - inline void set_limiting_recursion(bool value) { - limiting_recursion_ = value; - } - bool read_backward() { return read_backward_; } - void set_read_backward(bool value) { read_backward_ = value; } - 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; - } - - Isolate* isolate() const { return isolate_; } - Zone* zone() const { return zone_; } - - static const int kNoRegister = -1; - - private: - EndNode* accept_; - int next_register_; - int unicode_lookaround_stack_register_; - int unicode_lookaround_position_register_; - std::vector* work_list_; - int recursion_depth_; - RegExpMacroAssembler* macro_assembler_; - bool one_byte_; - bool reg_exp_too_big_; - bool limiting_recursion_; - bool optimize_; - bool read_backward_; - int current_expansion_factor_; - FrequencyCollator frequency_collator_; - Isolate* isolate_; - Zone* zone_; -}; - - -class RecursionCheck { - public: - explicit RecursionCheck(RegExpCompiler* compiler) : compiler_(compiler) { - compiler->IncrementRecursionDepth(); - } - ~RecursionCheck() { compiler_->DecrementRecursionDepth(); } - private: - RegExpCompiler* compiler_; -}; - - -static RegExpEngine::CompilationResult IrregexpRegExpTooBig(Isolate* isolate) { - return RegExpEngine::CompilationResult(isolate, "RegExp too big"); -} - - -// 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(Isolate* isolate, Zone* zone, int capture_count, - bool one_byte) - : next_register_(2 * (capture_count + 1)), - unicode_lookaround_stack_register_(kNoRegister), - unicode_lookaround_position_register_(kNoRegister), - work_list_(nullptr), - recursion_depth_(0), - one_byte_(one_byte), - reg_exp_too_big_(false), - limiting_recursion_(false), - optimize_(FLAG_regexp_optimization), - read_backward_(false), - current_expansion_factor_(1), - frequency_collator_(), - isolate_(isolate), - zone_(zone) { - accept_ = new(zone) EndNode(EndNode::ACCEPT, zone); - DCHECK_GE(RegExpMacroAssembler::kMaxRegister, next_register_ - 1); -} - -RegExpEngine::CompilationResult RegExpCompiler::Assemble( - Isolate* isolate, RegExpMacroAssembler* macro_assembler, RegExpNode* start, - int capture_count, Handle pattern) { -#ifdef DEBUG - if (FLAG_trace_regexp_assembler) - macro_assembler_ = new RegExpMacroAssemblerTracer(isolate, macro_assembler); - else -#endif - macro_assembler_ = macro_assembler; - - std::vector work_list; - work_list_ = &work_list; - Label fail; - macro_assembler_->PushBacktrack(&fail); - Trace new_trace; - start->Emit(this, &new_trace); - macro_assembler_->Bind(&fail); - macro_assembler_->Fail(); - while (!work_list.empty()) { - RegExpNode* node = work_list.back(); - work_list.pop_back(); - node->set_on_work_list(false); - if (!node->label()->is_bound()) node->Emit(this, &new_trace); - } - if (reg_exp_too_big_) { - macro_assembler_->AbortedCodeGeneration(); - return IrregexpRegExpTooBig(isolate_); - } - - Handle code = macro_assembler_->GetCode(pattern); - isolate->IncreaseTotalRegexpCodeGenerated(code->Size()); - work_list_ = nullptr; -#ifdef ENABLE_DISASSEMBLER - if (FLAG_print_code && !FLAG_regexp_interpret_all) { - CodeTracer::Scope trace_scope(isolate->GetCodeTracer()); - OFStream os(trace_scope.file()); - Handle::cast(code)->Disassemble(pattern->ToCString().get(), os); - } -#endif -#ifdef DEBUG - if (FLAG_trace_regexp_assembler) { - delete macro_assembler_; - } -#endif - return RegExpEngine::CompilationResult(*code, next_register_); -} - - -bool Trace::DeferredAction::Mentions(int that) { - if (action_type() == ActionNode::CLEAR_CAPTURES) { - Interval range = static_cast(this)->range(); - return range.Contains(that); - } else { - 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) { - DCHECK_EQ(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(action)->cp_offset(); - return true; - } else { - return false; - } - } - } - return false; -} - - -int Trace::FindAffectedRegisters(OutSet* affected_registers, - Zone* zone) { - 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(action)->range(); - for (int i = range.from(); i <= range.to(); i++) - affected_registers->Set(i, zone); - if (range.to() > max_register) max_register = range.to(); - } else { - affected_registers->Set(action->reg(), zone); - if (action->reg() > max_register) max_register = action->reg(); - } - } - return max_register; -} - - -void Trace::RestoreAffectedRegisters(RegExpMacroAssembler* assembler, - int max_register, - const OutSet& registers_to_pop, - const 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); - } - } -} - - -void Trace::PerformDeferredActions(RegExpMacroAssembler* assembler, - int max_register, - const OutSet& affected_registers, - OutSet* registers_to_pop, - OutSet* registers_to_clear, - Zone* zone) { - // 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). - enum DeferredActionUndoType { IGNORE, RESTORE, CLEAR }; - DeferredActionUndoType undo_action = IGNORE; - - int value = 0; - bool absolute = false; - bool clear = false; - static const int kNoStore = kMinInt; - int store_position = kNoStore; - // 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(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 - // occur 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 = RESTORE; - DCHECK_EQ(store_position, kNoStore); - DCHECK(!clear); - break; - } - case ActionNode::INCREMENT_REGISTER: - if (!absolute) { - value++; - } - DCHECK_EQ(store_position, kNoStore); - DCHECK(!clear); - undo_action = RESTORE; - break; - case ActionNode::STORE_POSITION: { - Trace::DeferredCapture* pc = - static_cast(action); - if (!clear && store_position == kNoStore) { - 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 = IGNORE; - } else { - undo_action = pc->is_capture() ? CLEAR : RESTORE; - } - DCHECK(!absolute); - DCHECK_EQ(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 == kNoStore) { - clear = true; - } - undo_action = RESTORE; - DCHECK(!absolute); - DCHECK_EQ(value, 0); - break; - } - default: - UNREACHABLE(); - break; - } - } - } - // Prepare for the undo-action (e.g., push if it's going to be popped). - if (undo_action == 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(reg, zone); - } else if (undo_action == CLEAR) { - registers_to_clear->Set(reg, zone); - } - // Perform the chronologically last action (or accumulated increment) - // for the register. - if (store_position != kNoStore) { - 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(); - - DCHECK(!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(&affected_registers, - compiler->zone()); - OutSet registers_to_pop; - OutSet registers_to_clear; - PerformDeferredActions(assembler, - max_register, - affected_registers, - ®isters_to_pop, - ®isters_to_clear, - compiler->zone()); - if (cp_offset_ != 0) { - assembler->AdvanceCurrentPosition(cp_offset_); - } - - // Create a new trivial state and generate the node with that. - Label undo; - assembler->PushBacktrack(&undo); - if (successor->KeepRecursing(compiler)) { - Trace new_state; - successor->Emit(compiler, &new_state); - } else { - compiler->AddWork(successor); - assembler->GoTo(successor->label()); - } - - // On backtrack we need to restore state. - assembler->Bind(&undo); - RestoreAffectedRegisters(assembler, - max_register, - registers_to_pop, - registers_to_clear); - if (backtrack() == nullptr) { - assembler->Backtrack(); - } else { - assembler->PopCurrentPosition(); - assembler->GoTo(backtrack()); - } -} - - -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()->is_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->ReadStackPointerFromRegister(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()->is_bound()) { - assembler->Bind(label()); - } - switch (action_) { - case ACCEPT: - assembler->Succeed(); - return; - case BACKTRACK: - assembler->GoTo(trace->backtrack()); - return; - case NEGATIVE_SUBMATCH_SUCCESS: - // This case is handled in a different virtual method. - UNREACHABLE(); - } - UNIMPLEMENTED(); -} - - -void GuardedAlternative::AddGuard(Guard* guard, Zone* zone) { - if (guards_ == nullptr) guards_ = new (zone) ZoneList(1, zone); - guards_->Add(guard, zone); -} - - -ActionNode* ActionNode::SetRegister(int reg, - int val, - RegExpNode* on_success) { - ActionNode* result = - new(on_success->zone()) ActionNode(SET_REGISTER, on_success); - result->data_.u_store_register.reg = reg; - result->data_.u_store_register.value = val; - return result; -} - - -ActionNode* ActionNode::IncrementRegister(int reg, RegExpNode* on_success) { - ActionNode* result = - new(on_success->zone()) ActionNode(INCREMENT_REGISTER, on_success); - result->data_.u_increment_register.reg = reg; - return result; -} - - -ActionNode* ActionNode::StorePosition(int reg, - bool is_capture, - RegExpNode* on_success) { - ActionNode* result = - new(on_success->zone()) ActionNode(STORE_POSITION, on_success); - result->data_.u_position_register.reg = reg; - result->data_.u_position_register.is_capture = is_capture; - return result; -} - - -ActionNode* ActionNode::ClearCaptures(Interval range, - RegExpNode* on_success) { - ActionNode* result = - new(on_success->zone()) 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; -} - - -ActionNode* ActionNode::BeginSubmatch(int stack_reg, - int position_reg, - RegExpNode* on_success) { - ActionNode* result = - new(on_success->zone()) ActionNode(BEGIN_SUBMATCH, on_success); - result->data_.u_submatch.stack_pointer_register = stack_reg; - result->data_.u_submatch.current_position_register = position_reg; - return result; -} - - -ActionNode* ActionNode::PositiveSubmatchSuccess(int stack_reg, - int position_reg, - int clear_register_count, - int clear_register_from, - RegExpNode* on_success) { - ActionNode* result = - new(on_success->zone()) ActionNode(POSITIVE_SUBMATCH_SUCCESS, on_success); - result->data_.u_submatch.stack_pointer_register = stack_reg; - result->data_.u_submatch.current_position_register = position_reg; - result->data_.u_submatch.clear_register_count = clear_register_count; - result->data_.u_submatch.clear_register_from = clear_register_from; - return result; -} - - -ActionNode* ActionNode::EmptyMatchCheck(int start_register, - int repetition_register, - int repetition_limit, - RegExpNode* on_success) { - ActionNode* result = - new(on_success->zone()) 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; -} - - -#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); -} - - -// ------------------------------------------------------------------- -// Emit code. - - -void ChoiceNode::GenerateGuard(RegExpMacroAssembler* macro_assembler, - Guard* guard, - Trace* trace) { - switch (guard->op()) { - case Guard::LT: - DCHECK(!trace->mentions_reg(guard->reg())); - macro_assembler->IfRegisterGE(guard->reg(), - guard->value(), - trace->backtrack()); - break; - case Guard::GEQ: - DCHECK(!trace->mentions_reg(guard->reg())); - macro_assembler->IfRegisterLT(guard->reg(), - guard->value(), - trace->backtrack()); - break; - } -} - - -// Returns the number of characters in the equivalence class, omitting those -// that cannot occur in the source string because it is Latin1. -static int GetCaseIndependentLetters(Isolate* isolate, uc16 character, - bool one_byte_subject, - unibrow::uchar* letters, - int letter_length) { -#ifdef V8_INTL_SUPPORT - icu::UnicodeSet set; - set.add(character); - set = set.closeOver(USET_CASE_INSENSITIVE); - int32_t range_count = set.getRangeCount(); - int items = 0; - for (int32_t i = 0; i < range_count; i++) { - UChar32 start = set.getRangeStart(i); - UChar32 end = set.getRangeEnd(i); - CHECK(end - start + items <= letter_length); - while (start <= end) { - if (one_byte_subject && start > String::kMaxOneByteCharCode) break; - letters[items++] = (unibrow::uchar)(start); - start++; - } - } - return items; -#else - int length = - isolate->jsregexp_uncanonicalize()->get(character, '\0', letters); - // Unibrow returns 0 or 1 for characters where case independence is - // trivial. - if (length == 0) { - letters[0] = character; - length = 1; - } - - if (one_byte_subject) { - int new_length = 0; - for (int i = 0; i < length; i++) { - if (letters[i] <= String::kMaxOneByteCharCode) { - letters[new_length++] = letters[i]; - } - } - length = new_length; - } - - return length; -#endif // V8_INTL_SUPPORT -} - -static inline bool EmitSimpleCharacter(Isolate* isolate, - RegExpCompiler* compiler, - uc16 c, - Label* on_failure, - int cp_offset, - bool check, - bool preloaded) { - RegExpMacroAssembler* assembler = compiler->macro_assembler(); - bool bound_checked = false; - if (!preloaded) { - assembler->LoadCurrentCharacter( - cp_offset, - on_failure, - check); - bound_checked = true; - } - assembler->CheckNotCharacter(c, on_failure); - return bound_checked; -} - - -// Only emits non-letters (things that don't have case). Only used for case -// independent matches. -static inline bool EmitAtomNonLetter(Isolate* isolate, - RegExpCompiler* compiler, - uc16 c, - Label* on_failure, - int cp_offset, - bool check, - bool preloaded) { - RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); - bool one_byte = compiler->one_byte(); - unibrow::uchar chars[4]; - int length = GetCaseIndependentLetters(isolate, c, one_byte, chars, 4); - if (length < 1) { - // This can't match. Must be an one-byte subject and a non-one-byte - // character. We do not need to do anything since the one-byte pass - // already handled this. - return false; // Bounds not checked. - } - bool checked = false; - // We handle the length > 1 case in a later pass. - if (length == 1) { - if (one_byte && c > String::kMaxOneByteCharCodeU) { - // Can't match - see above. - return false; // Bounds not checked. - } - if (!preloaded) { - macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check); - checked = check; - } - macro_assembler->CheckNotCharacter(c, on_failure); - } - return checked; -} - - -static bool ShortCutEmitCharacterPair(RegExpMacroAssembler* macro_assembler, - bool one_byte, uc16 c1, uc16 c2, - Label* on_failure) { - uc16 char_mask; - if (one_byte) { - char_mask = String::kMaxOneByteCharCode; - } else { - char_mask = String::kMaxUtf16CodeUnit; - } - uc16 exor = c1 ^ c2; - // Check whether exor has only one bit set. - if (((exor - 1) & exor) == 0) { - // If c1 and c2 differ only by one bit. - // Ecma262UnCanonicalize always gives the highest number last. - DCHECK(c2 > c1); - uc16 mask = char_mask ^ exor; - macro_assembler->CheckNotCharacterAfterAnd(c1, mask, on_failure); - return true; - } - DCHECK(c2 > c1); - uc16 diff = c2 - c1; - if (((diff - 1) & diff) == 0 && c1 >= diff) { - // If the characters differ by 2^n but don't differ by one bit then - // subtract the difference from the found character, then do the or - // trick. We avoid the theoretical case where negative numbers are - // involved in order to simplify code generation. - uc16 mask = char_mask ^ diff; - macro_assembler->CheckNotCharacterAfterMinusAnd(c1 - diff, - diff, - mask, - on_failure); - return true; - } - return false; -} - -using EmitCharacterFunction = bool(Isolate* isolate, RegExpCompiler* compiler, - uc16 c, Label* on_failure, int cp_offset, - bool check, bool preloaded); - -// Only emits letters (things that have case). Only used for case independent -// matches. -static inline bool EmitAtomLetter(Isolate* isolate, - RegExpCompiler* compiler, - uc16 c, - Label* on_failure, - int cp_offset, - bool check, - bool preloaded) { - RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); - bool one_byte = compiler->one_byte(); - unibrow::uchar chars[4]; - int length = GetCaseIndependentLetters(isolate, c, one_byte, chars, 4); - if (length <= 1) return false; - // We may not need to check against the end of the input string - // if this character lies before a character that matched. - if (!preloaded) { - macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check); - } - Label ok; - switch (length) { - case 2: { - if (ShortCutEmitCharacterPair(macro_assembler, one_byte, chars[0], - chars[1], on_failure)) { - } else { - macro_assembler->CheckCharacter(chars[0], &ok); - macro_assembler->CheckNotCharacter(chars[1], on_failure); - macro_assembler->Bind(&ok); - } - break; - } - case 4: - macro_assembler->CheckCharacter(chars[3], &ok); - V8_FALLTHROUGH; - case 3: - macro_assembler->CheckCharacter(chars[0], &ok); - macro_assembler->CheckCharacter(chars[1], &ok); - macro_assembler->CheckNotCharacter(chars[2], on_failure); - macro_assembler->Bind(&ok); - break; - default: - UNREACHABLE(); - } - return true; -} - - -static void EmitBoundaryTest(RegExpMacroAssembler* masm, - int border, - Label* fall_through, - Label* above_or_equal, - Label* below) { - if (below != fall_through) { - masm->CheckCharacterLT(border, below); - if (above_or_equal != fall_through) masm->GoTo(above_or_equal); - } else { - masm->CheckCharacterGT(border - 1, above_or_equal); - } -} - - -static void EmitDoubleBoundaryTest(RegExpMacroAssembler* masm, - int first, - int last, - Label* fall_through, - Label* in_range, - Label* out_of_range) { - if (in_range == fall_through) { - if (first == last) { - masm->CheckNotCharacter(first, out_of_range); - } else { - masm->CheckCharacterNotInRange(first, last, out_of_range); - } - } else { - if (first == last) { - masm->CheckCharacter(first, in_range); - } else { - masm->CheckCharacterInRange(first, last, in_range); - } - if (out_of_range != fall_through) masm->GoTo(out_of_range); - } -} - - -// even_label is for ranges[i] to ranges[i + 1] where i - start_index is even. -// odd_label is for ranges[i] to ranges[i + 1] where i - start_index is odd. -static void EmitUseLookupTable( - RegExpMacroAssembler* masm, - ZoneList* ranges, - int start_index, - int end_index, - int min_char, - Label* fall_through, - Label* even_label, - Label* odd_label) { - static const int kSize = RegExpMacroAssembler::kTableSize; - static const int kMask = RegExpMacroAssembler::kTableMask; - - int base = (min_char & ~kMask); - USE(base); - - // Assert that everything is on one kTableSize page. - for (int i = start_index; i <= end_index; i++) { - DCHECK_EQ(ranges->at(i) & ~kMask, base); - } - DCHECK(start_index == 0 || (ranges->at(start_index - 1) & ~kMask) <= base); - - char templ[kSize]; - Label* on_bit_set; - Label* on_bit_clear; - int bit; - if (even_label == fall_through) { - on_bit_set = odd_label; - on_bit_clear = even_label; - bit = 1; - } else { - on_bit_set = even_label; - on_bit_clear = odd_label; - bit = 0; - } - for (int i = 0; i < (ranges->at(start_index) & kMask) && i < kSize; i++) { - templ[i] = bit; - } - int j = 0; - bit ^= 1; - for (int i = start_index; i < end_index; i++) { - for (j = (ranges->at(i) & kMask); j < (ranges->at(i + 1) & kMask); j++) { - templ[j] = bit; - } - bit ^= 1; - } - for (int i = j; i < kSize; i++) { - templ[i] = bit; - } - Factory* factory = masm->isolate()->factory(); - // TODO(erikcorry): Cache these. - Handle ba = factory->NewByteArray(kSize, AllocationType::kOld); - for (int i = 0; i < kSize; i++) { - ba->set(i, templ[i]); - } - masm->CheckBitInTable(ba, on_bit_set); - if (on_bit_clear != fall_through) masm->GoTo(on_bit_clear); -} - - -static void CutOutRange(RegExpMacroAssembler* masm, - ZoneList* ranges, - int start_index, - int end_index, - int cut_index, - Label* even_label, - Label* odd_label) { - bool odd = (((cut_index - start_index) & 1) == 1); - Label* in_range_label = odd ? odd_label : even_label; - Label dummy; - EmitDoubleBoundaryTest(masm, - ranges->at(cut_index), - ranges->at(cut_index + 1) - 1, - &dummy, - in_range_label, - &dummy); - DCHECK(!dummy.is_linked()); - // Cut out the single range by rewriting the array. This creates a new - // range that is a merger of the two ranges on either side of the one we - // are cutting out. The oddity of the labels is preserved. - for (int j = cut_index; j > start_index; j--) { - ranges->at(j) = ranges->at(j - 1); - } - for (int j = cut_index + 1; j < end_index; j++) { - ranges->at(j) = ranges->at(j + 1); - } -} - - -// Unicode case. Split the search space into kSize spaces that are handled -// with recursion. -static void SplitSearchSpace(ZoneList* ranges, - int start_index, - int end_index, - int* new_start_index, - int* new_end_index, - int* border) { - static const int kSize = RegExpMacroAssembler::kTableSize; - static const int kMask = RegExpMacroAssembler::kTableMask; - - int first = ranges->at(start_index); - int last = ranges->at(end_index) - 1; - - *new_start_index = start_index; - *border = (ranges->at(start_index) & ~kMask) + kSize; - while (*new_start_index < end_index) { - if (ranges->at(*new_start_index) > *border) break; - (*new_start_index)++; - } - // new_start_index is the index of the first edge that is beyond the - // current kSize space. - - // For very large search spaces we do a binary chop search of the non-Latin1 - // space instead of just going to the end of the current kSize space. The - // heuristics are complicated a little by the fact that any 128-character - // encoding space can be quickly tested with a table lookup, so we don't - // wish to do binary chop search at a smaller granularity than that. A - // 128-character space can take up a lot of space in the ranges array if, - // for example, we only want to match every second character (eg. the lower - // case characters on some Unicode pages). - int binary_chop_index = (end_index + start_index) / 2; - // The first test ensures that we get to the code that handles the Latin1 - // range with a single not-taken branch, speeding up this important - // character range (even non-Latin1 charset-based text has spaces and - // punctuation). - if (*border - 1 > String::kMaxOneByteCharCode && // Latin1 case. - end_index - start_index > (*new_start_index - start_index) * 2 && - last - first > kSize * 2 && binary_chop_index > *new_start_index && - ranges->at(binary_chop_index) >= first + 2 * kSize) { - int scan_forward_for_section_border = binary_chop_index;; - int new_border = (ranges->at(binary_chop_index) | kMask) + 1; - - while (scan_forward_for_section_border < end_index) { - if (ranges->at(scan_forward_for_section_border) > new_border) { - *new_start_index = scan_forward_for_section_border; - *border = new_border; - break; - } - scan_forward_for_section_border++; - } - } - - DCHECK(*new_start_index > start_index); - *new_end_index = *new_start_index - 1; - if (ranges->at(*new_end_index) == *border) { - (*new_end_index)--; - } - if (*border >= ranges->at(end_index)) { - *border = ranges->at(end_index); - *new_start_index = end_index; // Won't be used. - *new_end_index = end_index - 1; - } -} - -// Gets a series of segment boundaries representing a character class. If the -// character is in the range between an even and an odd boundary (counting from -// start_index) then go to even_label, otherwise go to odd_label. We already -// know that the character is in the range of min_char to max_char inclusive. -// Either label can be nullptr indicating backtracking. Either label can also -// be equal to the fall_through label. -static void GenerateBranches(RegExpMacroAssembler* masm, ZoneList* ranges, - int start_index, int end_index, uc32 min_char, - uc32 max_char, Label* fall_through, - Label* even_label, Label* odd_label) { - DCHECK_LE(min_char, String::kMaxUtf16CodeUnit); - DCHECK_LE(max_char, String::kMaxUtf16CodeUnit); - - int first = ranges->at(start_index); - int last = ranges->at(end_index) - 1; - - DCHECK_LT(min_char, first); - - // Just need to test if the character is before or on-or-after - // a particular character. - if (start_index == end_index) { - EmitBoundaryTest(masm, first, fall_through, even_label, odd_label); - return; - } - - // Another almost trivial case: There is one interval in the middle that is - // different from the end intervals. - if (start_index + 1 == end_index) { - EmitDoubleBoundaryTest( - masm, first, last, fall_through, even_label, odd_label); - return; - } - - // It's not worth using table lookup if there are very few intervals in the - // character class. - if (end_index - start_index <= 6) { - // It is faster to test for individual characters, so we look for those - // first, then try arbitrary ranges in the second round. - static int kNoCutIndex = -1; - int cut = kNoCutIndex; - for (int i = start_index; i < end_index; i++) { - if (ranges->at(i) == ranges->at(i + 1) - 1) { - cut = i; - break; - } - } - if (cut == kNoCutIndex) cut = start_index; - CutOutRange( - masm, ranges, start_index, end_index, cut, even_label, odd_label); - DCHECK_GE(end_index - start_index, 2); - GenerateBranches(masm, - ranges, - start_index + 1, - end_index - 1, - min_char, - max_char, - fall_through, - even_label, - odd_label); - return; - } - - // If there are a lot of intervals in the regexp, then we will use tables to - // determine whether the character is inside or outside the character class. - static const int kBits = RegExpMacroAssembler::kTableSizeBits; - - if ((max_char >> kBits) == (min_char >> kBits)) { - EmitUseLookupTable(masm, - ranges, - start_index, - end_index, - min_char, - fall_through, - even_label, - odd_label); - return; - } - - if ((min_char >> kBits) != (first >> kBits)) { - masm->CheckCharacterLT(first, odd_label); - GenerateBranches(masm, - ranges, - start_index + 1, - end_index, - first, - max_char, - fall_through, - odd_label, - even_label); - return; - } - - int new_start_index = 0; - int new_end_index = 0; - int border = 0; - - SplitSearchSpace(ranges, - start_index, - end_index, - &new_start_index, - &new_end_index, - &border); - - Label handle_rest; - Label* above = &handle_rest; - if (border == last + 1) { - // We didn't find any section that started after the limit, so everything - // above the border is one of the terminal labels. - above = (end_index & 1) != (start_index & 1) ? odd_label : even_label; - DCHECK(new_end_index == end_index - 1); - } - - DCHECK_LE(start_index, new_end_index); - DCHECK_LE(new_start_index, end_index); - DCHECK_LT(start_index, new_start_index); - DCHECK_LT(new_end_index, end_index); - DCHECK(new_end_index + 1 == new_start_index || - (new_end_index + 2 == new_start_index && - border == ranges->at(new_end_index + 1))); - DCHECK_LT(min_char, border - 1); - DCHECK_LT(border, max_char); - DCHECK_LT(ranges->at(new_end_index), border); - DCHECK(border < ranges->at(new_start_index) || - (border == ranges->at(new_start_index) && - new_start_index == end_index && - new_end_index == end_index - 1 && - border == last + 1)); - DCHECK(new_start_index == 0 || border >= ranges->at(new_start_index - 1)); - - masm->CheckCharacterGT(border - 1, above); - Label dummy; - GenerateBranches(masm, - ranges, - start_index, - new_end_index, - min_char, - border - 1, - &dummy, - even_label, - odd_label); - if (handle_rest.is_linked()) { - masm->Bind(&handle_rest); - bool flip = (new_start_index & 1) != (start_index & 1); - GenerateBranches(masm, - ranges, - new_start_index, - end_index, - border, - max_char, - &dummy, - flip ? odd_label : even_label, - flip ? even_label : odd_label); - } -} - - -static void EmitCharClass(RegExpMacroAssembler* macro_assembler, - RegExpCharacterClass* cc, bool one_byte, - Label* on_failure, int cp_offset, bool check_offset, - bool preloaded, Zone* zone) { - ZoneList* ranges = cc->ranges(zone); - CharacterRange::Canonicalize(ranges); - - int max_char; - if (one_byte) { - max_char = String::kMaxOneByteCharCode; - } else { - max_char = String::kMaxUtf16CodeUnit; - } - - int range_count = ranges->length(); - - int last_valid_range = range_count - 1; - while (last_valid_range >= 0) { - CharacterRange& range = ranges->at(last_valid_range); - if (range.from() <= max_char) { - break; - } - last_valid_range--; - } - - if (last_valid_range < 0) { - if (!cc->is_negated()) { - macro_assembler->GoTo(on_failure); - } - if (check_offset) { - macro_assembler->CheckPosition(cp_offset, on_failure); - } - return; - } - - if (last_valid_range == 0 && - ranges->at(0).IsEverything(max_char)) { - if (cc->is_negated()) { - macro_assembler->GoTo(on_failure); - } else { - // This is a common case hit by non-anchored expressions. - if (check_offset) { - macro_assembler->CheckPosition(cp_offset, on_failure); - } - } - return; - } - - if (!preloaded) { - macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check_offset); - } - - if (cc->is_standard(zone) && - macro_assembler->CheckSpecialCharacterClass(cc->standard_type(), - on_failure)) { - return; - } - - - // A new list with ascending entries. Each entry is a code unit - // where there is a boundary between code units that are part of - // the class and code units that are not. Normally we insert an - // entry at zero which goes to the failure label, but if there - // was already one there we fall through for success on that entry. - // Subsequent entries have alternating meaning (success/failure). - ZoneList* range_boundaries = - new(zone) ZoneList(last_valid_range, zone); - - bool zeroth_entry_is_failure = !cc->is_negated(); - - for (int i = 0; i <= last_valid_range; i++) { - CharacterRange& range = ranges->at(i); - if (range.from() == 0) { - DCHECK_EQ(i, 0); - zeroth_entry_is_failure = !zeroth_entry_is_failure; - } else { - range_boundaries->Add(range.from(), zone); - } - range_boundaries->Add(range.to() + 1, zone); - } - int end_index = range_boundaries->length() - 1; - if (range_boundaries->at(end_index) > max_char) { - end_index--; - } - - Label fall_through; - GenerateBranches(macro_assembler, - range_boundaries, - 0, // start_index. - end_index, - 0, // min_char. - max_char, - &fall_through, - zeroth_entry_is_failure ? &fall_through : on_failure, - zeroth_entry_is_failure ? on_failure : &fall_through); - macro_assembler->Bind(&fall_through); -} - -RegExpNode::~RegExpNode() = default; - -RegExpNode::LimitResult RegExpNode::LimitVersions(RegExpCompiler* compiler, - Trace* trace) { - // If we are generating a greedy loop then don't stop and don't reuse code. - if (trace->stop_node() != nullptr) { - return CONTINUE; - } - - RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); - if (trace->is_trivial()) { - if (label_.is_bound() || on_work_list() || !KeepRecursing(compiler)) { - // If a generic version is already scheduled to be generated or we have - // recursed too deeply then just generate a jump to that code. - macro_assembler->GoTo(&label_); - // This will queue it up for generation of a generic version if it hasn't - // already been queued. - compiler->AddWork(this); - return DONE; - } - // Generate generic version of the node and bind the label for later use. - macro_assembler->Bind(&label_); - return CONTINUE; - } - - // We are being asked to make a non-generic version. Keep track of how many - // non-generic versions we generate so as not to overdo it. - trace_count_++; - if (KeepRecursing(compiler) && compiler->optimize() && - trace_count_ < kMaxCopiesCodeGenerated) { - return CONTINUE; - } - - // If we get here code has been generated for this node too many times or - // recursion is too deep. Time to switch to a generic version. The code for - // generic versions above can handle deep recursion properly. - bool was_limiting = compiler->limiting_recursion(); - compiler->set_limiting_recursion(true); - trace->Flush(compiler, this); - compiler->set_limiting_recursion(was_limiting); - return DONE; -} - - -bool RegExpNode::KeepRecursing(RegExpCompiler* compiler) { - return !compiler->limiting_recursion() && - compiler->recursion_depth() <= RegExpCompiler::kMaxRecursion; -} - - -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); -} - - -void ActionNode::FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) { - if (action_type_ != POSITIVE_SUBMATCH_SUCCESS) { - on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); - } - SaveBMInfo(bm, not_at_start, offset); -} - - -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); -} - - -void AssertionNode::FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) { - // Match the behaviour of EatsAtLeast on this node. - if (assertion_type() == AT_START && not_at_start) return; - on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); - SaveBMInfo(bm, not_at_start, offset); -} - - -int BackReferenceNode::EatsAtLeast(int still_to_find, - int budget, - bool not_at_start) { - if (read_backward()) return 0; - if (budget <= 0) return 0; - return on_success()->EatsAtLeast(still_to_find, - budget - 1, - not_at_start); -} - - -int TextNode::EatsAtLeast(int still_to_find, - int budget, - bool not_at_start) { - if (read_backward()) return 0; - 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 NegativeLookaroundChoiceNode::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_->at(1).node(); - return node->EatsAtLeast(still_to_find, budget - 1, not_at_start); -} - - -void NegativeLookaroundChoiceNode::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_->at(1).node(); - return node->GetQuickCheckDetails(details, compiler, filled_in, not_at_start); -} - - -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; - int choice_count = alternatives_->length(); - budget = (budget - 1) / choice_count; - for (int i = 0; i < choice_count; i++) { - RegExpNode* node = alternatives_->at(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 LoopChoiceNode::EatsAtLeast(int still_to_find, - int budget, - bool not_at_start) { - return EatsAtLeastHelper(still_to_find, - budget - 1, - loop_node_, - not_at_start); -} - - -int ChoiceNode::EatsAtLeast(int still_to_find, - int budget, - bool not_at_start) { - return EatsAtLeastHelper(still_to_find, budget, nullptr, not_at_start); -} - - -// Takes the left-most 1-bit and smears it out, setting all bits to its right. -static inline uint32_t SmearBitsRight(uint32_t v) { - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - return v; -} - - -bool QuickCheckDetails::Rationalize(bool asc) { - bool found_useful_op = false; - uint32_t char_mask; - if (asc) { - char_mask = String::kMaxOneByteCharCode; - } else { - char_mask = String::kMaxUtf16CodeUnit; - } - mask_ = 0; - value_ = 0; - int char_shift = 0; - for (int i = 0; i < characters_; i++) { - Position* pos = &positions_[i]; - if ((pos->mask & String::kMaxOneByteCharCode) != 0) { - found_useful_op = true; - } - mask_ |= (pos->mask & char_mask) << char_shift; - value_ |= (pos->value & char_mask) << char_shift; - char_shift += asc ? 8 : 16; - } - return found_useful_op; -} - - -bool RegExpNode::EmitQuickCheck(RegExpCompiler* compiler, - Trace* bounds_check_trace, - Trace* trace, - bool preload_has_checked_bounds, - Label* on_possible_success, - QuickCheckDetails* details, - bool fall_through_on_failure) { - if (details->characters() == 0) return false; - GetQuickCheckDetails( - details, compiler, 0, trace->at_start() == Trace::FALSE_VALUE); - if (details->cannot_match()) return false; - if (!details->Rationalize(compiler->one_byte())) return false; - DCHECK(details->characters() == 1 || - compiler->macro_assembler()->CanReadUnaligned()); - uint32_t mask = details->mask(); - uint32_t value = details->value(); - - RegExpMacroAssembler* assembler = compiler->macro_assembler(); - - if (trace->characters_preloaded() != details->characters()) { - DCHECK(trace->cp_offset() == bounds_check_trace->cp_offset()); - // We are attempting to preload the minimum number of characters - // any choice would eat, so if the bounds check fails, then none of the - // choices can succeed, so we can just immediately backtrack, rather - // than go to the next choice. - assembler->LoadCurrentCharacter(trace->cp_offset(), - bounds_check_trace->backtrack(), - !preload_has_checked_bounds, - details->characters()); - } - - - bool need_mask = true; - - if (details->characters() == 1) { - // If number of characters preloaded is 1 then we used a byte or 16 bit - // load so the value is already masked down. - uint32_t char_mask; - if (compiler->one_byte()) { - char_mask = String::kMaxOneByteCharCode; - } else { - char_mask = String::kMaxUtf16CodeUnit; - } - if ((mask & char_mask) == char_mask) need_mask = false; - mask &= char_mask; - } else { - // For 2-character preloads in one-byte mode or 1-character preloads in - // two-byte mode we also use a 16 bit load with zero extend. - static const uint32_t kTwoByteMask = 0xFFFF; - static const uint32_t kFourByteMask = 0xFFFFFFFF; - if (details->characters() == 2 && compiler->one_byte()) { - if ((mask & kTwoByteMask) == kTwoByteMask) need_mask = false; - } else if (details->characters() == 1 && !compiler->one_byte()) { - if ((mask & kTwoByteMask) == kTwoByteMask) need_mask = false; - } else { - if (mask == kFourByteMask) need_mask = false; - } - } - - if (fall_through_on_failure) { - if (need_mask) { - assembler->CheckCharacterAfterAnd(value, mask, on_possible_success); - } else { - assembler->CheckCharacter(value, on_possible_success); - } - } else { - if (need_mask) { - assembler->CheckNotCharacterAfterAnd(value, mask, trace->backtrack()); - } else { - assembler->CheckNotCharacter(value, trace->backtrack()); - } - } - return true; -} - - -// Here is the meat of GetQuickCheckDetails (see also the comment on the -// super-class in the .h file). -// -// We iterate along the text object, building up for each character a -// mask and value that can be used to test for a quick failure to match. -// The masks and values for the positions will be combined into a single -// machine word for the current character width in order to be used in -// generating a quick check. -void TextNode::GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, - int characters_filled_in, - bool not_at_start) { - // Do not collect any quick check details if the text node reads backward, - // since it reads in the opposite direction than we use for quick checks. - if (read_backward()) return; - Isolate* isolate = compiler->macro_assembler()->isolate(); - DCHECK(characters_filled_in < details->characters()); - int characters = details->characters(); - int char_mask; - if (compiler->one_byte()) { - char_mask = String::kMaxOneByteCharCode; - } else { - char_mask = String::kMaxUtf16CodeUnit; - } - for (int k = 0; k < elements()->length(); k++) { - TextElement elm = elements()->at(k); - if (elm.text_type() == TextElement::ATOM) { - Vector quarks = elm.atom()->data(); - for (int i = 0; i < characters && i < quarks.length(); i++) { - QuickCheckDetails::Position* pos = - details->positions(characters_filled_in); - uc16 c = quarks[i]; - if (elm.atom()->ignore_case()) { - unibrow::uchar chars[4]; - int length = GetCaseIndependentLetters( - isolate, c, compiler->one_byte(), chars, 4); - if (length == 0) { - // This can happen because all case variants are non-Latin1, but we - // know the input is Latin1. - details->set_cannot_match(); - pos->determines_perfectly = false; - return; - } - if (length == 1) { - // This letter has no case equivalents, so it's nice and simple - // and the mask-compare will determine definitely whether we have - // a match at this character position. - pos->mask = char_mask; - pos->value = c; - pos->determines_perfectly = true; - } else { - uint32_t common_bits = char_mask; - uint32_t bits = chars[0]; - for (int j = 1; j < length; j++) { - uint32_t differing_bits = ((chars[j] & common_bits) ^ bits); - common_bits ^= differing_bits; - bits &= common_bits; - } - // If length is 2 and common bits has only one zero in it then - // our mask and compare instruction will determine definitely - // whether we have a match at this character position. Otherwise - // it can only be an approximate check. - uint32_t one_zero = (common_bits | ~char_mask); - if (length == 2 && ((~one_zero) & ((~one_zero) - 1)) == 0) { - pos->determines_perfectly = true; - } - pos->mask = common_bits; - pos->value = bits; - } - } else { - // Don't ignore case. Nice simple case where the mask-compare will - // determine definitely whether we have a match at this character - // position. - if (c > char_mask) { - details->set_cannot_match(); - pos->determines_perfectly = false; - return; - } - pos->mask = char_mask; - pos->value = c; - pos->determines_perfectly = true; - } - characters_filled_in++; - DCHECK(characters_filled_in <= details->characters()); - if (characters_filled_in == details->characters()) { - return; - } - } - } else { - QuickCheckDetails::Position* pos = - details->positions(characters_filled_in); - RegExpCharacterClass* tree = elm.char_class(); - ZoneList* ranges = tree->ranges(zone()); - DCHECK(!ranges->is_empty()); - if (tree->is_negated()) { - // A quick check uses multi-character mask and compare. There is no - // useful way to incorporate a negative char class into this scheme - // so we just conservatively create a mask and value that will always - // succeed. - pos->mask = 0; - pos->value = 0; - } else { - int first_range = 0; - while (ranges->at(first_range).from() > char_mask) { - first_range++; - if (first_range == ranges->length()) { - details->set_cannot_match(); - pos->determines_perfectly = false; - return; - } - } - CharacterRange range = ranges->at(first_range); - uc16 from = range.from(); - uc16 to = range.to(); - if (to > char_mask) { - to = char_mask; - } - uint32_t differing_bits = (from ^ to); - // A mask and compare is only perfect if the differing bits form a - // number like 00011111 with one single block of trailing 1s. - if ((differing_bits & (differing_bits + 1)) == 0 && - from + differing_bits == to) { - pos->determines_perfectly = true; - } - uint32_t common_bits = ~SmearBitsRight(differing_bits); - uint32_t bits = (from & common_bits); - for (int i = first_range + 1; i < ranges->length(); i++) { - CharacterRange range = ranges->at(i); - uc16 from = range.from(); - uc16 to = range.to(); - if (from > char_mask) continue; - if (to > char_mask) to = char_mask; - // Here we are combining more ranges into the mask and compare - // value. With each new range the mask becomes more sparse and - // so the chances of a false positive rise. A character class - // with multiple ranges is assumed never to be equivalent to a - // mask and compare operation. - pos->determines_perfectly = false; - uint32_t new_common_bits = (from ^ to); - new_common_bits = ~SmearBitsRight(new_common_bits); - common_bits &= new_common_bits; - bits &= new_common_bits; - uint32_t differing_bits = (from & common_bits) ^ bits; - common_bits ^= differing_bits; - bits &= common_bits; - } - pos->mask = common_bits; - pos->value = bits; - } - characters_filled_in++; - DCHECK(characters_filled_in <= details->characters()); - if (characters_filled_in == details->characters()) { - return; - } - } - } - DCHECK(characters_filled_in != details->characters()); - if (!details->cannot_match()) { - on_success()-> GetQuickCheckDetails(details, - compiler, - characters_filled_in, - true); - } -} - - -void QuickCheckDetails::Clear() { - for (int i = 0; i < characters_; i++) { - positions_[i].mask = 0; - positions_[i].value = 0; - positions_[i].determines_perfectly = false; - } - characters_ = 0; -} - - -void QuickCheckDetails::Advance(int by, bool one_byte) { - if (by >= characters_ || by < 0) { - DCHECK_IMPLIES(by < 0, characters_ == 0); - Clear(); - return; - } - DCHECK_LE(characters_ - by, 4); - DCHECK_LE(characters_, 4); - for (int i = 0; i < characters_ - by; i++) { - positions_[i] = positions_[by + i]; - } - for (int i = characters_ - by; i < characters_; i++) { - positions_[i].mask = 0; - positions_[i].value = 0; - positions_[i].determines_perfectly = false; - } - characters_ -= by; - // We could change mask_ and value_ here but we would never advance unless - // they had already been used in a check and they won't be used again because - // it would gain us nothing. So there's no point. -} - - -void QuickCheckDetails::Merge(QuickCheckDetails* other, int from_index) { - DCHECK(characters_ == other->characters_); - if (other->cannot_match_) { - return; - } - if (cannot_match_) { - *this = *other; - return; - } - for (int i = from_index; i < characters_; i++) { - QuickCheckDetails::Position* pos = positions(i); - QuickCheckDetails::Position* other_pos = other->positions(i); - if (pos->mask != other_pos->mask || - pos->value != other_pos->value || - !other_pos->determines_perfectly) { - // Our mask-compare operation will be approximate unless we have the - // exact same operation on both sides of the alternation. - pos->determines_perfectly = false; - } - pos->mask &= other_pos->mask; - pos->value &= pos->mask; - other_pos->value &= pos->mask; - uc16 differing_bits = (pos->value ^ other_pos->value); - pos->mask &= ~differing_bits; - pos->value &= pos->mask; - } -} - - -class VisitMarker { - public: - explicit VisitMarker(NodeInfo* info) : info_(info) { - DCHECK(!info->visited); - info->visited = true; - } - ~VisitMarker() { - info_->visited = false; - } - private: - NodeInfo* info_; -}; - -RegExpNode* SeqRegExpNode::FilterOneByte(int depth) { - if (info()->replacement_calculated) return replacement(); - if (depth < 0) return this; - DCHECK(!info()->visited); - VisitMarker marker(info()); - return FilterSuccessor(depth - 1); -} - -RegExpNode* SeqRegExpNode::FilterSuccessor(int depth) { - RegExpNode* next = on_success_->FilterOneByte(depth - 1); - if (next == nullptr) return set_replacement(nullptr); - on_success_ = next; - return set_replacement(this); -} - -// 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(0x039C) || range.Contains(0x03BC) || - range.Contains(0x0178); -} - - -static bool RangesContainLatin1Equivalents(ZoneList* ranges) { - for (int i = 0; i < ranges->length(); i++) { - // TODO(dcarney): this could be a lot more efficient. - if (RangeContainsLatin1Equivalents(ranges->at(i))) return true; - } - return false; -} - -RegExpNode* TextNode::FilterOneByte(int depth) { - if (info()->replacement_calculated) return replacement(); - if (depth < 0) return this; - DCHECK(!info()->visited); - VisitMarker marker(info()); - int element_count = elements()->length(); - for (int i = 0; i < element_count; i++) { - TextElement elm = elements()->at(i); - if (elm.text_type() == TextElement::ATOM) { - Vector quarks = elm.atom()->data(); - for (int j = 0; j < quarks.length(); j++) { - uint16_t c = quarks[j]; - if (elm.atom()->ignore_case()) { - c = unibrow::Latin1::TryConvertToLatin1(c); - } - if (c > unibrow::Latin1::kMaxChar) return set_replacement(nullptr); - // Replace quark in case we converted to Latin-1. - uint16_t* writable_quarks = const_cast(quarks.begin()); - writable_quarks[j] = c; - } - } else { - DCHECK(elm.text_type() == TextElement::CHAR_CLASS); - RegExpCharacterClass* cc = elm.char_class(); - ZoneList* ranges = cc->ranges(zone()); - 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->at(0).from() == 0 && - ranges->at(0).to() >= String::kMaxOneByteCharCode) { - // This will be handled in a later filter. - if (IgnoreCase(cc->flags()) && RangesContainLatin1Equivalents(ranges)) - continue; - return set_replacement(nullptr); - } - } else { - if (range_count == 0 || - ranges->at(0).from() > String::kMaxOneByteCharCode) { - // This will be handled in a later filter. - if (IgnoreCase(cc->flags()) && RangesContainLatin1Equivalents(ranges)) - continue; - return set_replacement(nullptr); - } - } - } - } - return FilterSuccessor(depth - 1); -} - -RegExpNode* LoopChoiceNode::FilterOneByte(int depth) { - if (info()->replacement_calculated) return replacement(); - if (depth < 0) return this; - if (info()->visited) return this; - { - VisitMarker marker(info()); - - RegExpNode* continue_replacement = continue_node_->FilterOneByte(depth - 1); - // 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::FilterOneByte(depth - 1); -} - -RegExpNode* ChoiceNode::FilterOneByte(int depth) { - 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++) { - GuardedAlternative alternative = alternatives_->at(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_->at(i); - RegExpNode* replacement = alternative.node()->FilterOneByte(depth - 1); - DCHECK(replacement != this); // No missing EMPTY_MATCH_CHECK. - if (replacement != nullptr) { - alternatives_->at(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. - ZoneList* new_alternatives = - new(zone()) ZoneList(surviving, zone()); - for (int i = 0; i < choice_count; i++) { - RegExpNode* replacement = - alternatives_->at(i).node()->FilterOneByte(depth - 1); - if (replacement != nullptr) { - alternatives_->at(i).set_node(replacement); - new_alternatives->Add(alternatives_->at(i), zone()); - } - } - alternatives_ = new_alternatives; - return this; -} - -RegExpNode* NegativeLookaroundChoiceNode::FilterOneByte(int depth) { - 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_->at(1).node(); - RegExpNode* replacement = node->FilterOneByte(depth - 1); - if (replacement == nullptr) return set_replacement(nullptr); - alternatives_->at(1).set_node(replacement); - - RegExpNode* neg_node = alternatives_->at(0).node(); - RegExpNode* neg_replacement = neg_node->FilterOneByte(depth - 1); - // 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_->at(0).set_node(neg_replacement); - return set_replacement(this); -} - - -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); -} - - -void LoopChoiceNode::FillInBMInfo(Isolate* isolate, 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; - } - ChoiceNode::FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); - SaveBMInfo(bm, not_at_start, offset); -} - - -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(); - DCHECK_LT(0, choice_count); - alternatives_->at(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_->at(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); - } -} - - -// Check for [0-9A-Z_a-z]. -static void EmitWordCheck(RegExpMacroAssembler* assembler, - Label* word, - 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 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(); - - 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->one_byte()) { - assembler->CheckCharacterAfterAnd(0x2028, 0xFFFE, &ok); - } - assembler->CheckCharacter('\n', &ok); - assembler->CheckNotCharacter('\r', new_trace.backtrack()); - } - assembler->Bind(&ok); - on_success->Emit(compiler, &new_trace); -} - - -// 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(); - Isolate* isolate = assembler->isolate(); - 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 = - new(zone()) BoyerMooreLookahead(eats_at_least, compiler, zone()); - FillInBMInfo(isolate, 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) { - Label before_non_word; - 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); - Label ok; - BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord); - assembler->GoTo(&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 { - DCHECK(next_is_word_character == Trace::FALSE_VALUE); - BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord); - } -} - - -void AssertionNode::BacktrackIfPrevious( - RegExpCompiler* compiler, - Trace* trace, - AssertionNode::IfPrevious backtrack_if_previous) { - RegExpMacroAssembler* assembler = compiler->macro_assembler(); - Trace new_trace(*trace); - new_trace.InvalidateCurrentCharacter(); - - Label fall_through, dummy; - - Label* non_word = backtrack_if_previous == kIsNonWord ? - new_trace.backtrack() : - &fall_through; - Label* word = backtrack_if_previous == kIsNonWord ? - &fall_through : - new_trace.backtrack(); - - if (new_trace.cp_offset() == 0) { - // The start of input counts as a non-word character, so the question is - // decided if we are at the start. - assembler->CheckAtStart(non_word); - } - // 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, &dummy, false); - EmitWordCheck(assembler, word, non_word, backtrack_if_previous == kIsNonWord); - - assembler->Bind(&fall_through); - on_success()->Emit(compiler, &new_trace); -} - - -void AssertionNode::GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, - int filled_in, - bool not_at_start) { - if (assertion_type_ == AT_START && not_at_start) { - details->set_cannot_match(); - return; - } - return on_success()->GetQuickCheckDetails(details, - compiler, - filled_in, - not_at_start); -} - - -void AssertionNode::Emit(RegExpCompiler* compiler, Trace* trace) { - RegExpMacroAssembler* assembler = compiler->macro_assembler(); - switch (assertion_type_) { - case AT_END: { - Label ok; - assembler->CheckPosition(trace->cp_offset(), &ok); - assembler->GoTo(trace->backtrack()); - assembler->Bind(&ok); - break; - } - case AT_START: { - if (trace->at_start() == Trace::FALSE_VALUE) { - assembler->GoTo(trace->backtrack()); - return; - } - if (trace->at_start() == Trace::UNKNOWN) { - assembler->CheckNotAtStart(trace->cp_offset(), trace->backtrack()); - Trace at_start_trace = *trace; - at_start_trace.set_at_start(Trace::TRUE_VALUE); - on_success()->Emit(compiler, &at_start_trace); - return; - } - } - break; - case AFTER_NEWLINE: - EmitHat(compiler, on_success(), trace); - return; - case AT_BOUNDARY: - case AT_NON_BOUNDARY: { - EmitBoundaryCheck(compiler, trace); - return; - } - } - on_success()->Emit(compiler, trace); -} - - -static bool DeterminedAlready(QuickCheckDetails* quick_check, int offset) { - if (quick_check == nullptr) return false; - if (offset >= quick_check->characters()) return false; - return quick_check->positions(offset)->determines_perfectly; -} - - -static void UpdateBoundsCheck(int index, int* checked_up_to) { - if (index > *checked_up_to) { - *checked_up_to = index; - } -} - - -// We call this repeatedly to generate code for each pass over the text node. -// The passes are in increasing order of difficulty because we hope one -// of the first passes will fail in which case we are saved the work of the -// later passes. for example for the case independent regexp /%[asdfghjkl]a/ -// we will check the '%' in the first pass, the case independent 'a' in the -// second pass and the character class in the last pass. -// -// The passes are done from right to left, so for example to test for /bar/ -// we will first test for an 'r' with offset 2, then an 'a' with offset 1 -// and then a 'b' with offset 0. This means we can avoid the end-of-input -// bounds check most of the time. In the example we only need to check for -// end-of-input when loading the putative 'r'. -// -// A slight complication involves the fact that the first character may already -// be fetched into a register by the previous node. In this case we want to -// do the test for that character first. We do this in separate passes. The -// 'preloaded' argument indicates that we are doing such a 'pass'. If such a -// pass has been performed then subsequent passes will have true in -// first_element_checked to indicate that that character does not need to be -// checked again. -// -// In addition to all this we are passed a Trace, which can -// contain an AlternativeGeneration object. In this AlternativeGeneration -// object we can see details of any quick check that was already passed in -// order to get to the code we are now generating. The quick check can involve -// loading characters, which means we do not need to recheck the bounds -// up to the limit the quick check already checked. In addition the quick -// check can have involved a mask and compare operation which may simplify -// or obviate the need for further checks at some character positions. -void TextNode::TextEmitPass(RegExpCompiler* compiler, - TextEmitPassType pass, - bool preloaded, - Trace* trace, - bool first_element_checked, - int* checked_up_to) { - RegExpMacroAssembler* assembler = compiler->macro_assembler(); - Isolate* isolate = assembler->isolate(); - bool one_byte = compiler->one_byte(); - Label* backtrack = trace->backtrack(); - QuickCheckDetails* quick_check = trace->quick_check_performed(); - int element_count = elements()->length(); - int backward_offset = read_backward() ? -Length() : 0; - for (int i = preloaded ? 0 : element_count - 1; i >= 0; i--) { - TextElement elm = elements()->at(i); - int cp_offset = trace->cp_offset() + elm.cp_offset() + backward_offset; - if (elm.text_type() == TextElement::ATOM) { - if (SkipPass(pass, elm.atom()->ignore_case())) continue; - Vector quarks = elm.atom()->data(); - for (int j = preloaded ? 0 : quarks.length() - 1; j >= 0; j--) { - if (first_element_checked && i == 0 && j == 0) continue; - if (DeterminedAlready(quick_check, elm.cp_offset() + j)) continue; - EmitCharacterFunction* emit_function = nullptr; - uc16 quark = quarks[j]; - if (elm.atom()->ignore_case()) { - // Everywhere else we assume that a non-Latin-1 character cannot match - // a Latin-1 character. Avoid the cases where this is assumption is - // invalid by using the Latin1 equivalent instead. - quark = unibrow::Latin1::TryConvertToLatin1(quark); - } - switch (pass) { - case NON_LATIN1_MATCH: - DCHECK(one_byte); - if (quark > String::kMaxOneByteCharCode) { - assembler->GoTo(backtrack); - return; - } - break; - case NON_LETTER_CHARACTER_MATCH: - emit_function = &EmitAtomNonLetter; - break; - case SIMPLE_CHARACTER_MATCH: - emit_function = &EmitSimpleCharacter; - break; - case CASE_CHARACTER_MATCH: - emit_function = &EmitAtomLetter; - break; - default: - break; - } - if (emit_function != nullptr) { - bool bounds_check = *checked_up_to < cp_offset + j || read_backward(); - bool bound_checked = - emit_function(isolate, compiler, quark, backtrack, cp_offset + j, - bounds_check, preloaded); - if (bound_checked) UpdateBoundsCheck(cp_offset + j, checked_up_to); - } - } - } else { - DCHECK_EQ(TextElement::CHAR_CLASS, elm.text_type()); - if (pass == CHARACTER_CLASS_MATCH) { - if (first_element_checked && i == 0) continue; - if (DeterminedAlready(quick_check, elm.cp_offset())) continue; - RegExpCharacterClass* cc = elm.char_class(); - bool bounds_check = *checked_up_to < cp_offset || read_backward(); - EmitCharClass(assembler, cc, one_byte, backtrack, cp_offset, - bounds_check, preloaded, zone()); - UpdateBoundsCheck(cp_offset, checked_up_to); - } - } - } -} - - -int TextNode::Length() { - TextElement elm = elements()->last(); - DCHECK_LE(0, elm.cp_offset()); - return elm.cp_offset() + elm.length(); -} - -bool TextNode::SkipPass(TextEmitPassType pass, bool ignore_case) { - if (ignore_case) { - return pass == SIMPLE_CHARACTER_MATCH; - } else { - return pass == NON_LETTER_CHARACTER_MATCH || pass == CASE_CHARACTER_MATCH; - } -} - -TextNode* TextNode::CreateForCharacterRanges(Zone* zone, - ZoneList* ranges, - bool read_backward, - RegExpNode* on_success, - JSRegExp::Flags flags) { - DCHECK_NOT_NULL(ranges); - ZoneList* elms = new (zone) ZoneList(1, zone); - elms->Add(TextElement::CharClass( - new (zone) RegExpCharacterClass(zone, ranges, flags)), - zone); - return new (zone) TextNode(elms, read_backward, on_success); -} - -TextNode* TextNode::CreateForSurrogatePair(Zone* zone, CharacterRange lead, - CharacterRange trail, - bool read_backward, - RegExpNode* on_success, - JSRegExp::Flags flags) { - ZoneList* lead_ranges = CharacterRange::List(zone, lead); - ZoneList* trail_ranges = CharacterRange::List(zone, trail); - ZoneList* elms = new (zone) ZoneList(2, zone); - elms->Add(TextElement::CharClass( - new (zone) RegExpCharacterClass(zone, lead_ranges, flags)), - zone); - elms->Add(TextElement::CharClass( - new (zone) RegExpCharacterClass(zone, trail_ranges, flags)), - zone); - return new (zone) TextNode(elms, read_backward, on_success); -} - - -// This generates the code to match a text node. A text node can contain -// straight character sequences (possibly to be matched in a case-independent -// way) and character classes. For efficiency we do not do this in a single -// pass from left to right. Instead we pass over the text node several times, -// emitting code for some character positions every time. See the comment on -// TextEmitPass for details. -void TextNode::Emit(RegExpCompiler* compiler, Trace* trace) { - LimitResult limit_result = LimitVersions(compiler, trace); - if (limit_result == DONE) return; - DCHECK(limit_result == CONTINUE); - - if (trace->cp_offset() + Length() > RegExpMacroAssembler::kMaxCPOffset) { - compiler->SetRegExpTooBig(); - return; - } - - if (compiler->one_byte()) { - int dummy = 0; - TextEmitPass(compiler, NON_LATIN1_MATCH, false, trace, false, &dummy); - } - - bool first_elt_done = false; - int bound_checked_to = trace->cp_offset() - 1; - bound_checked_to += trace->bound_checked_up_to(); - - // If a character is preloaded into the current character register then - // check that now. - if (trace->characters_preloaded() == 1) { - for (int pass = kFirstRealPass; pass <= kLastPass; pass++) { - TextEmitPass(compiler, static_cast(pass), true, trace, - false, &bound_checked_to); - } - first_elt_done = true; - } - - for (int pass = kFirstRealPass; pass <= kLastPass; pass++) { - TextEmitPass(compiler, static_cast(pass), false, trace, - first_elt_done, &bound_checked_to); - } - - Trace successor_trace(*trace); - // If we advance backward, we may end up at the start. - successor_trace.AdvanceCurrentPositionInTrace( - read_backward() ? -Length() : Length(), compiler); - successor_trace.set_at_start(read_backward() ? Trace::UNKNOWN - : Trace::FALSE_VALUE); - RecursionCheck rc(compiler); - on_success()->Emit(compiler, &successor_trace); -} - - -void Trace::InvalidateCurrentCharacter() { - characters_preloaded_ = 0; -} - - -void Trace::AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler) { - // 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->one_byte()); - 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 TextNode::MakeCaseIndependent(Isolate* isolate, bool is_one_byte) { - int element_count = elements()->length(); - for (int i = 0; i < element_count; i++) { - TextElement elm = elements()->at(i); - if (elm.text_type() == TextElement::CHAR_CLASS) { - RegExpCharacterClass* cc = elm.char_class(); -#ifdef V8_INTL_SUPPORT - bool case_equivalents_already_added = - NeedsUnicodeCaseEquivalents(cc->flags()); -#else - bool case_equivalents_already_added = false; -#endif - if (IgnoreCase(cc->flags()) && !case_equivalents_already_added) { - // 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(zone())) continue; - ZoneList* ranges = cc->ranges(zone()); - CharacterRange::AddCaseEquivalents(isolate, zone(), ranges, - is_one_byte); - } - } - } -} - - -int TextNode::GreedyLoopTextLength() { return Length(); } - - -RegExpNode* TextNode::GetSuccessorOfOmnivorousTextNode( - RegExpCompiler* compiler) { - if (read_backward()) return nullptr; - if (elements()->length() != 1) return nullptr; - TextElement elm = elements()->at(0); - if (elm.text_type() != TextElement::CHAR_CLASS) return nullptr; - RegExpCharacterClass* node = elm.char_class(); - ZoneList* ranges = node->ranges(zone()); - CharacterRange::Canonicalize(ranges); - if (node->is_negated()) { - return ranges->length() == 0 ? on_success() : nullptr; - } - if (ranges->length() != 1) return nullptr; - uint32_t max_char; - if (compiler->one_byte()) { - max_char = String::kMaxOneByteCharCode; - } else { - max_char = String::kMaxUtf16CodeUnit; - } - return ranges->at(0).IsEverything(max_char) ? on_success() : nullptr; -} - - -// Finds the fixed match length of a sequence of nodes that goes from -// this alternative and back to this choice node. If there are variable -// length nodes or other complications in the way then return a sentinel -// value indicating that a greedy loop cannot be constructed. -int ChoiceNode::GreedyLoopTextLengthForAlternative( - GuardedAlternative* alternative) { - int length = 0; - RegExpNode* node = alternative->node(); - // Later we will generate code for all these text nodes using recursion - // so we have to limit the max number. - int recursion_depth = 0; - while (node != this) { - if (recursion_depth++ > RegExpCompiler::kMaxRecursion) { - return kNodeIsTooComplexForGreedyLoops; - } - int node_length = node->GreedyLoopTextLength(); - if (node_length == kNodeIsTooComplexForGreedyLoops) { - return kNodeIsTooComplexForGreedyLoops; - } - length += node_length; - SeqRegExpNode* seq_node = static_cast(node); - node = seq_node->on_success(); - } - return read_backward() ? -length : length; -} - - -void LoopChoiceNode::AddLoopAlternative(GuardedAlternative alt) { - DCHECK_NULL(loop_node_); - AddAlternative(alt); - loop_node_ = alt.node(); -} - - -void LoopChoiceNode::AddContinueAlternative(GuardedAlternative alt) { - DCHECK_NULL(continue_node_); - AddAlternative(alt); - continue_node_ = alt.node(); -} - - -void LoopChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) { - RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); - if (trace->stop_node() == this) { - // Back edge of greedy optimized loop node graph. - int text_length = - GreedyLoopTextLengthForAlternative(&(alternatives_->at(0))); - DCHECK_NE(kNodeIsTooComplexForGreedyLoops, text_length); - // Update the counter-based backtracking info on the stack. This is an - // optimization for greedy loops (see below). - DCHECK(trace->cp_offset() == text_length); - macro_assembler->AdvanceCurrentPosition(text_length); - macro_assembler->GoTo(trace->loop_label()); - return; - } - DCHECK_NULL(trace->stop_node()); - if (!trace->is_trivial()) { - trace->Flush(compiler, this); - return; - } - ChoiceNode::Emit(compiler, trace); -} - - -int ChoiceNode::CalculatePreloadCharacters(RegExpCompiler* compiler, - int eats_at_least) { - int preload_characters = Min(4, eats_at_least); - DCHECK_LE(preload_characters, 4); - if (compiler->macro_assembler()->CanReadUnaligned()) { - bool one_byte = compiler->one_byte(); - if (one_byte) { - // We can't preload 3 characters because there is no machine instruction - // to do that. We can't just load 4 because we could be reading - // beyond the end of the string, which could cause a memory fault. - if (preload_characters == 3) preload_characters = 2; - } else { - if (preload_characters > 2) preload_characters = 2; - } - } else { - if (preload_characters > 1) preload_characters = 1; - } - return preload_characters; -} - - -// This class is used when generating the alternatives in a choice node. It -// records the way the alternative is being code generated. -class AlternativeGeneration: public Malloced { - public: - AlternativeGeneration() - : possible_success(), - expects_preload(false), - after(), - quick_check_details() { } - Label possible_success; - bool expects_preload; - Label after; - QuickCheckDetails quick_check_details; -}; - - -// Creates a list of AlternativeGenerations. If the list has a reasonable -// size then it is on the stack, otherwise the excess is on the heap. -class AlternativeGenerationList { - public: - AlternativeGenerationList(int count, Zone* zone) - : alt_gens_(count, zone) { - for (int i = 0; i < count && i < kAFew; i++) { - alt_gens_.Add(a_few_alt_gens_ + i, zone); - } - for (int i = kAFew; i < count; i++) { - alt_gens_.Add(new AlternativeGeneration(), zone); - } - } - ~AlternativeGenerationList() { - for (int i = kAFew; i < alt_gens_.length(); i++) { - delete alt_gens_[i]; - alt_gens_[i] = nullptr; - } - } - - AlternativeGeneration* at(int i) { - return alt_gens_[i]; - } - - private: - static const int kAFew = 10; - ZoneList alt_gens_; - AlternativeGeneration a_few_alt_gens_[kAFew]; -}; - - -static const uc32 kRangeEndMarker = 0x110000; - -// The '2' variant is 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, 0x2000, 0x200B, 0x2028, 0x202A, 0x202F, 0x2030, - 0x205F, 0x2060, 0x3000, 0x3001, 0xFEFF, 0xFF00, kRangeEndMarker}; -static const int kSpaceRangeCount = arraysize(kSpaceRanges); - -static const int kWordRanges[] = { - '0', '9' + 1, 'A', 'Z' + 1, '_', '_' + 1, 'a', 'z' + 1, kRangeEndMarker}; -static const int kWordRangeCount = arraysize(kWordRanges); -static const int kDigitRanges[] = {'0', '9' + 1, kRangeEndMarker}; -static const int kDigitRangeCount = arraysize(kDigitRanges); -static const int kSurrogateRanges[] = { - kLeadSurrogateStart, kLeadSurrogateStart + 1, kRangeEndMarker}; -static const int kSurrogateRangeCount = arraysize(kSurrogateRanges); -static const int kLineTerminatorRanges[] = { - 0x000A, 0x000B, 0x000D, 0x000E, 0x2028, 0x202A, kRangeEndMarker}; -static const int kLineTerminatorRangeCount = arraysize(kLineTerminatorRanges); - -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_->at(i) = true; - } - return; - } - for (int i = interval.from(); i <= interval.to(); i++) { - int mod_character = (i & kMask); - if (!map_->at(mod_character)) { - map_count_++; - map_->at(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_->at(i) = true; - } -} - - -BoyerMooreLookahead::BoyerMooreLookahead( - int length, RegExpCompiler* compiler, Zone* zone) - : length_(length), - compiler_(compiler) { - if (compiler->one_byte()) { - max_char_ = String::kMaxOneByteCharCode; - } else { - max_char_ = String::kMaxUtf16CodeUnit; - } - bitmaps_ = new(zone) ZoneList(length, zone); - for (int i = 0; i < length; i++) { - bitmaps_->Add(new(zone) BoyerMoorePositionInfo(zone), zone); - } -} - - -// 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_->at(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_->one_byte() ? 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, - Handle 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->set(i, kSkipArrayEntry); - } - int skip = max_lookahead + 1 - min_lookahead; - - for (int i = max_lookahead; i >= min_lookahead; i--) { - BoyerMoorePositionInfo* map = bitmaps_->at(i); - for (int j = 0; j < kSize; j++) { - if (map->at(j)) { - boolean_skip_table->set(j, kDontSkipArrayEntry); - } - } - } - - return skip; -} - - -// See comment above on the implementation of GetSkipTable. -void BoyerMooreLookahead::EmitSkipInstructions(RegExpMacroAssembler* masm) { - const int kSize = RegExpMacroAssembler::kTableSize; - - int min_lookahead = 0; - int max_lookahead = 0; - - if (!FindWorthwhileInterval(&min_lookahead, &max_lookahead)) return; - - bool found_single_character = false; - int single_character = 0; - for (int i = max_lookahead; i >= min_lookahead; i--) { - BoyerMoorePositionInfo* map = bitmaps_->at(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; - } - - if (found_single_character) { - 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->GoTo(&again); - masm->Bind(&cont); - return; - } - - Factory* factory = masm->isolate()->factory(); - Handle boolean_skip_table = - factory->NewByteArray(kSize, AllocationType::kOld); - int skip_distance = GetSkipTable( - min_lookahead, max_lookahead, boolean_skip_table); - DCHECK_NE(0, skip_distance); - - Label cont, again; - masm->Bind(&again); - masm->LoadCurrentCharacter(max_lookahead, &cont, true); - masm->CheckBitInTable(boolean_skip_table, &cont); - masm->AdvanceCurrentPosition(skip_distance); - masm->GoTo(&again); - masm->Bind(&cont); -} - - -/* Code generation for choice nodes. - * - * We generate quick checks that do a mask and compare to eliminate a - * choice. If the quick check succeeds then it jumps to the continuation to - * do slow checks and check subsequent nodes. If it fails (the common case) - * it falls through to the next choice. - * - * Here is the desired flow graph. Nodes directly below each other imply - * fallthrough. Alternatives 1 and 2 have quick checks. Alternative - * 3 doesn't have a quick check so we have to call the slow check. - * Nodes are marked Qn for quick checks and Sn for slow checks. The entire - * regexp continuation is generated directly after the Sn node, up to the - * next GoTo if we decide to reuse some already generated code. Some - * nodes expect preload_characters to be preloaded into the current - * character register. R nodes do this preloading. Vertices are marked - * F for failures and S for success (possible success in the case of quick - * nodes). L, V, < and > are used as arrow heads. - * - * ----------> R - * | - * V - * Q1 -----> S1 - * | S / - * F| / - * | F/ - * | / - * | R - * | / - * V L - * Q2 -----> S2 - * | S / - * F| / - * | F/ - * | / - * | R - * | / - * V L - * S3 - * | - * F| - * | - * R - * | - * backtrack V - * <----------Q4 - * \ F | - * \ |S - * \ F V - * \-----S4 - * - * For greedy loops we push the current position, then generate the code that - * eats the input specially in EmitGreedyLoop. The other choice (the - * continuation) is generated by the normal code in EmitChoices, and steps back - * in the input to the starting position when it fails to match. The loop code - * looks like this (U is the unwind code that steps back in the greedy loop). - * - * _____ - * / \ - * V | - * ----------> S1 | - * /| | - * / |S | - * F/ \_____/ - * / - * |<----- - * | \ - * V |S - * Q2 ---> U----->backtrack - * | F / - * S| / - * V F / - * S2--/ - */ - -GreedyLoopState::GreedyLoopState(bool not_at_start) { - counter_backtrack_trace_.set_backtrack(&label_); - if (not_at_start) counter_backtrack_trace_.set_at_start(Trace::FALSE_VALUE); -} - - -void ChoiceNode::AssertGuardsMentionRegisters(Trace* trace) { -#ifdef DEBUG - int choice_count = alternatives_->length(); - for (int i = 0; i < choice_count - 1; i++) { - GuardedAlternative alternative = alternatives_->at(i); - ZoneList* guards = alternative.guards(); - int guard_count = (guards == nullptr) ? 0 : guards->length(); - for (int j = 0; j < guard_count; j++) { - DCHECK(!trace->mentions_reg(guards->at(j)->reg())); - } - } -#endif -} - - -void ChoiceNode::SetUpPreLoad(RegExpCompiler* compiler, - Trace* current_trace, - PreloadState* state) { - if (state->eats_at_least_ == PreloadState::kEatsAtLeastNotYetInitialized) { - // Save some time by looking at most one machine word ahead. - state->eats_at_least_ = - EatsAtLeast(compiler->one_byte() ? 4 : 2, kRecursionBudget, - current_trace->at_start() == Trace::FALSE_VALUE); - } - state->preload_characters_ = - CalculatePreloadCharacters(compiler, state->eats_at_least_); - - state->preload_is_current_ = - (current_trace->characters_preloaded() == state->preload_characters_); - state->preload_has_checked_bounds_ = state->preload_is_current_; -} - - -void ChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) { - int choice_count = alternatives_->length(); - - if (choice_count == 1 && alternatives_->at(0).guards() == nullptr) { - alternatives_->at(0).node()->Emit(compiler, trace); - return; - } - - AssertGuardsMentionRegisters(trace); - - LimitResult limit_result = LimitVersions(compiler, trace); - if (limit_result == DONE) return; - DCHECK(limit_result == CONTINUE); - - // For loop nodes we already flushed (see LoopChoiceNode::Emit), but for - // other choice nodes we only flush if we are out of code size budget. - if (trace->flush_budget() == 0 && trace->actions() != nullptr) { - trace->Flush(compiler, this); - return; - } - - RecursionCheck rc(compiler); - - PreloadState preload; - preload.init(); - GreedyLoopState greedy_loop_state(not_at_start()); - - int text_length = GreedyLoopTextLengthForAlternative(&alternatives_->at(0)); - AlternativeGenerationList alt_gens(choice_count, zone()); - - if (choice_count > 1 && text_length != kNodeIsTooComplexForGreedyLoops) { - trace = EmitGreedyLoop(compiler, - trace, - &alt_gens, - &preload, - &greedy_loop_state, - text_length); - } else { - // TODO(erikcorry): Delete this. We don't need this label, but it makes us - // match the traces produced pre-cleanup. - Label second_choice; - compiler->macro_assembler()->Bind(&second_choice); - - preload.eats_at_least_ = EmitOptimizedUnanchoredSearch(compiler, trace); - - EmitChoices(compiler, - &alt_gens, - 0, - trace, - &preload); - } - - // At this point we need to generate slow checks for the alternatives where - // the quick check was inlined. We can recognize these because the associated - // label was bound. - int new_flush_budget = trace->flush_budget() / choice_count; - for (int i = 0; i < choice_count; i++) { - AlternativeGeneration* alt_gen = alt_gens.at(i); - Trace new_trace(*trace); - // If there are actions to be flushed we have to limit how many times - // they are flushed. Take the budget of the parent trace and distribute - // it fairly amongst the children. - if (new_trace.actions() != nullptr) { - new_trace.set_flush_budget(new_flush_budget); - } - bool next_expects_preload = - i == choice_count - 1 ? false : alt_gens.at(i + 1)->expects_preload; - EmitOutOfLineContinuation(compiler, - &new_trace, - alternatives_->at(i), - alt_gen, - preload.preload_characters_, - next_expects_preload); - } -} - - -Trace* ChoiceNode::EmitGreedyLoop(RegExpCompiler* compiler, - Trace* trace, - AlternativeGenerationList* alt_gens, - PreloadState* preload, - GreedyLoopState* greedy_loop_state, - int text_length) { - RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); - // Here we have special handling for greedy loops containing only text nodes - // and other simple nodes. These are handled by pushing the current - // position on the stack and then incrementing the current position each - // time around the switch. On backtrack we decrement the current position - // and check it against the pushed value. This avoids pushing backtrack - // information for each iteration of the loop, which could take up a lot of - // space. - DCHECK(trace->stop_node() == nullptr); - macro_assembler->PushCurrentPosition(); - Label greedy_match_failed; - Trace greedy_match_trace; - if (not_at_start()) greedy_match_trace.set_at_start(Trace::FALSE_VALUE); - greedy_match_trace.set_backtrack(&greedy_match_failed); - Label loop_label; - macro_assembler->Bind(&loop_label); - greedy_match_trace.set_stop_node(this); - greedy_match_trace.set_loop_label(&loop_label); - alternatives_->at(0).node()->Emit(compiler, &greedy_match_trace); - macro_assembler->Bind(&greedy_match_failed); - - Label second_choice; // For use in greedy matches. - macro_assembler->Bind(&second_choice); - - Trace* new_trace = greedy_loop_state->counter_backtrack_trace(); - - EmitChoices(compiler, - alt_gens, - 1, - new_trace, - preload); - - macro_assembler->Bind(greedy_loop_state->label()); - // If we have unwound to the bottom then backtrack. - macro_assembler->CheckGreedyLoop(trace->backtrack()); - // Otherwise try the second priority at an earlier position. - macro_assembler->AdvanceCurrentPosition(-text_length); - macro_assembler->GoTo(&second_choice); - return new_trace; -} - -int ChoiceNode::EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler, - Trace* trace) { - int eats_at_least = PreloadState::kEatsAtLeastNotYetInitialized; - if (alternatives_->length() != 2) return eats_at_least; - - GuardedAlternative alt1 = alternatives_->at(1); - if (alt1.guards() != nullptr && alt1.guards()->length() != 0) { - return eats_at_least; - } - RegExpNode* eats_anything_node = alt1.node(); - if (eats_anything_node->GetSuccessorOfOmnivorousTextNode(compiler) != this) { - return eats_at_least; - } - - // Really we should be creating a new trace when we execute this function, - // but there is no need, because the code it generates cannot backtrack, and - // we always arrive here with a trivial trace (since it's the entry to a - // loop. That also implies that there are no preloaded characters, which is - // good, because it means we won't be violating any assumptions by - // overwriting those characters with new load instructions. - DCHECK(trace->is_trivial()); - - RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); - Isolate* isolate = macro_assembler->isolate(); - // At this point we know that we are at a non-greedy loop that will eat - // any character one at a time. Any non-anchored regexp has such a - // loop prepended to it in order to find where it starts. We look for - // a pattern of the form ...abc... where we can look 6 characters ahead - // and step forwards 3 if the character is not one of abc. Abc need - // not be atoms, they can be any reasonably limited character class or - // small alternation. - BoyerMooreLookahead* bm = bm_info(false); - if (bm == nullptr) { - eats_at_least = Min(kMaxLookaheadForBoyerMoore, - EatsAtLeast(kMaxLookaheadForBoyerMoore, - kRecursionBudget, - false)); - if (eats_at_least >= 1) { - bm = new(zone()) BoyerMooreLookahead(eats_at_least, - compiler, - zone()); - GuardedAlternative alt0 = alternatives_->at(0); - alt0.node()->FillInBMInfo(isolate, 0, kRecursionBudget, bm, false); - } - } - if (bm != nullptr) { - bm->EmitSkipInstructions(macro_assembler); - } - return eats_at_least; -} - - -void ChoiceNode::EmitChoices(RegExpCompiler* compiler, - AlternativeGenerationList* alt_gens, - int first_choice, - Trace* trace, - PreloadState* preload) { - RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); - SetUpPreLoad(compiler, trace, preload); - - // For now we just call all choices one after the other. The idea ultimately - // is to use the Dispatch table to try only the relevant ones. - int choice_count = alternatives_->length(); - - int new_flush_budget = trace->flush_budget() / choice_count; - - for (int i = first_choice; i < choice_count; i++) { - bool is_last = i == choice_count - 1; - bool fall_through_on_failure = !is_last; - GuardedAlternative alternative = alternatives_->at(i); - AlternativeGeneration* alt_gen = alt_gens->at(i); - alt_gen->quick_check_details.set_characters(preload->preload_characters_); - ZoneList* guards = alternative.guards(); - int guard_count = (guards == nullptr) ? 0 : guards->length(); - Trace new_trace(*trace); - new_trace.set_characters_preloaded(preload->preload_is_current_ ? - preload->preload_characters_ : - 0); - if (preload->preload_has_checked_bounds_) { - new_trace.set_bound_checked_up_to(preload->preload_characters_); - } - new_trace.quick_check_performed()->Clear(); - if (not_at_start_) new_trace.set_at_start(Trace::FALSE_VALUE); - if (!is_last) { - new_trace.set_backtrack(&alt_gen->after); - } - alt_gen->expects_preload = preload->preload_is_current_; - bool generate_full_check_inline = false; - if (compiler->optimize() && - try_to_emit_quick_check_for_alternative(i == 0) && - alternative.node()->EmitQuickCheck( - compiler, trace, &new_trace, preload->preload_has_checked_bounds_, - &alt_gen->possible_success, &alt_gen->quick_check_details, - fall_through_on_failure)) { - // Quick check was generated for this choice. - preload->preload_is_current_ = true; - preload->preload_has_checked_bounds_ = true; - // If we generated the quick check to fall through on possible success, - // we now need to generate the full check inline. - if (!fall_through_on_failure) { - macro_assembler->Bind(&alt_gen->possible_success); - new_trace.set_quick_check_performed(&alt_gen->quick_check_details); - new_trace.set_characters_preloaded(preload->preload_characters_); - new_trace.set_bound_checked_up_to(preload->preload_characters_); - generate_full_check_inline = true; - } - } else if (alt_gen->quick_check_details.cannot_match()) { - if (!fall_through_on_failure) { - macro_assembler->GoTo(trace->backtrack()); - } - continue; - } else { - // No quick check was generated. Put the full code here. - // If this is not the first choice then there could be slow checks from - // previous cases that go here when they fail. There's no reason to - // insist that they preload characters since the slow check we are about - // to generate probably can't use it. - if (i != first_choice) { - alt_gen->expects_preload = false; - new_trace.InvalidateCurrentCharacter(); - } - generate_full_check_inline = true; - } - if (generate_full_check_inline) { - if (new_trace.actions() != nullptr) { - new_trace.set_flush_budget(new_flush_budget); - } - for (int j = 0; j < guard_count; j++) { - GenerateGuard(macro_assembler, guards->at(j), &new_trace); - } - alternative.node()->Emit(compiler, &new_trace); - preload->preload_is_current_ = false; - } - macro_assembler->Bind(&alt_gen->after); - } -} - - -void ChoiceNode::EmitOutOfLineContinuation(RegExpCompiler* compiler, - Trace* trace, - GuardedAlternative alternative, - AlternativeGeneration* alt_gen, - int preload_characters, - bool next_expects_preload) { - if (!alt_gen->possible_success.is_linked()) return; - - RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); - macro_assembler->Bind(&alt_gen->possible_success); - Trace out_of_line_trace(*trace); - out_of_line_trace.set_characters_preloaded(preload_characters); - out_of_line_trace.set_quick_check_performed(&alt_gen->quick_check_details); - if (not_at_start_) out_of_line_trace.set_at_start(Trace::FALSE_VALUE); - ZoneList* guards = alternative.guards(); - int guard_count = (guards == nullptr) ? 0 : guards->length(); - if (next_expects_preload) { - Label reload_current_char; - out_of_line_trace.set_backtrack(&reload_current_char); - for (int j = 0; j < guard_count; j++) { - GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace); - } - alternative.node()->Emit(compiler, &out_of_line_trace); - macro_assembler->Bind(&reload_current_char); - // Reload the current character, since the next quick check expects that. - // We don't need to check bounds here because we only get into this - // code through a quick check which already did the checked load. - macro_assembler->LoadCurrentCharacter(trace->cp_offset(), nullptr, false, - preload_characters); - macro_assembler->GoTo(&(alt_gen->after)); - } else { - out_of_line_trace.set_backtrack(&(alt_gen->after)); - for (int j = 0; j < guard_count; j++) { - GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace); - } - alternative.node()->Emit(compiler, &out_of_line_trace); - } -} - - -void ActionNode::Emit(RegExpCompiler* compiler, Trace* trace) { - RegExpMacroAssembler* assembler = compiler->macro_assembler(); - LimitResult limit_result = LimitVersions(compiler, trace); - if (limit_result == DONE) return; - DCHECK(limit_result == CONTINUE); - - RecursionCheck rc(compiler); - - switch (action_type_) { - case STORE_POSITION: { - Trace::DeferredCapture - new_capture(data_.u_position_register.reg, - data_.u_position_register.is_capture, - trace); - Trace new_trace = *trace; - new_trace.add_action(&new_capture); - on_success()->Emit(compiler, &new_trace); - break; - } - case INCREMENT_REGISTER: { - Trace::DeferredIncrementRegister - new_increment(data_.u_increment_register.reg); - Trace new_trace = *trace; - new_trace.add_action(&new_increment); - on_success()->Emit(compiler, &new_trace); - break; - } - case SET_REGISTER: { - Trace::DeferredSetRegister - new_set(data_.u_store_register.reg, data_.u_store_register.value); - Trace new_trace = *trace; - new_trace.add_action(&new_set); - on_success()->Emit(compiler, &new_trace); - break; - } - case CLEAR_CAPTURES: { - Trace::DeferredClearCaptures - new_capture(Interval(data_.u_clear_captures.range_from, - data_.u_clear_captures.range_to)); - Trace new_trace = *trace; - new_trace.add_action(&new_capture); - on_success()->Emit(compiler, &new_trace); - break; - } - case BEGIN_SUBMATCH: - if (!trace->is_trivial()) { - trace->Flush(compiler, this); - } else { - assembler->WriteCurrentPositionToRegister( - data_.u_submatch.current_position_register, 0); - assembler->WriteStackPointerToRegister( - data_.u_submatch.stack_pointer_register); - on_success()->Emit(compiler, trace); - } - break; - case EMPTY_MATCH_CHECK: { - int start_pos_reg = data_.u_empty_match_check.start_register; - int stored_pos = 0; - int rep_reg = data_.u_empty_match_check.repetition_register; - bool has_minimum = (rep_reg != RegExpCompiler::kNoRegister); - bool know_dist = trace->GetStoredPosition(start_pos_reg, &stored_pos); - if (know_dist && !has_minimum && stored_pos == trace->cp_offset()) { - // If we know we haven't advanced and there is no minimum we - // can just backtrack immediately. - assembler->GoTo(trace->backtrack()); - } else if (know_dist && stored_pos < trace->cp_offset()) { - // If we know we've advanced we can generate the continuation - // immediately. - on_success()->Emit(compiler, trace); - } else if (!trace->is_trivial()) { - trace->Flush(compiler, this); - } else { - Label skip_empty_check; - // If we have a minimum number of repetitions we check the current - // number first and skip the empty check if it's not enough. - if (has_minimum) { - int limit = data_.u_empty_match_check.repetition_limit; - assembler->IfRegisterLT(rep_reg, limit, &skip_empty_check); - } - // If the match is empty we bail out, otherwise we fall through - // to the on-success continuation. - assembler->IfRegisterEqPos(data_.u_empty_match_check.start_register, - trace->backtrack()); - assembler->Bind(&skip_empty_check); - on_success()->Emit(compiler, trace); - } - break; - } - case POSITIVE_SUBMATCH_SUCCESS: { - if (!trace->is_trivial()) { - trace->Flush(compiler, this); - return; - } - assembler->ReadCurrentPositionFromRegister( - data_.u_submatch.current_position_register); - assembler->ReadStackPointerFromRegister( - data_.u_submatch.stack_pointer_register); - int clear_register_count = data_.u_submatch.clear_register_count; - if (clear_register_count == 0) { - on_success()->Emit(compiler, trace); - return; - } - int clear_registers_from = data_.u_submatch.clear_register_from; - Label clear_registers_backtrack; - Trace new_trace = *trace; - new_trace.set_backtrack(&clear_registers_backtrack); - on_success()->Emit(compiler, &new_trace); - - assembler->Bind(&clear_registers_backtrack); - int clear_registers_to = clear_registers_from + clear_register_count - 1; - assembler->ClearRegisters(clear_registers_from, clear_registers_to); - - DCHECK(trace->backtrack() == nullptr); - assembler->Backtrack(); - return; - } - default: - UNREACHABLE(); - } -} - - -void BackReferenceNode::Emit(RegExpCompiler* compiler, Trace* trace) { - RegExpMacroAssembler* assembler = compiler->macro_assembler(); - if (!trace->is_trivial()) { - trace->Flush(compiler, this); - return; - } - - LimitResult limit_result = LimitVersions(compiler, trace); - if (limit_result == DONE) return; - DCHECK(limit_result == CONTINUE); - - RecursionCheck rc(compiler); - - DCHECK_EQ(start_reg_ + 1, end_reg_); - if (IgnoreCase(flags_)) { - assembler->CheckNotBackReferenceIgnoreCase( - start_reg_, read_backward(), IsUnicode(flags_), trace->backtrack()); - } else { - assembler->CheckNotBackReference(start_reg_, read_backward(), - trace->backtrack()); - } - // We are going to advance backward, so we may end up at the start. - if (read_backward()) trace->set_at_start(Trace::UNKNOWN); - - // Check that the back reference does not end inside a surrogate pair. - if (IsUnicode(flags_) && !compiler->one_byte()) { - assembler->CheckNotInSurrogatePair(trace->cp_offset(), trace->backtrack()); - } - on_success()->Emit(compiler, trace); -} - - -// ------------------------------------------------------------------- -// Dot/dotty output - - -#ifdef DEBUG - - -class DotPrinter: public NodeVisitor { - public: - DotPrinter(std::ostream& os, bool ignore_case) // NOLINT - : os_(os), - ignore_case_(ignore_case) {} - void PrintNode(const char* label, RegExpNode* node); - void Visit(RegExpNode* node); - void PrintAttributes(RegExpNode* from); - void PrintOnFailure(RegExpNode* from, RegExpNode* to); -#define DECLARE_VISIT(Type) \ - virtual void Visit##Type(Type##Node* that); -FOR_EACH_NODE_TYPE(DECLARE_VISIT) -#undef DECLARE_VISIT - private: - std::ostream& os_; - bool ignore_case_; -}; - - -void DotPrinter::PrintNode(const char* label, RegExpNode* node) { - os_ << "digraph G {\n graph [label=\""; - for (int i = 0; label[i]; i++) { - switch (label[i]) { - case '\\': - os_ << "\\\\"; - break; - case '"': - os_ << "\""; - break; - default: - os_ << label[i]; - break; - } - } - os_ << "\"];\n"; - Visit(node); - os_ << "}" << std::endl; -} - - -void DotPrinter::Visit(RegExpNode* node) { - if (node->info()->visited) return; - node->info()->visited = true; - node->Accept(this); -} - - -void DotPrinter::PrintOnFailure(RegExpNode* from, RegExpNode* on_failure) { - os_ << " n" << from << " -> n" << on_failure << " [style=dotted];\n"; - Visit(on_failure); -} - - -class TableEntryBodyPrinter { - public: - TableEntryBodyPrinter(std::ostream& os, ChoiceNode* choice) // NOLINT - : os_(os), - choice_(choice) {} - void Call(uc16 from, DispatchTable::Entry entry) { - OutSet* out_set = entry.out_set(); - for (unsigned i = 0; i < OutSet::kFirstLimit; i++) { - if (out_set->Get(i)) { - os_ << " n" << choice() << ":s" << from << "o" << i << " -> n" - << choice()->alternatives()->at(i).node() << ";\n"; - } - } - } - private: - ChoiceNode* choice() { return choice_; } - std::ostream& os_; - ChoiceNode* choice_; -}; - - -class TableEntryHeaderPrinter { - public: - explicit TableEntryHeaderPrinter(std::ostream& os) // NOLINT - : first_(true), - os_(os) {} - void Call(uc16 from, DispatchTable::Entry entry) { - if (first_) { - first_ = false; - } else { - os_ << "|"; - } - os_ << "{\\" << AsUC16(from) << "-\\" << AsUC16(entry.to()) << "|{"; - OutSet* out_set = entry.out_set(); - int priority = 0; - for (unsigned i = 0; i < OutSet::kFirstLimit; i++) { - if (out_set->Get(i)) { - if (priority > 0) os_ << "|"; - os_ << " " << priority; - priority++; - } - } - os_ << "}}"; - } - - private: - bool first_; - std::ostream& os_; -}; - - -class AttributePrinter { - public: - explicit AttributePrinter(std::ostream& os) // NOLINT - : os_(os), - first_(true) {} - void PrintSeparator() { - if (first_) { - first_ = false; - } else { - os_ << "|"; - } - } - void PrintBit(const char* name, bool value) { - if (!value) return; - PrintSeparator(); - os_ << "{" << name << "}"; - } - void PrintPositive(const char* name, int value) { - if (value < 0) return; - PrintSeparator(); - os_ << "{" << name << "|" << value << "}"; - } - - private: - std::ostream& os_; - bool first_; -}; - - -void DotPrinter::PrintAttributes(RegExpNode* that) { - os_ << " a" << that << " [shape=Mrecord, color=grey, fontcolor=grey, " - << "margin=0.1, fontsize=10, label=\"{"; - AttributePrinter printer(os_); - NodeInfo* info = that->info(); - printer.PrintBit("NI", info->follows_newline_interest); - printer.PrintBit("WI", info->follows_word_interest); - printer.PrintBit("SI", info->follows_start_interest); - Label* label = that->label(); - if (label->is_bound()) - printer.PrintPositive("@", label->pos()); - os_ << "}\"];\n" - << " a" << that << " -> n" << that - << " [style=dashed, color=grey, arrowhead=none];\n"; -} - - -static const bool kPrintDispatchTable = false; -void DotPrinter::VisitChoice(ChoiceNode* that) { - if (kPrintDispatchTable) { - os_ << " n" << that << " [shape=Mrecord, label=\""; - TableEntryHeaderPrinter header_printer(os_); - that->GetTable(ignore_case_)->ForEach(&header_printer); - os_ << "\"]\n"; - PrintAttributes(that); - TableEntryBodyPrinter body_printer(os_, that); - that->GetTable(ignore_case_)->ForEach(&body_printer); - } else { - os_ << " n" << that << " [shape=Mrecord, label=\"?\"];\n"; - for (int i = 0; i < that->alternatives()->length(); i++) { - GuardedAlternative alt = that->alternatives()->at(i); - os_ << " n" << that << " -> n" << alt.node(); - } - } - for (int i = 0; i < that->alternatives()->length(); i++) { - GuardedAlternative alt = that->alternatives()->at(i); - alt.node()->Accept(this); - } -} - - -void DotPrinter::VisitText(TextNode* that) { - Zone* zone = that->zone(); - os_ << " n" << that << " [label=\""; - for (int i = 0; i < that->elements()->length(); i++) { - if (i > 0) os_ << " "; - TextElement elm = that->elements()->at(i); - switch (elm.text_type()) { - case TextElement::ATOM: { - Vector data = elm.atom()->data(); - for (int i = 0; i < data.length(); i++) { - os_ << static_cast(data[i]); - } - break; - } - case TextElement::CHAR_CLASS: { - RegExpCharacterClass* node = elm.char_class(); - os_ << "["; - if (node->is_negated()) os_ << "^"; - for (int j = 0; j < node->ranges(zone)->length(); j++) { - CharacterRange range = node->ranges(zone)->at(j); - os_ << AsUC16(range.from()) << "-" << AsUC16(range.to()); - } - os_ << "]"; - break; - } - default: - UNREACHABLE(); - } - } - os_ << "\", shape=box, peripheries=2];\n"; - PrintAttributes(that); - os_ << " n" << that << " -> n" << that->on_success() << ";\n"; - Visit(that->on_success()); -} - - -void DotPrinter::VisitBackReference(BackReferenceNode* that) { - os_ << " n" << that << " [label=\"$" << that->start_register() << "..$" - << that->end_register() << "\", shape=doubleoctagon];\n"; - PrintAttributes(that); - os_ << " n" << that << " -> n" << that->on_success() << ";\n"; - Visit(that->on_success()); -} - - -void DotPrinter::VisitEnd(EndNode* that) { - os_ << " n" << that << " [style=bold, shape=point];\n"; - PrintAttributes(that); -} - - -void DotPrinter::VisitAssertion(AssertionNode* that) { - os_ << " n" << that << " ["; - switch (that->assertion_type()) { - case AssertionNode::AT_END: - os_ << "label=\"$\", shape=septagon"; - break; - case AssertionNode::AT_START: - os_ << "label=\"^\", shape=septagon"; - break; - case AssertionNode::AT_BOUNDARY: - os_ << "label=\"\\b\", shape=septagon"; - break; - case AssertionNode::AT_NON_BOUNDARY: - os_ << "label=\"\\B\", shape=septagon"; - break; - case AssertionNode::AFTER_NEWLINE: - os_ << "label=\"(?<=\\n)\", shape=septagon"; - break; - } - os_ << "];\n"; - PrintAttributes(that); - RegExpNode* successor = that->on_success(); - os_ << " n" << that << " -> n" << successor << ";\n"; - Visit(successor); -} - - -void DotPrinter::VisitAction(ActionNode* that) { - os_ << " n" << that << " ["; - switch (that->action_type_) { - case ActionNode::SET_REGISTER: - os_ << "label=\"$" << that->data_.u_store_register.reg - << ":=" << that->data_.u_store_register.value << "\", shape=octagon"; - break; - case ActionNode::INCREMENT_REGISTER: - os_ << "label=\"$" << that->data_.u_increment_register.reg - << "++\", shape=octagon"; - break; - case ActionNode::STORE_POSITION: - os_ << "label=\"$" << that->data_.u_position_register.reg - << ":=$pos\", shape=octagon"; - break; - case ActionNode::BEGIN_SUBMATCH: - os_ << "label=\"$" << that->data_.u_submatch.current_position_register - << ":=$pos,begin\", shape=septagon"; - break; - case ActionNode::POSITIVE_SUBMATCH_SUCCESS: - os_ << "label=\"escape\", shape=septagon"; - break; - case ActionNode::EMPTY_MATCH_CHECK: - os_ << "label=\"$" << that->data_.u_empty_match_check.start_register - << "=$pos?,$" << that->data_.u_empty_match_check.repetition_register - << "<" << that->data_.u_empty_match_check.repetition_limit - << "?\", shape=septagon"; - break; - case ActionNode::CLEAR_CAPTURES: { - os_ << "label=\"clear $" << that->data_.u_clear_captures.range_from - << " to $" << that->data_.u_clear_captures.range_to - << "\", shape=septagon"; - break; - } - } - os_ << "];\n"; - PrintAttributes(that); - RegExpNode* successor = that->on_success(); - os_ << " n" << that << " -> n" << successor << ";\n"; - Visit(successor); -} - - -class DispatchTableDumper { - public: - explicit DispatchTableDumper(std::ostream& os) : os_(os) {} - void Call(uc16 key, DispatchTable::Entry entry); - private: - std::ostream& os_; -}; - - -void DispatchTableDumper::Call(uc16 key, DispatchTable::Entry entry) { - os_ << "[" << AsUC16(key) << "-" << AsUC16(entry.to()) << "]: {"; - OutSet* set = entry.out_set(); - bool first = true; - for (unsigned i = 0; i < OutSet::kFirstLimit; i++) { - if (set->Get(i)) { - if (first) { - first = false; - } else { - os_ << ", "; - } - os_ << i; - } - } - os_ << "}\n"; -} - - -void DispatchTable::Dump() { - OFStream os(stderr); - DispatchTableDumper dumper(os); - tree()->ForEach(&dumper); -} - - -void RegExpEngine::DotPrint(const char* label, - RegExpNode* node, - bool ignore_case) { - StdoutStream os; - DotPrinter printer(os, ignore_case); - printer.PrintNode(label, node); -} - - -#endif // DEBUG - - -// ------------------------------------------------------------------- -// Tree to graph conversion - -RegExpNode* RegExpAtom::ToNode(RegExpCompiler* compiler, - RegExpNode* on_success) { - ZoneList* elms = - new(compiler->zone()) ZoneList(1, compiler->zone()); - elms->Add(TextElement::Atom(this), compiler->zone()); - return new (compiler->zone()) - TextNode(elms, compiler->read_backward(), on_success); -} - - -RegExpNode* RegExpText::ToNode(RegExpCompiler* compiler, - RegExpNode* on_success) { - return new (compiler->zone()) - TextNode(elements(), compiler->read_backward(), on_success); -} - - -static bool CompareInverseRanges(ZoneList* ranges, - const int* special_class, - int length) { - length--; // Remove final marker. - DCHECK_EQ(kRangeEndMarker, special_class[length]); - DCHECK_NE(0, ranges->length()); - DCHECK_NE(0, length); - DCHECK_NE(0, special_class[0]); - if (ranges->length() != (length >> 1) + 1) { - return false; - } - CharacterRange range = ranges->at(0); - if (range.from() != 0) { - return false; - } - for (int i = 0; i < length; i += 2) { - if (special_class[i] != (range.to() + 1)) { - return false; - } - range = ranges->at((i >> 1) + 1); - if (special_class[i+1] != range.from()) { - return false; - } - } - if (range.to() != String::kMaxCodePoint) { - return false; - } - return true; -} - - -static bool CompareRanges(ZoneList* ranges, - const int* special_class, - int length) { - length--; // Remove final marker. - DCHECK_EQ(kRangeEndMarker, special_class[length]); - if (ranges->length() * 2 != length) { - return false; - } - for (int i = 0; i < length; i += 2) { - CharacterRange range = ranges->at(i >> 1); - if (range.from() != special_class[i] || - range.to() != special_class[i + 1] - 1) { - return false; - } - } - return true; -} - - -bool RegExpCharacterClass::is_standard(Zone* zone) { - // 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(zone), kSpaceRanges, kSpaceRangeCount)) { - set_.set_standard_set_type('s'); - return true; - } - if (CompareInverseRanges(set_.ranges(zone), kSpaceRanges, kSpaceRangeCount)) { - set_.set_standard_set_type('S'); - return true; - } - if (CompareInverseRanges(set_.ranges(zone), - kLineTerminatorRanges, - kLineTerminatorRangeCount)) { - set_.set_standard_set_type('.'); - return true; - } - if (CompareRanges(set_.ranges(zone), - kLineTerminatorRanges, - kLineTerminatorRangeCount)) { - set_.set_standard_set_type('n'); - return true; - } - if (CompareRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) { - set_.set_standard_set_type('w'); - return true; - } - if (CompareInverseRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) { - set_.set_standard_set_type('W'); - return true; - } - return false; -} - - -UnicodeRangeSplitter::UnicodeRangeSplitter(Zone* zone, - ZoneList* base) - : zone_(zone), - table_(zone), - bmp_(nullptr), - lead_surrogates_(nullptr), - trail_surrogates_(nullptr), - non_bmp_(nullptr) { - // The unicode range splitter categorizes given character ranges into: - // - Code points from the BMP representable by one code unit. - // - Code points outside the BMP that need to be split into surrogate pairs. - // - Lone lead surrogates. - // - Lone trail surrogates. - // Lone surrogates are valid code points, even though no actual characters. - // They require special matching to make sure we do not split surrogate pairs. - // We use the dispatch table to accomplish this. The base range is split up - // by the table by the overlay ranges, and the Call callback is used to - // filter and collect ranges for each category. - for (int i = 0; i < base->length(); i++) { - table_.AddRange(base->at(i), kBase, zone_); - } - // Add overlay ranges. - table_.AddRange(CharacterRange::Range(0, kLeadSurrogateStart - 1), - kBmpCodePoints, zone_); - table_.AddRange(CharacterRange::Range(kLeadSurrogateStart, kLeadSurrogateEnd), - kLeadSurrogates, zone_); - table_.AddRange( - CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd), - kTrailSurrogates, zone_); - table_.AddRange( - CharacterRange::Range(kTrailSurrogateEnd + 1, kNonBmpStart - 1), - kBmpCodePoints, zone_); - table_.AddRange(CharacterRange::Range(kNonBmpStart, kNonBmpEnd), - kNonBmpCodePoints, zone_); - table_.ForEach(this); -} - - -void UnicodeRangeSplitter::Call(uc32 from, DispatchTable::Entry entry) { - OutSet* outset = entry.out_set(); - if (!outset->Get(kBase)) return; - ZoneList** target = nullptr; - if (outset->Get(kBmpCodePoints)) { - target = &bmp_; - } else if (outset->Get(kLeadSurrogates)) { - target = &lead_surrogates_; - } else if (outset->Get(kTrailSurrogates)) { - target = &trail_surrogates_; - } else { - DCHECK(outset->Get(kNonBmpCodePoints)); - target = &non_bmp_; - } - if (*target == nullptr) - *target = new (zone_) ZoneList(2, zone_); - (*target)->Add(CharacterRange::Range(entry.from(), entry.to()), zone_); -} - -void AddBmpCharacters(RegExpCompiler* compiler, ChoiceNode* result, - RegExpNode* on_success, UnicodeRangeSplitter* splitter) { - ZoneList* bmp = splitter->bmp(); - if (bmp == nullptr) return; - JSRegExp::Flags default_flags = JSRegExp::Flags(); - result->AddAlternative(GuardedAlternative(TextNode::CreateForCharacterRanges( - compiler->zone(), bmp, compiler->read_backward(), on_success, - default_flags))); -} - -void AddNonBmpSurrogatePairs(RegExpCompiler* compiler, ChoiceNode* result, - RegExpNode* on_success, - UnicodeRangeSplitter* splitter) { - ZoneList* non_bmp = splitter->non_bmp(); - if (non_bmp == nullptr) return; - DCHECK(!compiler->one_byte()); - Zone* zone = compiler->zone(); - JSRegExp::Flags default_flags = JSRegExp::Flags(); - CharacterRange::Canonicalize(non_bmp); - for (int i = 0; i < non_bmp->length(); i++) { - // Match surrogate pair. - // E.g. [\u10005-\u11005] becomes - // \ud800[\udc05-\udfff]| - // [\ud801-\ud803][\udc00-\udfff]| - // \ud804[\udc00-\udc05] - uc32 from = non_bmp->at(i).from(); - uc32 to = non_bmp->at(i).to(); - uc16 from_l = unibrow::Utf16::LeadSurrogate(from); - uc16 from_t = unibrow::Utf16::TrailSurrogate(from); - uc16 to_l = unibrow::Utf16::LeadSurrogate(to); - uc16 to_t = unibrow::Utf16::TrailSurrogate(to); - if (from_l == to_l) { - // The lead surrogate is the same. - result->AddAlternative( - GuardedAlternative(TextNode::CreateForSurrogatePair( - zone, CharacterRange::Singleton(from_l), - CharacterRange::Range(from_t, to_t), compiler->read_backward(), - on_success, default_flags))); - } else { - if (from_t != kTrailSurrogateStart) { - // Add [from_l][from_t-\udfff] - result->AddAlternative( - GuardedAlternative(TextNode::CreateForSurrogatePair( - zone, CharacterRange::Singleton(from_l), - CharacterRange::Range(from_t, kTrailSurrogateEnd), - compiler->read_backward(), on_success, default_flags))); - from_l++; - } - if (to_t != kTrailSurrogateEnd) { - // Add [to_l][\udc00-to_t] - result->AddAlternative( - GuardedAlternative(TextNode::CreateForSurrogatePair( - zone, CharacterRange::Singleton(to_l), - CharacterRange::Range(kTrailSurrogateStart, to_t), - compiler->read_backward(), on_success, default_flags))); - to_l--; - } - if (from_l <= to_l) { - // Add [from_l-to_l][\udc00-\udfff] - result->AddAlternative( - GuardedAlternative(TextNode::CreateForSurrogatePair( - zone, CharacterRange::Range(from_l, to_l), - CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd), - compiler->read_backward(), on_success, default_flags))); - } - } - } -} - -RegExpNode* NegativeLookaroundAgainstReadDirectionAndMatch( - RegExpCompiler* compiler, ZoneList* lookbehind, - ZoneList* match, RegExpNode* on_success, bool read_backward, - JSRegExp::Flags flags) { - Zone* zone = compiler->zone(); - RegExpNode* match_node = TextNode::CreateForCharacterRanges( - zone, match, read_backward, on_success, flags); - int stack_register = compiler->UnicodeLookaroundStackRegister(); - int position_register = compiler->UnicodeLookaroundPositionRegister(); - RegExpLookaround::Builder lookaround(false, match_node, stack_register, - position_register); - RegExpNode* negative_match = TextNode::CreateForCharacterRanges( - zone, lookbehind, !read_backward, lookaround.on_match_success(), flags); - return lookaround.ForMatch(negative_match); -} - -RegExpNode* MatchAndNegativeLookaroundInReadDirection( - RegExpCompiler* compiler, ZoneList* match, - ZoneList* lookahead, RegExpNode* on_success, - bool read_backward, JSRegExp::Flags flags) { - Zone* zone = compiler->zone(); - int stack_register = compiler->UnicodeLookaroundStackRegister(); - int position_register = compiler->UnicodeLookaroundPositionRegister(); - RegExpLookaround::Builder lookaround(false, on_success, stack_register, - position_register); - RegExpNode* negative_match = TextNode::CreateForCharacterRanges( - zone, lookahead, read_backward, lookaround.on_match_success(), flags); - return TextNode::CreateForCharacterRanges( - zone, match, read_backward, lookaround.ForMatch(negative_match), flags); -} - -void AddLoneLeadSurrogates(RegExpCompiler* compiler, ChoiceNode* result, - RegExpNode* on_success, - UnicodeRangeSplitter* splitter) { - JSRegExp::Flags default_flags = JSRegExp::Flags(); - ZoneList* lead_surrogates = splitter->lead_surrogates(); - if (lead_surrogates == nullptr) return; - Zone* zone = compiler->zone(); - // E.g. \ud801 becomes \ud801(?![\udc00-\udfff]). - ZoneList* trail_surrogates = CharacterRange::List( - zone, CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd)); - - RegExpNode* match; - if (compiler->read_backward()) { - // Reading backward. Assert that reading forward, there is no trail - // surrogate, and then backward match the lead surrogate. - match = NegativeLookaroundAgainstReadDirectionAndMatch( - compiler, trail_surrogates, lead_surrogates, on_success, true, - default_flags); - } else { - // Reading forward. Forward match the lead surrogate and assert that - // no trail surrogate follows. - match = MatchAndNegativeLookaroundInReadDirection( - compiler, lead_surrogates, trail_surrogates, on_success, false, - default_flags); - } - result->AddAlternative(GuardedAlternative(match)); -} - -void AddLoneTrailSurrogates(RegExpCompiler* compiler, ChoiceNode* result, - RegExpNode* on_success, - UnicodeRangeSplitter* splitter) { - JSRegExp::Flags default_flags = JSRegExp::Flags(); - ZoneList* trail_surrogates = splitter->trail_surrogates(); - if (trail_surrogates == nullptr) return; - Zone* zone = compiler->zone(); - // E.g. \udc01 becomes (?* lead_surrogates = CharacterRange::List( - zone, CharacterRange::Range(kLeadSurrogateStart, kLeadSurrogateEnd)); - - RegExpNode* match; - if (compiler->read_backward()) { - // Reading backward. Backward match the trail surrogate and assert that no - // lead surrogate precedes it. - match = MatchAndNegativeLookaroundInReadDirection( - compiler, trail_surrogates, lead_surrogates, on_success, true, - default_flags); - } else { - // Reading forward. Assert that reading backward, there is no lead - // surrogate, and then forward match the trail surrogate. - match = NegativeLookaroundAgainstReadDirectionAndMatch( - compiler, lead_surrogates, trail_surrogates, on_success, false, - default_flags); - } - result->AddAlternative(GuardedAlternative(match)); -} - -RegExpNode* UnanchoredAdvance(RegExpCompiler* compiler, - RegExpNode* on_success) { - // This implements ES2015 21.2.5.2.3, AdvanceStringIndex. - DCHECK(!compiler->read_backward()); - Zone* zone = compiler->zone(); - // Advance any character. If the character happens to be a lead surrogate and - // we advanced into the middle of a surrogate pair, it will work out, as - // nothing will match from there. We will have to advance again, consuming - // the associated trail surrogate. - ZoneList* range = CharacterRange::List( - zone, CharacterRange::Range(0, String::kMaxUtf16CodeUnit)); - JSRegExp::Flags default_flags = JSRegExp::Flags(); - return TextNode::CreateForCharacterRanges(zone, range, false, on_success, - default_flags); -} - -void AddUnicodeCaseEquivalents(ZoneList* ranges, Zone* zone) { -#ifdef V8_INTL_SUPPORT - DCHECK(CharacterRange::IsCanonical(ranges)); - - // Micro-optimization to avoid passing large ranges to UnicodeSet::closeOver. - // See also https://crbug.com/v8/6727. - // TODO(jgruber): This only covers the special case of the {0,0x10FFFF} range, - // which we use frequently internally. But large ranges can also easily be - // created by the user. We might want to have a more general caching mechanism - // for such ranges. - if (ranges->length() == 1 && ranges->at(0).IsEverything(kNonBmpEnd)) return; - - // Use ICU to compute the case fold closure over the ranges. - icu::UnicodeSet set; - for (int i = 0; i < ranges->length(); i++) { - set.add(ranges->at(i).from(), ranges->at(i).to()); - } - ranges->Clear(); - set.closeOver(USET_CASE_INSENSITIVE); - // Full case mapping map single characters to multiple characters. - // Those are represented as strings in the set. Remove them so that - // we end up with only simple and common case mappings. - set.removeAllStrings(); - for (int i = 0; i < set.getRangeCount(); i++) { - ranges->Add(CharacterRange::Range(set.getRangeStart(i), set.getRangeEnd(i)), - zone); - } - // No errors and everything we collected have been ranges. - CharacterRange::Canonicalize(ranges); -#endif // V8_INTL_SUPPORT -} - - -RegExpNode* RegExpCharacterClass::ToNode(RegExpCompiler* compiler, - RegExpNode* on_success) { - set_.Canonicalize(); - Zone* zone = compiler->zone(); - ZoneList* ranges = this->ranges(zone); - if (NeedsUnicodeCaseEquivalents(flags_)) { - AddUnicodeCaseEquivalents(ranges, zone); - } - if (IsUnicode(flags_) && !compiler->one_byte() && - !contains_split_surrogate()) { - if (is_negated()) { - ZoneList* negated = - new (zone) ZoneList(2, zone); - CharacterRange::Negate(ranges, negated, zone); - ranges = negated; - } - if (ranges->length() == 0) { - JSRegExp::Flags default_flags; - RegExpCharacterClass* fail = - new (zone) RegExpCharacterClass(zone, ranges, default_flags); - return new (zone) TextNode(fail, compiler->read_backward(), on_success); - } - if (standard_type() == '*') { - return UnanchoredAdvance(compiler, on_success); - } else { - ChoiceNode* result = new (zone) ChoiceNode(2, zone); - UnicodeRangeSplitter splitter(zone, ranges); - AddBmpCharacters(compiler, result, on_success, &splitter); - AddNonBmpSurrogatePairs(compiler, result, on_success, &splitter); - AddLoneLeadSurrogates(compiler, result, on_success, &splitter); - AddLoneTrailSurrogates(compiler, result, on_success, &splitter); - return result; - } - } else { - return new (zone) TextNode(this, compiler->read_backward(), on_success); - } -} - - -int CompareFirstChar(RegExpTree* const* a, RegExpTree* const* b) { - RegExpAtom* atom1 = (*a)->AsAtom(); - RegExpAtom* atom2 = (*b)->AsAtom(); - uc16 character1 = atom1->data().at(0); - uc16 character2 = atom2->data().at(0); - if (character1 < character2) return -1; - if (character1 > character2) return 1; - return 0; -} - -#ifdef V8_INTL_SUPPORT - -// Case Insensitve comparesion -int CompareFirstCharCaseInsensitve(RegExpTree* const* a, RegExpTree* const* b) { - RegExpAtom* atom1 = (*a)->AsAtom(); - RegExpAtom* atom2 = (*b)->AsAtom(); - icu::UnicodeString character1(atom1->data().at(0)); - return character1.caseCompare(atom2->data().at(0), U_FOLD_CASE_DEFAULT); -} - -#else - -static unibrow::uchar Canonical( - unibrow::Mapping* canonicalize, - unibrow::uchar c) { - unibrow::uchar chars[unibrow::Ecma262Canonicalize::kMaxWidth]; - int length = canonicalize->get(c, '\0', chars); - DCHECK_LE(length, 1); - unibrow::uchar canonical = c; - if (length == 1) canonical = chars[0]; - return canonical; -} - -int CompareFirstCharCaseIndependent( - unibrow::Mapping* canonicalize, - RegExpTree* const* a, RegExpTree* const* b) { - RegExpAtom* atom1 = (*a)->AsAtom(); - RegExpAtom* atom2 = (*b)->AsAtom(); - unibrow::uchar character1 = atom1->data().at(0); - unibrow::uchar character2 = atom2->data().at(0); - if (character1 == character2) return 0; - if (character1 >= 'a' || character2 >= 'a') { - character1 = Canonical(canonicalize, character1); - character2 = Canonical(canonicalize, character2); - } - return static_cast(character1) - static_cast(character2); -} -#endif // V8_INTL_SUPPORT - -// We can stable sort runs of atoms, since the order does not matter if they -// start with different characters. -// Returns true if any consecutive atoms were found. -bool RegExpDisjunction::SortConsecutiveAtoms(RegExpCompiler* compiler) { - ZoneList* alternatives = this->alternatives(); - int length = alternatives->length(); - bool found_consecutive_atoms = false; - for (int i = 0; i < length; i++) { - while (i < length) { - RegExpTree* alternative = alternatives->at(i); - if (alternative->IsAtom()) break; - i++; - } - // i is length or it is the index of an atom. - if (i == length) break; - int first_atom = i; - JSRegExp::Flags flags = alternatives->at(i)->AsAtom()->flags(); - i++; - while (i < length) { - RegExpTree* alternative = alternatives->at(i); - if (!alternative->IsAtom()) break; - if (alternative->AsAtom()->flags() != flags) break; - i++; - } - // Sort atoms to get ones with common prefixes together. - // This step is more tricky if we are in a case-independent regexp, - // because it would change /is|I/ to /I|is/, and order matters when - // the regexp parts don't match only disjoint starting points. To fix - // this we have a version of CompareFirstChar that uses case- - // independent character classes for comparison. - DCHECK_LT(first_atom, alternatives->length()); - DCHECK_LE(i, alternatives->length()); - DCHECK_LE(first_atom, i); - if (IgnoreCase(flags)) { -#ifdef V8_INTL_SUPPORT - alternatives->StableSort(CompareFirstCharCaseInsensitve, first_atom, - i - first_atom); -#else - unibrow::Mapping* canonicalize = - compiler->isolate()->regexp_macro_assembler_canonicalize(); - auto compare_closure = - [canonicalize](RegExpTree* const* a, RegExpTree* const* b) { - return CompareFirstCharCaseIndependent(canonicalize, a, b); - }; - alternatives->StableSort(compare_closure, first_atom, i - first_atom); -#endif // V8_INTL_SUPPORT - } else { - alternatives->StableSort(CompareFirstChar, first_atom, i - first_atom); - } - if (i - first_atom > 1) found_consecutive_atoms = true; - } - return found_consecutive_atoms; -} - - -// Optimizes ab|ac|az to a(?:b|c|d). -void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) { - Zone* zone = compiler->zone(); - ZoneList* alternatives = this->alternatives(); - int length = alternatives->length(); - - int write_posn = 0; - int i = 0; - while (i < length) { - RegExpTree* alternative = alternatives->at(i); - if (!alternative->IsAtom()) { - alternatives->at(write_posn++) = alternatives->at(i); - i++; - continue; - } - RegExpAtom* const atom = alternative->AsAtom(); - JSRegExp::Flags flags = atom->flags(); -#ifdef V8_INTL_SUPPORT - icu::UnicodeString common_prefix(atom->data().at(0)); -#else - unibrow::uchar common_prefix = atom->data().at(0); -#endif // V8_INTL_SUPPORT - int first_with_prefix = i; - int prefix_length = atom->length(); - i++; - while (i < length) { - alternative = alternatives->at(i); - if (!alternative->IsAtom()) break; - RegExpAtom* const atom = alternative->AsAtom(); - if (atom->flags() != flags) break; -#ifdef V8_INTL_SUPPORT - icu::UnicodeString new_prefix(atom->data().at(0)); - if (new_prefix != common_prefix) { - if (!IgnoreCase(flags)) break; - if (common_prefix.caseCompare(new_prefix, U_FOLD_CASE_DEFAULT) != 0) - break; - } -#else - unibrow::uchar new_prefix = atom->data().at(0); - if (new_prefix != common_prefix) { - if (!IgnoreCase(flags)) break; - unibrow::Mapping* canonicalize = - compiler->isolate()->regexp_macro_assembler_canonicalize(); - new_prefix = Canonical(canonicalize, new_prefix); - common_prefix = Canonical(canonicalize, common_prefix); - if (new_prefix != common_prefix) break; - } -#endif // V8_INTL_SUPPORT - prefix_length = Min(prefix_length, atom->length()); - i++; - } - if (i > first_with_prefix + 2) { - // Found worthwhile run of alternatives with common prefix of at least one - // character. The sorting function above did not sort on more than one - // character for reasons of correctness, but there may still be a longer - // common prefix if the terms were similar or presorted in the input. - // Find out how long the common prefix is. - int run_length = i - first_with_prefix; - RegExpAtom* const atom = alternatives->at(first_with_prefix)->AsAtom(); - for (int j = 1; j < run_length && prefix_length > 1; j++) { - RegExpAtom* old_atom = - alternatives->at(j + first_with_prefix)->AsAtom(); - for (int k = 1; k < prefix_length; k++) { - if (atom->data().at(k) != old_atom->data().at(k)) { - prefix_length = k; - break; - } - } - } - RegExpAtom* prefix = new (zone) - RegExpAtom(atom->data().SubVector(0, prefix_length), flags); - ZoneList* pair = new (zone) ZoneList(2, zone); - pair->Add(prefix, zone); - ZoneList* suffixes = - new (zone) ZoneList(run_length, zone); - for (int j = 0; j < run_length; j++) { - RegExpAtom* old_atom = - alternatives->at(j + first_with_prefix)->AsAtom(); - int len = old_atom->length(); - if (len == prefix_length) { - suffixes->Add(new (zone) RegExpEmpty(), zone); - } else { - RegExpTree* suffix = new (zone) RegExpAtom( - old_atom->data().SubVector(prefix_length, old_atom->length()), - flags); - suffixes->Add(suffix, zone); - } - } - pair->Add(new (zone) RegExpDisjunction(suffixes), zone); - alternatives->at(write_posn++) = new (zone) RegExpAlternative(pair); - } else { - // Just copy any non-worthwhile alternatives. - for (int j = first_with_prefix; j < i; j++) { - alternatives->at(write_posn++) = alternatives->at(j); - } - } - } - alternatives->Rewind(write_posn); // Trim end of array. -} - - -// Optimizes b|c|z to [bcz]. -void RegExpDisjunction::FixSingleCharacterDisjunctions( - RegExpCompiler* compiler) { - Zone* zone = compiler->zone(); - ZoneList* alternatives = this->alternatives(); - int length = alternatives->length(); - - int write_posn = 0; - int i = 0; - while (i < length) { - RegExpTree* alternative = alternatives->at(i); - if (!alternative->IsAtom()) { - alternatives->at(write_posn++) = alternatives->at(i); - i++; - continue; - } - RegExpAtom* const atom = alternative->AsAtom(); - if (atom->length() != 1) { - alternatives->at(write_posn++) = alternatives->at(i); - i++; - continue; - } - JSRegExp::Flags flags = atom->flags(); - DCHECK_IMPLIES(IsUnicode(flags), - !unibrow::Utf16::IsLeadSurrogate(atom->data().at(0))); - bool contains_trail_surrogate = - unibrow::Utf16::IsTrailSurrogate(atom->data().at(0)); - int first_in_run = i; - i++; - // Find a run of single-character atom alternatives that have identical - // flags (case independence and unicode-ness). - while (i < length) { - alternative = alternatives->at(i); - if (!alternative->IsAtom()) break; - RegExpAtom* const atom = alternative->AsAtom(); - if (atom->length() != 1) break; - if (atom->flags() != flags) break; - DCHECK_IMPLIES(IsUnicode(flags), - !unibrow::Utf16::IsLeadSurrogate(atom->data().at(0))); - contains_trail_surrogate |= - unibrow::Utf16::IsTrailSurrogate(atom->data().at(0)); - i++; - } - if (i > first_in_run + 1) { - // Found non-trivial run of single-character alternatives. - int run_length = i - first_in_run; - ZoneList* ranges = - new (zone) ZoneList(2, zone); - for (int j = 0; j < run_length; j++) { - RegExpAtom* old_atom = alternatives->at(j + first_in_run)->AsAtom(); - DCHECK_EQ(old_atom->length(), 1); - ranges->Add(CharacterRange::Singleton(old_atom->data().at(0)), zone); - } - RegExpCharacterClass::CharacterClassFlags character_class_flags; - if (IsUnicode(flags) && contains_trail_surrogate) { - character_class_flags = RegExpCharacterClass::CONTAINS_SPLIT_SURROGATE; - } - alternatives->at(write_posn++) = new (zone) - RegExpCharacterClass(zone, ranges, flags, character_class_flags); - } else { - // Just copy any trivial alternatives. - for (int j = first_in_run; j < i; j++) { - alternatives->at(write_posn++) = alternatives->at(j); - } - } - } - alternatives->Rewind(write_posn); // Trim end of array. -} - - -RegExpNode* RegExpDisjunction::ToNode(RegExpCompiler* compiler, - RegExpNode* on_success) { - ZoneList* alternatives = this->alternatives(); - - if (alternatives->length() > 2) { - bool found_consecutive_atoms = SortConsecutiveAtoms(compiler); - if (found_consecutive_atoms) RationalizeConsecutiveAtoms(compiler); - FixSingleCharacterDisjunctions(compiler); - if (alternatives->length() == 1) { - return alternatives->at(0)->ToNode(compiler, on_success); - } - } - - int length = alternatives->length(); - - ChoiceNode* result = - new(compiler->zone()) ChoiceNode(length, compiler->zone()); - for (int i = 0; i < length; i++) { - GuardedAlternative alternative(alternatives->at(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) { - DCHECK_LT(0, factor); - 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_; - - DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpExpansionLimiter); -}; - - -RegExpNode* RegExpQuantifier::ToNode(int min, - int max, - bool is_greedy, - RegExpTree* body, - RegExpCompiler* compiler, - RegExpNode* on_success, - bool not_at_start) { - // 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(); - Zone* zone = compiler->zone(); - - if (body_can_be_empty) { - body_start_reg = compiler->AllocateRegister(); - } else if (compiler->optimize() && !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) { - DCHECK_LT(0, max); // 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 = new(zone) ChoiceNode(2, zone); - 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 && !compiler->read_backward()) { - 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 = new (zone) - LoopChoiceNode(body->min_match() == 0, compiler->read_backward(), zone); - if (not_at_start && !compiler->read_backward()) center->set_not_at_start(); - RegExpNode* loop_return = needs_counter - ? static_cast(ActionNode::IncrementRegister(reg_ctr, center)) - : static_cast(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 = - new(zone) Guard(reg_ctr, Guard::LT, max); - body_alt.AddGuard(body_guard, zone); - } - GuardedAlternative rest_alt(on_success); - if (has_min) { - Guard* rest_guard = new(compiler->zone()) Guard(reg_ctr, Guard::GEQ, min); - rest_alt.AddGuard(rest_guard, zone); - } - 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); - } else { - return center; - } -} - -namespace { -// Desugar \b to (?<=\w)(?=\W)|(?<=\W)(?=\w) and -// \B to (?<=\w)(?=\w)|(?<=\W)(?=\W) -RegExpNode* BoundaryAssertionAsLookaround(RegExpCompiler* compiler, - RegExpNode* on_success, - RegExpAssertion::AssertionType type, - JSRegExp::Flags flags) { - DCHECK(NeedsUnicodeCaseEquivalents(flags)); - Zone* zone = compiler->zone(); - ZoneList* word_range = - new (zone) ZoneList(2, zone); - CharacterRange::AddClassEscape('w', word_range, true, zone); - int stack_register = compiler->UnicodeLookaroundStackRegister(); - int position_register = compiler->UnicodeLookaroundPositionRegister(); - ChoiceNode* result = new (zone) ChoiceNode(2, zone); - // Add two choices. The (non-)boundary could start with a word or - // a non-word-character. - for (int i = 0; i < 2; i++) { - bool lookbehind_for_word = i == 0; - bool lookahead_for_word = - (type == RegExpAssertion::BOUNDARY) ^ lookbehind_for_word; - // Look to the left. - RegExpLookaround::Builder lookbehind(lookbehind_for_word, on_success, - stack_register, position_register); - RegExpNode* backward = TextNode::CreateForCharacterRanges( - zone, word_range, true, lookbehind.on_match_success(), flags); - // Look to the right. - RegExpLookaround::Builder lookahead(lookahead_for_word, - lookbehind.ForMatch(backward), - stack_register, position_register); - RegExpNode* forward = TextNode::CreateForCharacterRanges( - zone, word_range, false, lookahead.on_match_success(), flags); - result->AddAlternative(GuardedAlternative(lookahead.ForMatch(forward))); - } - return result; -} -} // anonymous namespace - -RegExpNode* RegExpAssertion::ToNode(RegExpCompiler* compiler, - RegExpNode* on_success) { - NodeInfo info; - Zone* zone = compiler->zone(); - - switch (assertion_type()) { - case START_OF_LINE: - return AssertionNode::AfterNewline(on_success); - case START_OF_INPUT: - return AssertionNode::AtStart(on_success); - case BOUNDARY: - return NeedsUnicodeCaseEquivalents(flags_) - ? BoundaryAssertionAsLookaround(compiler, on_success, BOUNDARY, - flags_) - : AssertionNode::AtBoundary(on_success); - case NON_BOUNDARY: - return NeedsUnicodeCaseEquivalents(flags_) - ? BoundaryAssertionAsLookaround(compiler, on_success, - NON_BOUNDARY, flags_) - : 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 = new(zone) ChoiceNode(2, zone); - // Create a newline atom. - ZoneList* newline_ranges = - new(zone) ZoneList(3, zone); - CharacterRange::AddClassEscape('n', newline_ranges, false, zone); - JSRegExp::Flags default_flags = JSRegExp::Flags(); - RegExpCharacterClass* newline_atom = - new (zone) RegExpCharacterClass('n', default_flags); - TextNode* newline_matcher = new (zone) TextNode( - newline_atom, false, 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: - UNREACHABLE(); - } - return on_success; -} - - -RegExpNode* RegExpBackReference::ToNode(RegExpCompiler* compiler, - RegExpNode* on_success) { - return new (compiler->zone()) - BackReferenceNode(RegExpCapture::StartRegister(index()), - RegExpCapture::EndRegister(index()), flags_, - compiler->read_backward(), on_success); -} - - -RegExpNode* RegExpEmpty::ToNode(RegExpCompiler* compiler, - RegExpNode* on_success) { - return on_success; -} - - -RegExpLookaround::Builder::Builder(bool is_positive, RegExpNode* on_success, - int stack_pointer_register, - int position_register, - int capture_register_count, - int capture_register_start) - : is_positive_(is_positive), - on_success_(on_success), - stack_pointer_register_(stack_pointer_register), - position_register_(position_register) { - if (is_positive_) { - on_match_success_ = ActionNode::PositiveSubmatchSuccess( - stack_pointer_register, position_register, capture_register_count, - capture_register_start, on_success_); - } else { - Zone* zone = on_success_->zone(); - on_match_success_ = new (zone) NegativeSubmatchSuccess( - stack_pointer_register, position_register, capture_register_count, - capture_register_start, zone); - } -} - - -RegExpNode* RegExpLookaround::Builder::ForMatch(RegExpNode* match) { - if (is_positive_) { - return ActionNode::BeginSubmatch(stack_pointer_register_, - position_register_, match); - } else { - Zone* zone = on_success_->zone(); - // We use a ChoiceNode to represent the negative lookaround. The first - // alternative is the negative match. On success, the end node backtracks. - // On failure, the second alternative is tried and leads to success. - // NegativeLookaheadChoiceNode is a special ChoiceNode that ignores the - // first exit when calculating quick checks. - ChoiceNode* choice_node = new (zone) NegativeLookaroundChoiceNode( - GuardedAlternative(match), GuardedAlternative(on_success_), zone); - return ActionNode::BeginSubmatch(stack_pointer_register_, - position_register_, choice_node); - } -} - - -RegExpNode* RegExpLookaround::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; - - RegExpNode* result; - bool was_reading_backward = compiler->read_backward(); - compiler->set_read_backward(type() == LOOKBEHIND); - Builder builder(is_positive(), on_success, stack_pointer_register, - position_register, register_count, register_start); - RegExpNode* match = body_->ToNode(compiler, builder.on_match_success()); - result = builder.ForMatch(match); - compiler->set_read_backward(was_reading_backward); - return result; -} - - -RegExpNode* RegExpCapture::ToNode(RegExpCompiler* compiler, - RegExpNode* on_success) { - return ToNode(body(), index(), compiler, on_success); -} - - -RegExpNode* RegExpCapture::ToNode(RegExpTree* body, - int index, - RegExpCompiler* compiler, - RegExpNode* on_success) { - DCHECK_NOT_NULL(body); - int start_reg = RegExpCapture::StartRegister(index); - int end_reg = RegExpCapture::EndRegister(index); - if (compiler->read_backward()) std::swap(start_reg, end_reg); - 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) { - ZoneList* children = nodes(); - RegExpNode* current = on_success; - if (compiler->read_backward()) { - for (int i = 0; i < children->length(); i++) { - current = children->at(i)->ToNode(compiler, current); - } - } else { - for (int i = children->length() - 1; i >= 0; i--) { - current = children->at(i)->ToNode(compiler, current); - } - } - return current; -} - - -static void AddClass(const int* elmv, - int elmc, - ZoneList* ranges, - Zone* zone) { - elmc--; - DCHECK_EQ(kRangeEndMarker, elmv[elmc]); - for (int i = 0; i < elmc; i += 2) { - DCHECK(elmv[i] < elmv[i + 1]); - ranges->Add(CharacterRange::Range(elmv[i], elmv[i + 1] - 1), zone); - } -} - - -static void AddClassNegated(const int *elmv, - int elmc, - ZoneList* ranges, - Zone* zone) { - elmc--; - DCHECK_EQ(kRangeEndMarker, elmv[elmc]); - DCHECK_NE(0x0000, elmv[0]); - DCHECK_NE(String::kMaxCodePoint, elmv[elmc - 1]); - uc16 last = 0x0000; - for (int i = 0; i < elmc; i += 2) { - DCHECK(last <= elmv[i] - 1); - DCHECK(elmv[i] < elmv[i + 1]); - ranges->Add(CharacterRange::Range(last, elmv[i] - 1), zone); - last = elmv[i + 1]; - } - ranges->Add(CharacterRange::Range(last, String::kMaxCodePoint), zone); -} - -void CharacterRange::AddClassEscape(char type, ZoneList* ranges, - bool add_unicode_case_equivalents, - Zone* zone) { - if (add_unicode_case_equivalents && (type == 'w' || type == 'W')) { - // See #sec-runtime-semantics-wordcharacters-abstract-operation - // In case of unicode and ignore_case, we need to create the closure over - // case equivalent characters before negating. - ZoneList* new_ranges = - new (zone) ZoneList(2, zone); - AddClass(kWordRanges, kWordRangeCount, new_ranges, zone); - AddUnicodeCaseEquivalents(new_ranges, zone); - if (type == 'W') { - ZoneList* negated = - new (zone) ZoneList(2, zone); - CharacterRange::Negate(new_ranges, negated, zone); - new_ranges = negated; - } - ranges->AddAll(*new_ranges, zone); - return; - } - AddClassEscape(type, ranges, zone); -} - -void CharacterRange::AddClassEscape(char type, ZoneList* ranges, - Zone* zone) { - switch (type) { - case 's': - AddClass(kSpaceRanges, kSpaceRangeCount, ranges, zone); - break; - case 'S': - AddClassNegated(kSpaceRanges, kSpaceRangeCount, ranges, zone); - break; - case 'w': - AddClass(kWordRanges, kWordRangeCount, ranges, zone); - break; - case 'W': - AddClassNegated(kWordRanges, kWordRangeCount, ranges, zone); - break; - case 'd': - AddClass(kDigitRanges, kDigitRangeCount, ranges, zone); - break; - case 'D': - AddClassNegated(kDigitRanges, kDigitRangeCount, ranges, zone); - break; - case '.': - AddClassNegated(kLineTerminatorRanges, - kLineTerminatorRangeCount, - ranges, - zone); - 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->Add(CharacterRange::Everything(), zone); - break; - // This is the set of characters matched by the $ and ^ symbols - // in multiline mode. - case 'n': - AddClass(kLineTerminatorRanges, - kLineTerminatorRangeCount, - ranges, - zone); - break; - default: - UNREACHABLE(); - } -} - - -Vector CharacterRange::GetWordBounds() { - return Vector(kWordRanges, kWordRangeCount - 1); -} - -#ifdef V8_INTL_SUPPORT -struct IgnoreSet { - IgnoreSet() : set(BuildIgnoreSet()) {} - const icu::UnicodeSet set; -}; - -struct SpecialAddSet { - SpecialAddSet() : set(BuildSpecialAddSet()) {} - const icu::UnicodeSet set; -}; - -icu::UnicodeSet BuildAsciiAToZSet() { - icu::UnicodeSet set('a', 'z'); - set.add('A', 'Z'); - set.freeze(); - return set; -} - -struct AsciiAToZSet { - AsciiAToZSet() : set(BuildAsciiAToZSet()) {} - const icu::UnicodeSet set; -}; - -static base::LazyInstance::type ignore_set = - LAZY_INSTANCE_INITIALIZER; - -static base::LazyInstance::type special_add_set = - LAZY_INSTANCE_INITIALIZER; - -static base::LazyInstance::type ascii_a_to_z_set = - LAZY_INSTANCE_INITIALIZER; -#endif // V8_INTL_SUPPORT - -// static -void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone, - ZoneList* ranges, - bool is_one_byte) { - CharacterRange::Canonicalize(ranges); - int range_count = ranges->length(); -#ifdef V8_INTL_SUPPORT - icu::UnicodeSet others; - for (int i = 0; i < range_count; i++) { - CharacterRange range = ranges->at(i); - uc32 from = range.from(); - if (from > String::kMaxUtf16CodeUnit) continue; - uc32 to = Min(range.to(), String::kMaxUtf16CodeUnit); - // Nothing to be done for surrogates. - if (from >= kLeadSurrogateStart && to <= kTrailSurrogateEnd) continue; - if (is_one_byte && !RangeContainsLatin1Equivalents(range)) { - if (from > String::kMaxOneByteCharCode) continue; - if (to > String::kMaxOneByteCharCode) to = String::kMaxOneByteCharCode; - } - others.add(from, to); - } - - // Set of characters already added to ranges that do not need to be added - // again. - icu::UnicodeSet already_added(others); - - // Set of characters in ranges that are in the 52 ASCII characters [a-zA-Z]. - icu::UnicodeSet in_ascii_a_to_z(others); - in_ascii_a_to_z.retainAll(ascii_a_to_z_set.Pointer()->set); - - // Remove all chars in [a-zA-Z] from others. - others.removeAll(in_ascii_a_to_z); - - // Set of characters in ranges that are overlapping with special add set. - icu::UnicodeSet in_special_add(others); - in_special_add.retainAll(special_add_set.Pointer()->set); - - others.removeAll(in_special_add); - - // Ignore all chars in ignore set. - others.removeAll(ignore_set.Pointer()->set); - - // For most of the chars in ranges that is still in others, find the case - // equivlant set by calling closeOver(USET_CASE_INSENSITIVE). - others.closeOver(USET_CASE_INSENSITIVE); - - // Because closeOver(USET_CASE_INSENSITIVE) may add ASCII [a-zA-Z] to others, - // but ECMA262 "i" mode won't consider that, remove them from others. - // Ex: U+017F add 'S' and 's' to others. - others.removeAll(ascii_a_to_z_set.Pointer()->set); - - // Special handling for in_ascii_a_to_z. - for (int32_t i = 0; i < in_ascii_a_to_z.getRangeCount(); i++) { - UChar32 start = in_ascii_a_to_z.getRangeStart(i); - UChar32 end = in_ascii_a_to_z.getRangeEnd(i); - // Check if it is uppercase A-Z by checking bit 6. - if (start & 0x0020) { - // Add the lowercases - others.add(start & 0x005F, end & 0x005F); - } else { - // Add the uppercases - others.add(start | 0x0020, end | 0x0020); - } - } - - // Special handling for chars in "Special Add" set. - for (int32_t i = 0; i < in_special_add.getRangeCount(); i++) { - UChar32 end = in_special_add.getRangeEnd(i); - for (UChar32 ch = in_special_add.getRangeStart(i); ch <= end; ch++) { - // Add the uppercase of this character if itself is not an uppercase - // character. - // Note: The if condiction cannot be u_islower(ch) because ch could be - // neither uppercase nor lowercase but Mn. - if (!u_isupper(ch)) { - others.add(u_toupper(ch)); - } - icu::UnicodeSet candidates(ch, ch); - candidates.closeOver(USET_CASE_INSENSITIVE); - for (int32_t j = 0; j < candidates.getRangeCount(); j++) { - UChar32 end2 = candidates.getRangeEnd(j); - for (UChar32 ch2 = candidates.getRangeStart(j); ch2 <= end2; ch2++) { - // Add character that is not uppercase to others. - if (!u_isupper(ch2)) { - others.add(ch2); - } - } - } - } - } - - // Remove all characters which already in the ranges. - others.removeAll(already_added); - - // Add others to the ranges - for (int32_t i = 0; i < others.getRangeCount(); i++) { - UChar32 from = others.getRangeStart(i); - UChar32 to = others.getRangeEnd(i); - if (from == to) { - ranges->Add(CharacterRange::Singleton(from), zone); - } else { - ranges->Add(CharacterRange::Range(from, to), zone); - } - } -#else - for (int i = 0; i < range_count; i++) { - CharacterRange range = ranges->at(i); - uc32 bottom = range.from(); - if (bottom > String::kMaxUtf16CodeUnit) continue; - uc32 top = Min(range.to(), String::kMaxUtf16CodeUnit); - // Nothing to be done for surrogates. - if (bottom >= kLeadSurrogateStart && top <= kTrailSurrogateEnd) continue; - if (is_one_byte && !RangeContainsLatin1Equivalents(range)) { - if (bottom > String::kMaxOneByteCharCode) continue; - if (top > String::kMaxOneByteCharCode) top = String::kMaxOneByteCharCode; - } - unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth]; - if (top == bottom) { - // If this is a singleton we just expand the one character. - int length = isolate->jsregexp_uncanonicalize()->get(bottom, '\0', chars); - for (int i = 0; i < length; i++) { - uc32 chr = chars[i]; - if (chr != bottom) { - ranges->Add(CharacterRange::Singleton(chars[i]), zone); - } - } - } else { - // If this is a range we expand the characters block by block, expanding - // contiguous subranges (blocks) one at a time. The approach is as - // follows. For a given start character we look up the remainder of the - // block that contains it (represented by the end point), for instance we - // find 'z' if the character is 'c'. A block is characterized by the - // property that all characters uncanonicalize in the same way, except - // that each entry in the result is incremented by the distance from the - // first element. So a-z is a block because 'a' uncanonicalizes to ['a', - // 'A'] and the k'th letter uncanonicalizes to ['a' + k, 'A' + k]. Once - // we've found the end point we look up its uncanonicalization and - // produce a range for each element. For instance for [c-f] we look up - // ['z', 'Z'] and produce [c-f] and [C-F]. We then only add a range if - // it is not already contained in the input, so [c-f] will be skipped but - // [C-F] will be added. If this range is not completely contained in a - // block we do this for all the blocks covered by the range (handling - // characters that is not in a block as a "singleton block"). - unibrow::uchar equivalents[unibrow::Ecma262UnCanonicalize::kMaxWidth]; - int pos = bottom; - while (pos <= top) { - int length = - isolate->jsregexp_canonrange()->get(pos, '\0', equivalents); - uc32 block_end; - if (length == 0) { - block_end = pos; - } else { - DCHECK_EQ(1, length); - block_end = equivalents[0]; - } - int end = (block_end > top) ? top : block_end; - length = isolate->jsregexp_uncanonicalize()->get(block_end, '\0', - equivalents); - for (int i = 0; i < length; i++) { - uc32 c = equivalents[i]; - uc32 range_from = c - (block_end - pos); - uc32 range_to = c - (block_end - end); - if (!(bottom <= range_from && range_to <= top)) { - ranges->Add(CharacterRange::Range(range_from, range_to), zone); - } - } - pos = end + 1; - } - } - } -#endif // V8_INTL_SUPPORT -} - -bool CharacterRange::IsCanonical(ZoneList* ranges) { - DCHECK_NOT_NULL(ranges); - int n = ranges->length(); - if (n <= 1) return true; - int max = ranges->at(0).to(); - for (int i = 1; i < n; i++) { - CharacterRange next_range = ranges->at(i); - if (next_range.from() <= max + 1) return false; - max = next_range.to(); - } - return true; -} - - -ZoneList* CharacterSet::ranges(Zone* zone) { - if (ranges_ == nullptr) { - ranges_ = new(zone) ZoneList(2, zone); - CharacterRange::AddClassEscape(standard_set_type_, ranges_, false, zone); - } - return ranges_; -} - - -// Move a number of elements in a zonelist to another position -// in the same list. Handles overlapping source and target areas. -static void MoveRanges(ZoneList* list, - int from, - int to, - int count) { - // Ranges are potentially overlapping. - if (from < to) { - for (int i = count - 1; i >= 0; i--) { - list->at(to + i) = list->at(from + i); - } - } else { - for (int i = 0; i < count; i++) { - list->at(to + i) = list->at(from + i); - } - } -} - - -static int InsertRangeInCanonicalList(ZoneList* 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. - uc32 from = insert.from(); - uc32 to = insert.to(); - int start_pos = 0; - int end_pos = count; - for (int i = count - 1; i >= 0; i--) { - CharacterRange current = list->at(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->at(start_pos) = insert; - return count + 1; - } - if (start_pos + 1 == end_pos) { - // Replace single existing range at position start_pos. - CharacterRange to_replace = list->at(start_pos); - int new_from = Min(to_replace.from(), from); - int new_to = Max(to_replace.to(), to); - list->at(start_pos) = CharacterRange::Range(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->at(start_pos).from(), from); - int new_to = Max(list->at(end_pos - 1).to(), to); - if (end_pos < count) { - MoveRanges(list, end_pos, start_pos + 1, count - end_pos); - } - list->at(start_pos) = CharacterRange::Range(new_from, new_to); - return count - (end_pos - start_pos) + 1; -} - - -void CharacterSet::Canonicalize() { - // Special/default classes are always considered canonical. The result - // of calling ranges() will be sorted. - if (ranges_ == nullptr) return; - CharacterRange::Canonicalize(ranges_); -} - - -void CharacterRange::Canonicalize(ZoneList* 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->at(0).to(); - int i = 1; - while (i < n) { - CharacterRange current = character_ranges->at(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. - int num_canonical = i; // Length of canonicalized part of list. - do { - num_canonical = InsertRangeInCanonicalList(character_ranges, - num_canonical, - character_ranges->at(read)); - read++; - } while (read < n); - character_ranges->Rewind(num_canonical); - - DCHECK(CharacterRange::IsCanonical(character_ranges)); -} - - -void CharacterRange::Negate(ZoneList* ranges, - ZoneList* negated_ranges, - Zone* zone) { - DCHECK(CharacterRange::IsCanonical(ranges)); - DCHECK_EQ(0, negated_ranges->length()); - int range_count = ranges->length(); - uc32 from = 0; - int i = 0; - if (range_count > 0 && ranges->at(0).from() == 0) { - from = ranges->at(0).to() + 1; - i = 1; - } - while (i < range_count) { - CharacterRange range = ranges->at(i); - negated_ranges->Add(CharacterRange::Range(from, range.from() - 1), zone); - from = range.to() + 1; - i++; - } - if (from < String::kMaxCodePoint) { - negated_ranges->Add(CharacterRange::Range(from, String::kMaxCodePoint), - zone); - } -} - - -// ------------------------------------------------------------------- -// Splay tree - - -OutSet* OutSet::Extend(unsigned value, Zone* zone) { - if (Get(value)) - return this; - if (successors(zone) != nullptr) { - for (int i = 0; i < successors(zone)->length(); i++) { - OutSet* successor = successors(zone)->at(i); - if (successor->Get(value)) - return successor; - } - } else { - successors_ = new(zone) ZoneList(2, zone); - } - OutSet* result = new(zone) OutSet(first_, remaining_); - result->Set(value, zone); - successors(zone)->Add(result, zone); - return result; -} - - -void OutSet::Set(unsigned value, Zone *zone) { - if (value < kFirstLimit) { - first_ |= (1 << value); - } else { - if (remaining_ == nullptr) - remaining_ = new(zone) ZoneList(1, zone); - if (remaining_->is_empty() || !remaining_->Contains(value)) - remaining_->Add(value, zone); - } -} - - -bool OutSet::Get(unsigned value) const { - if (value < kFirstLimit) { - return (first_ & (1 << value)) != 0; - } else if (remaining_ == nullptr) { - return false; - } else { - return remaining_->Contains(value); - } -} - - -const uc32 DispatchTable::Config::kNoKey = unibrow::Utf8::kBadChar; - - -void DispatchTable::AddRange(CharacterRange full_range, int value, - Zone* zone) { - CharacterRange current = full_range; - if (tree()->is_empty()) { - // If this is the first range we just insert into the table. - ZoneSplayTree::Locator loc; - bool inserted = tree()->Insert(current.from(), &loc); - DCHECK(inserted); - USE(inserted); - loc.set_value(Entry(current.from(), current.to(), - empty()->Extend(value, zone))); - return; - } - // First see if there is a range to the left of this one that - // overlaps. - ZoneSplayTree::Locator loc; - if (tree()->FindGreatestLessThan(current.from(), &loc)) { - Entry* entry = &loc.value(); - // If we've found a range that overlaps with this one, and it - // starts strictly to the left of this one, we have to fix it - // because the following code only handles ranges that start on - // or after the start point of the range we're adding. - if (entry->from() < current.from() && entry->to() >= current.from()) { - // Snap the overlapping range in half around the start point of - // the range we're adding. - CharacterRange left = - CharacterRange::Range(entry->from(), current.from() - 1); - CharacterRange right = CharacterRange::Range(current.from(), entry->to()); - // The left part of the overlapping range doesn't overlap. - // Truncate the whole entry to be just the left part. - entry->set_to(left.to()); - // The right part is the one that overlaps. We add this part - // to the map and let the next step deal with merging it with - // the range we're adding. - ZoneSplayTree::Locator loc; - bool inserted = tree()->Insert(right.from(), &loc); - DCHECK(inserted); - USE(inserted); - loc.set_value(Entry(right.from(), - right.to(), - entry->out_set())); - } - } - while (current.is_valid()) { - if (tree()->FindLeastGreaterThan(current.from(), &loc) && - (loc.value().from() <= current.to()) && - (loc.value().to() >= current.from())) { - Entry* entry = &loc.value(); - // We have overlap. If there is space between the start point of - // the range we're adding and where the overlapping range starts - // then we have to add a range covering just that space. - if (current.from() < entry->from()) { - ZoneSplayTree::Locator ins; - bool inserted = tree()->Insert(current.from(), &ins); - DCHECK(inserted); - USE(inserted); - ins.set_value(Entry(current.from(), - entry->from() - 1, - empty()->Extend(value, zone))); - current.set_from(entry->from()); - } - DCHECK_EQ(current.from(), entry->from()); - // If the overlapping range extends beyond the one we want to add - // we have to snap the right part off and add it separately. - if (entry->to() > current.to()) { - ZoneSplayTree::Locator ins; - bool inserted = tree()->Insert(current.to() + 1, &ins); - DCHECK(inserted); - USE(inserted); - ins.set_value(Entry(current.to() + 1, - entry->to(), - entry->out_set())); - entry->set_to(current.to()); - } - DCHECK(entry->to() <= current.to()); - // The overlapping range is now completely contained by the range - // we're adding so we can just update it and move the start point - // of the range we're adding just past it. - entry->AddValue(value, zone); - DCHECK(entry->to() + 1 > current.from()); - current.set_from(entry->to() + 1); - } else { - // There is no overlap so we can just add the range - ZoneSplayTree::Locator ins; - bool inserted = tree()->Insert(current.from(), &ins); - DCHECK(inserted); - USE(inserted); - ins.set_value(Entry(current.from(), - current.to(), - empty()->Extend(value, zone))); - break; - } - } -} - - -OutSet* DispatchTable::Get(uc32 value) { - ZoneSplayTree::Locator loc; - if (!tree()->FindGreatestLessThan(value, &loc)) - return empty(); - Entry* entry = &loc.value(); - if (value <= entry->to()) - return entry->out_set(); - else - return empty(); -} - - -// ------------------------------------------------------------------- -// Analysis - - -void Analysis::EnsureAnalyzed(RegExpNode* that) { - StackLimitCheck check(isolate()); - if (check.HasOverflowed()) { - 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 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()->at(i); - elm.set_cp_offset(cp_offset); - cp_offset += elm.length(); - } -} - - -void Analysis::VisitText(TextNode* that) { - that->MakeCaseIndependent(isolate(), is_one_byte_); - 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 (int i = 0; i < that->alternatives()->length(); i++) { - RegExpNode* node = that->alternatives()->at(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 (int i = 0; i < that->alternatives()->length(); i++) { - RegExpNode* node = that->alternatives()->at(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()); -} - - -void BackReferenceNode::FillInBMInfo(Isolate* isolate, 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); -} - - -STATIC_ASSERT(BoyerMoorePositionInfo::kMapSize == - RegExpMacroAssembler::kTableSize); - - -void ChoiceNode::FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) { - ZoneList* alts = alternatives(); - budget = (budget - 1) / alts->length(); - for (int i = 0; i < alts->length(); i++) { - GuardedAlternative& alt = alts->at(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; - } - alt.node()->FillInBMInfo(isolate, offset, budget, bm, not_at_start); - } - SaveBMInfo(bm, not_at_start, offset); -} - - -void TextNode::FillInBMInfo(Isolate* isolate, int initial_offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) { - if (initial_offset >= bm->length()) return; - int offset = initial_offset; - int max_char = bm->max_char(); - for (int i = 0; i < elements()->length(); i++) { - if (offset >= bm->length()) { - if (initial_offset == 0) set_bm_info(not_at_start, bm); - return; - } - TextElement text = elements()->at(i); - if (text.text_type() == TextElement::ATOM) { - RegExpAtom* atom = text.atom(); - for (int j = 0; j < atom->length(); j++, offset++) { - if (offset >= bm->length()) { - if (initial_offset == 0) set_bm_info(not_at_start, bm); - return; - } - uc16 character = atom->data()[j]; - if (IgnoreCase(atom->flags())) { - unibrow::uchar chars[4]; - int length = GetCaseIndependentLetters( - isolate, character, bm->max_char() == String::kMaxOneByteCharCode, - chars, 4); - for (int j = 0; j < length; j++) { - bm->Set(offset, chars[j]); - } - } else { - if (character <= max_char) bm->Set(offset, character); - } - } - } else { - DCHECK_EQ(TextElement::CHAR_CLASS, text.text_type()); - RegExpCharacterClass* char_class = text.char_class(); - ZoneList* ranges = char_class->ranges(zone()); - if (char_class->is_negated()) { - bm->SetAll(offset); - } else { - for (int k = 0; k < ranges->length(); k++) { - CharacterRange& range = ranges->at(k); - if (range.from() > max_char) continue; - int to = Min(max_char, static_cast(range.to())); - bm->SetInterval(offset, Interval(range.from(), to)); - } - } - offset++; - } - } - if (offset >= bm->length()) { - if (initial_offset == 0) set_bm_info(not_at_start, bm); - return; - } - on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, - true); // Not at start after a text node. - if (initial_offset == 0) set_bm_info(not_at_start, bm); -} - - -// ------------------------------------------------------------------- -// Dispatch table construction - - -void DispatchTableConstructor::VisitEnd(EndNode* that) { - AddRange(CharacterRange::Everything()); -} - - -void DispatchTableConstructor::BuildTable(ChoiceNode* node) { - node->set_being_calculated(true); - ZoneList* alternatives = node->alternatives(); - for (int i = 0; i < alternatives->length(); i++) { - set_choice_index(i); - alternatives->at(i).node()->Accept(this); - } - node->set_being_calculated(false); -} - - -class AddDispatchRange { - public: - explicit AddDispatchRange(DispatchTableConstructor* constructor) - : constructor_(constructor) { } - void Call(uc32 from, DispatchTable::Entry entry); - private: - DispatchTableConstructor* constructor_; -}; - - -void AddDispatchRange::Call(uc32 from, DispatchTable::Entry entry) { - constructor_->AddRange(CharacterRange::Range(from, entry.to())); -} - - -void DispatchTableConstructor::VisitChoice(ChoiceNode* node) { - if (node->being_calculated()) - return; - DispatchTable* table = node->GetTable(ignore_case_); - AddDispatchRange adder(this); - table->ForEach(&adder); -} - - -void DispatchTableConstructor::VisitBackReference(BackReferenceNode* that) { - // TODO(160): Find the node that we refer back to and propagate its start - // set back to here. For now we just accept anything. - AddRange(CharacterRange::Everything()); -} - - -void DispatchTableConstructor::VisitAssertion(AssertionNode* that) { - RegExpNode* target = that->on_success(); - target->Accept(this); -} - - -static int CompareRangeByFrom(const CharacterRange* a, - const CharacterRange* b) { - return Compare(a->from(), b->from()); -} - - -void DispatchTableConstructor::AddInverse(ZoneList* ranges) { - ranges->Sort(CompareRangeByFrom); - uc16 last = 0; - for (int i = 0; i < ranges->length(); i++) { - CharacterRange range = ranges->at(i); - if (last < range.from()) - AddRange(CharacterRange::Range(last, range.from() - 1)); - if (range.to() >= last) { - if (range.to() == String::kMaxCodePoint) { - return; - } else { - last = range.to() + 1; - } - } - } - AddRange(CharacterRange::Range(last, String::kMaxCodePoint)); -} - - -void DispatchTableConstructor::VisitText(TextNode* that) { - TextElement elm = that->elements()->at(0); - switch (elm.text_type()) { - case TextElement::ATOM: { - uc16 c = elm.atom()->data()[0]; - AddRange(CharacterRange::Range(c, c)); - break; - } - case TextElement::CHAR_CLASS: { - RegExpCharacterClass* tree = elm.char_class(); - ZoneList* ranges = tree->ranges(that->zone()); - if (tree->is_negated()) { - AddInverse(ranges); - } else { - for (int i = 0; i < ranges->length(); i++) - AddRange(ranges->at(i)); - } - break; - } - default: { - UNIMPLEMENTED(); - } - } -} - - -void DispatchTableConstructor::VisitAction(ActionNode* that) { - RegExpNode* target = that->on_success(); - target->Accept(this); -} - -RegExpNode* OptionallyStepBackToLeadSurrogate(RegExpCompiler* compiler, - RegExpNode* on_success, - JSRegExp::Flags flags) { - // If the regexp matching starts within a surrogate pair, step back - // to the lead surrogate and start matching from there. - DCHECK(!compiler->read_backward()); - Zone* zone = compiler->zone(); - ZoneList* lead_surrogates = CharacterRange::List( - zone, CharacterRange::Range(kLeadSurrogateStart, kLeadSurrogateEnd)); - ZoneList* trail_surrogates = CharacterRange::List( - zone, CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd)); - - ChoiceNode* optional_step_back = new (zone) ChoiceNode(2, zone); - - int stack_register = compiler->UnicodeLookaroundStackRegister(); - int position_register = compiler->UnicodeLookaroundPositionRegister(); - RegExpNode* step_back = TextNode::CreateForCharacterRanges( - zone, lead_surrogates, true, on_success, flags); - RegExpLookaround::Builder builder(true, step_back, stack_register, - position_register); - RegExpNode* match_trail = TextNode::CreateForCharacterRanges( - zone, trail_surrogates, false, builder.on_match_success(), flags); - - optional_step_back->AddAlternative( - GuardedAlternative(builder.ForMatch(match_trail))); - optional_step_back->AddAlternative(GuardedAlternative(on_success)); - - return optional_step_back; -} - - -RegExpEngine::CompilationResult RegExpEngine::Compile( - Isolate* isolate, Zone* zone, RegExpCompileData* data, - JSRegExp::Flags flags, Handle pattern, - Handle sample_subject, bool is_one_byte) { - if ((data->capture_count + 1) * 2 - 1 > RegExpMacroAssembler::kMaxRegister) { - return IrregexpRegExpTooBig(isolate); - } - bool is_sticky = IsSticky(flags); - bool is_global = IsGlobal(flags); - bool is_unicode = IsUnicode(flags); - RegExpCompiler compiler(isolate, zone, data->capture_count, is_one_byte); - - if (compiler.optimize()) - compiler.set_optimize(!TooMuchRegExpCode(isolate, pattern)); - - // Sample some characters from the middle of the string. - static const int kSampleSize = 128; - - sample_subject = String::Flatten(isolate, sample_subject); - int chars_sampled = 0; - int half_way = (sample_subject->length() - kSampleSize) / 2; - for (int i = Max(0, half_way); - i < sample_subject->length() && chars_sampled < kSampleSize; - i++, chars_sampled++) { - compiler.frequency_collator()->CountCharacter(sample_subject->Get(i)); - } - - // 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 = data->tree->IsAnchoredAtStart(); - int max_length = data->tree->max_match(); - if (!is_start_anchored && !is_sticky) { - // Add a .*? at the beginning, outside the body capture, unless - // this expression is anchored at the beginning or sticky. - JSRegExp::Flags default_flags = JSRegExp::Flags(); - RegExpNode* loop_node = RegExpQuantifier::ToNode( - 0, RegExpTree::kInfinity, false, - new (zone) RegExpCharacterClass('*', default_flags), &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 = new(zone) ChoiceNode(2, zone); - first_step_node->AddAlternative(GuardedAlternative(captured_body)); - first_step_node->AddAlternative(GuardedAlternative(new (zone) TextNode( - new (zone) RegExpCharacterClass('*', default_flags), false, - loop_node))); - node = first_step_node; - } else { - node = loop_node; - } - } - if (is_one_byte) { - node = node->FilterOneByte(RegExpCompiler::kMaxRecursion); - // 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->FilterOneByte(RegExpCompiler::kMaxRecursion); - } - } else if (is_unicode && (is_global || is_sticky)) { - node = OptionallyStepBackToLeadSurrogate(&compiler, node, flags); - } - - if (node == nullptr) node = new (zone) EndNode(EndNode::BACKTRACK, zone); - data->node = node; - Analysis analysis(isolate, is_one_byte); - analysis.EnsureAnalyzed(node); - if (analysis.has_failed()) { - const char* error_message = analysis.error_message(); - return CompilationResult(isolate, error_message); - } - - // Create the correct assembler for the architecture. - std::unique_ptr macro_assembler; - if (!FLAG_regexp_interpret_all) { - // Native regexp implementation. - DCHECK(!FLAG_jitless); - - NativeRegExpMacroAssembler::Mode mode = - is_one_byte ? NativeRegExpMacroAssembler::LATIN1 - : NativeRegExpMacroAssembler::UC16; - -#if V8_TARGET_ARCH_IA32 - macro_assembler.reset(new RegExpMacroAssemblerIA32( - isolate, zone, mode, (data->capture_count + 1) * 2)); -#elif V8_TARGET_ARCH_X64 - macro_assembler.reset(new RegExpMacroAssemblerX64( - isolate, zone, mode, (data->capture_count + 1) * 2)); -#elif V8_TARGET_ARCH_ARM - macro_assembler.reset(new RegExpMacroAssemblerARM( - isolate, zone, mode, (data->capture_count + 1) * 2)); -#elif V8_TARGET_ARCH_ARM64 - macro_assembler.reset(new RegExpMacroAssemblerARM64( - isolate, zone, mode, (data->capture_count + 1) * 2)); -#elif V8_TARGET_ARCH_S390 - macro_assembler.reset(new RegExpMacroAssemblerS390( - isolate, zone, mode, (data->capture_count + 1) * 2)); -#elif V8_TARGET_ARCH_PPC - macro_assembler.reset(new RegExpMacroAssemblerPPC( - isolate, zone, mode, (data->capture_count + 1) * 2)); -#elif V8_TARGET_ARCH_MIPS - macro_assembler.reset(new RegExpMacroAssemblerMIPS( - isolate, zone, mode, (data->capture_count + 1) * 2)); -#elif V8_TARGET_ARCH_MIPS64 - macro_assembler.reset(new RegExpMacroAssemblerMIPS( - isolate, zone, mode, (data->capture_count + 1) * 2)); -#else -#error "Unsupported architecture" -#endif - } else { - DCHECK(FLAG_regexp_interpret_all); - - // Interpreted regexp implementation. - macro_assembler.reset(new RegExpMacroAssemblerIrregexp(isolate, zone)); - } - - macro_assembler->set_slow_safe(TooMuchRegExpCode(isolate, pattern)); - - // 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 && !is_sticky && - max_length < kMaxBacksearchLimit) { - macro_assembler->SetCurrentPositionFromEnd(max_length); - } - - if (is_global) { - RegExpMacroAssembler::GlobalMode mode = RegExpMacroAssembler::GLOBAL; - if (data->tree->min_match() > 0) { - mode = RegExpMacroAssembler::GLOBAL_NO_ZERO_LENGTH_CHECK; - } else if (is_unicode) { - mode = RegExpMacroAssembler::GLOBAL_UNICODE; - } - macro_assembler->set_global_mode(mode); - } - - return compiler.Assemble(isolate, macro_assembler.get(), node, - data->capture_count, pattern); -} - -bool RegExpEngine::TooMuchRegExpCode(Isolate* isolate, Handle pattern) { - Heap* heap = isolate->heap(); - bool too_much = pattern->length() > RegExpImpl::kRegExpTooLargeToOptimize; - if (isolate->total_regexp_code_generated() > - RegExpImpl::kRegExpCompiledLimit && - heap->CommittedMemoryExecutable() > - RegExpImpl::kRegExpExecutableMemoryLimit) { - too_much = true; - } - return too_much; -} - -Object RegExpResultsCache::Lookup(Heap* heap, String key_string, - Object key_pattern, - FixedArray* last_match_cache, - ResultsCacheType type) { - FixedArray cache; - if (!key_string.IsInternalizedString()) return Smi::kZero; - if (type == STRING_SPLIT_SUBSTRINGS) { - DCHECK(key_pattern.IsString()); - if (!key_pattern.IsInternalizedString()) return Smi::kZero; - cache = heap->string_split_cache(); - } else { - DCHECK(type == REGEXP_MULTIPLE_INDICES); - DCHECK(key_pattern.IsFixedArray()); - cache = heap->regexp_multiple_cache(); - } - - uint32_t hash = key_string.Hash(); - uint32_t index = ((hash & (kRegExpResultsCacheSize - 1)) & - ~(kArrayEntriesPerCacheEntry - 1)); - if (cache.get(index + kStringOffset) != key_string || - cache.get(index + kPatternOffset) != key_pattern) { - index = - ((index + kArrayEntriesPerCacheEntry) & (kRegExpResultsCacheSize - 1)); - if (cache.get(index + kStringOffset) != key_string || - cache.get(index + kPatternOffset) != key_pattern) { - return Smi::kZero; - } - } - - *last_match_cache = FixedArray::cast(cache.get(index + kLastMatchOffset)); - return cache.get(index + kArrayOffset); -} - -void RegExpResultsCache::Enter(Isolate* isolate, Handle key_string, - Handle key_pattern, - Handle value_array, - Handle last_match_cache, - ResultsCacheType type) { - Factory* factory = isolate->factory(); - Handle cache; - if (!key_string->IsInternalizedString()) return; - if (type == STRING_SPLIT_SUBSTRINGS) { - DCHECK(key_pattern->IsString()); - if (!key_pattern->IsInternalizedString()) return; - cache = factory->string_split_cache(); - } else { - DCHECK(type == REGEXP_MULTIPLE_INDICES); - DCHECK(key_pattern->IsFixedArray()); - cache = factory->regexp_multiple_cache(); - } - - uint32_t hash = key_string->Hash(); - uint32_t index = ((hash & (kRegExpResultsCacheSize - 1)) & - ~(kArrayEntriesPerCacheEntry - 1)); - if (cache->get(index + kStringOffset) == Smi::kZero) { - cache->set(index + kStringOffset, *key_string); - cache->set(index + kPatternOffset, *key_pattern); - cache->set(index + kArrayOffset, *value_array); - cache->set(index + kLastMatchOffset, *last_match_cache); - } else { - uint32_t index2 = - ((index + kArrayEntriesPerCacheEntry) & (kRegExpResultsCacheSize - 1)); - if (cache->get(index2 + kStringOffset) == Smi::kZero) { - cache->set(index2 + kStringOffset, *key_string); - cache->set(index2 + kPatternOffset, *key_pattern); - cache->set(index2 + kArrayOffset, *value_array); - cache->set(index2 + kLastMatchOffset, *last_match_cache); - } else { - cache->set(index2 + kStringOffset, Smi::kZero); - cache->set(index2 + kPatternOffset, Smi::kZero); - cache->set(index2 + kArrayOffset, Smi::kZero); - cache->set(index2 + kLastMatchOffset, Smi::kZero); - cache->set(index + kStringOffset, *key_string); - cache->set(index + kPatternOffset, *key_pattern); - cache->set(index + kArrayOffset, *value_array); - cache->set(index + kLastMatchOffset, *last_match_cache); - } - } - // If the array is a reasonably short list of substrings, convert it into a - // list of internalized strings. - if (type == STRING_SPLIT_SUBSTRINGS && value_array->length() < 100) { - for (int i = 0; i < value_array->length(); i++) { - Handle str(String::cast(value_array->get(i)), isolate); - Handle internalized_str = factory->InternalizeString(str); - value_array->set(i, *internalized_str); - } - } - // Convert backing store to a copy-on-write array. - value_array->set_map_no_write_barrier( - ReadOnlyRoots(isolate).fixed_cow_array_map()); -} - -void RegExpResultsCache::Clear(FixedArray cache) { - for (int i = 0; i < kRegExpResultsCacheSize; i++) { - cache.set(i, Smi::kZero); - } -} - -} // namespace internal -} // namespace v8 diff --git a/chromium/v8/src/regexp/jsregexp.h b/chromium/v8/src/regexp/jsregexp.h deleted file mode 100644 index 832c7e3aa5c..00000000000 --- a/chromium/v8/src/regexp/jsregexp.h +++ /dev/null @@ -1,1548 +0,0 @@ -// Copyright 2012 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef V8_REGEXP_JSREGEXP_H_ -#define V8_REGEXP_JSREGEXP_H_ - -#include "src/execution/isolate.h" -#include "src/objects/js-regexp.h" -#include "src/regexp/regexp-ast.h" -#include "src/regexp/regexp-macro-assembler.h" -#include "src/utils/allocation.h" -#include "src/zone/zone-splay-tree.h" - -namespace v8 { -namespace internal { - -class NodeVisitor; -class RegExpCompiler; -class RegExpMacroAssembler; -class RegExpNode; -class RegExpTree; -class BoyerMooreLookahead; - -inline bool IgnoreCase(JSRegExp::Flags flags) { - return (flags & JSRegExp::kIgnoreCase) != 0; -} - -inline bool IsUnicode(JSRegExp::Flags flags) { - return (flags & JSRegExp::kUnicode) != 0; -} - -inline bool IsSticky(JSRegExp::Flags flags) { - return (flags & JSRegExp::kSticky) != 0; -} - -inline bool IsGlobal(JSRegExp::Flags flags) { - return (flags & JSRegExp::kGlobal) != 0; -} - -inline bool DotAll(JSRegExp::Flags flags) { - return (flags & JSRegExp::kDotAll) != 0; -} - -inline bool Multiline(JSRegExp::Flags flags) { - return (flags & JSRegExp::kMultiline) != 0; -} - -inline bool NeedsUnicodeCaseEquivalents(JSRegExp::Flags flags) { - // Both unicode and ignore_case flags are set. We need to use ICU to find - // the closure over case equivalents. - return IsUnicode(flags) && IgnoreCase(flags); -} - -class RegExpImpl { - public: - // Whether the irregexp engine generates native code or interpreter bytecode. - static bool UsesNativeRegExp() { return !FLAG_regexp_interpret_all; } - - // Returns a string representation of a regular expression. - // Implements RegExp.prototype.toString, see ECMA-262 section 15.10.6.4. - // This function calls the garbage collector if necessary. - static Handle ToString(Handle value); - - // Parses the RegExp pattern and prepares the JSRegExp object with - // generic data and choice of implementation - as well as what - // the implementation wants to store in the data field. - // Returns false if compilation fails. - V8_WARN_UNUSED_RESULT static MaybeHandle Compile( - Isolate* isolate, Handle re, Handle pattern, - JSRegExp::Flags flags); - - // See ECMA-262 section 15.10.6.2. - // This function calls the garbage collector if necessary. - V8_EXPORT_PRIVATE V8_WARN_UNUSED_RESULT static MaybeHandle Exec( - Isolate* isolate, Handle regexp, Handle subject, - int index, Handle last_match_info); - - // Prepares a JSRegExp object with Irregexp-specific data. - static void IrregexpInitialize(Isolate* isolate, Handle re, - Handle pattern, JSRegExp::Flags flags, - int capture_register_count); - - static void AtomCompile(Isolate* isolate, Handle re, - Handle pattern, JSRegExp::Flags flags, - Handle match_pattern); - - static int AtomExecRaw(Isolate* isolate, Handle regexp, - Handle subject, int index, int32_t* output, - int output_size); - - static Handle AtomExec(Isolate* isolate, Handle regexp, - Handle subject, int index, - Handle last_match_info); - - enum IrregexpResult { RE_FAILURE = 0, RE_SUCCESS = 1, RE_EXCEPTION = -1 }; - - // Prepare a RegExp for being executed one or more times (using - // IrregexpExecOnce) on the subject. - // This ensures that the regexp is compiled for the subject, and that - // the subject is flat. - // Returns the number of integer spaces required by IrregexpExecOnce - // as its "registers" argument. If the regexp cannot be compiled, - // an exception is set as pending, and this function returns negative. - static int IrregexpPrepare(Isolate* isolate, Handle regexp, - Handle subject); - - // Execute a regular expression on the subject, starting from index. - // If matching succeeds, return the number of matches. This can be larger - // than one in the case of global regular expressions. - // The captures and subcaptures are stored into the registers vector. - // If matching fails, returns RE_FAILURE. - // If execution fails, sets a pending exception and returns RE_EXCEPTION. - static int IrregexpExecRaw(Isolate* isolate, Handle regexp, - Handle subject, int index, int32_t* output, - int output_size); - - // Execute an Irregexp bytecode pattern. - // On a successful match, the result is a JSArray containing - // captured positions. On a failure, the result is the null value. - // Returns an empty handle in case of an exception. - V8_WARN_UNUSED_RESULT static MaybeHandle IrregexpExec( - Isolate* isolate, Handle regexp, Handle subject, - int index, Handle last_match_info); - - // Set last match info. If match is nullptr, then setting captures is - // omitted. - static Handle SetLastMatchInfo( - Isolate* isolate, Handle last_match_info, - Handle subject, int capture_count, int32_t* match); - - class GlobalCache { - public: - GlobalCache(Handle regexp, - Handle subject, - Isolate* isolate); - - V8_INLINE ~GlobalCache(); - - // Fetch the next entry in the cache for global regexp match results. - // This does not set the last match info. Upon failure, nullptr is - // returned. The cause can be checked with Result(). The previous result is - // still in available in memory when a failure happens. - V8_INLINE int32_t* FetchNext(); - - V8_INLINE int32_t* LastSuccessfulMatch(); - - V8_INLINE bool HasException() { return num_matches_ < 0; } - - private: - int AdvanceZeroLength(int last_index); - - int num_matches_; - int max_matches_; - int current_match_index_; - int registers_per_match_; - // Pointer to the last set of captures. - int32_t* register_array_; - int register_array_size_; - Handle regexp_; - Handle subject_; - Isolate* isolate_; - }; - - // For acting on the JSRegExp data FixedArray. - static int IrregexpMaxRegisterCount(FixedArray re); - static void SetIrregexpMaxRegisterCount(FixedArray re, int value); - static void SetIrregexpCaptureNameMap(FixedArray re, - Handle value); - static int IrregexpNumberOfCaptures(FixedArray re); - static int IrregexpNumberOfRegisters(FixedArray re); - static ByteArray IrregexpByteCode(FixedArray re, bool is_one_byte); - static Code IrregexpNativeCode(FixedArray re, bool is_one_byte); - - // Limit the space regexps take up on the heap. In order to limit this we - // would like to keep track of the amount of regexp code on the heap. This - // is not tracked, however. As a conservative approximation we track the - // total regexp code compiled including code that has subsequently been freed - // and the total executable memory at any point. - static const size_t kRegExpExecutableMemoryLimit = 16 * MB; - static const size_t kRegExpCompiledLimit = 1 * MB; - static const int kRegExpTooLargeToOptimize = 20 * KB; - - private: - static bool CompileIrregexp(Isolate* isolate, Handle re, - Handle sample_subject, bool is_one_byte); - static inline bool EnsureCompiledIrregexp(Isolate* isolate, - Handle re, - Handle sample_subject, - bool is_one_byte); -}; - - -// Represents the location of one element relative to the intersection of -// two sets. Corresponds to the four areas of a Venn diagram. -enum ElementInSetsRelation { - kInsideNone = 0, - kInsideFirst = 1, - kInsideSecond = 2, - kInsideBoth = 3 -}; - - -// A set of unsigned integers that behaves especially well on small -// integers (< 32). May do zone-allocation. -class OutSet: public ZoneObject { - public: - OutSet() : first_(0), remaining_(nullptr), successors_(nullptr) {} - OutSet* Extend(unsigned value, Zone* zone); - V8_EXPORT_PRIVATE bool Get(unsigned value) const; - static const unsigned kFirstLimit = 32; - - private: - // Destructively set a value in this set. In most cases you want - // to use Extend instead to ensure that only one instance exists - // that contains the same values. - void Set(unsigned value, Zone* zone); - - // The successors are a list of sets that contain the same values - // as this set and the one more value that is not present in this - // set. - ZoneList* successors(Zone* zone) { return successors_; } - - OutSet(uint32_t first, ZoneList* remaining) - : first_(first), remaining_(remaining), successors_(nullptr) {} - uint32_t first_; - ZoneList* remaining_; - ZoneList* successors_; - friend class Trace; -}; - - -// A mapping from integers, specified as ranges, to a set of integers. -// Used for mapping character ranges to choices. -class DispatchTable : public ZoneObject { - public: - explicit DispatchTable(Zone* zone) : tree_(zone) { } - - class Entry { - public: - Entry() : from_(0), to_(0), out_set_(nullptr) {} - Entry(uc32 from, uc32 to, OutSet* out_set) - : from_(from), to_(to), out_set_(out_set) { - DCHECK(from <= to); - } - uc32 from() { return from_; } - uc32 to() { return to_; } - void set_to(uc32 value) { to_ = value; } - void AddValue(int value, Zone* zone) { - out_set_ = out_set_->Extend(value, zone); - } - OutSet* out_set() { return out_set_; } - private: - uc32 from_; - uc32 to_; - OutSet* out_set_; - }; - - class Config { - public: - using Key = uc32; - using Value = Entry; - static const uc32 kNoKey; - static const Entry NoValue() { return Value(); } - static inline int Compare(uc32 a, uc32 b) { - if (a == b) - return 0; - else if (a < b) - return -1; - else - return 1; - } - }; - - V8_EXPORT_PRIVATE void AddRange(CharacterRange range, int value, Zone* zone); - V8_EXPORT_PRIVATE OutSet* Get(uc32 value); - void Dump(); - - template - void ForEach(Callback* callback) { - return tree()->ForEach(callback); - } - - private: - // There can't be a static empty set since it allocates its - // successors in a zone and caches them. - OutSet* empty() { return &empty_; } - OutSet empty_; - ZoneSplayTree* tree() { return &tree_; } - ZoneSplayTree tree_; -}; - - -// Categorizes character ranges into BMP, non-BMP, lead, and trail surrogates. -class UnicodeRangeSplitter { - public: - V8_EXPORT_PRIVATE UnicodeRangeSplitter(Zone* zone, - ZoneList* base); - void Call(uc32 from, DispatchTable::Entry entry); - - ZoneList* bmp() { return bmp_; } - ZoneList* lead_surrogates() { return lead_surrogates_; } - ZoneList* trail_surrogates() { return trail_surrogates_; } - ZoneList* non_bmp() const { return non_bmp_; } - - private: - static const int kBase = 0; - // Separate ranges into - static const int kBmpCodePoints = 1; - static const int kLeadSurrogates = 2; - static const int kTrailSurrogates = 3; - static const int kNonBmpCodePoints = 4; - - Zone* zone_; - DispatchTable table_; - ZoneList* bmp_; - ZoneList* lead_surrogates_; - ZoneList* trail_surrogates_; - ZoneList* non_bmp_; -}; - -#define FOR_EACH_NODE_TYPE(VISIT) \ - VISIT(End) \ - VISIT(Action) \ - VISIT(Choice) \ - VISIT(BackReference) \ - VISIT(Assertion) \ - VISIT(Text) - - -class Trace; -struct PreloadState; -class GreedyLoopState; -class AlternativeGenerationList; - -struct NodeInfo { - NodeInfo() - : being_analyzed(false), - been_analyzed(false), - follows_word_interest(false), - follows_newline_interest(false), - follows_start_interest(false), - at_end(false), - visited(false), - replacement_calculated(false) { } - - // Returns true if the interests and assumptions of this node - // matches the given one. - bool Matches(NodeInfo* that) { - return (at_end == that->at_end) && - (follows_word_interest == that->follows_word_interest) && - (follows_newline_interest == that->follows_newline_interest) && - (follows_start_interest == that->follows_start_interest); - } - - // Updates the interests of this node given the interests of the - // node preceding it. - void AddFromPreceding(NodeInfo* that) { - at_end |= that->at_end; - follows_word_interest |= that->follows_word_interest; - follows_newline_interest |= that->follows_newline_interest; - follows_start_interest |= that->follows_start_interest; - } - - bool HasLookbehind() { - return follows_word_interest || - follows_newline_interest || - follows_start_interest; - } - - // Sets the interests of this node to include the interests of the - // following node. - void AddFromFollowing(NodeInfo* that) { - follows_word_interest |= that->follows_word_interest; - follows_newline_interest |= that->follows_newline_interest; - follows_start_interest |= that->follows_start_interest; - } - - void ResetCompilationState() { - being_analyzed = false; - been_analyzed = false; - } - - bool being_analyzed: 1; - bool been_analyzed: 1; - - // These bits are set of this node has to know what the preceding - // character was. - bool follows_word_interest: 1; - bool follows_newline_interest: 1; - bool follows_start_interest: 1; - - bool at_end: 1; - bool visited: 1; - bool replacement_calculated: 1; -}; - - -// Details of a quick mask-compare check that can look ahead in the -// input stream. -class QuickCheckDetails { - public: - QuickCheckDetails() - : characters_(0), - mask_(0), - value_(0), - cannot_match_(false) { } - explicit QuickCheckDetails(int characters) - : characters_(characters), - mask_(0), - value_(0), - cannot_match_(false) { } - bool Rationalize(bool one_byte); - // Merge in the information from another branch of an alternation. - void Merge(QuickCheckDetails* other, int from_index); - // Advance the current position by some amount. - void Advance(int by, bool one_byte); - void Clear(); - bool cannot_match() { return cannot_match_; } - void set_cannot_match() { cannot_match_ = true; } - struct Position { - Position() : mask(0), value(0), determines_perfectly(false) { } - uc16 mask; - uc16 value; - bool determines_perfectly; - }; - int characters() { return characters_; } - void set_characters(int characters) { characters_ = characters; } - Position* positions(int index) { - DCHECK_LE(0, index); - DCHECK_GT(characters_, index); - return positions_ + index; - } - uint32_t mask() { return mask_; } - uint32_t value() { return value_; } - - private: - // How many characters do we have quick check information from. This is - // the same for all branches of a choice node. - int characters_; - Position positions_[4]; - // These values are the condensate of the above array after Rationalize(). - uint32_t mask_; - uint32_t value_; - // If set to true, there is no way this quick check can match at all. - // E.g., if it requires to be at the start of the input, and isn't. - bool cannot_match_; -}; - - -extern int kUninitializedRegExpNodePlaceHolder; - - -class RegExpNode: public ZoneObject { - public: - explicit RegExpNode(Zone* zone) - : replacement_(nullptr), - on_work_list_(false), - trace_count_(0), - zone_(zone) { - bm_info_[0] = bm_info_[1] = nullptr; - } - virtual ~RegExpNode(); - virtual void Accept(NodeVisitor* visitor) = 0; - // Generates a goto to this node or actually generates the code at this point. - virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0; - // How many characters must this node consume at a minimum in order to - // succeed. If we have found at least 'still_to_find' characters that - // must be consumed there is no need to ask any following nodes whether - // they are sure to eat any more characters. The not_at_start argument is - // used to indicate that we know we are not at the start of the input. In - // this case anchored branches will always fail and can be ignored when - // determining how many characters are consumed on success. - virtual int EatsAtLeast(int still_to_find, int budget, bool not_at_start) = 0; - // Emits some quick code that checks whether the preloaded characters match. - // Falls through on certain failure, jumps to the label on possible success. - // If the node cannot make a quick check it does nothing and returns false. - bool EmitQuickCheck(RegExpCompiler* compiler, - Trace* bounds_check_trace, - Trace* trace, - bool preload_has_checked_bounds, - Label* on_possible_success, - QuickCheckDetails* details_return, - bool fall_through_on_failure); - // For a given number of characters this returns a mask and a value. The - // next n characters are anded with the mask and compared with the value. - // A comparison failure indicates the node cannot match the next n characters. - // A comparison success indicates the node may match. - virtual void GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, - int characters_filled_in, - bool not_at_start) = 0; - static const int kNodeIsTooComplexForGreedyLoops = kMinInt; - virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; } - // Only returns the successor for a text node of length 1 that matches any - // character and that has no guards on it. - virtual RegExpNode* GetSuccessorOfOmnivorousTextNode( - RegExpCompiler* compiler) { - return nullptr; - } - - // Collects information on the possible code units (mod 128) that can match if - // we look forward. This is used for a Boyer-Moore-like string searching - // implementation. TODO(erikcorry): This should share more code with - // EatsAtLeast, GetQuickCheckDetails. The budget argument is used to limit - // the number of nodes we are willing to look at in order to create this data. - static const int kRecursionBudget = 200; - bool KeepRecursing(RegExpCompiler* compiler); - virtual void FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) { - UNREACHABLE(); - } - - // If we know that the input is one-byte then there are some nodes that can - // never match. This method returns a node that can be substituted for - // itself, or nullptr if the node can never match. - virtual RegExpNode* FilterOneByte(int depth) { return this; } - // Helper for FilterOneByte. - RegExpNode* replacement() { - DCHECK(info()->replacement_calculated); - return replacement_; - } - RegExpNode* set_replacement(RegExpNode* replacement) { - info()->replacement_calculated = true; - replacement_ = replacement; - return replacement; // For convenience. - } - - // We want to avoid recalculating the lookahead info, so we store it on the - // node. Only info that is for this node is stored. We can tell that the - // info is for this node when offset == 0, so the information is calculated - // relative to this node. - void SaveBMInfo(BoyerMooreLookahead* bm, bool not_at_start, int offset) { - if (offset == 0) set_bm_info(not_at_start, bm); - } - - Label* label() { return &label_; } - // If non-generic code is generated for a node (i.e. the node is not at the - // start of the trace) then it cannot be reused. This variable sets a limit - // on how often we allow that to happen before we insist on starting a new - // trace and generating generic code for a node that can be reused by flushing - // the deferred actions in the current trace and generating a goto. - static const int kMaxCopiesCodeGenerated = 10; - - bool on_work_list() { return on_work_list_; } - void set_on_work_list(bool value) { on_work_list_ = value; } - - NodeInfo* info() { return &info_; } - - BoyerMooreLookahead* bm_info(bool not_at_start) { - return bm_info_[not_at_start ? 1 : 0]; - } - - Zone* zone() const { return zone_; } - - protected: - enum LimitResult { DONE, CONTINUE }; - RegExpNode* replacement_; - - LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace); - - void set_bm_info(bool not_at_start, BoyerMooreLookahead* bm) { - bm_info_[not_at_start ? 1 : 0] = bm; - } - - private: - static const int kFirstCharBudget = 10; - Label label_; - bool on_work_list_; - NodeInfo info_; - // This variable keeps track of how many times code has been generated for - // this node (in different traces). We don't keep track of where the - // generated code is located unless the code is generated at the start of - // a trace, in which case it is generic and can be reused by flushing the - // deferred operations in the current trace and generating a goto. - int trace_count_; - BoyerMooreLookahead* bm_info_[2]; - - Zone* zone_; -}; - - -class SeqRegExpNode: public RegExpNode { - public: - explicit SeqRegExpNode(RegExpNode* on_success) - : RegExpNode(on_success->zone()), on_success_(on_success) { } - RegExpNode* on_success() { return on_success_; } - void set_on_success(RegExpNode* node) { on_success_ = node; } - RegExpNode* FilterOneByte(int depth) override; - void FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) override { - on_success_->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); - if (offset == 0) set_bm_info(not_at_start, bm); - } - - protected: - RegExpNode* FilterSuccessor(int depth); - - private: - RegExpNode* on_success_; -}; - - -class ActionNode: public SeqRegExpNode { - public: - enum ActionType { - SET_REGISTER, - INCREMENT_REGISTER, - STORE_POSITION, - BEGIN_SUBMATCH, - POSITIVE_SUBMATCH_SUCCESS, - EMPTY_MATCH_CHECK, - CLEAR_CAPTURES - }; - static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success); - static ActionNode* IncrementRegister(int reg, RegExpNode* on_success); - static ActionNode* StorePosition(int reg, - bool is_capture, - RegExpNode* on_success); - static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success); - static ActionNode* BeginSubmatch(int stack_pointer_reg, - int position_reg, - RegExpNode* on_success); - static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg, - int restore_reg, - int clear_capture_count, - int clear_capture_from, - RegExpNode* on_success); - static ActionNode* EmptyMatchCheck(int start_register, - int repetition_register, - int repetition_limit, - RegExpNode* on_success); - void Accept(NodeVisitor* visitor) override; - void Emit(RegExpCompiler* compiler, Trace* trace) override; - int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; - void GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, int filled_in, - bool not_at_start) override { - return on_success()->GetQuickCheckDetails( - details, compiler, filled_in, not_at_start); - } - void FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) override; - ActionType action_type() { return action_type_; } - // TODO(erikcorry): We should allow some action nodes in greedy loops. - int GreedyLoopTextLength() override { - return kNodeIsTooComplexForGreedyLoops; - } - - private: - union { - struct { - int reg; - int value; - } u_store_register; - struct { - int reg; - } u_increment_register; - struct { - int reg; - bool is_capture; - } u_position_register; - struct { - int stack_pointer_register; - int current_position_register; - int clear_register_count; - int clear_register_from; - } u_submatch; - struct { - int start_register; - int repetition_register; - int repetition_limit; - } u_empty_match_check; - struct { - int range_from; - int range_to; - } u_clear_captures; - } data_; - ActionNode(ActionType action_type, RegExpNode* on_success) - : SeqRegExpNode(on_success), - action_type_(action_type) { } - ActionType action_type_; - friend class DotPrinter; -}; - - -class TextNode: public SeqRegExpNode { - public: - TextNode(ZoneList* elms, bool read_backward, - RegExpNode* on_success) - : SeqRegExpNode(on_success), elms_(elms), read_backward_(read_backward) {} - TextNode(RegExpCharacterClass* that, bool read_backward, - RegExpNode* on_success) - : SeqRegExpNode(on_success), - elms_(new (zone()) ZoneList(1, zone())), - read_backward_(read_backward) { - elms_->Add(TextElement::CharClass(that), zone()); - } - // Create TextNode for a single character class for the given ranges. - static TextNode* CreateForCharacterRanges(Zone* zone, - ZoneList* ranges, - bool read_backward, - RegExpNode* on_success, - JSRegExp::Flags flags); - // Create TextNode for a surrogate pair with a range given for the - // lead and the trail surrogate each. - static TextNode* CreateForSurrogatePair(Zone* zone, CharacterRange lead, - CharacterRange trail, - bool read_backward, - RegExpNode* on_success, - JSRegExp::Flags flags); - void Accept(NodeVisitor* visitor) override; - void Emit(RegExpCompiler* compiler, Trace* trace) override; - int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; - void GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, int characters_filled_in, - bool not_at_start) override; - ZoneList* elements() { return elms_; } - bool read_backward() { return read_backward_; } - void MakeCaseIndependent(Isolate* isolate, bool is_one_byte); - int GreedyLoopTextLength() override; - RegExpNode* GetSuccessorOfOmnivorousTextNode( - RegExpCompiler* compiler) override; - void FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) override; - void CalculateOffsets(); - RegExpNode* FilterOneByte(int depth) override; - - private: - enum TextEmitPassType { - NON_LATIN1_MATCH, // Check for characters that can't match. - SIMPLE_CHARACTER_MATCH, // Case-dependent single character check. - NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs. - CASE_CHARACTER_MATCH, // Case-independent single character check. - CHARACTER_CLASS_MATCH // Character class. - }; - static bool SkipPass(TextEmitPassType pass, bool ignore_case); - static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH; - static const int kLastPass = CHARACTER_CLASS_MATCH; - void TextEmitPass(RegExpCompiler* compiler, - TextEmitPassType pass, - bool preloaded, - Trace* trace, - bool first_element_checked, - int* checked_up_to); - int Length(); - ZoneList* elms_; - bool read_backward_; -}; - - -class AssertionNode: public SeqRegExpNode { - public: - enum AssertionType { - AT_END, - AT_START, - AT_BOUNDARY, - AT_NON_BOUNDARY, - AFTER_NEWLINE - }; - static AssertionNode* AtEnd(RegExpNode* on_success) { - return new(on_success->zone()) AssertionNode(AT_END, on_success); - } - static AssertionNode* AtStart(RegExpNode* on_success) { - return new(on_success->zone()) AssertionNode(AT_START, on_success); - } - static AssertionNode* AtBoundary(RegExpNode* on_success) { - return new(on_success->zone()) AssertionNode(AT_BOUNDARY, on_success); - } - static AssertionNode* AtNonBoundary(RegExpNode* on_success) { - return new(on_success->zone()) AssertionNode(AT_NON_BOUNDARY, on_success); - } - static AssertionNode* AfterNewline(RegExpNode* on_success) { - return new(on_success->zone()) AssertionNode(AFTER_NEWLINE, on_success); - } - void Accept(NodeVisitor* visitor) override; - void Emit(RegExpCompiler* compiler, Trace* trace) override; - int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; - void GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, int filled_in, - bool not_at_start) override; - void FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) override; - AssertionType assertion_type() { return assertion_type_; } - - private: - void EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace); - enum IfPrevious { kIsNonWord, kIsWord }; - void BacktrackIfPrevious(RegExpCompiler* compiler, - Trace* trace, - IfPrevious backtrack_if_previous); - AssertionNode(AssertionType t, RegExpNode* on_success) - : SeqRegExpNode(on_success), assertion_type_(t) { } - AssertionType assertion_type_; -}; - - -class BackReferenceNode: public SeqRegExpNode { - public: - BackReferenceNode(int start_reg, int end_reg, JSRegExp::Flags flags, - bool read_backward, RegExpNode* on_success) - : SeqRegExpNode(on_success), - start_reg_(start_reg), - end_reg_(end_reg), - flags_(flags), - read_backward_(read_backward) {} - void Accept(NodeVisitor* visitor) override; - int start_register() { return start_reg_; } - int end_register() { return end_reg_; } - bool read_backward() { return read_backward_; } - void Emit(RegExpCompiler* compiler, Trace* trace) override; - int EatsAtLeast(int still_to_find, int recursion_depth, - bool not_at_start) override; - void GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, int characters_filled_in, - bool not_at_start) override { - return; - } - void FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) override; - - private: - int start_reg_; - int end_reg_; - JSRegExp::Flags flags_; - bool read_backward_; -}; - - -class EndNode: public RegExpNode { - public: - enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS }; - EndNode(Action action, Zone* zone) : RegExpNode(zone), action_(action) {} - void Accept(NodeVisitor* visitor) override; - void Emit(RegExpCompiler* compiler, Trace* trace) override; - int EatsAtLeast(int still_to_find, int recursion_depth, - bool not_at_start) override { - return 0; - } - void GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, int characters_filled_in, - bool not_at_start) override { - // Returning 0 from EatsAtLeast should ensure we never get here. - UNREACHABLE(); - } - void FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) override { - // Returning 0 from EatsAtLeast should ensure we never get here. - UNREACHABLE(); - } - - private: - Action action_; -}; - - -class NegativeSubmatchSuccess: public EndNode { - public: - NegativeSubmatchSuccess(int stack_pointer_reg, - int position_reg, - int clear_capture_count, - int clear_capture_start, - Zone* zone) - : EndNode(NEGATIVE_SUBMATCH_SUCCESS, zone), - stack_pointer_register_(stack_pointer_reg), - current_position_register_(position_reg), - clear_capture_count_(clear_capture_count), - clear_capture_start_(clear_capture_start) { } - void Emit(RegExpCompiler* compiler, Trace* trace) override; - - private: - int stack_pointer_register_; - int current_position_register_; - int clear_capture_count_; - int clear_capture_start_; -}; - - -class Guard: public ZoneObject { - public: - enum Relation { LT, GEQ }; - Guard(int reg, Relation op, int value) - : reg_(reg), - op_(op), - value_(value) { } - int reg() { return reg_; } - Relation op() { return op_; } - int value() { return value_; } - - private: - int reg_; - Relation op_; - int value_; -}; - - -class GuardedAlternative { - public: - explicit GuardedAlternative(RegExpNode* node) - : node_(node), guards_(nullptr) {} - void AddGuard(Guard* guard, Zone* zone); - RegExpNode* node() { return node_; } - void set_node(RegExpNode* node) { node_ = node; } - ZoneList* guards() { return guards_; } - - private: - RegExpNode* node_; - ZoneList* guards_; -}; - - -class AlternativeGeneration; - - -class ChoiceNode: public RegExpNode { - public: - explicit ChoiceNode(int expected_size, Zone* zone) - : RegExpNode(zone), - alternatives_(new (zone) - ZoneList(expected_size, zone)), - table_(nullptr), - not_at_start_(false), - being_calculated_(false) {} - void Accept(NodeVisitor* visitor) override; - void AddAlternative(GuardedAlternative node) { - alternatives()->Add(node, zone()); - } - ZoneList* alternatives() { return alternatives_; } - DispatchTable* GetTable(bool ignore_case); - void Emit(RegExpCompiler* compiler, Trace* trace) override; - int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; - int EatsAtLeastHelper(int still_to_find, - int budget, - RegExpNode* ignore_this_node, - bool not_at_start); - void GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, int characters_filled_in, - bool not_at_start) override; - void FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) override; - - bool being_calculated() { return being_calculated_; } - bool not_at_start() { return not_at_start_; } - void set_not_at_start() { not_at_start_ = true; } - void set_being_calculated(bool b) { being_calculated_ = b; } - virtual bool try_to_emit_quick_check_for_alternative(bool is_first) { - return true; - } - RegExpNode* FilterOneByte(int depth) override; - virtual bool read_backward() { return false; } - - protected: - int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative); - ZoneList* alternatives_; - - private: - friend class DispatchTableConstructor; - friend class Analysis; - void GenerateGuard(RegExpMacroAssembler* macro_assembler, - Guard* guard, - Trace* trace); - int CalculatePreloadCharacters(RegExpCompiler* compiler, int eats_at_least); - void EmitOutOfLineContinuation(RegExpCompiler* compiler, - Trace* trace, - GuardedAlternative alternative, - AlternativeGeneration* alt_gen, - int preload_characters, - bool next_expects_preload); - void SetUpPreLoad(RegExpCompiler* compiler, - Trace* current_trace, - PreloadState* preloads); - void AssertGuardsMentionRegisters(Trace* trace); - int EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler, Trace* trace); - Trace* EmitGreedyLoop(RegExpCompiler* compiler, - Trace* trace, - AlternativeGenerationList* alt_gens, - PreloadState* preloads, - GreedyLoopState* greedy_loop_state, - int text_length); - void EmitChoices(RegExpCompiler* compiler, - AlternativeGenerationList* alt_gens, - int first_choice, - Trace* trace, - PreloadState* preloads); - DispatchTable* table_; - // If true, this node is never checked at the start of the input. - // Allows a new trace to start with at_start() set to false. - bool not_at_start_; - bool being_calculated_; -}; - - -class NegativeLookaroundChoiceNode : public ChoiceNode { - public: - explicit NegativeLookaroundChoiceNode(GuardedAlternative this_must_fail, - GuardedAlternative then_do_this, - Zone* zone) - : ChoiceNode(2, zone) { - AddAlternative(this_must_fail); - AddAlternative(then_do_this); - } - int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; - void GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, int characters_filled_in, - bool not_at_start) override; - void FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) override { - alternatives_->at(1).node()->FillInBMInfo(isolate, offset, budget - 1, bm, - not_at_start); - if (offset == 0) set_bm_info(not_at_start, bm); - } - // For a negative lookahead we don't emit the quick check for the - // alternative that is expected to fail. This is because quick check code - // starts by loading enough characters for the alternative that takes fewest - // characters, but on a negative lookahead the negative branch did not take - // part in that calculation (EatsAtLeast) so the assumptions don't hold. - bool try_to_emit_quick_check_for_alternative(bool is_first) override { - return !is_first; - } - RegExpNode* FilterOneByte(int depth) override; -}; - - -class LoopChoiceNode: public ChoiceNode { - public: - LoopChoiceNode(bool body_can_be_zero_length, bool read_backward, Zone* zone) - : ChoiceNode(2, zone), - loop_node_(nullptr), - continue_node_(nullptr), - body_can_be_zero_length_(body_can_be_zero_length), - read_backward_(read_backward) {} - void AddLoopAlternative(GuardedAlternative alt); - void AddContinueAlternative(GuardedAlternative alt); - void Emit(RegExpCompiler* compiler, Trace* trace) override; - int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; - void GetQuickCheckDetails(QuickCheckDetails* details, - RegExpCompiler* compiler, int characters_filled_in, - bool not_at_start) override; - void FillInBMInfo(Isolate* isolate, int offset, int budget, - BoyerMooreLookahead* bm, bool not_at_start) override; - RegExpNode* loop_node() { return loop_node_; } - RegExpNode* continue_node() { return continue_node_; } - bool body_can_be_zero_length() { return body_can_be_zero_length_; } - bool read_backward() override { return read_backward_; } - void Accept(NodeVisitor* visitor) override; - RegExpNode* FilterOneByte(int depth) override; - - private: - // AddAlternative is made private for loop nodes because alternatives - // should not be added freely, we need to keep track of which node - // goes back to the node itself. - void AddAlternative(GuardedAlternative node) { - ChoiceNode::AddAlternative(node); - } - - RegExpNode* loop_node_; - RegExpNode* continue_node_; - bool body_can_be_zero_length_; - bool read_backward_; -}; - - -// Improve the speed that we scan for an initial point where a non-anchored -// regexp can match by using a Boyer-Moore-like table. This is done by -// identifying non-greedy non-capturing loops in the nodes that eat any -// character one at a time. For example in the middle of the regexp -// /foo[\s\S]*?bar/ we find such a loop. There is also such a loop implicitly -// inserted at the start of any non-anchored regexp. -// -// When we have found such a loop we look ahead in the nodes to find the set of -// characters that can come at given distances. For example for the regexp -// /.?foo/ we know that there are at least 3 characters ahead of us, and the -// sets of characters that can occur are [any, [f, o], [o]]. We find a range in -// the lookahead info where the set of characters is reasonably constrained. In -// our example this is from index 1 to 2 (0 is not constrained). We can now -// look 3 characters ahead and if we don't find one of [f, o] (the union of -// [f, o] and [o]) then we can skip forwards by the range size (in this case 2). -// -// For Unicode input strings we do the same, but modulo 128. -// -// We also look at the first string fed to the regexp and use that to get a hint -// of the character frequencies in the inputs. This affects the assessment of -// whether the set of characters is 'reasonably constrained'. -// -// We also have another lookahead mechanism (called quick check in the code), -// which uses a wide load of multiple characters followed by a mask and compare -// to determine whether a match is possible at this point. -enum ContainedInLattice { - kNotYet = 0, - kLatticeIn = 1, - kLatticeOut = 2, - kLatticeUnknown = 3 // Can also mean both in and out. -}; - - -inline ContainedInLattice Combine(ContainedInLattice a, ContainedInLattice b) { - return static_cast(a | b); -} - - -ContainedInLattice AddRange(ContainedInLattice a, - const int* ranges, - int ranges_size, - Interval new_range); - - -class BoyerMoorePositionInfo : public ZoneObject { - public: - explicit BoyerMoorePositionInfo(Zone* zone) - : map_(new(zone) ZoneList(kMapSize, zone)), - map_count_(0), - w_(kNotYet), - s_(kNotYet), - d_(kNotYet), - surrogate_(kNotYet) { - for (int i = 0; i < kMapSize; i++) { - map_->Add(false, zone); - } - } - - bool& at(int i) { return map_->at(i); } - - static const int kMapSize = 128; - static const int kMask = kMapSize - 1; - - int map_count() const { return map_count_; } - - void Set(int character); - void SetInterval(const Interval& interval); - void SetAll(); - bool is_non_word() { return w_ == kLatticeOut; } - bool is_word() { return w_ == kLatticeIn; } - - private: - ZoneList* map_; - int map_count_; // Number of set bits in the map. - ContainedInLattice w_; // The \w character class. - ContainedInLattice s_; // The \s character class. - ContainedInLattice d_; // The \d character class. - ContainedInLattice surrogate_; // Surrogate UTF-16 code units. -}; - - -class BoyerMooreLookahead : public ZoneObject { - public: - BoyerMooreLookahead(int length, RegExpCompiler* compiler, Zone* zone); - - int length() { return length_; } - int max_char() { return max_char_; } - RegExpCompiler* compiler() { return compiler_; } - - int Count(int map_number) { - return bitmaps_->at(map_number)->map_count(); - } - - BoyerMoorePositionInfo* at(int i) { return bitmaps_->at(i); } - - void Set(int map_number, int character) { - if (character > max_char_) return; - BoyerMoorePositionInfo* info = bitmaps_->at(map_number); - info->Set(character); - } - - void SetInterval(int map_number, const Interval& interval) { - if (interval.from() > max_char_) return; - BoyerMoorePositionInfo* info = bitmaps_->at(map_number); - if (interval.to() > max_char_) { - info->SetInterval(Interval(interval.from(), max_char_)); - } else { - info->SetInterval(interval); - } - } - - void SetAll(int map_number) { - bitmaps_->at(map_number)->SetAll(); - } - - void SetRest(int from_map) { - for (int i = from_map; i < length_; i++) SetAll(i); - } - void EmitSkipInstructions(RegExpMacroAssembler* masm); - - private: - // This is the value obtained by EatsAtLeast. If we do not have at least this - // many characters left in the sample string then the match is bound to fail. - // Therefore it is OK to read a character this far ahead of the current match - // point. - int length_; - RegExpCompiler* compiler_; - // 0xff for Latin1, 0xffff for UTF-16. - int max_char_; - ZoneList* bitmaps_; - - int GetSkipTable(int min_lookahead, - int max_lookahead, - Handle boolean_skip_table); - bool FindWorthwhileInterval(int* from, int* to); - int FindBestInterval( - int max_number_of_chars, int old_biggest_points, int* from, int* to); -}; - - -// There are many ways to generate code for a node. This class encapsulates -// the current way we should be generating. In other words it encapsulates -// the current state of the code generator. The effect of this is that we -// generate code for paths that the matcher can take through the regular -// expression. A given node in the regexp can be code-generated several times -// as it can be part of several traces. For example for the regexp: -// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part -// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code -// to match foo is generated only once (the traces have a common prefix). The -// code to store the capture is deferred and generated (twice) after the places -// where baz has been matched. -class Trace { - public: - // A value for a property that is either known to be true, know to be false, - // or not known. - enum TriBool { - UNKNOWN = -1, FALSE_VALUE = 0, TRUE_VALUE = 1 - }; - - class DeferredAction { - public: - DeferredAction(ActionNode::ActionType action_type, int reg) - : action_type_(action_type), reg_(reg), next_(nullptr) {} - DeferredAction* next() { return next_; } - bool Mentions(int reg); - int reg() { return reg_; } - ActionNode::ActionType action_type() { return action_type_; } - private: - ActionNode::ActionType action_type_; - int reg_; - DeferredAction* next_; - friend class Trace; - }; - - class DeferredCapture : public DeferredAction { - public: - DeferredCapture(int reg, bool is_capture, Trace* trace) - : DeferredAction(ActionNode::STORE_POSITION, reg), - cp_offset_(trace->cp_offset()), - is_capture_(is_capture) { } - int cp_offset() { return cp_offset_; } - bool is_capture() { return is_capture_; } - private: - int cp_offset_; - bool is_capture_; - void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; } - }; - - class DeferredSetRegister : public DeferredAction { - public: - DeferredSetRegister(int reg, int value) - : DeferredAction(ActionNode::SET_REGISTER, reg), - value_(value) { } - int value() { return value_; } - private: - int value_; - }; - - class DeferredClearCaptures : public DeferredAction { - public: - explicit DeferredClearCaptures(Interval range) - : DeferredAction(ActionNode::CLEAR_CAPTURES, -1), - range_(range) { } - Interval range() { return range_; } - private: - Interval range_; - }; - - class DeferredIncrementRegister : public DeferredAction { - public: - explicit DeferredIncrementRegister(int reg) - : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { } - }; - - Trace() - : cp_offset_(0), - actions_(nullptr), - backtrack_(nullptr), - stop_node_(nullptr), - loop_label_(nullptr), - characters_preloaded_(0), - bound_checked_up_to_(0), - flush_budget_(100), - at_start_(UNKNOWN) {} - - // End the trace. This involves flushing the deferred actions in the trace - // and pushing a backtrack location onto the backtrack stack. Once this is - // done we can start a new trace or go to one that has already been - // generated. - void Flush(RegExpCompiler* compiler, RegExpNode* successor); - int cp_offset() { return cp_offset_; } - DeferredAction* actions() { return actions_; } - // A trivial trace is one that has no deferred actions or other state that - // affects the assumptions used when generating code. There is no recorded - // backtrack location in a trivial trace, so with a trivial trace we will - // generate code that, on a failure to match, gets the backtrack location - // from the backtrack stack rather than using a direct jump instruction. We - // always start code generation with a trivial trace and non-trivial traces - // are created as we emit code for nodes or add to the list of deferred - // actions in the trace. The location of the code generated for a node using - // a trivial trace is recorded in a label in the node so that gotos can be - // generated to that code. - bool is_trivial() { - return backtrack_ == nullptr && actions_ == nullptr && cp_offset_ == 0 && - characters_preloaded_ == 0 && bound_checked_up_to_ == 0 && - quick_check_performed_.characters() == 0 && at_start_ == UNKNOWN; - } - TriBool at_start() { return at_start_; } - void set_at_start(TriBool at_start) { at_start_ = at_start; } - Label* backtrack() { return backtrack_; } - Label* loop_label() { return loop_label_; } - RegExpNode* stop_node() { return stop_node_; } - int characters_preloaded() { return characters_preloaded_; } - int bound_checked_up_to() { return bound_checked_up_to_; } - int flush_budget() { return flush_budget_; } - QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; } - bool mentions_reg(int reg); - // Returns true if a deferred position store exists to the specified - // register and stores the offset in the out-parameter. Otherwise - // returns false. - bool GetStoredPosition(int reg, int* cp_offset); - // These set methods and AdvanceCurrentPositionInTrace should be used only on - // new traces - the intention is that traces are immutable after creation. - void add_action(DeferredAction* new_action) { - DCHECK(new_action->next_ == nullptr); - new_action->next_ = actions_; - actions_ = new_action; - } - void set_backtrack(Label* backtrack) { backtrack_ = backtrack; } - void set_stop_node(RegExpNode* node) { stop_node_ = node; } - void set_loop_label(Label* label) { loop_label_ = label; } - void set_characters_preloaded(int count) { characters_preloaded_ = count; } - void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; } - void set_flush_budget(int to) { flush_budget_ = to; } - void set_quick_check_performed(QuickCheckDetails* d) { - quick_check_performed_ = *d; - } - void InvalidateCurrentCharacter(); - void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler); - - private: - int FindAffectedRegisters(OutSet* affected_registers, Zone* zone); - void PerformDeferredActions(RegExpMacroAssembler* macro, - int max_register, - const OutSet& affected_registers, - OutSet* registers_to_pop, - OutSet* registers_to_clear, - Zone* zone); - void RestoreAffectedRegisters(RegExpMacroAssembler* macro, - int max_register, - const OutSet& registers_to_pop, - const OutSet& registers_to_clear); - int cp_offset_; - DeferredAction* actions_; - Label* backtrack_; - RegExpNode* stop_node_; - Label* loop_label_; - int characters_preloaded_; - int bound_checked_up_to_; - QuickCheckDetails quick_check_performed_; - int flush_budget_; - TriBool at_start_; -}; - - -class GreedyLoopState { - public: - explicit GreedyLoopState(bool not_at_start); - - Label* label() { return &label_; } - Trace* counter_backtrack_trace() { return &counter_backtrack_trace_; } - - private: - Label label_; - Trace counter_backtrack_trace_; -}; - - -struct PreloadState { - static const int kEatsAtLeastNotYetInitialized = -1; - bool preload_is_current_; - bool preload_has_checked_bounds_; - int preload_characters_; - int eats_at_least_; - void init() { - eats_at_least_ = kEatsAtLeastNotYetInitialized; - } -}; - - -class NodeVisitor { - public: - virtual ~NodeVisitor() = default; -#define DECLARE_VISIT(Type) \ - virtual void Visit##Type(Type##Node* that) = 0; -FOR_EACH_NODE_TYPE(DECLARE_VISIT) -#undef DECLARE_VISIT - virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); } -}; - - -// Node visitor used to add the start set of the alternatives to the -// dispatch table of a choice node. -class V8_EXPORT_PRIVATE DispatchTableConstructor : public NodeVisitor { - public: - DispatchTableConstructor(DispatchTable* table, bool ignore_case, - Zone* zone) - : table_(table), - choice_index_(-1), - ignore_case_(ignore_case), - zone_(zone) { } - - void BuildTable(ChoiceNode* node); - - void AddRange(CharacterRange range) { - table()->AddRange(range, choice_index_, zone_); - } - - void AddInverse(ZoneList* ranges); - -#define DECLARE_VISIT(Type) \ - virtual void Visit##Type(Type##Node* that); -FOR_EACH_NODE_TYPE(DECLARE_VISIT) -#undef DECLARE_VISIT - - DispatchTable* table() { return table_; } - void set_choice_index(int value) { choice_index_ = value; } - - protected: - DispatchTable* table_; - int choice_index_; - bool ignore_case_; - Zone* zone_; -}; - -// Assertion propagation moves information about assertions such as -// \b to the affected nodes. For instance, in /.\b./ information must -// be propagated to the first '.' that whatever follows needs to know -// if it matched a word or a non-word, and to the second '.' that it -// has to check if it succeeds a word or non-word. In this case the -// result will be something like: -// -// +-------+ +------------+ -// | . | | . | -// +-------+ ---> +------------+ -// | word? | | check word | -// +-------+ +------------+ -class Analysis: public NodeVisitor { - public: - Analysis(Isolate* isolate, bool is_one_byte) - : isolate_(isolate), is_one_byte_(is_one_byte), error_message_(nullptr) {} - void EnsureAnalyzed(RegExpNode* node); - -#define DECLARE_VISIT(Type) void Visit##Type(Type##Node* that) override; - FOR_EACH_NODE_TYPE(DECLARE_VISIT) -#undef DECLARE_VISIT - void VisitLoopChoice(LoopChoiceNode* that) override; - - bool has_failed() { return error_message_ != nullptr; } - const char* error_message() { - DCHECK(error_message_ != nullptr); - return error_message_; - } - void fail(const char* error_message) { - error_message_ = error_message; - } - - Isolate* isolate() const { return isolate_; } - - private: - Isolate* isolate_; - bool is_one_byte_; - const char* error_message_; - - DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis); -}; - - -struct RegExpCompileData { - RegExpCompileData() - : tree(nullptr), - node(nullptr), - simple(true), - contains_anchor(false), - capture_count(0) {} - RegExpTree* tree; - RegExpNode* node; - bool simple; - bool contains_anchor; - Handle capture_name_map; - Handle error; - int capture_count; -}; - - -class RegExpEngine: public AllStatic { - public: - struct CompilationResult { - inline CompilationResult(Isolate* isolate, const char* error_message); - CompilationResult(Object code, int registers) - : code(code), num_registers(registers) {} - const char* const error_message = nullptr; - Object const code; - int const num_registers = 0; - }; - - V8_EXPORT_PRIVATE static CompilationResult Compile( - Isolate* isolate, Zone* zone, RegExpCompileData* input, - JSRegExp::Flags flags, Handle pattern, - Handle sample_subject, bool is_one_byte); - - static bool TooMuchRegExpCode(Isolate* isolate, Handle pattern); - - V8_EXPORT_PRIVATE static void DotPrint(const char* label, RegExpNode* node, - bool ignore_case); -}; - - -class RegExpResultsCache : public AllStatic { - public: - enum ResultsCacheType { REGEXP_MULTIPLE_INDICES, STRING_SPLIT_SUBSTRINGS }; - - // Attempt to retrieve a cached result. On failure, 0 is returned as a Smi. - // On success, the returned result is guaranteed to be a COW-array. - static Object Lookup(Heap* heap, String key_string, Object key_pattern, - FixedArray* last_match_out, ResultsCacheType type); - // Attempt to add value_array to the cache specified by type. On success, - // value_array is turned into a COW-array. - static void Enter(Isolate* isolate, Handle key_string, - Handle key_pattern, Handle value_array, - Handle last_match_cache, ResultsCacheType type); - static void Clear(FixedArray cache); - static const int kRegExpResultsCacheSize = 0x100; - - private: - static const int kArrayEntriesPerCacheEntry = 4; - static const int kStringOffset = 0; - static const int kPatternOffset = 1; - static const int kArrayOffset = 2; - static const int kLastMatchOffset = 3; -}; - -} // namespace internal -} // namespace v8 - -#endif // V8_REGEXP_JSREGEXP_H_ diff --git a/chromium/v8/src/regexp/regexp-ast.h b/chromium/v8/src/regexp/regexp-ast.h index 1fa9f7a35b2..aab67cad154 100644 --- a/chromium/v8/src/regexp/regexp-ast.h +++ b/chromium/v8/src/regexp/regexp-ast.h @@ -50,7 +50,7 @@ class RegExpVisitor { // A simple closed interval. class Interval { public: - Interval() : from_(kNone), to_(kNone) {} + Interval() : from_(kNone), to_(kNone - 1) {} // '- 1' for branchless size(). Interval(int from, int to) : from_(from), to_(to) {} Interval Union(Interval that) { if (that.from_ == kNone) @@ -60,12 +60,16 @@ class Interval { else return Interval(Min(from_, that.from_), Max(to_, that.to_)); } + bool Contains(int value) { return (from_ <= value) && (value <= to_); } bool is_empty() { return from_ == kNone; } int from() const { return from_; } int to() const { return to_; } + int size() const { return to_ - from_ + 1; } + static Interval Empty() { return Interval(); } - static const int kNone = -1; + + static constexpr int kNone = -1; private: int from_; @@ -268,12 +272,13 @@ class RegExpAlternative final : public RegExpTree { class RegExpAssertion final : public RegExpTree { public: enum AssertionType { - START_OF_LINE, - START_OF_INPUT, - END_OF_LINE, - END_OF_INPUT, - BOUNDARY, - NON_BOUNDARY + START_OF_LINE = 0, + START_OF_INPUT = 1, + END_OF_LINE = 2, + END_OF_INPUT = 3, + BOUNDARY = 4, + NON_BOUNDARY = 5, + LAST_TYPE = NON_BOUNDARY, }; RegExpAssertion(AssertionType type, JSRegExp::Flags flags) : assertion_type_(type), flags_(flags) {} @@ -285,7 +290,8 @@ class RegExpAssertion final : public RegExpTree { bool IsAnchoredAtEnd() override; int min_match() override { return 0; } int max_match() override { return 0; } - AssertionType assertion_type() { return assertion_type_; } + AssertionType assertion_type() const { return assertion_type_; } + JSRegExp::Flags flags() const { return flags_; } private: const AssertionType assertion_type_; diff --git a/chromium/v8/src/regexp/regexp-bytecode-generator-inl.h b/chromium/v8/src/regexp/regexp-bytecode-generator-inl.h new file mode 100644 index 00000000000..bd906fea153 --- /dev/null +++ b/chromium/v8/src/regexp/regexp-bytecode-generator-inl.h @@ -0,0 +1,56 @@ +// Copyright 2008-2009 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_REGEXP_REGEXP_BYTECODE_GENERATOR_INL_H_ +#define V8_REGEXP_REGEXP_BYTECODE_GENERATOR_INL_H_ + +#include "src/regexp/regexp-bytecode-generator.h" + +#include "src/ast/ast.h" +#include "src/regexp/regexp-bytecodes.h" + +namespace v8 { +namespace internal { + +void RegExpBytecodeGenerator::Emit(uint32_t byte, uint32_t twenty_four_bits) { + uint32_t word = ((twenty_four_bits << BYTECODE_SHIFT) | byte); + DCHECK(pc_ <= buffer_.length()); + if (pc_ + 3 >= buffer_.length()) { + Expand(); + } + *reinterpret_cast(buffer_.begin() + pc_) = word; + pc_ += 4; +} + +void RegExpBytecodeGenerator::Emit16(uint32_t word) { + DCHECK(pc_ <= buffer_.length()); + if (pc_ + 1 >= buffer_.length()) { + Expand(); + } + *reinterpret_cast(buffer_.begin() + pc_) = word; + pc_ += 2; +} + +void RegExpBytecodeGenerator::Emit8(uint32_t word) { + DCHECK(pc_ <= buffer_.length()); + if (pc_ == buffer_.length()) { + Expand(); + } + *reinterpret_cast(buffer_.begin() + pc_) = word; + pc_ += 1; +} + +void RegExpBytecodeGenerator::Emit32(uint32_t word) { + DCHECK(pc_ <= buffer_.length()); + if (pc_ + 3 >= buffer_.length()) { + Expand(); + } + *reinterpret_cast(buffer_.begin() + pc_) = word; + pc_ += 4; +} + +} // namespace internal +} // namespace v8 + +#endif // V8_REGEXP_REGEXP_BYTECODE_GENERATOR_INL_H_ diff --git a/chromium/v8/src/regexp/regexp-bytecode-generator.cc b/chromium/v8/src/regexp/regexp-bytecode-generator.cc new file mode 100644 index 00000000000..ee3b4015d51 --- /dev/null +++ b/chromium/v8/src/regexp/regexp-bytecode-generator.cc @@ -0,0 +1,378 @@ +// Copyright 2008-2009 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "src/regexp/regexp-bytecode-generator.h" + +#include "src/ast/ast.h" +#include "src/objects/objects-inl.h" +#include "src/regexp/regexp-bytecode-generator-inl.h" +#include "src/regexp/regexp-bytecodes.h" +#include "src/regexp/regexp-macro-assembler.h" + +namespace v8 { +namespace internal { + +RegExpBytecodeGenerator::RegExpBytecodeGenerator(Isolate* isolate, Zone* zone) + : RegExpMacroAssembler(isolate, zone), + buffer_(Vector::New(1024)), + pc_(0), + advance_current_end_(kInvalidPC), + isolate_(isolate) {} + +RegExpBytecodeGenerator::~RegExpBytecodeGenerator() { + if (backtrack_.is_linked()) backtrack_.Unuse(); + buffer_.Dispose(); +} + +RegExpBytecodeGenerator::IrregexpImplementation +RegExpBytecodeGenerator::Implementation() { + return kBytecodeImplementation; +} + +void RegExpBytecodeGenerator::Bind(Label* l) { + advance_current_end_ = kInvalidPC; + DCHECK(!l->is_bound()); + if (l->is_linked()) { + int pos = l->pos(); + while (pos != 0) { + int fixup = pos; + pos = *reinterpret_cast(buffer_.begin() + fixup); + *reinterpret_cast(buffer_.begin() + fixup) = pc_; + } + } + l->bind_to(pc_); +} + +void RegExpBytecodeGenerator::EmitOrLink(Label* l) { + if (l == nullptr) l = &backtrack_; + if (l->is_bound()) { + Emit32(l->pos()); + } else { + int pos = 0; + if (l->is_linked()) { + pos = l->pos(); + } + l->link_to(pc_); + Emit32(pos); + } +} + +void RegExpBytecodeGenerator::PopRegister(int register_index) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_POP_REGISTER, register_index); +} + +void RegExpBytecodeGenerator::PushRegister(int register_index, + StackCheckFlag check_stack_limit) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_PUSH_REGISTER, register_index); +} + +void RegExpBytecodeGenerator::WriteCurrentPositionToRegister(int register_index, + int cp_offset) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_SET_REGISTER_TO_CP, register_index); + Emit32(cp_offset); // Current position offset. +} + +void RegExpBytecodeGenerator::ClearRegisters(int reg_from, int reg_to) { + DCHECK(reg_from <= reg_to); + for (int reg = reg_from; reg <= reg_to; reg++) { + SetRegister(reg, -1); + } +} + +void RegExpBytecodeGenerator::ReadCurrentPositionFromRegister( + int register_index) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_SET_CP_TO_REGISTER, register_index); +} + +void RegExpBytecodeGenerator::WriteStackPointerToRegister(int register_index) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_SET_REGISTER_TO_SP, register_index); +} + +void RegExpBytecodeGenerator::ReadStackPointerFromRegister(int register_index) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_SET_SP_TO_REGISTER, register_index); +} + +void RegExpBytecodeGenerator::SetCurrentPositionFromEnd(int by) { + DCHECK(is_uint24(by)); + Emit(BC_SET_CURRENT_POSITION_FROM_END, by); +} + +void RegExpBytecodeGenerator::SetRegister(int register_index, int to) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_SET_REGISTER, register_index); + Emit32(to); +} + +void RegExpBytecodeGenerator::AdvanceRegister(int register_index, int by) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_ADVANCE_REGISTER, register_index); + Emit32(by); +} + +void RegExpBytecodeGenerator::PopCurrentPosition() { Emit(BC_POP_CP, 0); } + +void RegExpBytecodeGenerator::PushCurrentPosition() { Emit(BC_PUSH_CP, 0); } + +void RegExpBytecodeGenerator::Backtrack() { Emit(BC_POP_BT, 0); } + +void RegExpBytecodeGenerator::GoTo(Label* l) { + if (advance_current_end_ == pc_) { + // Combine advance current and goto. + pc_ = advance_current_start_; + Emit(BC_ADVANCE_CP_AND_GOTO, advance_current_offset_); + EmitOrLink(l); + advance_current_end_ = kInvalidPC; + } else { + // Regular goto. + Emit(BC_GOTO, 0); + EmitOrLink(l); + } +} + +void RegExpBytecodeGenerator::PushBacktrack(Label* l) { + Emit(BC_PUSH_BT, 0); + EmitOrLink(l); +} + +bool RegExpBytecodeGenerator::Succeed() { + Emit(BC_SUCCEED, 0); + return false; // Restart matching for global regexp not supported. +} + +void RegExpBytecodeGenerator::Fail() { Emit(BC_FAIL, 0); } + +void RegExpBytecodeGenerator::AdvanceCurrentPosition(int by) { + DCHECK_LE(kMinCPOffset, by); + DCHECK_GE(kMaxCPOffset, by); + advance_current_start_ = pc_; + advance_current_offset_ = by; + Emit(BC_ADVANCE_CP, by); + advance_current_end_ = pc_; +} + +void RegExpBytecodeGenerator::CheckGreedyLoop( + Label* on_tos_equals_current_position) { + Emit(BC_CHECK_GREEDY, 0); + EmitOrLink(on_tos_equals_current_position); +} + +void RegExpBytecodeGenerator::LoadCurrentCharacter(int cp_offset, + Label* on_failure, + bool check_bounds, + int characters) { + DCHECK_LE(kMinCPOffset, cp_offset); + DCHECK_GE(kMaxCPOffset, cp_offset); + int bytecode; + if (check_bounds) { + if (characters == 4) { + bytecode = BC_LOAD_4_CURRENT_CHARS; + } else if (characters == 2) { + bytecode = BC_LOAD_2_CURRENT_CHARS; + } else { + DCHECK_EQ(1, characters); + bytecode = BC_LOAD_CURRENT_CHAR; + } + } else { + if (characters == 4) { + bytecode = BC_LOAD_4_CURRENT_CHARS_UNCHECKED; + } else if (characters == 2) { + bytecode = BC_LOAD_2_CURRENT_CHARS_UNCHECKED; + } else { + DCHECK_EQ(1, characters); + bytecode = BC_LOAD_CURRENT_CHAR_UNCHECKED; + } + } + Emit(bytecode, cp_offset); + if (check_bounds) EmitOrLink(on_failure); +} + +void RegExpBytecodeGenerator::CheckCharacterLT(uc16 limit, Label* on_less) { + Emit(BC_CHECK_LT, limit); + EmitOrLink(on_less); +} + +void RegExpBytecodeGenerator::CheckCharacterGT(uc16 limit, Label* on_greater) { + Emit(BC_CHECK_GT, limit); + EmitOrLink(on_greater); +} + +void RegExpBytecodeGenerator::CheckCharacter(uint32_t c, Label* on_equal) { + if (c > MAX_FIRST_ARG) { + Emit(BC_CHECK_4_CHARS, 0); + Emit32(c); + } else { + Emit(BC_CHECK_CHAR, c); + } + EmitOrLink(on_equal); +} + +void RegExpBytecodeGenerator::CheckAtStart(Label* on_at_start) { + Emit(BC_CHECK_AT_START, 0); + EmitOrLink(on_at_start); +} + +void RegExpBytecodeGenerator::CheckNotAtStart(int cp_offset, + Label* on_not_at_start) { + Emit(BC_CHECK_NOT_AT_START, cp_offset); + EmitOrLink(on_not_at_start); +} + +void RegExpBytecodeGenerator::CheckNotCharacter(uint32_t c, + Label* on_not_equal) { + if (c > MAX_FIRST_ARG) { + Emit(BC_CHECK_NOT_4_CHARS, 0); + Emit32(c); + } else { + Emit(BC_CHECK_NOT_CHAR, c); + } + EmitOrLink(on_not_equal); +} + +void RegExpBytecodeGenerator::CheckCharacterAfterAnd(uint32_t c, uint32_t mask, + Label* on_equal) { + if (c > MAX_FIRST_ARG) { + Emit(BC_AND_CHECK_4_CHARS, 0); + Emit32(c); + } else { + Emit(BC_AND_CHECK_CHAR, c); + } + Emit32(mask); + EmitOrLink(on_equal); +} + +void RegExpBytecodeGenerator::CheckNotCharacterAfterAnd(uint32_t c, + uint32_t mask, + Label* on_not_equal) { + if (c > MAX_FIRST_ARG) { + Emit(BC_AND_CHECK_NOT_4_CHARS, 0); + Emit32(c); + } else { + Emit(BC_AND_CHECK_NOT_CHAR, c); + } + Emit32(mask); + EmitOrLink(on_not_equal); +} + +void RegExpBytecodeGenerator::CheckNotCharacterAfterMinusAnd( + uc16 c, uc16 minus, uc16 mask, Label* on_not_equal) { + Emit(BC_MINUS_AND_CHECK_NOT_CHAR, c); + Emit16(minus); + Emit16(mask); + EmitOrLink(on_not_equal); +} + +void RegExpBytecodeGenerator::CheckCharacterInRange(uc16 from, uc16 to, + Label* on_in_range) { + Emit(BC_CHECK_CHAR_IN_RANGE, 0); + Emit16(from); + Emit16(to); + EmitOrLink(on_in_range); +} + +void RegExpBytecodeGenerator::CheckCharacterNotInRange(uc16 from, uc16 to, + Label* on_not_in_range) { + Emit(BC_CHECK_CHAR_NOT_IN_RANGE, 0); + Emit16(from); + Emit16(to); + EmitOrLink(on_not_in_range); +} + +void RegExpBytecodeGenerator::CheckBitInTable(Handle table, + Label* on_bit_set) { + Emit(BC_CHECK_BIT_IN_TABLE, 0); + EmitOrLink(on_bit_set); + for (int i = 0; i < kTableSize; i += kBitsPerByte) { + int byte = 0; + for (int j = 0; j < kBitsPerByte; j++) { + if (table->get(i + j) != 0) byte |= 1 << j; + } + Emit8(byte); + } +} + +void RegExpBytecodeGenerator::CheckNotBackReference(int start_reg, + bool read_backward, + Label* on_not_equal) { + DCHECK_LE(0, start_reg); + DCHECK_GE(kMaxRegister, start_reg); + Emit(read_backward ? BC_CHECK_NOT_BACK_REF_BACKWARD : BC_CHECK_NOT_BACK_REF, + start_reg); + EmitOrLink(on_not_equal); +} + +void RegExpBytecodeGenerator::CheckNotBackReferenceIgnoreCase( + int start_reg, bool read_backward, bool unicode, Label* on_not_equal) { + DCHECK_LE(0, start_reg); + DCHECK_GE(kMaxRegister, start_reg); + Emit(read_backward ? (unicode ? BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD + : BC_CHECK_NOT_BACK_REF_NO_CASE_BACKWARD) + : (unicode ? BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE + : BC_CHECK_NOT_BACK_REF_NO_CASE), + start_reg); + EmitOrLink(on_not_equal); +} + +void RegExpBytecodeGenerator::IfRegisterLT(int register_index, int comparand, + Label* on_less_than) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_CHECK_REGISTER_LT, register_index); + Emit32(comparand); + EmitOrLink(on_less_than); +} + +void RegExpBytecodeGenerator::IfRegisterGE(int register_index, int comparand, + Label* on_greater_or_equal) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_CHECK_REGISTER_GE, register_index); + Emit32(comparand); + EmitOrLink(on_greater_or_equal); +} + +void RegExpBytecodeGenerator::IfRegisterEqPos(int register_index, + Label* on_eq) { + DCHECK_LE(0, register_index); + DCHECK_GE(kMaxRegister, register_index); + Emit(BC_CHECK_REGISTER_EQ_POS, register_index); + EmitOrLink(on_eq); +} + +Handle RegExpBytecodeGenerator::GetCode(Handle source) { + Bind(&backtrack_); + Emit(BC_POP_BT, 0); + Handle array = isolate_->factory()->NewByteArray(length()); + Copy(array->GetDataStartAddress()); + return array; +} + +int RegExpBytecodeGenerator::length() { return pc_; } + +void RegExpBytecodeGenerator::Copy(byte* a) { + MemCopy(a, buffer_.begin(), length()); +} + +void RegExpBytecodeGenerator::Expand() { + Vector old_buffer = buffer_; + buffer_ = Vector::New(old_buffer.length() * 2); + MemCopy(buffer_.begin(), old_buffer.begin(), old_buffer.length()); + old_buffer.Dispose(); +} + +} // namespace internal +} // namespace v8 diff --git a/chromium/v8/src/regexp/regexp-bytecode-generator.h b/chromium/v8/src/regexp/regexp-bytecode-generator.h new file mode 100644 index 00000000000..b7207e977c8 --- /dev/null +++ b/chromium/v8/src/regexp/regexp-bytecode-generator.h @@ -0,0 +1,113 @@ +// Copyright 2012 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_REGEXP_REGEXP_BYTECODE_GENERATOR_H_ +#define V8_REGEXP_REGEXP_BYTECODE_GENERATOR_H_ + +#include "src/regexp/regexp-macro-assembler.h" + +namespace v8 { +namespace internal { + +// An assembler/generator for the Irregexp byte code. +class V8_EXPORT_PRIVATE RegExpBytecodeGenerator : public RegExpMacroAssembler { + public: + // Create an assembler. Instructions and relocation information are emitted + // into a buffer, with the instructions starting from the beginning and the + // relocation information starting from the end of the buffer. See CodeDesc + // for a detailed comment on the layout (globals.h). + // + // The assembler allocates and grows its own buffer, and buffer_size + // determines the initial buffer size. The buffer is owned by the assembler + // and deallocated upon destruction of the assembler. + RegExpBytecodeGenerator(Isolate* isolate, Zone* zone); + virtual ~RegExpBytecodeGenerator(); + // The byte-code interpreter checks on each push anyway. + virtual int stack_limit_slack() { return 1; } + virtual bool CanReadUnaligned() { return false; } + virtual void Bind(Label* label); + virtual void AdvanceCurrentPosition(int by); // Signed cp change. + virtual void PopCurrentPosition(); + virtual void PushCurrentPosition(); + virtual void Backtrack(); + virtual void GoTo(Label* label); + virtual void PushBacktrack(Label* label); + virtual bool Succeed(); + virtual void Fail(); + virtual void PopRegister(int register_index); + virtual void PushRegister(int register_index, + StackCheckFlag check_stack_limit); + virtual void AdvanceRegister(int reg, int by); // r[reg] += by. + virtual void SetCurrentPositionFromEnd(int by); + virtual void SetRegister(int register_index, int to); + virtual void WriteCurrentPositionToRegister(int reg, int cp_offset); + virtual void ClearRegisters(int reg_from, int reg_to); + virtual void ReadCurrentPositionFromRegister(int reg); + virtual void WriteStackPointerToRegister(int reg); + virtual void ReadStackPointerFromRegister(int reg); + virtual void LoadCurrentCharacter(int cp_offset, Label* on_end_of_input, + bool check_bounds = true, + int characters = 1); + virtual void CheckCharacter(unsigned c, Label* on_equal); + virtual void CheckCharacterAfterAnd(unsigned c, unsigned mask, + Label* on_equal); + virtual void CheckCharacterGT(uc16 limit, Label* on_greater); + virtual void CheckCharacterLT(uc16 limit, Label* on_less); + virtual void CheckGreedyLoop(Label* on_tos_equals_current_position); + virtual void CheckAtStart(Label* on_at_start); + virtual void CheckNotAtStart(int cp_offset, Label* on_not_at_start); + virtual void CheckNotCharacter(unsigned c, Label* on_not_equal); + virtual void CheckNotCharacterAfterAnd(unsigned c, unsigned mask, + Label* on_not_equal); + virtual void CheckNotCharacterAfterMinusAnd(uc16 c, uc16 minus, uc16 mask, + Label* on_not_equal); + virtual void CheckCharacterInRange(uc16 from, uc16 to, Label* on_in_range); + virtual void CheckCharacterNotInRange(uc16 from, uc16 to, + Label* on_not_in_range); + virtual void CheckBitInTable(Handle table, Label* on_bit_set); + virtual void CheckNotBackReference(int start_reg, bool read_backward, + Label* on_no_match); + virtual void CheckNotBackReferenceIgnoreCase(int start_reg, + bool read_backward, bool unicode, + Label* on_no_match); + virtual void IfRegisterLT(int register_index, int comparand, Label* if_lt); + virtual void IfRegisterGE(int register_index, int comparand, Label* if_ge); + virtual void IfRegisterEqPos(int register_index, Label* if_eq); + + virtual IrregexpImplementation Implementation(); + virtual Handle GetCode(Handle source); + + private: + void Expand(); + // Code and bitmap emission. + inline void EmitOrLink(Label* label); + inline void Emit32(uint32_t x); + inline void Emit16(uint32_t x); + inline void Emit8(uint32_t x); + inline void Emit(uint32_t bc, uint32_t arg); + // Bytecode buffer. + int length(); + void Copy(byte* a); + + // The buffer into which code and relocation info are generated. + Vector buffer_; + // The program counter. + int pc_; + Label backtrack_; + + int advance_current_start_; + int advance_current_offset_; + int advance_current_end_; + + Isolate* isolate_; + + static const int kInvalidPC = -1; + + DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpBytecodeGenerator); +}; + +} // namespace internal +} // namespace v8 + +#endif // V8_REGEXP_REGEXP_BYTECODE_GENERATOR_H_ diff --git a/chromium/v8/src/regexp/regexp-bytecodes.h b/chromium/v8/src/regexp/regexp-bytecodes.h new file mode 100644 index 00000000000..8b1468c1bfa --- /dev/null +++ b/chromium/v8/src/regexp/regexp-bytecodes.h @@ -0,0 +1,84 @@ +// Copyright 2011 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_REGEXP_REGEXP_BYTECODES_H_ +#define V8_REGEXP_REGEXP_BYTECODES_H_ + +namespace v8 { +namespace internal { + +const int BYTECODE_MASK = 0xff; +// The first argument is packed in with the byte code in one word, but so it +// has 24 bits, but it can be positive and negative so only use 23 bits for +// positive values. +const unsigned int MAX_FIRST_ARG = 0x7fffffu; +const int BYTECODE_SHIFT = 8; + +#define BYTECODE_ITERATOR(V) \ + V(BREAK, 0, 4) /* bc8 */ \ + V(PUSH_CP, 1, 4) /* bc8 pad24 */ \ + V(PUSH_BT, 2, 8) /* bc8 pad24 offset32 */ \ + V(PUSH_REGISTER, 3, 4) /* bc8 reg_idx24 */ \ + V(SET_REGISTER_TO_CP, 4, 8) /* bc8 reg_idx24 offset32 */ \ + V(SET_CP_TO_REGISTER, 5, 4) /* bc8 reg_idx24 */ \ + V(SET_REGISTER_TO_SP, 6, 4) /* bc8 reg_idx24 */ \ + V(SET_SP_TO_REGISTER, 7, 4) /* bc8 reg_idx24 */ \ + V(SET_REGISTER, 8, 8) /* bc8 reg_idx24 value32 */ \ + V(ADVANCE_REGISTER, 9, 8) /* bc8 reg_idx24 value32 */ \ + V(POP_CP, 10, 4) /* bc8 pad24 */ \ + V(POP_BT, 11, 4) /* bc8 pad24 */ \ + V(POP_REGISTER, 12, 4) /* bc8 reg_idx24 */ \ + V(FAIL, 13, 4) /* bc8 pad24 */ \ + V(SUCCEED, 14, 4) /* bc8 pad24 */ \ + V(ADVANCE_CP, 15, 4) /* bc8 offset24 */ \ + V(GOTO, 16, 8) /* bc8 pad24 addr32 */ \ + V(LOAD_CURRENT_CHAR, 17, 8) /* bc8 offset24 addr32 */ \ + V(LOAD_CURRENT_CHAR_UNCHECKED, 18, 4) /* bc8 offset24 */ \ + V(LOAD_2_CURRENT_CHARS, 19, 8) /* bc8 offset24 addr32 */ \ + V(LOAD_2_CURRENT_CHARS_UNCHECKED, 20, 4) /* bc8 offset24 */ \ + V(LOAD_4_CURRENT_CHARS, 21, 8) /* bc8 offset24 addr32 */ \ + V(LOAD_4_CURRENT_CHARS_UNCHECKED, 22, 4) /* bc8 offset24 */ \ + V(CHECK_4_CHARS, 23, 12) /* bc8 pad24 uint32 addr32 */ \ + V(CHECK_CHAR, 24, 8) /* bc8 pad8 uint16 addr32 */ \ + V(CHECK_NOT_4_CHARS, 25, 12) /* bc8 pad24 uint32 addr32 */ \ + V(CHECK_NOT_CHAR, 26, 8) /* bc8 pad8 uint16 addr32 */ \ + V(AND_CHECK_4_CHARS, 27, 16) /* bc8 pad24 uint32 uint32 addr32 */ \ + V(AND_CHECK_CHAR, 28, 12) /* bc8 pad8 uint16 uint32 addr32 */ \ + V(AND_CHECK_NOT_4_CHARS, 29, 16) /* bc8 pad24 uint32 uint32 addr32 */ \ + V(AND_CHECK_NOT_CHAR, 30, 12) /* bc8 pad8 uint16 uint32 addr32 */ \ + V(MINUS_AND_CHECK_NOT_CHAR, 31, 12) /* bc8 pad8 uc16 uc16 uc16 addr32 */ \ + V(CHECK_CHAR_IN_RANGE, 32, 12) /* bc8 pad24 uc16 uc16 addr32 */ \ + V(CHECK_CHAR_NOT_IN_RANGE, 33, 12) /* bc8 pad24 uc16 uc16 addr32 */ \ + V(CHECK_BIT_IN_TABLE, 34, 24) /* bc8 pad24 addr32 bits128 */ \ + V(CHECK_LT, 35, 8) /* bc8 pad8 uc16 addr32 */ \ + V(CHECK_GT, 36, 8) /* bc8 pad8 uc16 addr32 */ \ + V(CHECK_NOT_BACK_REF, 37, 8) /* bc8 reg_idx24 addr32 */ \ + V(CHECK_NOT_BACK_REF_NO_CASE, 38, 8) /* bc8 reg_idx24 addr32 */ \ + V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE, 39, 8) \ + V(CHECK_NOT_BACK_REF_BACKWARD, 40, 8) /* bc8 reg_idx24 addr32 */ \ + V(CHECK_NOT_BACK_REF_NO_CASE_BACKWARD, 41, 8) /* bc8 reg_idx24 addr32 */ \ + V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD, 42, 8) \ + V(CHECK_NOT_REGS_EQUAL, 43, 12) /* bc8 regidx24 reg_idx32 addr32 */ \ + V(CHECK_REGISTER_LT, 44, 12) /* bc8 reg_idx24 value32 addr32 */ \ + V(CHECK_REGISTER_GE, 45, 12) /* bc8 reg_idx24 value32 addr32 */ \ + V(CHECK_REGISTER_EQ_POS, 46, 8) /* bc8 reg_idx24 addr32 */ \ + V(CHECK_AT_START, 47, 8) /* bc8 pad24 addr32 */ \ + V(CHECK_NOT_AT_START, 48, 8) /* bc8 offset24 addr32 */ \ + V(CHECK_GREEDY, 49, 8) /* bc8 pad24 addr32 */ \ + V(ADVANCE_CP_AND_GOTO, 50, 8) /* bc8 offset24 addr32 */ \ + V(SET_CURRENT_POSITION_FROM_END, 51, 4) /* bc8 idx24 */ + +#define DECLARE_BYTECODES(name, code, length) static const int BC_##name = code; +BYTECODE_ITERATOR(DECLARE_BYTECODES) +#undef DECLARE_BYTECODES + +#define DECLARE_BYTECODE_LENGTH(name, code, length) \ + static const int BC_##name##_LENGTH = length; +BYTECODE_ITERATOR(DECLARE_BYTECODE_LENGTH) +#undef DECLARE_BYTECODE_LENGTH + +} // namespace internal +} // namespace v8 + +#endif // V8_REGEXP_REGEXP_BYTECODES_H_ diff --git a/chromium/v8/src/regexp/regexp-compiler-tonode.cc b/chromium/v8/src/regexp/regexp-compiler-tonode.cc new file mode 100644 index 00000000000..d12c35682e8 --- /dev/null +++ b/chromium/v8/src/regexp/regexp-compiler-tonode.cc @@ -0,0 +1,1678 @@ +// Copyright 2019 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "src/regexp/regexp-compiler.h" + +#include "src/execution/isolate.h" +#include "src/regexp/regexp.h" +#ifdef V8_INTL_SUPPORT +#include "src/regexp/special-case.h" +#endif // V8_INTL_SUPPORT +#include "src/strings/unicode-inl.h" +#include "src/zone/zone-list-inl.h" + +#ifdef V8_INTL_SUPPORT +#include "unicode/locid.h" +#include "unicode/uniset.h" +#include "unicode/utypes.h" +#endif // V8_INTL_SUPPORT + +namespace v8 { +namespace internal { + +using namespace regexp_compiler_constants; // NOLINT(build/namespaces) + +// ------------------------------------------------------------------- +// Tree to graph conversion + +RegExpNode* RegExpAtom::ToNode(RegExpCompiler* compiler, + RegExpNode* on_success) { + ZoneList* elms = + new (compiler->zone()) ZoneList(1, compiler->zone()); + elms->Add(TextElement::Atom(this), compiler->zone()); + return new (compiler->zone()) + TextNode(elms, compiler->read_backward(), on_success); +} + +RegExpNode* RegExpText::ToNode(RegExpCompiler* compiler, + RegExpNode* on_success) { + return new (compiler->zone()) + TextNode(elements(), compiler->read_backward(), on_success); +} + +static bool CompareInverseRanges(ZoneList* ranges, + const int* special_class, int length) { + length--; // Remove final marker. + DCHECK_EQ(kRangeEndMarker, special_class[length]); + DCHECK_NE(0, ranges->length()); + DCHECK_NE(0, length); + DCHECK_NE(0, special_class[0]); + if (ranges->length() != (length >> 1) + 1) { + return false; + } + CharacterRange range = ranges->at(0); + if (range.from() != 0) { + return false; + } + for (int i = 0; i < length; i += 2) { + if (special_class[i] != (range.to() + 1)) { + return false; + } + range = ranges->at((i >> 1) + 1); + if (special_class[i + 1] != range.from()) { + return false; + } + } + if (range.to() != String::kMaxCodePoint) { + return false; + } + return true; +} + +static bool CompareRanges(ZoneList* ranges, + const int* special_class, int length) { + length--; // Remove final marker. + DCHECK_EQ(kRangeEndMarker, special_class[length]); + if (ranges->length() * 2 != length) { + return false; + } + for (int i = 0; i < length; i += 2) { + CharacterRange range = ranges->at(i >> 1); + if (range.from() != special_class[i] || + range.to() != special_class[i + 1] - 1) { + return false; + } + } + return true; +} + +bool RegExpCharacterClass::is_standard(Zone* zone) { + // 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(zone), kSpaceRanges, kSpaceRangeCount)) { + set_.set_standard_set_type('s'); + return true; + } + if (CompareInverseRanges(set_.ranges(zone), kSpaceRanges, kSpaceRangeCount)) { + set_.set_standard_set_type('S'); + return true; + } + if (CompareInverseRanges(set_.ranges(zone), kLineTerminatorRanges, + kLineTerminatorRangeCount)) { + set_.set_standard_set_type('.'); + return true; + } + if (CompareRanges(set_.ranges(zone), kLineTerminatorRanges, + kLineTerminatorRangeCount)) { + set_.set_standard_set_type('n'); + return true; + } + if (CompareRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) { + set_.set_standard_set_type('w'); + return true; + } + if (CompareInverseRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) { + set_.set_standard_set_type('W'); + return true; + } + return false; +} + +UnicodeRangeSplitter::UnicodeRangeSplitter(ZoneList* base) { + // The unicode range splitter categorizes given character ranges into: + // - Code points from the BMP representable by one code unit. + // - Code points outside the BMP that need to be split into surrogate pairs. + // - Lone lead surrogates. + // - Lone trail surrogates. + // Lone surrogates are valid code points, even though no actual characters. + // They require special matching to make sure we do not split surrogate pairs. + + for (int i = 0; i < base->length(); i++) AddRange(base->at(i)); +} + +void UnicodeRangeSplitter::AddRange(CharacterRange range) { + static constexpr uc32 kBmp1Start = 0; + static constexpr uc32 kBmp1End = kLeadSurrogateStart - 1; + static constexpr uc32 kBmp2Start = kTrailSurrogateEnd + 1; + static constexpr uc32 kBmp2End = kNonBmpStart - 1; + + // Ends are all inclusive. + STATIC_ASSERT(kBmp1Start == 0); + STATIC_ASSERT(kBmp1Start < kBmp1End); + STATIC_ASSERT(kBmp1End + 1 == kLeadSurrogateStart); + STATIC_ASSERT(kLeadSurrogateStart < kLeadSurrogateEnd); + STATIC_ASSERT(kLeadSurrogateEnd + 1 == kTrailSurrogateStart); + STATIC_ASSERT(kTrailSurrogateStart < kTrailSurrogateEnd); + STATIC_ASSERT(kTrailSurrogateEnd + 1 == kBmp2Start); + STATIC_ASSERT(kBmp2Start < kBmp2End); + STATIC_ASSERT(kBmp2End + 1 == kNonBmpStart); + STATIC_ASSERT(kNonBmpStart < kNonBmpEnd); + + static constexpr uc32 kStarts[] = { + kBmp1Start, kLeadSurrogateStart, kTrailSurrogateStart, + kBmp2Start, kNonBmpStart, + }; + + static constexpr uc32 kEnds[] = { + kBmp1End, kLeadSurrogateEnd, kTrailSurrogateEnd, kBmp2End, kNonBmpEnd, + }; + + CharacterRangeVector* const kTargets[] = { + &bmp_, &lead_surrogates_, &trail_surrogates_, &bmp_, &non_bmp_, + }; + + static constexpr int kCount = arraysize(kStarts); + STATIC_ASSERT(kCount == arraysize(kEnds)); + STATIC_ASSERT(kCount == arraysize(kTargets)); + + for (int i = 0; i < kCount; i++) { + if (kStarts[i] > range.to()) break; + const uc32 from = std::max(kStarts[i], range.from()); + const uc32 to = std::min(kEnds[i], range.to()); + if (from > to) continue; + kTargets[i]->emplace_back(CharacterRange::Range(from, to)); + } +} + +namespace { + +// Translates between new and old V8-isms (SmallVector, ZoneList). +ZoneList* ToCanonicalZoneList( + const UnicodeRangeSplitter::CharacterRangeVector* v, Zone* zone) { + if (v->empty()) return nullptr; + + ZoneList* result = + new (zone) ZoneList(static_cast(v->size()), zone); + for (size_t i = 0; i < v->size(); i++) { + result->Add(v->at(i), zone); + } + + CharacterRange::Canonicalize(result); + return result; +} + +void AddBmpCharacters(RegExpCompiler* compiler, ChoiceNode* result, + RegExpNode* on_success, UnicodeRangeSplitter* splitter) { + ZoneList* bmp = + ToCanonicalZoneList(splitter->bmp(), compiler->zone()); + if (bmp == nullptr) return; + JSRegExp::Flags default_flags = JSRegExp::Flags(); + result->AddAlternative(GuardedAlternative(TextNode::CreateForCharacterRanges( + compiler->zone(), bmp, compiler->read_backward(), on_success, + default_flags))); +} + +void AddNonBmpSurrogatePairs(RegExpCompiler* compiler, ChoiceNode* result, + RegExpNode* on_success, + UnicodeRangeSplitter* splitter) { + ZoneList* non_bmp = + ToCanonicalZoneList(splitter->non_bmp(), compiler->zone()); + if (non_bmp == nullptr) return; + DCHECK(!compiler->one_byte()); + Zone* zone = compiler->zone(); + JSRegExp::Flags default_flags = JSRegExp::Flags(); + CharacterRange::Canonicalize(non_bmp); + for (int i = 0; i < non_bmp->length(); i++) { + // Match surrogate pair. + // E.g. [\u10005-\u11005] becomes + // \ud800[\udc05-\udfff]| + // [\ud801-\ud803][\udc00-\udfff]| + // \ud804[\udc00-\udc05] + uc32 from = non_bmp->at(i).from(); + uc32 to = non_bmp->at(i).to(); + uc16 from_l = unibrow::Utf16::LeadSurrogate(from); + uc16 from_t = unibrow::Utf16::TrailSurrogate(from); + uc16 to_l = unibrow::Utf16::LeadSurrogate(to); + uc16 to_t = unibrow::Utf16::TrailSurrogate(to); + if (from_l == to_l) { + // The lead surrogate is the same. + result->AddAlternative( + GuardedAlternative(TextNode::CreateForSurrogatePair( + zone, CharacterRange::Singleton(from_l), + CharacterRange::Range(from_t, to_t), compiler->read_backward(), + on_success, default_flags))); + } else { + if (from_t != kTrailSurrogateStart) { + // Add [from_l][from_t-\udfff] + result->AddAlternative( + GuardedAlternative(TextNode::CreateForSurrogatePair( + zone, CharacterRange::Singleton(from_l), + CharacterRange::Range(from_t, kTrailSurrogateEnd), + compiler->read_backward(), on_success, default_flags))); + from_l++; + } + if (to_t != kTrailSurrogateEnd) { + // Add [to_l][\udc00-to_t] + result->AddAlternative( + GuardedAlternative(TextNode::CreateForSurrogatePair( + zone, CharacterRange::Singleton(to_l), + CharacterRange::Range(kTrailSurrogateStart, to_t), + compiler->read_backward(), on_success, default_flags))); + to_l--; + } + if (from_l <= to_l) { + // Add [from_l-to_l][\udc00-\udfff] + result->AddAlternative( + GuardedAlternative(TextNode::CreateForSurrogatePair( + zone, CharacterRange::Range(from_l, to_l), + CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd), + compiler->read_backward(), on_success, default_flags))); + } + } + } +} + +RegExpNode* NegativeLookaroundAgainstReadDirectionAndMatch( + RegExpCompiler* compiler, ZoneList* lookbehind, + ZoneList* match, RegExpNode* on_success, bool read_backward, + JSRegExp::Flags flags) { + Zone* zone = compiler->zone(); + RegExpNode* match_node = TextNode::CreateForCharacterRanges( + zone, match, read_backward, on_success, flags); + int stack_register = compiler->UnicodeLookaroundStackRegister(); + int position_register = compiler->UnicodeLookaroundPositionRegister(); + RegExpLookaround::Builder lookaround(false, match_node, stack_register, + position_register); + RegExpNode* negative_match = TextNode::CreateForCharacterRanges( + zone, lookbehind, !read_backward, lookaround.on_match_success(), flags); + return lookaround.ForMatch(negative_match); +} + +RegExpNode* MatchAndNegativeLookaroundInReadDirection( + RegExpCompiler* compiler, ZoneList* match, + ZoneList* lookahead, RegExpNode* on_success, + bool read_backward, JSRegExp::Flags flags) { + Zone* zone = compiler->zone(); + int stack_register = compiler->UnicodeLookaroundStackRegister(); + int position_register = compiler->UnicodeLookaroundPositionRegister(); + RegExpLookaround::Builder lookaround(false, on_success, stack_register, + position_register); + RegExpNode* negative_match = TextNode::CreateForCharacterRanges( + zone, lookahead, read_backward, lookaround.on_match_success(), flags); + return TextNode::CreateForCharacterRanges( + zone, match, read_backward, lookaround.ForMatch(negative_match), flags); +} + +void AddLoneLeadSurrogates(RegExpCompiler* compiler, ChoiceNode* result, + RegExpNode* on_success, + UnicodeRangeSplitter* splitter) { + JSRegExp::Flags default_flags = JSRegExp::Flags(); + ZoneList* lead_surrogates = + ToCanonicalZoneList(splitter->lead_surrogates(), compiler->zone()); + if (lead_surrogates == nullptr) return; + Zone* zone = compiler->zone(); + // E.g. \ud801 becomes \ud801(?![\udc00-\udfff]). + ZoneList* trail_surrogates = CharacterRange::List( + zone, CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd)); + + RegExpNode* match; + if (compiler->read_backward()) { + // Reading backward. Assert that reading forward, there is no trail + // surrogate, and then backward match the lead surrogate. + match = NegativeLookaroundAgainstReadDirectionAndMatch( + compiler, trail_surrogates, lead_surrogates, on_success, true, + default_flags); + } else { + // Reading forward. Forward match the lead surrogate and assert that + // no trail surrogate follows. + match = MatchAndNegativeLookaroundInReadDirection( + compiler, lead_surrogates, trail_surrogates, on_success, false, + default_flags); + } + result->AddAlternative(GuardedAlternative(match)); +} + +void AddLoneTrailSurrogates(RegExpCompiler* compiler, ChoiceNode* result, + RegExpNode* on_success, + UnicodeRangeSplitter* splitter) { + JSRegExp::Flags default_flags = JSRegExp::Flags(); + ZoneList* trail_surrogates = + ToCanonicalZoneList(splitter->trail_surrogates(), compiler->zone()); + if (trail_surrogates == nullptr) return; + Zone* zone = compiler->zone(); + // E.g. \udc01 becomes (?* lead_surrogates = CharacterRange::List( + zone, CharacterRange::Range(kLeadSurrogateStart, kLeadSurrogateEnd)); + + RegExpNode* match; + if (compiler->read_backward()) { + // Reading backward. Backward match the trail surrogate and assert that no + // lead surrogate precedes it. + match = MatchAndNegativeLookaroundInReadDirection( + compiler, trail_surrogates, lead_surrogates, on_success, true, + default_flags); + } else { + // Reading forward. Assert that reading backward, there is no lead + // surrogate, and then forward match the trail surrogate. + match = NegativeLookaroundAgainstReadDirectionAndMatch( + compiler, lead_surrogates, trail_surrogates, on_success, false, + default_flags); + } + result->AddAlternative(GuardedAlternative(match)); +} + +RegExpNode* UnanchoredAdvance(RegExpCompiler* compiler, + RegExpNode* on_success) { + // This implements ES2015 21.2.5.2.3, AdvanceStringIndex. + DCHECK(!compiler->read_backward()); + Zone* zone = compiler->zone(); + // Advance any character. If the character happens to be a lead surrogate and + // we advanced into the middle of a surrogate pair, it will work out, as + // nothing will match from there. We will have to advance again, consuming + // the associated trail surrogate. + ZoneList* range = CharacterRange::List( + zone, CharacterRange::Range(0, String::kMaxUtf16CodeUnit)); + JSRegExp::Flags default_flags = JSRegExp::Flags(); + return TextNode::CreateForCharacterRanges(zone, range, false, on_success, + default_flags); +} + +void AddUnicodeCaseEquivalents(ZoneList* ranges, Zone* zone) { +#ifdef V8_INTL_SUPPORT + DCHECK(CharacterRange::IsCanonical(ranges)); + + // Micro-optimization to avoid passing large ranges to UnicodeSet::closeOver. + // See also https://crbug.com/v8/6727. + // TODO(jgruber): This only covers the special case of the {0,0x10FFFF} range, + // which we use frequently internally. But large ranges can also easily be + // created by the user. We might want to have a more general caching mechanism + // for such ranges. + if (ranges->length() == 1 && ranges->at(0).IsEverything(kNonBmpEnd)) return; + + // Use ICU to compute the case fold closure over the ranges. + icu::UnicodeSet set; + for (int i = 0; i < ranges->length(); i++) { + set.add(ranges->at(i).from(), ranges->at(i).to()); + } + ranges->Clear(); + set.closeOver(USET_CASE_INSENSITIVE); + // Full case mapping map single characters to multiple characters. + // Those are represented as strings in the set. Remove them so that + // we end up with only simple and common case mappings. + set.removeAllStrings(); + for (int i = 0; i < set.getRangeCount(); i++) { + ranges->Add(CharacterRange::Range(set.getRangeStart(i), set.getRangeEnd(i)), + zone); + } + // No errors and everything we collected have been ranges. + CharacterRange::Canonicalize(ranges); +#endif // V8_INTL_SUPPORT +} + +} // namespace + +RegExpNode* RegExpCharacterClass::ToNode(RegExpCompiler* compiler, + RegExpNode* on_success) { + set_.Canonicalize(); + Zone* zone = compiler->zone(); + ZoneList* ranges = this->ranges(zone); + if (NeedsUnicodeCaseEquivalents(flags_)) { + AddUnicodeCaseEquivalents(ranges, zone); + } + if (IsUnicode(flags_) && !compiler->one_byte() && + !contains_split_surrogate()) { + if (is_negated()) { + ZoneList* negated = + new (zone) ZoneList(2, zone); + CharacterRange::Negate(ranges, negated, zone); + ranges = negated; + } + if (ranges->length() == 0) { + JSRegExp::Flags default_flags; + RegExpCharacterClass* fail = + new (zone) RegExpCharacterClass(zone, ranges, default_flags); + return new (zone) TextNode(fail, compiler->read_backward(), on_success); + } + if (standard_type() == '*') { + return UnanchoredAdvance(compiler, on_success); + } else { + ChoiceNode* result = new (zone) ChoiceNode(2, zone); + UnicodeRangeSplitter splitter(ranges); + AddBmpCharacters(compiler, result, on_success, &splitter); + AddNonBmpSurrogatePairs(compiler, result, on_success, &splitter); + AddLoneLeadSurrogates(compiler, result, on_success, &splitter); + AddLoneTrailSurrogates(compiler, result, on_success, &splitter); + return result; + } + } else { + return new (zone) TextNode(this, compiler->read_backward(), on_success); + } +} + +int CompareFirstChar(RegExpTree* const* a, RegExpTree* const* b) { + RegExpAtom* atom1 = (*a)->AsAtom(); + RegExpAtom* atom2 = (*b)->AsAtom(); + uc16 character1 = atom1->data().at(0); + uc16 character2 = atom2->data().at(0); + if (character1 < character2) return -1; + if (character1 > character2) return 1; + return 0; +} + +#ifdef V8_INTL_SUPPORT + +// Case Insensitve comparesion +int CompareFirstCharCaseInsensitve(RegExpTree* const* a, RegExpTree* const* b) { + RegExpAtom* atom1 = (*a)->AsAtom(); + RegExpAtom* atom2 = (*b)->AsAtom(); + icu::UnicodeString character1(atom1->data().at(0)); + return character1.caseCompare(atom2->data().at(0), U_FOLD_CASE_DEFAULT); +} + +#else + +static unibrow::uchar Canonical( + unibrow::Mapping* canonicalize, + unibrow::uchar c) { + unibrow::uchar chars[unibrow::Ecma262Canonicalize::kMaxWidth]; + int length = canonicalize->get(c, '\0', chars); + DCHECK_LE(length, 1); + unibrow::uchar canonical = c; + if (length == 1) canonical = chars[0]; + return canonical; +} + +int CompareFirstCharCaseIndependent( + unibrow::Mapping* canonicalize, + RegExpTree* const* a, RegExpTree* const* b) { + RegExpAtom* atom1 = (*a)->AsAtom(); + RegExpAtom* atom2 = (*b)->AsAtom(); + unibrow::uchar character1 = atom1->data().at(0); + unibrow::uchar character2 = atom2->data().at(0); + if (character1 == character2) return 0; + if (character1 >= 'a' || character2 >= 'a') { + character1 = Canonical(canonicalize, character1); + character2 = Canonical(canonicalize, character2); + } + return static_cast(character1) - static_cast(character2); +} +#endif // V8_INTL_SUPPORT + +// We can stable sort runs of atoms, since the order does not matter if they +// start with different characters. +// Returns true if any consecutive atoms were found. +bool RegExpDisjunction::SortConsecutiveAtoms(RegExpCompiler* compiler) { + ZoneList* alternatives = this->alternatives(); + int length = alternatives->length(); + bool found_consecutive_atoms = false; + for (int i = 0; i < length; i++) { + while (i < length) { + RegExpTree* alternative = alternatives->at(i); + if (alternative->IsAtom()) break; + i++; + } + // i is length or it is the index of an atom. + if (i == length) break; + int first_atom = i; + JSRegExp::Flags flags = alternatives->at(i)->AsAtom()->flags(); + i++; + while (i < length) { + RegExpTree* alternative = alternatives->at(i); + if (!alternative->IsAtom()) break; + if (alternative->AsAtom()->flags() != flags) break; + i++; + } + // Sort atoms to get ones with common prefixes together. + // This step is more tricky if we are in a case-independent regexp, + // because it would change /is|I/ to /I|is/, and order matters when + // the regexp parts don't match only disjoint starting points. To fix + // this we have a version of CompareFirstChar that uses case- + // independent character classes for comparison. + DCHECK_LT(first_atom, alternatives->length()); + DCHECK_LE(i, alternatives->length()); + DCHECK_LE(first_atom, i); + if (IgnoreCase(flags)) { +#ifdef V8_INTL_SUPPORT + alternatives->StableSort(CompareFirstCharCaseInsensitve, first_atom, + i - first_atom); +#else + unibrow::Mapping* canonicalize = + compiler->isolate()->regexp_macro_assembler_canonicalize(); + auto compare_closure = [canonicalize](RegExpTree* const* a, + RegExpTree* const* b) { + return CompareFirstCharCaseIndependent(canonicalize, a, b); + }; + alternatives->StableSort(compare_closure, first_atom, i - first_atom); +#endif // V8_INTL_SUPPORT + } else { + alternatives->StableSort(CompareFirstChar, first_atom, i - first_atom); + } + if (i - first_atom > 1) found_consecutive_atoms = true; + } + return found_consecutive_atoms; +} + +// Optimizes ab|ac|az to a(?:b|c|d). +void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) { + Zone* zone = compiler->zone(); + ZoneList* alternatives = this->alternatives(); + int length = alternatives->length(); + + int write_posn = 0; + int i = 0; + while (i < length) { + RegExpTree* alternative = alternatives->at(i); + if (!alternative->IsAtom()) { + alternatives->at(write_posn++) = alternatives->at(i); + i++; + continue; + } + RegExpAtom* const atom = alternative->AsAtom(); + JSRegExp::Flags flags = atom->flags(); +#ifdef V8_INTL_SUPPORT + icu::UnicodeString common_prefix(atom->data().at(0)); +#else + unibrow::uchar common_prefix = atom->data().at(0); +#endif // V8_INTL_SUPPORT + int first_with_prefix = i; + int prefix_length = atom->length(); + i++; + while (i < length) { + alternative = alternatives->at(i); + if (!alternative->IsAtom()) break; + RegExpAtom* const atom = alternative->AsAtom(); + if (atom->flags() != flags) break; +#ifdef V8_INTL_SUPPORT + icu::UnicodeString new_prefix(atom->data().at(0)); + if (new_prefix != common_prefix) { + if (!IgnoreCase(flags)) break; + if (common_prefix.caseCompare(new_prefix, U_FOLD_CASE_DEFAULT) != 0) + break; + } +#else + unibrow::uchar new_prefix = atom->data().at(0); + if (new_prefix != common_prefix) { + if (!IgnoreCase(flags)) break; + unibrow::Mapping* canonicalize = + compiler->isolate()->regexp_macro_assembler_canonicalize(); + new_prefix = Canonical(canonicalize, new_prefix); + common_prefix = Canonical(canonicalize, common_prefix); + if (new_prefix != common_prefix) break; + } +#endif // V8_INTL_SUPPORT + prefix_length = Min(prefix_length, atom->length()); + i++; + } + if (i > first_with_prefix + 2) { + // Found worthwhile run of alternatives with common prefix of at least one + // character. The sorting function above did not sort on more than one + // character for reasons of correctness, but there may still be a longer + // common prefix if the terms were similar or presorted in the input. + // Find out how long the common prefix is. + int run_length = i - first_with_prefix; + RegExpAtom* const atom = alternatives->at(first_with_prefix)->AsAtom(); + for (int j = 1; j < run_length && prefix_length > 1; j++) { + RegExpAtom* old_atom = + alternatives->at(j + first_with_prefix)->AsAtom(); + for (int k = 1; k < prefix_length; k++) { + if (atom->data().at(k) != old_atom->data().at(k)) { + prefix_length = k; + break; + } + } + } + RegExpAtom* prefix = new (zone) + RegExpAtom(atom->data().SubVector(0, prefix_length), flags); + ZoneList* pair = new (zone) ZoneList(2, zone); + pair->Add(prefix, zone); + ZoneList* suffixes = + new (zone) ZoneList(run_length, zone); + for (int j = 0; j < run_length; j++) { + RegExpAtom* old_atom = + alternatives->at(j + first_with_prefix)->AsAtom(); + int len = old_atom->length(); + if (len == prefix_length) { + suffixes->Add(new (zone) RegExpEmpty(), zone); + } else { + RegExpTree* suffix = new (zone) RegExpAtom( + old_atom->data().SubVector(prefix_length, old_atom->length()), + flags); + suffixes->Add(suffix, zone); + } + } + pair->Add(new (zone) RegExpDisjunction(suffixes), zone); + alternatives->at(write_posn++) = new (zone) RegExpAlternative(pair); + } else { + // Just copy any non-worthwhile alternatives. + for (int j = first_with_prefix; j < i; j++) { + alternatives->at(write_posn++) = alternatives->at(j); + } + } + } + alternatives->Rewind(write_posn); // Trim end of array. +} + +// Optimizes b|c|z to [bcz]. +void RegExpDisjunction::FixSingleCharacterDisjunctions( + RegExpCompiler* compiler) { + Zone* zone = compiler->zone(); + ZoneList* alternatives = this->alternatives(); + int length = alternatives->length(); + + int write_posn = 0; + int i = 0; + while (i < length) { + RegExpTree* alternative = alternatives->at(i); + if (!alternative->IsAtom()) { + alternatives->at(write_posn++) = alternatives->at(i); + i++; + continue; + } + RegExpAtom* const atom = alternative->AsAtom(); + if (atom->length() != 1) { + alternatives->at(write_posn++) = alternatives->at(i); + i++; + continue; + } + JSRegExp::Flags flags = atom->flags(); + DCHECK_IMPLIES(IsUnicode(flags), + !unibrow::Utf16::IsLeadSurrogate(atom->data().at(0))); + bool contains_trail_surrogate = + unibrow::Utf16::IsTrailSurrogate(atom->data().at(0)); + int first_in_run = i; + i++; + // Find a run of single-character atom alternatives that have identical + // flags (case independence and unicode-ness). + while (i < length) { + alternative = alternatives->at(i); + if (!alternative->IsAtom()) break; + RegExpAtom* const atom = alternative->AsAtom(); + if (atom->length() != 1) break; + if (atom->flags() != flags) break; + DCHECK_IMPLIES(IsUnicode(flags), + !unibrow::Utf16::IsLeadSurrogate(atom->data().at(0))); + contains_trail_surrogate |= + unibrow::Utf16::IsTrailSurrogate(atom->data().at(0)); + i++; + } + if (i > first_in_run + 1) { + // Found non-trivial run of single-character alternatives. + int run_length = i - first_in_run; + ZoneList* ranges = + new (zone) ZoneList(2, zone); + for (int j = 0; j < run_length; j++) { + RegExpAtom* old_atom = alternatives->at(j + first_in_run)->AsAtom(); + DCHECK_EQ(old_atom->length(), 1); + ranges->Add(CharacterRange::Singleton(old_atom->data().at(0)), zone); + } + RegExpCharacterClass::CharacterClassFlags character_class_flags; + if (IsUnicode(flags) && contains_trail_surrogate) { + character_class_flags = RegExpCharacterClass::CONTAINS_SPLIT_SURROGATE; + } + alternatives->at(write_posn++) = new (zone) + RegExpCharacterClass(zone, ranges, flags, character_class_flags); + } else { + // Just copy any trivial alternatives. + for (int j = first_in_run; j < i; j++) { + alternatives->at(write_posn++) = alternatives->at(j); + } + } + } + alternatives->Rewind(write_posn); // Trim end of array. +} + +RegExpNode* RegExpDisjunction::ToNode(RegExpCompiler* compiler, + RegExpNode* on_success) { + ZoneList* alternatives = this->alternatives(); + + if (alternatives->length() > 2) { + bool found_consecutive_atoms = SortConsecutiveAtoms(compiler); + if (found_consecutive_atoms) RationalizeConsecutiveAtoms(compiler); + FixSingleCharacterDisjunctions(compiler); + if (alternatives->length() == 1) { + return alternatives->at(0)->ToNode(compiler, on_success); + } + } + + int length = alternatives->length(); + + ChoiceNode* result = + new (compiler->zone()) ChoiceNode(length, compiler->zone()); + for (int i = 0; i < length; i++) { + GuardedAlternative alternative( + alternatives->at(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); +} + +namespace { +// Desugar \b to (?<=\w)(?=\W)|(?<=\W)(?=\w) and +// \B to (?<=\w)(?=\w)|(?<=\W)(?=\W) +RegExpNode* BoundaryAssertionAsLookaround(RegExpCompiler* compiler, + RegExpNode* on_success, + RegExpAssertion::AssertionType type, + JSRegExp::Flags flags) { + DCHECK(NeedsUnicodeCaseEquivalents(flags)); + Zone* zone = compiler->zone(); + ZoneList* word_range = + new (zone) ZoneList(2, zone); + CharacterRange::AddClassEscape('w', word_range, true, zone); + int stack_register = compiler->UnicodeLookaroundStackRegister(); + int position_register = compiler->UnicodeLookaroundPositionRegister(); + ChoiceNode* result = new (zone) ChoiceNode(2, zone); + // Add two choices. The (non-)boundary could start with a word or + // a non-word-character. + for (int i = 0; i < 2; i++) { + bool lookbehind_for_word = i == 0; + bool lookahead_for_word = + (type == RegExpAssertion::BOUNDARY) ^ lookbehind_for_word; + // Look to the left. + RegExpLookaround::Builder lookbehind(lookbehind_for_word, on_success, + stack_register, position_register); + RegExpNode* backward = TextNode::CreateForCharacterRanges( + zone, word_range, true, lookbehind.on_match_success(), flags); + // Look to the right. + RegExpLookaround::Builder lookahead(lookahead_for_word, + lookbehind.ForMatch(backward), + stack_register, position_register); + RegExpNode* forward = TextNode::CreateForCharacterRanges( + zone, word_range, false, lookahead.on_match_success(), flags); + result->AddAlternative(GuardedAlternative(lookahead.ForMatch(forward))); + } + return result; +} +} // anonymous namespace + +RegExpNode* RegExpAssertion::ToNode(RegExpCompiler* compiler, + RegExpNode* on_success) { + NodeInfo info; + Zone* zone = compiler->zone(); + + switch (assertion_type()) { + case START_OF_LINE: + return AssertionNode::AfterNewline(on_success); + case START_OF_INPUT: + return AssertionNode::AtStart(on_success); + case BOUNDARY: + return NeedsUnicodeCaseEquivalents(flags_) + ? BoundaryAssertionAsLookaround(compiler, on_success, BOUNDARY, + flags_) + : AssertionNode::AtBoundary(on_success); + case NON_BOUNDARY: + return NeedsUnicodeCaseEquivalents(flags_) + ? BoundaryAssertionAsLookaround(compiler, on_success, + NON_BOUNDARY, flags_) + : 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 = new (zone) ChoiceNode(2, zone); + // Create a newline atom. + ZoneList* newline_ranges = + new (zone) ZoneList(3, zone); + CharacterRange::AddClassEscape('n', newline_ranges, false, zone); + JSRegExp::Flags default_flags = JSRegExp::Flags(); + RegExpCharacterClass* newline_atom = + new (zone) RegExpCharacterClass('n', default_flags); + TextNode* newline_matcher = + new (zone) TextNode(newline_atom, false, + 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: + UNREACHABLE(); + } + return on_success; +} + +RegExpNode* RegExpBackReference::ToNode(RegExpCompiler* compiler, + RegExpNode* on_success) { + return new (compiler->zone()) + BackReferenceNode(RegExpCapture::StartRegister(index()), + RegExpCapture::EndRegister(index()), flags_, + compiler->read_backward(), on_success); +} + +RegExpNode* RegExpEmpty::ToNode(RegExpCompiler* compiler, + RegExpNode* on_success) { + return on_success; +} + +RegExpLookaround::Builder::Builder(bool is_positive, RegExpNode* on_success, + int stack_pointer_register, + int position_register, + int capture_register_count, + int capture_register_start) + : is_positive_(is_positive), + on_success_(on_success), + stack_pointer_register_(stack_pointer_register), + position_register_(position_register) { + if (is_positive_) { + on_match_success_ = ActionNode::PositiveSubmatchSuccess( + stack_pointer_register, position_register, capture_register_count, + capture_register_start, on_success_); + } else { + Zone* zone = on_success_->zone(); + on_match_success_ = new (zone) NegativeSubmatchSuccess( + stack_pointer_register, position_register, capture_register_count, + capture_register_start, zone); + } +} + +RegExpNode* RegExpLookaround::Builder::ForMatch(RegExpNode* match) { + if (is_positive_) { + return ActionNode::BeginSubmatch(stack_pointer_register_, + position_register_, match); + } else { + Zone* zone = on_success_->zone(); + // We use a ChoiceNode to represent the negative lookaround. The first + // alternative is the negative match. On success, the end node backtracks. + // On failure, the second alternative is tried and leads to success. + // NegativeLookaheadChoiceNode is a special ChoiceNode that ignores the + // first exit when calculating quick checks. + ChoiceNode* choice_node = new (zone) NegativeLookaroundChoiceNode( + GuardedAlternative(match), GuardedAlternative(on_success_), zone); + return ActionNode::BeginSubmatch(stack_pointer_register_, + position_register_, choice_node); + } +} + +RegExpNode* RegExpLookaround::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; + + RegExpNode* result; + bool was_reading_backward = compiler->read_backward(); + compiler->set_read_backward(type() == LOOKBEHIND); + Builder builder(is_positive(), on_success, stack_pointer_register, + position_register, register_count, register_start); + RegExpNode* match = body_->ToNode(compiler, builder.on_match_success()); + result = builder.ForMatch(match); + compiler->set_read_backward(was_reading_backward); + return result; +} + +RegExpNode* RegExpCapture::ToNode(RegExpCompiler* compiler, + RegExpNode* on_success) { + return ToNode(body(), index(), compiler, on_success); +} + +RegExpNode* RegExpCapture::ToNode(RegExpTree* body, int index, + RegExpCompiler* compiler, + RegExpNode* on_success) { + DCHECK_NOT_NULL(body); + int start_reg = RegExpCapture::StartRegister(index); + int end_reg = RegExpCapture::EndRegister(index); + if (compiler->read_backward()) std::swap(start_reg, end_reg); + 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); +} + +namespace { + +class AssertionSequenceRewriter final { + public: + // TODO(jgruber): Consider moving this to a separate AST tree rewriter pass + // instead of sprinkling rewrites into the AST->Node conversion process. + static void MaybeRewrite(ZoneList* terms, Zone* zone) { + AssertionSequenceRewriter rewriter(terms, zone); + + static constexpr int kNoIndex = -1; + int from = kNoIndex; + + for (int i = 0; i < terms->length(); i++) { + RegExpTree* t = terms->at(i); + if (from == kNoIndex && t->IsAssertion()) { + from = i; // Start a sequence. + } else if (from != kNoIndex && !t->IsAssertion()) { + // Terminate and process the sequence. + if (i - from > 1) rewriter.Rewrite(from, i); + from = kNoIndex; + } + } + + if (from != kNoIndex && terms->length() - from > 1) { + rewriter.Rewrite(from, terms->length()); + } + } + + // All assertions are zero width. A consecutive sequence of assertions is + // order-independent. There's two ways we can optimize here: + // 1. fold all identical assertions. + // 2. if any assertion combinations are known to fail (e.g. \b\B), the entire + // sequence fails. + void Rewrite(int from, int to) { + DCHECK_GT(to, from + 1); + + // Bitfield of all seen assertions. + uint32_t seen_assertions = 0; + STATIC_ASSERT(RegExpAssertion::LAST_TYPE < kUInt32Size * kBitsPerByte); + + // Flags must match for folding. + JSRegExp::Flags flags = terms_->at(from)->AsAssertion()->flags(); + bool saw_mismatched_flags = false; + + for (int i = from; i < to; i++) { + RegExpAssertion* t = terms_->at(i)->AsAssertion(); + if (t->flags() != flags) saw_mismatched_flags = true; + const uint32_t bit = 1 << t->assertion_type(); + + if ((seen_assertions & bit) && !saw_mismatched_flags) { + // Fold duplicates. + terms_->Set(i, new (zone_) RegExpEmpty()); + } + + seen_assertions |= bit; + } + + // Collapse failures. + const uint32_t always_fails_mask = + 1 << RegExpAssertion::BOUNDARY | 1 << RegExpAssertion::NON_BOUNDARY; + if ((seen_assertions & always_fails_mask) == always_fails_mask) { + ReplaceSequenceWithFailure(from, to); + } + } + + void ReplaceSequenceWithFailure(int from, int to) { + // Replace the entire sequence with a single node that always fails. + // TODO(jgruber): Consider adding an explicit Fail kind. Until then, the + // negated '*' (everything) range serves the purpose. + ZoneList* ranges = + new (zone_) ZoneList(0, zone_); + RegExpCharacterClass* cc = + new (zone_) RegExpCharacterClass(zone_, ranges, JSRegExp::Flags()); + terms_->Set(from, cc); + + // Zero out the rest. + RegExpEmpty* empty = new (zone_) RegExpEmpty(); + for (int i = from + 1; i < to; i++) terms_->Set(i, empty); + } + + private: + AssertionSequenceRewriter(ZoneList* terms, Zone* zone) + : zone_(zone), terms_(terms) {} + + Zone* zone_; + ZoneList* terms_; +}; + +} // namespace + +RegExpNode* RegExpAlternative::ToNode(RegExpCompiler* compiler, + RegExpNode* on_success) { + ZoneList* children = nodes(); + + AssertionSequenceRewriter::MaybeRewrite(children, compiler->zone()); + + RegExpNode* current = on_success; + if (compiler->read_backward()) { + for (int i = 0; i < children->length(); i++) { + current = children->at(i)->ToNode(compiler, current); + } + } else { + for (int i = children->length() - 1; i >= 0; i--) { + current = children->at(i)->ToNode(compiler, current); + } + } + return current; +} + +static void AddClass(const int* elmv, int elmc, + ZoneList* ranges, Zone* zone) { + elmc--; + DCHECK_EQ(kRangeEndMarker, elmv[elmc]); + for (int i = 0; i < elmc; i += 2) { + DCHECK(elmv[i] < elmv[i + 1]); + ranges->Add(CharacterRange::Range(elmv[i], elmv[i + 1] - 1), zone); + } +} + +static void AddClassNegated(const int* elmv, int elmc, + ZoneList* ranges, Zone* zone) { + elmc--; + DCHECK_EQ(kRangeEndMarker, elmv[elmc]); + DCHECK_NE(0x0000, elmv[0]); + DCHECK_NE(String::kMaxCodePoint, elmv[elmc - 1]); + uc16 last = 0x0000; + for (int i = 0; i < elmc; i += 2) { + DCHECK(last <= elmv[i] - 1); + DCHECK(elmv[i] < elmv[i + 1]); + ranges->Add(CharacterRange::Range(last, elmv[i] - 1), zone); + last = elmv[i + 1]; + } + ranges->Add(CharacterRange::Range(last, String::kMaxCodePoint), zone); +} + +void CharacterRange::AddClassEscape(char type, ZoneList* ranges, + bool add_unicode_case_equivalents, + Zone* zone) { + if (add_unicode_case_equivalents && (type == 'w' || type == 'W')) { + // See #sec-runtime-semantics-wordcharacters-abstract-operation + // In case of unicode and ignore_case, we need to create the closure over + // case equivalent characters before negating. + ZoneList* new_ranges = + new (zone) ZoneList(2, zone); + AddClass(kWordRanges, kWordRangeCount, new_ranges, zone); + AddUnicodeCaseEquivalents(new_ranges, zone); + if (type == 'W') { + ZoneList* negated = + new (zone) ZoneList(2, zone); + CharacterRange::Negate(new_ranges, negated, zone); + new_ranges = negated; + } + ranges->AddAll(*new_ranges, zone); + return; + } + AddClassEscape(type, ranges, zone); +} + +void CharacterRange::AddClassEscape(char type, ZoneList* ranges, + Zone* zone) { + switch (type) { + case 's': + AddClass(kSpaceRanges, kSpaceRangeCount, ranges, zone); + break; + case 'S': + AddClassNegated(kSpaceRanges, kSpaceRangeCount, ranges, zone); + break; + case 'w': + AddClass(kWordRanges, kWordRangeCount, ranges, zone); + break; + case 'W': + AddClassNegated(kWordRanges, kWordRangeCount, ranges, zone); + break; + case 'd': + AddClass(kDigitRanges, kDigitRangeCount, ranges, zone); + break; + case 'D': + AddClassNegated(kDigitRanges, kDigitRangeCount, ranges, zone); + break; + case '.': + AddClassNegated(kLineTerminatorRanges, kLineTerminatorRangeCount, ranges, + zone); + 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->Add(CharacterRange::Everything(), zone); + break; + // This is the set of characters matched by the $ and ^ symbols + // in multiline mode. + case 'n': + AddClass(kLineTerminatorRanges, kLineTerminatorRangeCount, ranges, zone); + break; + default: + UNREACHABLE(); + } +} + +Vector CharacterRange::GetWordBounds() { + return Vector(kWordRanges, kWordRangeCount - 1); +} + +#ifdef V8_INTL_SUPPORT +struct IgnoreSet { + IgnoreSet() : set(BuildIgnoreSet()) {} + const icu::UnicodeSet set; +}; + +struct SpecialAddSet { + SpecialAddSet() : set(BuildSpecialAddSet()) {} + const icu::UnicodeSet set; +}; + +icu::UnicodeSet BuildAsciiAToZSet() { + icu::UnicodeSet set('a', 'z'); + set.add('A', 'Z'); + set.freeze(); + return set; +} + +struct AsciiAToZSet { + AsciiAToZSet() : set(BuildAsciiAToZSet()) {} + const icu::UnicodeSet set; +}; + +static base::LazyInstance::type ignore_set = + LAZY_INSTANCE_INITIALIZER; + +static base::LazyInstance::type special_add_set = + LAZY_INSTANCE_INITIALIZER; + +static base::LazyInstance::type ascii_a_to_z_set = + LAZY_INSTANCE_INITIALIZER; +#endif // V8_INTL_SUPPORT + +// static +void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone, + ZoneList* ranges, + bool is_one_byte) { + CharacterRange::Canonicalize(ranges); + int range_count = ranges->length(); +#ifdef V8_INTL_SUPPORT + icu::UnicodeSet others; + for (int i = 0; i < range_count; i++) { + CharacterRange range = ranges->at(i); + uc32 from = range.from(); + if (from > String::kMaxUtf16CodeUnit) continue; + uc32 to = Min(range.to(), String::kMaxUtf16CodeUnit); + // Nothing to be done for surrogates. + if (from >= kLeadSurrogateStart && to <= kTrailSurrogateEnd) continue; + if (is_one_byte && !RangeContainsLatin1Equivalents(range)) { + if (from > String::kMaxOneByteCharCode) continue; + if (to > String::kMaxOneByteCharCode) to = String::kMaxOneByteCharCode; + } + others.add(from, to); + } + + // Set of characters already added to ranges that do not need to be added + // again. + icu::UnicodeSet already_added(others); + + // Set of characters in ranges that are in the 52 ASCII characters [a-zA-Z]. + icu::UnicodeSet in_ascii_a_to_z(others); + in_ascii_a_to_z.retainAll(ascii_a_to_z_set.Pointer()->set); + + // Remove all chars in [a-zA-Z] from others. + others.removeAll(in_ascii_a_to_z); + + // Set of characters in ranges that are overlapping with special add set. + icu::UnicodeSet in_special_add(others); + in_special_add.retainAll(special_add_set.Pointer()->set); + + others.removeAll(in_special_add); + + // Ignore all chars in ignore set. + others.removeAll(ignore_set.Pointer()->set); + + // For most of the chars in ranges that is still in others, find the case + // equivlant set by calling closeOver(USET_CASE_INSENSITIVE). + others.closeOver(USET_CASE_INSENSITIVE); + + // Because closeOver(USET_CASE_INSENSITIVE) may add ASCII [a-zA-Z] to others, + // but ECMA262 "i" mode won't consider that, remove them from others. + // Ex: U+017F add 'S' and 's' to others. + others.removeAll(ascii_a_to_z_set.Pointer()->set); + + // Special handling for in_ascii_a_to_z. + for (int32_t i = 0; i < in_ascii_a_to_z.getRangeCount(); i++) { + UChar32 start = in_ascii_a_to_z.getRangeStart(i); + UChar32 end = in_ascii_a_to_z.getRangeEnd(i); + // Check if it is uppercase A-Z by checking bit 6. + if (start & 0x0020) { + // Add the lowercases + others.add(start & 0x005F, end & 0x005F); + } else { + // Add the uppercases + others.add(start | 0x0020, end | 0x0020); + } + } + + // Special handling for chars in "Special Add" set. + for (int32_t i = 0; i < in_special_add.getRangeCount(); i++) { + UChar32 end = in_special_add.getRangeEnd(i); + for (UChar32 ch = in_special_add.getRangeStart(i); ch <= end; ch++) { + // Add the uppercase of this character if itself is not an uppercase + // character. + // Note: The if condiction cannot be u_islower(ch) because ch could be + // neither uppercase nor lowercase but Mn. + if (!u_isupper(ch)) { + others.add(u_toupper(ch)); + } + icu::UnicodeSet candidates(ch, ch); + candidates.closeOver(USET_CASE_INSENSITIVE); + for (int32_t j = 0; j < candidates.getRangeCount(); j++) { + UChar32 end2 = candidates.getRangeEnd(j); + for (UChar32 ch2 = candidates.getRangeStart(j); ch2 <= end2; ch2++) { + // Add character that is not uppercase to others. + if (!u_isupper(ch2)) { + others.add(ch2); + } + } + } + } + } + + // Remove all characters which already in the ranges. + others.removeAll(already_added); + + // Add others to the ranges + for (int32_t i = 0; i < others.getRangeCount(); i++) { + UChar32 from = others.getRangeStart(i); + UChar32 to = others.getRangeEnd(i); + if (from == to) { + ranges->Add(CharacterRange::Singleton(from), zone); + } else { + ranges->Add(CharacterRange::Range(from, to), zone); + } + } +#else + for (int i = 0; i < range_count; i++) { + CharacterRange range = ranges->at(i); + uc32 bottom = range.from(); + if (bottom > String::kMaxUtf16CodeUnit) continue; + uc32 top = Min(range.to(), String::kMaxUtf16CodeUnit); + // Nothing to be done for surrogates. + if (bottom >= kLeadSurrogateStart && top <= kTrailSurrogateEnd) continue; + if (is_one_byte && !RangeContainsLatin1Equivalents(range)) { + if (bottom > String::kMaxOneByteCharCode) continue; + if (top > String::kMaxOneByteCharCode) top = String::kMaxOneByteCharCode; + } + unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth]; + if (top == bottom) { + // If this is a singleton we just expand the one character. + int length = isolate->jsregexp_uncanonicalize()->get(bottom, '\0', chars); + for (int i = 0; i < length; i++) { + uc32 chr = chars[i]; + if (chr != bottom) { + ranges->Add(CharacterRange::Singleton(chars[i]), zone); + } + } + } else { + // If this is a range we expand the characters block by block, expanding + // contiguous subranges (blocks) one at a time. The approach is as + // follows. For a given start character we look up the remainder of the + // block that contains it (represented by the end point), for instance we + // find 'z' if the character is 'c'. A block is characterized by the + // property that all characters uncanonicalize in the same way, except + // that each entry in the result is incremented by the distance from the + // first element. So a-z is a block because 'a' uncanonicalizes to ['a', + // 'A'] and the k'th letter uncanonicalizes to ['a' + k, 'A' + k]. Once + // we've found the end point we look up its uncanonicalization and + // produce a range for each element. For instance for [c-f] we look up + // ['z', 'Z'] and produce [c-f] and [C-F]. We then only add a range if + // it is not already contained in the input, so [c-f] will be skipped but + // [C-F] will be added. If this range is not completely contained in a + // block we do this for all the blocks covered by the range (handling + // characters that is not in a block as a "singleton block"). + unibrow::uchar equivalents[unibrow::Ecma262UnCanonicalize::kMaxWidth]; + int pos = bottom; + while (pos <= top) { + int length = + isolate->jsregexp_canonrange()->get(pos, '\0', equivalents); + uc32 block_end; + if (length == 0) { + block_end = pos; + } else { + DCHECK_EQ(1, length); + block_end = equivalents[0]; + } + int end = (block_end > top) ? top : block_end; + length = isolate->jsregexp_uncanonicalize()->get(block_end, '\0', + equivalents); + for (int i = 0; i < length; i++) { + uc32 c = equivalents[i]; + uc32 range_from = c - (block_end - pos); + uc32 range_to = c - (block_end - end); + if (!(bottom <= range_from && range_to <= top)) { + ranges->Add(CharacterRange::Range(range_from, range_to), zone); + } + } + pos = end + 1; + } + } + } +#endif // V8_INTL_SUPPORT +} + +bool CharacterRange::IsCanonical(ZoneList* ranges) { + DCHECK_NOT_NULL(ranges); + int n = ranges->length(); + if (n <= 1) return true; + int max = ranges->at(0).to(); + for (int i = 1; i < n; i++) { + CharacterRange next_range = ranges->at(i); + if (next_range.from() <= max + 1) return false; + max = next_range.to(); + } + return true; +} + +ZoneList* CharacterSet::ranges(Zone* zone) { + if (ranges_ == nullptr) { + ranges_ = new (zone) ZoneList(2, zone); + CharacterRange::AddClassEscape(standard_set_type_, ranges_, false, zone); + } + return ranges_; +} + +// Move a number of elements in a zonelist to another position +// in the same list. Handles overlapping source and target areas. +static void MoveRanges(ZoneList* list, int from, int to, + int count) { + // Ranges are potentially overlapping. + if (from < to) { + for (int i = count - 1; i >= 0; i--) { + list->at(to + i) = list->at(from + i); + } + } else { + for (int i = 0; i < count; i++) { + list->at(to + i) = list->at(from + i); + } + } +} + +static int InsertRangeInCanonicalList(ZoneList* 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. + uc32 from = insert.from(); + uc32 to = insert.to(); + int start_pos = 0; + int end_pos = count; + for (int i = count - 1; i >= 0; i--) { + CharacterRange current = list->at(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->at(start_pos) = insert; + return count + 1; + } + if (start_pos + 1 == end_pos) { + // Replace single existing range at position start_pos. + CharacterRange to_replace = list->at(start_pos); + int new_from = Min(to_replace.from(), from); + int new_to = Max(to_replace.to(), to); + list->at(start_pos) = CharacterRange::Range(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->at(start_pos).from(), from); + int new_to = Max(list->at(end_pos - 1).to(), to); + if (end_pos < count) { + MoveRanges(list, end_pos, start_pos + 1, count - end_pos); + } + list->at(start_pos) = CharacterRange::Range(new_from, new_to); + return count - (end_pos - start_pos) + 1; +} + +void CharacterSet::Canonicalize() { + // Special/default classes are always considered canonical. The result + // of calling ranges() will be sorted. + if (ranges_ == nullptr) return; + CharacterRange::Canonicalize(ranges_); +} + +void CharacterRange::Canonicalize(ZoneList* 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->at(0).to(); + int i = 1; + while (i < n) { + CharacterRange current = character_ranges->at(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. + int num_canonical = i; // Length of canonicalized part of list. + do { + num_canonical = InsertRangeInCanonicalList(character_ranges, num_canonical, + character_ranges->at(read)); + read++; + } while (read < n); + character_ranges->Rewind(num_canonical); + + DCHECK(CharacterRange::IsCanonical(character_ranges)); +} + +void CharacterRange::Negate(ZoneList* ranges, + ZoneList* negated_ranges, + Zone* zone) { + DCHECK(CharacterRange::IsCanonical(ranges)); + DCHECK_EQ(0, negated_ranges->length()); + int range_count = ranges->length(); + uc32 from = 0; + int i = 0; + if (range_count > 0 && ranges->at(0).from() == 0) { + from = ranges->at(0).to() + 1; + i = 1; + } + while (i < range_count) { + CharacterRange range = ranges->at(i); + negated_ranges->Add(CharacterRange::Range(from, range.from() - 1), zone); + from = range.to() + 1; + i++; + } + if (from < String::kMaxCodePoint) { + negated_ranges->Add(CharacterRange::Range(from, String::kMaxCodePoint), + zone); + } +} + +// 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) { + DCHECK_LT(0, factor); + 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_; + + DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpExpansionLimiter); +}; + +RegExpNode* RegExpQuantifier::ToNode(int min, int max, bool is_greedy, + RegExpTree* body, RegExpCompiler* compiler, + RegExpNode* on_success, + bool not_at_start) { + // 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(); + Zone* zone = compiler->zone(); + + if (body_can_be_empty) { + body_start_reg = compiler->AllocateRegister(); + } else if (compiler->optimize() && !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) { + DCHECK_LT(0, max); // 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 = new (zone) ChoiceNode(2, zone); + 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 && !compiler->read_backward()) { + 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 = new (zone) + LoopChoiceNode(body->min_match() == 0, compiler->read_backward(), zone); + if (not_at_start && !compiler->read_backward()) center->set_not_at_start(); + RegExpNode* loop_return = + needs_counter ? static_cast( + ActionNode::IncrementRegister(reg_ctr, center)) + : static_cast(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 = new (zone) Guard(reg_ctr, Guard::LT, max); + body_alt.AddGuard(body_guard, zone); + } + GuardedAlternative rest_alt(on_success); + if (has_min) { + Guard* rest_guard = new (compiler->zone()) Guard(reg_ctr, Guard::GEQ, min); + rest_alt.AddGuard(rest_guard, zone); + } + 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); + } else { + return center; + } +} + +} // namespace internal +} // namespace v8 diff --git a/chromium/v8/src/regexp/regexp-compiler.cc b/chromium/v8/src/regexp/regexp-compiler.cc new file mode 100644 index 00000000000..c643f988c0f --- /dev/null +++ b/chromium/v8/src/regexp/regexp-compiler.cc @@ -0,0 +1,3551 @@ +// Copyright 2019 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "src/regexp/regexp-compiler.h" + +#include "src/diagnostics/code-tracer.h" +#include "src/execution/isolate.h" +#include "src/objects/objects-inl.h" +#include "src/regexp/regexp-macro-assembler-arch.h" +#include "src/regexp/regexp-macro-assembler-tracer.h" +#include "src/strings/unicode-inl.h" +#include "src/utils/ostreams.h" +#include "src/zone/zone-list-inl.h" + +#ifdef V8_INTL_SUPPORT +#include "unicode/locid.h" +#include "unicode/uniset.h" +#include "unicode/utypes.h" +#endif // V8_INTL_SUPPORT + +namespace v8 { +namespace internal { + +using namespace regexp_compiler_constants; // NOLINT(build/namespaces) + +// ------------------------------------------------------------------- +// 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 ast.cc. +// 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 +// +// backtrack code location: +// pop current position +// } +// +// +// * Actions nodes are generated as follows +// +// +// push backtrack code location +// +// backtrack code location: +// +// +// +// * Matching nodes are generated as follows: +// if input string matches at current position +// update current position +// +// else +// +// +// 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. + +void RegExpTree::AppendToText(RegExpText* text, Zone* zone) { UNREACHABLE(); } + +void RegExpAtom::AppendToText(RegExpText* text, Zone* zone) { + text->AddElement(TextElement::Atom(this), zone); +} + +void RegExpCharacterClass::AppendToText(RegExpText* text, Zone* zone) { + text->AddElement(TextElement::CharClass(this), zone); +} + +void RegExpText::AppendToText(RegExpText* text, Zone* zone) { + for (int i = 0; i < elements()->length(); i++) + text->AddElement(elements()->at(i), zone); +} + +TextElement TextElement::Atom(RegExpAtom* atom) { + return TextElement(ATOM, atom); +} + +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; + } + UNREACHABLE(); +} + +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(Isolate* isolate, Zone* zone, int capture_count, + bool one_byte) + : next_register_(2 * (capture_count + 1)), + unicode_lookaround_stack_register_(kNoRegister), + unicode_lookaround_position_register_(kNoRegister), + work_list_(nullptr), + recursion_depth_(0), + one_byte_(one_byte), + reg_exp_too_big_(false), + limiting_recursion_(false), + optimize_(FLAG_regexp_optimization), + read_backward_(false), + current_expansion_factor_(1), + frequency_collator_(), + isolate_(isolate), + zone_(zone) { + accept_ = new (zone) EndNode(EndNode::ACCEPT, zone); + DCHECK_GE(RegExpMacroAssembler::kMaxRegister, next_register_ - 1); +} + +RegExpCompiler::CompilationResult RegExpCompiler::Assemble( + Isolate* isolate, RegExpMacroAssembler* macro_assembler, RegExpNode* start, + int capture_count, Handle pattern) { +#ifdef DEBUG + if (FLAG_trace_regexp_assembler) + macro_assembler_ = new RegExpMacroAssemblerTracer(isolate, macro_assembler); + else +#endif + macro_assembler_ = macro_assembler; + + std::vector work_list; + work_list_ = &work_list; + Label fail; + macro_assembler_->PushBacktrack(&fail); + Trace new_trace; + start->Emit(this, &new_trace); + macro_assembler_->Bind(&fail); + macro_assembler_->Fail(); + while (!work_list.empty()) { + RegExpNode* node = work_list.back(); + work_list.pop_back(); + node->set_on_work_list(false); + if (!node->label()->is_bound()) node->Emit(this, &new_trace); + } + if (reg_exp_too_big_) { + macro_assembler_->AbortedCodeGeneration(); + return CompilationResult::RegExpTooBig(); + } + + Handle code = macro_assembler_->GetCode(pattern); + isolate->IncreaseTotalRegexpCodeGenerated(code->Size()); + work_list_ = nullptr; +#ifdef ENABLE_DISASSEMBLER + if (FLAG_print_code && !FLAG_regexp_interpret_all) { + CodeTracer::Scope trace_scope(isolate->GetCodeTracer()); + OFStream os(trace_scope.file()); + Handle::cast(code)->Disassemble(pattern->ToCString().get(), os); + } +#endif +#ifdef DEBUG + if (FLAG_trace_regexp_assembler) { + delete macro_assembler_; + } +#endif + return {*code, next_register_}; +} + +bool Trace::DeferredAction::Mentions(int that) { + if (action_type() == ActionNode::CLEAR_CAPTURES) { + Interval range = static_cast(this)->range(); + return range.Contains(that); + } else { + 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) { + DCHECK_EQ(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(action)->cp_offset(); + return true; + } else { + return false; + } + } + } + return false; +} + +// A (dynamically-sized) set of unsigned integers that behaves especially well +// on small integers (< kFirstLimit). May do zone-allocation. +class DynamicBitSet : public ZoneObject { + public: + V8_EXPORT_PRIVATE bool Get(unsigned value) const { + if (value < kFirstLimit) { + return (first_ & (1 << value)) != 0; + } else if (remaining_ == nullptr) { + return false; + } else { + return remaining_->Contains(value); + } + } + + // Destructively set a value in this set. + void Set(unsigned value, Zone* zone) { + if (value < kFirstLimit) { + first_ |= (1 << value); + } else { + if (remaining_ == nullptr) + remaining_ = new (zone) ZoneList(1, zone); + if (remaining_->is_empty() || !remaining_->Contains(value)) + remaining_->Add(value, zone); + } + } + + private: + static constexpr unsigned kFirstLimit = 32; + + uint32_t first_ = 0; + ZoneList* remaining_ = nullptr; +}; + +int Trace::FindAffectedRegisters(DynamicBitSet* affected_registers, + Zone* zone) { + 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(action)->range(); + for (int i = range.from(); i <= range.to(); i++) + affected_registers->Set(i, zone); + if (range.to() > max_register) max_register = range.to(); + } else { + affected_registers->Set(action->reg(), zone); + if (action->reg() > max_register) max_register = action->reg(); + } + } + return max_register; +} + +void Trace::RestoreAffectedRegisters(RegExpMacroAssembler* assembler, + int max_register, + const DynamicBitSet& registers_to_pop, + const DynamicBitSet& 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); + } + } +} + +void Trace::PerformDeferredActions(RegExpMacroAssembler* assembler, + int max_register, + const DynamicBitSet& affected_registers, + DynamicBitSet* registers_to_pop, + DynamicBitSet* registers_to_clear, + Zone* zone) { + // 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). + enum DeferredActionUndoType { IGNORE, RESTORE, CLEAR }; + DeferredActionUndoType undo_action = IGNORE; + + int value = 0; + bool absolute = false; + bool clear = false; + static const int kNoStore = kMinInt; + int store_position = kNoStore; + // 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(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 + // occur 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 = RESTORE; + DCHECK_EQ(store_position, kNoStore); + DCHECK(!clear); + break; + } + case ActionNode::INCREMENT_REGISTER: + if (!absolute) { + value++; + } + DCHECK_EQ(store_position, kNoStore); + DCHECK(!clear); + undo_action = RESTORE; + break; + case ActionNode::STORE_POSITION: { + Trace::DeferredCapture* pc = + static_cast(action); + if (!clear && store_position == kNoStore) { + 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 = IGNORE; + } else { + undo_action = pc->is_capture() ? CLEAR : RESTORE; + } + DCHECK(!absolute); + DCHECK_EQ(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 == kNoStore) { + clear = true; + } + undo_action = RESTORE; + DCHECK(!absolute); + DCHECK_EQ(value, 0); + break; + } + default: + UNREACHABLE(); + break; + } + } + } + // Prepare for the undo-action (e.g., push if it's going to be popped). + if (undo_action == 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(reg, zone); + } else if (undo_action == CLEAR) { + registers_to_clear->Set(reg, zone); + } + // Perform the chronologically last action (or accumulated increment) + // for the register. + if (store_position != kNoStore) { + 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(); + + DCHECK(!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. + DynamicBitSet 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(&affected_registers, compiler->zone()); + DynamicBitSet registers_to_pop; + DynamicBitSet registers_to_clear; + PerformDeferredActions(assembler, max_register, affected_registers, + ®isters_to_pop, ®isters_to_clear, + compiler->zone()); + if (cp_offset_ != 0) { + assembler->AdvanceCurrentPosition(cp_offset_); + } + + // Create a new trivial state and generate the node with that. + Label undo; + assembler->PushBacktrack(&undo); + if (successor->KeepRecursing(compiler)) { + Trace new_state; + successor->Emit(compiler, &new_state); + } else { + compiler->AddWork(successor); + assembler->GoTo(successor->label()); + } + + // On backtrack we need to restore state. + assembler->Bind(&undo); + RestoreAffectedRegisters(assembler, max_register, registers_to_pop, + registers_to_clear); + if (backtrack() == nullptr) { + assembler->Backtrack(); + } else { + assembler->PopCurrentPosition(); + assembler->GoTo(backtrack()); + } +} + +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()->is_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->ReadStackPointerFromRegister(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()->is_bound()) { + assembler->Bind(label()); + } + switch (action_) { + case ACCEPT: + assembler->Succeed(); + return; + case BACKTRACK: + assembler->GoTo(trace->backtrack()); + return; + case NEGATIVE_SUBMATCH_SUCCESS: + // This case is handled in a different virtual method. + UNREACHABLE(); + } + UNIMPLEMENTED(); +} + +void GuardedAlternative::AddGuard(Guard* guard, Zone* zone) { + if (guards_ == nullptr) guards_ = new (zone) ZoneList(1, zone); + guards_->Add(guard, zone); +} + +ActionNode* ActionNode::SetRegister(int reg, int val, RegExpNode* on_success) { + ActionNode* result = + new (on_success->zone()) ActionNode(SET_REGISTER, on_success); + result->data_.u_store_register.reg = reg; + result->data_.u_store_register.value = val; + return result; +} + +ActionNode* ActionNode::IncrementRegister(int reg, RegExpNode* on_success) { + ActionNode* result = + new (on_success->zone()) ActionNode(INCREMENT_REGISTER, on_success); + result->data_.u_increment_register.reg = reg; + return result; +} + +ActionNode* ActionNode::StorePosition(int reg, bool is_capture, + RegExpNode* on_success) { + ActionNode* result = + new (on_success->zone()) ActionNode(STORE_POSITION, on_success); + result->data_.u_position_register.reg = reg; + result->data_.u_position_register.is_capture = is_capture; + return result; +} + +ActionNode* ActionNode::ClearCaptures(Interval range, RegExpNode* on_success) { + ActionNode* result = + new (on_success->zone()) 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; +} + +ActionNode* ActionNode::BeginSubmatch(int stack_reg, int position_reg, + RegExpNode* on_success) { + ActionNode* result = + new (on_success->zone()) ActionNode(BEGIN_SUBMATCH, on_success); + result->data_.u_submatch.stack_pointer_register = stack_reg; + result->data_.u_submatch.current_position_register = position_reg; + return result; +} + +ActionNode* ActionNode::PositiveSubmatchSuccess(int stack_reg, int position_reg, + int clear_register_count, + int clear_register_from, + RegExpNode* on_success) { + ActionNode* result = new (on_success->zone()) + ActionNode(POSITIVE_SUBMATCH_SUCCESS, on_success); + result->data_.u_submatch.stack_pointer_register = stack_reg; + result->data_.u_submatch.current_position_register = position_reg; + result->data_.u_submatch.clear_register_count = clear_register_count; + result->data_.u_submatch.clear_register_from = clear_register_from; + return result; +} + +ActionNode* ActionNode::EmptyMatchCheck(int start_register, + int repetition_register, + int repetition_limit, + RegExpNode* on_success) { + ActionNode* result = + new (on_success->zone()) 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; +} + +#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); +} + +// ------------------------------------------------------------------- +// Emit code. + +void ChoiceNode::GenerateGuard(RegExpMacroAssembler* macro_assembler, + Guard* guard, Trace* trace) { + switch (guard->op()) { + case Guard::LT: + DCHECK(!trace->mentions_reg(guard->reg())); + macro_assembler->IfRegisterGE(guard->reg(), guard->value(), + trace->backtrack()); + break; + case Guard::GEQ: + DCHECK(!trace->mentions_reg(guard->reg())); + macro_assembler->IfRegisterLT(guard->reg(), guard->value(), + trace->backtrack()); + break; + } +} + +// Returns the number of characters in the equivalence class, omitting those +// that cannot occur in the source string because it is Latin1. +static int GetCaseIndependentLetters(Isolate* isolate, uc16 character, + bool one_byte_subject, + unibrow::uchar* letters, + int letter_length) { +#ifdef V8_INTL_SUPPORT + icu::UnicodeSet set; + set.add(character); + set = set.closeOver(USET_CASE_INSENSITIVE); + int32_t range_count = set.getRangeCount(); + int items = 0; + for (int32_t i = 0; i < range_count; i++) { + UChar32 start = set.getRangeStart(i); + UChar32 end = set.getRangeEnd(i); + CHECK(end - start + items <= letter_length); + while (start <= end) { + if (one_byte_subject && start > String::kMaxOneByteCharCode) break; + letters[items++] = (unibrow::uchar)(start); + start++; + } + } + return items; +#else + int length = + isolate->jsregexp_uncanonicalize()->get(character, '\0', letters); + // Unibrow returns 0 or 1 for characters where case independence is + // trivial. + if (length == 0) { + letters[0] = character; + length = 1; + } + + if (one_byte_subject) { + int new_length = 0; + for (int i = 0; i < length; i++) { + if (letters[i] <= String::kMaxOneByteCharCode) { + letters[new_length++] = letters[i]; + } + } + length = new_length; + } + + return length; +#endif // V8_INTL_SUPPORT +} + +static inline bool EmitSimpleCharacter(Isolate* isolate, + RegExpCompiler* compiler, uc16 c, + Label* on_failure, int cp_offset, + bool check, bool preloaded) { + RegExpMacroAssembler* assembler = compiler->macro_assembler(); + bool bound_checked = false; + if (!preloaded) { + assembler->LoadCurrentCharacter(cp_offset, on_failure, check); + bound_checked = true; + } + assembler->CheckNotCharacter(c, on_failure); + return bound_checked; +} + +// Only emits non-letters (things that don't have case). Only used for case +// independent matches. +static inline bool EmitAtomNonLetter(Isolate* isolate, RegExpCompiler* compiler, + uc16 c, Label* on_failure, int cp_offset, + bool check, bool preloaded) { + RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); + bool one_byte = compiler->one_byte(); + unibrow::uchar chars[4]; + int length = GetCaseIndependentLetters(isolate, c, one_byte, chars, 4); + if (length < 1) { + // This can't match. Must be an one-byte subject and a non-one-byte + // character. We do not need to do anything since the one-byte pass + // already handled this. + return false; // Bounds not checked. + } + bool checked = false; + // We handle the length > 1 case in a later pass. + if (length == 1) { + if (one_byte && c > String::kMaxOneByteCharCodeU) { + // Can't match - see above. + return false; // Bounds not checked. + } + if (!preloaded) { + macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check); + checked = check; + } + macro_assembler->CheckNotCharacter(c, on_failure); + } + return checked; +} + +static bool ShortCutEmitCharacterPair(RegExpMacroAssembler* macro_assembler, + bool one_byte, uc16 c1, uc16 c2, + Label* on_failure) { + uc16 char_mask; + if (one_byte) { + char_mask = String::kMaxOneByteCharCode; + } else { + char_mask = String::kMaxUtf16CodeUnit; + } + uc16 exor = c1 ^ c2; + // Check whether exor has only one bit set. + if (((exor - 1) & exor) == 0) { + // If c1 and c2 differ only by one bit. + // Ecma262UnCanonicalize always gives the highest number last. + DCHECK(c2 > c1); + uc16 mask = char_mask ^ exor; + macro_assembler->CheckNotCharacterAfterAnd(c1, mask, on_failure); + return true; + } + DCHECK(c2 > c1); + uc16 diff = c2 - c1; + if (((diff - 1) & diff) == 0 && c1 >= diff) { + // If the characters differ by 2^n but don't differ by one bit then + // subtract the difference from the found character, then do the or + // trick. We avoid the theoretical case where negative numbers are + // involved in order to simplify code generation. + uc16 mask = char_mask ^ diff; + macro_assembler->CheckNotCharacterAfterMinusAnd(c1 - diff, diff, mask, + on_failure); + return true; + } + return false; +} + +using EmitCharacterFunction = bool(Isolate* isolate, RegExpCompiler* compiler, + uc16 c, Label* on_failure, int cp_offset, + bool check, bool preloaded); + +// Only emits letters (things that have case). Only used for case independent +// matches. +static inline bool EmitAtomLetter(Isolate* isolate, RegExpCompiler* compiler, + uc16 c, Label* on_failure, int cp_offset, + bool check, bool preloaded) { + RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); + bool one_byte = compiler->one_byte(); + unibrow::uchar chars[4]; + int length = GetCaseIndependentLetters(isolate, c, one_byte, chars, 4); + if (length <= 1) return false; + // We may not need to check against the end of the input string + // if this character lies before a character that matched. + if (!preloaded) { + macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check); + } + Label ok; + switch (length) { + case 2: { + if (ShortCutEmitCharacterPair(macro_assembler, one_byte, chars[0], + chars[1], on_failure)) { + } else { + macro_assembler->CheckCharacter(chars[0], &ok); + macro_assembler->CheckNotCharacter(chars[1], on_failure); + macro_assembler->Bind(&ok); + } + break; + } + case 4: + macro_assembler->CheckCharacter(chars[3], &ok); + V8_FALLTHROUGH; + case 3: + macro_assembler->CheckCharacter(chars[0], &ok); + macro_assembler->CheckCharacter(chars[1], &ok); + macro_assembler->CheckNotCharacter(chars[2], on_failure); + macro_assembler->Bind(&ok); + break; + default: + UNREACHABLE(); + } + return true; +} + +static void EmitBoundaryTest(RegExpMacroAssembler* masm, int border, + Label* fall_through, Label* above_or_equal, + Label* below) { + if (below != fall_through) { + masm->CheckCharacterLT(border, below); + if (above_or_equal != fall_through) masm->GoTo(above_or_equal); + } else { + masm->CheckCharacterGT(border - 1, above_or_equal); + } +} + +static void EmitDoubleBoundaryTest(RegExpMacroAssembler* masm, int first, + int last, Label* fall_through, + Label* in_range, Label* out_of_range) { + if (in_range == fall_through) { + if (first == last) { + masm->CheckNotCharacter(first, out_of_range); + } else { + masm->CheckCharacterNotInRange(first, last, out_of_range); + } + } else { + if (first == last) { + masm->CheckCharacter(first, in_range); + } else { + masm->CheckCharacterInRange(first, last, in_range); + } + if (out_of_range != fall_through) masm->GoTo(out_of_range); + } +} + +// even_label is for ranges[i] to ranges[i + 1] where i - start_index is even. +// odd_label is for ranges[i] to ranges[i + 1] where i - start_index is odd. +static void EmitUseLookupTable(RegExpMacroAssembler* masm, + ZoneList* ranges, int start_index, + int end_index, int min_char, Label* fall_through, + Label* even_label, Label* odd_label) { + static const int kSize = RegExpMacroAssembler::kTableSize; + static const int kMask = RegExpMacroAssembler::kTableMask; + + int base = (min_char & ~kMask); + USE(base); + + // Assert that everything is on one kTableSize page. + for (int i = start_index; i <= end_index; i++) { + DCHECK_EQ(ranges->at(i) & ~kMask, base); + } + DCHECK(start_index == 0 || (ranges->at(start_index - 1) & ~kMask) <= base); + + char templ[kSize]; + Label* on_bit_set; + Label* on_bit_clear; + int bit; + if (even_label == fall_through) { + on_bit_set = odd_label; + on_bit_clear = even_label; + bit = 1; + } else { + on_bit_set = even_label; + on_bit_clear = odd_label; + bit = 0; + } + for (int i = 0; i < (ranges->at(start_index) & kMask) && i < kSize; i++) { + templ[i] = bit; + } + int j = 0; + bit ^= 1; + for (int i = start_index; i < end_index; i++) { + for (j = (ranges->at(i) & kMask); j < (ranges->at(i + 1) & kMask); j++) { + templ[j] = bit; + } + bit ^= 1; + } + for (int i = j; i < kSize; i++) { + templ[i] = bit; + } + Factory* factory = masm->isolate()->factory(); + // TODO(erikcorry): Cache these. + Handle ba = factory->NewByteArray(kSize, AllocationType::kOld); + for (int i = 0; i < kSize; i++) { + ba->set(i, templ[i]); + } + masm->CheckBitInTable(ba, on_bit_set); + if (on_bit_clear != fall_through) masm->GoTo(on_bit_clear); +} + +static void CutOutRange(RegExpMacroAssembler* masm, ZoneList* ranges, + int start_index, int end_index, int cut_index, + Label* even_label, Label* odd_label) { + bool odd = (((cut_index - start_index) & 1) == 1); + Label* in_range_label = odd ? odd_label : even_label; + Label dummy; + EmitDoubleBoundaryTest(masm, ranges->at(cut_index), + ranges->at(cut_index + 1) - 1, &dummy, in_range_label, + &dummy); + DCHECK(!dummy.is_linked()); + // Cut out the single range by rewriting the array. This creates a new + // range that is a merger of the two ranges on either side of the one we + // are cutting out. The oddity of the labels is preserved. + for (int j = cut_index; j > start_index; j--) { + ranges->at(j) = ranges->at(j - 1); + } + for (int j = cut_index + 1; j < end_index; j++) { + ranges->at(j) = ranges->at(j + 1); + } +} + +// Unicode case. Split the search space into kSize spaces that are handled +// with recursion. +static void SplitSearchSpace(ZoneList* ranges, int start_index, + int end_index, int* new_start_index, + int* new_end_index, int* border) { + static const int kSize = RegExpMacroAssembler::kTableSize; + static const int kMask = RegExpMacroAssembler::kTableMask; + + int first = ranges->at(start_index); + int last = ranges->at(end_index) - 1; + + *new_start_index = start_index; + *border = (ranges->at(start_index) & ~kMask) + kSize; + while (*new_start_index < end_index) { + if (ranges->at(*new_start_index) > *border) break; + (*new_start_index)++; + } + // new_start_index is the index of the first edge that is beyond the + // current kSize space. + + // For very large search spaces we do a binary chop search of the non-Latin1 + // space instead of just going to the end of the current kSize space. The + // heuristics are complicated a little by the fact that any 128-character + // encoding space can be quickly tested with a table lookup, so we don't + // wish to do binary chop search at a smaller granularity than that. A + // 128-character space can take up a lot of space in the ranges array if, + // for example, we only want to match every second character (eg. the lower + // case characters on some Unicode pages). + int binary_chop_index = (end_index + start_index) / 2; + // The first test ensures that we get to the code that handles the Latin1 + // range with a single not-taken branch, speeding up this important + // character range (even non-Latin1 charset-based text has spaces and + // punctuation). + if (*border - 1 > String::kMaxOneByteCharCode && // Latin1 case. + end_index - start_index > (*new_start_index - start_index) * 2 && + last - first > kSize * 2 && binary_chop_index > *new_start_index && + ranges->at(binary_chop_index) >= first + 2 * kSize) { + int scan_forward_for_section_border = binary_chop_index; + int new_border = (ranges->at(binary_chop_index) | kMask) + 1; + + while (scan_forward_for_section_border < end_index) { + if (ranges->at(scan_forward_for_section_border) > new_border) { + *new_start_index = scan_forward_for_section_border; + *border = new_border; + break; + } + scan_forward_for_section_border++; + } + } + + DCHECK(*new_start_index > start_index); + *new_end_index = *new_start_index - 1; + if (ranges->at(*new_end_index) == *border) { + (*new_end_index)--; + } + if (*border >= ranges->at(end_index)) { + *border = ranges->at(end_index); + *new_start_index = end_index; // Won't be used. + *new_end_index = end_index - 1; + } +} + +// Gets a series of segment boundaries representing a character class. If the +// character is in the range between an even and an odd boundary (counting from +// start_index) then go to even_label, otherwise go to odd_label. We already +// know that the character is in the range of min_char to max_char inclusive. +// Either label can be nullptr indicating backtracking. Either label can also +// be equal to the fall_through label. +static void GenerateBranches(RegExpMacroAssembler* masm, ZoneList* ranges, + int start_index, int end_index, uc32 min_char, + uc32 max_char, Label* fall_through, + Label* even_label, Label* odd_label) { + DCHECK_LE(min_char, String::kMaxUtf16CodeUnit); + DCHECK_LE(max_char, String::kMaxUtf16CodeUnit); + + int first = ranges->at(start_index); + int last = ranges->at(end_index) - 1; + + DCHECK_LT(min_char, first); + + // Just need to test if the character is before or on-or-after + // a particular character. + if (start_index == end_index) { + EmitBoundaryTest(masm, first, fall_through, even_label, odd_label); + return; + } + + // Another almost trivial case: There is one interval in the middle that is + // different from the end intervals. + if (start_index + 1 == end_index) { + EmitDoubleBoundaryTest(masm, first, last, fall_through, even_label, + odd_label); + return; + } + + // It's not worth using table lookup if there are very few intervals in the + // character class. + if (end_index - start_index <= 6) { + // It is faster to test for individual characters, so we look for those + // first, then try arbitrary ranges in the second round. + static int kNoCutIndex = -1; + int cut = kNoCutIndex; + for (int i = start_index; i < end_index; i++) { + if (ranges->at(i) == ranges->at(i + 1) - 1) { + cut = i; + break; + } + } + if (cut == kNoCutIndex) cut = start_index; + CutOutRange(masm, ranges, start_index, end_index, cut, even_label, + odd_label); + DCHECK_GE(end_index - start_index, 2); + GenerateBranches(masm, ranges, start_index + 1, end_index - 1, min_char, + max_char, fall_through, even_label, odd_label); + return; + } + + // If there are a lot of intervals in the regexp, then we will use tables to + // determine whether the character is inside or outside the character class. + static const int kBits = RegExpMacroAssembler::kTableSizeBits; + + if ((max_char >> kBits) == (min_char >> kBits)) { + EmitUseLookupTable(masm, ranges, start_index, end_index, min_char, + fall_through, even_label, odd_label); + return; + } + + if ((min_char >> kBits) != (first >> kBits)) { + masm->CheckCharacterLT(first, odd_label); + GenerateBranches(masm, ranges, start_index + 1, end_index, first, max_char, + fall_through, odd_label, even_label); + return; + } + + int new_start_index = 0; + int new_end_index = 0; + int border = 0; + + SplitSearchSpace(ranges, start_index, end_index, &new_start_index, + &new_end_index, &border); + + Label handle_rest; + Label* above = &handle_rest; + if (border == last + 1) { + // We didn't find any section that started after the limit, so everything + // above the border is one of the terminal labels. + above = (end_index & 1) != (start_index & 1) ? odd_label : even_label; + DCHECK(new_end_index == end_index - 1); + } + + DCHECK_LE(start_index, new_end_index); + DCHECK_LE(new_start_index, end_index); + DCHECK_LT(start_index, new_start_index); + DCHECK_LT(new_end_index, end_index); + DCHECK(new_end_index + 1 == new_start_index || + (new_end_index + 2 == new_start_index && + border == ranges->at(new_end_index + 1))); + DCHECK_LT(min_char, border - 1); + DCHECK_LT(border, max_char); + DCHECK_LT(ranges->at(new_end_index), border); + DCHECK(border < ranges->at(new_start_index) || + (border == ranges->at(new_start_index) && + new_start_index == end_index && new_end_index == end_index - 1 && + border == last + 1)); + DCHECK(new_start_index == 0 || border >= ranges->at(new_start_index - 1)); + + masm->CheckCharacterGT(border - 1, above); + Label dummy; + GenerateBranches(masm, ranges, start_index, new_end_index, min_char, + border - 1, &dummy, even_label, odd_label); + if (handle_rest.is_linked()) { + masm->Bind(&handle_rest); + bool flip = (new_start_index & 1) != (start_index & 1); + GenerateBranches(masm, ranges, new_start_index, end_index, border, max_char, + &dummy, flip ? odd_label : even_label, + flip ? even_label : odd_label); + } +} + +static void EmitCharClass(RegExpMacroAssembler* macro_assembler, + RegExpCharacterClass* cc, bool one_byte, + Label* on_failure, int cp_offset, bool check_offset, + bool preloaded, Zone* zone) { + ZoneList* ranges = cc->ranges(zone); + CharacterRange::Canonicalize(ranges); + + int max_char; + if (one_byte) { + max_char = String::kMaxOneByteCharCode; + } else { + max_char = String::kMaxUtf16CodeUnit; + } + + int range_count = ranges->length(); + + int last_valid_range = range_count - 1; + while (last_valid_range >= 0) { + CharacterRange& range = ranges->at(last_valid_range); + if (range.from() <= max_char) { + break; + } + last_valid_range--; + } + + if (last_valid_range < 0) { + if (!cc->is_negated()) { + macro_assembler->GoTo(on_failure); + } + if (check_offset) { + macro_assembler->CheckPosition(cp_offset, on_failure); + } + return; + } + + if (last_valid_range == 0 && ranges->at(0).IsEverything(max_char)) { + if (cc->is_negated()) { + macro_assembler->GoTo(on_failure); + } else { + // This is a common case hit by non-anchored expressions. + if (check_offset) { + macro_assembler->CheckPosition(cp_offset, on_failure); + } + } + return; + } + + if (!preloaded) { + macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check_offset); + } + + if (cc->is_standard(zone) && macro_assembler->CheckSpecialCharacterClass( + cc->standard_type(), on_failure)) { + return; + } + + // A new list with ascending entries. Each entry is a code unit + // where there is a boundary between code units that are part of + // the class and code units that are not. Normally we insert an + // entry at zero which goes to the failure label, but if there + // was already one there we fall through for success on that entry. + // Subsequent entries have alternating meaning (success/failure). + ZoneList* range_boundaries = + new (zone) ZoneList(last_valid_range, zone); + + bool zeroth_entry_is_failure = !cc->is_negated(); + + for (int i = 0; i <= last_valid_range; i++) { + CharacterRange& range = ranges->at(i); + if (range.from() == 0) { + DCHECK_EQ(i, 0); + zeroth_entry_is_failure = !zeroth_entry_is_failure; + } else { + range_boundaries->Add(range.from(), zone); + } + range_boundaries->Add(range.to() + 1, zone); + } + int end_index = range_boundaries->length() - 1; + if (range_boundaries->at(end_index) > max_char) { + end_index--; + } + + Label fall_through; + GenerateBranches(macro_assembler, range_boundaries, + 0, // start_index. + end_index, + 0, // min_char. + max_char, &fall_through, + zeroth_entry_is_failure ? &fall_through : on_failure, + zeroth_entry_is_failure ? on_failure : &fall_through); + macro_assembler->Bind(&fall_through); +} + +RegExpNode::~RegExpNode() = default; + +RegExpNode::LimitResult RegExpNode::LimitVersions(RegExpCompiler* compiler, + Trace* trace) { + // If we are generating a greedy loop then don't stop and don't reuse code. + if (trace->stop_node() != nullptr) { + return CONTINUE; + } + + RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); + if (trace->is_trivial()) { + if (label_.is_bound() || on_work_list() || !KeepRecursing(compiler)) { + // If a generic version is already scheduled to be generated or we have + // recursed too deeply then just generate a jump to that code. + macro_assembler->GoTo(&label_); + // This will queue it up for generation of a generic version if it hasn't + // already been queued. + compiler->AddWork(this); + return DONE; + } + // Generate generic version of the node and bind the label for later use. + macro_assembler->Bind(&label_); + return CONTINUE; + } + + // We are being asked to make a non-generic version. Keep track of how many + // non-generic versions we generate so as not to overdo it. + trace_count_++; + if (KeepRecursing(compiler) && compiler->optimize() && + trace_count_ < kMaxCopiesCodeGenerated) { + return CONTINUE; + } + + // If we get here code has been generated for this node too many times or + // recursion is too deep. Time to switch to a generic version. The code for + // generic versions above can handle deep recursion properly. + bool was_limiting = compiler->limiting_recursion(); + compiler->set_limiting_recursion(true); + trace->Flush(compiler, this); + compiler->set_limiting_recursion(was_limiting); + return DONE; +} + +bool RegExpNode::KeepRecursing(RegExpCompiler* compiler) { + return !compiler->limiting_recursion() && + compiler->recursion_depth() <= RegExpCompiler::kMaxRecursion; +} + +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); +} + +void ActionNode::FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) { + if (action_type_ == POSITIVE_SUBMATCH_SUCCESS) { + // Anything may follow a positive submatch success, thus we need to accept + // all characters from this position onwards. + bm->SetRest(offset); + } else { + on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); + } + SaveBMInfo(bm, not_at_start, offset); +} + +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); +} + +void AssertionNode::FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) { + // Match the behaviour of EatsAtLeast on this node. + if (assertion_type() == AT_START && not_at_start) return; + on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); + SaveBMInfo(bm, not_at_start, offset); +} + +int BackReferenceNode::EatsAtLeast(int still_to_find, int budget, + bool not_at_start) { + if (read_backward()) return 0; + if (budget <= 0) return 0; + return on_success()->EatsAtLeast(still_to_find, budget - 1, not_at_start); +} + +int TextNode::EatsAtLeast(int still_to_find, int budget, bool not_at_start) { + if (read_backward()) return 0; + 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 NegativeLookaroundChoiceNode::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_->at(1).node(); + return node->EatsAtLeast(still_to_find, budget - 1, not_at_start); +} + +void NegativeLookaroundChoiceNode::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_->at(1).node(); + return node->GetQuickCheckDetails(details, compiler, filled_in, not_at_start); +} + +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; + int choice_count = alternatives_->length(); + budget = (budget - 1) / choice_count; + for (int i = 0; i < choice_count; i++) { + RegExpNode* node = alternatives_->at(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 LoopChoiceNode::EatsAtLeast(int still_to_find, int budget, + bool not_at_start) { + return EatsAtLeastHelper(still_to_find, budget - 1, loop_node_, not_at_start); +} + +int ChoiceNode::EatsAtLeast(int still_to_find, int budget, bool not_at_start) { + return EatsAtLeastHelper(still_to_find, budget, nullptr, not_at_start); +} + +// Takes the left-most 1-bit and smears it out, setting all bits to its right. +static inline uint32_t SmearBitsRight(uint32_t v) { + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + return v; +} + +bool QuickCheckDetails::Rationalize(bool asc) { + bool found_useful_op = false; + uint32_t char_mask; + if (asc) { + char_mask = String::kMaxOneByteCharCode; + } else { + char_mask = String::kMaxUtf16CodeUnit; + } + mask_ = 0; + value_ = 0; + int char_shift = 0; + for (int i = 0; i < characters_; i++) { + Position* pos = &positions_[i]; + if ((pos->mask & String::kMaxOneByteCharCode) != 0) { + found_useful_op = true; + } + mask_ |= (pos->mask & char_mask) << char_shift; + value_ |= (pos->value & char_mask) << char_shift; + char_shift += asc ? 8 : 16; + } + return found_useful_op; +} + +bool RegExpNode::EmitQuickCheck(RegExpCompiler* compiler, + Trace* bounds_check_trace, Trace* trace, + bool preload_has_checked_bounds, + Label* on_possible_success, + QuickCheckDetails* details, + bool fall_through_on_failure) { + if (details->characters() == 0) return false; + GetQuickCheckDetails(details, compiler, 0, + trace->at_start() == Trace::FALSE_VALUE); + if (details->cannot_match()) return false; + if (!details->Rationalize(compiler->one_byte())) return false; + DCHECK(details->characters() == 1 || + compiler->macro_assembler()->CanReadUnaligned()); + uint32_t mask = details->mask(); + uint32_t value = details->value(); + + RegExpMacroAssembler* assembler = compiler->macro_assembler(); + + if (trace->characters_preloaded() != details->characters()) { + DCHECK(trace->cp_offset() == bounds_check_trace->cp_offset()); + // We are attempting to preload the minimum number of characters + // any choice would eat, so if the bounds check fails, then none of the + // choices can succeed, so we can just immediately backtrack, rather + // than go to the next choice. + assembler->LoadCurrentCharacter( + trace->cp_offset(), bounds_check_trace->backtrack(), + !preload_has_checked_bounds, details->characters()); + } + + bool need_mask = true; + + if (details->characters() == 1) { + // If number of characters preloaded is 1 then we used a byte or 16 bit + // load so the value is already masked down. + uint32_t char_mask; + if (compiler->one_byte()) { + char_mask = String::kMaxOneByteCharCode; + } else { + char_mask = String::kMaxUtf16CodeUnit; + } + if ((mask & char_mask) == char_mask) need_mask = false; + mask &= char_mask; + } else { + // For 2-character preloads in one-byte mode or 1-character preloads in + // two-byte mode we also use a 16 bit load with zero extend. + static const uint32_t kTwoByteMask = 0xFFFF; + static const uint32_t kFourByteMask = 0xFFFFFFFF; + if (details->characters() == 2 && compiler->one_byte()) { + if ((mask & kTwoByteMask) == kTwoByteMask) need_mask = false; + } else if (details->characters() == 1 && !compiler->one_byte()) { + if ((mask & kTwoByteMask) == kTwoByteMask) need_mask = false; + } else { + if (mask == kFourByteMask) need_mask = false; + } + } + + if (fall_through_on_failure) { + if (need_mask) { + assembler->CheckCharacterAfterAnd(value, mask, on_possible_success); + } else { + assembler->CheckCharacter(value, on_possible_success); + } + } else { + if (need_mask) { + assembler->CheckNotCharacterAfterAnd(value, mask, trace->backtrack()); + } else { + assembler->CheckNotCharacter(value, trace->backtrack()); + } + } + return true; +} + +// Here is the meat of GetQuickCheckDetails (see also the comment on the +// super-class in the .h file). +// +// We iterate along the text object, building up for each character a +// mask and value that can be used to test for a quick failure to match. +// The masks and values for the positions will be combined into a single +// machine word for the current character width in order to be used in +// generating a quick check. +void TextNode::GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, + int characters_filled_in, + bool not_at_start) { + // Do not collect any quick check details if the text node reads backward, + // since it reads in the opposite direction than we use for quick checks. + if (read_backward()) return; + Isolate* isolate = compiler->macro_assembler()->isolate(); + DCHECK(characters_filled_in < details->characters()); + int characters = details->characters(); + int char_mask; + if (compiler->one_byte()) { + char_mask = String::kMaxOneByteCharCode; + } else { + char_mask = String::kMaxUtf16CodeUnit; + } + for (int k = 0; k < elements()->length(); k++) { + TextElement elm = elements()->at(k); + if (elm.text_type() == TextElement::ATOM) { + Vector quarks = elm.atom()->data(); + for (int i = 0; i < characters && i < quarks.length(); i++) { + QuickCheckDetails::Position* pos = + details->positions(characters_filled_in); + uc16 c = quarks[i]; + if (elm.atom()->ignore_case()) { + unibrow::uchar chars[4]; + int length = GetCaseIndependentLetters( + isolate, c, compiler->one_byte(), chars, 4); + if (length == 0) { + // This can happen because all case variants are non-Latin1, but we + // know the input is Latin1. + details->set_cannot_match(); + pos->determines_perfectly = false; + return; + } + if (length == 1) { + // This letter has no case equivalents, so it's nice and simple + // and the mask-compare will determine definitely whether we have + // a match at this character position. + pos->mask = char_mask; + pos->value = c; + pos->determines_perfectly = true; + } else { + uint32_t common_bits = char_mask; + uint32_t bits = chars[0]; + for (int j = 1; j < length; j++) { + uint32_t differing_bits = ((chars[j] & common_bits) ^ bits); + common_bits ^= differing_bits; + bits &= common_bits; + } + // If length is 2 and common bits has only one zero in it then + // our mask and compare instruction will determine definitely + // whether we have a match at this character position. Otherwise + // it can only be an approximate check. + uint32_t one_zero = (common_bits | ~char_mask); + if (length == 2 && ((~one_zero) & ((~one_zero) - 1)) == 0) { + pos->determines_perfectly = true; + } + pos->mask = common_bits; + pos->value = bits; + } + } else { + // Don't ignore case. Nice simple case where the mask-compare will + // determine definitely whether we have a match at this character + // position. + if (c > char_mask) { + details->set_cannot_match(); + pos->determines_perfectly = false; + return; + } + pos->mask = char_mask; + pos->value = c; + pos->determines_perfectly = true; + } + characters_filled_in++; + DCHECK(characters_filled_in <= details->characters()); + if (characters_filled_in == details->characters()) { + return; + } + } + } else { + QuickCheckDetails::Position* pos = + details->positions(characters_filled_in); + RegExpCharacterClass* tree = elm.char_class(); + ZoneList* ranges = tree->ranges(zone()); + DCHECK(!ranges->is_empty()); + if (tree->is_negated()) { + // A quick check uses multi-character mask and compare. There is no + // useful way to incorporate a negative char class into this scheme + // so we just conservatively create a mask and value that will always + // succeed. + pos->mask = 0; + pos->value = 0; + } else { + int first_range = 0; + while (ranges->at(first_range).from() > char_mask) { + first_range++; + if (first_range == ranges->length()) { + details->set_cannot_match(); + pos->determines_perfectly = false; + return; + } + } + CharacterRange range = ranges->at(first_range); + uc16 from = range.from(); + uc16 to = range.to(); + if (to > char_mask) { + to = char_mask; + } + uint32_t differing_bits = (from ^ to); + // A mask and compare is only perfect if the differing bits form a + // number like 00011111 with one single block of trailing 1s. + if ((differing_bits & (differing_bits + 1)) == 0 && + from + differing_bits == to) { + pos->determines_perfectly = true; + } + uint32_t common_bits = ~SmearBitsRight(differing_bits); + uint32_t bits = (from & common_bits); + for (int i = first_range + 1; i < ranges->length(); i++) { + CharacterRange range = ranges->at(i); + uc16 from = range.from(); + uc16 to = range.to(); + if (from > char_mask) continue; + if (to > char_mask) to = char_mask; + // Here we are combining more ranges into the mask and compare + // value. With each new range the mask becomes more sparse and + // so the chances of a false positive rise. A character class + // with multiple ranges is assumed never to be equivalent to a + // mask and compare operation. + pos->determines_perfectly = false; + uint32_t new_common_bits = (from ^ to); + new_common_bits = ~SmearBitsRight(new_common_bits); + common_bits &= new_common_bits; + bits &= new_common_bits; + uint32_t differing_bits = (from & common_bits) ^ bits; + common_bits ^= differing_bits; + bits &= common_bits; + } + pos->mask = common_bits; + pos->value = bits; + } + characters_filled_in++; + DCHECK(characters_filled_in <= details->characters()); + if (characters_filled_in == details->characters()) { + return; + } + } + } + DCHECK(characters_filled_in != details->characters()); + if (!details->cannot_match()) { + on_success()->GetQuickCheckDetails(details, compiler, characters_filled_in, + true); + } +} + +void QuickCheckDetails::Clear() { + for (int i = 0; i < characters_; i++) { + positions_[i].mask = 0; + positions_[i].value = 0; + positions_[i].determines_perfectly = false; + } + characters_ = 0; +} + +void QuickCheckDetails::Advance(int by, bool one_byte) { + if (by >= characters_ || by < 0) { + DCHECK_IMPLIES(by < 0, characters_ == 0); + Clear(); + return; + } + DCHECK_LE(characters_ - by, 4); + DCHECK_LE(characters_, 4); + for (int i = 0; i < characters_ - by; i++) { + positions_[i] = positions_[by + i]; + } + for (int i = characters_ - by; i < characters_; i++) { + positions_[i].mask = 0; + positions_[i].value = 0; + positions_[i].determines_perfectly = false; + } + characters_ -= by; + // We could change mask_ and value_ here but we would never advance unless + // they had already been used in a check and they won't be used again because + // it would gain us nothing. So there's no point. +} + +void QuickCheckDetails::Merge(QuickCheckDetails* other, int from_index) { + DCHECK(characters_ == other->characters_); + if (other->cannot_match_) { + return; + } + if (cannot_match_) { + *this = *other; + return; + } + for (int i = from_index; i < characters_; i++) { + QuickCheckDetails::Position* pos = positions(i); + QuickCheckDetails::Position* other_pos = other->positions(i); + if (pos->mask != other_pos->mask || pos->value != other_pos->value || + !other_pos->determines_perfectly) { + // Our mask-compare operation will be approximate unless we have the + // exact same operation on both sides of the alternation. + pos->determines_perfectly = false; + } + pos->mask &= other_pos->mask; + pos->value &= pos->mask; + other_pos->value &= pos->mask; + uc16 differing_bits = (pos->value ^ other_pos->value); + pos->mask &= ~differing_bits; + pos->value &= pos->mask; + } +} + +class VisitMarker { + public: + explicit VisitMarker(NodeInfo* info) : info_(info) { + DCHECK(!info->visited); + info->visited = true; + } + ~VisitMarker() { info_->visited = false; } + + private: + NodeInfo* info_; +}; + +RegExpNode* SeqRegExpNode::FilterOneByte(int depth) { + if (info()->replacement_calculated) return replacement(); + if (depth < 0) return this; + DCHECK(!info()->visited); + VisitMarker marker(info()); + return FilterSuccessor(depth - 1); +} + +RegExpNode* SeqRegExpNode::FilterSuccessor(int depth) { + RegExpNode* next = on_success_->FilterOneByte(depth - 1); + if (next == nullptr) return set_replacement(nullptr); + on_success_ = next; + return set_replacement(this); +} + +// We need to check for the following characters: 0x39C 0x3BC 0x178. +bool RangeContainsLatin1Equivalents(CharacterRange range) { + // TODO(dcarney): this could be a lot more efficient. + return range.Contains(0x039C) || range.Contains(0x03BC) || + range.Contains(0x0178); +} + +static bool RangesContainLatin1Equivalents(ZoneList* ranges) { + for (int i = 0; i < ranges->length(); i++) { + // TODO(dcarney): this could be a lot more efficient. + if (RangeContainsLatin1Equivalents(ranges->at(i))) return true; + } + return false; +} + +RegExpNode* TextNode::FilterOneByte(int depth) { + if (info()->replacement_calculated) return replacement(); + if (depth < 0) return this; + DCHECK(!info()->visited); + VisitMarker marker(info()); + int element_count = elements()->length(); + for (int i = 0; i < element_count; i++) { + TextElement elm = elements()->at(i); + if (elm.text_type() == TextElement::ATOM) { + Vector quarks = elm.atom()->data(); + for (int j = 0; j < quarks.length(); j++) { + uint16_t c = quarks[j]; + if (elm.atom()->ignore_case()) { + c = unibrow::Latin1::TryConvertToLatin1(c); + } + if (c > unibrow::Latin1::kMaxChar) return set_replacement(nullptr); + // Replace quark in case we converted to Latin-1. + uint16_t* writable_quarks = const_cast(quarks.begin()); + writable_quarks[j] = c; + } + } else { + DCHECK(elm.text_type() == TextElement::CHAR_CLASS); + RegExpCharacterClass* cc = elm.char_class(); + ZoneList* ranges = cc->ranges(zone()); + 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->at(0).from() == 0 && + ranges->at(0).to() >= String::kMaxOneByteCharCode) { + // This will be handled in a later filter. + if (IgnoreCase(cc->flags()) && RangesContainLatin1Equivalents(ranges)) + continue; + return set_replacement(nullptr); + } + } else { + if (range_count == 0 || + ranges->at(0).from() > String::kMaxOneByteCharCode) { + // This will be handled in a later filter. + if (IgnoreCase(cc->flags()) && RangesContainLatin1Equivalents(ranges)) + continue; + return set_replacement(nullptr); + } + } + } + } + return FilterSuccessor(depth - 1); +} + +RegExpNode* LoopChoiceNode::FilterOneByte(int depth) { + if (info()->replacement_calculated) return replacement(); + if (depth < 0) return this; + if (info()->visited) return this; + { + VisitMarker marker(info()); + + RegExpNode* continue_replacement = continue_node_->FilterOneByte(depth - 1); + // 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::FilterOneByte(depth - 1); +} + +RegExpNode* ChoiceNode::FilterOneByte(int depth) { + 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++) { + GuardedAlternative alternative = alternatives_->at(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_->at(i); + RegExpNode* replacement = alternative.node()->FilterOneByte(depth - 1); + DCHECK(replacement != this); // No missing EMPTY_MATCH_CHECK. + if (replacement != nullptr) { + alternatives_->at(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. + ZoneList* new_alternatives = + new (zone()) ZoneList(surviving, zone()); + for (int i = 0; i < choice_count; i++) { + RegExpNode* replacement = + alternatives_->at(i).node()->FilterOneByte(depth - 1); + if (replacement != nullptr) { + alternatives_->at(i).set_node(replacement); + new_alternatives->Add(alternatives_->at(i), zone()); + } + } + alternatives_ = new_alternatives; + return this; +} + +RegExpNode* NegativeLookaroundChoiceNode::FilterOneByte(int depth) { + 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_->at(1).node(); + RegExpNode* replacement = node->FilterOneByte(depth - 1); + if (replacement == nullptr) return set_replacement(nullptr); + alternatives_->at(1).set_node(replacement); + + RegExpNode* neg_node = alternatives_->at(0).node(); + RegExpNode* neg_replacement = neg_node->FilterOneByte(depth - 1); + // 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_->at(0).set_node(neg_replacement); + return set_replacement(this); +} + +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); +} + +void LoopChoiceNode::FillInBMInfo(Isolate* isolate, 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; + } + ChoiceNode::FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); + SaveBMInfo(bm, not_at_start, offset); +} + +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(); + DCHECK_LT(0, choice_count); + alternatives_->at(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_->at(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); + } +} + +// Check for [0-9A-Z_a-z]. +static void EmitWordCheck(RegExpMacroAssembler* assembler, Label* word, + 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 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(); + + 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->one_byte()) { + assembler->CheckCharacterAfterAnd(0x2028, 0xFFFE, &ok); + } + assembler->CheckCharacter('\n', &ok); + assembler->CheckNotCharacter('\r', new_trace.backtrack()); + } + assembler->Bind(&ok); + on_success->Emit(compiler, &new_trace); +} + +// 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(); + Isolate* isolate = assembler->isolate(); + 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 = + new (zone()) BoyerMooreLookahead(eats_at_least, compiler, zone()); + FillInBMInfo(isolate, 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) { + Label before_non_word; + 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); + Label ok; + BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord); + assembler->GoTo(&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 { + DCHECK(next_is_word_character == Trace::FALSE_VALUE); + BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord); + } +} + +void AssertionNode::BacktrackIfPrevious( + RegExpCompiler* compiler, Trace* trace, + AssertionNode::IfPrevious backtrack_if_previous) { + RegExpMacroAssembler* assembler = compiler->macro_assembler(); + Trace new_trace(*trace); + new_trace.InvalidateCurrentCharacter(); + + Label fall_through, dummy; + + Label* non_word = backtrack_if_previous == kIsNonWord ? new_trace.backtrack() + : &fall_through; + Label* word = backtrack_if_previous == kIsNonWord ? &fall_through + : new_trace.backtrack(); + + if (new_trace.cp_offset() == 0) { + // The start of input counts as a non-word character, so the question is + // decided if we are at the start. + assembler->CheckAtStart(non_word); + } + // 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, &dummy, false); + EmitWordCheck(assembler, word, non_word, backtrack_if_previous == kIsNonWord); + + assembler->Bind(&fall_through); + on_success()->Emit(compiler, &new_trace); +} + +void AssertionNode::GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, + int filled_in, bool not_at_start) { + if (assertion_type_ == AT_START && not_at_start) { + details->set_cannot_match(); + return; + } + return on_success()->GetQuickCheckDetails(details, compiler, filled_in, + not_at_start); +} + +void AssertionNode::Emit(RegExpCompiler* compiler, Trace* trace) { + RegExpMacroAssembler* assembler = compiler->macro_assembler(); + switch (assertion_type_) { + case AT_END: { + Label ok; + assembler->CheckPosition(trace->cp_offset(), &ok); + assembler->GoTo(trace->backtrack()); + assembler->Bind(&ok); + break; + } + case AT_START: { + if (trace->at_start() == Trace::FALSE_VALUE) { + assembler->GoTo(trace->backtrack()); + return; + } + if (trace->at_start() == Trace::UNKNOWN) { + assembler->CheckNotAtStart(trace->cp_offset(), trace->backtrack()); + Trace at_start_trace = *trace; + at_start_trace.set_at_start(Trace::TRUE_VALUE); + on_success()->Emit(compiler, &at_start_trace); + return; + } + } break; + case AFTER_NEWLINE: + EmitHat(compiler, on_success(), trace); + return; + case AT_BOUNDARY: + case AT_NON_BOUNDARY: { + EmitBoundaryCheck(compiler, trace); + return; + } + } + on_success()->Emit(compiler, trace); +} + +static bool DeterminedAlready(QuickCheckDetails* quick_check, int offset) { + if (quick_check == nullptr) return false; + if (offset >= quick_check->characters()) return false; + return quick_check->positions(offset)->determines_perfectly; +} + +static void UpdateBoundsCheck(int index, int* checked_up_to) { + if (index > *checked_up_to) { + *checked_up_to = index; + } +} + +// We call this repeatedly to generate code for each pass over the text node. +// The passes are in increasing order of difficulty because we hope one +// of the first passes will fail in which case we are saved the work of the +// later passes. for example for the case independent regexp /%[asdfghjkl]a/ +// we will check the '%' in the first pass, the case independent 'a' in the +// second pass and the character class in the last pass. +// +// The passes are done from right to left, so for example to test for /bar/ +// we will first test for an 'r' with offset 2, then an 'a' with offset 1 +// and then a 'b' with offset 0. This means we can avoid the end-of-input +// bounds check most of the time. In the example we only need to check for +// end-of-input when loading the putative 'r'. +// +// A slight complication involves the fact that the first character may already +// be fetched into a register by the previous node. In this case we want to +// do the test for that character first. We do this in separate passes. The +// 'preloaded' argument indicates that we are doing such a 'pass'. If such a +// pass has been performed then subsequent passes will have true in +// first_element_checked to indicate that that character does not need to be +// checked again. +// +// In addition to all this we are passed a Trace, which can +// contain an AlternativeGeneration object. In this AlternativeGeneration +// object we can see details of any quick check that was already passed in +// order to get to the code we are now generating. The quick check can involve +// loading characters, which means we do not need to recheck the bounds +// up to the limit the quick check already checked. In addition the quick +// check can have involved a mask and compare operation which may simplify +// or obviate the need for further checks at some character positions. +void TextNode::TextEmitPass(RegExpCompiler* compiler, TextEmitPassType pass, + bool preloaded, Trace* trace, + bool first_element_checked, int* checked_up_to) { + RegExpMacroAssembler* assembler = compiler->macro_assembler(); + Isolate* isolate = assembler->isolate(); + bool one_byte = compiler->one_byte(); + Label* backtrack = trace->backtrack(); + QuickCheckDetails* quick_check = trace->quick_check_performed(); + int element_count = elements()->length(); + int backward_offset = read_backward() ? -Length() : 0; + for (int i = preloaded ? 0 : element_count - 1; i >= 0; i--) { + TextElement elm = elements()->at(i); + int cp_offset = trace->cp_offset() + elm.cp_offset() + backward_offset; + if (elm.text_type() == TextElement::ATOM) { + if (SkipPass(pass, elm.atom()->ignore_case())) continue; + Vector quarks = elm.atom()->data(); + for (int j = preloaded ? 0 : quarks.length() - 1; j >= 0; j--) { + if (first_element_checked && i == 0 && j == 0) continue; + if (DeterminedAlready(quick_check, elm.cp_offset() + j)) continue; + EmitCharacterFunction* emit_function = nullptr; + uc16 quark = quarks[j]; + if (elm.atom()->ignore_case()) { + // Everywhere else we assume that a non-Latin-1 character cannot match + // a Latin-1 character. Avoid the cases where this is assumption is + // invalid by using the Latin1 equivalent instead. + quark = unibrow::Latin1::TryConvertToLatin1(quark); + } + switch (pass) { + case NON_LATIN1_MATCH: + DCHECK(one_byte); + if (quark > String::kMaxOneByteCharCode) { + assembler->GoTo(backtrack); + return; + } + break; + case NON_LETTER_CHARACTER_MATCH: + emit_function = &EmitAtomNonLetter; + break; + case SIMPLE_CHARACTER_MATCH: + emit_function = &EmitSimpleCharacter; + break; + case CASE_CHARACTER_MATCH: + emit_function = &EmitAtomLetter; + break; + default: + break; + } + if (emit_function != nullptr) { + bool bounds_check = *checked_up_to < cp_offset + j || read_backward(); + bool bound_checked = + emit_function(isolate, compiler, quark, backtrack, cp_offset + j, + bounds_check, preloaded); + if (bound_checked) UpdateBoundsCheck(cp_offset + j, checked_up_to); + } + } + } else { + DCHECK_EQ(TextElement::CHAR_CLASS, elm.text_type()); + if (pass == CHARACTER_CLASS_MATCH) { + if (first_element_checked && i == 0) continue; + if (DeterminedAlready(quick_check, elm.cp_offset())) continue; + RegExpCharacterClass* cc = elm.char_class(); + bool bounds_check = *checked_up_to < cp_offset || read_backward(); + EmitCharClass(assembler, cc, one_byte, backtrack, cp_offset, + bounds_check, preloaded, zone()); + UpdateBoundsCheck(cp_offset, checked_up_to); + } + } + } +} + +int TextNode::Length() { + TextElement elm = elements()->last(); + DCHECK_LE(0, elm.cp_offset()); + return elm.cp_offset() + elm.length(); +} + +bool TextNode::SkipPass(TextEmitPassType pass, bool ignore_case) { + if (ignore_case) { + return pass == SIMPLE_CHARACTER_MATCH; + } else { + return pass == NON_LETTER_CHARACTER_MATCH || pass == CASE_CHARACTER_MATCH; + } +} + +TextNode* TextNode::CreateForCharacterRanges(Zone* zone, + ZoneList* ranges, + bool read_backward, + RegExpNode* on_success, + JSRegExp::Flags flags) { + DCHECK_NOT_NULL(ranges); + ZoneList* elms = new (zone) ZoneList(1, zone); + elms->Add(TextElement::CharClass( + new (zone) RegExpCharacterClass(zone, ranges, flags)), + zone); + return new (zone) TextNode(elms, read_backward, on_success); +} + +TextNode* TextNode::CreateForSurrogatePair(Zone* zone, CharacterRange lead, + CharacterRange trail, + bool read_backward, + RegExpNode* on_success, + JSRegExp::Flags flags) { + ZoneList* lead_ranges = CharacterRange::List(zone, lead); + ZoneList* trail_ranges = CharacterRange::List(zone, trail); + ZoneList* elms = new (zone) ZoneList(2, zone); + elms->Add(TextElement::CharClass( + new (zone) RegExpCharacterClass(zone, lead_ranges, flags)), + zone); + elms->Add(TextElement::CharClass( + new (zone) RegExpCharacterClass(zone, trail_ranges, flags)), + zone); + return new (zone) TextNode(elms, read_backward, on_success); +} + +// This generates the code to match a text node. A text node can contain +// straight character sequences (possibly to be matched in a case-independent +// way) and character classes. For efficiency we do not do this in a single +// pass from left to right. Instead we pass over the text node several times, +// emitting code for some character positions every time. See the comment on +// TextEmitPass for details. +void TextNode::Emit(RegExpCompiler* compiler, Trace* trace) { + LimitResult limit_result = LimitVersions(compiler, trace); + if (limit_result == DONE) return; + DCHECK(limit_result == CONTINUE); + + if (trace->cp_offset() + Length() > RegExpMacroAssembler::kMaxCPOffset) { + compiler->SetRegExpTooBig(); + return; + } + + if (compiler->one_byte()) { + int dummy = 0; + TextEmitPass(compiler, NON_LATIN1_MATCH, false, trace, false, &dummy); + } + + bool first_elt_done = false; + int bound_checked_to = trace->cp_offset() - 1; + bound_checked_to += trace->bound_checked_up_to(); + + // If a character is preloaded into the current character register then + // check that now. + if (trace->characters_preloaded() == 1) { + for (int pass = kFirstRealPass; pass <= kLastPass; pass++) { + TextEmitPass(compiler, static_cast(pass), true, trace, + false, &bound_checked_to); + } + first_elt_done = true; + } + + for (int pass = kFirstRealPass; pass <= kLastPass; pass++) { + TextEmitPass(compiler, static_cast(pass), false, trace, + first_elt_done, &bound_checked_to); + } + + Trace successor_trace(*trace); + // If we advance backward, we may end up at the start. + successor_trace.AdvanceCurrentPositionInTrace( + read_backward() ? -Length() : Length(), compiler); + successor_trace.set_at_start(read_backward() ? Trace::UNKNOWN + : Trace::FALSE_VALUE); + RecursionCheck rc(compiler); + on_success()->Emit(compiler, &successor_trace); +} + +void Trace::InvalidateCurrentCharacter() { characters_preloaded_ = 0; } + +void Trace::AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler) { + // 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->one_byte()); + 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 TextNode::MakeCaseIndependent(Isolate* isolate, bool is_one_byte) { + int element_count = elements()->length(); + for (int i = 0; i < element_count; i++) { + TextElement elm = elements()->at(i); + if (elm.text_type() == TextElement::CHAR_CLASS) { + RegExpCharacterClass* cc = elm.char_class(); +#ifdef V8_INTL_SUPPORT + bool case_equivalents_already_added = + NeedsUnicodeCaseEquivalents(cc->flags()); +#else + bool case_equivalents_already_added = false; +#endif + if (IgnoreCase(cc->flags()) && !case_equivalents_already_added) { + // 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(zone())) continue; + ZoneList* ranges = cc->ranges(zone()); + CharacterRange::AddCaseEquivalents(isolate, zone(), ranges, + is_one_byte); + } + } + } +} + +int TextNode::GreedyLoopTextLength() { return Length(); } + +RegExpNode* TextNode::GetSuccessorOfOmnivorousTextNode( + RegExpCompiler* compiler) { + if (read_backward()) return nullptr; + if (elements()->length() != 1) return nullptr; + TextElement elm = elements()->at(0); + if (elm.text_type() != TextElement::CHAR_CLASS) return nullptr; + RegExpCharacterClass* node = elm.char_class(); + ZoneList* ranges = node->ranges(zone()); + CharacterRange::Canonicalize(ranges); + if (node->is_negated()) { + return ranges->length() == 0 ? on_success() : nullptr; + } + if (ranges->length() != 1) return nullptr; + uint32_t max_char; + if (compiler->one_byte()) { + max_char = String::kMaxOneByteCharCode; + } else { + max_char = String::kMaxUtf16CodeUnit; + } + return ranges->at(0).IsEverything(max_char) ? on_success() : nullptr; +} + +// Finds the fixed match length of a sequence of nodes that goes from +// this alternative and back to this choice node. If there are variable +// length nodes or other complications in the way then return a sentinel +// value indicating that a greedy loop cannot be constructed. +int ChoiceNode::GreedyLoopTextLengthForAlternative( + GuardedAlternative* alternative) { + int length = 0; + RegExpNode* node = alternative->node(); + // Later we will generate code for all these text nodes using recursion + // so we have to limit the max number. + int recursion_depth = 0; + while (node != this) { + if (recursion_depth++ > RegExpCompiler::kMaxRecursion) { + return kNodeIsTooComplexForGreedyLoops; + } + int node_length = node->GreedyLoopTextLength(); + if (node_length == kNodeIsTooComplexForGreedyLoops) { + return kNodeIsTooComplexForGreedyLoops; + } + length += node_length; + SeqRegExpNode* seq_node = static_cast(node); + node = seq_node->on_success(); + } + return read_backward() ? -length : length; +} + +void LoopChoiceNode::AddLoopAlternative(GuardedAlternative alt) { + DCHECK_NULL(loop_node_); + AddAlternative(alt); + loop_node_ = alt.node(); +} + +void LoopChoiceNode::AddContinueAlternative(GuardedAlternative alt) { + DCHECK_NULL(continue_node_); + AddAlternative(alt); + continue_node_ = alt.node(); +} + +void LoopChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) { + RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); + if (trace->stop_node() == this) { + // Back edge of greedy optimized loop node graph. + int text_length = + GreedyLoopTextLengthForAlternative(&(alternatives_->at(0))); + DCHECK_NE(kNodeIsTooComplexForGreedyLoops, text_length); + // Update the counter-based backtracking info on the stack. This is an + // optimization for greedy loops (see below). + DCHECK(trace->cp_offset() == text_length); + macro_assembler->AdvanceCurrentPosition(text_length); + macro_assembler->GoTo(trace->loop_label()); + return; + } + DCHECK_NULL(trace->stop_node()); + if (!trace->is_trivial()) { + trace->Flush(compiler, this); + return; + } + ChoiceNode::Emit(compiler, trace); +} + +int ChoiceNode::CalculatePreloadCharacters(RegExpCompiler* compiler, + int eats_at_least) { + int preload_characters = Min(4, eats_at_least); + DCHECK_LE(preload_characters, 4); + if (compiler->macro_assembler()->CanReadUnaligned()) { + bool one_byte = compiler->one_byte(); + if (one_byte) { + // We can't preload 3 characters because there is no machine instruction + // to do that. We can't just load 4 because we could be reading + // beyond the end of the string, which could cause a memory fault. + if (preload_characters == 3) preload_characters = 2; + } else { + if (preload_characters > 2) preload_characters = 2; + } + } else { + if (preload_characters > 1) preload_characters = 1; + } + return preload_characters; +} + +// This class is used when generating the alternatives in a choice node. It +// records the way the alternative is being code generated. +class AlternativeGeneration : public Malloced { + public: + AlternativeGeneration() + : possible_success(), + expects_preload(false), + after(), + quick_check_details() {} + Label possible_success; + bool expects_preload; + Label after; + QuickCheckDetails quick_check_details; +}; + +// Creates a list of AlternativeGenerations. If the list has a reasonable +// size then it is on the stack, otherwise the excess is on the heap. +class AlternativeGenerationList { + public: + AlternativeGenerationList(int count, Zone* zone) : alt_gens_(count, zone) { + for (int i = 0; i < count && i < kAFew; i++) { + alt_gens_.Add(a_few_alt_gens_ + i, zone); + } + for (int i = kAFew; i < count; i++) { + alt_gens_.Add(new AlternativeGeneration(), zone); + } + } + ~AlternativeGenerationList() { + for (int i = kAFew; i < alt_gens_.length(); i++) { + delete alt_gens_[i]; + alt_gens_[i] = nullptr; + } + } + + AlternativeGeneration* at(int i) { return alt_gens_[i]; } + + private: + static const int kAFew = 10; + ZoneList alt_gens_; + AlternativeGeneration a_few_alt_gens_[kAFew]; +}; + +void BoyerMoorePositionInfo::Set(int character) { + SetInterval(Interval(character, character)); +} + +namespace { + +ContainedInLattice AddRange(ContainedInLattice containment, const int* ranges, + int ranges_length, Interval new_range) { + DCHECK_EQ(1, ranges_length & 1); + DCHECK_EQ(String::kMaxCodePoint + 1, ranges[ranges_length - 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; +} + +int BitsetFirstSetBit(BoyerMoorePositionInfo::Bitset bitset) { + STATIC_ASSERT(BoyerMoorePositionInfo::kMapSize == + 2 * kInt64Size * kBitsPerByte); + + // Slight fiddling is needed here, since the bitset is of length 128 while + // CountTrailingZeros requires an integral type and std::bitset can only + // convert to unsigned long long. So we handle the most- and least-significant + // bits separately. + + { + static constexpr BoyerMoorePositionInfo::Bitset mask(~uint64_t{0}); + BoyerMoorePositionInfo::Bitset masked_bitset = bitset & mask; + STATIC_ASSERT(kInt64Size >= sizeof(decltype(masked_bitset.to_ullong()))); + uint64_t lsb = masked_bitset.to_ullong(); + if (lsb != 0) return base::bits::CountTrailingZeros(lsb); + } + + { + BoyerMoorePositionInfo::Bitset masked_bitset = bitset >> 64; + uint64_t msb = masked_bitset.to_ullong(); + if (msb != 0) return 64 + base::bits::CountTrailingZeros(msb); + } + + return -1; +} + +} // namespace + +void BoyerMoorePositionInfo::SetInterval(const Interval& interval) { + w_ = AddRange(w_, kWordRanges, kWordRangeCount, interval); + + if (interval.size() >= kMapSize) { + map_count_ = kMapSize; + map_.set(); + return; + } + + for (int i = interval.from(); i <= interval.to(); i++) { + int mod_character = (i & kMask); + if (!map_[mod_character]) { + map_count_++; + map_.set(mod_character); + } + if (map_count_ == kMapSize) return; + } +} + +void BoyerMoorePositionInfo::SetAll() { + w_ = kLatticeUnknown; + if (map_count_ != kMapSize) { + map_count_ = kMapSize; + map_.set(); + } +} + +BoyerMooreLookahead::BoyerMooreLookahead(int length, RegExpCompiler* compiler, + Zone* zone) + : length_(length), compiler_(compiler) { + if (compiler->one_byte()) { + max_char_ = String::kMaxOneByteCharCode; + } else { + max_char_ = String::kMaxUtf16CodeUnit; + } + bitmaps_ = new (zone) ZoneList(length, zone); + for (int i = 0; i < length; i++) { + bitmaps_->Add(new (zone) BoyerMoorePositionInfo(), zone); + } +} + +// 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; + + BoyerMoorePositionInfo::Bitset union_bitset; + for (; i < length_ && Count(i) <= max_number_of_chars; i++) { + union_bitset |= bitmaps_->at(i)->raw_bitset(); + } + + int frequency = 0; + + // Iterate only over set bits. + int j; + while ((j = BitsetFirstSetBit(union_bitset)) != -1) { + DCHECK(union_bitset[j]); // Sanity check. + // 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; + union_bitset.reset(j); + } + + // 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_->one_byte() ? 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, + Handle boolean_skip_table) { + const int kSkipArrayEntry = 0; + const int kDontSkipArrayEntry = 1; + + std::memset(boolean_skip_table->GetDataStartAddress(), kSkipArrayEntry, + boolean_skip_table->length()); + + for (int i = max_lookahead; i >= min_lookahead; i--) { + BoyerMoorePositionInfo::Bitset bitset = bitmaps_->at(i)->raw_bitset(); + + // Iterate only over set bits. + int j; + while ((j = BitsetFirstSetBit(bitset)) != -1) { + DCHECK(bitset[j]); // Sanity check. + boolean_skip_table->set(j, kDontSkipArrayEntry); + bitset.reset(j); + } + } + + const int skip = max_lookahead + 1 - min_lookahead; + return skip; +} + +// See comment above on the implementation of GetSkipTable. +void BoyerMooreLookahead::EmitSkipInstructions(RegExpMacroAssembler* masm) { + const int kSize = RegExpMacroAssembler::kTableSize; + + int min_lookahead = 0; + int max_lookahead = 0; + + if (!FindWorthwhileInterval(&min_lookahead, &max_lookahead)) return; + + // Check if we only have a single non-empty position info, and that info + // contains precisely one character. + bool found_single_character = false; + int single_character = 0; + for (int i = max_lookahead; i >= min_lookahead; i--) { + BoyerMoorePositionInfo* map = bitmaps_->at(i); + if (map->map_count() == 0) continue; + + if (found_single_character || map->map_count() > 1) { + found_single_character = false; + break; + } + + DCHECK(!found_single_character); + DCHECK_EQ(map->map_count(), 1); + + found_single_character = true; + single_character = BitsetFirstSetBit(map->raw_bitset()); + + DCHECK_NE(single_character, -1); + } + + 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; + } + + if (found_single_character) { + 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->GoTo(&again); + masm->Bind(&cont); + return; + } + + Factory* factory = masm->isolate()->factory(); + Handle boolean_skip_table = + factory->NewByteArray(kSize, AllocationType::kOld); + int skip_distance = + GetSkipTable(min_lookahead, max_lookahead, boolean_skip_table); + DCHECK_NE(0, skip_distance); + + Label cont, again; + masm->Bind(&again); + masm->LoadCurrentCharacter(max_lookahead, &cont, true); + masm->CheckBitInTable(boolean_skip_table, &cont); + masm->AdvanceCurrentPosition(skip_distance); + masm->GoTo(&again); + masm->Bind(&cont); +} + +/* Code generation for choice nodes. + * + * We generate quick checks that do a mask and compare to eliminate a + * choice. If the quick check succeeds then it jumps to the continuation to + * do slow checks and check subsequent nodes. If it fails (the common case) + * it falls through to the next choice. + * + * Here is the desired flow graph. Nodes directly below each other imply + * fallthrough. Alternatives 1 and 2 have quick checks. Alternative + * 3 doesn't have a quick check so we have to call the slow check. + * Nodes are marked Qn for quick checks and Sn for slow checks. The entire + * regexp continuation is generated directly after the Sn node, up to the + * next GoTo if we decide to reuse some already generated code. Some + * nodes expect preload_characters to be preloaded into the current + * character register. R nodes do this preloading. Vertices are marked + * F for failures and S for success (possible success in the case of quick + * nodes). L, V, < and > are used as arrow heads. + * + * ----------> R + * | + * V + * Q1 -----> S1 + * | S / + * F| / + * | F/ + * | / + * | R + * | / + * V L + * Q2 -----> S2 + * | S / + * F| / + * | F/ + * | / + * | R + * | / + * V L + * S3 + * | + * F| + * | + * R + * | + * backtrack V + * <----------Q4 + * \ F | + * \ |S + * \ F V + * \-----S4 + * + * For greedy loops we push the current position, then generate the code that + * eats the input specially in EmitGreedyLoop. The other choice (the + * continuation) is generated by the normal code in EmitChoices, and steps back + * in the input to the starting position when it fails to match. The loop code + * looks like this (U is the unwind code that steps back in the greedy loop). + * + * _____ + * / \ + * V | + * ----------> S1 | + * /| | + * / |S | + * F/ \_____/ + * / + * |<----- + * | \ + * V |S + * Q2 ---> U----->backtrack + * | F / + * S| / + * V F / + * S2--/ + */ + +GreedyLoopState::GreedyLoopState(bool not_at_start) { + counter_backtrack_trace_.set_backtrack(&label_); + if (not_at_start) counter_backtrack_trace_.set_at_start(Trace::FALSE_VALUE); +} + +void ChoiceNode::AssertGuardsMentionRegisters(Trace* trace) { +#ifdef DEBUG + int choice_count = alternatives_->length(); + for (int i = 0; i < choice_count - 1; i++) { + GuardedAlternative alternative = alternatives_->at(i); + ZoneList* guards = alternative.guards(); + int guard_count = (guards == nullptr) ? 0 : guards->length(); + for (int j = 0; j < guard_count; j++) { + DCHECK(!trace->mentions_reg(guards->at(j)->reg())); + } + } +#endif +} + +void ChoiceNode::SetUpPreLoad(RegExpCompiler* compiler, Trace* current_trace, + PreloadState* state) { + if (state->eats_at_least_ == PreloadState::kEatsAtLeastNotYetInitialized) { + // Save some time by looking at most one machine word ahead. + state->eats_at_least_ = + EatsAtLeast(compiler->one_byte() ? 4 : 2, kRecursionBudget, + current_trace->at_start() == Trace::FALSE_VALUE); + } + state->preload_characters_ = + CalculatePreloadCharacters(compiler, state->eats_at_least_); + + state->preload_is_current_ = + (current_trace->characters_preloaded() == state->preload_characters_); + state->preload_has_checked_bounds_ = state->preload_is_current_; +} + +void ChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) { + int choice_count = alternatives_->length(); + + if (choice_count == 1 && alternatives_->at(0).guards() == nullptr) { + alternatives_->at(0).node()->Emit(compiler, trace); + return; + } + + AssertGuardsMentionRegisters(trace); + + LimitResult limit_result = LimitVersions(compiler, trace); + if (limit_result == DONE) return; + DCHECK(limit_result == CONTINUE); + + // For loop nodes we already flushed (see LoopChoiceNode::Emit), but for + // other choice nodes we only flush if we are out of code size budget. + if (trace->flush_budget() == 0 && trace->actions() != nullptr) { + trace->Flush(compiler, this); + return; + } + + RecursionCheck rc(compiler); + + PreloadState preload; + preload.init(); + GreedyLoopState greedy_loop_state(not_at_start()); + + int text_length = GreedyLoopTextLengthForAlternative(&alternatives_->at(0)); + AlternativeGenerationList alt_gens(choice_count, zone()); + + if (choice_count > 1 && text_length != kNodeIsTooComplexForGreedyLoops) { + trace = EmitGreedyLoop(compiler, trace, &alt_gens, &preload, + &greedy_loop_state, text_length); + } else { + // TODO(erikcorry): Delete this. We don't need this label, but it makes us + // match the traces produced pre-cleanup. + Label second_choice; + compiler->macro_assembler()->Bind(&second_choice); + + preload.eats_at_least_ = EmitOptimizedUnanchoredSearch(compiler, trace); + + EmitChoices(compiler, &alt_gens, 0, trace, &preload); + } + + // At this point we need to generate slow checks for the alternatives where + // the quick check was inlined. We can recognize these because the associated + // label was bound. + int new_flush_budget = trace->flush_budget() / choice_count; + for (int i = 0; i < choice_count; i++) { + AlternativeGeneration* alt_gen = alt_gens.at(i); + Trace new_trace(*trace); + // If there are actions to be flushed we have to limit how many times + // they are flushed. Take the budget of the parent trace and distribute + // it fairly amongst the children. + if (new_trace.actions() != nullptr) { + new_trace.set_flush_budget(new_flush_budget); + } + bool next_expects_preload = + i == choice_count - 1 ? false : alt_gens.at(i + 1)->expects_preload; + EmitOutOfLineContinuation(compiler, &new_trace, alternatives_->at(i), + alt_gen, preload.preload_characters_, + next_expects_preload); + } +} + +Trace* ChoiceNode::EmitGreedyLoop(RegExpCompiler* compiler, Trace* trace, + AlternativeGenerationList* alt_gens, + PreloadState* preload, + GreedyLoopState* greedy_loop_state, + int text_length) { + RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); + // Here we have special handling for greedy loops containing only text nodes + // and other simple nodes. These are handled by pushing the current + // position on the stack and then incrementing the current position each + // time around the switch. On backtrack we decrement the current position + // and check it against the pushed value. This avoids pushing backtrack + // information for each iteration of the loop, which could take up a lot of + // space. + DCHECK(trace->stop_node() == nullptr); + macro_assembler->PushCurrentPosition(); + Label greedy_match_failed; + Trace greedy_match_trace; + if (not_at_start()) greedy_match_trace.set_at_start(Trace::FALSE_VALUE); + greedy_match_trace.set_backtrack(&greedy_match_failed); + Label loop_label; + macro_assembler->Bind(&loop_label); + greedy_match_trace.set_stop_node(this); + greedy_match_trace.set_loop_label(&loop_label); + alternatives_->at(0).node()->Emit(compiler, &greedy_match_trace); + macro_assembler->Bind(&greedy_match_failed); + + Label second_choice; // For use in greedy matches. + macro_assembler->Bind(&second_choice); + + Trace* new_trace = greedy_loop_state->counter_backtrack_trace(); + + EmitChoices(compiler, alt_gens, 1, new_trace, preload); + + macro_assembler->Bind(greedy_loop_state->label()); + // If we have unwound to the bottom then backtrack. + macro_assembler->CheckGreedyLoop(trace->backtrack()); + // Otherwise try the second priority at an earlier position. + macro_assembler->AdvanceCurrentPosition(-text_length); + macro_assembler->GoTo(&second_choice); + return new_trace; +} + +int ChoiceNode::EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler, + Trace* trace) { + int eats_at_least = PreloadState::kEatsAtLeastNotYetInitialized; + if (alternatives_->length() != 2) return eats_at_least; + + GuardedAlternative alt1 = alternatives_->at(1); + if (alt1.guards() != nullptr && alt1.guards()->length() != 0) { + return eats_at_least; + } + RegExpNode* eats_anything_node = alt1.node(); + if (eats_anything_node->GetSuccessorOfOmnivorousTextNode(compiler) != this) { + return eats_at_least; + } + + // Really we should be creating a new trace when we execute this function, + // but there is no need, because the code it generates cannot backtrack, and + // we always arrive here with a trivial trace (since it's the entry to a + // loop. That also implies that there are no preloaded characters, which is + // good, because it means we won't be violating any assumptions by + // overwriting those characters with new load instructions. + DCHECK(trace->is_trivial()); + + RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); + Isolate* isolate = macro_assembler->isolate(); + // At this point we know that we are at a non-greedy loop that will eat + // any character one at a time. Any non-anchored regexp has such a + // loop prepended to it in order to find where it starts. We look for + // a pattern of the form ...abc... where we can look 6 characters ahead + // and step forwards 3 if the character is not one of abc. Abc need + // not be atoms, they can be any reasonably limited character class or + // small alternation. + BoyerMooreLookahead* bm = bm_info(false); + if (bm == nullptr) { + eats_at_least = + Min(kMaxLookaheadForBoyerMoore, + EatsAtLeast(kMaxLookaheadForBoyerMoore, kRecursionBudget, false)); + if (eats_at_least >= 1) { + bm = new (zone()) BoyerMooreLookahead(eats_at_least, compiler, zone()); + GuardedAlternative alt0 = alternatives_->at(0); + alt0.node()->FillInBMInfo(isolate, 0, kRecursionBudget, bm, false); + } + } + if (bm != nullptr) { + bm->EmitSkipInstructions(macro_assembler); + } + return eats_at_least; +} + +void ChoiceNode::EmitChoices(RegExpCompiler* compiler, + AlternativeGenerationList* alt_gens, + int first_choice, Trace* trace, + PreloadState* preload) { + RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); + SetUpPreLoad(compiler, trace, preload); + + // For now we just call all choices one after the other. The idea ultimately + // is to use the Dispatch table to try only the relevant ones. + int choice_count = alternatives_->length(); + + int new_flush_budget = trace->flush_budget() / choice_count; + + for (int i = first_choice; i < choice_count; i++) { + bool is_last = i == choice_count - 1; + bool fall_through_on_failure = !is_last; + GuardedAlternative alternative = alternatives_->at(i); + AlternativeGeneration* alt_gen = alt_gens->at(i); + alt_gen->quick_check_details.set_characters(preload->preload_characters_); + ZoneList* guards = alternative.guards(); + int guard_count = (guards == nullptr) ? 0 : guards->length(); + Trace new_trace(*trace); + new_trace.set_characters_preloaded( + preload->preload_is_current_ ? preload->preload_characters_ : 0); + if (preload->preload_has_checked_bounds_) { + new_trace.set_bound_checked_up_to(preload->preload_characters_); + } + new_trace.quick_check_performed()->Clear(); + if (not_at_start_) new_trace.set_at_start(Trace::FALSE_VALUE); + if (!is_last) { + new_trace.set_backtrack(&alt_gen->after); + } + alt_gen->expects_preload = preload->preload_is_current_; + bool generate_full_check_inline = false; + if (compiler->optimize() && + try_to_emit_quick_check_for_alternative(i == 0) && + alternative.node()->EmitQuickCheck( + compiler, trace, &new_trace, preload->preload_has_checked_bounds_, + &alt_gen->possible_success, &alt_gen->quick_check_details, + fall_through_on_failure)) { + // Quick check was generated for this choice. + preload->preload_is_current_ = true; + preload->preload_has_checked_bounds_ = true; + // If we generated the quick check to fall through on possible success, + // we now need to generate the full check inline. + if (!fall_through_on_failure) { + macro_assembler->Bind(&alt_gen->possible_success); + new_trace.set_quick_check_performed(&alt_gen->quick_check_details); + new_trace.set_characters_preloaded(preload->preload_characters_); + new_trace.set_bound_checked_up_to(preload->preload_characters_); + generate_full_check_inline = true; + } + } else if (alt_gen->quick_check_details.cannot_match()) { + if (!fall_through_on_failure) { + macro_assembler->GoTo(trace->backtrack()); + } + continue; + } else { + // No quick check was generated. Put the full code here. + // If this is not the first choice then there could be slow checks from + // previous cases that go here when they fail. There's no reason to + // insist that they preload characters since the slow check we are about + // to generate probably can't use it. + if (i != first_choice) { + alt_gen->expects_preload = false; + new_trace.InvalidateCurrentCharacter(); + } + generate_full_check_inline = true; + } + if (generate_full_check_inline) { + if (new_trace.actions() != nullptr) { + new_trace.set_flush_budget(new_flush_budget); + } + for (int j = 0; j < guard_count; j++) { + GenerateGuard(macro_assembler, guards->at(j), &new_trace); + } + alternative.node()->Emit(compiler, &new_trace); + preload->preload_is_current_ = false; + } + macro_assembler->Bind(&alt_gen->after); + } +} + +void ChoiceNode::EmitOutOfLineContinuation(RegExpCompiler* compiler, + Trace* trace, + GuardedAlternative alternative, + AlternativeGeneration* alt_gen, + int preload_characters, + bool next_expects_preload) { + if (!alt_gen->possible_success.is_linked()) return; + + RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); + macro_assembler->Bind(&alt_gen->possible_success); + Trace out_of_line_trace(*trace); + out_of_line_trace.set_characters_preloaded(preload_characters); + out_of_line_trace.set_quick_check_performed(&alt_gen->quick_check_details); + if (not_at_start_) out_of_line_trace.set_at_start(Trace::FALSE_VALUE); + ZoneList* guards = alternative.guards(); + int guard_count = (guards == nullptr) ? 0 : guards->length(); + if (next_expects_preload) { + Label reload_current_char; + out_of_line_trace.set_backtrack(&reload_current_char); + for (int j = 0; j < guard_count; j++) { + GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace); + } + alternative.node()->Emit(compiler, &out_of_line_trace); + macro_assembler->Bind(&reload_current_char); + // Reload the current character, since the next quick check expects that. + // We don't need to check bounds here because we only get into this + // code through a quick check which already did the checked load. + macro_assembler->LoadCurrentCharacter(trace->cp_offset(), nullptr, false, + preload_characters); + macro_assembler->GoTo(&(alt_gen->after)); + } else { + out_of_line_trace.set_backtrack(&(alt_gen->after)); + for (int j = 0; j < guard_count; j++) { + GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace); + } + alternative.node()->Emit(compiler, &out_of_line_trace); + } +} + +void ActionNode::Emit(RegExpCompiler* compiler, Trace* trace) { + RegExpMacroAssembler* assembler = compiler->macro_assembler(); + LimitResult limit_result = LimitVersions(compiler, trace); + if (limit_result == DONE) return; + DCHECK(limit_result == CONTINUE); + + RecursionCheck rc(compiler); + + switch (action_type_) { + case STORE_POSITION: { + Trace::DeferredCapture new_capture(data_.u_position_register.reg, + data_.u_position_register.is_capture, + trace); + Trace new_trace = *trace; + new_trace.add_action(&new_capture); + on_success()->Emit(compiler, &new_trace); + break; + } + case INCREMENT_REGISTER: { + Trace::DeferredIncrementRegister new_increment( + data_.u_increment_register.reg); + Trace new_trace = *trace; + new_trace.add_action(&new_increment); + on_success()->Emit(compiler, &new_trace); + break; + } + case SET_REGISTER: { + Trace::DeferredSetRegister new_set(data_.u_store_register.reg, + data_.u_store_register.value); + Trace new_trace = *trace; + new_trace.add_action(&new_set); + on_success()->Emit(compiler, &new_trace); + break; + } + case CLEAR_CAPTURES: { + Trace::DeferredClearCaptures new_capture(Interval( + data_.u_clear_captures.range_from, data_.u_clear_captures.range_to)); + Trace new_trace = *trace; + new_trace.add_action(&new_capture); + on_success()->Emit(compiler, &new_trace); + break; + } + case BEGIN_SUBMATCH: + if (!trace->is_trivial()) { + trace->Flush(compiler, this); + } else { + assembler->WriteCurrentPositionToRegister( + data_.u_submatch.current_position_register, 0); + assembler->WriteStackPointerToRegister( + data_.u_submatch.stack_pointer_register); + on_success()->Emit(compiler, trace); + } + break; + case EMPTY_MATCH_CHECK: { + int start_pos_reg = data_.u_empty_match_check.start_register; + int stored_pos = 0; + int rep_reg = data_.u_empty_match_check.repetition_register; + bool has_minimum = (rep_reg != RegExpCompiler::kNoRegister); + bool know_dist = trace->GetStoredPosition(start_pos_reg, &stored_pos); + if (know_dist && !has_minimum && stored_pos == trace->cp_offset()) { + // If we know we haven't advanced and there is no minimum we + // can just backtrack immediately. + assembler->GoTo(trace->backtrack()); + } else if (know_dist && stored_pos < trace->cp_offset()) { + // If we know we've advanced we can generate the continuation + // immediately. + on_success()->Emit(compiler, trace); + } else if (!trace->is_trivial()) { + trace->Flush(compiler, this); + } else { + Label skip_empty_check; + // If we have a minimum number of repetitions we check the current + // number first and skip the empty check if it's not enough. + if (has_minimum) { + int limit = data_.u_empty_match_check.repetition_limit; + assembler->IfRegisterLT(rep_reg, limit, &skip_empty_check); + } + // If the match is empty we bail out, otherwise we fall through + // to the on-success continuation. + assembler->IfRegisterEqPos(data_.u_empty_match_check.start_register, + trace->backtrack()); + assembler->Bind(&skip_empty_check); + on_success()->Emit(compiler, trace); + } + break; + } + case POSITIVE_SUBMATCH_SUCCESS: { + if (!trace->is_trivial()) { + trace->Flush(compiler, this); + return; + } + assembler->ReadCurrentPositionFromRegister( + data_.u_submatch.current_position_register); + assembler->ReadStackPointerFromRegister( + data_.u_submatch.stack_pointer_register); + int clear_register_count = data_.u_submatch.clear_register_count; + if (clear_register_count == 0) { + on_success()->Emit(compiler, trace); + return; + } + int clear_registers_from = data_.u_submatch.clear_register_from; + Label clear_registers_backtrack; + Trace new_trace = *trace; + new_trace.set_backtrack(&clear_registers_backtrack); + on_success()->Emit(compiler, &new_trace); + + assembler->Bind(&clear_registers_backtrack); + int clear_registers_to = clear_registers_from + clear_register_count - 1; + assembler->ClearRegisters(clear_registers_from, clear_registers_to); + + DCHECK(trace->backtrack() == nullptr); + assembler->Backtrack(); + return; + } + default: + UNREACHABLE(); + } +} + +void BackReferenceNode::Emit(RegExpCompiler* compiler, Trace* trace) { + RegExpMacroAssembler* assembler = compiler->macro_assembler(); + if (!trace->is_trivial()) { + trace->Flush(compiler, this); + return; + } + + LimitResult limit_result = LimitVersions(compiler, trace); + if (limit_result == DONE) return; + DCHECK(limit_result == CONTINUE); + + RecursionCheck rc(compiler); + + DCHECK_EQ(start_reg_ + 1, end_reg_); + if (IgnoreCase(flags_)) { + assembler->CheckNotBackReferenceIgnoreCase( + start_reg_, read_backward(), IsUnicode(flags_), trace->backtrack()); + } else { + assembler->CheckNotBackReference(start_reg_, read_backward(), + trace->backtrack()); + } + // We are going to advance backward, so we may end up at the start. + if (read_backward()) trace->set_at_start(Trace::UNKNOWN); + + // Check that the back reference does not end inside a surrogate pair. + if (IsUnicode(flags_) && !compiler->one_byte()) { + assembler->CheckNotInSurrogatePair(trace->cp_offset(), trace->backtrack()); + } + on_success()->Emit(compiler, trace); +} + +// ------------------------------------------------------------------- +// Analysis + +void Analysis::EnsureAnalyzed(RegExpNode* that) { + StackLimitCheck check(isolate()); + if (check.HasOverflowed()) { + 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 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()->at(i); + elm.set_cp_offset(cp_offset); + cp_offset += elm.length(); + } +} + +void Analysis::VisitText(TextNode* that) { + that->MakeCaseIndependent(isolate(), is_one_byte_); + 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 (int i = 0; i < that->alternatives()->length(); i++) { + RegExpNode* node = that->alternatives()->at(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 (int i = 0; i < that->alternatives()->length(); i++) { + RegExpNode* node = that->alternatives()->at(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()); +} + +void BackReferenceNode::FillInBMInfo(Isolate* isolate, 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); +} + +STATIC_ASSERT(BoyerMoorePositionInfo::kMapSize == + RegExpMacroAssembler::kTableSize); + +void ChoiceNode::FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) { + ZoneList* alts = alternatives(); + budget = (budget - 1) / alts->length(); + for (int i = 0; i < alts->length(); i++) { + GuardedAlternative& alt = alts->at(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; + } + alt.node()->FillInBMInfo(isolate, offset, budget, bm, not_at_start); + } + SaveBMInfo(bm, not_at_start, offset); +} + +void TextNode::FillInBMInfo(Isolate* isolate, int initial_offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) { + if (initial_offset >= bm->length()) return; + int offset = initial_offset; + int max_char = bm->max_char(); + for (int i = 0; i < elements()->length(); i++) { + if (offset >= bm->length()) { + if (initial_offset == 0) set_bm_info(not_at_start, bm); + return; + } + TextElement text = elements()->at(i); + if (text.text_type() == TextElement::ATOM) { + RegExpAtom* atom = text.atom(); + for (int j = 0; j < atom->length(); j++, offset++) { + if (offset >= bm->length()) { + if (initial_offset == 0) set_bm_info(not_at_start, bm); + return; + } + uc16 character = atom->data()[j]; + if (IgnoreCase(atom->flags())) { + unibrow::uchar chars[4]; + int length = GetCaseIndependentLetters( + isolate, character, bm->max_char() == String::kMaxOneByteCharCode, + chars, 4); + for (int j = 0; j < length; j++) { + bm->Set(offset, chars[j]); + } + } else { + if (character <= max_char) bm->Set(offset, character); + } + } + } else { + DCHECK_EQ(TextElement::CHAR_CLASS, text.text_type()); + RegExpCharacterClass* char_class = text.char_class(); + ZoneList* ranges = char_class->ranges(zone()); + if (char_class->is_negated()) { + bm->SetAll(offset); + } else { + for (int k = 0; k < ranges->length(); k++) { + CharacterRange& range = ranges->at(k); + if (range.from() > max_char) continue; + int to = Min(max_char, static_cast(range.to())); + bm->SetInterval(offset, Interval(range.from(), to)); + } + } + offset++; + } + } + if (offset >= bm->length()) { + if (initial_offset == 0) set_bm_info(not_at_start, bm); + return; + } + on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, + true); // Not at start after a text node. + if (initial_offset == 0) set_bm_info(not_at_start, bm); +} + +// static +RegExpNode* RegExpCompiler::OptionallyStepBackToLeadSurrogate( + RegExpCompiler* compiler, RegExpNode* on_success, JSRegExp::Flags flags) { + DCHECK(!compiler->read_backward()); + Zone* zone = compiler->zone(); + ZoneList* lead_surrogates = CharacterRange::List( + zone, CharacterRange::Range(kLeadSurrogateStart, kLeadSurrogateEnd)); + ZoneList* trail_surrogates = CharacterRange::List( + zone, CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd)); + + ChoiceNode* optional_step_back = new (zone) ChoiceNode(2, zone); + + int stack_register = compiler->UnicodeLookaroundStackRegister(); + int position_register = compiler->UnicodeLookaroundPositionRegister(); + RegExpNode* step_back = TextNode::CreateForCharacterRanges( + zone, lead_surrogates, true, on_success, flags); + RegExpLookaround::Builder builder(true, step_back, stack_register, + position_register); + RegExpNode* match_trail = TextNode::CreateForCharacterRanges( + zone, trail_surrogates, false, builder.on_match_success(), flags); + + optional_step_back->AddAlternative( + GuardedAlternative(builder.ForMatch(match_trail))); + optional_step_back->AddAlternative(GuardedAlternative(on_success)); + + return optional_step_back; +} + +} // namespace internal +} // namespace v8 diff --git a/chromium/v8/src/regexp/regexp-compiler.h b/chromium/v8/src/regexp/regexp-compiler.h new file mode 100644 index 00000000000..1b70abfd981 --- /dev/null +++ b/chromium/v8/src/regexp/regexp-compiler.h @@ -0,0 +1,657 @@ +// Copyright 2019 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_REGEXP_REGEXP_COMPILER_H_ +#define V8_REGEXP_REGEXP_COMPILER_H_ + +#include + +#include "src/base/small-vector.h" +#include "src/regexp/regexp-nodes.h" + +namespace v8 { +namespace internal { + +class DynamicBitSet; +class Isolate; + +namespace regexp_compiler_constants { + +// The '2' variant is 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. +constexpr uc32 kRangeEndMarker = 0x110000; +constexpr int kSpaceRanges[] = { + '\t', '\r' + 1, ' ', ' ' + 1, 0x00A0, 0x00A1, 0x1680, + 0x1681, 0x2000, 0x200B, 0x2028, 0x202A, 0x202F, 0x2030, + 0x205F, 0x2060, 0x3000, 0x3001, 0xFEFF, 0xFF00, kRangeEndMarker}; +constexpr int kSpaceRangeCount = arraysize(kSpaceRanges); + +constexpr int kWordRanges[] = {'0', '9' + 1, 'A', 'Z' + 1, '_', + '_' + 1, 'a', 'z' + 1, kRangeEndMarker}; +constexpr int kWordRangeCount = arraysize(kWordRanges); +constexpr int kDigitRanges[] = {'0', '9' + 1, kRangeEndMarker}; +constexpr int kDigitRangeCount = arraysize(kDigitRanges); +constexpr int kSurrogateRanges[] = {kLeadSurrogateStart, + kLeadSurrogateStart + 1, kRangeEndMarker}; +constexpr int kSurrogateRangeCount = arraysize(kSurrogateRanges); +constexpr int kLineTerminatorRanges[] = {0x000A, 0x000B, 0x000D, 0x000E, + 0x2028, 0x202A, kRangeEndMarker}; +constexpr int kLineTerminatorRangeCount = arraysize(kLineTerminatorRanges); + +// More makes code generation slower, less makes V8 benchmark score lower. +constexpr int kMaxLookaheadForBoyerMoore = 8; +// In a 3-character pattern you can maximally step forwards 3 characters +// at a time, which is not always enough to pay for the extra logic. +constexpr int kPatternTooShortForBoyerMoore = 2; + +} // namespace regexp_compiler_constants + +inline bool IgnoreCase(JSRegExp::Flags flags) { + return (flags & JSRegExp::kIgnoreCase) != 0; +} + +inline bool IsUnicode(JSRegExp::Flags flags) { + return (flags & JSRegExp::kUnicode) != 0; +} + +inline bool IsSticky(JSRegExp::Flags flags) { + return (flags & JSRegExp::kSticky) != 0; +} + +inline bool IsGlobal(JSRegExp::Flags flags) { + return (flags & JSRegExp::kGlobal) != 0; +} + +inline bool DotAll(JSRegExp::Flags flags) { + return (flags & JSRegExp::kDotAll) != 0; +} + +inline bool Multiline(JSRegExp::Flags flags) { + return (flags & JSRegExp::kMultiline) != 0; +} + +inline bool NeedsUnicodeCaseEquivalents(JSRegExp::Flags flags) { + // Both unicode and ignore_case flags are set. We need to use ICU to find + // the closure over case equivalents. + return IsUnicode(flags) && IgnoreCase(flags); +} + +// Details of a quick mask-compare check that can look ahead in the +// input stream. +class QuickCheckDetails { + public: + QuickCheckDetails() + : characters_(0), mask_(0), value_(0), cannot_match_(false) {} + explicit QuickCheckDetails(int characters) + : characters_(characters), mask_(0), value_(0), cannot_match_(false) {} + bool Rationalize(bool one_byte); + // Merge in the information from another branch of an alternation. + void Merge(QuickCheckDetails* other, int from_index); + // Advance the current position by some amount. + void Advance(int by, bool one_byte); + void Clear(); + bool cannot_match() { return cannot_match_; } + void set_cannot_match() { cannot_match_ = true; } + struct Position { + Position() : mask(0), value(0), determines_perfectly(false) {} + uc16 mask; + uc16 value; + bool determines_perfectly; + }; + int characters() { return characters_; } + void set_characters(int characters) { characters_ = characters; } + Position* positions(int index) { + DCHECK_LE(0, index); + DCHECK_GT(characters_, index); + return positions_ + index; + } + uint32_t mask() { return mask_; } + uint32_t value() { return value_; } + + private: + // How many characters do we have quick check information from. This is + // the same for all branches of a choice node. + int characters_; + Position positions_[4]; + // These values are the condensate of the above array after Rationalize(). + uint32_t mask_; + uint32_t value_; + // If set to true, there is no way this quick check can match at all. + // E.g., if it requires to be at the start of the input, and isn't. + bool cannot_match_; +}; + +// Improve the speed that we scan for an initial point where a non-anchored +// regexp can match by using a Boyer-Moore-like table. This is done by +// identifying non-greedy non-capturing loops in the nodes that eat any +// character one at a time. For example in the middle of the regexp +// /foo[\s\S]*?bar/ we find such a loop. There is also such a loop implicitly +// inserted at the start of any non-anchored regexp. +// +// When we have found such a loop we look ahead in the nodes to find the set of +// characters that can come at given distances. For example for the regexp +// /.?foo/ we know that there are at least 3 characters ahead of us, and the +// sets of characters that can occur are [any, [f, o], [o]]. We find a range in +// the lookahead info where the set of characters is reasonably constrained. In +// our example this is from index 1 to 2 (0 is not constrained). We can now +// look 3 characters ahead and if we don't find one of [f, o] (the union of +// [f, o] and [o]) then we can skip forwards by the range size (in this case 2). +// +// For Unicode input strings we do the same, but modulo 128. +// +// We also look at the first string fed to the regexp and use that to get a hint +// of the character frequencies in the inputs. This affects the assessment of +// whether the set of characters is 'reasonably constrained'. +// +// We also have another lookahead mechanism (called quick check in the code), +// which uses a wide load of multiple characters followed by a mask and compare +// to determine whether a match is possible at this point. +enum ContainedInLattice { + kNotYet = 0, + kLatticeIn = 1, + kLatticeOut = 2, + kLatticeUnknown = 3 // Can also mean both in and out. +}; + +inline ContainedInLattice Combine(ContainedInLattice a, ContainedInLattice b) { + return static_cast(a | b); +} + +class BoyerMoorePositionInfo : public ZoneObject { + public: + bool at(int i) const { return map_[i]; } + + static constexpr int kMapSize = 128; + static constexpr int kMask = kMapSize - 1; + + int map_count() const { return map_count_; } + + void Set(int character); + void SetInterval(const Interval& interval); + void SetAll(); + + bool is_non_word() { return w_ == kLatticeOut; } + bool is_word() { return w_ == kLatticeIn; } + + using Bitset = std::bitset; + Bitset raw_bitset() const { return map_; } + + private: + Bitset map_; + int map_count_ = 0; // Number of set bits in the map. + ContainedInLattice w_ = kNotYet; // The \w character class. +}; + +class BoyerMooreLookahead : public ZoneObject { + public: + BoyerMooreLookahead(int length, RegExpCompiler* compiler, Zone* zone); + + int length() { return length_; } + int max_char() { return max_char_; } + RegExpCompiler* compiler() { return compiler_; } + + int Count(int map_number) { return bitmaps_->at(map_number)->map_count(); } + + BoyerMoorePositionInfo* at(int i) { return bitmaps_->at(i); } + + void Set(int map_number, int character) { + if (character > max_char_) return; + BoyerMoorePositionInfo* info = bitmaps_->at(map_number); + info->Set(character); + } + + void SetInterval(int map_number, const Interval& interval) { + if (interval.from() > max_char_) return; + BoyerMoorePositionInfo* info = bitmaps_->at(map_number); + if (interval.to() > max_char_) { + info->SetInterval(Interval(interval.from(), max_char_)); + } else { + info->SetInterval(interval); + } + } + + void SetAll(int map_number) { bitmaps_->at(map_number)->SetAll(); } + + void SetRest(int from_map) { + for (int i = from_map; i < length_; i++) SetAll(i); + } + void EmitSkipInstructions(RegExpMacroAssembler* masm); + + private: + // This is the value obtained by EatsAtLeast. If we do not have at least this + // many characters left in the sample string then the match is bound to fail. + // Therefore it is OK to read a character this far ahead of the current match + // point. + int length_; + RegExpCompiler* compiler_; + // 0xff for Latin1, 0xffff for UTF-16. + int max_char_; + ZoneList* bitmaps_; + + int GetSkipTable(int min_lookahead, int max_lookahead, + Handle boolean_skip_table); + bool FindWorthwhileInterval(int* from, int* to); + int FindBestInterval(int max_number_of_chars, int old_biggest_points, + int* from, int* to); +}; + +// There are many ways to generate code for a node. This class encapsulates +// the current way we should be generating. In other words it encapsulates +// the current state of the code generator. The effect of this is that we +// generate code for paths that the matcher can take through the regular +// expression. A given node in the regexp can be code-generated several times +// as it can be part of several traces. For example for the regexp: +// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part +// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code +// to match foo is generated only once (the traces have a common prefix). The +// code to store the capture is deferred and generated (twice) after the places +// where baz has been matched. +class Trace { + public: + // A value for a property that is either known to be true, know to be false, + // or not known. + enum TriBool { UNKNOWN = -1, FALSE_VALUE = 0, TRUE_VALUE = 1 }; + + class DeferredAction { + public: + DeferredAction(ActionNode::ActionType action_type, int reg) + : action_type_(action_type), reg_(reg), next_(nullptr) {} + DeferredAction* next() { return next_; } + bool Mentions(int reg); + int reg() { return reg_; } + ActionNode::ActionType action_type() { return action_type_; } + + private: + ActionNode::ActionType action_type_; + int reg_; + DeferredAction* next_; + friend class Trace; + }; + + class DeferredCapture : public DeferredAction { + public: + DeferredCapture(int reg, bool is_capture, Trace* trace) + : DeferredAction(ActionNode::STORE_POSITION, reg), + cp_offset_(trace->cp_offset()), + is_capture_(is_capture) {} + int cp_offset() { return cp_offset_; } + bool is_capture() { return is_capture_; } + + private: + int cp_offset_; + bool is_capture_; + void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; } + }; + + class DeferredSetRegister : public DeferredAction { + public: + DeferredSetRegister(int reg, int value) + : DeferredAction(ActionNode::SET_REGISTER, reg), value_(value) {} + int value() { return value_; } + + private: + int value_; + }; + + class DeferredClearCaptures : public DeferredAction { + public: + explicit DeferredClearCaptures(Interval range) + : DeferredAction(ActionNode::CLEAR_CAPTURES, -1), range_(range) {} + Interval range() { return range_; } + + private: + Interval range_; + }; + + class DeferredIncrementRegister : public DeferredAction { + public: + explicit DeferredIncrementRegister(int reg) + : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) {} + }; + + Trace() + : cp_offset_(0), + actions_(nullptr), + backtrack_(nullptr), + stop_node_(nullptr), + loop_label_(nullptr), + characters_preloaded_(0), + bound_checked_up_to_(0), + flush_budget_(100), + at_start_(UNKNOWN) {} + + // End the trace. This involves flushing the deferred actions in the trace + // and pushing a backtrack location onto the backtrack stack. Once this is + // done we can start a new trace or go to one that has already been + // generated. + void Flush(RegExpCompiler* compiler, RegExpNode* successor); + int cp_offset() { return cp_offset_; } + DeferredAction* actions() { return actions_; } + // A trivial trace is one that has no deferred actions or other state that + // affects the assumptions used when generating code. There is no recorded + // backtrack location in a trivial trace, so with a trivial trace we will + // generate code that, on a failure to match, gets the backtrack location + // from the backtrack stack rather than using a direct jump instruction. We + // always start code generation with a trivial trace and non-trivial traces + // are created as we emit code for nodes or add to the list of deferred + // actions in the trace. The location of the code generated for a node using + // a trivial trace is recorded in a label in the node so that gotos can be + // generated to that code. + bool is_trivial() { + return backtrack_ == nullptr && actions_ == nullptr && cp_offset_ == 0 && + characters_preloaded_ == 0 && bound_checked_up_to_ == 0 && + quick_check_performed_.characters() == 0 && at_start_ == UNKNOWN; + } + TriBool at_start() { return at_start_; } + void set_at_start(TriBool at_start) { at_start_ = at_start; } + Label* backtrack() { return backtrack_; } + Label* loop_label() { return loop_label_; } + RegExpNode* stop_node() { return stop_node_; } + int characters_preloaded() { return characters_preloaded_; } + int bound_checked_up_to() { return bound_checked_up_to_; } + int flush_budget() { return flush_budget_; } + QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; } + bool mentions_reg(int reg); + // Returns true if a deferred position store exists to the specified + // register and stores the offset in the out-parameter. Otherwise + // returns false. + bool GetStoredPosition(int reg, int* cp_offset); + // These set methods and AdvanceCurrentPositionInTrace should be used only on + // new traces - the intention is that traces are immutable after creation. + void add_action(DeferredAction* new_action) { + DCHECK(new_action->next_ == nullptr); + new_action->next_ = actions_; + actions_ = new_action; + } + void set_backtrack(Label* backtrack) { backtrack_ = backtrack; } + void set_stop_node(RegExpNode* node) { stop_node_ = node; } + void set_loop_label(Label* label) { loop_label_ = label; } + void set_characters_preloaded(int count) { characters_preloaded_ = count; } + void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; } + void set_flush_budget(int to) { flush_budget_ = to; } + void set_quick_check_performed(QuickCheckDetails* d) { + quick_check_performed_ = *d; + } + void InvalidateCurrentCharacter(); + void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler); + + private: + int FindAffectedRegisters(DynamicBitSet* affected_registers, Zone* zone); + void PerformDeferredActions(RegExpMacroAssembler* macro, int max_register, + const DynamicBitSet& affected_registers, + DynamicBitSet* registers_to_pop, + DynamicBitSet* registers_to_clear, Zone* zone); + void RestoreAffectedRegisters(RegExpMacroAssembler* macro, int max_register, + const DynamicBitSet& registers_to_pop, + const DynamicBitSet& registers_to_clear); + int cp_offset_; + DeferredAction* actions_; + Label* backtrack_; + RegExpNode* stop_node_; + Label* loop_label_; + int characters_preloaded_; + int bound_checked_up_to_; + QuickCheckDetails quick_check_performed_; + int flush_budget_; + TriBool at_start_; +}; + +class GreedyLoopState { + public: + explicit GreedyLoopState(bool not_at_start); + + Label* label() { return &label_; } + Trace* counter_backtrack_trace() { return &counter_backtrack_trace_; } + + private: + Label label_; + Trace counter_backtrack_trace_; +}; + +struct PreloadState { + static const int kEatsAtLeastNotYetInitialized = -1; + bool preload_is_current_; + bool preload_has_checked_bounds_; + int preload_characters_; + int eats_at_least_; + void init() { eats_at_least_ = kEatsAtLeastNotYetInitialized; } +}; + +// Assertion propagation moves information about assertions such as +// \b to the affected nodes. For instance, in /.\b./ information must +// be propagated to the first '.' that whatever follows needs to know +// if it matched a word or a non-word, and to the second '.' that it +// has to check if it succeeds a word or non-word. In this case the +// result will be something like: +// +// +-------+ +------------+ +// | . | | . | +// +-------+ ---> +------------+ +// | word? | | check word | +// +-------+ +------------+ +class Analysis : public NodeVisitor { + public: + Analysis(Isolate* isolate, bool is_one_byte) + : isolate_(isolate), is_one_byte_(is_one_byte), error_message_(nullptr) {} + void EnsureAnalyzed(RegExpNode* node); + +#define DECLARE_VISIT(Type) void Visit##Type(Type##Node* that) override; + FOR_EACH_NODE_TYPE(DECLARE_VISIT) +#undef DECLARE_VISIT + void VisitLoopChoice(LoopChoiceNode* that) override; + + bool has_failed() { return error_message_ != nullptr; } + const char* error_message() { + DCHECK(error_message_ != nullptr); + return error_message_; + } + void fail(const char* error_message) { error_message_ = error_message; } + + Isolate* isolate() const { return isolate_; } + + private: + Isolate* isolate_; + bool is_one_byte_; + const char* error_message_; + + DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis); +}; + +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) { + DCHECK((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 RegExpCompiler { + public: + RegExpCompiler(Isolate* isolate, Zone* zone, int capture_count, + bool is_one_byte); + + int AllocateRegister() { + if (next_register_ >= RegExpMacroAssembler::kMaxRegister) { + reg_exp_too_big_ = true; + return next_register_; + } + return next_register_++; + } + + // Lookarounds to match lone surrogates for unicode character class matches + // are never nested. We can therefore reuse registers. + int UnicodeLookaroundStackRegister() { + if (unicode_lookaround_stack_register_ == kNoRegister) { + unicode_lookaround_stack_register_ = AllocateRegister(); + } + return unicode_lookaround_stack_register_; + } + + int UnicodeLookaroundPositionRegister() { + if (unicode_lookaround_position_register_ == kNoRegister) { + unicode_lookaround_position_register_ = AllocateRegister(); + } + return unicode_lookaround_position_register_; + } + + struct CompilationResult final { + explicit CompilationResult(const char* error_message) + : error_message(error_message) {} + CompilationResult(Object code, int registers) + : code(code), num_registers(registers) {} + + static CompilationResult RegExpTooBig() { + return CompilationResult("RegExp too big"); + } + + bool Succeeded() const { return error_message == nullptr; } + + const char* const error_message = nullptr; + Object code; + int num_registers = 0; + }; + + CompilationResult Assemble(Isolate* isolate, RegExpMacroAssembler* assembler, + RegExpNode* start, int capture_count, + Handle pattern); + + // If the regexp matching starts within a surrogate pair, step back to the + // lead surrogate and start matching from there. + static RegExpNode* OptionallyStepBackToLeadSurrogate(RegExpCompiler* compiler, + RegExpNode* on_success, + JSRegExp::Flags flags); + + inline void AddWork(RegExpNode* node) { + if (!node->on_work_list() && !node->label()->is_bound()) { + node->set_on_work_list(true); + work_list_->push_back(node); + } + } + + 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 one_byte() { return one_byte_; } + inline bool optimize() { return optimize_; } + inline void set_optimize(bool value) { optimize_ = value; } + inline bool limiting_recursion() { return limiting_recursion_; } + inline void set_limiting_recursion(bool value) { + limiting_recursion_ = value; + } + bool read_backward() { return read_backward_; } + void set_read_backward(bool value) { read_backward_ = value; } + 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; + } + + Isolate* isolate() const { return isolate_; } + Zone* zone() const { return zone_; } + + static const int kNoRegister = -1; + + private: + EndNode* accept_; + int next_register_; + int unicode_lookaround_stack_register_; + int unicode_lookaround_position_register_; + std::vector* work_list_; + int recursion_depth_; + RegExpMacroAssembler* macro_assembler_; + bool one_byte_; + bool reg_exp_too_big_; + bool limiting_recursion_; + bool optimize_; + bool read_backward_; + int current_expansion_factor_; + FrequencyCollator frequency_collator_; + Isolate* isolate_; + Zone* zone_; +}; + +// Categorizes character ranges into BMP, non-BMP, lead, and trail surrogates. +class UnicodeRangeSplitter { + public: + V8_EXPORT_PRIVATE UnicodeRangeSplitter(ZoneList* base); + + static constexpr int kInitialSize = 8; + using CharacterRangeVector = base::SmallVector; + + const CharacterRangeVector* bmp() const { return &bmp_; } + const CharacterRangeVector* lead_surrogates() const { + return &lead_surrogates_; + } + const CharacterRangeVector* trail_surrogates() const { + return &trail_surrogates_; + } + const CharacterRangeVector* non_bmp() const { return &non_bmp_; } + + private: + void AddRange(CharacterRange range); + + CharacterRangeVector bmp_; + CharacterRangeVector lead_surrogates_; + CharacterRangeVector trail_surrogates_; + CharacterRangeVector non_bmp_; +}; + +// We need to check for the following characters: 0x39C 0x3BC 0x178. +// TODO(jgruber): Move to CharacterRange. +bool RangeContainsLatin1Equivalents(CharacterRange range); + +} // namespace internal +} // namespace v8 + +#endif // V8_REGEXP_REGEXP_COMPILER_H_ diff --git a/chromium/v8/src/regexp/regexp-dotprinter.cc b/chromium/v8/src/regexp/regexp-dotprinter.cc new file mode 100644 index 00000000000..a6d72aaf5bb --- /dev/null +++ b/chromium/v8/src/regexp/regexp-dotprinter.cc @@ -0,0 +1,244 @@ +// Copyright 2019 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "src/regexp/regexp-dotprinter.h" + +#include "src/regexp/regexp-compiler.h" +#include "src/utils/ostreams.h" + +namespace v8 { +namespace internal { + +// ------------------------------------------------------------------- +// Dot/dotty output + +#ifdef DEBUG + +class DotPrinterImpl : public NodeVisitor { + public: + explicit DotPrinterImpl(std::ostream& os) : os_(os) {} + void PrintNode(const char* label, RegExpNode* node); + void Visit(RegExpNode* node); + void PrintAttributes(RegExpNode* from); + void PrintOnFailure(RegExpNode* from, RegExpNode* to); +#define DECLARE_VISIT(Type) virtual void Visit##Type(Type##Node* that); + FOR_EACH_NODE_TYPE(DECLARE_VISIT) +#undef DECLARE_VISIT + private: + std::ostream& os_; +}; + +void DotPrinterImpl::PrintNode(const char* label, RegExpNode* node) { + os_ << "digraph G {\n graph [label=\""; + for (int i = 0; label[i]; i++) { + switch (label[i]) { + case '\\': + os_ << "\\\\"; + break; + case '"': + os_ << "\""; + break; + default: + os_ << label[i]; + break; + } + } + os_ << "\"];\n"; + Visit(node); + os_ << "}" << std::endl; +} + +void DotPrinterImpl::Visit(RegExpNode* node) { + if (node->info()->visited) return; + node->info()->visited = true; + node->Accept(this); +} + +void DotPrinterImpl::PrintOnFailure(RegExpNode* from, RegExpNode* on_failure) { + os_ << " n" << from << " -> n" << on_failure << " [style=dotted];\n"; + Visit(on_failure); +} + +class AttributePrinter { + public: + explicit AttributePrinter(std::ostream& os) // NOLINT + : os_(os), first_(true) {} + void PrintSeparator() { + if (first_) { + first_ = false; + } else { + os_ << "|"; + } + } + void PrintBit(const char* name, bool value) { + if (!value) return; + PrintSeparator(); + os_ << "{" << name << "}"; + } + void PrintPositive(const char* name, int value) { + if (value < 0) return; + PrintSeparator(); + os_ << "{" << name << "|" << value << "}"; + } + + private: + std::ostream& os_; + bool first_; +}; + +void DotPrinterImpl::PrintAttributes(RegExpNode* that) { + os_ << " a" << that << " [shape=Mrecord, color=grey, fontcolor=grey, " + << "margin=0.1, fontsize=10, label=\"{"; + AttributePrinter printer(os_); + NodeInfo* info = that->info(); + printer.PrintBit("NI", info->follows_newline_interest); + printer.PrintBit("WI", info->follows_word_interest); + printer.PrintBit("SI", info->follows_start_interest); + Label* label = that->label(); + if (label->is_bound()) printer.PrintPositive("@", label->pos()); + os_ << "}\"];\n" + << " a" << that << " -> n" << that + << " [style=dashed, color=grey, arrowhead=none];\n"; +} + +void DotPrinterImpl::VisitChoice(ChoiceNode* that) { + os_ << " n" << that << " [shape=Mrecord, label=\"?\"];\n"; + for (int i = 0; i < that->alternatives()->length(); i++) { + GuardedAlternative alt = that->alternatives()->at(i); + os_ << " n" << that << " -> n" << alt.node(); + } + for (int i = 0; i < that->alternatives()->length(); i++) { + GuardedAlternative alt = that->alternatives()->at(i); + alt.node()->Accept(this); + } +} + +void DotPrinterImpl::VisitText(TextNode* that) { + Zone* zone = that->zone(); + os_ << " n" << that << " [label=\""; + for (int i = 0; i < that->elements()->length(); i++) { + if (i > 0) os_ << " "; + TextElement elm = that->elements()->at(i); + switch (elm.text_type()) { + case TextElement::ATOM: { + Vector data = elm.atom()->data(); + for (int i = 0; i < data.length(); i++) { + os_ << static_cast(data[i]); + } + break; + } + case TextElement::CHAR_CLASS: { + RegExpCharacterClass* node = elm.char_class(); + os_ << "["; + if (node->is_negated()) os_ << "^"; + for (int j = 0; j < node->ranges(zone)->length(); j++) { + CharacterRange range = node->ranges(zone)->at(j); + os_ << AsUC16(range.from()) << "-" << AsUC16(range.to()); + } + os_ << "]"; + break; + } + default: + UNREACHABLE(); + } + } + os_ << "\", shape=box, peripheries=2];\n"; + PrintAttributes(that); + os_ << " n" << that << " -> n" << that->on_success() << ";\n"; + Visit(that->on_success()); +} + +void DotPrinterImpl::VisitBackReference(BackReferenceNode* that) { + os_ << " n" << that << " [label=\"$" << that->start_register() << "..$" + << that->end_register() << "\", shape=doubleoctagon];\n"; + PrintAttributes(that); + os_ << " n" << that << " -> n" << that->on_success() << ";\n"; + Visit(that->on_success()); +} + +void DotPrinterImpl::VisitEnd(EndNode* that) { + os_ << " n" << that << " [style=bold, shape=point];\n"; + PrintAttributes(that); +} + +void DotPrinterImpl::VisitAssertion(AssertionNode* that) { + os_ << " n" << that << " ["; + switch (that->assertion_type()) { + case AssertionNode::AT_END: + os_ << "label=\"$\", shape=septagon"; + break; + case AssertionNode::AT_START: + os_ << "label=\"^\", shape=septagon"; + break; + case AssertionNode::AT_BOUNDARY: + os_ << "label=\"\\b\", shape=septagon"; + break; + case AssertionNode::AT_NON_BOUNDARY: + os_ << "label=\"\\B\", shape=septagon"; + break; + case AssertionNode::AFTER_NEWLINE: + os_ << "label=\"(?<=\\n)\", shape=septagon"; + break; + } + os_ << "];\n"; + PrintAttributes(that); + RegExpNode* successor = that->on_success(); + os_ << " n" << that << " -> n" << successor << ";\n"; + Visit(successor); +} + +void DotPrinterImpl::VisitAction(ActionNode* that) { + os_ << " n" << that << " ["; + switch (that->action_type_) { + case ActionNode::SET_REGISTER: + os_ << "label=\"$" << that->data_.u_store_register.reg + << ":=" << that->data_.u_store_register.value << "\", shape=octagon"; + break; + case ActionNode::INCREMENT_REGISTER: + os_ << "label=\"$" << that->data_.u_increment_register.reg + << "++\", shape=octagon"; + break; + case ActionNode::STORE_POSITION: + os_ << "label=\"$" << that->data_.u_position_register.reg + << ":=$pos\", shape=octagon"; + break; + case ActionNode::BEGIN_SUBMATCH: + os_ << "label=\"$" << that->data_.u_submatch.current_position_register + << ":=$pos,begin\", shape=septagon"; + break; + case ActionNode::POSITIVE_SUBMATCH_SUCCESS: + os_ << "label=\"escape\", shape=septagon"; + break; + case ActionNode::EMPTY_MATCH_CHECK: + os_ << "label=\"$" << that->data_.u_empty_match_check.start_register + << "=$pos?,$" << that->data_.u_empty_match_check.repetition_register + << "<" << that->data_.u_empty_match_check.repetition_limit + << "?\", shape=septagon"; + break; + case ActionNode::CLEAR_CAPTURES: { + os_ << "label=\"clear $" << that->data_.u_clear_captures.range_from + << " to $" << that->data_.u_clear_captures.range_to + << "\", shape=septagon"; + break; + } + } + os_ << "];\n"; + PrintAttributes(that); + RegExpNode* successor = that->on_success(); + os_ << " n" << that << " -> n" << successor << ";\n"; + Visit(successor); +} + +#endif // DEBUG + +void DotPrinter::DotPrint(const char* label, RegExpNode* node) { +#ifdef DEBUG + StdoutStream os; + DotPrinterImpl printer(os); + printer.PrintNode(label, node); +#endif // DEBUG +} + +} // namespace internal +} // namespace v8 diff --git a/chromium/v8/src/regexp/regexp-dotprinter.h b/chromium/v8/src/regexp/regexp-dotprinter.h new file mode 100644 index 00000000000..d9c75fc1f2c --- /dev/null +++ b/chromium/v8/src/regexp/regexp-dotprinter.h @@ -0,0 +1,23 @@ +// Copyright 2019 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_REGEXP_REGEXP_DOTPRINTER_H_ +#define V8_REGEXP_REGEXP_DOTPRINTER_H_ + +#include "src/common/globals.h" + +namespace v8 { +namespace internal { + +class RegExpNode; + +class DotPrinter final : public AllStatic { + public: + static void DotPrint(const char* label, RegExpNode* node); +}; + +} // namespace internal +} // namespace v8 + +#endif // V8_REGEXP_REGEXP_DOTPRINTER_H_ diff --git a/chromium/v8/src/regexp/regexp-interpreter.cc b/chromium/v8/src/regexp/regexp-interpreter.cc new file mode 100644 index 00000000000..881758861cd --- /dev/null +++ b/chromium/v8/src/regexp/regexp-interpreter.cc @@ -0,0 +1,680 @@ +// Copyright 2011 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// A simple interpreter for the Irregexp byte code. + +#include "src/regexp/regexp-interpreter.h" + +#include "src/ast/ast.h" +#include "src/base/small-vector.h" +#include "src/objects/objects-inl.h" +#include "src/regexp/regexp-bytecodes.h" +#include "src/regexp/regexp-macro-assembler.h" +#include "src/regexp/regexp.h" +#include "src/strings/unicode.h" +#include "src/utils/utils.h" + +#ifdef V8_INTL_SUPPORT +#include "unicode/uchar.h" +#endif // V8_INTL_SUPPORT + +namespace v8 { +namespace internal { + +static bool BackRefMatchesNoCase(Isolate* isolate, int from, int current, + int len, Vector subject, + bool unicode) { + Address offset_a = + reinterpret_cast
(const_cast(&subject.at(from))); + Address offset_b = + reinterpret_cast
(const_cast(&subject.at(current))); + size_t length = len * kUC16Size; + return RegExpMacroAssembler::CaseInsensitiveCompareUC16( + offset_a, offset_b, length, unicode ? nullptr : isolate) == 1; +} + +static bool BackRefMatchesNoCase(Isolate* isolate, int from, int current, + int len, Vector subject, + bool unicode) { + // For Latin1 characters the unicode flag makes no difference. + for (int i = 0; i < len; i++) { + unsigned int old_char = subject[from++]; + unsigned int new_char = subject[current++]; + if (old_char == new_char) continue; + // Convert both characters to lower case. + old_char |= 0x20; + new_char |= 0x20; + if (old_char != new_char) return false; + // Not letters in the ASCII range and Latin-1 range. + if (!(old_char - 'a' <= 'z' - 'a') && + !(old_char - 224 <= 254 - 224 && old_char != 247)) { + return false; + } + } + return true; +} + +#ifdef DEBUG +static void TraceInterpreter(const byte* code_base, const byte* pc, + int stack_depth, int current_position, + uint32_t current_char, int bytecode_length, + const char* bytecode_name) { + if (FLAG_trace_regexp_bytecodes) { + bool printable = (current_char < 127 && current_char >= 32); + const char* format = + printable + ? "pc = %02x, sp = %d, curpos = %d, curchar = %08x (%c), bc = %s" + : "pc = %02x, sp = %d, curpos = %d, curchar = %08x .%c., bc = %s"; + PrintF(format, pc - code_base, stack_depth, current_position, current_char, + printable ? current_char : '.', bytecode_name); + for (int i = 0; i < bytecode_length; i++) { + printf(", %02x", pc[i]); + } + printf(" "); + for (int i = 1; i < bytecode_length; i++) { + unsigned char b = pc[i]; + if (b < 127 && b >= 32) { + printf("%c", b); + } else { + printf("."); + } + } + printf("\n"); + } +} + +#define BYTECODE(name) \ + case BC_##name: \ + TraceInterpreter(code_base, pc, backtrack_stack.sp(), current, \ + current_char, BC_##name##_LENGTH, #name); +#else +#define BYTECODE(name) case BC_##name: +#endif + +static int32_t Load32Aligned(const byte* pc) { + DCHECK_EQ(0, reinterpret_cast(pc) & 3); + return *reinterpret_cast(pc); +} + +static int32_t Load16Aligned(const byte* pc) { + DCHECK_EQ(0, reinterpret_cast(pc) & 1); + return *reinterpret_cast(pc); +} + +// A simple abstraction over the backtracking stack used by the interpreter. +// +// Despite the name 'backtracking' stack, it's actually used as a generic stack +// that stores both program counters (= offsets into the bytecode) and generic +// integer values. +class BacktrackStack { + public: + BacktrackStack() = default; + + void push(int v) { data_.emplace_back(v); } + int peek() const { + DCHECK(!data_.empty()); + return data_.back(); + } + int pop() { + int v = peek(); + data_.pop_back(); + return v; + } + + // The 'sp' is the index of the first empty element in the stack. + int sp() const { return static_cast(data_.size()); } + void set_sp(int new_sp) { + DCHECK_LE(new_sp, sp()); + data_.resize_no_init(new_sp); + } + + private: + // Semi-arbitrary. Should be large enough for common cases to remain in the + // static stack-allocated backing store, but small enough not to waste space. + static constexpr int kStaticCapacity = 64; + + base::SmallVector data_; + + DISALLOW_COPY_AND_ASSIGN(BacktrackStack); +}; + +namespace { + +IrregexpInterpreter::Result StackOverflow(Isolate* isolate) { + // We abort interpreter execution after the stack overflow is thrown, and thus + // allow allocation here despite the outer DisallowHeapAllocationScope. + AllowHeapAllocation yes_gc; + isolate->StackOverflow(); + return IrregexpInterpreter::EXCEPTION; +} + +// Runs all pending interrupts. Callers must update unhandlified object +// references after this function completes. +IrregexpInterpreter::Result HandleInterrupts(Isolate* isolate, + Handle subject_string) { + DisallowHeapAllocation no_gc; + + StackLimitCheck check(isolate); + if (check.JsHasOverflowed()) { + return StackOverflow(isolate); // A real stack overflow. + } + + // Handle interrupts if any exist. + if (check.InterruptRequested()) { + const bool was_one_byte = + String::IsOneByteRepresentationUnderneath(*subject_string); + + Object result; + { + AllowHeapAllocation yes_gc; + result = isolate->stack_guard()->HandleInterrupts(); + } + + if (result.IsException(isolate)) { + return IrregexpInterpreter::EXCEPTION; + } + + // If we changed between a LATIN1 and a UC16 string, we need to restart + // regexp matching with the appropriate template instantiation of RawMatch. + if (String::IsOneByteRepresentationUnderneath(*subject_string) != + was_one_byte) { + return IrregexpInterpreter::RETRY; + } + } + + return IrregexpInterpreter::SUCCESS; +} + +template +void UpdateCodeAndSubjectReferences(Isolate* isolate, + Handle code_array, + Handle subject_string, + const byte** code_base_out, + const byte** pc_out, + Vector* subject_string_out) { + DisallowHeapAllocation no_gc; + + if (*code_base_out != code_array->GetDataStartAddress()) { + const intptr_t pc_offset = *pc_out - *code_base_out; + DCHECK_GT(pc_offset, 0); + *code_base_out = code_array->GetDataStartAddress(); + *pc_out = *code_base_out + pc_offset; + } + + DCHECK(subject_string->IsFlat()); + *subject_string_out = subject_string->GetCharVector(no_gc); +} + +template +IrregexpInterpreter::Result RawMatch(Isolate* isolate, + Handle code_array, + Handle subject_string, + Vector subject, int* registers, + int current, uint32_t current_char) { + DisallowHeapAllocation no_gc; + + const byte* pc = code_array->GetDataStartAddress(); + const byte* code_base = pc; + + BacktrackStack backtrack_stack; + +#ifdef DEBUG + if (FLAG_trace_regexp_bytecodes) { + PrintF("\n\nStart bytecode interpreter\n\n"); + } +#endif + while (true) { + const int32_t insn = Load32Aligned(pc); + switch (insn & BYTECODE_MASK) { + BYTECODE(BREAK) { UNREACHABLE(); } + BYTECODE(PUSH_CP) { + backtrack_stack.push(current); + pc += BC_PUSH_CP_LENGTH; + break; + } + BYTECODE(PUSH_BT) { + backtrack_stack.push(Load32Aligned(pc + 4)); + pc += BC_PUSH_BT_LENGTH; + break; + } + BYTECODE(PUSH_REGISTER) { + backtrack_stack.push(registers[insn >> BYTECODE_SHIFT]); + pc += BC_PUSH_REGISTER_LENGTH; + break; + } + BYTECODE(SET_REGISTER) { + registers[insn >> BYTECODE_SHIFT] = Load32Aligned(pc + 4); + pc += BC_SET_REGISTER_LENGTH; + break; + } + BYTECODE(ADVANCE_REGISTER) { + registers[insn >> BYTECODE_SHIFT] += Load32Aligned(pc + 4); + pc += BC_ADVANCE_REGISTER_LENGTH; + break; + } + BYTECODE(SET_REGISTER_TO_CP) { + registers[insn >> BYTECODE_SHIFT] = current + Load32Aligned(pc + 4); + pc += BC_SET_REGISTER_TO_CP_LENGTH; + break; + } + BYTECODE(SET_CP_TO_REGISTER) { + current = registers[insn >> BYTECODE_SHIFT]; + pc += BC_SET_CP_TO_REGISTER_LENGTH; + break; + } + BYTECODE(SET_REGISTER_TO_SP) { + registers[insn >> BYTECODE_SHIFT] = backtrack_stack.sp(); + pc += BC_SET_REGISTER_TO_SP_LENGTH; + break; + } + BYTECODE(SET_SP_TO_REGISTER) { + backtrack_stack.set_sp(registers[insn >> BYTECODE_SHIFT]); + pc += BC_SET_SP_TO_REGISTER_LENGTH; + break; + } + BYTECODE(POP_CP) { + current = backtrack_stack.pop(); + pc += BC_POP_CP_LENGTH; + break; + } + BYTECODE(POP_BT) { + IrregexpInterpreter::Result return_code = + HandleInterrupts(isolate, subject_string); + if (return_code != IrregexpInterpreter::SUCCESS) return return_code; + + UpdateCodeAndSubjectReferences(isolate, code_array, subject_string, + &code_base, &pc, &subject); + + pc = code_base + backtrack_stack.pop(); + break; + } + BYTECODE(POP_REGISTER) { + registers[insn >> BYTECODE_SHIFT] = backtrack_stack.pop(); + pc += BC_POP_REGISTER_LENGTH; + break; + } + BYTECODE(FAIL) { return IrregexpInterpreter::FAILURE; } + BYTECODE(SUCCEED) { return IrregexpInterpreter::SUCCESS; } + BYTECODE(ADVANCE_CP) { + current += insn >> BYTECODE_SHIFT; + pc += BC_ADVANCE_CP_LENGTH; + break; + } + BYTECODE(GOTO) { + pc = code_base + Load32Aligned(pc + 4); + break; + } + BYTECODE(ADVANCE_CP_AND_GOTO) { + current += insn >> BYTECODE_SHIFT; + pc = code_base + Load32Aligned(pc + 4); + break; + } + BYTECODE(CHECK_GREEDY) { + if (current == backtrack_stack.peek()) { + backtrack_stack.pop(); + pc = code_base + Load32Aligned(pc + 4); + } else { + pc += BC_CHECK_GREEDY_LENGTH; + } + break; + } + BYTECODE(LOAD_CURRENT_CHAR) { + int pos = current + (insn >> BYTECODE_SHIFT); + if (pos >= subject.length() || pos < 0) { + pc = code_base + Load32Aligned(pc + 4); + } else { + current_char = subject[pos]; + pc += BC_LOAD_CURRENT_CHAR_LENGTH; + } + break; + } + BYTECODE(LOAD_CURRENT_CHAR_UNCHECKED) { + int pos = current + (insn >> BYTECODE_SHIFT); + current_char = subject[pos]; + pc += BC_LOAD_CURRENT_CHAR_UNCHECKED_LENGTH; + break; + } + BYTECODE(LOAD_2_CURRENT_CHARS) { + int pos = current + (insn >> BYTECODE_SHIFT); + if (pos + 2 > subject.length() || pos < 0) { + pc = code_base + Load32Aligned(pc + 4); + } else { + Char next = subject[pos + 1]; + current_char = + (subject[pos] | (next << (kBitsPerByte * sizeof(Char)))); + pc += BC_LOAD_2_CURRENT_CHARS_LENGTH; + } + break; + } + BYTECODE(LOAD_2_CURRENT_CHARS_UNCHECKED) { + int pos = current + (insn >> BYTECODE_SHIFT); + Char next = subject[pos + 1]; + current_char = (subject[pos] | (next << (kBitsPerByte * sizeof(Char)))); + pc += BC_LOAD_2_CURRENT_CHARS_UNCHECKED_LENGTH; + break; + } + BYTECODE(LOAD_4_CURRENT_CHARS) { + DCHECK_EQ(1, sizeof(Char)); + int pos = current + (insn >> BYTECODE_SHIFT); + if (pos + 4 > subject.length() || pos < 0) { + pc = code_base + Load32Aligned(pc + 4); + } else { + Char next1 = subject[pos + 1]; + Char next2 = subject[pos + 2]; + Char next3 = subject[pos + 3]; + current_char = + (subject[pos] | (next1 << 8) | (next2 << 16) | (next3 << 24)); + pc += BC_LOAD_4_CURRENT_CHARS_LENGTH; + } + break; + } + BYTECODE(LOAD_4_CURRENT_CHARS_UNCHECKED) { + DCHECK_EQ(1, sizeof(Char)); + int pos = current + (insn >> BYTECODE_SHIFT); + Char next1 = subject[pos + 1]; + Char next2 = subject[pos + 2]; + Char next3 = subject[pos + 3]; + current_char = + (subject[pos] | (next1 << 8) | (next2 << 16) | (next3 << 24)); + pc += BC_LOAD_4_CURRENT_CHARS_UNCHECKED_LENGTH; + break; + } + BYTECODE(CHECK_4_CHARS) { + uint32_t c = Load32Aligned(pc + 4); + if (c == current_char) { + pc = code_base + Load32Aligned(pc + 8); + } else { + pc += BC_CHECK_4_CHARS_LENGTH; + } + break; + } + BYTECODE(CHECK_CHAR) { + uint32_t c = (insn >> BYTECODE_SHIFT); + if (c == current_char) { + pc = code_base + Load32Aligned(pc + 4); + } else { + pc += BC_CHECK_CHAR_LENGTH; + } + break; + } + BYTECODE(CHECK_NOT_4_CHARS) { + uint32_t c = Load32Aligned(pc + 4); + if (c != current_char) { + pc = code_base + Load32Aligned(pc + 8); + } else { + pc += BC_CHECK_NOT_4_CHARS_LENGTH; + } + break; + } + BYTECODE(CHECK_NOT_CHAR) { + uint32_t c = (insn >> BYTECODE_SHIFT); + if (c != current_char) { + pc = code_base + Load32Aligned(pc + 4); + } else { + pc += BC_CHECK_NOT_CHAR_LENGTH; + } + break; + } + BYTECODE(AND_CHECK_4_CHARS) { + uint32_t c = Load32Aligned(pc + 4); + if (c == (current_char & Load32Aligned(pc + 8))) { + pc = code_base + Load32Aligned(pc + 12); + } else { + pc += BC_AND_CHECK_4_CHARS_LENGTH; + } + break; + } + BYTECODE(AND_CHECK_CHAR) { + uint32_t c = (insn >> BYTECODE_SHIFT); + if (c == (current_char & Load32Aligned(pc + 4))) { + pc = code_base + Load32Aligned(pc + 8); + } else { + pc += BC_AND_CHECK_CHAR_LENGTH; + } + break; + } + BYTECODE(AND_CHECK_NOT_4_CHARS) { + uint32_t c = Load32Aligned(pc + 4); + if (c != (current_char & Load32Aligned(pc + 8))) { + pc = code_base + Load32Aligned(pc + 12); + } else { + pc += BC_AND_CHECK_NOT_4_CHARS_LENGTH; + } + break; + } + BYTECODE(AND_CHECK_NOT_CHAR) { + uint32_t c = (insn >> BYTECODE_SHIFT); + if (c != (current_char & Load32Aligned(pc + 4))) { + pc = code_base + Load32Aligned(pc + 8); + } else { + pc += BC_AND_CHECK_NOT_CHAR_LENGTH; + } + break; + } + BYTECODE(MINUS_AND_CHECK_NOT_CHAR) { + uint32_t c = (insn >> BYTECODE_SHIFT); + uint32_t minus = Load16Aligned(pc + 4); + uint32_t mask = Load16Aligned(pc + 6); + if (c != ((current_char - minus) & mask)) { + pc = code_base + Load32Aligned(pc + 8); + } else { + pc += BC_MINUS_AND_CHECK_NOT_CHAR_LENGTH; + } + break; + } + BYTECODE(CHECK_CHAR_IN_RANGE) { + uint32_t from = Load16Aligned(pc + 4); + uint32_t to = Load16Aligned(pc + 6); + if (from <= current_char && current_char <= to) { + pc = code_base + Load32Aligned(pc + 8); + } else { + pc += BC_CHECK_CHAR_IN_RANGE_LENGTH; + } + break; + } + BYTECODE(CHECK_CHAR_NOT_IN_RANGE) { + uint32_t from = Load16Aligned(pc + 4); + uint32_t to = Load16Aligned(pc + 6); + if (from > current_char || current_char > to) { + pc = code_base + Load32Aligned(pc + 8); + } else { + pc += BC_CHECK_CHAR_NOT_IN_RANGE_LENGTH; + } + break; + } + BYTECODE(CHECK_BIT_IN_TABLE) { + int mask = RegExpMacroAssembler::kTableMask; + byte b = pc[8 + ((current_char & mask) >> kBitsPerByteLog2)]; + int bit = (current_char & (kBitsPerByte - 1)); + if ((b & (1 << bit)) != 0) { + pc = code_base + Load32Aligned(pc + 4); + } else { + pc += BC_CHECK_BIT_IN_TABLE_LENGTH; + } + break; + } + BYTECODE(CHECK_LT) { + uint32_t limit = (insn >> BYTECODE_SHIFT); + if (current_char < limit) { + pc = code_base + Load32Aligned(pc + 4); + } else { + pc += BC_CHECK_LT_LENGTH; + } + break; + } + BYTECODE(CHECK_GT) { + uint32_t limit = (insn >> BYTECODE_SHIFT); + if (current_char > limit) { + pc = code_base + Load32Aligned(pc + 4); + } else { + pc += BC_CHECK_GT_LENGTH; + } + break; + } + BYTECODE(CHECK_REGISTER_LT) { + if (registers[insn >> BYTECODE_SHIFT] < Load32Aligned(pc + 4)) { + pc = code_base + Load32Aligned(pc + 8); + } else { + pc += BC_CHECK_REGISTER_LT_LENGTH; + } + break; + } + BYTECODE(CHECK_REGISTER_GE) { + if (registers[insn >> BYTECODE_SHIFT] >= Load32Aligned(pc + 4)) { + pc = code_base + Load32Aligned(pc + 8); + } else { + pc += BC_CHECK_REGISTER_GE_LENGTH; + } + break; + } + BYTECODE(CHECK_REGISTER_EQ_POS) { + if (registers[insn >> BYTECODE_SHIFT] == current) { + pc = code_base + Load32Aligned(pc + 4); + } else { + pc += BC_CHECK_REGISTER_EQ_POS_LENGTH; + } + break; + } + BYTECODE(CHECK_NOT_REGS_EQUAL) { + if (registers[insn >> BYTECODE_SHIFT] == + registers[Load32Aligned(pc + 4)]) { + pc += BC_CHECK_NOT_REGS_EQUAL_LENGTH; + } else { + pc = code_base + Load32Aligned(pc + 8); + } + break; + } + BYTECODE(CHECK_NOT_BACK_REF) { + int from = registers[insn >> BYTECODE_SHIFT]; + int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; + if (from >= 0 && len > 0) { + if (current + len > subject.length() || + CompareChars(&subject[from], &subject[current], len) != 0) { + pc = code_base + Load32Aligned(pc + 4); + break; + } + current += len; + } + pc += BC_CHECK_NOT_BACK_REF_LENGTH; + break; + } + BYTECODE(CHECK_NOT_BACK_REF_BACKWARD) { + int from = registers[insn >> BYTECODE_SHIFT]; + int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; + if (from >= 0 && len > 0) { + if (current - len < 0 || + CompareChars(&subject[from], &subject[current - len], len) != 0) { + pc = code_base + Load32Aligned(pc + 4); + break; + } + current -= len; + } + pc += BC_CHECK_NOT_BACK_REF_BACKWARD_LENGTH; + break; + } + BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE) + V8_FALLTHROUGH; + BYTECODE(CHECK_NOT_BACK_REF_NO_CASE) { + bool unicode = + (insn & BYTECODE_MASK) == BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE; + int from = registers[insn >> BYTECODE_SHIFT]; + int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; + if (from >= 0 && len > 0) { + if (current + len > subject.length() || + !BackRefMatchesNoCase(isolate, from, current, len, subject, + unicode)) { + pc = code_base + Load32Aligned(pc + 4); + break; + } + current += len; + } + pc += BC_CHECK_NOT_BACK_REF_NO_CASE_LENGTH; + break; + } + BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD) + V8_FALLTHROUGH; + BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_BACKWARD) { + bool unicode = (insn & BYTECODE_MASK) == + BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD; + int from = registers[insn >> BYTECODE_SHIFT]; + int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; + if (from >= 0 && len > 0) { + if (current - len < 0 || + !BackRefMatchesNoCase(isolate, from, current - len, len, subject, + unicode)) { + pc = code_base + Load32Aligned(pc + 4); + break; + } + current -= len; + } + pc += BC_CHECK_NOT_BACK_REF_NO_CASE_BACKWARD_LENGTH; + break; + } + BYTECODE(CHECK_AT_START) { + if (current == 0) { + pc = code_base + Load32Aligned(pc + 4); + } else { + pc += BC_CHECK_AT_START_LENGTH; + } + break; + } + BYTECODE(CHECK_NOT_AT_START) { + if (current + (insn >> BYTECODE_SHIFT) == 0) { + pc += BC_CHECK_NOT_AT_START_LENGTH; + } else { + pc = code_base + Load32Aligned(pc + 4); + } + break; + } + BYTECODE(SET_CURRENT_POSITION_FROM_END) { + int by = static_cast(insn) >> BYTECODE_SHIFT; + if (subject.length() - current > by) { + current = subject.length() - by; + current_char = subject[current - 1]; + } + pc += BC_SET_CURRENT_POSITION_FROM_END_LENGTH; + break; + } + default: + UNREACHABLE(); + break; + } + } +} + +#undef BYTECODE + +} // namespace + +// static +IrregexpInterpreter::Result IrregexpInterpreter::Match( + Isolate* isolate, Handle code_array, + Handle subject_string, int* registers, int start_position) { + DCHECK(subject_string->IsFlat()); + + // Note: Heap allocation *is* allowed in two situations: + // 1. When creating & throwing a stack overflow exception. The interpreter + // aborts afterwards, and thus possible-moved objects are never used. + // 2. When handling interrupts. We manually relocate unhandlified references + // after interrupts have run. + DisallowHeapAllocation no_gc; + + uc16 previous_char = '\n'; + String::FlatContent subject_content = subject_string->GetFlatContent(no_gc); + if (subject_content.IsOneByte()) { + Vector subject_vector = subject_content.ToOneByteVector(); + if (start_position != 0) previous_char = subject_vector[start_position - 1]; + return RawMatch(isolate, code_array, subject_string, subject_vector, + registers, start_position, previous_char); + } else { + DCHECK(subject_content.IsTwoByte()); + Vector subject_vector = subject_content.ToUC16Vector(); + if (start_position != 0) previous_char = subject_vector[start_position - 1]; + return RawMatch(isolate, code_array, subject_string, subject_vector, + registers, start_position, previous_char); + } +} + +} // namespace internal +} // namespace v8 diff --git a/chromium/v8/src/regexp/regexp-interpreter.h b/chromium/v8/src/regexp/regexp-interpreter.h new file mode 100644 index 00000000000..ad27dcd2961 --- /dev/null +++ b/chromium/v8/src/regexp/regexp-interpreter.h @@ -0,0 +1,33 @@ +// Copyright 2011 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// A simple interpreter for the Irregexp byte code. + +#ifndef V8_REGEXP_REGEXP_INTERPRETER_H_ +#define V8_REGEXP_REGEXP_INTERPRETER_H_ + +#include "src/regexp/regexp.h" + +namespace v8 { +namespace internal { + +class V8_EXPORT_PRIVATE IrregexpInterpreter { + public: + enum Result { + FAILURE = RegExp::kInternalRegExpFailure, + SUCCESS = RegExp::kInternalRegExpSuccess, + EXCEPTION = RegExp::kInternalRegExpException, + RETRY = RegExp::kInternalRegExpRetry, + }; + + // The caller is responsible for initializing registers before each call. + static Result Match(Isolate* isolate, Handle code_array, + Handle subject_string, int* registers, + int start_position); +}; + +} // namespace internal +} // namespace v8 + +#endif // V8_REGEXP_REGEXP_INTERPRETER_H_ diff --git a/chromium/v8/src/regexp/regexp-macro-assembler-arch.h b/chromium/v8/src/regexp/regexp-macro-assembler-arch.h new file mode 100644 index 00000000000..2dc6739e421 --- /dev/null +++ b/chromium/v8/src/regexp/regexp-macro-assembler-arch.h @@ -0,0 +1,30 @@ +// Copyright 2019 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_REGEXP_REGEXP_MACRO_ASSEMBLER_ARCH_H_ +#define V8_REGEXP_REGEXP_MACRO_ASSEMBLER_ARCH_H_ + +#include "src/regexp/regexp-macro-assembler.h" + +#if V8_TARGET_ARCH_IA32 +#include "src/regexp/ia32/regexp-macro-assembler-ia32.h" +#elif V8_TARGET_ARCH_X64 +#include "src/regexp/x64/regexp-macro-assembler-x64.h" +#elif V8_TARGET_ARCH_ARM64 +#include "src/regexp/arm64/regexp-macro-assembler-arm64.h" +#elif V8_TARGET_ARCH_ARM +#include "src/regexp/arm/regexp-macro-assembler-arm.h" +#elif V8_TARGET_ARCH_PPC +#include "src/regexp/ppc/regexp-macro-assembler-ppc.h" +#elif V8_TARGET_ARCH_MIPS +#include "src/regexp/mips/regexp-macro-assembler-mips.h" +#elif V8_TARGET_ARCH_MIPS64 +#include "src/regexp/mips64/regexp-macro-assembler-mips64.h" +#elif V8_TARGET_ARCH_S390 +#include "src/regexp/s390/regexp-macro-assembler-s390.h" +#else +#error Unsupported target architecture. +#endif + +#endif // V8_REGEXP_REGEXP_MACRO_ASSEMBLER_ARCH_H_ diff --git a/chromium/v8/src/regexp/regexp-macro-assembler-irregexp-inl.h b/chromium/v8/src/regexp/regexp-macro-assembler-irregexp-inl.h deleted file mode 100644 index cda48aa00bc..00000000000 --- a/chromium/v8/src/regexp/regexp-macro-assembler-irregexp-inl.h +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2008-2009 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef V8_REGEXP_REGEXP_MACRO_ASSEMBLER_IRREGEXP_INL_H_ -#define V8_REGEXP_REGEXP_MACRO_ASSEMBLER_IRREGEXP_INL_H_ - -#include "src/regexp/regexp-macro-assembler-irregexp.h" - -#include "src/ast/ast.h" -#include "src/regexp/bytecodes-irregexp.h" - -namespace v8 { -namespace internal { - -void RegExpMacroAssemblerIrregexp::Emit(uint32_t byte, - uint32_t twenty_four_bits) { - uint32_t word = ((twenty_four_bits << BYTECODE_SHIFT) | byte); - DCHECK(pc_ <= buffer_.length()); - if (pc_ + 3 >= buffer_.length()) { - Expand(); - } - *reinterpret_cast(buffer_.begin() + pc_) = word; - pc_ += 4; -} - - -void RegExpMacroAssemblerIrregexp::Emit16(uint32_t word) { - DCHECK(pc_ <= buffer_.length()); - if (pc_ + 1 >= buffer_.length()) { - Expand(); - } - *reinterpret_cast(buffer_.begin() + pc_) = word; - pc_ += 2; -} - - -void RegExpMacroAssemblerIrregexp::Emit8(uint32_t word) { - DCHECK(pc_ <= buffer_.length()); - if (pc_ == buffer_.length()) { - Expand(); - } - *reinterpret_cast(buffer_.begin() + pc_) = word; - pc_ += 1; -} - - -void RegExpMacroAssemblerIrregexp::Emit32(uint32_t word) { - DCHECK(pc_ <= buffer_.length()); - if (pc_ + 3 >= buffer_.length()) { - Expand(); - } - *reinterpret_cast(buffer_.begin() + pc_) = word; - pc_ += 4; -} - -} // namespace internal -} // namespace v8 - -#endif // V8_REGEXP_REGEXP_MACRO_ASSEMBLER_IRREGEXP_INL_H_ diff --git a/chromium/v8/src/regexp/regexp-macro-assembler-irregexp.cc b/chromium/v8/src/regexp/regexp-macro-assembler-irregexp.cc deleted file mode 100644 index 712f00e5095..00000000000 --- a/chromium/v8/src/regexp/regexp-macro-assembler-irregexp.cc +++ /dev/null @@ -1,455 +0,0 @@ -// Copyright 2008-2009 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "src/regexp/regexp-macro-assembler-irregexp.h" - -#include "src/ast/ast.h" -#include "src/objects/objects-inl.h" -#include "src/regexp/bytecodes-irregexp.h" -#include "src/regexp/regexp-macro-assembler-irregexp-inl.h" -#include "src/regexp/regexp-macro-assembler.h" - -namespace v8 { -namespace internal { - -RegExpMacroAssemblerIrregexp::RegExpMacroAssemblerIrregexp(Isolate* isolate, - Zone* zone) - : RegExpMacroAssembler(isolate, zone), - buffer_(Vector::New(1024)), - pc_(0), - own_buffer_(true), - advance_current_end_(kInvalidPC), - isolate_(isolate) {} - -RegExpMacroAssemblerIrregexp::~RegExpMacroAssemblerIrregexp() { - if (backtrack_.is_linked()) backtrack_.Unuse(); - if (own_buffer_) buffer_.Dispose(); -} - - -RegExpMacroAssemblerIrregexp::IrregexpImplementation -RegExpMacroAssemblerIrregexp::Implementation() { - return kBytecodeImplementation; -} - - -void RegExpMacroAssemblerIrregexp::Bind(Label* l) { - advance_current_end_ = kInvalidPC; - DCHECK(!l->is_bound()); - if (l->is_linked()) { - int pos = l->pos(); - while (pos != 0) { - int fixup = pos; - pos = *reinterpret_cast(buffer_.begin() + fixup); - *reinterpret_cast(buffer_.begin() + fixup) = pc_; - } - } - l->bind_to(pc_); -} - - -void RegExpMacroAssemblerIrregexp::EmitOrLink(Label* l) { - if (l == nullptr) l = &backtrack_; - if (l->is_bound()) { - Emit32(l->pos()); - } else { - int pos = 0; - if (l->is_linked()) { - pos = l->pos(); - } - l->link_to(pc_); - Emit32(pos); - } -} - - -void RegExpMacroAssemblerIrregexp::PopRegister(int register_index) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_POP_REGISTER, register_index); -} - - -void RegExpMacroAssemblerIrregexp::PushRegister( - int register_index, - StackCheckFlag check_stack_limit) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_PUSH_REGISTER, register_index); -} - - -void RegExpMacroAssemblerIrregexp::WriteCurrentPositionToRegister( - int register_index, int cp_offset) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_SET_REGISTER_TO_CP, register_index); - Emit32(cp_offset); // Current position offset. -} - - -void RegExpMacroAssemblerIrregexp::ClearRegisters(int reg_from, int reg_to) { - DCHECK(reg_from <= reg_to); - for (int reg = reg_from; reg <= reg_to; reg++) { - SetRegister(reg, -1); - } -} - - -void RegExpMacroAssemblerIrregexp::ReadCurrentPositionFromRegister( - int register_index) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_SET_CP_TO_REGISTER, register_index); -} - - -void RegExpMacroAssemblerIrregexp::WriteStackPointerToRegister( - int register_index) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_SET_REGISTER_TO_SP, register_index); -} - - -void RegExpMacroAssemblerIrregexp::ReadStackPointerFromRegister( - int register_index) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_SET_SP_TO_REGISTER, register_index); -} - - -void RegExpMacroAssemblerIrregexp::SetCurrentPositionFromEnd(int by) { - DCHECK(is_uint24(by)); - Emit(BC_SET_CURRENT_POSITION_FROM_END, by); -} - - -void RegExpMacroAssemblerIrregexp::SetRegister(int register_index, int to) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_SET_REGISTER, register_index); - Emit32(to); -} - - -void RegExpMacroAssemblerIrregexp::AdvanceRegister(int register_index, int by) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_ADVANCE_REGISTER, register_index); - Emit32(by); -} - - -void RegExpMacroAssemblerIrregexp::PopCurrentPosition() { - Emit(BC_POP_CP, 0); -} - - -void RegExpMacroAssemblerIrregexp::PushCurrentPosition() { - Emit(BC_PUSH_CP, 0); -} - - -void RegExpMacroAssemblerIrregexp::Backtrack() { - Emit(BC_POP_BT, 0); -} - - -void RegExpMacroAssemblerIrregexp::GoTo(Label* l) { - if (advance_current_end_ == pc_) { - // Combine advance current and goto. - pc_ = advance_current_start_; - Emit(BC_ADVANCE_CP_AND_GOTO, advance_current_offset_); - EmitOrLink(l); - advance_current_end_ = kInvalidPC; - } else { - // Regular goto. - Emit(BC_GOTO, 0); - EmitOrLink(l); - } -} - - -void RegExpMacroAssemblerIrregexp::PushBacktrack(Label* l) { - Emit(BC_PUSH_BT, 0); - EmitOrLink(l); -} - - -bool RegExpMacroAssemblerIrregexp::Succeed() { - Emit(BC_SUCCEED, 0); - return false; // Restart matching for global regexp not supported. -} - - -void RegExpMacroAssemblerIrregexp::Fail() { - Emit(BC_FAIL, 0); -} - - -void RegExpMacroAssemblerIrregexp::AdvanceCurrentPosition(int by) { - DCHECK_LE(kMinCPOffset, by); - DCHECK_GE(kMaxCPOffset, by); - advance_current_start_ = pc_; - advance_current_offset_ = by; - Emit(BC_ADVANCE_CP, by); - advance_current_end_ = pc_; -} - - -void RegExpMacroAssemblerIrregexp::CheckGreedyLoop( - Label* on_tos_equals_current_position) { - Emit(BC_CHECK_GREEDY, 0); - EmitOrLink(on_tos_equals_current_position); -} - - -void RegExpMacroAssemblerIrregexp::LoadCurrentCharacter(int cp_offset, - Label* on_failure, - bool check_bounds, - int characters) { - DCHECK_LE(kMinCPOffset, cp_offset); - DCHECK_GE(kMaxCPOffset, cp_offset); - int bytecode; - if (check_bounds) { - if (characters == 4) { - bytecode = BC_LOAD_4_CURRENT_CHARS; - } else if (characters == 2) { - bytecode = BC_LOAD_2_CURRENT_CHARS; - } else { - DCHECK_EQ(1, characters); - bytecode = BC_LOAD_CURRENT_CHAR; - } - } else { - if (characters == 4) { - bytecode = BC_LOAD_4_CURRENT_CHARS_UNCHECKED; - } else if (characters == 2) { - bytecode = BC_LOAD_2_CURRENT_CHARS_UNCHECKED; - } else { - DCHECK_EQ(1, characters); - bytecode = BC_LOAD_CURRENT_CHAR_UNCHECKED; - } - } - Emit(bytecode, cp_offset); - if (check_bounds) EmitOrLink(on_failure); -} - - -void RegExpMacroAssemblerIrregexp::CheckCharacterLT(uc16 limit, - Label* on_less) { - Emit(BC_CHECK_LT, limit); - EmitOrLink(on_less); -} - - -void RegExpMacroAssemblerIrregexp::CheckCharacterGT(uc16 limit, - Label* on_greater) { - Emit(BC_CHECK_GT, limit); - EmitOrLink(on_greater); -} - - -void RegExpMacroAssemblerIrregexp::CheckCharacter(uint32_t c, Label* on_equal) { - if (c > MAX_FIRST_ARG) { - Emit(BC_CHECK_4_CHARS, 0); - Emit32(c); - } else { - Emit(BC_CHECK_CHAR, c); - } - EmitOrLink(on_equal); -} - - -void RegExpMacroAssemblerIrregexp::CheckAtStart(Label* on_at_start) { - Emit(BC_CHECK_AT_START, 0); - EmitOrLink(on_at_start); -} - - -void RegExpMacroAssemblerIrregexp::CheckNotAtStart(int cp_offset, - Label* on_not_at_start) { - Emit(BC_CHECK_NOT_AT_START, cp_offset); - EmitOrLink(on_not_at_start); -} - - -void RegExpMacroAssemblerIrregexp::CheckNotCharacter(uint32_t c, - Label* on_not_equal) { - if (c > MAX_FIRST_ARG) { - Emit(BC_CHECK_NOT_4_CHARS, 0); - Emit32(c); - } else { - Emit(BC_CHECK_NOT_CHAR, c); - } - EmitOrLink(on_not_equal); -} - - -void RegExpMacroAssemblerIrregexp::CheckCharacterAfterAnd( - uint32_t c, - uint32_t mask, - Label* on_equal) { - if (c > MAX_FIRST_ARG) { - Emit(BC_AND_CHECK_4_CHARS, 0); - Emit32(c); - } else { - Emit(BC_AND_CHECK_CHAR, c); - } - Emit32(mask); - EmitOrLink(on_equal); -} - - -void RegExpMacroAssemblerIrregexp::CheckNotCharacterAfterAnd( - uint32_t c, - uint32_t mask, - Label* on_not_equal) { - if (c > MAX_FIRST_ARG) { - Emit(BC_AND_CHECK_NOT_4_CHARS, 0); - Emit32(c); - } else { - Emit(BC_AND_CHECK_NOT_CHAR, c); - } - Emit32(mask); - EmitOrLink(on_not_equal); -} - - -void RegExpMacroAssemblerIrregexp::CheckNotCharacterAfterMinusAnd( - uc16 c, - uc16 minus, - uc16 mask, - Label* on_not_equal) { - Emit(BC_MINUS_AND_CHECK_NOT_CHAR, c); - Emit16(minus); - Emit16(mask); - EmitOrLink(on_not_equal); -} - - -void RegExpMacroAssemblerIrregexp::CheckCharacterInRange( - uc16 from, - uc16 to, - Label* on_in_range) { - Emit(BC_CHECK_CHAR_IN_RANGE, 0); - Emit16(from); - Emit16(to); - EmitOrLink(on_in_range); -} - - -void RegExpMacroAssemblerIrregexp::CheckCharacterNotInRange( - uc16 from, - uc16 to, - Label* on_not_in_range) { - Emit(BC_CHECK_CHAR_NOT_IN_RANGE, 0); - Emit16(from); - Emit16(to); - EmitOrLink(on_not_in_range); -} - - -void RegExpMacroAssemblerIrregexp::CheckBitInTable( - Handle table, Label* on_bit_set) { - Emit(BC_CHECK_BIT_IN_TABLE, 0); - EmitOrLink(on_bit_set); - for (int i = 0; i < kTableSize; i += kBitsPerByte) { - int byte = 0; - for (int j = 0; j < kBitsPerByte; j++) { - if (table->get(i + j) != 0) byte |= 1 << j; - } - Emit8(byte); - } -} - - -void RegExpMacroAssemblerIrregexp::CheckNotBackReference(int start_reg, - bool read_backward, - Label* on_not_equal) { - DCHECK_LE(0, start_reg); - DCHECK_GE(kMaxRegister, start_reg); - Emit(read_backward ? BC_CHECK_NOT_BACK_REF_BACKWARD : BC_CHECK_NOT_BACK_REF, - start_reg); - EmitOrLink(on_not_equal); -} - - -void RegExpMacroAssemblerIrregexp::CheckNotBackReferenceIgnoreCase( - int start_reg, bool read_backward, bool unicode, Label* on_not_equal) { - DCHECK_LE(0, start_reg); - DCHECK_GE(kMaxRegister, start_reg); - Emit(read_backward ? (unicode ? BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD - : BC_CHECK_NOT_BACK_REF_NO_CASE_BACKWARD) - : (unicode ? BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE - : BC_CHECK_NOT_BACK_REF_NO_CASE), - start_reg); - EmitOrLink(on_not_equal); -} - - -void RegExpMacroAssemblerIrregexp::IfRegisterLT(int register_index, - int comparand, - Label* on_less_than) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_CHECK_REGISTER_LT, register_index); - Emit32(comparand); - EmitOrLink(on_less_than); -} - - -void RegExpMacroAssemblerIrregexp::IfRegisterGE(int register_index, - int comparand, - Label* on_greater_or_equal) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_CHECK_REGISTER_GE, register_index); - Emit32(comparand); - EmitOrLink(on_greater_or_equal); -} - - -void RegExpMacroAssemblerIrregexp::IfRegisterEqPos(int register_index, - Label* on_eq) { - DCHECK_LE(0, register_index); - DCHECK_GE(kMaxRegister, register_index); - Emit(BC_CHECK_REGISTER_EQ_POS, register_index); - EmitOrLink(on_eq); -} - - -Handle RegExpMacroAssemblerIrregexp::GetCode( - Handle source) { - Bind(&backtrack_); - Emit(BC_POP_BT, 0); - Handle array = isolate_->factory()->NewByteArray(length()); - Copy(array->GetDataStartAddress()); - return array; -} - - -int RegExpMacroAssemblerIrregexp::length() { - return pc_; -} - -void RegExpMacroAssemblerIrregexp::Copy(byte* a) { - MemCopy(a, buffer_.begin(), length()); -} - - -void RegExpMacroAssemblerIrregexp::Expand() { - bool old_buffer_was_our_own = own_buffer_; - Vector old_buffer = buffer_; - buffer_ = Vector::New(old_buffer.length() * 2); - own_buffer_ = true; - MemCopy(buffer_.begin(), old_buffer.begin(), old_buffer.length()); - if (old_buffer_was_our_own) { - old_buffer.Dispose(); - } -} - -} // namespace internal -} // namespace v8 diff --git a/chromium/v8/src/regexp/regexp-macro-assembler-irregexp.h b/chromium/v8/src/regexp/regexp-macro-assembler-irregexp.h deleted file mode 100644 index 9e17dca4150..00000000000 --- a/chromium/v8/src/regexp/regexp-macro-assembler-irregexp.h +++ /dev/null @@ -1,124 +0,0 @@ -// Copyright 2012 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef V8_REGEXP_REGEXP_MACRO_ASSEMBLER_IRREGEXP_H_ -#define V8_REGEXP_REGEXP_MACRO_ASSEMBLER_IRREGEXP_H_ - -#include "src/regexp/regexp-macro-assembler.h" - -namespace v8 { -namespace internal { - -// A light-weight assembler for the Irregexp byte code. -class V8_EXPORT_PRIVATE RegExpMacroAssemblerIrregexp - : public RegExpMacroAssembler { - public: - // Create an assembler. Instructions and relocation information are emitted - // into a buffer, with the instructions starting from the beginning and the - // relocation information starting from the end of the buffer. See CodeDesc - // for a detailed comment on the layout (globals.h). - // - // The assembler allocates and grows its own buffer, and buffer_size - // determines the initial buffer size. The buffer is owned by the assembler - // and deallocated upon destruction of the assembler. - RegExpMacroAssemblerIrregexp(Isolate* isolate, Zone* zone); - virtual ~RegExpMacroAssemblerIrregexp(); - // The byte-code interpreter checks on each push anyway. - virtual int stack_limit_slack() { return 1; } - virtual bool CanReadUnaligned() { return false; } - virtual void Bind(Label* label); - virtual void AdvanceCurrentPosition(int by); // Signed cp change. - virtual void PopCurrentPosition(); - virtual void PushCurrentPosition(); - virtual void Backtrack(); - virtual void GoTo(Label* label); - virtual void PushBacktrack(Label* label); - virtual bool Succeed(); - virtual void Fail(); - virtual void PopRegister(int register_index); - virtual void PushRegister(int register_index, - StackCheckFlag check_stack_limit); - virtual void AdvanceRegister(int reg, int by); // r[reg] += by. - virtual void SetCurrentPositionFromEnd(int by); - virtual void SetRegister(int register_index, int to); - virtual void WriteCurrentPositionToRegister(int reg, int cp_offset); - virtual void ClearRegisters(int reg_from, int reg_to); - virtual void ReadCurrentPositionFromRegister(int reg); - virtual void WriteStackPointerToRegister(int reg); - virtual void ReadStackPointerFromRegister(int reg); - virtual void LoadCurrentCharacter(int cp_offset, - Label* on_end_of_input, - bool check_bounds = true, - int characters = 1); - virtual void CheckCharacter(unsigned c, Label* on_equal); - virtual void CheckCharacterAfterAnd(unsigned c, - unsigned mask, - Label* on_equal); - virtual void CheckCharacterGT(uc16 limit, Label* on_greater); - virtual void CheckCharacterLT(uc16 limit, Label* on_less); - virtual void CheckGreedyLoop(Label* on_tos_equals_current_position); - virtual void CheckAtStart(Label* on_at_start); - virtual void CheckNotAtStart(int cp_offset, Label* on_not_at_start); - virtual void CheckNotCharacter(unsigned c, Label* on_not_equal); - virtual void CheckNotCharacterAfterAnd(unsigned c, - unsigned mask, - Label* on_not_equal); - virtual void CheckNotCharacterAfterMinusAnd(uc16 c, - uc16 minus, - uc16 mask, - Label* on_not_equal); - virtual void CheckCharacterInRange(uc16 from, - uc16 to, - Label* on_in_range); - virtual void CheckCharacterNotInRange(uc16 from, - uc16 to, - Label* on_not_in_range); - virtual void CheckBitInTable(Handle table, Label* on_bit_set); - virtual void CheckNotBackReference(int start_reg, bool read_backward, - Label* on_no_match); - virtual void CheckNotBackReferenceIgnoreCase(int start_reg, - bool read_backward, bool unicode, - Label* on_no_match); - virtual void IfRegisterLT(int register_index, int comparand, Label* if_lt); - virtual void IfRegisterGE(int register_index, int comparand, Label* if_ge); - virtual void IfRegisterEqPos(int register_index, Label* if_eq); - - virtual IrregexpImplementation Implementation(); - virtual Handle GetCode(Handle source); - - private: - void Expand(); - // Code and bitmap emission. - inline void EmitOrLink(Label* label); - inline void Emit32(uint32_t x); - inline void Emit16(uint32_t x); - inline void Emit8(uint32_t x); - inline void Emit(uint32_t bc, uint32_t arg); - // Bytecode buffer. - int length(); - void Copy(byte* a); - - // The buffer into which code and relocation info are generated. - Vector buffer_; - // The program counter. - int pc_; - // True if the assembler owns the buffer, false if buffer is external. - bool own_buffer_; - Label backtrack_; - - int advance_current_start_; - int advance_current_offset_; - int advance_current_end_; - - Isolate* isolate_; - - static const int kInvalidPC = -1; - - DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpMacroAssemblerIrregexp); -}; - -} // namespace internal -} // namespace v8 - -#endif // V8_REGEXP_REGEXP_MACRO_ASSEMBLER_IRREGEXP_H_ diff --git a/chromium/v8/src/regexp/regexp-macro-assembler.cc b/chromium/v8/src/regexp/regexp-macro-assembler.cc index cfe827ef4e4..68fa16db611 100644 --- a/chromium/v8/src/regexp/regexp-macro-assembler.cc +++ b/chromium/v8/src/regexp/regexp-macro-assembler.cc @@ -133,7 +133,8 @@ int NativeRegExpMacroAssembler::CheckStackGuardState( Isolate* isolate, int start_index, bool is_direct_call, Address* return_address, Code re_code, Address* subject, const byte** input_start, const byte** input_end) { - AllowHeapAllocation allow_allocation; + DisallowHeapAllocation no_gc; + DCHECK(re_code.raw_instruction_start() <= *return_address); DCHECK(*return_address <= re_code.raw_instruction_end()); int return_value = 0; @@ -154,15 +155,15 @@ int NativeRegExpMacroAssembler::CheckStackGuardState( // forcing the call through the runtime system. return_value = js_has_overflowed ? EXCEPTION : RETRY; } else if (js_has_overflowed) { + AllowHeapAllocation yes_gc; isolate->StackOverflow(); return_value = EXCEPTION; - } else { + } else if (check.InterruptRequested()) { + AllowHeapAllocation yes_gc; Object result = isolate->stack_guard()->HandleInterrupts(); if (result.IsException(isolate)) return_value = EXCEPTION; } - DisallowHeapAllocation no_gc; - if (*code_handle != re_code) { // Return address no longer valid intptr_t delta = code_handle->address() - re_code.address(); // Overwrite the return address on the stack. diff --git a/chromium/v8/src/regexp/regexp-macro-assembler.h b/chromium/v8/src/regexp/regexp-macro-assembler.h index 8626d1a19ef..b55ac13590d 100644 --- a/chromium/v8/src/regexp/regexp-macro-assembler.h +++ b/chromium/v8/src/regexp/regexp-macro-assembler.h @@ -7,6 +7,7 @@ #include "src/codegen/label.h" #include "src/regexp/regexp-ast.h" +#include "src/regexp/regexp.h" namespace v8 { namespace internal { @@ -206,7 +207,12 @@ class NativeRegExpMacroAssembler: public RegExpMacroAssembler { // FAILURE: Matching failed. // SUCCESS: Matching succeeded, and the output array has been filled with // capture positions. - enum Result { RETRY = -2, EXCEPTION = -1, FAILURE = 0, SUCCESS = 1 }; + enum Result { + FAILURE = RegExp::kInternalRegExpFailure, + SUCCESS = RegExp::kInternalRegExpSuccess, + EXCEPTION = RegExp::kInternalRegExpException, + RETRY = RegExp::kInternalRegExpRetry, + }; NativeRegExpMacroAssembler(Isolate* isolate, Zone* zone); ~NativeRegExpMacroAssembler() override; diff --git a/chromium/v8/src/regexp/regexp-nodes.h b/chromium/v8/src/regexp/regexp-nodes.h new file mode 100644 index 00000000000..4c13b74926d --- /dev/null +++ b/chromium/v8/src/regexp/regexp-nodes.h @@ -0,0 +1,675 @@ +// Copyright 2019 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_REGEXP_REGEXP_NODES_H_ +#define V8_REGEXP_REGEXP_NODES_H_ + +#include "src/regexp/regexp-macro-assembler.h" +#include "src/zone/zone.h" + +namespace v8 { +namespace internal { + +class AlternativeGenerationList; +class BoyerMooreLookahead; +class GreedyLoopState; +class Label; +class NodeVisitor; +class QuickCheckDetails; +class RegExpCompiler; +class Trace; +struct PreloadState; + +#define FOR_EACH_NODE_TYPE(VISIT) \ + VISIT(End) \ + VISIT(Action) \ + VISIT(Choice) \ + VISIT(BackReference) \ + VISIT(Assertion) \ + VISIT(Text) + +struct NodeInfo final { + NodeInfo() + : being_analyzed(false), + been_analyzed(false), + follows_word_interest(false), + follows_newline_interest(false), + follows_start_interest(false), + at_end(false), + visited(false), + replacement_calculated(false) {} + + // Returns true if the interests and assumptions of this node + // matches the given one. + bool Matches(NodeInfo* that) { + return (at_end == that->at_end) && + (follows_word_interest == that->follows_word_interest) && + (follows_newline_interest == that->follows_newline_interest) && + (follows_start_interest == that->follows_start_interest); + } + + // Updates the interests of this node given the interests of the + // node preceding it. + void AddFromPreceding(NodeInfo* that) { + at_end |= that->at_end; + follows_word_interest |= that->follows_word_interest; + follows_newline_interest |= that->follows_newline_interest; + follows_start_interest |= that->follows_start_interest; + } + + bool HasLookbehind() { + return follows_word_interest || follows_newline_interest || + follows_start_interest; + } + + // Sets the interests of this node to include the interests of the + // following node. + void AddFromFollowing(NodeInfo* that) { + follows_word_interest |= that->follows_word_interest; + follows_newline_interest |= that->follows_newline_interest; + follows_start_interest |= that->follows_start_interest; + } + + void ResetCompilationState() { + being_analyzed = false; + been_analyzed = false; + } + + bool being_analyzed : 1; + bool been_analyzed : 1; + + // These bits are set of this node has to know what the preceding + // character was. + bool follows_word_interest : 1; + bool follows_newline_interest : 1; + bool follows_start_interest : 1; + + bool at_end : 1; + bool visited : 1; + bool replacement_calculated : 1; +}; + +class RegExpNode : public ZoneObject { + public: + explicit RegExpNode(Zone* zone) + : replacement_(nullptr), + on_work_list_(false), + trace_count_(0), + zone_(zone) { + bm_info_[0] = bm_info_[1] = nullptr; + } + virtual ~RegExpNode(); + virtual void Accept(NodeVisitor* visitor) = 0; + // Generates a goto to this node or actually generates the code at this point. + virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0; + // How many characters must this node consume at a minimum in order to + // succeed. If we have found at least 'still_to_find' characters that + // must be consumed there is no need to ask any following nodes whether + // they are sure to eat any more characters. The not_at_start argument is + // used to indicate that we know we are not at the start of the input. In + // this case anchored branches will always fail and can be ignored when + // determining how many characters are consumed on success. + virtual int EatsAtLeast(int still_to_find, int budget, bool not_at_start) = 0; + // Emits some quick code that checks whether the preloaded characters match. + // Falls through on certain failure, jumps to the label on possible success. + // If the node cannot make a quick check it does nothing and returns false. + bool EmitQuickCheck(RegExpCompiler* compiler, Trace* bounds_check_trace, + Trace* trace, bool preload_has_checked_bounds, + Label* on_possible_success, + QuickCheckDetails* details_return, + bool fall_through_on_failure); + // For a given number of characters this returns a mask and a value. The + // next n characters are anded with the mask and compared with the value. + // A comparison failure indicates the node cannot match the next n characters. + // A comparison success indicates the node may match. + virtual void GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, + int characters_filled_in, + bool not_at_start) = 0; + static const int kNodeIsTooComplexForGreedyLoops = kMinInt; + virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; } + // Only returns the successor for a text node of length 1 that matches any + // character and that has no guards on it. + virtual RegExpNode* GetSuccessorOfOmnivorousTextNode( + RegExpCompiler* compiler) { + return nullptr; + } + + // Collects information on the possible code units (mod 128) that can match if + // we look forward. This is used for a Boyer-Moore-like string searching + // implementation. TODO(erikcorry): This should share more code with + // EatsAtLeast, GetQuickCheckDetails. The budget argument is used to limit + // the number of nodes we are willing to look at in order to create this data. + static const int kRecursionBudget = 200; + bool KeepRecursing(RegExpCompiler* compiler); + virtual void FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) { + UNREACHABLE(); + } + + // If we know that the input is one-byte then there are some nodes that can + // never match. This method returns a node that can be substituted for + // itself, or nullptr if the node can never match. + virtual RegExpNode* FilterOneByte(int depth) { return this; } + // Helper for FilterOneByte. + RegExpNode* replacement() { + DCHECK(info()->replacement_calculated); + return replacement_; + } + RegExpNode* set_replacement(RegExpNode* replacement) { + info()->replacement_calculated = true; + replacement_ = replacement; + return replacement; // For convenience. + } + + // We want to avoid recalculating the lookahead info, so we store it on the + // node. Only info that is for this node is stored. We can tell that the + // info is for this node when offset == 0, so the information is calculated + // relative to this node. + void SaveBMInfo(BoyerMooreLookahead* bm, bool not_at_start, int offset) { + if (offset == 0) set_bm_info(not_at_start, bm); + } + + Label* label() { return &label_; } + // If non-generic code is generated for a node (i.e. the node is not at the + // start of the trace) then it cannot be reused. This variable sets a limit + // on how often we allow that to happen before we insist on starting a new + // trace and generating generic code for a node that can be reused by flushing + // the deferred actions in the current trace and generating a goto. + static const int kMaxCopiesCodeGenerated = 10; + + bool on_work_list() { return on_work_list_; } + void set_on_work_list(bool value) { on_work_list_ = value; } + + NodeInfo* info() { return &info_; } + + BoyerMooreLookahead* bm_info(bool not_at_start) { + return bm_info_[not_at_start ? 1 : 0]; + } + + Zone* zone() const { return zone_; } + + protected: + enum LimitResult { DONE, CONTINUE }; + RegExpNode* replacement_; + + LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace); + + void set_bm_info(bool not_at_start, BoyerMooreLookahead* bm) { + bm_info_[not_at_start ? 1 : 0] = bm; + } + + private: + static const int kFirstCharBudget = 10; + Label label_; + bool on_work_list_; + NodeInfo info_; + // This variable keeps track of how many times code has been generated for + // this node (in different traces). We don't keep track of where the + // generated code is located unless the code is generated at the start of + // a trace, in which case it is generic and can be reused by flushing the + // deferred operations in the current trace and generating a goto. + int trace_count_; + BoyerMooreLookahead* bm_info_[2]; + + Zone* zone_; +}; + +class SeqRegExpNode : public RegExpNode { + public: + explicit SeqRegExpNode(RegExpNode* on_success) + : RegExpNode(on_success->zone()), on_success_(on_success) {} + RegExpNode* on_success() { return on_success_; } + void set_on_success(RegExpNode* node) { on_success_ = node; } + RegExpNode* FilterOneByte(int depth) override; + void FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) override { + on_success_->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start); + if (offset == 0) set_bm_info(not_at_start, bm); + } + + protected: + RegExpNode* FilterSuccessor(int depth); + + private: + RegExpNode* on_success_; +}; + +class ActionNode : public SeqRegExpNode { + public: + enum ActionType { + SET_REGISTER, + INCREMENT_REGISTER, + STORE_POSITION, + BEGIN_SUBMATCH, + POSITIVE_SUBMATCH_SUCCESS, + EMPTY_MATCH_CHECK, + CLEAR_CAPTURES + }; + static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success); + static ActionNode* IncrementRegister(int reg, RegExpNode* on_success); + static ActionNode* StorePosition(int reg, bool is_capture, + RegExpNode* on_success); + static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success); + static ActionNode* BeginSubmatch(int stack_pointer_reg, int position_reg, + RegExpNode* on_success); + static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg, + int restore_reg, + int clear_capture_count, + int clear_capture_from, + RegExpNode* on_success); + static ActionNode* EmptyMatchCheck(int start_register, + int repetition_register, + int repetition_limit, + RegExpNode* on_success); + void Accept(NodeVisitor* visitor) override; + void Emit(RegExpCompiler* compiler, Trace* trace) override; + int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; + void GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, int filled_in, + bool not_at_start) override { + return on_success()->GetQuickCheckDetails(details, compiler, filled_in, + not_at_start); + } + void FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) override; + ActionType action_type() { return action_type_; } + // TODO(erikcorry): We should allow some action nodes in greedy loops. + int GreedyLoopTextLength() override { + return kNodeIsTooComplexForGreedyLoops; + } + + private: + union { + struct { + int reg; + int value; + } u_store_register; + struct { + int reg; + } u_increment_register; + struct { + int reg; + bool is_capture; + } u_position_register; + struct { + int stack_pointer_register; + int current_position_register; + int clear_register_count; + int clear_register_from; + } u_submatch; + struct { + int start_register; + int repetition_register; + int repetition_limit; + } u_empty_match_check; + struct { + int range_from; + int range_to; + } u_clear_captures; + } data_; + ActionNode(ActionType action_type, RegExpNode* on_success) + : SeqRegExpNode(on_success), action_type_(action_type) {} + ActionType action_type_; + friend class DotPrinterImpl; +}; + +class TextNode : public SeqRegExpNode { + public: + TextNode(ZoneList* elms, bool read_backward, + RegExpNode* on_success) + : SeqRegExpNode(on_success), elms_(elms), read_backward_(read_backward) {} + TextNode(RegExpCharacterClass* that, bool read_backward, + RegExpNode* on_success) + : SeqRegExpNode(on_success), + elms_(new (zone()) ZoneList(1, zone())), + read_backward_(read_backward) { + elms_->Add(TextElement::CharClass(that), zone()); + } + // Create TextNode for a single character class for the given ranges. + static TextNode* CreateForCharacterRanges(Zone* zone, + ZoneList* ranges, + bool read_backward, + RegExpNode* on_success, + JSRegExp::Flags flags); + // Create TextNode for a surrogate pair with a range given for the + // lead and the trail surrogate each. + static TextNode* CreateForSurrogatePair(Zone* zone, CharacterRange lead, + CharacterRange trail, + bool read_backward, + RegExpNode* on_success, + JSRegExp::Flags flags); + void Accept(NodeVisitor* visitor) override; + void Emit(RegExpCompiler* compiler, Trace* trace) override; + int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; + void GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, int characters_filled_in, + bool not_at_start) override; + ZoneList* elements() { return elms_; } + bool read_backward() { return read_backward_; } + void MakeCaseIndependent(Isolate* isolate, bool is_one_byte); + int GreedyLoopTextLength() override; + RegExpNode* GetSuccessorOfOmnivorousTextNode( + RegExpCompiler* compiler) override; + void FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) override; + void CalculateOffsets(); + RegExpNode* FilterOneByte(int depth) override; + + private: + enum TextEmitPassType { + NON_LATIN1_MATCH, // Check for characters that can't match. + SIMPLE_CHARACTER_MATCH, // Case-dependent single character check. + NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs. + CASE_CHARACTER_MATCH, // Case-independent single character check. + CHARACTER_CLASS_MATCH // Character class. + }; + static bool SkipPass(TextEmitPassType pass, bool ignore_case); + static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH; + static const int kLastPass = CHARACTER_CLASS_MATCH; + void TextEmitPass(RegExpCompiler* compiler, TextEmitPassType pass, + bool preloaded, Trace* trace, bool first_element_checked, + int* checked_up_to); + int Length(); + ZoneList* elms_; + bool read_backward_; +}; + +class AssertionNode : public SeqRegExpNode { + public: + enum AssertionType { + AT_END, + AT_START, + AT_BOUNDARY, + AT_NON_BOUNDARY, + AFTER_NEWLINE + }; + static AssertionNode* AtEnd(RegExpNode* on_success) { + return new (on_success->zone()) AssertionNode(AT_END, on_success); + } + static AssertionNode* AtStart(RegExpNode* on_success) { + return new (on_success->zone()) AssertionNode(AT_START, on_success); + } + static AssertionNode* AtBoundary(RegExpNode* on_success) { + return new (on_success->zone()) AssertionNode(AT_BOUNDARY, on_success); + } + static AssertionNode* AtNonBoundary(RegExpNode* on_success) { + return new (on_success->zone()) AssertionNode(AT_NON_BOUNDARY, on_success); + } + static AssertionNode* AfterNewline(RegExpNode* on_success) { + return new (on_success->zone()) AssertionNode(AFTER_NEWLINE, on_success); + } + void Accept(NodeVisitor* visitor) override; + void Emit(RegExpCompiler* compiler, Trace* trace) override; + int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; + void GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, int filled_in, + bool not_at_start) override; + void FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) override; + AssertionType assertion_type() { return assertion_type_; } + + private: + void EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace); + enum IfPrevious { kIsNonWord, kIsWord }; + void BacktrackIfPrevious(RegExpCompiler* compiler, Trace* trace, + IfPrevious backtrack_if_previous); + AssertionNode(AssertionType t, RegExpNode* on_success) + : SeqRegExpNode(on_success), assertion_type_(t) {} + AssertionType assertion_type_; +}; + +class BackReferenceNode : public SeqRegExpNode { + public: + BackReferenceNode(int start_reg, int end_reg, JSRegExp::Flags flags, + bool read_backward, RegExpNode* on_success) + : SeqRegExpNode(on_success), + start_reg_(start_reg), + end_reg_(end_reg), + flags_(flags), + read_backward_(read_backward) {} + void Accept(NodeVisitor* visitor) override; + int start_register() { return start_reg_; } + int end_register() { return end_reg_; } + bool read_backward() { return read_backward_; } + void Emit(RegExpCompiler* compiler, Trace* trace) override; + int EatsAtLeast(int still_to_find, int recursion_depth, + bool not_at_start) override; + void GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, int characters_filled_in, + bool not_at_start) override { + return; + } + void FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) override; + + private: + int start_reg_; + int end_reg_; + JSRegExp::Flags flags_; + bool read_backward_; +}; + +class EndNode : public RegExpNode { + public: + enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS }; + EndNode(Action action, Zone* zone) : RegExpNode(zone), action_(action) {} + void Accept(NodeVisitor* visitor) override; + void Emit(RegExpCompiler* compiler, Trace* trace) override; + int EatsAtLeast(int still_to_find, int recursion_depth, + bool not_at_start) override { + return 0; + } + void GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, int characters_filled_in, + bool not_at_start) override { + // Returning 0 from EatsAtLeast should ensure we never get here. + UNREACHABLE(); + } + void FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) override { + // Returning 0 from EatsAtLeast should ensure we never get here. + UNREACHABLE(); + } + + private: + Action action_; +}; + +class NegativeSubmatchSuccess : public EndNode { + public: + NegativeSubmatchSuccess(int stack_pointer_reg, int position_reg, + int clear_capture_count, int clear_capture_start, + Zone* zone) + : EndNode(NEGATIVE_SUBMATCH_SUCCESS, zone), + stack_pointer_register_(stack_pointer_reg), + current_position_register_(position_reg), + clear_capture_count_(clear_capture_count), + clear_capture_start_(clear_capture_start) {} + void Emit(RegExpCompiler* compiler, Trace* trace) override; + + private: + int stack_pointer_register_; + int current_position_register_; + int clear_capture_count_; + int clear_capture_start_; +}; + +class Guard : public ZoneObject { + public: + enum Relation { LT, GEQ }; + Guard(int reg, Relation op, int value) : reg_(reg), op_(op), value_(value) {} + int reg() { return reg_; } + Relation op() { return op_; } + int value() { return value_; } + + private: + int reg_; + Relation op_; + int value_; +}; + +class GuardedAlternative { + public: + explicit GuardedAlternative(RegExpNode* node) + : node_(node), guards_(nullptr) {} + void AddGuard(Guard* guard, Zone* zone); + RegExpNode* node() { return node_; } + void set_node(RegExpNode* node) { node_ = node; } + ZoneList* guards() { return guards_; } + + private: + RegExpNode* node_; + ZoneList* guards_; +}; + +class AlternativeGeneration; + +class ChoiceNode : public RegExpNode { + public: + explicit ChoiceNode(int expected_size, Zone* zone) + : RegExpNode(zone), + alternatives_(new (zone) + ZoneList(expected_size, zone)), + not_at_start_(false), + being_calculated_(false) {} + void Accept(NodeVisitor* visitor) override; + void AddAlternative(GuardedAlternative node) { + alternatives()->Add(node, zone()); + } + ZoneList* alternatives() { return alternatives_; } + void Emit(RegExpCompiler* compiler, Trace* trace) override; + int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; + int EatsAtLeastHelper(int still_to_find, int budget, + RegExpNode* ignore_this_node, bool not_at_start); + void GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, int characters_filled_in, + bool not_at_start) override; + void FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) override; + + bool being_calculated() { return being_calculated_; } + bool not_at_start() { return not_at_start_; } + void set_not_at_start() { not_at_start_ = true; } + void set_being_calculated(bool b) { being_calculated_ = b; } + virtual bool try_to_emit_quick_check_for_alternative(bool is_first) { + return true; + } + RegExpNode* FilterOneByte(int depth) override; + virtual bool read_backward() { return false; } + + protected: + int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative); + ZoneList* alternatives_; + + private: + friend class Analysis; + + void GenerateGuard(RegExpMacroAssembler* macro_assembler, Guard* guard, + Trace* trace); + int CalculatePreloadCharacters(RegExpCompiler* compiler, int eats_at_least); + void EmitOutOfLineContinuation(RegExpCompiler* compiler, Trace* trace, + GuardedAlternative alternative, + AlternativeGeneration* alt_gen, + int preload_characters, + bool next_expects_preload); + void SetUpPreLoad(RegExpCompiler* compiler, Trace* current_trace, + PreloadState* preloads); + void AssertGuardsMentionRegisters(Trace* trace); + int EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler, Trace* trace); + Trace* EmitGreedyLoop(RegExpCompiler* compiler, Trace* trace, + AlternativeGenerationList* alt_gens, + PreloadState* preloads, + GreedyLoopState* greedy_loop_state, int text_length); + void EmitChoices(RegExpCompiler* compiler, + AlternativeGenerationList* alt_gens, int first_choice, + Trace* trace, PreloadState* preloads); + + // If true, this node is never checked at the start of the input. + // Allows a new trace to start with at_start() set to false. + bool not_at_start_; + bool being_calculated_; +}; + +class NegativeLookaroundChoiceNode : public ChoiceNode { + public: + explicit NegativeLookaroundChoiceNode(GuardedAlternative this_must_fail, + GuardedAlternative then_do_this, + Zone* zone) + : ChoiceNode(2, zone) { + AddAlternative(this_must_fail); + AddAlternative(then_do_this); + } + int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; + void GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, int characters_filled_in, + bool not_at_start) override; + void FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) override { + alternatives_->at(1).node()->FillInBMInfo(isolate, offset, budget - 1, bm, + not_at_start); + if (offset == 0) set_bm_info(not_at_start, bm); + } + // For a negative lookahead we don't emit the quick check for the + // alternative that is expected to fail. This is because quick check code + // starts by loading enough characters for the alternative that takes fewest + // characters, but on a negative lookahead the negative branch did not take + // part in that calculation (EatsAtLeast) so the assumptions don't hold. + bool try_to_emit_quick_check_for_alternative(bool is_first) override { + return !is_first; + } + RegExpNode* FilterOneByte(int depth) override; +}; + +class LoopChoiceNode : public ChoiceNode { + public: + LoopChoiceNode(bool body_can_be_zero_length, bool read_backward, Zone* zone) + : ChoiceNode(2, zone), + loop_node_(nullptr), + continue_node_(nullptr), + body_can_be_zero_length_(body_can_be_zero_length), + read_backward_(read_backward) {} + void AddLoopAlternative(GuardedAlternative alt); + void AddContinueAlternative(GuardedAlternative alt); + void Emit(RegExpCompiler* compiler, Trace* trace) override; + int EatsAtLeast(int still_to_find, int budget, bool not_at_start) override; + void GetQuickCheckDetails(QuickCheckDetails* details, + RegExpCompiler* compiler, int characters_filled_in, + bool not_at_start) override; + void FillInBMInfo(Isolate* isolate, int offset, int budget, + BoyerMooreLookahead* bm, bool not_at_start) override; + RegExpNode* loop_node() { return loop_node_; } + RegExpNode* continue_node() { return continue_node_; } + bool body_can_be_zero_length() { return body_can_be_zero_length_; } + bool read_backward() override { return read_backward_; } + void Accept(NodeVisitor* visitor) override; + RegExpNode* FilterOneByte(int depth) override; + + private: + // AddAlternative is made private for loop nodes because alternatives + // should not be added freely, we need to keep track of which node + // goes back to the node itself. + void AddAlternative(GuardedAlternative node) { + ChoiceNode::AddAlternative(node); + } + + RegExpNode* loop_node_; + RegExpNode* continue_node_; + bool body_can_be_zero_length_; + bool read_backward_; +}; + +class NodeVisitor { + public: + virtual ~NodeVisitor() = default; +#define DECLARE_VISIT(Type) virtual void Visit##Type(Type##Node* that) = 0; + FOR_EACH_NODE_TYPE(DECLARE_VISIT) +#undef DECLARE_VISIT + virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); } +}; + +} // namespace internal +} // namespace v8 + +#endif // V8_REGEXP_REGEXP_NODES_H_ diff --git a/chromium/v8/src/regexp/regexp-parser.cc b/chromium/v8/src/regexp/regexp-parser.cc index 7cae456f563..3647680969a 100644 --- a/chromium/v8/src/regexp/regexp-parser.cc +++ b/chromium/v8/src/regexp/regexp-parser.cc @@ -9,8 +9,9 @@ #include "src/execution/isolate.h" #include "src/heap/factory.h" #include "src/objects/objects-inl.h" -#include "src/regexp/jsregexp.h" #include "src/regexp/property-sequences.h" +#include "src/regexp/regexp-macro-assembler.h" +#include "src/regexp/regexp.h" #include "src/strings/char-predicates-inl.h" #include "src/utils/ostreams.h" #include "src/utils/utils.h" @@ -879,24 +880,25 @@ bool RegExpParser::CreateNamedCaptureAtIndex(const ZoneVector* name, DCHECK(0 < index && index <= captures_started_); DCHECK_NOT_NULL(name); + RegExpCapture* capture = GetCapture(index); + DCHECK_NULL(capture->name()); + + capture->set_name(name); + if (named_captures_ == nullptr) { - named_captures_ = new (zone()) ZoneList(1, zone()); + named_captures_ = new (zone_->New(sizeof(*named_captures_))) + ZoneSet(zone()); } else { // Check for duplicates and bail if we find any. - // TODO(jgruber): O(n^2). - for (const auto& named_capture : *named_captures_) { - if (*named_capture->name() == *name) { - ReportError(CStrVector("Duplicate capture group name")); - return false; - } + + const auto& named_capture_it = named_captures_->find(capture); + if (named_capture_it != named_captures_->end()) { + ReportError(CStrVector("Duplicate capture group name")); + return false; } } - RegExpCapture* capture = GetCapture(index); - DCHECK_NULL(capture->name()); - - capture->set_name(name); - named_captures_->Add(capture, zone()); + named_captures_->emplace(capture); return true; } @@ -943,20 +945,22 @@ void RegExpParser::PatchNamedBackReferences() { } // Look up and patch the actual capture for each named back reference. - // TODO(jgruber): O(n^2), optimize if necessary. for (int i = 0; i < named_back_references_->length(); i++) { RegExpBackReference* ref = named_back_references_->at(i); - int index = -1; - for (const auto& capture : *named_captures_) { - if (*capture->name() == *ref->name()) { - index = capture->index(); - break; - } - } + // Capture used to search the named_captures_ by name, index of the + // capture is never used. + static const int kInvalidIndex = 0; + RegExpCapture* search_capture = new (zone()) RegExpCapture(kInvalidIndex); + DCHECK_NULL(search_capture->name()); + search_capture->set_name(ref->name()); - if (index == -1) { + int index = -1; + const auto& capture_it = named_captures_->find(search_capture); + if (capture_it != named_captures_->end()) { + index = (*capture_it)->index(); + } else { ReportError(CStrVector("Invalid named capture referenced")); return; } @@ -981,16 +985,17 @@ RegExpCapture* RegExpParser::GetCapture(int index) { } Handle RegExpParser::CreateCaptureNameMap() { - if (named_captures_ == nullptr || named_captures_->is_empty()) + if (named_captures_ == nullptr || named_captures_->empty()) { return Handle(); + } Factory* factory = isolate()->factory(); - int len = named_captures_->length() * 2; + int len = static_cast(named_captures_->size()) * 2; Handle array = factory->NewFixedArray(len); - for (int i = 0; i < named_captures_->length(); i++) { - RegExpCapture* capture = named_captures_->at(i); + int i = 0; + for (const auto& capture : *named_captures_) { Vector capture_name(capture->name()->data(), capture->name()->size()); // CSA code in ConstructNewResultFromMatchInfo requires these strings to be @@ -998,7 +1003,10 @@ Handle RegExpParser::CreateCaptureNameMap() { Handle name = factory->InternalizeString(capture_name); array->set(i * 2, *name); array->set(i * 2 + 1, Smi::FromInt(capture->index())); + + i++; } + DCHECK_EQ(i * 2, len); return array; } @@ -1963,12 +1971,6 @@ void RegExpBuilder::AddTerm(RegExpTree* term) { void RegExpBuilder::AddAssertion(RegExpTree* assert) { FlushText(); - if (terms_.length() > 0 && terms_.last()->IsAssertion()) { - // Omit repeated assertions of the same type. - RegExpAssertion* last = terms_.last()->AsAssertion(); - RegExpAssertion* next = assert->AsAssertion(); - if (last->assertion_type() == next->assertion_type()) return; - } terms_.Add(assert, zone()); LAST(ADD_ASSERT); } diff --git a/chromium/v8/src/regexp/regexp-parser.h b/chromium/v8/src/regexp/regexp-parser.h index bf9e62ed71f..36cec7e984e 100644 --- a/chromium/v8/src/regexp/regexp-parser.h +++ b/chromium/v8/src/regexp/regexp-parser.h @@ -326,11 +326,19 @@ class V8_EXPORT_PRIVATE RegExpParser { FlatStringReader* in() { return in_; } void ScanForCaptures(); + struct RegExpCaptureNameLess { + bool operator()(const RegExpCapture* lhs, const RegExpCapture* rhs) const { + DCHECK_NOT_NULL(lhs); + DCHECK_NOT_NULL(rhs); + return *lhs->name() < *rhs->name(); + } + }; + Isolate* isolate_; Zone* zone_; Handle* error_; ZoneList* captures_; - ZoneList* named_captures_; + ZoneSet* named_captures_; ZoneList* named_back_references_; FlatStringReader* in_; uc32 current_; diff --git a/chromium/v8/src/regexp/regexp-utils.cc b/chromium/v8/src/regexp/regexp-utils.cc index 49f9d4476bf..ad50270fdcd 100644 --- a/chromium/v8/src/regexp/regexp-utils.cc +++ b/chromium/v8/src/regexp/regexp-utils.cc @@ -8,7 +8,7 @@ #include "src/heap/factory.h" #include "src/objects/js-regexp-inl.h" #include "src/objects/objects-inl.h" -#include "src/regexp/jsregexp.h" +#include "src/regexp/regexp.h" namespace v8 { namespace internal { @@ -179,7 +179,9 @@ bool RegExpUtils::IsUnmodifiedRegExp(Isolate* isolate, Handle obj) { return false; } - if (!isolate->IsRegExpSpeciesLookupChainIntact()) return false; + if (!isolate->IsRegExpSpeciesLookupChainIntact(isolate->native_context())) { + return false; + } // The smi check is required to omit ToLength(lastIndex) calls with possible // user-code execution on the fast path. diff --git a/chromium/v8/src/regexp/regexp.cc b/chromium/v8/src/regexp/regexp.cc new file mode 100644 index 00000000000..15b0321c464 --- /dev/null +++ b/chromium/v8/src/regexp/regexp.cc @@ -0,0 +1,1018 @@ +// Copyright 2012 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "src/regexp/regexp.h" + +#include "src/codegen/compilation-cache.h" +#include "src/heap/heap-inl.h" +#include "src/objects/js-regexp-inl.h" +#include "src/regexp/regexp-bytecode-generator.h" +#include "src/regexp/regexp-compiler.h" +#include "src/regexp/regexp-dotprinter.h" +#include "src/regexp/regexp-interpreter.h" +#include "src/regexp/regexp-macro-assembler-arch.h" +#include "src/regexp/regexp-parser.h" +#include "src/strings/string-search.h" + +namespace v8 { +namespace internal { + +using namespace regexp_compiler_constants; // NOLINT(build/namespaces) + +class RegExpImpl final : public AllStatic { + public: + // Returns a string representation of a regular expression. + // Implements RegExp.prototype.toString, see ECMA-262 section 15.10.6.4. + // This function calls the garbage collector if necessary. + static Handle ToString(Handle value); + + // Prepares a JSRegExp object with Irregexp-specific data. + static void IrregexpInitialize(Isolate* isolate, Handle re, + Handle pattern, JSRegExp::Flags flags, + int capture_register_count); + + static void AtomCompile(Isolate* isolate, Handle re, + Handle pattern, JSRegExp::Flags flags, + Handle match_pattern); + + static int AtomExecRaw(Isolate* isolate, Handle regexp, + Handle subject, int index, int32_t* output, + int output_size); + + static Handle AtomExec(Isolate* isolate, Handle regexp, + Handle subject, int index, + Handle last_match_info); + + // Execute a regular expression on the subject, starting from index. + // If matching succeeds, return the number of matches. This can be larger + // than one in the case of global regular expressions. + // The captures and subcaptures are stored into the registers vector. + // If matching fails, returns RE_FAILURE. + // If execution fails, sets a pending exception and returns RE_EXCEPTION. + static int IrregexpExecRaw(Isolate* isolate, Handle regexp, + Handle subject, int index, int32_t* output, + int output_size); + + // Execute an Irregexp bytecode pattern. + // On a successful match, the result is a JSArray containing + // captured positions. On a failure, the result is the null value. + // Returns an empty handle in case of an exception. + V8_WARN_UNUSED_RESULT static MaybeHandle IrregexpExec( + Isolate* isolate, Handle regexp, Handle subject, + int index, Handle last_match_info); + + static bool CompileIrregexp(Isolate* isolate, Handle re, + Handle sample_subject, bool is_one_byte); + static inline bool EnsureCompiledIrregexp(Isolate* isolate, + Handle re, + Handle sample_subject, + bool is_one_byte); + + // Returns true on success, false on failure. + static bool Compile(Isolate* isolate, Zone* zone, RegExpCompileData* input, + JSRegExp::Flags flags, Handle pattern, + Handle sample_subject, bool is_one_byte); + + // For acting on the JSRegExp data FixedArray. + static int IrregexpMaxRegisterCount(FixedArray re); + static void SetIrregexpMaxRegisterCount(FixedArray re, int value); + static void SetIrregexpCaptureNameMap(FixedArray re, + Handle value); + static int IrregexpNumberOfCaptures(FixedArray re); + static int IrregexpNumberOfRegisters(FixedArray re); + static ByteArray IrregexpByteCode(FixedArray re, bool is_one_byte); + static Code IrregexpNativeCode(FixedArray re, bool is_one_byte); +}; + +V8_WARN_UNUSED_RESULT +static inline MaybeHandle ThrowRegExpException( + Isolate* isolate, Handle re, Handle pattern, + Handle error_text) { + THROW_NEW_ERROR( + isolate, + NewSyntaxError(MessageTemplate::kMalformedRegExp, pattern, error_text), + Object); +} + +inline void ThrowRegExpException(Isolate* isolate, Handle re, + Handle error_text) { + USE(ThrowRegExpException(isolate, re, Handle(re->Pattern(), isolate), + error_text)); +} + +// Identifies the sort of regexps where the regexp engine is faster +// than the code used for atom matches. +static bool HasFewDifferentCharacters(Handle pattern) { + int length = Min(kMaxLookaheadForBoyerMoore, pattern->length()); + if (length <= kPatternTooShortForBoyerMoore) return false; + const int kMod = 128; + bool character_found[kMod]; + int different = 0; + memset(&character_found[0], 0, sizeof(character_found)); + for (int i = 0; i < length; i++) { + int ch = (pattern->Get(i) & (kMod - 1)); + if (!character_found[ch]) { + character_found[ch] = true; + different++; + // We declare a regexp low-alphabet if it has at least 3 times as many + // characters as it has different characters. + if (different * 3 > length) return false; + } + } + return true; +} + +// Generic RegExp methods. Dispatches to implementation specific methods. + +// static +MaybeHandle RegExp::Compile(Isolate* isolate, Handle re, + Handle pattern, + JSRegExp::Flags flags) { + DCHECK(pattern->IsFlat()); + + Zone zone(isolate->allocator(), ZONE_NAME); + CompilationCache* compilation_cache = isolate->compilation_cache(); + MaybeHandle maybe_cached = + compilation_cache->LookupRegExp(pattern, flags); + Handle cached; + if (maybe_cached.ToHandle(&cached)) { + re->set_data(*cached); + return re; + } + + PostponeInterruptsScope postpone(isolate); + RegExpCompileData parse_result; + FlatStringReader reader(isolate, pattern); + DCHECK(!isolate->has_pending_exception()); + if (!RegExpParser::ParseRegExp(isolate, &zone, &reader, flags, + &parse_result)) { + // Throw an exception if we fail to parse the pattern. + return ThrowRegExpException(isolate, re, pattern, parse_result.error); + } + + bool has_been_compiled = false; + + if (parse_result.simple && !IgnoreCase(flags) && !IsSticky(flags) && + !HasFewDifferentCharacters(pattern)) { + // Parse-tree is a single atom that is equal to the pattern. + RegExpImpl::AtomCompile(isolate, re, pattern, flags, pattern); + has_been_compiled = true; + } else if (parse_result.tree->IsAtom() && !IsSticky(flags) && + parse_result.capture_count == 0) { + RegExpAtom* atom = parse_result.tree->AsAtom(); + Vector atom_pattern = atom->data(); + Handle atom_string; + ASSIGN_RETURN_ON_EXCEPTION( + isolate, atom_string, + isolate->factory()->NewStringFromTwoByte(atom_pattern), Object); + if (!IgnoreCase(atom->flags()) && !HasFewDifferentCharacters(atom_string)) { + RegExpImpl::AtomCompile(isolate, re, pattern, flags, atom_string); + has_been_compiled = true; + } + } + if (!has_been_compiled) { + RegExpImpl::IrregexpInitialize(isolate, re, pattern, flags, + parse_result.capture_count); + } + DCHECK(re->data().IsFixedArray()); + // Compilation succeeded so the data is set on the regexp + // and we can store it in the cache. + Handle data(FixedArray::cast(re->data()), isolate); + compilation_cache->PutRegExp(pattern, flags, data); + + return re; +} + +// static +MaybeHandle RegExp::Exec(Isolate* isolate, Handle regexp, + Handle subject, int index, + Handle last_match_info) { + switch (regexp->TypeTag()) { + case JSRegExp::ATOM: + return RegExpImpl::AtomExec(isolate, regexp, subject, index, + last_match_info); + case JSRegExp::IRREGEXP: { + return RegExpImpl::IrregexpExec(isolate, regexp, subject, index, + last_match_info); + } + default: + UNREACHABLE(); + } +} + +// RegExp Atom implementation: Simple string search using indexOf. + +void RegExpImpl::AtomCompile(Isolate* isolate, Handle re, + Handle pattern, JSRegExp::Flags flags, + Handle match_pattern) { + isolate->factory()->SetRegExpAtomData(re, JSRegExp::ATOM, pattern, flags, + match_pattern); +} + +static void SetAtomLastCapture(Isolate* isolate, + Handle last_match_info, + String subject, int from, int to) { + SealHandleScope shs(isolate); + last_match_info->SetNumberOfCaptureRegisters(2); + last_match_info->SetLastSubject(subject); + last_match_info->SetLastInput(subject); + last_match_info->SetCapture(0, from); + last_match_info->SetCapture(1, to); +} + +int RegExpImpl::AtomExecRaw(Isolate* isolate, Handle regexp, + Handle subject, int index, int32_t* output, + int output_size) { + DCHECK_LE(0, index); + DCHECK_LE(index, subject->length()); + + subject = String::Flatten(isolate, subject); + DisallowHeapAllocation no_gc; // ensure vectors stay valid + + String needle = String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex)); + int needle_len = needle.length(); + DCHECK(needle.IsFlat()); + DCHECK_LT(0, needle_len); + + if (index + needle_len > subject->length()) { + return RegExp::RE_FAILURE; + } + + for (int i = 0; i < output_size; i += 2) { + String::FlatContent needle_content = needle.GetFlatContent(no_gc); + String::FlatContent subject_content = subject->GetFlatContent(no_gc); + DCHECK(needle_content.IsFlat()); + DCHECK(subject_content.IsFlat()); + // dispatch on type of strings + index = + (needle_content.IsOneByte() + ? (subject_content.IsOneByte() + ? SearchString(isolate, subject_content.ToOneByteVector(), + needle_content.ToOneByteVector(), index) + : SearchString(isolate, subject_content.ToUC16Vector(), + needle_content.ToOneByteVector(), index)) + : (subject_content.IsOneByte() + ? SearchString(isolate, subject_content.ToOneByteVector(), + needle_content.ToUC16Vector(), index) + : SearchString(isolate, subject_content.ToUC16Vector(), + needle_content.ToUC16Vector(), index))); + if (index == -1) { + return i / 2; // Return number of matches. + } else { + output[i] = index; + output[i + 1] = index + needle_len; + index += needle_len; + } + } + return output_size / 2; +} + +Handle RegExpImpl::AtomExec(Isolate* isolate, Handle re, + Handle subject, int index, + Handle last_match_info) { + static const int kNumRegisters = 2; + STATIC_ASSERT(kNumRegisters <= Isolate::kJSRegexpStaticOffsetsVectorSize); + int32_t* output_registers = isolate->jsregexp_static_offsets_vector(); + + int res = + AtomExecRaw(isolate, re, subject, index, output_registers, kNumRegisters); + + if (res == RegExp::RE_FAILURE) return isolate->factory()->null_value(); + + DCHECK_EQ(res, RegExp::RE_SUCCESS); + SealHandleScope shs(isolate); + SetAtomLastCapture(isolate, last_match_info, *subject, output_registers[0], + output_registers[1]); + return last_match_info; +} + +// Irregexp implementation. + +// Ensures that the regexp object contains a compiled version of the +// source for either one-byte or two-byte subject strings. +// If the compiled version doesn't already exist, it is compiled +// from the source pattern. +// If compilation fails, an exception is thrown and this function +// returns false. +bool RegExpImpl::EnsureCompiledIrregexp(Isolate* isolate, Handle re, + Handle sample_subject, + bool is_one_byte) { + Object compiled_code = re->DataAt(JSRegExp::code_index(is_one_byte)); + if (compiled_code != Smi::FromInt(JSRegExp::kUninitializedValue)) { + DCHECK(FLAG_regexp_interpret_all ? compiled_code.IsByteArray() + : compiled_code.IsCode()); + return true; + } + return CompileIrregexp(isolate, re, sample_subject, is_one_byte); +} + +bool RegExpImpl::CompileIrregexp(Isolate* isolate, Handle re, + Handle sample_subject, + bool is_one_byte) { + // Compile the RegExp. + Zone zone(isolate->allocator(), ZONE_NAME); + PostponeInterruptsScope postpone(isolate); +#ifdef DEBUG + Object entry = re->DataAt(JSRegExp::code_index(is_one_byte)); + // When arriving here entry can only be a smi representing an uncompiled + // regexp. + DCHECK(entry.IsSmi()); + int entry_value = Smi::ToInt(entry); + DCHECK_EQ(JSRegExp::kUninitializedValue, entry_value); +#endif + + JSRegExp::Flags flags = re->GetFlags(); + + Handle pattern(re->Pattern(), isolate); + pattern = String::Flatten(isolate, pattern); + RegExpCompileData compile_data; + FlatStringReader reader(isolate, pattern); + if (!RegExpParser::ParseRegExp(isolate, &zone, &reader, flags, + &compile_data)) { + // Throw an exception if we fail to parse the pattern. + // THIS SHOULD NOT HAPPEN. We already pre-parsed it successfully once. + USE(ThrowRegExpException(isolate, re, pattern, compile_data.error)); + return false; + } + const bool compilation_succeeded = + Compile(isolate, &zone, &compile_data, flags, pattern, sample_subject, + is_one_byte); + if (!compilation_succeeded) { + DCHECK(!compile_data.error.is_null()); + ThrowRegExpException(isolate, re, compile_data.error); + return false; + } + + Handle data = + Handle(FixedArray::cast(re->data()), isolate); + data->set(JSRegExp::code_index(is_one_byte), compile_data.code); + SetIrregexpCaptureNameMap(*data, compile_data.capture_name_map); + int register_max = IrregexpMaxRegisterCount(*data); + if (compile_data.register_count > register_max) { + SetIrregexpMaxRegisterCount(*data, compile_data.register_count); + } + + return true; +} + +int RegExpImpl::IrregexpMaxRegisterCount(FixedArray re) { + return Smi::cast(re.get(JSRegExp::kIrregexpMaxRegisterCountIndex)).value(); +} + +void RegExpImpl::SetIrregexpMaxRegisterCount(FixedArray re, int value) { + re.set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(value)); +} + +void RegExpImpl::SetIrregexpCaptureNameMap(FixedArray re, + Handle value) { + if (value.is_null()) { + re.set(JSRegExp::kIrregexpCaptureNameMapIndex, Smi::kZero); + } else { + re.set(JSRegExp::kIrregexpCaptureNameMapIndex, *value); + } +} + +int RegExpImpl::IrregexpNumberOfCaptures(FixedArray re) { + return Smi::ToInt(re.get(JSRegExp::kIrregexpCaptureCountIndex)); +} + +int RegExpImpl::IrregexpNumberOfRegisters(FixedArray re) { + return Smi::ToInt(re.get(JSRegExp::kIrregexpMaxRegisterCountIndex)); +} + +ByteArray RegExpImpl::IrregexpByteCode(FixedArray re, bool is_one_byte) { + return ByteArray::cast(re.get(JSRegExp::code_index(is_one_byte))); +} + +Code RegExpImpl::IrregexpNativeCode(FixedArray re, bool is_one_byte) { + return Code::cast(re.get(JSRegExp::code_index(is_one_byte))); +} + +void RegExpImpl::IrregexpInitialize(Isolate* isolate, Handle re, + Handle pattern, + JSRegExp::Flags flags, int capture_count) { + // Initialize compiled code entries to null. + isolate->factory()->SetRegExpIrregexpData(re, JSRegExp::IRREGEXP, pattern, + flags, capture_count); +} + +// static +int RegExp::IrregexpPrepare(Isolate* isolate, Handle regexp, + Handle subject) { + DCHECK(subject->IsFlat()); + + // Check representation of the underlying storage. + bool is_one_byte = String::IsOneByteRepresentationUnderneath(*subject); + if (!RegExpImpl::EnsureCompiledIrregexp(isolate, regexp, subject, + is_one_byte)) { + return -1; + } + + DisallowHeapAllocation no_gc; + FixedArray data = FixedArray::cast(regexp->data()); + if (FLAG_regexp_interpret_all) { + // Byte-code regexp needs space allocated for all its registers. + // The result captures are copied to the start of the registers array + // if the match succeeds. This way those registers are not clobbered + // when we set the last match info from last successful match. + return RegExpImpl::IrregexpNumberOfRegisters(data) + + (RegExpImpl::IrregexpNumberOfCaptures(data) + 1) * 2; + } else { + // Native regexp only needs room to output captures. Registers are handled + // internally. + return (RegExpImpl::IrregexpNumberOfCaptures(data) + 1) * 2; + } +} + +int RegExpImpl::IrregexpExecRaw(Isolate* isolate, Handle regexp, + Handle subject, int index, + int32_t* output, int output_size) { + Handle irregexp(FixedArray::cast(regexp->data()), isolate); + + DCHECK_LE(0, index); + DCHECK_LE(index, subject->length()); + DCHECK(subject->IsFlat()); + + bool is_one_byte = String::IsOneByteRepresentationUnderneath(*subject); + + if (!FLAG_regexp_interpret_all) { + DCHECK(output_size >= (IrregexpNumberOfCaptures(*irregexp) + 1) * 2); + do { + EnsureCompiledIrregexp(isolate, regexp, subject, is_one_byte); + Handle code(IrregexpNativeCode(*irregexp, is_one_byte), isolate); + // The stack is used to allocate registers for the compiled regexp code. + // This means that in case of failure, the output registers array is left + // untouched and contains the capture results from the previous successful + // match. We can use that to set the last match info lazily. + int res = NativeRegExpMacroAssembler::Match(code, subject, output, + output_size, index, isolate); + if (res != NativeRegExpMacroAssembler::RETRY) { + DCHECK(res != NativeRegExpMacroAssembler::EXCEPTION || + isolate->has_pending_exception()); + STATIC_ASSERT(static_cast(NativeRegExpMacroAssembler::SUCCESS) == + RegExp::RE_SUCCESS); + STATIC_ASSERT(static_cast(NativeRegExpMacroAssembler::FAILURE) == + RegExp::RE_FAILURE); + STATIC_ASSERT(static_cast(NativeRegExpMacroAssembler::EXCEPTION) == + RegExp::RE_EXCEPTION); + return res; + } + // If result is RETRY, the string has changed representation, and we + // must restart from scratch. + // In this case, it means we must make sure we are prepared to handle + // the, potentially, different subject (the string can switch between + // being internal and external, and even between being Latin1 and UC16, + // but the characters are always the same). + RegExp::IrregexpPrepare(isolate, regexp, subject); + is_one_byte = String::IsOneByteRepresentationUnderneath(*subject); + } while (true); + UNREACHABLE(); + } else { + DCHECK(FLAG_regexp_interpret_all); + DCHECK(output_size >= IrregexpNumberOfRegisters(*irregexp)); + // We must have done EnsureCompiledIrregexp, so we can get the number of + // registers. + int number_of_capture_registers = + (IrregexpNumberOfCaptures(*irregexp) + 1) * 2; + int32_t* raw_output = &output[number_of_capture_registers]; + + do { + // We do not touch the actual capture result registers until we know there + // has been a match so that we can use those capture results to set the + // last match info. + for (int i = number_of_capture_registers - 1; i >= 0; i--) { + raw_output[i] = -1; + } + Handle byte_codes(IrregexpByteCode(*irregexp, is_one_byte), + isolate); + + IrregexpInterpreter::Result result = IrregexpInterpreter::Match( + isolate, byte_codes, subject, raw_output, index); + DCHECK_IMPLIES(result == IrregexpInterpreter::EXCEPTION, + isolate->has_pending_exception()); + + switch (result) { + case IrregexpInterpreter::SUCCESS: + // Copy capture results to the start of the registers array. + MemCopy(output, raw_output, + number_of_capture_registers * sizeof(int32_t)); + return result; + case IrregexpInterpreter::EXCEPTION: + case IrregexpInterpreter::FAILURE: + return result; + case IrregexpInterpreter::RETRY: + // The string has changed representation, and we must restart the + // match. + is_one_byte = String::IsOneByteRepresentationUnderneath(*subject); + EnsureCompiledIrregexp(isolate, regexp, subject, is_one_byte); + break; + } + } while (true); + UNREACHABLE(); + } +} + +MaybeHandle RegExpImpl::IrregexpExec( + Isolate* isolate, Handle regexp, Handle subject, + int previous_index, Handle last_match_info) { + DCHECK_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); + + subject = String::Flatten(isolate, subject); + + // Prepare space for the return values. +#ifdef DEBUG + if (FLAG_regexp_interpret_all && FLAG_trace_regexp_bytecodes) { + String pattern = regexp->Pattern(); + PrintF("\n\nRegexp match: /%s/\n\n", pattern.ToCString().get()); + PrintF("\n\nSubject string: '%s'\n\n", subject->ToCString().get()); + } +#endif + int required_registers = RegExp::IrregexpPrepare(isolate, regexp, subject); + if (required_registers < 0) { + // Compiling failed with an exception. + DCHECK(isolate->has_pending_exception()); + return MaybeHandle(); + } + + int32_t* output_registers = nullptr; + if (required_registers > Isolate::kJSRegexpStaticOffsetsVectorSize) { + output_registers = NewArray(required_registers); + } + std::unique_ptr auto_release(output_registers); + if (output_registers == nullptr) { + output_registers = isolate->jsregexp_static_offsets_vector(); + } + + int res = + RegExpImpl::IrregexpExecRaw(isolate, regexp, subject, previous_index, + output_registers, required_registers); + if (res == RegExp::RE_SUCCESS) { + int capture_count = + IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())); + return RegExp::SetLastMatchInfo(isolate, last_match_info, subject, + capture_count, output_registers); + } + if (res == RegExp::RE_EXCEPTION) { + DCHECK(isolate->has_pending_exception()); + return MaybeHandle(); + } + DCHECK(res == RegExp::RE_FAILURE); + return isolate->factory()->null_value(); +} + +// static +Handle RegExp::SetLastMatchInfo( + Isolate* isolate, Handle last_match_info, + Handle subject, int capture_count, int32_t* match) { + // This is the only place where match infos can grow. If, after executing the + // regexp, RegExpExecStub finds that the match info is too small, it restarts + // execution in RegExpImpl::Exec, which finally grows the match info right + // here. + + int capture_register_count = (capture_count + 1) * 2; + Handle result = RegExpMatchInfo::ReserveCaptures( + isolate, last_match_info, capture_register_count); + result->SetNumberOfCaptureRegisters(capture_register_count); + + if (*result != *last_match_info) { + if (*last_match_info == *isolate->regexp_last_match_info()) { + // This inner condition is only needed for special situations like the + // regexp fuzzer, where we pass our own custom RegExpMatchInfo to + // RegExpImpl::Exec; there actually want to bypass the Isolate's match + // info and execute the regexp without side effects. + isolate->native_context()->set_regexp_last_match_info(*result); + } + } + + DisallowHeapAllocation no_allocation; + if (match != nullptr) { + for (int i = 0; i < capture_register_count; i += 2) { + result->SetCapture(i, match[i]); + result->SetCapture(i + 1, match[i + 1]); + } + } + result->SetLastSubject(*subject); + result->SetLastInput(*subject); + return result; +} + +// static +void RegExp::DotPrintForTesting(const char* label, RegExpNode* node) { + DotPrinter::DotPrint(label, node); +} + +namespace { + +// Returns true if we've either generated too much irregex code within this +// isolate, or the pattern string is too long. +bool TooMuchRegExpCode(Isolate* isolate, Handle pattern) { + // Limit the space regexps take up on the heap. In order to limit this we + // would like to keep track of the amount of regexp code on the heap. This + // is not tracked, however. As a conservative approximation we track the + // total regexp code compiled including code that has subsequently been freed + // and the total executable memory at any point. + static constexpr size_t kRegExpExecutableMemoryLimit = 16 * MB; + static constexpr size_t kRegExpCompiledLimit = 1 * MB; + + Heap* heap = isolate->heap(); + if (pattern->length() > RegExp::kRegExpTooLargeToOptimize) return true; + return (isolate->total_regexp_code_generated() > kRegExpCompiledLimit && + heap->CommittedMemoryExecutable() > kRegExpExecutableMemoryLimit); +} + +} // namespace + +// static +bool RegExp::CompileForTesting(Isolate* isolate, Zone* zone, + RegExpCompileData* data, JSRegExp::Flags flags, + Handle pattern, + Handle sample_subject, + bool is_one_byte) { + return RegExpImpl::Compile(isolate, zone, data, flags, pattern, + sample_subject, is_one_byte); +} + +bool RegExpImpl::Compile(Isolate* isolate, Zone* zone, RegExpCompileData* data, + JSRegExp::Flags flags, Handle pattern, + Handle sample_subject, bool is_one_byte) { + if ((data->capture_count + 1) * 2 - 1 > RegExpMacroAssembler::kMaxRegister) { + data->error = + isolate->factory()->NewStringFromAsciiChecked("RegExp too big"); + return false; + } + + bool is_sticky = IsSticky(flags); + bool is_global = IsGlobal(flags); + bool is_unicode = IsUnicode(flags); + RegExpCompiler compiler(isolate, zone, data->capture_count, is_one_byte); + + if (compiler.optimize()) { + compiler.set_optimize(!TooMuchRegExpCode(isolate, pattern)); + } + + // Sample some characters from the middle of the string. + static const int kSampleSize = 128; + + sample_subject = String::Flatten(isolate, sample_subject); + int chars_sampled = 0; + int half_way = (sample_subject->length() - kSampleSize) / 2; + for (int i = Max(0, half_way); + i < sample_subject->length() && chars_sampled < kSampleSize; + i++, chars_sampled++) { + compiler.frequency_collator()->CountCharacter(sample_subject->Get(i)); + } + + // 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 = data->tree->IsAnchoredAtStart(); + int max_length = data->tree->max_match(); + if (!is_start_anchored && !is_sticky) { + // Add a .*? at the beginning, outside the body capture, unless + // this expression is anchored at the beginning or sticky. + JSRegExp::Flags default_flags = JSRegExp::Flags(); + RegExpNode* loop_node = RegExpQuantifier::ToNode( + 0, RegExpTree::kInfinity, false, + new (zone) RegExpCharacterClass('*', default_flags), &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 = new (zone) ChoiceNode(2, zone); + first_step_node->AddAlternative(GuardedAlternative(captured_body)); + first_step_node->AddAlternative(GuardedAlternative(new (zone) TextNode( + new (zone) RegExpCharacterClass('*', default_flags), false, + loop_node))); + node = first_step_node; + } else { + node = loop_node; + } + } + if (is_one_byte) { + node = node->FilterOneByte(RegExpCompiler::kMaxRecursion); + // 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->FilterOneByte(RegExpCompiler::kMaxRecursion); + } + } else if (is_unicode && (is_global || is_sticky)) { + node = RegExpCompiler::OptionallyStepBackToLeadSurrogate(&compiler, node, + flags); + } + + if (node == nullptr) node = new (zone) EndNode(EndNode::BACKTRACK, zone); + data->node = node; + Analysis analysis(isolate, is_one_byte); + analysis.EnsureAnalyzed(node); + if (analysis.has_failed()) { + data->error = + isolate->factory()->NewStringFromAsciiChecked(analysis.error_message()); + return false; + } + + // Create the correct assembler for the architecture. + std::unique_ptr macro_assembler; + if (!FLAG_regexp_interpret_all) { + // Native regexp implementation. + DCHECK(!FLAG_jitless); + + NativeRegExpMacroAssembler::Mode mode = + is_one_byte ? NativeRegExpMacroAssembler::LATIN1 + : NativeRegExpMacroAssembler::UC16; + +#if V8_TARGET_ARCH_IA32 + macro_assembler.reset(new RegExpMacroAssemblerIA32( + isolate, zone, mode, (data->capture_count + 1) * 2)); +#elif V8_TARGET_ARCH_X64 + macro_assembler.reset(new RegExpMacroAssemblerX64( + isolate, zone, mode, (data->capture_count + 1) * 2)); +#elif V8_TARGET_ARCH_ARM + macro_assembler.reset(new RegExpMacroAssemblerARM( + isolate, zone, mode, (data->capture_count + 1) * 2)); +#elif V8_TARGET_ARCH_ARM64 + macro_assembler.reset(new RegExpMacroAssemblerARM64( + isolate, zone, mode, (data->capture_count + 1) * 2)); +#elif V8_TARGET_ARCH_S390 + macro_assembler.reset(new RegExpMacroAssemblerS390( + isolate, zone, mode, (data->capture_count + 1) * 2)); +#elif V8_TARGET_ARCH_PPC + macro_assembler.reset(new RegExpMacroAssemblerPPC( + isolate, zone, mode, (data->capture_count + 1) * 2)); +#elif V8_TARGET_ARCH_MIPS + macro_assembler.reset(new RegExpMacroAssemblerMIPS( + isolate, zone, mode, (data->capture_count + 1) * 2)); +#elif V8_TARGET_ARCH_MIPS64 + macro_assembler.reset(new RegExpMacroAssemblerMIPS( + isolate, zone, mode, (data->capture_count + 1) * 2)); +#else +#error "Unsupported architecture" +#endif + } else { + DCHECK(FLAG_regexp_interpret_all); + + // Interpreted regexp implementation. + macro_assembler.reset(new RegExpBytecodeGenerator(isolate, zone)); + } + + macro_assembler->set_slow_safe(TooMuchRegExpCode(isolate, pattern)); + + // 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 && !is_sticky && + max_length < kMaxBacksearchLimit) { + macro_assembler->SetCurrentPositionFromEnd(max_length); + } + + if (is_global) { + RegExpMacroAssembler::GlobalMode mode = RegExpMacroAssembler::GLOBAL; + if (data->tree->min_match() > 0) { + mode = RegExpMacroAssembler::GLOBAL_NO_ZERO_LENGTH_CHECK; + } else if (is_unicode) { + mode = RegExpMacroAssembler::GLOBAL_UNICODE; + } + macro_assembler->set_global_mode(mode); + } + + RegExpCompiler::CompilationResult result = compiler.Assemble( + isolate, macro_assembler.get(), node, data->capture_count, pattern); + + if (FLAG_correctness_fuzzer_suppressions && + strncmp(result.error_message, "Stack overflow", 15) == 0) { + FATAL("Aborting on stack overflow"); + } + + if (result.error_message != nullptr) { + data->error = + isolate->factory()->NewStringFromAsciiChecked(result.error_message); + } + data->code = result.code; + data->register_count = result.num_registers; + + return result.Succeeded(); +} + +RegExpGlobalCache::RegExpGlobalCache(Handle regexp, + Handle subject, Isolate* isolate) + : register_array_(nullptr), + register_array_size_(0), + regexp_(regexp), + subject_(subject), + isolate_(isolate) { + bool interpreted = FLAG_regexp_interpret_all; + + if (regexp_->TypeTag() == JSRegExp::ATOM) { + static const int kAtomRegistersPerMatch = 2; + registers_per_match_ = kAtomRegistersPerMatch; + // There is no distinction between interpreted and native for atom regexps. + interpreted = false; + } else { + registers_per_match_ = RegExp::IrregexpPrepare(isolate_, regexp_, subject_); + if (registers_per_match_ < 0) { + num_matches_ = -1; // Signal exception. + return; + } + } + + DCHECK(IsGlobal(regexp->GetFlags())); + if (!interpreted) { + register_array_size_ = + Max(registers_per_match_, Isolate::kJSRegexpStaticOffsetsVectorSize); + max_matches_ = register_array_size_ / registers_per_match_; + } else { + // Global loop in interpreted regexp is not implemented. We choose + // the size of the offsets vector so that it can only store one match. + register_array_size_ = registers_per_match_; + max_matches_ = 1; + } + + if (register_array_size_ > Isolate::kJSRegexpStaticOffsetsVectorSize) { + register_array_ = NewArray(register_array_size_); + } else { + register_array_ = isolate->jsregexp_static_offsets_vector(); + } + + // Set state so that fetching the results the first time triggers a call + // to the compiled regexp. + current_match_index_ = max_matches_ - 1; + num_matches_ = max_matches_; + DCHECK_LE(2, registers_per_match_); // Each match has at least one capture. + DCHECK_GE(register_array_size_, registers_per_match_); + int32_t* last_match = + ®ister_array_[current_match_index_ * registers_per_match_]; + last_match[0] = -1; + last_match[1] = 0; +} + +RegExpGlobalCache::~RegExpGlobalCache() { + // Deallocate the register array if we allocated it in the constructor + // (as opposed to using the existing jsregexp_static_offsets_vector). + if (register_array_size_ > Isolate::kJSRegexpStaticOffsetsVectorSize) { + DeleteArray(register_array_); + } +} + +int RegExpGlobalCache::AdvanceZeroLength(int last_index) { + if (IsUnicode(regexp_->GetFlags()) && last_index + 1 < subject_->length() && + unibrow::Utf16::IsLeadSurrogate(subject_->Get(last_index)) && + unibrow::Utf16::IsTrailSurrogate(subject_->Get(last_index + 1))) { + // Advance over the surrogate pair. + return last_index + 2; + } + return last_index + 1; +} + +int32_t* RegExpGlobalCache::FetchNext() { + current_match_index_++; + if (current_match_index_ >= num_matches_) { + // Current batch of results exhausted. + // Fail if last batch was not even fully filled. + if (num_matches_ < max_matches_) { + num_matches_ = 0; // Signal failed match. + return nullptr; + } + + int32_t* last_match = + ®ister_array_[(current_match_index_ - 1) * registers_per_match_]; + int last_end_index = last_match[1]; + + if (regexp_->TypeTag() == JSRegExp::ATOM) { + num_matches_ = + RegExpImpl::AtomExecRaw(isolate_, regexp_, subject_, last_end_index, + register_array_, register_array_size_); + } else { + int last_start_index = last_match[0]; + if (last_start_index == last_end_index) { + // Zero-length match. Advance by one code point. + last_end_index = AdvanceZeroLength(last_end_index); + } + if (last_end_index > subject_->length()) { + num_matches_ = 0; // Signal failed match. + return nullptr; + } + num_matches_ = RegExpImpl::IrregexpExecRaw( + isolate_, regexp_, subject_, last_end_index, register_array_, + register_array_size_); + } + + if (num_matches_ <= 0) return nullptr; + current_match_index_ = 0; + return register_array_; + } else { + return ®ister_array_[current_match_index_ * registers_per_match_]; + } +} + +int32_t* RegExpGlobalCache::LastSuccessfulMatch() { + int index = current_match_index_ * registers_per_match_; + if (num_matches_ == 0) { + // After a failed match we shift back by one result. + index -= registers_per_match_; + } + return ®ister_array_[index]; +} + +Object RegExpResultsCache::Lookup(Heap* heap, String key_string, + Object key_pattern, + FixedArray* last_match_cache, + ResultsCacheType type) { + FixedArray cache; + if (!key_string.IsInternalizedString()) return Smi::kZero; + if (type == STRING_SPLIT_SUBSTRINGS) { + DCHECK(key_pattern.IsString()); + if (!key_pattern.IsInternalizedString()) return Smi::kZero; + cache = heap->string_split_cache(); + } else { + DCHECK(type == REGEXP_MULTIPLE_INDICES); + DCHECK(key_pattern.IsFixedArray()); + cache = heap->regexp_multiple_cache(); + } + + uint32_t hash = key_string.Hash(); + uint32_t index = ((hash & (kRegExpResultsCacheSize - 1)) & + ~(kArrayEntriesPerCacheEntry - 1)); + if (cache.get(index + kStringOffset) != key_string || + cache.get(index + kPatternOffset) != key_pattern) { + index = + ((index + kArrayEntriesPerCacheEntry) & (kRegExpResultsCacheSize - 1)); + if (cache.get(index + kStringOffset) != key_string || + cache.get(index + kPatternOffset) != key_pattern) { + return Smi::kZero; + } + } + + *last_match_cache = FixedArray::cast(cache.get(index + kLastMatchOffset)); + return cache.get(index + kArrayOffset); +} + +void RegExpResultsCache::Enter(Isolate* isolate, Handle key_string, + Handle key_pattern, + Handle value_array, + Handle last_match_cache, + ResultsCacheType type) { + Factory* factory = isolate->factory(); + Handle cache; + if (!key_string->IsInternalizedString()) return; + if (type == STRING_SPLIT_SUBSTRINGS) { + DCHECK(key_pattern->IsString()); + if (!key_pattern->IsInternalizedString()) return; + cache = factory->string_split_cache(); + } else { + DCHECK(type == REGEXP_MULTIPLE_INDICES); + DCHECK(key_pattern->IsFixedArray()); + cache = factory->regexp_multiple_cache(); + } + + uint32_t hash = key_string->Hash(); + uint32_t index = ((hash & (kRegExpResultsCacheSize - 1)) & + ~(kArrayEntriesPerCacheEntry - 1)); + if (cache->get(index + kStringOffset) == Smi::kZero) { + cache->set(index + kStringOffset, *key_string); + cache->set(index + kPatternOffset, *key_pattern); + cache->set(index + kArrayOffset, *value_array); + cache->set(index + kLastMatchOffset, *last_match_cache); + } else { + uint32_t index2 = + ((index + kArrayEntriesPerCacheEntry) & (kRegExpResultsCacheSize - 1)); + if (cache->get(index2 + kStringOffset) == Smi::kZero) { + cache->set(index2 + kStringOffset, *key_string); + cache->set(index2 + kPatternOffset, *key_pattern); + cache->set(index2 + kArrayOffset, *value_array); + cache->set(index2 + kLastMatchOffset, *last_match_cache); + } else { + cache->set(index2 + kStringOffset, Smi::kZero); + cache->set(index2 + kPatternOffset, Smi::kZero); + cache->set(index2 + kArrayOffset, Smi::kZero); + cache->set(index2 + kLastMatchOffset, Smi::kZero); + cache->set(index + kStringOffset, *key_string); + cache->set(index + kPatternOffset, *key_pattern); + cache->set(index + kArrayOffset, *value_array); + cache->set(index + kLastMatchOffset, *last_match_cache); + } + } + // If the array is a reasonably short list of substrings, convert it into a + // list of internalized strings. + if (type == STRING_SPLIT_SUBSTRINGS && value_array->length() < 100) { + for (int i = 0; i < value_array->length(); i++) { + Handle str(String::cast(value_array->get(i)), isolate); + Handle internalized_str = factory->InternalizeString(str); + value_array->set(i, *internalized_str); + } + } + // Convert backing store to a copy-on-write array. + value_array->set_map_no_write_barrier( + ReadOnlyRoots(isolate).fixed_cow_array_map()); +} + +void RegExpResultsCache::Clear(FixedArray cache) { + for (int i = 0; i < kRegExpResultsCacheSize; i++) { + cache.set(i, Smi::kZero); + } +} + +} // namespace internal +} // namespace v8 diff --git a/chromium/v8/src/regexp/regexp.h b/chromium/v8/src/regexp/regexp.h new file mode 100644 index 00000000000..0f3ed463dae --- /dev/null +++ b/chromium/v8/src/regexp/regexp.h @@ -0,0 +1,177 @@ +// Copyright 2012 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_REGEXP_REGEXP_H_ +#define V8_REGEXP_REGEXP_H_ + +#include "src/objects/js-regexp.h" + +namespace v8 { +namespace internal { + +class RegExpNode; +class RegExpTree; + +// TODO(jgruber): Consider splitting between ParseData and CompileData. +struct RegExpCompileData { + // The parsed AST as produced by the RegExpParser. + RegExpTree* tree = nullptr; + + // The compiled Node graph as produced by RegExpTree::ToNode methods. + RegExpNode* node = nullptr; + + // The generated code as produced by the compiler. Either a Code object (for + // irregexp native code) or a ByteArray (for irregexp bytecode). + Object code; + + // True, iff the pattern is a 'simple' atom with zero captures. In other + // words, the pattern consists of a string with no metacharacters and special + // regexp features, and can be implemented as a standard string search. + bool simple = true; + + // True, iff the pattern is anchored at the start of the string with '^'. + bool contains_anchor = false; + + // Only use if the pattern contains named captures. If so, this contains a + // mapping of capture names to capture indices. + Handle capture_name_map; + + // The error message. Only used if an error occurred during parsing or + // compilation. + Handle error; + + // The number of capture groups, without the global capture \0. + int capture_count = 0; + + // The number of registers used by the generated code. + int register_count = 0; +}; + +class RegExp final : public AllStatic { + public: + // Whether the irregexp engine generates native code or interpreter bytecode. + static bool GeneratesNativeCode() { return !FLAG_regexp_interpret_all; } + + // Parses the RegExp pattern and prepares the JSRegExp object with + // generic data and choice of implementation - as well as what + // the implementation wants to store in the data field. + // Returns false if compilation fails. + V8_WARN_UNUSED_RESULT static MaybeHandle Compile( + Isolate* isolate, Handle re, Handle pattern, + JSRegExp::Flags flags); + + // See ECMA-262 section 15.10.6.2. + // This function calls the garbage collector if necessary. + V8_EXPORT_PRIVATE V8_WARN_UNUSED_RESULT static MaybeHandle Exec( + Isolate* isolate, Handle regexp, Handle subject, + int index, Handle last_match_info); + + // Integral return values used throughout regexp code layers. + static constexpr int kInternalRegExpFailure = 0; + static constexpr int kInternalRegExpSuccess = 1; + static constexpr int kInternalRegExpException = -1; + static constexpr int kInternalRegExpRetry = -2; + + enum IrregexpResult { + RE_FAILURE = kInternalRegExpFailure, + RE_SUCCESS = kInternalRegExpSuccess, + RE_EXCEPTION = kInternalRegExpException, + }; + + // Prepare a RegExp for being executed one or more times (using + // IrregexpExecOnce) on the subject. + // This ensures that the regexp is compiled for the subject, and that + // the subject is flat. + // Returns the number of integer spaces required by IrregexpExecOnce + // as its "registers" argument. If the regexp cannot be compiled, + // an exception is set as pending, and this function returns negative. + static int IrregexpPrepare(Isolate* isolate, Handle regexp, + Handle subject); + + // Set last match info. If match is nullptr, then setting captures is + // omitted. + static Handle SetLastMatchInfo( + Isolate* isolate, Handle last_match_info, + Handle subject, int capture_count, int32_t* match); + + V8_EXPORT_PRIVATE static bool CompileForTesting(Isolate* isolate, Zone* zone, + RegExpCompileData* input, + JSRegExp::Flags flags, + Handle pattern, + Handle sample_subject, + bool is_one_byte); + + V8_EXPORT_PRIVATE static void DotPrintForTesting(const char* label, + RegExpNode* node); + + static const int kRegExpTooLargeToOptimize = 20 * KB; +}; + +// Uses a special global mode of irregexp-generated code to perform a global +// search and return multiple results at once. As such, this is essentially an +// iterator over multiple results (retrieved batch-wise in advance). +class RegExpGlobalCache final { + public: + RegExpGlobalCache(Handle regexp, Handle subject, + Isolate* isolate); + + ~RegExpGlobalCache(); + + // Fetch the next entry in the cache for global regexp match results. + // This does not set the last match info. Upon failure, nullptr is + // returned. The cause can be checked with Result(). The previous result is + // still in available in memory when a failure happens. + int32_t* FetchNext(); + + int32_t* LastSuccessfulMatch(); + + bool HasException() { return num_matches_ < 0; } + + private: + int AdvanceZeroLength(int last_index); + + int num_matches_; + int max_matches_; + int current_match_index_; + int registers_per_match_; + // Pointer to the last set of captures. + int32_t* register_array_; + int register_array_size_; + Handle regexp_; + Handle subject_; + Isolate* isolate_; +}; + +// Caches results for specific regexp queries on the isolate. At the time of +// writing, this is used during global calls to RegExp.prototype.exec and +// @@split. +class RegExpResultsCache final : public AllStatic { + public: + enum ResultsCacheType { REGEXP_MULTIPLE_INDICES, STRING_SPLIT_SUBSTRINGS }; + + // Attempt to retrieve a cached result. On failure, 0 is returned as a Smi. + // On success, the returned result is guaranteed to be a COW-array. + static Object Lookup(Heap* heap, String key_string, Object key_pattern, + FixedArray* last_match_out, ResultsCacheType type); + // Attempt to add value_array to the cache specified by type. On success, + // value_array is turned into a COW-array. + static void Enter(Isolate* isolate, Handle key_string, + Handle key_pattern, Handle value_array, + Handle last_match_cache, ResultsCacheType type); + static void Clear(FixedArray cache); + + static constexpr int kRegExpResultsCacheSize = 0x100; + + private: + static constexpr int kStringOffset = 0; + static constexpr int kPatternOffset = 1; + static constexpr int kArrayOffset = 2; + static constexpr int kLastMatchOffset = 3; + static constexpr int kArrayEntriesPerCacheEntry = 4; +}; + +} // namespace internal +} // namespace v8 + +#endif // V8_REGEXP_REGEXP_H_ -- cgit v1.2.1