1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "src/base/platform/platform.h"
9 #include "src/compilation-cache.h"
10 #include "src/compiler.h"
11 #include "src/execution.h"
12 #include "src/factory.h"
13 #include "src/jsregexp-inl.h"
14 #include "src/jsregexp.h"
15 #include "src/ostreams.h"
16 #include "src/parser.h"
17 #include "src/regexp-macro-assembler.h"
18 #include "src/regexp-macro-assembler-irregexp.h"
19 #include "src/regexp-macro-assembler-tracer.h"
20 #include "src/regexp-stack.h"
21 #include "src/runtime/runtime.h"
22 #include "src/string-search.h"
23 #include "src/unicode-decoder.h"
25 #ifndef V8_INTERPRETED_REGEXP
26 #if V8_TARGET_ARCH_IA32
27 #include "src/ia32/regexp-macro-assembler-ia32.h" // NOLINT
28 #elif V8_TARGET_ARCH_X64
29 #include "src/x64/regexp-macro-assembler-x64.h" // NOLINT
30 #elif V8_TARGET_ARCH_ARM64
31 #include "src/arm64/regexp-macro-assembler-arm64.h" // NOLINT
32 #elif V8_TARGET_ARCH_ARM
33 #include "src/arm/regexp-macro-assembler-arm.h" // NOLINT
34 #elif V8_TARGET_ARCH_PPC
35 #include "src/ppc/regexp-macro-assembler-ppc.h" // NOLINT
36 #elif V8_TARGET_ARCH_MIPS
37 #include "src/mips/regexp-macro-assembler-mips.h" // NOLINT
38 #elif V8_TARGET_ARCH_MIPS64
39 #include "src/mips64/regexp-macro-assembler-mips64.h" // NOLINT
40 #elif V8_TARGET_ARCH_X87
41 #include "src/x87/regexp-macro-assembler-x87.h" // NOLINT
43 #error Unsupported target architecture.
47 #include "src/interpreter-irregexp.h"
53 MaybeHandle<Object> RegExpImpl::CreateRegExpLiteral(
54 Handle<JSFunction> constructor,
55 Handle<String> pattern,
56 Handle<String> flags) {
57 // Call the construct code with 2 arguments.
58 Handle<Object> argv[] = { pattern, flags };
59 return Execution::New(constructor, arraysize(argv), argv);
64 static inline MaybeHandle<Object> ThrowRegExpException(
66 Handle<String> pattern,
67 Handle<String> error_text,
68 const char* message) {
69 Isolate* isolate = re->GetIsolate();
70 Factory* factory = isolate->factory();
71 Handle<FixedArray> elements = factory->NewFixedArray(2);
72 elements->set(0, *pattern);
73 elements->set(1, *error_text);
74 Handle<JSArray> array = factory->NewJSArrayWithElements(elements);
75 Handle<Object> regexp_err;
76 THROW_NEW_ERROR(isolate, NewSyntaxError(message, array), Object);
80 ContainedInLattice AddRange(ContainedInLattice containment,
84 DCHECK((ranges_length & 1) == 1);
85 DCHECK(ranges[ranges_length - 1] == String::kMaxUtf16CodeUnit + 1);
86 if (containment == kLatticeUnknown) return containment;
89 for (int i = 0; i < ranges_length; inside = !inside, last = ranges[i], i++) {
90 // Consider the range from last to ranges[i].
91 // We haven't got to the new range yet.
92 if (ranges[i] <= new_range.from()) continue;
93 // New range is wholly inside last-ranges[i]. Note that new_range.to() is
94 // inclusive, but the values in ranges are not.
95 if (last <= new_range.from() && new_range.to() < ranges[i]) {
96 return Combine(containment, inside ? kLatticeIn : kLatticeOut);
98 return kLatticeUnknown;
104 // More makes code generation slower, less makes V8 benchmark score lower.
105 const int kMaxLookaheadForBoyerMoore = 8;
106 // In a 3-character pattern you can maximally step forwards 3 characters
107 // at a time, which is not always enough to pay for the extra logic.
108 const int kPatternTooShortForBoyerMoore = 2;
111 // Identifies the sort of regexps where the regexp engine is faster
112 // than the code used for atom matches.
113 static bool HasFewDifferentCharacters(Handle<String> pattern) {
114 int length = Min(kMaxLookaheadForBoyerMoore, pattern->length());
115 if (length <= kPatternTooShortForBoyerMoore) return false;
116 const int kMod = 128;
117 bool character_found[kMod];
119 memset(&character_found[0], 0, sizeof(character_found));
120 for (int i = 0; i < length; i++) {
121 int ch = (pattern->Get(i) & (kMod - 1));
122 if (!character_found[ch]) {
123 character_found[ch] = true;
125 // We declare a regexp low-alphabet if it has at least 3 times as many
126 // characters as it has different characters.
127 if (different * 3 > length) return false;
134 // Generic RegExp methods. Dispatches to implementation specific methods.
137 MaybeHandle<Object> RegExpImpl::Compile(Handle<JSRegExp> re,
138 Handle<String> pattern,
139 JSRegExp::Flags flags) {
140 Isolate* isolate = re->GetIsolate();
142 CompilationCache* compilation_cache = isolate->compilation_cache();
143 MaybeHandle<FixedArray> maybe_cached =
144 compilation_cache->LookupRegExp(pattern, flags);
145 Handle<FixedArray> cached;
146 bool in_cache = maybe_cached.ToHandle(&cached);
147 LOG(isolate, RegExpCompileEvent(re, in_cache));
149 Handle<Object> result;
151 re->set_data(*cached);
154 pattern = String::Flatten(pattern);
155 PostponeInterruptsScope postpone(isolate);
156 RegExpCompileData parse_result;
157 FlatStringReader reader(isolate, pattern);
158 if (!RegExpParser::ParseRegExp(re->GetIsolate(), &zone, &reader,
159 flags.is_multiline(), flags.is_unicode(),
161 // Throw an exception if we fail to parse the pattern.
162 return ThrowRegExpException(re,
168 bool has_been_compiled = false;
170 if (parse_result.simple &&
171 !flags.is_ignore_case() &&
172 !flags.is_sticky() &&
173 !HasFewDifferentCharacters(pattern)) {
174 // Parse-tree is a single atom that is equal to the pattern.
175 AtomCompile(re, pattern, flags, pattern);
176 has_been_compiled = true;
177 } else if (parse_result.tree->IsAtom() &&
178 !flags.is_ignore_case() &&
179 !flags.is_sticky() &&
180 parse_result.capture_count == 0) {
181 RegExpAtom* atom = parse_result.tree->AsAtom();
182 Vector<const uc16> atom_pattern = atom->data();
183 Handle<String> atom_string;
184 ASSIGN_RETURN_ON_EXCEPTION(
185 isolate, atom_string,
186 isolate->factory()->NewStringFromTwoByte(atom_pattern),
188 if (!HasFewDifferentCharacters(atom_string)) {
189 AtomCompile(re, pattern, flags, atom_string);
190 has_been_compiled = true;
193 if (!has_been_compiled) {
194 IrregexpInitialize(re, pattern, flags, parse_result.capture_count);
196 DCHECK(re->data()->IsFixedArray());
197 // Compilation succeeded so the data is set on the regexp
198 // and we can store it in the cache.
199 Handle<FixedArray> data(FixedArray::cast(re->data()));
200 compilation_cache->PutRegExp(pattern, flags, data);
206 MaybeHandle<Object> RegExpImpl::Exec(Handle<JSRegExp> regexp,
207 Handle<String> subject,
209 Handle<JSArray> last_match_info) {
210 switch (regexp->TypeTag()) {
212 return AtomExec(regexp, subject, index, last_match_info);
213 case JSRegExp::IRREGEXP: {
214 return IrregexpExec(regexp, subject, index, last_match_info);
218 return MaybeHandle<Object>();
223 // RegExp Atom implementation: Simple string search using indexOf.
226 void RegExpImpl::AtomCompile(Handle<JSRegExp> re,
227 Handle<String> pattern,
228 JSRegExp::Flags flags,
229 Handle<String> match_pattern) {
230 re->GetIsolate()->factory()->SetRegExpAtomData(re,
238 static void SetAtomLastCapture(FixedArray* array,
242 SealHandleScope shs(array->GetIsolate());
243 RegExpImpl::SetLastCaptureCount(array, 2);
244 RegExpImpl::SetLastSubject(array, subject);
245 RegExpImpl::SetLastInput(array, subject);
246 RegExpImpl::SetCapture(array, 0, from);
247 RegExpImpl::SetCapture(array, 1, to);
251 int RegExpImpl::AtomExecRaw(Handle<JSRegExp> regexp,
252 Handle<String> subject,
256 Isolate* isolate = regexp->GetIsolate();
259 DCHECK(index <= subject->length());
261 subject = String::Flatten(subject);
262 DisallowHeapAllocation no_gc; // ensure vectors stay valid
264 String* needle = String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex));
265 int needle_len = needle->length();
266 DCHECK(needle->IsFlat());
267 DCHECK_LT(0, needle_len);
269 if (index + needle_len > subject->length()) {
270 return RegExpImpl::RE_FAILURE;
273 for (int i = 0; i < output_size; i += 2) {
274 String::FlatContent needle_content = needle->GetFlatContent();
275 String::FlatContent subject_content = subject->GetFlatContent();
276 DCHECK(needle_content.IsFlat());
277 DCHECK(subject_content.IsFlat());
278 // dispatch on type of strings
280 (needle_content.IsOneByte()
281 ? (subject_content.IsOneByte()
282 ? SearchString(isolate, subject_content.ToOneByteVector(),
283 needle_content.ToOneByteVector(), index)
284 : SearchString(isolate, subject_content.ToUC16Vector(),
285 needle_content.ToOneByteVector(), index))
286 : (subject_content.IsOneByte()
287 ? SearchString(isolate, subject_content.ToOneByteVector(),
288 needle_content.ToUC16Vector(), index)
289 : SearchString(isolate, subject_content.ToUC16Vector(),
290 needle_content.ToUC16Vector(), index)));
292 return i / 2; // Return number of matches.
295 output[i+1] = index + needle_len;
299 return output_size / 2;
303 Handle<Object> RegExpImpl::AtomExec(Handle<JSRegExp> re,
304 Handle<String> subject,
306 Handle<JSArray> last_match_info) {
307 Isolate* isolate = re->GetIsolate();
309 static const int kNumRegisters = 2;
310 STATIC_ASSERT(kNumRegisters <= Isolate::kJSRegexpStaticOffsetsVectorSize);
311 int32_t* output_registers = isolate->jsregexp_static_offsets_vector();
313 int res = AtomExecRaw(re, subject, index, output_registers, kNumRegisters);
315 if (res == RegExpImpl::RE_FAILURE) return isolate->factory()->null_value();
317 DCHECK_EQ(res, RegExpImpl::RE_SUCCESS);
318 SealHandleScope shs(isolate);
319 FixedArray* array = FixedArray::cast(last_match_info->elements());
320 SetAtomLastCapture(array, *subject, output_registers[0], output_registers[1]);
321 return last_match_info;
325 // Irregexp implementation.
327 // Ensures that the regexp object contains a compiled version of the
328 // source for either one-byte or two-byte subject strings.
329 // If the compiled version doesn't already exist, it is compiled
330 // from the source pattern.
331 // If compilation fails, an exception is thrown and this function
333 bool RegExpImpl::EnsureCompiledIrregexp(Handle<JSRegExp> re,
334 Handle<String> sample_subject,
336 Object* compiled_code = re->DataAt(JSRegExp::code_index(is_one_byte));
337 #ifdef V8_INTERPRETED_REGEXP
338 if (compiled_code->IsByteArray()) return true;
339 #else // V8_INTERPRETED_REGEXP (RegExp native code)
340 if (compiled_code->IsCode()) return true;
342 // We could potentially have marked this as flushable, but have kept
343 // a saved version if we did not flush it yet.
344 Object* saved_code = re->DataAt(JSRegExp::saved_code_index(is_one_byte));
345 if (saved_code->IsCode()) {
346 // Reinstate the code in the original place.
347 re->SetDataAt(JSRegExp::code_index(is_one_byte), saved_code);
348 DCHECK(compiled_code->IsSmi());
351 return CompileIrregexp(re, sample_subject, is_one_byte);
355 static void CreateRegExpErrorObjectAndThrow(Handle<JSRegExp> re,
356 Handle<String> error_message,
358 Factory* factory = isolate->factory();
359 Handle<FixedArray> elements = factory->NewFixedArray(2);
360 elements->set(0, re->Pattern());
361 elements->set(1, *error_message);
362 Handle<JSArray> array = factory->NewJSArrayWithElements(elements);
363 Handle<Object> error;
364 MaybeHandle<Object> maybe_error =
365 factory->NewSyntaxError("malformed_regexp", array);
366 if (maybe_error.ToHandle(&error)) isolate->Throw(*error);
370 bool RegExpImpl::CompileIrregexp(Handle<JSRegExp> re,
371 Handle<String> sample_subject,
373 // Compile the RegExp.
374 Isolate* isolate = re->GetIsolate();
376 PostponeInterruptsScope postpone(isolate);
377 // If we had a compilation error the last time this is saved at the
379 Object* entry = re->DataAt(JSRegExp::code_index(is_one_byte));
380 // When arriving here entry can only be a smi, either representing an
381 // uncompiled regexp, a previous compilation error, or code that has
383 DCHECK(entry->IsSmi());
384 int entry_value = Smi::cast(entry)->value();
385 DCHECK(entry_value == JSRegExp::kUninitializedValue ||
386 entry_value == JSRegExp::kCompilationErrorValue ||
387 (entry_value < JSRegExp::kCodeAgeMask && entry_value >= 0));
389 if (entry_value == JSRegExp::kCompilationErrorValue) {
390 // A previous compilation failed and threw an error which we store in
391 // the saved code index (we store the error message, not the actual
392 // error). Recreate the error object and throw it.
393 Object* error_string = re->DataAt(JSRegExp::saved_code_index(is_one_byte));
394 DCHECK(error_string->IsString());
395 Handle<String> error_message(String::cast(error_string));
396 CreateRegExpErrorObjectAndThrow(re, error_message, isolate);
400 JSRegExp::Flags flags = re->GetFlags();
402 Handle<String> pattern(re->Pattern());
403 pattern = String::Flatten(pattern);
404 RegExpCompileData compile_data;
405 FlatStringReader reader(isolate, pattern);
406 if (!RegExpParser::ParseRegExp(isolate, &zone, &reader, flags.is_multiline(),
407 flags.is_unicode(), &compile_data)) {
408 // Throw an exception if we fail to parse the pattern.
409 // THIS SHOULD NOT HAPPEN. We already pre-parsed it successfully once.
410 USE(ThrowRegExpException(re,
413 "malformed_regexp"));
416 RegExpEngine::CompilationResult result = RegExpEngine::Compile(
417 isolate, &zone, &compile_data, flags.is_ignore_case(), flags.is_global(),
418 flags.is_multiline(), flags.is_sticky(), pattern, sample_subject,
420 if (result.error_message != NULL) {
421 // Unable to compile regexp.
422 Handle<String> error_message = isolate->factory()->NewStringFromUtf8(
423 CStrVector(result.error_message)).ToHandleChecked();
424 CreateRegExpErrorObjectAndThrow(re, error_message, isolate);
428 Handle<FixedArray> data = Handle<FixedArray>(FixedArray::cast(re->data()));
429 data->set(JSRegExp::code_index(is_one_byte), result.code);
430 int register_max = IrregexpMaxRegisterCount(*data);
431 if (result.num_registers > register_max) {
432 SetIrregexpMaxRegisterCount(*data, result.num_registers);
439 int RegExpImpl::IrregexpMaxRegisterCount(FixedArray* re) {
441 re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value();
445 void RegExpImpl::SetIrregexpMaxRegisterCount(FixedArray* re, int value) {
446 re->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(value));
450 int RegExpImpl::IrregexpNumberOfCaptures(FixedArray* re) {
451 return Smi::cast(re->get(JSRegExp::kIrregexpCaptureCountIndex))->value();
455 int RegExpImpl::IrregexpNumberOfRegisters(FixedArray* re) {
456 return Smi::cast(re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value();
460 ByteArray* RegExpImpl::IrregexpByteCode(FixedArray* re, bool is_one_byte) {
461 return ByteArray::cast(re->get(JSRegExp::code_index(is_one_byte)));
465 Code* RegExpImpl::IrregexpNativeCode(FixedArray* re, bool is_one_byte) {
466 return Code::cast(re->get(JSRegExp::code_index(is_one_byte)));
470 void RegExpImpl::IrregexpInitialize(Handle<JSRegExp> re,
471 Handle<String> pattern,
472 JSRegExp::Flags flags,
474 // Initialize compiled code entries to null.
475 re->GetIsolate()->factory()->SetRegExpIrregexpData(re,
483 int RegExpImpl::IrregexpPrepare(Handle<JSRegExp> regexp,
484 Handle<String> subject) {
485 subject = String::Flatten(subject);
487 // Check representation of the underlying storage.
488 bool is_one_byte = subject->IsOneByteRepresentationUnderneath();
489 if (!EnsureCompiledIrregexp(regexp, subject, is_one_byte)) return -1;
491 #ifdef V8_INTERPRETED_REGEXP
492 // Byte-code regexp needs space allocated for all its registers.
493 // The result captures are copied to the start of the registers array
494 // if the match succeeds. This way those registers are not clobbered
495 // when we set the last match info from last successful match.
496 return IrregexpNumberOfRegisters(FixedArray::cast(regexp->data())) +
497 (IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())) + 1) * 2;
498 #else // V8_INTERPRETED_REGEXP
499 // Native regexp only needs room to output captures. Registers are handled
501 return (IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())) + 1) * 2;
502 #endif // V8_INTERPRETED_REGEXP
506 int RegExpImpl::IrregexpExecRaw(Handle<JSRegExp> regexp,
507 Handle<String> subject,
511 Isolate* isolate = regexp->GetIsolate();
513 Handle<FixedArray> irregexp(FixedArray::cast(regexp->data()), isolate);
516 DCHECK(index <= subject->length());
517 DCHECK(subject->IsFlat());
519 bool is_one_byte = subject->IsOneByteRepresentationUnderneath();
521 #ifndef V8_INTERPRETED_REGEXP
522 DCHECK(output_size >= (IrregexpNumberOfCaptures(*irregexp) + 1) * 2);
524 EnsureCompiledIrregexp(regexp, subject, is_one_byte);
525 Handle<Code> code(IrregexpNativeCode(*irregexp, is_one_byte), isolate);
526 // The stack is used to allocate registers for the compiled regexp code.
527 // This means that in case of failure, the output registers array is left
528 // untouched and contains the capture results from the previous successful
529 // match. We can use that to set the last match info lazily.
530 NativeRegExpMacroAssembler::Result res =
531 NativeRegExpMacroAssembler::Match(code,
537 if (res != NativeRegExpMacroAssembler::RETRY) {
538 DCHECK(res != NativeRegExpMacroAssembler::EXCEPTION ||
539 isolate->has_pending_exception());
541 static_cast<int>(NativeRegExpMacroAssembler::SUCCESS) == RE_SUCCESS);
543 static_cast<int>(NativeRegExpMacroAssembler::FAILURE) == RE_FAILURE);
544 STATIC_ASSERT(static_cast<int>(NativeRegExpMacroAssembler::EXCEPTION)
546 return static_cast<IrregexpResult>(res);
548 // If result is RETRY, the string has changed representation, and we
549 // must restart from scratch.
550 // In this case, it means we must make sure we are prepared to handle
551 // the, potentially, different subject (the string can switch between
552 // being internal and external, and even between being Latin1 and UC16,
553 // but the characters are always the same).
554 IrregexpPrepare(regexp, subject);
555 is_one_byte = subject->IsOneByteRepresentationUnderneath();
559 #else // V8_INTERPRETED_REGEXP
561 DCHECK(output_size >= IrregexpNumberOfRegisters(*irregexp));
562 // We must have done EnsureCompiledIrregexp, so we can get the number of
564 int number_of_capture_registers =
565 (IrregexpNumberOfCaptures(*irregexp) + 1) * 2;
566 int32_t* raw_output = &output[number_of_capture_registers];
567 // We do not touch the actual capture result registers until we know there
568 // has been a match so that we can use those capture results to set the
570 for (int i = number_of_capture_registers - 1; i >= 0; i--) {
573 Handle<ByteArray> byte_codes(IrregexpByteCode(*irregexp, is_one_byte),
576 IrregexpResult result = IrregexpInterpreter::Match(isolate,
581 if (result == RE_SUCCESS) {
582 // Copy capture results to the start of the registers array.
583 MemCopy(output, raw_output, number_of_capture_registers * sizeof(int32_t));
585 if (result == RE_EXCEPTION) {
586 DCHECK(!isolate->has_pending_exception());
587 isolate->StackOverflow();
590 #endif // V8_INTERPRETED_REGEXP
594 MaybeHandle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp,
595 Handle<String> subject,
597 Handle<JSArray> last_match_info) {
598 Isolate* isolate = regexp->GetIsolate();
599 DCHECK_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP);
601 // Prepare space for the return values.
602 #if defined(V8_INTERPRETED_REGEXP) && defined(DEBUG)
603 if (FLAG_trace_regexp_bytecodes) {
604 String* pattern = regexp->Pattern();
605 PrintF("\n\nRegexp match: /%s/\n\n", pattern->ToCString().get());
606 PrintF("\n\nSubject string: '%s'\n\n", subject->ToCString().get());
609 int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject);
610 if (required_registers < 0) {
611 // Compiling failed with an exception.
612 DCHECK(isolate->has_pending_exception());
613 return MaybeHandle<Object>();
616 int32_t* output_registers = NULL;
617 if (required_registers > Isolate::kJSRegexpStaticOffsetsVectorSize) {
618 output_registers = NewArray<int32_t>(required_registers);
620 SmartArrayPointer<int32_t> auto_release(output_registers);
621 if (output_registers == NULL) {
622 output_registers = isolate->jsregexp_static_offsets_vector();
625 int res = RegExpImpl::IrregexpExecRaw(
626 regexp, subject, previous_index, output_registers, required_registers);
627 if (res == RE_SUCCESS) {
629 IrregexpNumberOfCaptures(FixedArray::cast(regexp->data()));
630 return SetLastMatchInfo(
631 last_match_info, subject, capture_count, output_registers);
633 if (res == RE_EXCEPTION) {
634 DCHECK(isolate->has_pending_exception());
635 return MaybeHandle<Object>();
637 DCHECK(res == RE_FAILURE);
638 return isolate->factory()->null_value();
642 Handle<JSArray> RegExpImpl::SetLastMatchInfo(Handle<JSArray> last_match_info,
643 Handle<String> subject,
646 DCHECK(last_match_info->HasFastObjectElements());
647 int capture_register_count = (capture_count + 1) * 2;
648 JSArray::EnsureSize(last_match_info,
649 capture_register_count + kLastMatchOverhead);
650 DisallowHeapAllocation no_allocation;
651 FixedArray* array = FixedArray::cast(last_match_info->elements());
653 for (int i = 0; i < capture_register_count; i += 2) {
654 SetCapture(array, i, match[i]);
655 SetCapture(array, i + 1, match[i + 1]);
658 SetLastCaptureCount(array, capture_register_count);
659 SetLastSubject(array, *subject);
660 SetLastInput(array, *subject);
661 return last_match_info;
665 RegExpImpl::GlobalCache::GlobalCache(Handle<JSRegExp> regexp,
666 Handle<String> subject,
669 : register_array_(NULL),
670 register_array_size_(0),
673 #ifdef V8_INTERPRETED_REGEXP
674 bool interpreted = true;
676 bool interpreted = false;
677 #endif // V8_INTERPRETED_REGEXP
679 if (regexp_->TypeTag() == JSRegExp::ATOM) {
680 static const int kAtomRegistersPerMatch = 2;
681 registers_per_match_ = kAtomRegistersPerMatch;
682 // There is no distinction between interpreted and native for atom regexps.
685 registers_per_match_ = RegExpImpl::IrregexpPrepare(regexp_, subject_);
686 if (registers_per_match_ < 0) {
687 num_matches_ = -1; // Signal exception.
692 if (is_global && !interpreted) {
693 register_array_size_ =
694 Max(registers_per_match_, Isolate::kJSRegexpStaticOffsetsVectorSize);
695 max_matches_ = register_array_size_ / registers_per_match_;
697 // Global loop in interpreted regexp is not implemented. We choose
698 // the size of the offsets vector so that it can only store one match.
699 register_array_size_ = registers_per_match_;
703 if (register_array_size_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
704 register_array_ = NewArray<int32_t>(register_array_size_);
706 register_array_ = isolate->jsregexp_static_offsets_vector();
709 // Set state so that fetching the results the first time triggers a call
710 // to the compiled regexp.
711 current_match_index_ = max_matches_ - 1;
712 num_matches_ = max_matches_;
713 DCHECK(registers_per_match_ >= 2); // Each match has at least one capture.
714 DCHECK_GE(register_array_size_, registers_per_match_);
715 int32_t* last_match =
716 ®ister_array_[current_match_index_ * registers_per_match_];
722 // -------------------------------------------------------------------
723 // Implementation of the Irregexp regular expression engine.
725 // The Irregexp regular expression engine is intended to be a complete
726 // implementation of ECMAScript regular expressions. It generates either
727 // bytecodes or native code.
729 // The Irregexp regexp engine is structured in three steps.
730 // 1) The parser generates an abstract syntax tree. See ast.cc.
731 // 2) From the AST a node network is created. The nodes are all
732 // subclasses of RegExpNode. The nodes represent states when
733 // executing a regular expression. Several optimizations are
734 // performed on the node network.
735 // 3) From the nodes we generate either byte codes or native code
736 // that can actually execute the regular expression (perform
737 // the search). The code generation step is described in more
742 // The nodes are divided into four main categories.
744 // These represent places where the regular expression can
745 // match in more than one way. For example on entry to an
746 // alternation (foo|bar) or a repetition (*, +, ? or {}).
748 // These represent places where some action should be
749 // performed. Examples include recording the current position
750 // in the input string to a register (in order to implement
751 // captures) or other actions on register for example in order
752 // to implement the counters needed for {} repetitions.
754 // These attempt to match some element part of the input string.
755 // Examples of elements include character classes, plain strings
756 // or back references.
758 // These are used to implement the actions required on finding
759 // a successful match or failing to find a match.
761 // The code generated (whether as byte codes or native code) maintains
762 // some state as it runs. This consists of the following elements:
764 // * The capture registers. Used for string captures.
765 // * Other registers. Used for counters etc.
766 // * The current position.
767 // * The stack of backtracking information. Used when a matching node
768 // fails to find a match and needs to try an alternative.
770 // Conceptual regular expression execution model:
772 // There is a simple conceptual model of regular expression execution
773 // which will be presented first. The actual code generated is a more
774 // efficient simulation of the simple conceptual model:
776 // * Choice nodes are implemented as follows:
777 // For each choice except the last {
778 // push current position
779 // push backtrack code location
780 // <generate code to test for choice>
781 // backtrack code location:
782 // pop current position
784 // <generate code to test for last choice>
786 // * Actions nodes are generated as follows
787 // <push affected registers on backtrack stack>
788 // <generate code to perform action>
789 // push backtrack code location
790 // <generate code to test for following nodes>
791 // backtrack code location:
792 // <pop affected registers to restore their state>
793 // <pop backtrack location from stack and go to it>
795 // * Matching nodes are generated as follows:
796 // if input string matches at current position
797 // update current position
798 // <generate code to test for following nodes>
800 // <pop backtrack location from stack and go to it>
802 // Thus it can be seen that the current position is saved and restored
803 // by the choice nodes, whereas the registers are saved and restored by
804 // by the action nodes that manipulate them.
806 // The other interesting aspect of this model is that nodes are generated
807 // at the point where they are needed by a recursive call to Emit(). If
808 // the node has already been code generated then the Emit() call will
809 // generate a jump to the previously generated code instead. In order to
810 // limit recursion it is possible for the Emit() function to put the node
811 // on a work list for later generation and instead generate a jump. The
812 // destination of the jump is resolved later when the code is generated.
814 // Actual regular expression code generation.
816 // Code generation is actually more complicated than the above. In order
817 // to improve the efficiency of the generated code some optimizations are
820 // * Choice nodes have 1-character lookahead.
821 // A choice node looks at the following character and eliminates some of
822 // the choices immediately based on that character. This is not yet
824 // * Simple greedy loops store reduced backtracking information.
825 // A quantifier like /.*foo/m will greedily match the whole input. It will
826 // then need to backtrack to a point where it can match "foo". The naive
827 // implementation of this would push each character position onto the
828 // backtracking stack, then pop them off one by one. This would use space
829 // proportional to the length of the input string. However since the "."
830 // can only match in one way and always has a constant length (in this case
831 // of 1) it suffices to store the current position on the top of the stack
832 // once. Matching now becomes merely incrementing the current position and
833 // backtracking becomes decrementing the current position and checking the
834 // result against the stored current position. This is faster and saves
836 // * The current state is virtualized.
837 // This is used to defer expensive operations until it is clear that they
838 // are needed and to generate code for a node more than once, allowing
839 // specialized an efficient versions of the code to be created. This is
840 // explained in the section below.
842 // Execution state virtualization.
844 // Instead of emitting code, nodes that manipulate the state can record their
845 // manipulation in an object called the Trace. The Trace object can record a
846 // current position offset, an optional backtrack code location on the top of
847 // the virtualized backtrack stack and some register changes. When a node is
848 // to be emitted it can flush the Trace or update it. Flushing the Trace
849 // will emit code to bring the actual state into line with the virtual state.
850 // Avoiding flushing the state can postpone some work (e.g. updates of capture
851 // registers). Postponing work can save time when executing the regular
852 // expression since it may be found that the work never has to be done as a
853 // failure to match can occur. In addition it is much faster to jump to a
854 // known backtrack code location than it is to pop an unknown backtrack
855 // location from the stack and jump there.
857 // The virtual state found in the Trace affects code generation. For example
858 // the virtual state contains the difference between the actual current
859 // position and the virtual current position, and matching code needs to use
860 // this offset to attempt a match in the correct location of the input
861 // string. Therefore code generated for a non-trivial trace is specialized
862 // to that trace. The code generator therefore has the ability to generate
863 // code for each node several times. In order to limit the size of the
864 // generated code there is an arbitrary limit on how many specialized sets of
865 // code may be generated for a given node. If the limit is reached, the
866 // trace is flushed and a generic version of the code for a node is emitted.
867 // This is subsequently used for that node. The code emitted for non-generic
868 // trace is not recorded in the node and so it cannot currently be reused in
869 // the event that code generation is requested for an identical trace.
872 void RegExpTree::AppendToText(RegExpText* text, Zone* zone) {
877 void RegExpAtom::AppendToText(RegExpText* text, Zone* zone) {
878 text->AddElement(TextElement::Atom(this), zone);
882 void RegExpCharacterClass::AppendToText(RegExpText* text, Zone* zone) {
883 text->AddElement(TextElement::CharClass(this), zone);
887 void RegExpText::AppendToText(RegExpText* text, Zone* zone) {
888 for (int i = 0; i < elements()->length(); i++)
889 text->AddElement(elements()->at(i), zone);
893 TextElement TextElement::Atom(RegExpAtom* atom) {
894 return TextElement(ATOM, atom);
898 TextElement TextElement::CharClass(RegExpCharacterClass* char_class) {
899 return TextElement(CHAR_CLASS, char_class);
903 int TextElement::length() const {
904 switch (text_type()) {
906 return atom()->length();
916 DispatchTable* ChoiceNode::GetTable(bool ignore_case) {
917 if (table_ == NULL) {
918 table_ = new(zone()) DispatchTable(zone());
919 DispatchTableConstructor cons(table_, ignore_case, zone());
920 cons.BuildTable(this);
926 class FrequencyCollator {
928 FrequencyCollator() : total_samples_(0) {
929 for (int i = 0; i < RegExpMacroAssembler::kTableSize; i++) {
930 frequencies_[i] = CharacterFrequency(i);
934 void CountCharacter(int character) {
935 int index = (character & RegExpMacroAssembler::kTableMask);
936 frequencies_[index].Increment();
940 // Does not measure in percent, but rather per-128 (the table size from the
941 // regexp macro assembler).
942 int Frequency(int in_character) {
943 DCHECK((in_character & RegExpMacroAssembler::kTableMask) == in_character);
944 if (total_samples_ < 1) return 1; // Division by zero.
946 (frequencies_[in_character].counter() * 128) / total_samples_;
947 return freq_in_per128;
951 class CharacterFrequency {
953 CharacterFrequency() : counter_(0), character_(-1) { }
954 explicit CharacterFrequency(int character)
955 : counter_(0), character_(character) { }
957 void Increment() { counter_++; }
958 int counter() { return counter_; }
959 int character() { return character_; }
968 CharacterFrequency frequencies_[RegExpMacroAssembler::kTableSize];
973 class RegExpCompiler {
975 RegExpCompiler(Isolate* isolate, Zone* zone, int capture_count,
976 bool ignore_case, bool is_one_byte);
978 int AllocateRegister() {
979 if (next_register_ >= RegExpMacroAssembler::kMaxRegister) {
980 reg_exp_too_big_ = true;
981 return next_register_;
983 return next_register_++;
986 RegExpEngine::CompilationResult Assemble(RegExpMacroAssembler* assembler,
989 Handle<String> pattern);
991 inline void AddWork(RegExpNode* node) { work_list_->Add(node); }
993 static const int kImplementationOffset = 0;
994 static const int kNumberOfRegistersOffset = 0;
995 static const int kCodeOffset = 1;
997 RegExpMacroAssembler* macro_assembler() { return macro_assembler_; }
998 EndNode* accept() { return accept_; }
1000 static const int kMaxRecursion = 100;
1001 inline int recursion_depth() { return recursion_depth_; }
1002 inline void IncrementRecursionDepth() { recursion_depth_++; }
1003 inline void DecrementRecursionDepth() { recursion_depth_--; }
1005 void SetRegExpTooBig() { reg_exp_too_big_ = true; }
1007 inline bool ignore_case() { return ignore_case_; }
1008 inline bool one_byte() { return one_byte_; }
1009 inline bool optimize() { return optimize_; }
1010 inline void set_optimize(bool value) { optimize_ = value; }
1011 FrequencyCollator* frequency_collator() { return &frequency_collator_; }
1013 int current_expansion_factor() { return current_expansion_factor_; }
1014 void set_current_expansion_factor(int value) {
1015 current_expansion_factor_ = value;
1018 Isolate* isolate() const { return isolate_; }
1019 Zone* zone() const { return zone_; }
1021 static const int kNoRegister = -1;
1026 List<RegExpNode*>* work_list_;
1027 int recursion_depth_;
1028 RegExpMacroAssembler* macro_assembler_;
1031 bool reg_exp_too_big_;
1033 int current_expansion_factor_;
1034 FrequencyCollator frequency_collator_;
1040 class RecursionCheck {
1042 explicit RecursionCheck(RegExpCompiler* compiler) : compiler_(compiler) {
1043 compiler->IncrementRecursionDepth();
1045 ~RecursionCheck() { compiler_->DecrementRecursionDepth(); }
1047 RegExpCompiler* compiler_;
1051 static RegExpEngine::CompilationResult IrregexpRegExpTooBig(Isolate* isolate) {
1052 return RegExpEngine::CompilationResult(isolate, "RegExp too big");
1056 // Attempts to compile the regexp using an Irregexp code generator. Returns
1057 // a fixed array or a null handle depending on whether it succeeded.
1058 RegExpCompiler::RegExpCompiler(Isolate* isolate, Zone* zone, int capture_count,
1059 bool ignore_case, bool one_byte)
1060 : next_register_(2 * (capture_count + 1)),
1062 recursion_depth_(0),
1063 ignore_case_(ignore_case),
1064 one_byte_(one_byte),
1065 reg_exp_too_big_(false),
1066 optimize_(FLAG_regexp_optimization),
1067 current_expansion_factor_(1),
1068 frequency_collator_(),
1071 accept_ = new(zone) EndNode(EndNode::ACCEPT, zone);
1072 DCHECK(next_register_ - 1 <= RegExpMacroAssembler::kMaxRegister);
1076 RegExpEngine::CompilationResult RegExpCompiler::Assemble(
1077 RegExpMacroAssembler* macro_assembler,
1080 Handle<String> pattern) {
1081 Heap* heap = pattern->GetHeap();
1084 if (FLAG_trace_regexp_assembler)
1086 new RegExpMacroAssemblerTracer(isolate(), macro_assembler);
1089 macro_assembler_ = macro_assembler;
1091 List <RegExpNode*> work_list(0);
1092 work_list_ = &work_list;
1094 macro_assembler_->PushBacktrack(&fail);
1096 start->Emit(this, &new_trace);
1097 macro_assembler_->Bind(&fail);
1098 macro_assembler_->Fail();
1099 while (!work_list.is_empty()) {
1100 work_list.RemoveLast()->Emit(this, &new_trace);
1102 if (reg_exp_too_big_) return IrregexpRegExpTooBig(isolate_);
1104 Handle<HeapObject> code = macro_assembler_->GetCode(pattern);
1105 heap->IncreaseTotalRegexpCodeGenerated(code->Size());
1107 #ifdef ENABLE_DISASSEMBLER
1108 if (FLAG_print_code) {
1109 CodeTracer::Scope trace_scope(heap->isolate()->GetCodeTracer());
1110 OFStream os(trace_scope.file());
1111 Handle<Code>::cast(code)->Disassemble(pattern->ToCString().get(), os);
1115 if (FLAG_trace_regexp_assembler) {
1116 delete macro_assembler_;
1119 return RegExpEngine::CompilationResult(*code, next_register_);
1123 bool Trace::DeferredAction::Mentions(int that) {
1124 if (action_type() == ActionNode::CLEAR_CAPTURES) {
1125 Interval range = static_cast<DeferredClearCaptures*>(this)->range();
1126 return range.Contains(that);
1128 return reg() == that;
1133 bool Trace::mentions_reg(int reg) {
1134 for (DeferredAction* action = actions_;
1136 action = action->next()) {
1137 if (action->Mentions(reg))
1144 bool Trace::GetStoredPosition(int reg, int* cp_offset) {
1145 DCHECK_EQ(0, *cp_offset);
1146 for (DeferredAction* action = actions_;
1148 action = action->next()) {
1149 if (action->Mentions(reg)) {
1150 if (action->action_type() == ActionNode::STORE_POSITION) {
1151 *cp_offset = static_cast<DeferredCapture*>(action)->cp_offset();
1162 int Trace::FindAffectedRegisters(OutSet* affected_registers,
1164 int max_register = RegExpCompiler::kNoRegister;
1165 for (DeferredAction* action = actions_;
1167 action = action->next()) {
1168 if (action->action_type() == ActionNode::CLEAR_CAPTURES) {
1169 Interval range = static_cast<DeferredClearCaptures*>(action)->range();
1170 for (int i = range.from(); i <= range.to(); i++)
1171 affected_registers->Set(i, zone);
1172 if (range.to() > max_register) max_register = range.to();
1174 affected_registers->Set(action->reg(), zone);
1175 if (action->reg() > max_register) max_register = action->reg();
1178 return max_register;
1182 void Trace::RestoreAffectedRegisters(RegExpMacroAssembler* assembler,
1184 const OutSet& registers_to_pop,
1185 const OutSet& registers_to_clear) {
1186 for (int reg = max_register; reg >= 0; reg--) {
1187 if (registers_to_pop.Get(reg)) {
1188 assembler->PopRegister(reg);
1189 } else if (registers_to_clear.Get(reg)) {
1191 while (reg > 0 && registers_to_clear.Get(reg - 1)) {
1194 assembler->ClearRegisters(reg, clear_to);
1200 void Trace::PerformDeferredActions(RegExpMacroAssembler* assembler,
1202 const OutSet& affected_registers,
1203 OutSet* registers_to_pop,
1204 OutSet* registers_to_clear,
1206 // The "+1" is to avoid a push_limit of zero if stack_limit_slack() is 1.
1207 const int push_limit = (assembler->stack_limit_slack() + 1) / 2;
1209 // Count pushes performed to force a stack limit check occasionally.
1212 for (int reg = 0; reg <= max_register; reg++) {
1213 if (!affected_registers.Get(reg)) {
1217 // The chronologically first deferred action in the trace
1218 // is used to infer the action needed to restore a register
1219 // to its previous state (or not, if it's safe to ignore it).
1220 enum DeferredActionUndoType { IGNORE, RESTORE, CLEAR };
1221 DeferredActionUndoType undo_action = IGNORE;
1224 bool absolute = false;
1226 int store_position = -1;
1227 // This is a little tricky because we are scanning the actions in reverse
1228 // historical order (newest first).
1229 for (DeferredAction* action = actions_;
1231 action = action->next()) {
1232 if (action->Mentions(reg)) {
1233 switch (action->action_type()) {
1234 case ActionNode::SET_REGISTER: {
1235 Trace::DeferredSetRegister* psr =
1236 static_cast<Trace::DeferredSetRegister*>(action);
1238 value += psr->value();
1241 // SET_REGISTER is currently only used for newly introduced loop
1242 // counters. They can have a significant previous value if they
1243 // occour in a loop. TODO(lrn): Propagate this information, so
1244 // we can set undo_action to IGNORE if we know there is no value to
1246 undo_action = RESTORE;
1247 DCHECK_EQ(store_position, -1);
1251 case ActionNode::INCREMENT_REGISTER:
1255 DCHECK_EQ(store_position, -1);
1257 undo_action = RESTORE;
1259 case ActionNode::STORE_POSITION: {
1260 Trace::DeferredCapture* pc =
1261 static_cast<Trace::DeferredCapture*>(action);
1262 if (!clear && store_position == -1) {
1263 store_position = pc->cp_offset();
1266 // For captures we know that stores and clears alternate.
1267 // Other register, are never cleared, and if the occur
1268 // inside a loop, they might be assigned more than once.
1270 // Registers zero and one, aka "capture zero", is
1271 // always set correctly if we succeed. There is no
1272 // need to undo a setting on backtrack, because we
1273 // will set it again or fail.
1274 undo_action = IGNORE;
1276 undo_action = pc->is_capture() ? CLEAR : RESTORE;
1279 DCHECK_EQ(value, 0);
1282 case ActionNode::CLEAR_CAPTURES: {
1283 // Since we're scanning in reverse order, if we've already
1284 // set the position we have to ignore historically earlier
1285 // clearing operations.
1286 if (store_position == -1) {
1289 undo_action = RESTORE;
1291 DCHECK_EQ(value, 0);
1300 // Prepare for the undo-action (e.g., push if it's going to be popped).
1301 if (undo_action == RESTORE) {
1303 RegExpMacroAssembler::StackCheckFlag stack_check =
1304 RegExpMacroAssembler::kNoStackLimitCheck;
1305 if (pushes == push_limit) {
1306 stack_check = RegExpMacroAssembler::kCheckStackLimit;
1310 assembler->PushRegister(reg, stack_check);
1311 registers_to_pop->Set(reg, zone);
1312 } else if (undo_action == CLEAR) {
1313 registers_to_clear->Set(reg, zone);
1315 // Perform the chronologically last action (or accumulated increment)
1316 // for the register.
1317 if (store_position != -1) {
1318 assembler->WriteCurrentPositionToRegister(reg, store_position);
1320 assembler->ClearRegisters(reg, reg);
1321 } else if (absolute) {
1322 assembler->SetRegister(reg, value);
1323 } else if (value != 0) {
1324 assembler->AdvanceRegister(reg, value);
1330 // This is called as we come into a loop choice node and some other tricky
1331 // nodes. It normalizes the state of the code generator to ensure we can
1332 // generate generic code.
1333 void Trace::Flush(RegExpCompiler* compiler, RegExpNode* successor) {
1334 RegExpMacroAssembler* assembler = compiler->macro_assembler();
1336 DCHECK(!is_trivial());
1338 if (actions_ == NULL && backtrack() == NULL) {
1339 // Here we just have some deferred cp advances to fix and we are back to
1340 // a normal situation. We may also have to forget some information gained
1341 // through a quick check that was already performed.
1342 if (cp_offset_ != 0) assembler->AdvanceCurrentPosition(cp_offset_);
1343 // Create a new trivial state and generate the node with that.
1345 successor->Emit(compiler, &new_state);
1349 // Generate deferred actions here along with code to undo them again.
1350 OutSet affected_registers;
1352 if (backtrack() != NULL) {
1353 // Here we have a concrete backtrack location. These are set up by choice
1354 // nodes and so they indicate that we have a deferred save of the current
1355 // position which we may need to emit here.
1356 assembler->PushCurrentPosition();
1359 int max_register = FindAffectedRegisters(&affected_registers,
1361 OutSet registers_to_pop;
1362 OutSet registers_to_clear;
1363 PerformDeferredActions(assembler,
1367 ®isters_to_clear,
1369 if (cp_offset_ != 0) {
1370 assembler->AdvanceCurrentPosition(cp_offset_);
1373 // Create a new trivial state and generate the node with that.
1375 assembler->PushBacktrack(&undo);
1377 successor->Emit(compiler, &new_state);
1379 // On backtrack we need to restore state.
1380 assembler->Bind(&undo);
1381 RestoreAffectedRegisters(assembler,
1384 registers_to_clear);
1385 if (backtrack() == NULL) {
1386 assembler->Backtrack();
1388 assembler->PopCurrentPosition();
1389 assembler->GoTo(backtrack());
1394 void NegativeSubmatchSuccess::Emit(RegExpCompiler* compiler, Trace* trace) {
1395 RegExpMacroAssembler* assembler = compiler->macro_assembler();
1397 // Omit flushing the trace. We discard the entire stack frame anyway.
1399 if (!label()->is_bound()) {
1400 // We are completely independent of the trace, since we ignore it,
1401 // so this code can be used as the generic version.
1402 assembler->Bind(label());
1405 // Throw away everything on the backtrack stack since the start
1406 // of the negative submatch and restore the character position.
1407 assembler->ReadCurrentPositionFromRegister(current_position_register_);
1408 assembler->ReadStackPointerFromRegister(stack_pointer_register_);
1409 if (clear_capture_count_ > 0) {
1410 // Clear any captures that might have been performed during the success
1411 // of the body of the negative look-ahead.
1412 int clear_capture_end = clear_capture_start_ + clear_capture_count_ - 1;
1413 assembler->ClearRegisters(clear_capture_start_, clear_capture_end);
1415 // Now that we have unwound the stack we find at the top of the stack the
1416 // backtrack that the BeginSubmatch node got.
1417 assembler->Backtrack();
1421 void EndNode::Emit(RegExpCompiler* compiler, Trace* trace) {
1422 if (!trace->is_trivial()) {
1423 trace->Flush(compiler, this);
1426 RegExpMacroAssembler* assembler = compiler->macro_assembler();
1427 if (!label()->is_bound()) {
1428 assembler->Bind(label());
1432 assembler->Succeed();
1435 assembler->GoTo(trace->backtrack());
1437 case NEGATIVE_SUBMATCH_SUCCESS:
1438 // This case is handled in a different virtual method.
1445 void GuardedAlternative::AddGuard(Guard* guard, Zone* zone) {
1446 if (guards_ == NULL)
1447 guards_ = new(zone) ZoneList<Guard*>(1, zone);
1448 guards_->Add(guard, zone);
1452 ActionNode* ActionNode::SetRegister(int reg,
1454 RegExpNode* on_success) {
1455 ActionNode* result =
1456 new(on_success->zone()) ActionNode(SET_REGISTER, on_success);
1457 result->data_.u_store_register.reg = reg;
1458 result->data_.u_store_register.value = val;
1463 ActionNode* ActionNode::IncrementRegister(int reg, RegExpNode* on_success) {
1464 ActionNode* result =
1465 new(on_success->zone()) ActionNode(INCREMENT_REGISTER, on_success);
1466 result->data_.u_increment_register.reg = reg;
1471 ActionNode* ActionNode::StorePosition(int reg,
1473 RegExpNode* on_success) {
1474 ActionNode* result =
1475 new(on_success->zone()) ActionNode(STORE_POSITION, on_success);
1476 result->data_.u_position_register.reg = reg;
1477 result->data_.u_position_register.is_capture = is_capture;
1482 ActionNode* ActionNode::ClearCaptures(Interval range,
1483 RegExpNode* on_success) {
1484 ActionNode* result =
1485 new(on_success->zone()) ActionNode(CLEAR_CAPTURES, on_success);
1486 result->data_.u_clear_captures.range_from = range.from();
1487 result->data_.u_clear_captures.range_to = range.to();
1492 ActionNode* ActionNode::BeginSubmatch(int stack_reg,
1494 RegExpNode* on_success) {
1495 ActionNode* result =
1496 new(on_success->zone()) ActionNode(BEGIN_SUBMATCH, on_success);
1497 result->data_.u_submatch.stack_pointer_register = stack_reg;
1498 result->data_.u_submatch.current_position_register = position_reg;
1503 ActionNode* ActionNode::PositiveSubmatchSuccess(int stack_reg,
1505 int clear_register_count,
1506 int clear_register_from,
1507 RegExpNode* on_success) {
1508 ActionNode* result =
1509 new(on_success->zone()) ActionNode(POSITIVE_SUBMATCH_SUCCESS, on_success);
1510 result->data_.u_submatch.stack_pointer_register = stack_reg;
1511 result->data_.u_submatch.current_position_register = position_reg;
1512 result->data_.u_submatch.clear_register_count = clear_register_count;
1513 result->data_.u_submatch.clear_register_from = clear_register_from;
1518 ActionNode* ActionNode::EmptyMatchCheck(int start_register,
1519 int repetition_register,
1520 int repetition_limit,
1521 RegExpNode* on_success) {
1522 ActionNode* result =
1523 new(on_success->zone()) ActionNode(EMPTY_MATCH_CHECK, on_success);
1524 result->data_.u_empty_match_check.start_register = start_register;
1525 result->data_.u_empty_match_check.repetition_register = repetition_register;
1526 result->data_.u_empty_match_check.repetition_limit = repetition_limit;
1531 #define DEFINE_ACCEPT(Type) \
1532 void Type##Node::Accept(NodeVisitor* visitor) { \
1533 visitor->Visit##Type(this); \
1535 FOR_EACH_NODE_TYPE(DEFINE_ACCEPT)
1536 #undef DEFINE_ACCEPT
1539 void LoopChoiceNode::Accept(NodeVisitor* visitor) {
1540 visitor->VisitLoopChoice(this);
1544 // -------------------------------------------------------------------
1548 void ChoiceNode::GenerateGuard(RegExpMacroAssembler* macro_assembler,
1551 switch (guard->op()) {
1553 DCHECK(!trace->mentions_reg(guard->reg()));
1554 macro_assembler->IfRegisterGE(guard->reg(),
1556 trace->backtrack());
1559 DCHECK(!trace->mentions_reg(guard->reg()));
1560 macro_assembler->IfRegisterLT(guard->reg(),
1562 trace->backtrack());
1568 // Returns the number of characters in the equivalence class, omitting those
1569 // that cannot occur in the source string because it is ASCII.
1570 static int GetCaseIndependentLetters(Isolate* isolate, uc16 character,
1571 bool one_byte_subject,
1572 unibrow::uchar* letters) {
1574 isolate->jsregexp_uncanonicalize()->get(character, '\0', letters);
1575 // Unibrow returns 0 or 1 for characters where case independence is
1578 letters[0] = character;
1581 if (!one_byte_subject || character <= String::kMaxOneByteCharCode) {
1585 // The standard requires that non-ASCII characters cannot have ASCII
1586 // character codes in their equivalence class.
1587 // TODO(dcarney): issue 3550 this is not actually true for Latin1 anymore,
1588 // is it? For example, \u00C5 is equivalent to \u212B.
1593 static inline bool EmitSimpleCharacter(Isolate* isolate,
1594 RegExpCompiler* compiler,
1600 RegExpMacroAssembler* assembler = compiler->macro_assembler();
1601 bool bound_checked = false;
1603 assembler->LoadCurrentCharacter(
1607 bound_checked = true;
1609 assembler->CheckNotCharacter(c, on_failure);
1610 return bound_checked;
1614 // Only emits non-letters (things that don't have case). Only used for case
1615 // independent matches.
1616 static inline bool EmitAtomNonLetter(Isolate* isolate,
1617 RegExpCompiler* compiler,
1623 RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
1624 bool one_byte = compiler->one_byte();
1625 unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
1626 int length = GetCaseIndependentLetters(isolate, c, one_byte, chars);
1628 // This can't match. Must be an one-byte subject and a non-one-byte
1629 // character. We do not need to do anything since the one-byte pass
1630 // already handled this.
1631 return false; // Bounds not checked.
1633 bool checked = false;
1634 // We handle the length > 1 case in a later pass.
1636 if (one_byte && c > String::kMaxOneByteCharCodeU) {
1637 // Can't match - see above.
1638 return false; // Bounds not checked.
1641 macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check);
1644 macro_assembler->CheckNotCharacter(c, on_failure);
1650 static bool ShortCutEmitCharacterPair(RegExpMacroAssembler* macro_assembler,
1651 bool one_byte, uc16 c1, uc16 c2,
1652 Label* on_failure) {
1655 char_mask = String::kMaxOneByteCharCode;
1657 char_mask = String::kMaxUtf16CodeUnit;
1659 uc16 exor = c1 ^ c2;
1660 // Check whether exor has only one bit set.
1661 if (((exor - 1) & exor) == 0) {
1662 // If c1 and c2 differ only by one bit.
1663 // Ecma262UnCanonicalize always gives the highest number last.
1665 uc16 mask = char_mask ^ exor;
1666 macro_assembler->CheckNotCharacterAfterAnd(c1, mask, on_failure);
1670 uc16 diff = c2 - c1;
1671 if (((diff - 1) & diff) == 0 && c1 >= diff) {
1672 // If the characters differ by 2^n but don't differ by one bit then
1673 // subtract the difference from the found character, then do the or
1674 // trick. We avoid the theoretical case where negative numbers are
1675 // involved in order to simplify code generation.
1676 uc16 mask = char_mask ^ diff;
1677 macro_assembler->CheckNotCharacterAfterMinusAnd(c1 - diff,
1687 typedef bool EmitCharacterFunction(Isolate* isolate,
1688 RegExpCompiler* compiler,
1695 // Only emits letters (things that have case). Only used for case independent
1697 static inline bool EmitAtomLetter(Isolate* isolate,
1698 RegExpCompiler* compiler,
1704 RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
1705 bool one_byte = compiler->one_byte();
1706 unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
1707 int length = GetCaseIndependentLetters(isolate, c, one_byte, chars);
1708 if (length <= 1) return false;
1709 // We may not need to check against the end of the input string
1710 // if this character lies before a character that matched.
1712 macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check);
1715 DCHECK(unibrow::Ecma262UnCanonicalize::kMaxWidth == 4);
1718 if (ShortCutEmitCharacterPair(macro_assembler, one_byte, chars[0],
1719 chars[1], on_failure)) {
1721 macro_assembler->CheckCharacter(chars[0], &ok);
1722 macro_assembler->CheckNotCharacter(chars[1], on_failure);
1723 macro_assembler->Bind(&ok);
1728 macro_assembler->CheckCharacter(chars[3], &ok);
1731 macro_assembler->CheckCharacter(chars[0], &ok);
1732 macro_assembler->CheckCharacter(chars[1], &ok);
1733 macro_assembler->CheckNotCharacter(chars[2], on_failure);
1734 macro_assembler->Bind(&ok);
1744 static void EmitBoundaryTest(RegExpMacroAssembler* masm,
1746 Label* fall_through,
1747 Label* above_or_equal,
1749 if (below != fall_through) {
1750 masm->CheckCharacterLT(border, below);
1751 if (above_or_equal != fall_through) masm->GoTo(above_or_equal);
1753 masm->CheckCharacterGT(border - 1, above_or_equal);
1758 static void EmitDoubleBoundaryTest(RegExpMacroAssembler* masm,
1761 Label* fall_through,
1763 Label* out_of_range) {
1764 if (in_range == fall_through) {
1765 if (first == last) {
1766 masm->CheckNotCharacter(first, out_of_range);
1768 masm->CheckCharacterNotInRange(first, last, out_of_range);
1771 if (first == last) {
1772 masm->CheckCharacter(first, in_range);
1774 masm->CheckCharacterInRange(first, last, in_range);
1776 if (out_of_range != fall_through) masm->GoTo(out_of_range);
1781 // even_label is for ranges[i] to ranges[i + 1] where i - start_index is even.
1782 // odd_label is for ranges[i] to ranges[i + 1] where i - start_index is odd.
1783 static void EmitUseLookupTable(
1784 RegExpMacroAssembler* masm,
1785 ZoneList<int>* ranges,
1789 Label* fall_through,
1792 static const int kSize = RegExpMacroAssembler::kTableSize;
1793 static const int kMask = RegExpMacroAssembler::kTableMask;
1795 int base = (min_char & ~kMask);
1798 // Assert that everything is on one kTableSize page.
1799 for (int i = start_index; i <= end_index; i++) {
1800 DCHECK_EQ(ranges->at(i) & ~kMask, base);
1802 DCHECK(start_index == 0 || (ranges->at(start_index - 1) & ~kMask) <= base);
1806 Label* on_bit_clear;
1808 if (even_label == fall_through) {
1809 on_bit_set = odd_label;
1810 on_bit_clear = even_label;
1813 on_bit_set = even_label;
1814 on_bit_clear = odd_label;
1817 for (int i = 0; i < (ranges->at(start_index) & kMask) && i < kSize; i++) {
1822 for (int i = start_index; i < end_index; i++) {
1823 for (j = (ranges->at(i) & kMask); j < (ranges->at(i + 1) & kMask); j++) {
1828 for (int i = j; i < kSize; i++) {
1831 Factory* factory = masm->isolate()->factory();
1832 // TODO(erikcorry): Cache these.
1833 Handle<ByteArray> ba = factory->NewByteArray(kSize, TENURED);
1834 for (int i = 0; i < kSize; i++) {
1835 ba->set(i, templ[i]);
1837 masm->CheckBitInTable(ba, on_bit_set);
1838 if (on_bit_clear != fall_through) masm->GoTo(on_bit_clear);
1842 static void CutOutRange(RegExpMacroAssembler* masm,
1843 ZoneList<int>* ranges,
1849 bool odd = (((cut_index - start_index) & 1) == 1);
1850 Label* in_range_label = odd ? odd_label : even_label;
1852 EmitDoubleBoundaryTest(masm,
1853 ranges->at(cut_index),
1854 ranges->at(cut_index + 1) - 1,
1858 DCHECK(!dummy.is_linked());
1859 // Cut out the single range by rewriting the array. This creates a new
1860 // range that is a merger of the two ranges on either side of the one we
1861 // are cutting out. The oddity of the labels is preserved.
1862 for (int j = cut_index; j > start_index; j--) {
1863 ranges->at(j) = ranges->at(j - 1);
1865 for (int j = cut_index + 1; j < end_index; j++) {
1866 ranges->at(j) = ranges->at(j + 1);
1871 // Unicode case. Split the search space into kSize spaces that are handled
1873 static void SplitSearchSpace(ZoneList<int>* ranges,
1876 int* new_start_index,
1879 static const int kSize = RegExpMacroAssembler::kTableSize;
1880 static const int kMask = RegExpMacroAssembler::kTableMask;
1882 int first = ranges->at(start_index);
1883 int last = ranges->at(end_index) - 1;
1885 *new_start_index = start_index;
1886 *border = (ranges->at(start_index) & ~kMask) + kSize;
1887 while (*new_start_index < end_index) {
1888 if (ranges->at(*new_start_index) > *border) break;
1889 (*new_start_index)++;
1891 // new_start_index is the index of the first edge that is beyond the
1892 // current kSize space.
1894 // For very large search spaces we do a binary chop search of the non-Latin1
1895 // space instead of just going to the end of the current kSize space. The
1896 // heuristics are complicated a little by the fact that any 128-character
1897 // encoding space can be quickly tested with a table lookup, so we don't
1898 // wish to do binary chop search at a smaller granularity than that. A
1899 // 128-character space can take up a lot of space in the ranges array if,
1900 // for example, we only want to match every second character (eg. the lower
1901 // case characters on some Unicode pages).
1902 int binary_chop_index = (end_index + start_index) / 2;
1903 // The first test ensures that we get to the code that handles the Latin1
1904 // range with a single not-taken branch, speeding up this important
1905 // character range (even non-Latin1 charset-based text has spaces and
1907 if (*border - 1 > String::kMaxOneByteCharCode && // Latin1 case.
1908 end_index - start_index > (*new_start_index - start_index) * 2 &&
1909 last - first > kSize * 2 && binary_chop_index > *new_start_index &&
1910 ranges->at(binary_chop_index) >= first + 2 * kSize) {
1911 int scan_forward_for_section_border = binary_chop_index;;
1912 int new_border = (ranges->at(binary_chop_index) | kMask) + 1;
1914 while (scan_forward_for_section_border < end_index) {
1915 if (ranges->at(scan_forward_for_section_border) > new_border) {
1916 *new_start_index = scan_forward_for_section_border;
1917 *border = new_border;
1920 scan_forward_for_section_border++;
1924 DCHECK(*new_start_index > start_index);
1925 *new_end_index = *new_start_index - 1;
1926 if (ranges->at(*new_end_index) == *border) {
1929 if (*border >= ranges->at(end_index)) {
1930 *border = ranges->at(end_index);
1931 *new_start_index = end_index; // Won't be used.
1932 *new_end_index = end_index - 1;
1937 // Gets a series of segment boundaries representing a character class. If the
1938 // character is in the range between an even and an odd boundary (counting from
1939 // start_index) then go to even_label, otherwise go to odd_label. We already
1940 // know that the character is in the range of min_char to max_char inclusive.
1941 // Either label can be NULL indicating backtracking. Either label can also be
1942 // equal to the fall_through label.
1943 static void GenerateBranches(RegExpMacroAssembler* masm,
1944 ZoneList<int>* ranges,
1949 Label* fall_through,
1952 int first = ranges->at(start_index);
1953 int last = ranges->at(end_index) - 1;
1955 DCHECK_LT(min_char, first);
1957 // Just need to test if the character is before or on-or-after
1958 // a particular character.
1959 if (start_index == end_index) {
1960 EmitBoundaryTest(masm, first, fall_through, even_label, odd_label);
1964 // Another almost trivial case: There is one interval in the middle that is
1965 // different from the end intervals.
1966 if (start_index + 1 == end_index) {
1967 EmitDoubleBoundaryTest(
1968 masm, first, last, fall_through, even_label, odd_label);
1972 // It's not worth using table lookup if there are very few intervals in the
1974 if (end_index - start_index <= 6) {
1975 // It is faster to test for individual characters, so we look for those
1976 // first, then try arbitrary ranges in the second round.
1977 static int kNoCutIndex = -1;
1978 int cut = kNoCutIndex;
1979 for (int i = start_index; i < end_index; i++) {
1980 if (ranges->at(i) == ranges->at(i + 1) - 1) {
1985 if (cut == kNoCutIndex) cut = start_index;
1987 masm, ranges, start_index, end_index, cut, even_label, odd_label);
1988 DCHECK_GE(end_index - start_index, 2);
1989 GenerateBranches(masm,
2001 // If there are a lot of intervals in the regexp, then we will use tables to
2002 // determine whether the character is inside or outside the character class.
2003 static const int kBits = RegExpMacroAssembler::kTableSizeBits;
2005 if ((max_char >> kBits) == (min_char >> kBits)) {
2006 EmitUseLookupTable(masm,
2017 if ((min_char >> kBits) != (first >> kBits)) {
2018 masm->CheckCharacterLT(first, odd_label);
2019 GenerateBranches(masm,
2031 int new_start_index = 0;
2032 int new_end_index = 0;
2035 SplitSearchSpace(ranges,
2043 Label* above = &handle_rest;
2044 if (border == last + 1) {
2045 // We didn't find any section that started after the limit, so everything
2046 // above the border is one of the terminal labels.
2047 above = (end_index & 1) != (start_index & 1) ? odd_label : even_label;
2048 DCHECK(new_end_index == end_index - 1);
2051 DCHECK_LE(start_index, new_end_index);
2052 DCHECK_LE(new_start_index, end_index);
2053 DCHECK_LT(start_index, new_start_index);
2054 DCHECK_LT(new_end_index, end_index);
2055 DCHECK(new_end_index + 1 == new_start_index ||
2056 (new_end_index + 2 == new_start_index &&
2057 border == ranges->at(new_end_index + 1)));
2058 DCHECK_LT(min_char, border - 1);
2059 DCHECK_LT(border, max_char);
2060 DCHECK_LT(ranges->at(new_end_index), border);
2061 DCHECK(border < ranges->at(new_start_index) ||
2062 (border == ranges->at(new_start_index) &&
2063 new_start_index == end_index &&
2064 new_end_index == end_index - 1 &&
2065 border == last + 1));
2066 DCHECK(new_start_index == 0 || border >= ranges->at(new_start_index - 1));
2068 masm->CheckCharacterGT(border - 1, above);
2070 GenerateBranches(masm,
2079 if (handle_rest.is_linked()) {
2080 masm->Bind(&handle_rest);
2081 bool flip = (new_start_index & 1) != (start_index & 1);
2082 GenerateBranches(masm,
2089 flip ? odd_label : even_label,
2090 flip ? even_label : odd_label);
2095 static void EmitCharClass(RegExpMacroAssembler* macro_assembler,
2096 RegExpCharacterClass* cc, bool one_byte,
2097 Label* on_failure, int cp_offset, bool check_offset,
2098 bool preloaded, Zone* zone) {
2099 ZoneList<CharacterRange>* ranges = cc->ranges(zone);
2100 if (!CharacterRange::IsCanonical(ranges)) {
2101 CharacterRange::Canonicalize(ranges);
2106 max_char = String::kMaxOneByteCharCode;
2108 max_char = String::kMaxUtf16CodeUnit;
2111 int range_count = ranges->length();
2113 int last_valid_range = range_count - 1;
2114 while (last_valid_range >= 0) {
2115 CharacterRange& range = ranges->at(last_valid_range);
2116 if (range.from() <= max_char) {
2122 if (last_valid_range < 0) {
2123 if (!cc->is_negated()) {
2124 macro_assembler->GoTo(on_failure);
2127 macro_assembler->CheckPosition(cp_offset, on_failure);
2132 if (last_valid_range == 0 &&
2133 ranges->at(0).IsEverything(max_char)) {
2134 if (cc->is_negated()) {
2135 macro_assembler->GoTo(on_failure);
2137 // This is a common case hit by non-anchored expressions.
2139 macro_assembler->CheckPosition(cp_offset, on_failure);
2144 if (last_valid_range == 0 &&
2145 !cc->is_negated() &&
2146 ranges->at(0).IsEverything(max_char)) {
2147 // This is a common case hit by non-anchored expressions.
2149 macro_assembler->CheckPosition(cp_offset, on_failure);
2155 macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check_offset);
2158 if (cc->is_standard(zone) &&
2159 macro_assembler->CheckSpecialCharacterClass(cc->standard_type(),
2165 // A new list with ascending entries. Each entry is a code unit
2166 // where there is a boundary between code units that are part of
2167 // the class and code units that are not. Normally we insert an
2168 // entry at zero which goes to the failure label, but if there
2169 // was already one there we fall through for success on that entry.
2170 // Subsequent entries have alternating meaning (success/failure).
2171 ZoneList<int>* range_boundaries =
2172 new(zone) ZoneList<int>(last_valid_range, zone);
2174 bool zeroth_entry_is_failure = !cc->is_negated();
2176 for (int i = 0; i <= last_valid_range; i++) {
2177 CharacterRange& range = ranges->at(i);
2178 if (range.from() == 0) {
2180 zeroth_entry_is_failure = !zeroth_entry_is_failure;
2182 range_boundaries->Add(range.from(), zone);
2184 range_boundaries->Add(range.to() + 1, zone);
2186 int end_index = range_boundaries->length() - 1;
2187 if (range_boundaries->at(end_index) > max_char) {
2192 GenerateBranches(macro_assembler,
2199 zeroth_entry_is_failure ? &fall_through : on_failure,
2200 zeroth_entry_is_failure ? on_failure : &fall_through);
2201 macro_assembler->Bind(&fall_through);
2205 RegExpNode::~RegExpNode() {
2209 RegExpNode::LimitResult RegExpNode::LimitVersions(RegExpCompiler* compiler,
2211 // If we are generating a greedy loop then don't stop and don't reuse code.
2212 if (trace->stop_node() != NULL) {
2216 RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
2217 if (trace->is_trivial()) {
2218 if (label_.is_bound()) {
2219 // We are being asked to generate a generic version, but that's already
2220 // been done so just go to it.
2221 macro_assembler->GoTo(&label_);
2224 if (compiler->recursion_depth() >= RegExpCompiler::kMaxRecursion) {
2225 // To avoid too deep recursion we push the node to the work queue and just
2226 // generate a goto here.
2227 compiler->AddWork(this);
2228 macro_assembler->GoTo(&label_);
2231 // Generate generic version of the node and bind the label for later use.
2232 macro_assembler->Bind(&label_);
2236 // We are being asked to make a non-generic version. Keep track of how many
2237 // non-generic versions we generate so as not to overdo it.
2239 if (compiler->optimize() && trace_count_ < kMaxCopiesCodeGenerated &&
2240 compiler->recursion_depth() <= RegExpCompiler::kMaxRecursion) {
2244 // If we get here code has been generated for this node too many times or
2245 // recursion is too deep. Time to switch to a generic version. The code for
2246 // generic versions above can handle deep recursion properly.
2247 trace->Flush(compiler, this);
2252 int ActionNode::EatsAtLeast(int still_to_find,
2254 bool not_at_start) {
2255 if (budget <= 0) return 0;
2256 if (action_type_ == POSITIVE_SUBMATCH_SUCCESS) return 0; // Rewinds input!
2257 return on_success()->EatsAtLeast(still_to_find,
2263 void ActionNode::FillInBMInfo(int offset,
2265 BoyerMooreLookahead* bm,
2266 bool not_at_start) {
2267 if (action_type_ == BEGIN_SUBMATCH) {
2268 bm->SetRest(offset);
2269 } else if (action_type_ != POSITIVE_SUBMATCH_SUCCESS) {
2270 on_success()->FillInBMInfo(offset, budget - 1, bm, not_at_start);
2272 SaveBMInfo(bm, not_at_start, offset);
2276 int AssertionNode::EatsAtLeast(int still_to_find,
2278 bool not_at_start) {
2279 if (budget <= 0) return 0;
2280 // If we know we are not at the start and we are asked "how many characters
2281 // will you match if you succeed?" then we can answer anything since false
2282 // implies false. So lets just return the max answer (still_to_find) since
2283 // that won't prevent us from preloading a lot of characters for the other
2284 // branches in the node graph.
2285 if (assertion_type() == AT_START && not_at_start) return still_to_find;
2286 return on_success()->EatsAtLeast(still_to_find,
2292 void AssertionNode::FillInBMInfo(int offset,
2294 BoyerMooreLookahead* bm,
2295 bool not_at_start) {
2296 // Match the behaviour of EatsAtLeast on this node.
2297 if (assertion_type() == AT_START && not_at_start) return;
2298 on_success()->FillInBMInfo(offset, budget - 1, bm, not_at_start);
2299 SaveBMInfo(bm, not_at_start, offset);
2303 int BackReferenceNode::EatsAtLeast(int still_to_find,
2305 bool not_at_start) {
2306 if (budget <= 0) return 0;
2307 return on_success()->EatsAtLeast(still_to_find,
2313 int TextNode::EatsAtLeast(int still_to_find,
2315 bool not_at_start) {
2316 int answer = Length();
2317 if (answer >= still_to_find) return answer;
2318 if (budget <= 0) return answer;
2319 // We are not at start after this node so we set the last argument to 'true'.
2320 return answer + on_success()->EatsAtLeast(still_to_find - answer,
2326 int NegativeLookaheadChoiceNode::EatsAtLeast(int still_to_find,
2328 bool not_at_start) {
2329 if (budget <= 0) return 0;
2330 // Alternative 0 is the negative lookahead, alternative 1 is what comes
2332 RegExpNode* node = alternatives_->at(1).node();
2333 return node->EatsAtLeast(still_to_find, budget - 1, not_at_start);
2337 void NegativeLookaheadChoiceNode::GetQuickCheckDetails(
2338 QuickCheckDetails* details,
2339 RegExpCompiler* compiler,
2341 bool not_at_start) {
2342 // Alternative 0 is the negative lookahead, alternative 1 is what comes
2344 RegExpNode* node = alternatives_->at(1).node();
2345 return node->GetQuickCheckDetails(details, compiler, filled_in, not_at_start);
2349 int ChoiceNode::EatsAtLeastHelper(int still_to_find,
2351 RegExpNode* ignore_this_node,
2352 bool not_at_start) {
2353 if (budget <= 0) return 0;
2355 int choice_count = alternatives_->length();
2356 budget = (budget - 1) / choice_count;
2357 for (int i = 0; i < choice_count; i++) {
2358 RegExpNode* node = alternatives_->at(i).node();
2359 if (node == ignore_this_node) continue;
2360 int node_eats_at_least =
2361 node->EatsAtLeast(still_to_find, budget, not_at_start);
2362 if (node_eats_at_least < min) min = node_eats_at_least;
2363 if (min == 0) return 0;
2369 int LoopChoiceNode::EatsAtLeast(int still_to_find,
2371 bool not_at_start) {
2372 return EatsAtLeastHelper(still_to_find,
2379 int ChoiceNode::EatsAtLeast(int still_to_find,
2381 bool not_at_start) {
2382 return EatsAtLeastHelper(still_to_find,
2389 // Takes the left-most 1-bit and smears it out, setting all bits to its right.
2390 static inline uint32_t SmearBitsRight(uint32_t v) {
2400 bool QuickCheckDetails::Rationalize(bool asc) {
2401 bool found_useful_op = false;
2404 char_mask = String::kMaxOneByteCharCode;
2406 char_mask = String::kMaxUtf16CodeUnit;
2411 for (int i = 0; i < characters_; i++) {
2412 Position* pos = &positions_[i];
2413 if ((pos->mask & String::kMaxOneByteCharCode) != 0) {
2414 found_useful_op = true;
2416 mask_ |= (pos->mask & char_mask) << char_shift;
2417 value_ |= (pos->value & char_mask) << char_shift;
2418 char_shift += asc ? 8 : 16;
2420 return found_useful_op;
2424 bool RegExpNode::EmitQuickCheck(RegExpCompiler* compiler,
2425 Trace* bounds_check_trace,
2427 bool preload_has_checked_bounds,
2428 Label* on_possible_success,
2429 QuickCheckDetails* details,
2430 bool fall_through_on_failure) {
2431 if (details->characters() == 0) return false;
2432 GetQuickCheckDetails(
2433 details, compiler, 0, trace->at_start() == Trace::FALSE_VALUE);
2434 if (details->cannot_match()) return false;
2435 if (!details->Rationalize(compiler->one_byte())) return false;
2436 DCHECK(details->characters() == 1 ||
2437 compiler->macro_assembler()->CanReadUnaligned());
2438 uint32_t mask = details->mask();
2439 uint32_t value = details->value();
2441 RegExpMacroAssembler* assembler = compiler->macro_assembler();
2443 if (trace->characters_preloaded() != details->characters()) {
2444 DCHECK(trace->cp_offset() == bounds_check_trace->cp_offset());
2445 // We are attempting to preload the minimum number of characters
2446 // any choice would eat, so if the bounds check fails, then none of the
2447 // choices can succeed, so we can just immediately backtrack, rather
2448 // than go to the next choice.
2449 assembler->LoadCurrentCharacter(trace->cp_offset(),
2450 bounds_check_trace->backtrack(),
2451 !preload_has_checked_bounds,
2452 details->characters());
2456 bool need_mask = true;
2458 if (details->characters() == 1) {
2459 // If number of characters preloaded is 1 then we used a byte or 16 bit
2460 // load so the value is already masked down.
2462 if (compiler->one_byte()) {
2463 char_mask = String::kMaxOneByteCharCode;
2465 char_mask = String::kMaxUtf16CodeUnit;
2467 if ((mask & char_mask) == char_mask) need_mask = false;
2470 // For 2-character preloads in one-byte mode or 1-character preloads in
2471 // two-byte mode we also use a 16 bit load with zero extend.
2472 if (details->characters() == 2 && compiler->one_byte()) {
2473 if ((mask & 0xffff) == 0xffff) need_mask = false;
2474 } else if (details->characters() == 1 && !compiler->one_byte()) {
2475 if ((mask & 0xffff) == 0xffff) need_mask = false;
2477 if (mask == 0xffffffff) need_mask = false;
2481 if (fall_through_on_failure) {
2483 assembler->CheckCharacterAfterAnd(value, mask, on_possible_success);
2485 assembler->CheckCharacter(value, on_possible_success);
2489 assembler->CheckNotCharacterAfterAnd(value, mask, trace->backtrack());
2491 assembler->CheckNotCharacter(value, trace->backtrack());
2498 // Here is the meat of GetQuickCheckDetails (see also the comment on the
2499 // super-class in the .h file).
2501 // We iterate along the text object, building up for each character a
2502 // mask and value that can be used to test for a quick failure to match.
2503 // The masks and values for the positions will be combined into a single
2504 // machine word for the current character width in order to be used in
2505 // generating a quick check.
2506 void TextNode::GetQuickCheckDetails(QuickCheckDetails* details,
2507 RegExpCompiler* compiler,
2508 int characters_filled_in,
2509 bool not_at_start) {
2510 Isolate* isolate = compiler->macro_assembler()->isolate();
2511 DCHECK(characters_filled_in < details->characters());
2512 int characters = details->characters();
2514 if (compiler->one_byte()) {
2515 char_mask = String::kMaxOneByteCharCode;
2517 char_mask = String::kMaxUtf16CodeUnit;
2519 for (int k = 0; k < elms_->length(); k++) {
2520 TextElement elm = elms_->at(k);
2521 if (elm.text_type() == TextElement::ATOM) {
2522 Vector<const uc16> quarks = elm.atom()->data();
2523 for (int i = 0; i < characters && i < quarks.length(); i++) {
2524 QuickCheckDetails::Position* pos =
2525 details->positions(characters_filled_in);
2527 if (c > char_mask) {
2528 // If we expect a non-Latin1 character from an one-byte string,
2529 // there is no way we can match. Not even case-independent
2530 // matching can turn an Latin1 character into non-Latin1 or
2532 // TODO(dcarney): issue 3550. Verify that this works as expected.
2533 // For example, \u0178 is uppercase of \u00ff (y-umlaut).
2534 details->set_cannot_match();
2535 pos->determines_perfectly = false;
2538 if (compiler->ignore_case()) {
2539 unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
2540 int length = GetCaseIndependentLetters(isolate, c,
2541 compiler->one_byte(), chars);
2542 DCHECK(length != 0); // Can only happen if c > char_mask (see above).
2544 // This letter has no case equivalents, so it's nice and simple
2545 // and the mask-compare will determine definitely whether we have
2546 // a match at this character position.
2547 pos->mask = char_mask;
2549 pos->determines_perfectly = true;
2551 uint32_t common_bits = char_mask;
2552 uint32_t bits = chars[0];
2553 for (int j = 1; j < length; j++) {
2554 uint32_t differing_bits = ((chars[j] & common_bits) ^ bits);
2555 common_bits ^= differing_bits;
2556 bits &= common_bits;
2558 // If length is 2 and common bits has only one zero in it then
2559 // our mask and compare instruction will determine definitely
2560 // whether we have a match at this character position. Otherwise
2561 // it can only be an approximate check.
2562 uint32_t one_zero = (common_bits | ~char_mask);
2563 if (length == 2 && ((~one_zero) & ((~one_zero) - 1)) == 0) {
2564 pos->determines_perfectly = true;
2566 pos->mask = common_bits;
2570 // Don't ignore case. Nice simple case where the mask-compare will
2571 // determine definitely whether we have a match at this character
2573 pos->mask = char_mask;
2575 pos->determines_perfectly = true;
2577 characters_filled_in++;
2578 DCHECK(characters_filled_in <= details->characters());
2579 if (characters_filled_in == details->characters()) {
2584 QuickCheckDetails::Position* pos =
2585 details->positions(characters_filled_in);
2586 RegExpCharacterClass* tree = elm.char_class();
2587 ZoneList<CharacterRange>* ranges = tree->ranges(zone());
2588 if (tree->is_negated()) {
2589 // A quick check uses multi-character mask and compare. There is no
2590 // useful way to incorporate a negative char class into this scheme
2591 // so we just conservatively create a mask and value that will always
2596 int first_range = 0;
2597 while (ranges->at(first_range).from() > char_mask) {
2599 if (first_range == ranges->length()) {
2600 details->set_cannot_match();
2601 pos->determines_perfectly = false;
2605 CharacterRange range = ranges->at(first_range);
2606 uc16 from = range.from();
2607 uc16 to = range.to();
2608 if (to > char_mask) {
2611 uint32_t differing_bits = (from ^ to);
2612 // A mask and compare is only perfect if the differing bits form a
2613 // number like 00011111 with one single block of trailing 1s.
2614 if ((differing_bits & (differing_bits + 1)) == 0 &&
2615 from + differing_bits == to) {
2616 pos->determines_perfectly = true;
2618 uint32_t common_bits = ~SmearBitsRight(differing_bits);
2619 uint32_t bits = (from & common_bits);
2620 for (int i = first_range + 1; i < ranges->length(); i++) {
2621 CharacterRange range = ranges->at(i);
2622 uc16 from = range.from();
2623 uc16 to = range.to();
2624 if (from > char_mask) continue;
2625 if (to > char_mask) to = char_mask;
2626 // Here we are combining more ranges into the mask and compare
2627 // value. With each new range the mask becomes more sparse and
2628 // so the chances of a false positive rise. A character class
2629 // with multiple ranges is assumed never to be equivalent to a
2630 // mask and compare operation.
2631 pos->determines_perfectly = false;
2632 uint32_t new_common_bits = (from ^ to);
2633 new_common_bits = ~SmearBitsRight(new_common_bits);
2634 common_bits &= new_common_bits;
2635 bits &= new_common_bits;
2636 uint32_t differing_bits = (from & common_bits) ^ bits;
2637 common_bits ^= differing_bits;
2638 bits &= common_bits;
2640 pos->mask = common_bits;
2643 characters_filled_in++;
2644 DCHECK(characters_filled_in <= details->characters());
2645 if (characters_filled_in == details->characters()) {
2650 DCHECK(characters_filled_in != details->characters());
2651 if (!details->cannot_match()) {
2652 on_success()-> GetQuickCheckDetails(details,
2654 characters_filled_in,
2660 void QuickCheckDetails::Clear() {
2661 for (int i = 0; i < characters_; i++) {
2662 positions_[i].mask = 0;
2663 positions_[i].value = 0;
2664 positions_[i].determines_perfectly = false;
2670 void QuickCheckDetails::Advance(int by, bool one_byte) {
2672 if (by >= characters_) {
2676 for (int i = 0; i < characters_ - by; i++) {
2677 positions_[i] = positions_[by + i];
2679 for (int i = characters_ - by; i < characters_; i++) {
2680 positions_[i].mask = 0;
2681 positions_[i].value = 0;
2682 positions_[i].determines_perfectly = false;
2685 // We could change mask_ and value_ here but we would never advance unless
2686 // they had already been used in a check and they won't be used again because
2687 // it would gain us nothing. So there's no point.
2691 void QuickCheckDetails::Merge(QuickCheckDetails* other, int from_index) {
2692 DCHECK(characters_ == other->characters_);
2693 if (other->cannot_match_) {
2696 if (cannot_match_) {
2700 for (int i = from_index; i < characters_; i++) {
2701 QuickCheckDetails::Position* pos = positions(i);
2702 QuickCheckDetails::Position* other_pos = other->positions(i);
2703 if (pos->mask != other_pos->mask ||
2704 pos->value != other_pos->value ||
2705 !other_pos->determines_perfectly) {
2706 // Our mask-compare operation will be approximate unless we have the
2707 // exact same operation on both sides of the alternation.
2708 pos->determines_perfectly = false;
2710 pos->mask &= other_pos->mask;
2711 pos->value &= pos->mask;
2712 other_pos->value &= pos->mask;
2713 uc16 differing_bits = (pos->value ^ other_pos->value);
2714 pos->mask &= ~differing_bits;
2715 pos->value &= pos->mask;
2722 explicit VisitMarker(NodeInfo* info) : info_(info) {
2723 DCHECK(!info->visited);
2724 info->visited = true;
2727 info_->visited = false;
2734 RegExpNode* SeqRegExpNode::FilterOneByte(int depth, bool ignore_case) {
2735 if (info()->replacement_calculated) return replacement();
2736 if (depth < 0) return this;
2737 DCHECK(!info()->visited);
2738 VisitMarker marker(info());
2739 return FilterSuccessor(depth - 1, ignore_case);
2743 RegExpNode* SeqRegExpNode::FilterSuccessor(int depth, bool ignore_case) {
2744 RegExpNode* next = on_success_->FilterOneByte(depth - 1, ignore_case);
2745 if (next == NULL) return set_replacement(NULL);
2747 return set_replacement(this);
2751 // We need to check for the following characters: 0x39c 0x3bc 0x178.
2752 static inline bool RangeContainsLatin1Equivalents(CharacterRange range) {
2753 // TODO(dcarney): this could be a lot more efficient.
2754 return range.Contains(0x39c) ||
2755 range.Contains(0x3bc) || range.Contains(0x178);
2759 static bool RangesContainLatin1Equivalents(ZoneList<CharacterRange>* ranges) {
2760 for (int i = 0; i < ranges->length(); i++) {
2761 // TODO(dcarney): this could be a lot more efficient.
2762 if (RangeContainsLatin1Equivalents(ranges->at(i))) return true;
2768 RegExpNode* TextNode::FilterOneByte(int depth, bool ignore_case) {
2769 if (info()->replacement_calculated) return replacement();
2770 if (depth < 0) return this;
2771 DCHECK(!info()->visited);
2772 VisitMarker marker(info());
2773 int element_count = elms_->length();
2774 for (int i = 0; i < element_count; i++) {
2775 TextElement elm = elms_->at(i);
2776 if (elm.text_type() == TextElement::ATOM) {
2777 Vector<const uc16> quarks = elm.atom()->data();
2778 for (int j = 0; j < quarks.length(); j++) {
2779 uint16_t c = quarks[j];
2780 if (c <= String::kMaxOneByteCharCode) continue;
2781 if (!ignore_case) return set_replacement(NULL);
2782 // Here, we need to check for characters whose upper and lower cases
2783 // are outside the Latin-1 range.
2784 uint16_t converted = unibrow::Latin1::ConvertNonLatin1ToLatin1(c);
2785 // Character is outside Latin-1 completely
2786 if (converted == 0) return set_replacement(NULL);
2787 // Convert quark to Latin-1 in place.
2788 uint16_t* copy = const_cast<uint16_t*>(quarks.start());
2789 copy[j] = converted;
2792 DCHECK(elm.text_type() == TextElement::CHAR_CLASS);
2793 RegExpCharacterClass* cc = elm.char_class();
2794 ZoneList<CharacterRange>* ranges = cc->ranges(zone());
2795 if (!CharacterRange::IsCanonical(ranges)) {
2796 CharacterRange::Canonicalize(ranges);
2798 // Now they are in order so we only need to look at the first.
2799 int range_count = ranges->length();
2800 if (cc->is_negated()) {
2801 if (range_count != 0 &&
2802 ranges->at(0).from() == 0 &&
2803 ranges->at(0).to() >= String::kMaxOneByteCharCode) {
2804 // This will be handled in a later filter.
2805 if (ignore_case && RangesContainLatin1Equivalents(ranges)) continue;
2806 return set_replacement(NULL);
2809 if (range_count == 0 ||
2810 ranges->at(0).from() > String::kMaxOneByteCharCode) {
2811 // This will be handled in a later filter.
2812 if (ignore_case && RangesContainLatin1Equivalents(ranges)) continue;
2813 return set_replacement(NULL);
2818 return FilterSuccessor(depth - 1, ignore_case);
2822 RegExpNode* LoopChoiceNode::FilterOneByte(int depth, bool ignore_case) {
2823 if (info()->replacement_calculated) return replacement();
2824 if (depth < 0) return this;
2825 if (info()->visited) return this;
2827 VisitMarker marker(info());
2829 RegExpNode* continue_replacement =
2830 continue_node_->FilterOneByte(depth - 1, ignore_case);
2831 // If we can't continue after the loop then there is no sense in doing the
2833 if (continue_replacement == NULL) return set_replacement(NULL);
2836 return ChoiceNode::FilterOneByte(depth - 1, ignore_case);
2840 RegExpNode* ChoiceNode::FilterOneByte(int depth, bool ignore_case) {
2841 if (info()->replacement_calculated) return replacement();
2842 if (depth < 0) return this;
2843 if (info()->visited) return this;
2844 VisitMarker marker(info());
2845 int choice_count = alternatives_->length();
2847 for (int i = 0; i < choice_count; i++) {
2848 GuardedAlternative alternative = alternatives_->at(i);
2849 if (alternative.guards() != NULL && alternative.guards()->length() != 0) {
2850 set_replacement(this);
2856 RegExpNode* survivor = NULL;
2857 for (int i = 0; i < choice_count; i++) {
2858 GuardedAlternative alternative = alternatives_->at(i);
2859 RegExpNode* replacement =
2860 alternative.node()->FilterOneByte(depth - 1, ignore_case);
2861 DCHECK(replacement != this); // No missing EMPTY_MATCH_CHECK.
2862 if (replacement != NULL) {
2863 alternatives_->at(i).set_node(replacement);
2865 survivor = replacement;
2868 if (surviving < 2) return set_replacement(survivor);
2870 set_replacement(this);
2871 if (surviving == choice_count) {
2874 // Only some of the nodes survived the filtering. We need to rebuild the
2875 // alternatives list.
2876 ZoneList<GuardedAlternative>* new_alternatives =
2877 new(zone()) ZoneList<GuardedAlternative>(surviving, zone());
2878 for (int i = 0; i < choice_count; i++) {
2879 RegExpNode* replacement =
2880 alternatives_->at(i).node()->FilterOneByte(depth - 1, ignore_case);
2881 if (replacement != NULL) {
2882 alternatives_->at(i).set_node(replacement);
2883 new_alternatives->Add(alternatives_->at(i), zone());
2886 alternatives_ = new_alternatives;
2891 RegExpNode* NegativeLookaheadChoiceNode::FilterOneByte(int depth,
2893 if (info()->replacement_calculated) return replacement();
2894 if (depth < 0) return this;
2895 if (info()->visited) return this;
2896 VisitMarker marker(info());
2897 // Alternative 0 is the negative lookahead, alternative 1 is what comes
2899 RegExpNode* node = alternatives_->at(1).node();
2900 RegExpNode* replacement = node->FilterOneByte(depth - 1, ignore_case);
2901 if (replacement == NULL) return set_replacement(NULL);
2902 alternatives_->at(1).set_node(replacement);
2904 RegExpNode* neg_node = alternatives_->at(0).node();
2905 RegExpNode* neg_replacement = neg_node->FilterOneByte(depth - 1, ignore_case);
2906 // If the negative lookahead is always going to fail then
2907 // we don't need to check it.
2908 if (neg_replacement == NULL) return set_replacement(replacement);
2909 alternatives_->at(0).set_node(neg_replacement);
2910 return set_replacement(this);
2914 void LoopChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details,
2915 RegExpCompiler* compiler,
2916 int characters_filled_in,
2917 bool not_at_start) {
2918 if (body_can_be_zero_length_ || info()->visited) return;
2919 VisitMarker marker(info());
2920 return ChoiceNode::GetQuickCheckDetails(details,
2922 characters_filled_in,
2927 void LoopChoiceNode::FillInBMInfo(int offset,
2929 BoyerMooreLookahead* bm,
2930 bool not_at_start) {
2931 if (body_can_be_zero_length_ || budget <= 0) {
2932 bm->SetRest(offset);
2933 SaveBMInfo(bm, not_at_start, offset);
2936 ChoiceNode::FillInBMInfo(offset, budget - 1, bm, not_at_start);
2937 SaveBMInfo(bm, not_at_start, offset);
2941 void ChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details,
2942 RegExpCompiler* compiler,
2943 int characters_filled_in,
2944 bool not_at_start) {
2945 not_at_start = (not_at_start || not_at_start_);
2946 int choice_count = alternatives_->length();
2947 DCHECK(choice_count > 0);
2948 alternatives_->at(0).node()->GetQuickCheckDetails(details,
2950 characters_filled_in,
2952 for (int i = 1; i < choice_count; i++) {
2953 QuickCheckDetails new_details(details->characters());
2954 RegExpNode* node = alternatives_->at(i).node();
2955 node->GetQuickCheckDetails(&new_details, compiler,
2956 characters_filled_in,
2958 // Here we merge the quick match details of the two branches.
2959 details->Merge(&new_details, characters_filled_in);
2964 // Check for [0-9A-Z_a-z].
2965 static void EmitWordCheck(RegExpMacroAssembler* assembler,
2968 bool fall_through_on_word) {
2969 if (assembler->CheckSpecialCharacterClass(
2970 fall_through_on_word ? 'w' : 'W',
2971 fall_through_on_word ? non_word : word)) {
2972 // Optimized implementation available.
2975 assembler->CheckCharacterGT('z', non_word);
2976 assembler->CheckCharacterLT('0', non_word);
2977 assembler->CheckCharacterGT('a' - 1, word);
2978 assembler->CheckCharacterLT('9' + 1, word);
2979 assembler->CheckCharacterLT('A', non_word);
2980 assembler->CheckCharacterLT('Z' + 1, word);
2981 if (fall_through_on_word) {
2982 assembler->CheckNotCharacter('_', non_word);
2984 assembler->CheckCharacter('_', word);
2989 // Emit the code to check for a ^ in multiline mode (1-character lookbehind
2990 // that matches newline or the start of input).
2991 static void EmitHat(RegExpCompiler* compiler,
2992 RegExpNode* on_success,
2994 RegExpMacroAssembler* assembler = compiler->macro_assembler();
2995 // We will be loading the previous character into the current character
2997 Trace new_trace(*trace);
2998 new_trace.InvalidateCurrentCharacter();
3001 if (new_trace.cp_offset() == 0) {
3002 // The start of input counts as a newline in this context, so skip to
3003 // ok if we are at the start.
3004 assembler->CheckAtStart(&ok);
3006 // We already checked that we are not at the start of input so it must be
3007 // OK to load the previous character.
3008 assembler->LoadCurrentCharacter(new_trace.cp_offset() -1,
3009 new_trace.backtrack(),
3011 if (!assembler->CheckSpecialCharacterClass('n',
3012 new_trace.backtrack())) {
3013 // Newline means \n, \r, 0x2028 or 0x2029.
3014 if (!compiler->one_byte()) {
3015 assembler->CheckCharacterAfterAnd(0x2028, 0xfffe, &ok);
3017 assembler->CheckCharacter('\n', &ok);
3018 assembler->CheckNotCharacter('\r', new_trace.backtrack());
3020 assembler->Bind(&ok);
3021 on_success->Emit(compiler, &new_trace);
3025 // Emit the code to handle \b and \B (word-boundary or non-word-boundary).
3026 void AssertionNode::EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace) {
3027 RegExpMacroAssembler* assembler = compiler->macro_assembler();
3028 Trace::TriBool next_is_word_character = Trace::UNKNOWN;
3029 bool not_at_start = (trace->at_start() == Trace::FALSE_VALUE);
3030 BoyerMooreLookahead* lookahead = bm_info(not_at_start);
3031 if (lookahead == NULL) {
3033 Min(kMaxLookaheadForBoyerMoore, EatsAtLeast(kMaxLookaheadForBoyerMoore,
3036 if (eats_at_least >= 1) {
3037 BoyerMooreLookahead* bm =
3038 new(zone()) BoyerMooreLookahead(eats_at_least, compiler, zone());
3039 FillInBMInfo(0, kRecursionBudget, bm, not_at_start);
3040 if (bm->at(0)->is_non_word())
3041 next_is_word_character = Trace::FALSE_VALUE;
3042 if (bm->at(0)->is_word()) next_is_word_character = Trace::TRUE_VALUE;
3045 if (lookahead->at(0)->is_non_word())
3046 next_is_word_character = Trace::FALSE_VALUE;
3047 if (lookahead->at(0)->is_word())
3048 next_is_word_character = Trace::TRUE_VALUE;
3050 bool at_boundary = (assertion_type_ == AssertionNode::AT_BOUNDARY);
3051 if (next_is_word_character == Trace::UNKNOWN) {
3052 Label before_non_word;
3054 if (trace->characters_preloaded() != 1) {
3055 assembler->LoadCurrentCharacter(trace->cp_offset(), &before_non_word);
3057 // Fall through on non-word.
3058 EmitWordCheck(assembler, &before_word, &before_non_word, false);
3059 // Next character is not a word character.
3060 assembler->Bind(&before_non_word);
3062 BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord);
3063 assembler->GoTo(&ok);
3065 assembler->Bind(&before_word);
3066 BacktrackIfPrevious(compiler, trace, at_boundary ? kIsWord : kIsNonWord);
3067 assembler->Bind(&ok);
3068 } else if (next_is_word_character == Trace::TRUE_VALUE) {
3069 BacktrackIfPrevious(compiler, trace, at_boundary ? kIsWord : kIsNonWord);
3071 DCHECK(next_is_word_character == Trace::FALSE_VALUE);
3072 BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord);
3077 void AssertionNode::BacktrackIfPrevious(
3078 RegExpCompiler* compiler,
3080 AssertionNode::IfPrevious backtrack_if_previous) {
3081 RegExpMacroAssembler* assembler = compiler->macro_assembler();
3082 Trace new_trace(*trace);
3083 new_trace.InvalidateCurrentCharacter();
3085 Label fall_through, dummy;
3087 Label* non_word = backtrack_if_previous == kIsNonWord ?
3088 new_trace.backtrack() :
3090 Label* word = backtrack_if_previous == kIsNonWord ?
3092 new_trace.backtrack();
3094 if (new_trace.cp_offset() == 0) {
3095 // The start of input counts as a non-word character, so the question is
3096 // decided if we are at the start.
3097 assembler->CheckAtStart(non_word);
3099 // We already checked that we are not at the start of input so it must be
3100 // OK to load the previous character.
3101 assembler->LoadCurrentCharacter(new_trace.cp_offset() - 1, &dummy, false);
3102 EmitWordCheck(assembler, word, non_word, backtrack_if_previous == kIsNonWord);
3104 assembler->Bind(&fall_through);
3105 on_success()->Emit(compiler, &new_trace);
3109 void AssertionNode::GetQuickCheckDetails(QuickCheckDetails* details,
3110 RegExpCompiler* compiler,
3112 bool not_at_start) {
3113 if (assertion_type_ == AT_START && not_at_start) {
3114 details->set_cannot_match();
3117 return on_success()->GetQuickCheckDetails(details,
3124 void AssertionNode::Emit(RegExpCompiler* compiler, Trace* trace) {
3125 RegExpMacroAssembler* assembler = compiler->macro_assembler();
3126 switch (assertion_type_) {
3129 assembler->CheckPosition(trace->cp_offset(), &ok);
3130 assembler->GoTo(trace->backtrack());
3131 assembler->Bind(&ok);
3135 if (trace->at_start() == Trace::FALSE_VALUE) {
3136 assembler->GoTo(trace->backtrack());
3139 if (trace->at_start() == Trace::UNKNOWN) {
3140 assembler->CheckNotAtStart(trace->backtrack());
3141 Trace at_start_trace = *trace;
3142 at_start_trace.set_at_start(true);
3143 on_success()->Emit(compiler, &at_start_trace);
3149 EmitHat(compiler, on_success(), trace);
3152 case AT_NON_BOUNDARY: {
3153 EmitBoundaryCheck(compiler, trace);
3157 on_success()->Emit(compiler, trace);
3161 static bool DeterminedAlready(QuickCheckDetails* quick_check, int offset) {
3162 if (quick_check == NULL) return false;
3163 if (offset >= quick_check->characters()) return false;
3164 return quick_check->positions(offset)->determines_perfectly;
3168 static void UpdateBoundsCheck(int index, int* checked_up_to) {
3169 if (index > *checked_up_to) {
3170 *checked_up_to = index;
3175 // We call this repeatedly to generate code for each pass over the text node.
3176 // The passes are in increasing order of difficulty because we hope one
3177 // of the first passes will fail in which case we are saved the work of the
3178 // later passes. for example for the case independent regexp /%[asdfghjkl]a/
3179 // we will check the '%' in the first pass, the case independent 'a' in the
3180 // second pass and the character class in the last pass.
3182 // The passes are done from right to left, so for example to test for /bar/
3183 // we will first test for an 'r' with offset 2, then an 'a' with offset 1
3184 // and then a 'b' with offset 0. This means we can avoid the end-of-input
3185 // bounds check most of the time. In the example we only need to check for
3186 // end-of-input when loading the putative 'r'.
3188 // A slight complication involves the fact that the first character may already
3189 // be fetched into a register by the previous node. In this case we want to
3190 // do the test for that character first. We do this in separate passes. The
3191 // 'preloaded' argument indicates that we are doing such a 'pass'. If such a
3192 // pass has been performed then subsequent passes will have true in
3193 // first_element_checked to indicate that that character does not need to be
3196 // In addition to all this we are passed a Trace, which can
3197 // contain an AlternativeGeneration object. In this AlternativeGeneration
3198 // object we can see details of any quick check that was already passed in
3199 // order to get to the code we are now generating. The quick check can involve
3200 // loading characters, which means we do not need to recheck the bounds
3201 // up to the limit the quick check already checked. In addition the quick
3202 // check can have involved a mask and compare operation which may simplify
3203 // or obviate the need for further checks at some character positions.
3204 void TextNode::TextEmitPass(RegExpCompiler* compiler,
3205 TextEmitPassType pass,
3208 bool first_element_checked,
3209 int* checked_up_to) {
3210 RegExpMacroAssembler* assembler = compiler->macro_assembler();
3211 Isolate* isolate = assembler->isolate();
3212 bool one_byte = compiler->one_byte();
3213 Label* backtrack = trace->backtrack();
3214 QuickCheckDetails* quick_check = trace->quick_check_performed();
3215 int element_count = elms_->length();
3216 for (int i = preloaded ? 0 : element_count - 1; i >= 0; i--) {
3217 TextElement elm = elms_->at(i);
3218 int cp_offset = trace->cp_offset() + elm.cp_offset();
3219 if (elm.text_type() == TextElement::ATOM) {
3220 Vector<const uc16> quarks = elm.atom()->data();
3221 for (int j = preloaded ? 0 : quarks.length() - 1; j >= 0; j--) {
3222 if (first_element_checked && i == 0 && j == 0) continue;
3223 if (DeterminedAlready(quick_check, elm.cp_offset() + j)) continue;
3224 EmitCharacterFunction* emit_function = NULL;
3226 case NON_LATIN1_MATCH:
3228 if (quarks[j] > String::kMaxOneByteCharCode) {
3229 assembler->GoTo(backtrack);
3233 case NON_LETTER_CHARACTER_MATCH:
3234 emit_function = &EmitAtomNonLetter;
3236 case SIMPLE_CHARACTER_MATCH:
3237 emit_function = &EmitSimpleCharacter;
3239 case CASE_CHARACTER_MATCH:
3240 emit_function = &EmitAtomLetter;
3245 if (emit_function != NULL) {
3246 bool bound_checked = emit_function(isolate,
3251 *checked_up_to < cp_offset + j,
3253 if (bound_checked) UpdateBoundsCheck(cp_offset + j, checked_up_to);
3257 DCHECK_EQ(TextElement::CHAR_CLASS, elm.text_type());
3258 if (pass == CHARACTER_CLASS_MATCH) {
3259 if (first_element_checked && i == 0) continue;
3260 if (DeterminedAlready(quick_check, elm.cp_offset())) continue;
3261 RegExpCharacterClass* cc = elm.char_class();
3262 EmitCharClass(assembler, cc, one_byte, backtrack, cp_offset,
3263 *checked_up_to < cp_offset, preloaded, zone());
3264 UpdateBoundsCheck(cp_offset, checked_up_to);
3271 int TextNode::Length() {
3272 TextElement elm = elms_->last();
3273 DCHECK(elm.cp_offset() >= 0);
3274 return elm.cp_offset() + elm.length();
3278 bool TextNode::SkipPass(int int_pass, bool ignore_case) {
3279 TextEmitPassType pass = static_cast<TextEmitPassType>(int_pass);
3281 return pass == SIMPLE_CHARACTER_MATCH;
3283 return pass == NON_LETTER_CHARACTER_MATCH || pass == CASE_CHARACTER_MATCH;
3288 // This generates the code to match a text node. A text node can contain
3289 // straight character sequences (possibly to be matched in a case-independent
3290 // way) and character classes. For efficiency we do not do this in a single
3291 // pass from left to right. Instead we pass over the text node several times,
3292 // emitting code for some character positions every time. See the comment on
3293 // TextEmitPass for details.
3294 void TextNode::Emit(RegExpCompiler* compiler, Trace* trace) {
3295 LimitResult limit_result = LimitVersions(compiler, trace);
3296 if (limit_result == DONE) return;
3297 DCHECK(limit_result == CONTINUE);
3299 if (trace->cp_offset() + Length() > RegExpMacroAssembler::kMaxCPOffset) {
3300 compiler->SetRegExpTooBig();
3304 if (compiler->one_byte()) {
3306 TextEmitPass(compiler, NON_LATIN1_MATCH, false, trace, false, &dummy);
3309 bool first_elt_done = false;
3310 int bound_checked_to = trace->cp_offset() - 1;
3311 bound_checked_to += trace->bound_checked_up_to();
3313 // If a character is preloaded into the current character register then
3315 if (trace->characters_preloaded() == 1) {
3316 for (int pass = kFirstRealPass; pass <= kLastPass; pass++) {
3317 if (!SkipPass(pass, compiler->ignore_case())) {
3318 TextEmitPass(compiler,
3319 static_cast<TextEmitPassType>(pass),
3326 first_elt_done = true;
3329 for (int pass = kFirstRealPass; pass <= kLastPass; pass++) {
3330 if (!SkipPass(pass, compiler->ignore_case())) {
3331 TextEmitPass(compiler,
3332 static_cast<TextEmitPassType>(pass),
3340 Trace successor_trace(*trace);
3341 successor_trace.set_at_start(false);
3342 successor_trace.AdvanceCurrentPositionInTrace(Length(), compiler);
3343 RecursionCheck rc(compiler);
3344 on_success()->Emit(compiler, &successor_trace);
3348 void Trace::InvalidateCurrentCharacter() {
3349 characters_preloaded_ = 0;
3353 void Trace::AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler) {
3355 // We don't have an instruction for shifting the current character register
3356 // down or for using a shifted value for anything so lets just forget that
3357 // we preloaded any characters into it.
3358 characters_preloaded_ = 0;
3359 // Adjust the offsets of the quick check performed information. This
3360 // information is used to find out what we already determined about the
3361 // characters by means of mask and compare.
3362 quick_check_performed_.Advance(by, compiler->one_byte());
3364 if (cp_offset_ > RegExpMacroAssembler::kMaxCPOffset) {
3365 compiler->SetRegExpTooBig();
3368 bound_checked_up_to_ = Max(0, bound_checked_up_to_ - by);
3372 void TextNode::MakeCaseIndependent(Isolate* isolate, bool is_one_byte) {
3373 int element_count = elms_->length();
3374 for (int i = 0; i < element_count; i++) {
3375 TextElement elm = elms_->at(i);
3376 if (elm.text_type() == TextElement::CHAR_CLASS) {
3377 RegExpCharacterClass* cc = elm.char_class();
3378 // None of the standard character classes is different in the case
3379 // independent case and it slows us down if we don't know that.
3380 if (cc->is_standard(zone())) continue;
3381 ZoneList<CharacterRange>* ranges = cc->ranges(zone());
3382 int range_count = ranges->length();
3383 for (int j = 0; j < range_count; j++) {
3384 ranges->at(j).AddCaseEquivalents(isolate, zone(), ranges, is_one_byte);
3391 int TextNode::GreedyLoopTextLength() {
3392 TextElement elm = elms_->at(elms_->length() - 1);
3393 return elm.cp_offset() + elm.length();
3397 RegExpNode* TextNode::GetSuccessorOfOmnivorousTextNode(
3398 RegExpCompiler* compiler) {
3399 if (elms_->length() != 1) return NULL;
3400 TextElement elm = elms_->at(0);
3401 if (elm.text_type() != TextElement::CHAR_CLASS) return NULL;
3402 RegExpCharacterClass* node = elm.char_class();
3403 ZoneList<CharacterRange>* ranges = node->ranges(zone());
3404 if (!CharacterRange::IsCanonical(ranges)) {
3405 CharacterRange::Canonicalize(ranges);
3407 if (node->is_negated()) {
3408 return ranges->length() == 0 ? on_success() : NULL;
3410 if (ranges->length() != 1) return NULL;
3412 if (compiler->one_byte()) {
3413 max_char = String::kMaxOneByteCharCode;
3415 max_char = String::kMaxUtf16CodeUnit;
3417 return ranges->at(0).IsEverything(max_char) ? on_success() : NULL;
3421 // Finds the fixed match length of a sequence of nodes that goes from
3422 // this alternative and back to this choice node. If there are variable
3423 // length nodes or other complications in the way then return a sentinel
3424 // value indicating that a greedy loop cannot be constructed.
3425 int ChoiceNode::GreedyLoopTextLengthForAlternative(
3426 GuardedAlternative* alternative) {
3428 RegExpNode* node = alternative->node();
3429 // Later we will generate code for all these text nodes using recursion
3430 // so we have to limit the max number.
3431 int recursion_depth = 0;
3432 while (node != this) {
3433 if (recursion_depth++ > RegExpCompiler::kMaxRecursion) {
3434 return kNodeIsTooComplexForGreedyLoops;
3436 int node_length = node->GreedyLoopTextLength();
3437 if (node_length == kNodeIsTooComplexForGreedyLoops) {
3438 return kNodeIsTooComplexForGreedyLoops;
3440 length += node_length;
3441 SeqRegExpNode* seq_node = static_cast<SeqRegExpNode*>(node);
3442 node = seq_node->on_success();
3448 void LoopChoiceNode::AddLoopAlternative(GuardedAlternative alt) {
3449 DCHECK_NULL(loop_node_);
3450 AddAlternative(alt);
3451 loop_node_ = alt.node();
3455 void LoopChoiceNode::AddContinueAlternative(GuardedAlternative alt) {
3456 DCHECK_NULL(continue_node_);
3457 AddAlternative(alt);
3458 continue_node_ = alt.node();
3462 void LoopChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) {
3463 RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
3464 if (trace->stop_node() == this) {
3465 // Back edge of greedy optimized loop node graph.
3467 GreedyLoopTextLengthForAlternative(&(alternatives_->at(0)));
3468 DCHECK(text_length != kNodeIsTooComplexForGreedyLoops);
3469 // Update the counter-based backtracking info on the stack. This is an
3470 // optimization for greedy loops (see below).
3471 DCHECK(trace->cp_offset() == text_length);
3472 macro_assembler->AdvanceCurrentPosition(text_length);
3473 macro_assembler->GoTo(trace->loop_label());
3476 DCHECK_NULL(trace->stop_node());
3477 if (!trace->is_trivial()) {
3478 trace->Flush(compiler, this);
3481 ChoiceNode::Emit(compiler, trace);
3485 int ChoiceNode::CalculatePreloadCharacters(RegExpCompiler* compiler,
3486 int eats_at_least) {
3487 int preload_characters = Min(4, eats_at_least);
3488 if (compiler->macro_assembler()->CanReadUnaligned()) {
3489 bool one_byte = compiler->one_byte();
3491 if (preload_characters > 4) preload_characters = 4;
3492 // We can't preload 3 characters because there is no machine instruction
3493 // to do that. We can't just load 4 because we could be reading
3494 // beyond the end of the string, which could cause a memory fault.
3495 if (preload_characters == 3) preload_characters = 2;
3497 if (preload_characters > 2) preload_characters = 2;
3500 if (preload_characters > 1) preload_characters = 1;
3502 return preload_characters;
3506 // This class is used when generating the alternatives in a choice node. It
3507 // records the way the alternative is being code generated.
3508 class AlternativeGeneration: public Malloced {
3510 AlternativeGeneration()
3511 : possible_success(),
3512 expects_preload(false),
3514 quick_check_details() { }
3515 Label possible_success;
3516 bool expects_preload;
3518 QuickCheckDetails quick_check_details;
3522 // Creates a list of AlternativeGenerations. If the list has a reasonable
3523 // size then it is on the stack, otherwise the excess is on the heap.
3524 class AlternativeGenerationList {
3526 AlternativeGenerationList(int count, Zone* zone)
3527 : alt_gens_(count, zone) {
3528 for (int i = 0; i < count && i < kAFew; i++) {
3529 alt_gens_.Add(a_few_alt_gens_ + i, zone);
3531 for (int i = kAFew; i < count; i++) {
3532 alt_gens_.Add(new AlternativeGeneration(), zone);
3535 ~AlternativeGenerationList() {
3536 for (int i = kAFew; i < alt_gens_.length(); i++) {
3537 delete alt_gens_[i];
3538 alt_gens_[i] = NULL;
3542 AlternativeGeneration* at(int i) {
3543 return alt_gens_[i];
3547 static const int kAFew = 10;
3548 ZoneList<AlternativeGeneration*> alt_gens_;
3549 AlternativeGeneration a_few_alt_gens_[kAFew];
3553 // The '2' variant is has inclusive from and exclusive to.
3554 // This covers \s as defined in ECMA-262 5.1, 15.10.2.12,
3555 // which include WhiteSpace (7.2) or LineTerminator (7.3) values.
3556 static const int kSpaceRanges[] = { '\t', '\r' + 1, ' ', ' ' + 1,
3557 0x00A0, 0x00A1, 0x1680, 0x1681, 0x180E, 0x180F, 0x2000, 0x200B,
3558 0x2028, 0x202A, 0x202F, 0x2030, 0x205F, 0x2060, 0x3000, 0x3001,
3559 0xFEFF, 0xFF00, 0x10000 };
3560 static const int kSpaceRangeCount = arraysize(kSpaceRanges);
3562 static const int kWordRanges[] = {
3563 '0', '9' + 1, 'A', 'Z' + 1, '_', '_' + 1, 'a', 'z' + 1, 0x10000 };
3564 static const int kWordRangeCount = arraysize(kWordRanges);
3565 static const int kDigitRanges[] = { '0', '9' + 1, 0x10000 };
3566 static const int kDigitRangeCount = arraysize(kDigitRanges);
3567 static const int kSurrogateRanges[] = { 0xd800, 0xe000, 0x10000 };
3568 static const int kSurrogateRangeCount = arraysize(kSurrogateRanges);
3569 static const int kLineTerminatorRanges[] = { 0x000A, 0x000B, 0x000D, 0x000E,
3570 0x2028, 0x202A, 0x10000 };
3571 static const int kLineTerminatorRangeCount = arraysize(kLineTerminatorRanges);
3574 void BoyerMoorePositionInfo::Set(int character) {
3575 SetInterval(Interval(character, character));
3579 void BoyerMoorePositionInfo::SetInterval(const Interval& interval) {
3580 s_ = AddRange(s_, kSpaceRanges, kSpaceRangeCount, interval);
3581 w_ = AddRange(w_, kWordRanges, kWordRangeCount, interval);
3582 d_ = AddRange(d_, kDigitRanges, kDigitRangeCount, interval);
3584 AddRange(surrogate_, kSurrogateRanges, kSurrogateRangeCount, interval);
3585 if (interval.to() - interval.from() >= kMapSize - 1) {
3586 if (map_count_ != kMapSize) {
3587 map_count_ = kMapSize;
3588 for (int i = 0; i < kMapSize; i++) map_->at(i) = true;
3592 for (int i = interval.from(); i <= interval.to(); i++) {
3593 int mod_character = (i & kMask);
3594 if (!map_->at(mod_character)) {
3596 map_->at(mod_character) = true;
3598 if (map_count_ == kMapSize) return;
3603 void BoyerMoorePositionInfo::SetAll() {
3604 s_ = w_ = d_ = kLatticeUnknown;
3605 if (map_count_ != kMapSize) {
3606 map_count_ = kMapSize;
3607 for (int i = 0; i < kMapSize; i++) map_->at(i) = true;
3612 BoyerMooreLookahead::BoyerMooreLookahead(
3613 int length, RegExpCompiler* compiler, Zone* zone)
3615 compiler_(compiler) {
3616 if (compiler->one_byte()) {
3617 max_char_ = String::kMaxOneByteCharCode;
3619 max_char_ = String::kMaxUtf16CodeUnit;
3621 bitmaps_ = new(zone) ZoneList<BoyerMoorePositionInfo*>(length, zone);
3622 for (int i = 0; i < length; i++) {
3623 bitmaps_->Add(new(zone) BoyerMoorePositionInfo(zone), zone);
3628 // Find the longest range of lookahead that has the fewest number of different
3629 // characters that can occur at a given position. Since we are optimizing two
3630 // different parameters at once this is a tradeoff.
3631 bool BoyerMooreLookahead::FindWorthwhileInterval(int* from, int* to) {
3632 int biggest_points = 0;
3633 // If more than 32 characters out of 128 can occur it is unlikely that we can
3634 // be lucky enough to step forwards much of the time.
3635 const int kMaxMax = 32;
3636 for (int max_number_of_chars = 4;
3637 max_number_of_chars < kMaxMax;
3638 max_number_of_chars *= 2) {
3640 FindBestInterval(max_number_of_chars, biggest_points, from, to);
3642 if (biggest_points == 0) return false;
3647 // Find the highest-points range between 0 and length_ where the character
3648 // information is not too vague. 'Too vague' means that there are more than
3649 // max_number_of_chars that can occur at this position. Calculates the number
3650 // of points as the product of width-of-the-range and
3651 // probability-of-finding-one-of-the-characters, where the probability is
3652 // calculated using the frequency distribution of the sample subject string.
3653 int BoyerMooreLookahead::FindBestInterval(
3654 int max_number_of_chars, int old_biggest_points, int* from, int* to) {
3655 int biggest_points = old_biggest_points;
3656 static const int kSize = RegExpMacroAssembler::kTableSize;
3657 for (int i = 0; i < length_; ) {
3658 while (i < length_ && Count(i) > max_number_of_chars) i++;
3659 if (i == length_) break;
3660 int remembered_from = i;
3661 bool union_map[kSize];
3662 for (int j = 0; j < kSize; j++) union_map[j] = false;
3663 while (i < length_ && Count(i) <= max_number_of_chars) {
3664 BoyerMoorePositionInfo* map = bitmaps_->at(i);
3665 for (int j = 0; j < kSize; j++) union_map[j] |= map->at(j);
3669 for (int j = 0; j < kSize; j++) {
3671 // Add 1 to the frequency to give a small per-character boost for
3672 // the cases where our sampling is not good enough and many
3673 // characters have a frequency of zero. This means the frequency
3674 // can theoretically be up to 2*kSize though we treat it mostly as
3675 // a fraction of kSize.
3676 frequency += compiler_->frequency_collator()->Frequency(j) + 1;
3679 // We use the probability of skipping times the distance we are skipping to
3680 // judge the effectiveness of this. Actually we have a cut-off: By
3681 // dividing by 2 we switch off the skipping if the probability of skipping
3682 // is less than 50%. This is because the multibyte mask-and-compare
3683 // skipping in quickcheck is more likely to do well on this case.
3684 bool in_quickcheck_range =
3685 ((i - remembered_from < 4) ||
3686 (compiler_->one_byte() ? remembered_from <= 4 : remembered_from <= 2));
3687 // Called 'probability' but it is only a rough estimate and can actually
3688 // be outside the 0-kSize range.
3689 int probability = (in_quickcheck_range ? kSize / 2 : kSize) - frequency;
3690 int points = (i - remembered_from) * probability;
3691 if (points > biggest_points) {
3692 *from = remembered_from;
3694 biggest_points = points;
3697 return biggest_points;
3701 // Take all the characters that will not prevent a successful match if they
3702 // occur in the subject string in the range between min_lookahead and
3703 // max_lookahead (inclusive) measured from the current position. If the
3704 // character at max_lookahead offset is not one of these characters, then we
3705 // can safely skip forwards by the number of characters in the range.
3706 int BoyerMooreLookahead::GetSkipTable(int min_lookahead,
3708 Handle<ByteArray> boolean_skip_table) {
3709 const int kSize = RegExpMacroAssembler::kTableSize;
3711 const int kSkipArrayEntry = 0;
3712 const int kDontSkipArrayEntry = 1;
3714 for (int i = 0; i < kSize; i++) {
3715 boolean_skip_table->set(i, kSkipArrayEntry);
3717 int skip = max_lookahead + 1 - min_lookahead;
3719 for (int i = max_lookahead; i >= min_lookahead; i--) {
3720 BoyerMoorePositionInfo* map = bitmaps_->at(i);
3721 for (int j = 0; j < kSize; j++) {
3723 boolean_skip_table->set(j, kDontSkipArrayEntry);
3732 // See comment above on the implementation of GetSkipTable.
3733 void BoyerMooreLookahead::EmitSkipInstructions(RegExpMacroAssembler* masm) {
3734 const int kSize = RegExpMacroAssembler::kTableSize;
3736 int min_lookahead = 0;
3737 int max_lookahead = 0;
3739 if (!FindWorthwhileInterval(&min_lookahead, &max_lookahead)) return;
3741 bool found_single_character = false;
3742 int single_character = 0;
3743 for (int i = max_lookahead; i >= min_lookahead; i--) {
3744 BoyerMoorePositionInfo* map = bitmaps_->at(i);
3745 if (map->map_count() > 1 ||
3746 (found_single_character && map->map_count() != 0)) {
3747 found_single_character = false;
3750 for (int j = 0; j < kSize; j++) {
3752 found_single_character = true;
3753 single_character = j;
3759 int lookahead_width = max_lookahead + 1 - min_lookahead;
3761 if (found_single_character && lookahead_width == 1 && max_lookahead < 3) {
3762 // The mask-compare can probably handle this better.
3766 if (found_single_character) {
3769 masm->LoadCurrentCharacter(max_lookahead, &cont, true);
3770 if (max_char_ > kSize) {
3771 masm->CheckCharacterAfterAnd(single_character,
3772 RegExpMacroAssembler::kTableMask,
3775 masm->CheckCharacter(single_character, &cont);
3777 masm->AdvanceCurrentPosition(lookahead_width);
3783 Factory* factory = masm->isolate()->factory();
3784 Handle<ByteArray> boolean_skip_table = factory->NewByteArray(kSize, TENURED);
3785 int skip_distance = GetSkipTable(
3786 min_lookahead, max_lookahead, boolean_skip_table);
3787 DCHECK(skip_distance != 0);
3791 masm->LoadCurrentCharacter(max_lookahead, &cont, true);
3792 masm->CheckBitInTable(boolean_skip_table, &cont);
3793 masm->AdvanceCurrentPosition(skip_distance);
3799 /* Code generation for choice nodes.
3801 * We generate quick checks that do a mask and compare to eliminate a
3802 * choice. If the quick check succeeds then it jumps to the continuation to
3803 * do slow checks and check subsequent nodes. If it fails (the common case)
3804 * it falls through to the next choice.
3806 * Here is the desired flow graph. Nodes directly below each other imply
3807 * fallthrough. Alternatives 1 and 2 have quick checks. Alternative
3808 * 3 doesn't have a quick check so we have to call the slow check.
3809 * Nodes are marked Qn for quick checks and Sn for slow checks. The entire
3810 * regexp continuation is generated directly after the Sn node, up to the
3811 * next GoTo if we decide to reuse some already generated code. Some
3812 * nodes expect preload_characters to be preloaded into the current
3813 * character register. R nodes do this preloading. Vertices are marked
3814 * F for failures and S for success (possible success in the case of quick
3815 * nodes). L, V, < and > are used as arrow heads.
3849 * For greedy loops we push the current position, then generate the code that
3850 * eats the input specially in EmitGreedyLoop. The other choice (the
3851 * continuation) is generated by the normal code in EmitChoices, and steps back
3852 * in the input to the starting position when it fails to match. The loop code
3853 * looks like this (U is the unwind code that steps back in the greedy loop).
3866 * Q2 ---> U----->backtrack
3873 GreedyLoopState::GreedyLoopState(bool not_at_start) {
3874 counter_backtrack_trace_.set_backtrack(&label_);
3875 if (not_at_start) counter_backtrack_trace_.set_at_start(false);
3879 void ChoiceNode::AssertGuardsMentionRegisters(Trace* trace) {
3881 int choice_count = alternatives_->length();
3882 for (int i = 0; i < choice_count - 1; i++) {
3883 GuardedAlternative alternative = alternatives_->at(i);
3884 ZoneList<Guard*>* guards = alternative.guards();
3885 int guard_count = (guards == NULL) ? 0 : guards->length();
3886 for (int j = 0; j < guard_count; j++) {
3887 DCHECK(!trace->mentions_reg(guards->at(j)->reg()));
3894 void ChoiceNode::SetUpPreLoad(RegExpCompiler* compiler,
3895 Trace* current_trace,
3896 PreloadState* state) {
3897 if (state->eats_at_least_ == PreloadState::kEatsAtLeastNotYetInitialized) {
3898 // Save some time by looking at most one machine word ahead.
3899 state->eats_at_least_ =
3900 EatsAtLeast(compiler->one_byte() ? 4 : 2, kRecursionBudget,
3901 current_trace->at_start() == Trace::FALSE_VALUE);
3903 state->preload_characters_ =
3904 CalculatePreloadCharacters(compiler, state->eats_at_least_);
3906 state->preload_is_current_ =
3907 (current_trace->characters_preloaded() == state->preload_characters_);
3908 state->preload_has_checked_bounds_ = state->preload_is_current_;
3912 void ChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) {
3913 int choice_count = alternatives_->length();
3915 AssertGuardsMentionRegisters(trace);
3917 LimitResult limit_result = LimitVersions(compiler, trace);
3918 if (limit_result == DONE) return;
3919 DCHECK(limit_result == CONTINUE);
3921 // For loop nodes we already flushed (see LoopChoiceNode::Emit), but for
3922 // other choice nodes we only flush if we are out of code size budget.
3923 if (trace->flush_budget() == 0 && trace->actions() != NULL) {
3924 trace->Flush(compiler, this);
3928 RecursionCheck rc(compiler);
3930 PreloadState preload;
3932 GreedyLoopState greedy_loop_state(not_at_start());
3934 int text_length = GreedyLoopTextLengthForAlternative(&alternatives_->at(0));
3935 AlternativeGenerationList alt_gens(choice_count, zone());
3937 if (choice_count > 1 && text_length != kNodeIsTooComplexForGreedyLoops) {
3938 trace = EmitGreedyLoop(compiler,
3945 // TODO(erikcorry): Delete this. We don't need this label, but it makes us
3946 // match the traces produced pre-cleanup.
3947 Label second_choice;
3948 compiler->macro_assembler()->Bind(&second_choice);
3950 preload.eats_at_least_ = EmitOptimizedUnanchoredSearch(compiler, trace);
3952 EmitChoices(compiler,
3959 // At this point we need to generate slow checks for the alternatives where
3960 // the quick check was inlined. We can recognize these because the associated
3962 int new_flush_budget = trace->flush_budget() / choice_count;
3963 for (int i = 0; i < choice_count; i++) {
3964 AlternativeGeneration* alt_gen = alt_gens.at(i);
3965 Trace new_trace(*trace);
3966 // If there are actions to be flushed we have to limit how many times
3967 // they are flushed. Take the budget of the parent trace and distribute
3968 // it fairly amongst the children.
3969 if (new_trace.actions() != NULL) {
3970 new_trace.set_flush_budget(new_flush_budget);
3972 bool next_expects_preload =
3973 i == choice_count - 1 ? false : alt_gens.at(i + 1)->expects_preload;
3974 EmitOutOfLineContinuation(compiler,
3976 alternatives_->at(i),
3978 preload.preload_characters_,
3979 next_expects_preload);
3984 Trace* ChoiceNode::EmitGreedyLoop(RegExpCompiler* compiler,
3986 AlternativeGenerationList* alt_gens,
3987 PreloadState* preload,
3988 GreedyLoopState* greedy_loop_state,
3990 RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
3991 // Here we have special handling for greedy loops containing only text nodes
3992 // and other simple nodes. These are handled by pushing the current
3993 // position on the stack and then incrementing the current position each
3994 // time around the switch. On backtrack we decrement the current position
3995 // and check it against the pushed value. This avoids pushing backtrack
3996 // information for each iteration of the loop, which could take up a lot of
3998 DCHECK(trace->stop_node() == NULL);
3999 macro_assembler->PushCurrentPosition();
4000 Label greedy_match_failed;
4001 Trace greedy_match_trace;
4002 if (not_at_start()) greedy_match_trace.set_at_start(false);
4003 greedy_match_trace.set_backtrack(&greedy_match_failed);
4005 macro_assembler->Bind(&loop_label);
4006 greedy_match_trace.set_stop_node(this);
4007 greedy_match_trace.set_loop_label(&loop_label);
4008 alternatives_->at(0).node()->Emit(compiler, &greedy_match_trace);
4009 macro_assembler->Bind(&greedy_match_failed);
4011 Label second_choice; // For use in greedy matches.
4012 macro_assembler->Bind(&second_choice);
4014 Trace* new_trace = greedy_loop_state->counter_backtrack_trace();
4016 EmitChoices(compiler,
4022 macro_assembler->Bind(greedy_loop_state->label());
4023 // If we have unwound to the bottom then backtrack.
4024 macro_assembler->CheckGreedyLoop(trace->backtrack());
4025 // Otherwise try the second priority at an earlier position.
4026 macro_assembler->AdvanceCurrentPosition(-text_length);
4027 macro_assembler->GoTo(&second_choice);
4031 int ChoiceNode::EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler,
4033 int eats_at_least = PreloadState::kEatsAtLeastNotYetInitialized;
4034 if (alternatives_->length() != 2) return eats_at_least;
4036 GuardedAlternative alt1 = alternatives_->at(1);
4037 if (alt1.guards() != NULL && alt1.guards()->length() != 0) {
4038 return eats_at_least;
4040 RegExpNode* eats_anything_node = alt1.node();
4041 if (eats_anything_node->GetSuccessorOfOmnivorousTextNode(compiler) != this) {
4042 return eats_at_least;
4045 // Really we should be creating a new trace when we execute this function,
4046 // but there is no need, because the code it generates cannot backtrack, and
4047 // we always arrive here with a trivial trace (since it's the entry to a
4048 // loop. That also implies that there are no preloaded characters, which is
4049 // good, because it means we won't be violating any assumptions by
4050 // overwriting those characters with new load instructions.
4051 DCHECK(trace->is_trivial());
4053 RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
4054 // At this point we know that we are at a non-greedy loop that will eat
4055 // any character one at a time. Any non-anchored regexp has such a
4056 // loop prepended to it in order to find where it starts. We look for
4057 // a pattern of the form ...abc... where we can look 6 characters ahead
4058 // and step forwards 3 if the character is not one of abc. Abc need
4059 // not be atoms, they can be any reasonably limited character class or
4060 // small alternation.
4061 BoyerMooreLookahead* bm = bm_info(false);
4063 eats_at_least = Min(kMaxLookaheadForBoyerMoore,
4064 EatsAtLeast(kMaxLookaheadForBoyerMoore,
4067 if (eats_at_least >= 1) {
4068 bm = new(zone()) BoyerMooreLookahead(eats_at_least,
4071 GuardedAlternative alt0 = alternatives_->at(0);
4072 alt0.node()->FillInBMInfo(0, kRecursionBudget, bm, false);
4076 bm->EmitSkipInstructions(macro_assembler);
4078 return eats_at_least;
4082 void ChoiceNode::EmitChoices(RegExpCompiler* compiler,
4083 AlternativeGenerationList* alt_gens,
4086 PreloadState* preload) {
4087 RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
4088 SetUpPreLoad(compiler, trace, preload);
4090 // For now we just call all choices one after the other. The idea ultimately
4091 // is to use the Dispatch table to try only the relevant ones.
4092 int choice_count = alternatives_->length();
4094 int new_flush_budget = trace->flush_budget() / choice_count;
4096 for (int i = first_choice; i < choice_count; i++) {
4097 bool is_last = i == choice_count - 1;
4098 bool fall_through_on_failure = !is_last;
4099 GuardedAlternative alternative = alternatives_->at(i);
4100 AlternativeGeneration* alt_gen = alt_gens->at(i);
4101 alt_gen->quick_check_details.set_characters(preload->preload_characters_);
4102 ZoneList<Guard*>* guards = alternative.guards();
4103 int guard_count = (guards == NULL) ? 0 : guards->length();
4104 Trace new_trace(*trace);
4105 new_trace.set_characters_preloaded(preload->preload_is_current_ ?
4106 preload->preload_characters_ :
4108 if (preload->preload_has_checked_bounds_) {
4109 new_trace.set_bound_checked_up_to(preload->preload_characters_);
4111 new_trace.quick_check_performed()->Clear();
4112 if (not_at_start_) new_trace.set_at_start(Trace::FALSE_VALUE);
4114 new_trace.set_backtrack(&alt_gen->after);
4116 alt_gen->expects_preload = preload->preload_is_current_;
4117 bool generate_full_check_inline = false;
4118 if (compiler->optimize() &&
4119 try_to_emit_quick_check_for_alternative(i == 0) &&
4120 alternative.node()->EmitQuickCheck(
4121 compiler, trace, &new_trace, preload->preload_has_checked_bounds_,
4122 &alt_gen->possible_success, &alt_gen->quick_check_details,
4123 fall_through_on_failure)) {
4124 // Quick check was generated for this choice.
4125 preload->preload_is_current_ = true;
4126 preload->preload_has_checked_bounds_ = true;
4127 // If we generated the quick check to fall through on possible success,
4128 // we now need to generate the full check inline.
4129 if (!fall_through_on_failure) {
4130 macro_assembler->Bind(&alt_gen->possible_success);
4131 new_trace.set_quick_check_performed(&alt_gen->quick_check_details);
4132 new_trace.set_characters_preloaded(preload->preload_characters_);
4133 new_trace.set_bound_checked_up_to(preload->preload_characters_);
4134 generate_full_check_inline = true;
4136 } else if (alt_gen->quick_check_details.cannot_match()) {
4137 if (!fall_through_on_failure) {
4138 macro_assembler->GoTo(trace->backtrack());
4142 // No quick check was generated. Put the full code here.
4143 // If this is not the first choice then there could be slow checks from
4144 // previous cases that go here when they fail. There's no reason to
4145 // insist that they preload characters since the slow check we are about
4146 // to generate probably can't use it.
4147 if (i != first_choice) {
4148 alt_gen->expects_preload = false;
4149 new_trace.InvalidateCurrentCharacter();
4151 generate_full_check_inline = true;
4153 if (generate_full_check_inline) {
4154 if (new_trace.actions() != NULL) {
4155 new_trace.set_flush_budget(new_flush_budget);
4157 for (int j = 0; j < guard_count; j++) {
4158 GenerateGuard(macro_assembler, guards->at(j), &new_trace);
4160 alternative.node()->Emit(compiler, &new_trace);
4161 preload->preload_is_current_ = false;
4163 macro_assembler->Bind(&alt_gen->after);
4168 void ChoiceNode::EmitOutOfLineContinuation(RegExpCompiler* compiler,
4170 GuardedAlternative alternative,
4171 AlternativeGeneration* alt_gen,
4172 int preload_characters,
4173 bool next_expects_preload) {
4174 if (!alt_gen->possible_success.is_linked()) return;
4176 RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
4177 macro_assembler->Bind(&alt_gen->possible_success);
4178 Trace out_of_line_trace(*trace);
4179 out_of_line_trace.set_characters_preloaded(preload_characters);
4180 out_of_line_trace.set_quick_check_performed(&alt_gen->quick_check_details);
4181 if (not_at_start_) out_of_line_trace.set_at_start(Trace::FALSE_VALUE);
4182 ZoneList<Guard*>* guards = alternative.guards();
4183 int guard_count = (guards == NULL) ? 0 : guards->length();
4184 if (next_expects_preload) {
4185 Label reload_current_char;
4186 out_of_line_trace.set_backtrack(&reload_current_char);
4187 for (int j = 0; j < guard_count; j++) {
4188 GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace);
4190 alternative.node()->Emit(compiler, &out_of_line_trace);
4191 macro_assembler->Bind(&reload_current_char);
4192 // Reload the current character, since the next quick check expects that.
4193 // We don't need to check bounds here because we only get into this
4194 // code through a quick check which already did the checked load.
4195 macro_assembler->LoadCurrentCharacter(trace->cp_offset(),
4198 preload_characters);
4199 macro_assembler->GoTo(&(alt_gen->after));
4201 out_of_line_trace.set_backtrack(&(alt_gen->after));
4202 for (int j = 0; j < guard_count; j++) {
4203 GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace);
4205 alternative.node()->Emit(compiler, &out_of_line_trace);
4210 void ActionNode::Emit(RegExpCompiler* compiler, Trace* trace) {
4211 RegExpMacroAssembler* assembler = compiler->macro_assembler();
4212 LimitResult limit_result = LimitVersions(compiler, trace);
4213 if (limit_result == DONE) return;
4214 DCHECK(limit_result == CONTINUE);
4216 RecursionCheck rc(compiler);
4218 switch (action_type_) {
4219 case STORE_POSITION: {
4220 Trace::DeferredCapture
4221 new_capture(data_.u_position_register.reg,
4222 data_.u_position_register.is_capture,
4224 Trace new_trace = *trace;
4225 new_trace.add_action(&new_capture);
4226 on_success()->Emit(compiler, &new_trace);
4229 case INCREMENT_REGISTER: {
4230 Trace::DeferredIncrementRegister
4231 new_increment(data_.u_increment_register.reg);
4232 Trace new_trace = *trace;
4233 new_trace.add_action(&new_increment);
4234 on_success()->Emit(compiler, &new_trace);
4237 case SET_REGISTER: {
4238 Trace::DeferredSetRegister
4239 new_set(data_.u_store_register.reg, data_.u_store_register.value);
4240 Trace new_trace = *trace;
4241 new_trace.add_action(&new_set);
4242 on_success()->Emit(compiler, &new_trace);
4245 case CLEAR_CAPTURES: {
4246 Trace::DeferredClearCaptures
4247 new_capture(Interval(data_.u_clear_captures.range_from,
4248 data_.u_clear_captures.range_to));
4249 Trace new_trace = *trace;
4250 new_trace.add_action(&new_capture);
4251 on_success()->Emit(compiler, &new_trace);
4254 case BEGIN_SUBMATCH:
4255 if (!trace->is_trivial()) {
4256 trace->Flush(compiler, this);
4258 assembler->WriteCurrentPositionToRegister(
4259 data_.u_submatch.current_position_register, 0);
4260 assembler->WriteStackPointerToRegister(
4261 data_.u_submatch.stack_pointer_register);
4262 on_success()->Emit(compiler, trace);
4265 case EMPTY_MATCH_CHECK: {
4266 int start_pos_reg = data_.u_empty_match_check.start_register;
4268 int rep_reg = data_.u_empty_match_check.repetition_register;
4269 bool has_minimum = (rep_reg != RegExpCompiler::kNoRegister);
4270 bool know_dist = trace->GetStoredPosition(start_pos_reg, &stored_pos);
4271 if (know_dist && !has_minimum && stored_pos == trace->cp_offset()) {
4272 // If we know we haven't advanced and there is no minimum we
4273 // can just backtrack immediately.
4274 assembler->GoTo(trace->backtrack());
4275 } else if (know_dist && stored_pos < trace->cp_offset()) {
4276 // If we know we've advanced we can generate the continuation
4278 on_success()->Emit(compiler, trace);
4279 } else if (!trace->is_trivial()) {
4280 trace->Flush(compiler, this);
4282 Label skip_empty_check;
4283 // If we have a minimum number of repetitions we check the current
4284 // number first and skip the empty check if it's not enough.
4286 int limit = data_.u_empty_match_check.repetition_limit;
4287 assembler->IfRegisterLT(rep_reg, limit, &skip_empty_check);
4289 // If the match is empty we bail out, otherwise we fall through
4290 // to the on-success continuation.
4291 assembler->IfRegisterEqPos(data_.u_empty_match_check.start_register,
4292 trace->backtrack());
4293 assembler->Bind(&skip_empty_check);
4294 on_success()->Emit(compiler, trace);
4298 case POSITIVE_SUBMATCH_SUCCESS: {
4299 if (!trace->is_trivial()) {
4300 trace->Flush(compiler, this);
4303 assembler->ReadCurrentPositionFromRegister(
4304 data_.u_submatch.current_position_register);
4305 assembler->ReadStackPointerFromRegister(
4306 data_.u_submatch.stack_pointer_register);
4307 int clear_register_count = data_.u_submatch.clear_register_count;
4308 if (clear_register_count == 0) {
4309 on_success()->Emit(compiler, trace);
4312 int clear_registers_from = data_.u_submatch.clear_register_from;
4313 Label clear_registers_backtrack;
4314 Trace new_trace = *trace;
4315 new_trace.set_backtrack(&clear_registers_backtrack);
4316 on_success()->Emit(compiler, &new_trace);
4318 assembler->Bind(&clear_registers_backtrack);
4319 int clear_registers_to = clear_registers_from + clear_register_count - 1;
4320 assembler->ClearRegisters(clear_registers_from, clear_registers_to);
4322 DCHECK(trace->backtrack() == NULL);
4323 assembler->Backtrack();
4332 void BackReferenceNode::Emit(RegExpCompiler* compiler, Trace* trace) {
4333 RegExpMacroAssembler* assembler = compiler->macro_assembler();
4334 if (!trace->is_trivial()) {
4335 trace->Flush(compiler, this);
4339 LimitResult limit_result = LimitVersions(compiler, trace);
4340 if (limit_result == DONE) return;
4341 DCHECK(limit_result == CONTINUE);
4343 RecursionCheck rc(compiler);
4345 DCHECK_EQ(start_reg_ + 1, end_reg_);
4346 if (compiler->ignore_case()) {
4347 assembler->CheckNotBackReferenceIgnoreCase(start_reg_,
4348 trace->backtrack());
4350 assembler->CheckNotBackReference(start_reg_, trace->backtrack());
4352 on_success()->Emit(compiler, trace);
4356 // -------------------------------------------------------------------
4363 class DotPrinter: public NodeVisitor {
4365 DotPrinter(std::ostream& os, bool ignore_case) // NOLINT
4367 ignore_case_(ignore_case) {}
4368 void PrintNode(const char* label, RegExpNode* node);
4369 void Visit(RegExpNode* node);
4370 void PrintAttributes(RegExpNode* from);
4371 void PrintOnFailure(RegExpNode* from, RegExpNode* to);
4372 #define DECLARE_VISIT(Type) \
4373 virtual void Visit##Type(Type##Node* that);
4374 FOR_EACH_NODE_TYPE(DECLARE_VISIT)
4375 #undef DECLARE_VISIT
4382 void DotPrinter::PrintNode(const char* label, RegExpNode* node) {
4383 os_ << "digraph G {\n graph [label=\"";
4384 for (int i = 0; label[i]; i++) {
4399 os_ << "}" << std::endl;
4403 void DotPrinter::Visit(RegExpNode* node) {
4404 if (node->info()->visited) return;
4405 node->info()->visited = true;
4410 void DotPrinter::PrintOnFailure(RegExpNode* from, RegExpNode* on_failure) {
4411 os_ << " n" << from << " -> n" << on_failure << " [style=dotted];\n";
4416 class TableEntryBodyPrinter {
4418 TableEntryBodyPrinter(std::ostream& os, ChoiceNode* choice) // NOLINT
4421 void Call(uc16 from, DispatchTable::Entry entry) {
4422 OutSet* out_set = entry.out_set();
4423 for (unsigned i = 0; i < OutSet::kFirstLimit; i++) {
4424 if (out_set->Get(i)) {
4425 os_ << " n" << choice() << ":s" << from << "o" << i << " -> n"
4426 << choice()->alternatives()->at(i).node() << ";\n";
4431 ChoiceNode* choice() { return choice_; }
4433 ChoiceNode* choice_;
4437 class TableEntryHeaderPrinter {
4439 explicit TableEntryHeaderPrinter(std::ostream& os) // NOLINT
4442 void Call(uc16 from, DispatchTable::Entry entry) {
4448 os_ << "{\\" << AsUC16(from) << "-\\" << AsUC16(entry.to()) << "|{";
4449 OutSet* out_set = entry.out_set();
4451 for (unsigned i = 0; i < OutSet::kFirstLimit; i++) {
4452 if (out_set->Get(i)) {
4453 if (priority > 0) os_ << "|";
4454 os_ << "<s" << from << "o" << i << "> " << priority;
4467 class AttributePrinter {
4469 explicit AttributePrinter(std::ostream& os) // NOLINT
4472 void PrintSeparator() {
4479 void PrintBit(const char* name, bool value) {
4482 os_ << "{" << name << "}";
4484 void PrintPositive(const char* name, int value) {
4485 if (value < 0) return;
4487 os_ << "{" << name << "|" << value << "}";
4496 void DotPrinter::PrintAttributes(RegExpNode* that) {
4497 os_ << " a" << that << " [shape=Mrecord, color=grey, fontcolor=grey, "
4498 << "margin=0.1, fontsize=10, label=\"{";
4499 AttributePrinter printer(os_);
4500 NodeInfo* info = that->info();
4501 printer.PrintBit("NI", info->follows_newline_interest);
4502 printer.PrintBit("WI", info->follows_word_interest);
4503 printer.PrintBit("SI", info->follows_start_interest);
4504 Label* label = that->label();
4505 if (label->is_bound())
4506 printer.PrintPositive("@", label->pos());
4508 << " a" << that << " -> n" << that
4509 << " [style=dashed, color=grey, arrowhead=none];\n";
4513 static const bool kPrintDispatchTable = false;
4514 void DotPrinter::VisitChoice(ChoiceNode* that) {
4515 if (kPrintDispatchTable) {
4516 os_ << " n" << that << " [shape=Mrecord, label=\"";
4517 TableEntryHeaderPrinter header_printer(os_);
4518 that->GetTable(ignore_case_)->ForEach(&header_printer);
4520 PrintAttributes(that);
4521 TableEntryBodyPrinter body_printer(os_, that);
4522 that->GetTable(ignore_case_)->ForEach(&body_printer);
4524 os_ << " n" << that << " [shape=Mrecord, label=\"?\"];\n";
4525 for (int i = 0; i < that->alternatives()->length(); i++) {
4526 GuardedAlternative alt = that->alternatives()->at(i);
4527 os_ << " n" << that << " -> n" << alt.node();
4530 for (int i = 0; i < that->alternatives()->length(); i++) {
4531 GuardedAlternative alt = that->alternatives()->at(i);
4532 alt.node()->Accept(this);
4537 void DotPrinter::VisitText(TextNode* that) {
4538 Zone* zone = that->zone();
4539 os_ << " n" << that << " [label=\"";
4540 for (int i = 0; i < that->elements()->length(); i++) {
4541 if (i > 0) os_ << " ";
4542 TextElement elm = that->elements()->at(i);
4543 switch (elm.text_type()) {
4544 case TextElement::ATOM: {
4545 Vector<const uc16> data = elm.atom()->data();
4546 for (int i = 0; i < data.length(); i++) {
4547 os_ << static_cast<char>(data[i]);
4551 case TextElement::CHAR_CLASS: {
4552 RegExpCharacterClass* node = elm.char_class();
4554 if (node->is_negated()) os_ << "^";
4555 for (int j = 0; j < node->ranges(zone)->length(); j++) {
4556 CharacterRange range = node->ranges(zone)->at(j);
4557 os_ << AsUC16(range.from()) << "-" << AsUC16(range.to());
4566 os_ << "\", shape=box, peripheries=2];\n";
4567 PrintAttributes(that);
4568 os_ << " n" << that << " -> n" << that->on_success() << ";\n";
4569 Visit(that->on_success());
4573 void DotPrinter::VisitBackReference(BackReferenceNode* that) {
4574 os_ << " n" << that << " [label=\"$" << that->start_register() << "..$"
4575 << that->end_register() << "\", shape=doubleoctagon];\n";
4576 PrintAttributes(that);
4577 os_ << " n" << that << " -> n" << that->on_success() << ";\n";
4578 Visit(that->on_success());
4582 void DotPrinter::VisitEnd(EndNode* that) {
4583 os_ << " n" << that << " [style=bold, shape=point];\n";
4584 PrintAttributes(that);
4588 void DotPrinter::VisitAssertion(AssertionNode* that) {
4589 os_ << " n" << that << " [";
4590 switch (that->assertion_type()) {
4591 case AssertionNode::AT_END:
4592 os_ << "label=\"$\", shape=septagon";
4594 case AssertionNode::AT_START:
4595 os_ << "label=\"^\", shape=septagon";
4597 case AssertionNode::AT_BOUNDARY:
4598 os_ << "label=\"\\b\", shape=septagon";
4600 case AssertionNode::AT_NON_BOUNDARY:
4601 os_ << "label=\"\\B\", shape=septagon";
4603 case AssertionNode::AFTER_NEWLINE:
4604 os_ << "label=\"(?<=\\n)\", shape=septagon";
4608 PrintAttributes(that);
4609 RegExpNode* successor = that->on_success();
4610 os_ << " n" << that << " -> n" << successor << ";\n";
4615 void DotPrinter::VisitAction(ActionNode* that) {
4616 os_ << " n" << that << " [";
4617 switch (that->action_type_) {
4618 case ActionNode::SET_REGISTER:
4619 os_ << "label=\"$" << that->data_.u_store_register.reg
4620 << ":=" << that->data_.u_store_register.value << "\", shape=octagon";
4622 case ActionNode::INCREMENT_REGISTER:
4623 os_ << "label=\"$" << that->data_.u_increment_register.reg
4624 << "++\", shape=octagon";
4626 case ActionNode::STORE_POSITION:
4627 os_ << "label=\"$" << that->data_.u_position_register.reg
4628 << ":=$pos\", shape=octagon";
4630 case ActionNode::BEGIN_SUBMATCH:
4631 os_ << "label=\"$" << that->data_.u_submatch.current_position_register
4632 << ":=$pos,begin\", shape=septagon";
4634 case ActionNode::POSITIVE_SUBMATCH_SUCCESS:
4635 os_ << "label=\"escape\", shape=septagon";
4637 case ActionNode::EMPTY_MATCH_CHECK:
4638 os_ << "label=\"$" << that->data_.u_empty_match_check.start_register
4639 << "=$pos?,$" << that->data_.u_empty_match_check.repetition_register
4640 << "<" << that->data_.u_empty_match_check.repetition_limit
4641 << "?\", shape=septagon";
4643 case ActionNode::CLEAR_CAPTURES: {
4644 os_ << "label=\"clear $" << that->data_.u_clear_captures.range_from
4645 << " to $" << that->data_.u_clear_captures.range_to
4646 << "\", shape=septagon";
4651 PrintAttributes(that);
4652 RegExpNode* successor = that->on_success();
4653 os_ << " n" << that << " -> n" << successor << ";\n";
4658 class DispatchTableDumper {
4660 explicit DispatchTableDumper(std::ostream& os) : os_(os) {}
4661 void Call(uc16 key, DispatchTable::Entry entry);
4667 void DispatchTableDumper::Call(uc16 key, DispatchTable::Entry entry) {
4668 os_ << "[" << AsUC16(key) << "-" << AsUC16(entry.to()) << "]: {";
4669 OutSet* set = entry.out_set();
4671 for (unsigned i = 0; i < OutSet::kFirstLimit; i++) {
4685 void DispatchTable::Dump() {
4686 OFStream os(stderr);
4687 DispatchTableDumper dumper(os);
4688 tree()->ForEach(&dumper);
4692 void RegExpEngine::DotPrint(const char* label,
4695 OFStream os(stdout);
4696 DotPrinter printer(os, ignore_case);
4697 printer.PrintNode(label, node);
4704 // -------------------------------------------------------------------
4705 // Tree to graph conversion
4707 RegExpNode* RegExpAtom::ToNode(RegExpCompiler* compiler,
4708 RegExpNode* on_success) {
4709 ZoneList<TextElement>* elms =
4710 new(compiler->zone()) ZoneList<TextElement>(1, compiler->zone());
4711 elms->Add(TextElement::Atom(this), compiler->zone());
4712 return new(compiler->zone()) TextNode(elms, on_success);
4716 RegExpNode* RegExpText::ToNode(RegExpCompiler* compiler,
4717 RegExpNode* on_success) {
4718 return new(compiler->zone()) TextNode(elements(), on_success);
4722 static bool CompareInverseRanges(ZoneList<CharacterRange>* ranges,
4723 const int* special_class,
4725 length--; // Remove final 0x10000.
4726 DCHECK(special_class[length] == 0x10000);
4727 DCHECK(ranges->length() != 0);
4728 DCHECK(length != 0);
4729 DCHECK(special_class[0] != 0);
4730 if (ranges->length() != (length >> 1) + 1) {
4733 CharacterRange range = ranges->at(0);
4734 if (range.from() != 0) {
4737 for (int i = 0; i < length; i += 2) {
4738 if (special_class[i] != (range.to() + 1)) {
4741 range = ranges->at((i >> 1) + 1);
4742 if (special_class[i+1] != range.from()) {
4746 if (range.to() != 0xffff) {
4753 static bool CompareRanges(ZoneList<CharacterRange>* ranges,
4754 const int* special_class,
4756 length--; // Remove final 0x10000.
4757 DCHECK(special_class[length] == 0x10000);
4758 if (ranges->length() * 2 != length) {
4761 for (int i = 0; i < length; i += 2) {
4762 CharacterRange range = ranges->at(i >> 1);
4763 if (range.from() != special_class[i] ||
4764 range.to() != special_class[i + 1] - 1) {
4772 bool RegExpCharacterClass::is_standard(Zone* zone) {
4773 // TODO(lrn): Remove need for this function, by not throwing away information
4778 if (set_.is_standard()) {
4781 if (CompareRanges(set_.ranges(zone), kSpaceRanges, kSpaceRangeCount)) {
4782 set_.set_standard_set_type('s');
4785 if (CompareInverseRanges(set_.ranges(zone), kSpaceRanges, kSpaceRangeCount)) {
4786 set_.set_standard_set_type('S');
4789 if (CompareInverseRanges(set_.ranges(zone),
4790 kLineTerminatorRanges,
4791 kLineTerminatorRangeCount)) {
4792 set_.set_standard_set_type('.');
4795 if (CompareRanges(set_.ranges(zone),
4796 kLineTerminatorRanges,
4797 kLineTerminatorRangeCount)) {
4798 set_.set_standard_set_type('n');
4801 if (CompareRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) {
4802 set_.set_standard_set_type('w');
4805 if (CompareInverseRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) {
4806 set_.set_standard_set_type('W');
4813 RegExpNode* RegExpCharacterClass::ToNode(RegExpCompiler* compiler,
4814 RegExpNode* on_success) {
4815 return new(compiler->zone()) TextNode(this, on_success);
4819 RegExpNode* RegExpDisjunction::ToNode(RegExpCompiler* compiler,
4820 RegExpNode* on_success) {
4821 ZoneList<RegExpTree*>* alternatives = this->alternatives();
4822 int length = alternatives->length();
4823 ChoiceNode* result =
4824 new(compiler->zone()) ChoiceNode(length, compiler->zone());
4825 for (int i = 0; i < length; i++) {
4826 GuardedAlternative alternative(alternatives->at(i)->ToNode(compiler,
4828 result->AddAlternative(alternative);
4834 RegExpNode* RegExpQuantifier::ToNode(RegExpCompiler* compiler,
4835 RegExpNode* on_success) {
4836 return ToNode(min(),
4845 // Scoped object to keep track of how much we unroll quantifier loops in the
4846 // regexp graph generator.
4847 class RegExpExpansionLimiter {
4849 static const int kMaxExpansionFactor = 6;
4850 RegExpExpansionLimiter(RegExpCompiler* compiler, int factor)
4851 : compiler_(compiler),
4852 saved_expansion_factor_(compiler->current_expansion_factor()),
4853 ok_to_expand_(saved_expansion_factor_ <= kMaxExpansionFactor) {
4855 if (ok_to_expand_) {
4856 if (factor > kMaxExpansionFactor) {
4857 // Avoid integer overflow of the current expansion factor.
4858 ok_to_expand_ = false;
4859 compiler->set_current_expansion_factor(kMaxExpansionFactor + 1);
4861 int new_factor = saved_expansion_factor_ * factor;
4862 ok_to_expand_ = (new_factor <= kMaxExpansionFactor);
4863 compiler->set_current_expansion_factor(new_factor);
4868 ~RegExpExpansionLimiter() {
4869 compiler_->set_current_expansion_factor(saved_expansion_factor_);
4872 bool ok_to_expand() { return ok_to_expand_; }
4875 RegExpCompiler* compiler_;
4876 int saved_expansion_factor_;
4879 DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpExpansionLimiter);
4883 RegExpNode* RegExpQuantifier::ToNode(int min,
4887 RegExpCompiler* compiler,
4888 RegExpNode* on_success,
4889 bool not_at_start) {
4890 // x{f, t} becomes this:
4896 // (r=0)-->(?)---/ [if r < t]
4898 // [if r >= f] \----> ...
4901 // 15.10.2.5 RepeatMatcher algorithm.
4902 // The parser has already eliminated the case where max is 0. In the case
4903 // where max_match is zero the parser has removed the quantifier if min was
4904 // > 0 and removed the atom if min was 0. See AddQuantifierToAtom.
4906 // If we know that we cannot match zero length then things are a little
4907 // simpler since we don't need to make the special zero length match check
4908 // from step 2.1. If the min and max are small we can unroll a little in
4910 static const int kMaxUnrolledMinMatches = 3; // Unroll (foo)+ and (foo){3,}
4911 static const int kMaxUnrolledMaxMatches = 3; // Unroll (foo)? and (foo){x,3}
4912 if (max == 0) return on_success; // This can happen due to recursion.
4913 bool body_can_be_empty = (body->min_match() == 0);
4914 int body_start_reg = RegExpCompiler::kNoRegister;
4915 Interval capture_registers = body->CaptureRegisters();
4916 bool needs_capture_clearing = !capture_registers.is_empty();
4917 Zone* zone = compiler->zone();
4919 if (body_can_be_empty) {
4920 body_start_reg = compiler->AllocateRegister();
4921 } else if (compiler->optimize() && !needs_capture_clearing) {
4922 // Only unroll if there are no captures and the body can't be
4925 RegExpExpansionLimiter limiter(
4926 compiler, min + ((max != min) ? 1 : 0));
4927 if (min > 0 && min <= kMaxUnrolledMinMatches && limiter.ok_to_expand()) {
4928 int new_max = (max == kInfinity) ? max : max - min;
4929 // Recurse once to get the loop or optional matches after the fixed
4931 RegExpNode* answer = ToNode(
4932 0, new_max, is_greedy, body, compiler, on_success, true);
4933 // Unroll the forced matches from 0 to min. This can cause chains of
4934 // TextNodes (which the parser does not generate). These should be
4935 // combined if it turns out they hinder good code generation.
4936 for (int i = 0; i < min; i++) {
4937 answer = body->ToNode(compiler, answer);
4942 if (max <= kMaxUnrolledMaxMatches && min == 0) {
4943 DCHECK(max > 0); // Due to the 'if' above.
4944 RegExpExpansionLimiter limiter(compiler, max);
4945 if (limiter.ok_to_expand()) {
4946 // Unroll the optional matches up to max.
4947 RegExpNode* answer = on_success;
4948 for (int i = 0; i < max; i++) {
4949 ChoiceNode* alternation = new(zone) ChoiceNode(2, zone);
4951 alternation->AddAlternative(
4952 GuardedAlternative(body->ToNode(compiler, answer)));
4953 alternation->AddAlternative(GuardedAlternative(on_success));
4955 alternation->AddAlternative(GuardedAlternative(on_success));
4956 alternation->AddAlternative(
4957 GuardedAlternative(body->ToNode(compiler, answer)));
4959 answer = alternation;
4960 if (not_at_start) alternation->set_not_at_start();
4966 bool has_min = min > 0;
4967 bool has_max = max < RegExpTree::kInfinity;
4968 bool needs_counter = has_min || has_max;
4969 int reg_ctr = needs_counter
4970 ? compiler->AllocateRegister()
4971 : RegExpCompiler::kNoRegister;
4972 LoopChoiceNode* center = new(zone) LoopChoiceNode(body->min_match() == 0,
4974 if (not_at_start) center->set_not_at_start();
4975 RegExpNode* loop_return = needs_counter
4976 ? static_cast<RegExpNode*>(ActionNode::IncrementRegister(reg_ctr, center))
4977 : static_cast<RegExpNode*>(center);
4978 if (body_can_be_empty) {
4979 // If the body can be empty we need to check if it was and then
4981 loop_return = ActionNode::EmptyMatchCheck(body_start_reg,
4986 RegExpNode* body_node = body->ToNode(compiler, loop_return);
4987 if (body_can_be_empty) {
4988 // If the body can be empty we need to store the start position
4989 // so we can bail out if it was empty.
4990 body_node = ActionNode::StorePosition(body_start_reg, false, body_node);
4992 if (needs_capture_clearing) {
4993 // Before entering the body of this loop we need to clear captures.
4994 body_node = ActionNode::ClearCaptures(capture_registers, body_node);
4996 GuardedAlternative body_alt(body_node);
4999 new(zone) Guard(reg_ctr, Guard::LT, max);
5000 body_alt.AddGuard(body_guard, zone);
5002 GuardedAlternative rest_alt(on_success);
5004 Guard* rest_guard = new(compiler->zone()) Guard(reg_ctr, Guard::GEQ, min);
5005 rest_alt.AddGuard(rest_guard, zone);
5008 center->AddLoopAlternative(body_alt);
5009 center->AddContinueAlternative(rest_alt);
5011 center->AddContinueAlternative(rest_alt);
5012 center->AddLoopAlternative(body_alt);
5014 if (needs_counter) {
5015 return ActionNode::SetRegister(reg_ctr, 0, center);
5022 RegExpNode* RegExpAssertion::ToNode(RegExpCompiler* compiler,
5023 RegExpNode* on_success) {
5025 Zone* zone = compiler->zone();
5027 switch (assertion_type()) {
5029 return AssertionNode::AfterNewline(on_success);
5030 case START_OF_INPUT:
5031 return AssertionNode::AtStart(on_success);
5033 return AssertionNode::AtBoundary(on_success);
5035 return AssertionNode::AtNonBoundary(on_success);
5037 return AssertionNode::AtEnd(on_success);
5039 // Compile $ in multiline regexps as an alternation with a positive
5040 // lookahead in one side and an end-of-input on the other side.
5041 // We need two registers for the lookahead.
5042 int stack_pointer_register = compiler->AllocateRegister();
5043 int position_register = compiler->AllocateRegister();
5044 // The ChoiceNode to distinguish between a newline and end-of-input.
5045 ChoiceNode* result = new(zone) ChoiceNode(2, zone);
5046 // Create a newline atom.
5047 ZoneList<CharacterRange>* newline_ranges =
5048 new(zone) ZoneList<CharacterRange>(3, zone);
5049 CharacterRange::AddClassEscape('n', newline_ranges, zone);
5050 RegExpCharacterClass* newline_atom = new(zone) RegExpCharacterClass('n');
5051 TextNode* newline_matcher = new(zone) TextNode(
5053 ActionNode::PositiveSubmatchSuccess(stack_pointer_register,
5055 0, // No captures inside.
5056 -1, // Ignored if no captures.
5058 // Create an end-of-input matcher.
5059 RegExpNode* end_of_line = ActionNode::BeginSubmatch(
5060 stack_pointer_register,
5063 // Add the two alternatives to the ChoiceNode.
5064 GuardedAlternative eol_alternative(end_of_line);
5065 result->AddAlternative(eol_alternative);
5066 GuardedAlternative end_alternative(AssertionNode::AtEnd(on_success));
5067 result->AddAlternative(end_alternative);
5077 RegExpNode* RegExpBackReference::ToNode(RegExpCompiler* compiler,
5078 RegExpNode* on_success) {
5079 return new(compiler->zone())
5080 BackReferenceNode(RegExpCapture::StartRegister(index()),
5081 RegExpCapture::EndRegister(index()),
5086 RegExpNode* RegExpEmpty::ToNode(RegExpCompiler* compiler,
5087 RegExpNode* on_success) {
5092 RegExpNode* RegExpLookahead::ToNode(RegExpCompiler* compiler,
5093 RegExpNode* on_success) {
5094 int stack_pointer_register = compiler->AllocateRegister();
5095 int position_register = compiler->AllocateRegister();
5097 const int registers_per_capture = 2;
5098 const int register_of_first_capture = 2;
5099 int register_count = capture_count_ * registers_per_capture;
5100 int register_start =
5101 register_of_first_capture + capture_from_ * registers_per_capture;
5103 RegExpNode* success;
5104 if (is_positive()) {
5105 RegExpNode* node = ActionNode::BeginSubmatch(
5106 stack_pointer_register,
5110 ActionNode::PositiveSubmatchSuccess(stack_pointer_register,
5117 // We use a ChoiceNode for a negative lookahead because it has most of
5118 // the characteristics we need. It has the body of the lookahead as its
5119 // first alternative and the expression after the lookahead of the second
5120 // alternative. If the first alternative succeeds then the
5121 // NegativeSubmatchSuccess will unwind the stack including everything the
5122 // choice node set up and backtrack. If the first alternative fails then
5123 // the second alternative is tried, which is exactly the desired result
5124 // for a negative lookahead. The NegativeLookaheadChoiceNode is a special
5125 // ChoiceNode that knows to ignore the first exit when calculating quick
5127 Zone* zone = compiler->zone();
5129 GuardedAlternative body_alt(
5132 success = new(zone) NegativeSubmatchSuccess(stack_pointer_register,
5137 ChoiceNode* choice_node =
5138 new(zone) NegativeLookaheadChoiceNode(body_alt,
5139 GuardedAlternative(on_success),
5141 return ActionNode::BeginSubmatch(stack_pointer_register,
5148 RegExpNode* RegExpCapture::ToNode(RegExpCompiler* compiler,
5149 RegExpNode* on_success) {
5150 return ToNode(body(), index(), compiler, on_success);
5154 RegExpNode* RegExpCapture::ToNode(RegExpTree* body,
5156 RegExpCompiler* compiler,
5157 RegExpNode* on_success) {
5158 int start_reg = RegExpCapture::StartRegister(index);
5159 int end_reg = RegExpCapture::EndRegister(index);
5160 RegExpNode* store_end = ActionNode::StorePosition(end_reg, true, on_success);
5161 RegExpNode* body_node = body->ToNode(compiler, store_end);
5162 return ActionNode::StorePosition(start_reg, true, body_node);
5166 RegExpNode* RegExpAlternative::ToNode(RegExpCompiler* compiler,
5167 RegExpNode* on_success) {
5168 ZoneList<RegExpTree*>* children = nodes();
5169 RegExpNode* current = on_success;
5170 for (int i = children->length() - 1; i >= 0; i--) {
5171 current = children->at(i)->ToNode(compiler, current);
5177 static void AddClass(const int* elmv,
5179 ZoneList<CharacterRange>* ranges,
5182 DCHECK(elmv[elmc] == 0x10000);
5183 for (int i = 0; i < elmc; i += 2) {
5184 DCHECK(elmv[i] < elmv[i + 1]);
5185 ranges->Add(CharacterRange(elmv[i], elmv[i + 1] - 1), zone);
5190 static void AddClassNegated(const int *elmv,
5192 ZoneList<CharacterRange>* ranges,
5195 DCHECK(elmv[elmc] == 0x10000);
5196 DCHECK(elmv[0] != 0x0000);
5197 DCHECK(elmv[elmc-1] != String::kMaxUtf16CodeUnit);
5199 for (int i = 0; i < elmc; i += 2) {
5200 DCHECK(last <= elmv[i] - 1);
5201 DCHECK(elmv[i] < elmv[i + 1]);
5202 ranges->Add(CharacterRange(last, elmv[i] - 1), zone);
5205 ranges->Add(CharacterRange(last, String::kMaxUtf16CodeUnit), zone);
5209 void CharacterRange::AddClassEscape(uc16 type,
5210 ZoneList<CharacterRange>* ranges,
5214 AddClass(kSpaceRanges, kSpaceRangeCount, ranges, zone);
5217 AddClassNegated(kSpaceRanges, kSpaceRangeCount, ranges, zone);
5220 AddClass(kWordRanges, kWordRangeCount, ranges, zone);
5223 AddClassNegated(kWordRanges, kWordRangeCount, ranges, zone);
5226 AddClass(kDigitRanges, kDigitRangeCount, ranges, zone);
5229 AddClassNegated(kDigitRanges, kDigitRangeCount, ranges, zone);
5232 AddClassNegated(kLineTerminatorRanges,
5233 kLineTerminatorRangeCount,
5237 // This is not a character range as defined by the spec but a
5238 // convenient shorthand for a character class that matches any
5241 ranges->Add(CharacterRange::Everything(), zone);
5243 // This is the set of characters matched by the $ and ^ symbols
5244 // in multiline mode.
5246 AddClass(kLineTerminatorRanges,
5247 kLineTerminatorRangeCount,
5257 Vector<const int> CharacterRange::GetWordBounds() {
5258 return Vector<const int>(kWordRanges, kWordRangeCount - 1);
5262 class CharacterRangeSplitter {
5264 CharacterRangeSplitter(ZoneList<CharacterRange>** included,
5265 ZoneList<CharacterRange>** excluded,
5267 : included_(included),
5268 excluded_(excluded),
5270 void Call(uc16 from, DispatchTable::Entry entry);
5272 static const int kInBase = 0;
5273 static const int kInOverlay = 1;
5276 ZoneList<CharacterRange>** included_;
5277 ZoneList<CharacterRange>** excluded_;
5282 void CharacterRangeSplitter::Call(uc16 from, DispatchTable::Entry entry) {
5283 if (!entry.out_set()->Get(kInBase)) return;
5284 ZoneList<CharacterRange>** target = entry.out_set()->Get(kInOverlay)
5287 if (*target == NULL) *target = new(zone_) ZoneList<CharacterRange>(2, zone_);
5288 (*target)->Add(CharacterRange(entry.from(), entry.to()), zone_);
5292 void CharacterRange::Split(ZoneList<CharacterRange>* base,
5293 Vector<const int> overlay,
5294 ZoneList<CharacterRange>** included,
5295 ZoneList<CharacterRange>** excluded,
5297 DCHECK_NULL(*included);
5298 DCHECK_NULL(*excluded);
5299 DispatchTable table(zone);
5300 for (int i = 0; i < base->length(); i++)
5301 table.AddRange(base->at(i), CharacterRangeSplitter::kInBase, zone);
5302 for (int i = 0; i < overlay.length(); i += 2) {
5303 table.AddRange(CharacterRange(overlay[i], overlay[i + 1] - 1),
5304 CharacterRangeSplitter::kInOverlay, zone);
5306 CharacterRangeSplitter callback(included, excluded, zone);
5307 table.ForEach(&callback);
5311 void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone,
5312 ZoneList<CharacterRange>* ranges,
5314 uc16 bottom = from();
5316 if (is_one_byte && !RangeContainsLatin1Equivalents(*this)) {
5317 if (bottom > String::kMaxOneByteCharCode) return;
5318 if (top > String::kMaxOneByteCharCode) top = String::kMaxOneByteCharCode;
5320 unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
5321 if (top == bottom) {
5322 // If this is a singleton we just expand the one character.
5323 int length = isolate->jsregexp_uncanonicalize()->get(bottom, '\0', chars);
5324 for (int i = 0; i < length; i++) {
5325 uc32 chr = chars[i];
5326 if (chr != bottom) {
5327 ranges->Add(CharacterRange::Singleton(chars[i]), zone);
5331 // If this is a range we expand the characters block by block,
5332 // expanding contiguous subranges (blocks) one at a time.
5333 // The approach is as follows. For a given start character we
5334 // look up the remainder of the block that contains it (represented
5335 // by the end point), for instance we find 'z' if the character
5336 // is 'c'. A block is characterized by the property
5337 // that all characters uncanonicalize in the same way, except that
5338 // each entry in the result is incremented by the distance from the first
5339 // element. So a-z is a block because 'a' uncanonicalizes to ['a', 'A'] and
5340 // the k'th letter uncanonicalizes to ['a' + k, 'A' + k].
5341 // Once we've found the end point we look up its uncanonicalization
5342 // and produce a range for each element. For instance for [c-f]
5343 // we look up ['z', 'Z'] and produce [c-f] and [C-F]. We then only
5344 // add a range if it is not already contained in the input, so [c-f]
5345 // will be skipped but [C-F] will be added. If this range is not
5346 // completely contained in a block we do this for all the blocks
5347 // covered by the range (handling characters that is not in a block
5348 // as a "singleton block").
5349 unibrow::uchar range[unibrow::Ecma262UnCanonicalize::kMaxWidth];
5351 while (pos <= top) {
5352 int length = isolate->jsregexp_canonrange()->get(pos, '\0', range);
5357 DCHECK_EQ(1, length);
5358 block_end = range[0];
5360 int end = (block_end > top) ? top : block_end;
5361 length = isolate->jsregexp_uncanonicalize()->get(block_end, '\0', range);
5362 for (int i = 0; i < length; i++) {
5364 uc16 range_from = c - (block_end - pos);
5365 uc16 range_to = c - (block_end - end);
5366 if (!(bottom <= range_from && range_to <= top)) {
5367 ranges->Add(CharacterRange(range_from, range_to), zone);
5376 bool CharacterRange::IsCanonical(ZoneList<CharacterRange>* ranges) {
5377 DCHECK_NOT_NULL(ranges);
5378 int n = ranges->length();
5379 if (n <= 1) return true;
5380 int max = ranges->at(0).to();
5381 for (int i = 1; i < n; i++) {
5382 CharacterRange next_range = ranges->at(i);
5383 if (next_range.from() <= max + 1) return false;
5384 max = next_range.to();
5390 ZoneList<CharacterRange>* CharacterSet::ranges(Zone* zone) {
5391 if (ranges_ == NULL) {
5392 ranges_ = new(zone) ZoneList<CharacterRange>(2, zone);
5393 CharacterRange::AddClassEscape(standard_set_type_, ranges_, zone);
5399 // Move a number of elements in a zonelist to another position
5400 // in the same list. Handles overlapping source and target areas.
5401 static void MoveRanges(ZoneList<CharacterRange>* list,
5405 // Ranges are potentially overlapping.
5407 for (int i = count - 1; i >= 0; i--) {
5408 list->at(to + i) = list->at(from + i);
5411 for (int i = 0; i < count; i++) {
5412 list->at(to + i) = list->at(from + i);
5418 static int InsertRangeInCanonicalList(ZoneList<CharacterRange>* list,
5420 CharacterRange insert) {
5421 // Inserts a range into list[0..count[, which must be sorted
5422 // by from value and non-overlapping and non-adjacent, using at most
5423 // list[0..count] for the result. Returns the number of resulting
5424 // canonicalized ranges. Inserting a range may collapse existing ranges into
5425 // fewer ranges, so the return value can be anything in the range 1..count+1.
5426 uc16 from = insert.from();
5427 uc16 to = insert.to();
5429 int end_pos = count;
5430 for (int i = count - 1; i >= 0; i--) {
5431 CharacterRange current = list->at(i);
5432 if (current.from() > to + 1) {
5434 } else if (current.to() + 1 < from) {
5440 // Inserted range overlaps, or is adjacent to, ranges at positions
5441 // [start_pos..end_pos[. Ranges before start_pos or at or after end_pos are
5442 // not affected by the insertion.
5443 // If start_pos == end_pos, the range must be inserted before start_pos.
5444 // if start_pos < end_pos, the entire range from start_pos to end_pos
5445 // must be merged with the insert range.
5447 if (start_pos == end_pos) {
5448 // Insert between existing ranges at position start_pos.
5449 if (start_pos < count) {
5450 MoveRanges(list, start_pos, start_pos + 1, count - start_pos);
5452 list->at(start_pos) = insert;
5455 if (start_pos + 1 == end_pos) {
5456 // Replace single existing range at position start_pos.
5457 CharacterRange to_replace = list->at(start_pos);
5458 int new_from = Min(to_replace.from(), from);
5459 int new_to = Max(to_replace.to(), to);
5460 list->at(start_pos) = CharacterRange(new_from, new_to);
5463 // Replace a number of existing ranges from start_pos to end_pos - 1.
5464 // Move the remaining ranges down.
5466 int new_from = Min(list->at(start_pos).from(), from);
5467 int new_to = Max(list->at(end_pos - 1).to(), to);
5468 if (end_pos < count) {
5469 MoveRanges(list, end_pos, start_pos + 1, count - end_pos);
5471 list->at(start_pos) = CharacterRange(new_from, new_to);
5472 return count - (end_pos - start_pos) + 1;
5476 void CharacterSet::Canonicalize() {
5477 // Special/default classes are always considered canonical. The result
5478 // of calling ranges() will be sorted.
5479 if (ranges_ == NULL) return;
5480 CharacterRange::Canonicalize(ranges_);
5484 void CharacterRange::Canonicalize(ZoneList<CharacterRange>* character_ranges) {
5485 if (character_ranges->length() <= 1) return;
5486 // Check whether ranges are already canonical (increasing, non-overlapping,
5488 int n = character_ranges->length();
5489 int max = character_ranges->at(0).to();
5492 CharacterRange current = character_ranges->at(i);
5493 if (current.from() <= max + 1) {
5499 // Canonical until the i'th range. If that's all of them, we are done.
5502 // The ranges at index i and forward are not canonicalized. Make them so by
5503 // doing the equivalent of insertion sort (inserting each into the previous
5505 // Notice that inserting a range can reduce the number of ranges in the
5506 // result due to combining of adjacent and overlapping ranges.
5507 int read = i; // Range to insert.
5508 int num_canonical = i; // Length of canonicalized part of list.
5510 num_canonical = InsertRangeInCanonicalList(character_ranges,
5512 character_ranges->at(read));
5515 character_ranges->Rewind(num_canonical);
5517 DCHECK(CharacterRange::IsCanonical(character_ranges));
5521 void CharacterRange::Negate(ZoneList<CharacterRange>* ranges,
5522 ZoneList<CharacterRange>* negated_ranges,
5524 DCHECK(CharacterRange::IsCanonical(ranges));
5525 DCHECK_EQ(0, negated_ranges->length());
5526 int range_count = ranges->length();
5529 if (range_count > 0 && ranges->at(0).from() == 0) {
5530 from = ranges->at(0).to();
5533 while (i < range_count) {
5534 CharacterRange range = ranges->at(i);
5535 negated_ranges->Add(CharacterRange(from + 1, range.from() - 1), zone);
5539 if (from < String::kMaxUtf16CodeUnit) {
5540 negated_ranges->Add(CharacterRange(from + 1, String::kMaxUtf16CodeUnit),
5546 // -------------------------------------------------------------------
5550 OutSet* OutSet::Extend(unsigned value, Zone* zone) {
5553 if (successors(zone) != NULL) {
5554 for (int i = 0; i < successors(zone)->length(); i++) {
5555 OutSet* successor = successors(zone)->at(i);
5556 if (successor->Get(value))
5560 successors_ = new(zone) ZoneList<OutSet*>(2, zone);
5562 OutSet* result = new(zone) OutSet(first_, remaining_);
5563 result->Set(value, zone);
5564 successors(zone)->Add(result, zone);
5569 void OutSet::Set(unsigned value, Zone *zone) {
5570 if (value < kFirstLimit) {
5571 first_ |= (1 << value);
5573 if (remaining_ == NULL)
5574 remaining_ = new(zone) ZoneList<unsigned>(1, zone);
5575 if (remaining_->is_empty() || !remaining_->Contains(value))
5576 remaining_->Add(value, zone);
5581 bool OutSet::Get(unsigned value) const {
5582 if (value < kFirstLimit) {
5583 return (first_ & (1 << value)) != 0;
5584 } else if (remaining_ == NULL) {
5587 return remaining_->Contains(value);
5592 const uc16 DispatchTable::Config::kNoKey = unibrow::Utf8::kBadChar;
5595 void DispatchTable::AddRange(CharacterRange full_range, int value,
5597 CharacterRange current = full_range;
5598 if (tree()->is_empty()) {
5599 // If this is the first range we just insert into the table.
5600 ZoneSplayTree<Config>::Locator loc;
5601 bool inserted = tree()->Insert(current.from(), &loc);
5604 loc.set_value(Entry(current.from(), current.to(),
5605 empty()->Extend(value, zone)));
5608 // First see if there is a range to the left of this one that
5610 ZoneSplayTree<Config>::Locator loc;
5611 if (tree()->FindGreatestLessThan(current.from(), &loc)) {
5612 Entry* entry = &loc.value();
5613 // If we've found a range that overlaps with this one, and it
5614 // starts strictly to the left of this one, we have to fix it
5615 // because the following code only handles ranges that start on
5616 // or after the start point of the range we're adding.
5617 if (entry->from() < current.from() && entry->to() >= current.from()) {
5618 // Snap the overlapping range in half around the start point of
5619 // the range we're adding.
5620 CharacterRange left(entry->from(), current.from() - 1);
5621 CharacterRange right(current.from(), entry->to());
5622 // The left part of the overlapping range doesn't overlap.
5623 // Truncate the whole entry to be just the left part.
5624 entry->set_to(left.to());
5625 // The right part is the one that overlaps. We add this part
5626 // to the map and let the next step deal with merging it with
5627 // the range we're adding.
5628 ZoneSplayTree<Config>::Locator loc;
5629 bool inserted = tree()->Insert(right.from(), &loc);
5632 loc.set_value(Entry(right.from(),
5637 while (current.is_valid()) {
5638 if (tree()->FindLeastGreaterThan(current.from(), &loc) &&
5639 (loc.value().from() <= current.to()) &&
5640 (loc.value().to() >= current.from())) {
5641 Entry* entry = &loc.value();
5642 // We have overlap. If there is space between the start point of
5643 // the range we're adding and where the overlapping range starts
5644 // then we have to add a range covering just that space.
5645 if (current.from() < entry->from()) {
5646 ZoneSplayTree<Config>::Locator ins;
5647 bool inserted = tree()->Insert(current.from(), &ins);
5650 ins.set_value(Entry(current.from(),
5652 empty()->Extend(value, zone)));
5653 current.set_from(entry->from());
5655 DCHECK_EQ(current.from(), entry->from());
5656 // If the overlapping range extends beyond the one we want to add
5657 // we have to snap the right part off and add it separately.
5658 if (entry->to() > current.to()) {
5659 ZoneSplayTree<Config>::Locator ins;
5660 bool inserted = tree()->Insert(current.to() + 1, &ins);
5663 ins.set_value(Entry(current.to() + 1,
5666 entry->set_to(current.to());
5668 DCHECK(entry->to() <= current.to());
5669 // The overlapping range is now completely contained by the range
5670 // we're adding so we can just update it and move the start point
5671 // of the range we're adding just past it.
5672 entry->AddValue(value, zone);
5673 // Bail out if the last interval ended at 0xFFFF since otherwise
5674 // adding 1 will wrap around to 0.
5675 if (entry->to() == String::kMaxUtf16CodeUnit)
5677 DCHECK(entry->to() + 1 > current.from());
5678 current.set_from(entry->to() + 1);
5680 // There is no overlap so we can just add the range
5681 ZoneSplayTree<Config>::Locator ins;
5682 bool inserted = tree()->Insert(current.from(), &ins);
5685 ins.set_value(Entry(current.from(),
5687 empty()->Extend(value, zone)));
5694 OutSet* DispatchTable::Get(uc16 value) {
5695 ZoneSplayTree<Config>::Locator loc;
5696 if (!tree()->FindGreatestLessThan(value, &loc))
5698 Entry* entry = &loc.value();
5699 if (value <= entry->to())
5700 return entry->out_set();
5706 // -------------------------------------------------------------------
5710 void Analysis::EnsureAnalyzed(RegExpNode* that) {
5711 StackLimitCheck check(isolate());
5712 if (check.HasOverflowed()) {
5713 fail("Stack overflow");
5716 if (that->info()->been_analyzed || that->info()->being_analyzed)
5718 that->info()->being_analyzed = true;
5720 that->info()->being_analyzed = false;
5721 that->info()->been_analyzed = true;
5725 void Analysis::VisitEnd(EndNode* that) {
5730 void TextNode::CalculateOffsets() {
5731 int element_count = elements()->length();
5732 // Set up the offsets of the elements relative to the start. This is a fixed
5733 // quantity since a TextNode can only contain fixed-width things.
5735 for (int i = 0; i < element_count; i++) {
5736 TextElement& elm = elements()->at(i);
5737 elm.set_cp_offset(cp_offset);
5738 cp_offset += elm.length();
5743 void Analysis::VisitText(TextNode* that) {
5745 that->MakeCaseIndependent(isolate(), is_one_byte_);
5747 EnsureAnalyzed(that->on_success());
5748 if (!has_failed()) {
5749 that->CalculateOffsets();
5754 void Analysis::VisitAction(ActionNode* that) {
5755 RegExpNode* target = that->on_success();
5756 EnsureAnalyzed(target);
5757 if (!has_failed()) {
5758 // If the next node is interested in what it follows then this node
5759 // has to be interested too so it can pass the information on.
5760 that->info()->AddFromFollowing(target->info());
5765 void Analysis::VisitChoice(ChoiceNode* that) {
5766 NodeInfo* info = that->info();
5767 for (int i = 0; i < that->alternatives()->length(); i++) {
5768 RegExpNode* node = that->alternatives()->at(i).node();
5769 EnsureAnalyzed(node);
5770 if (has_failed()) return;
5771 // Anything the following nodes need to know has to be known by
5772 // this node also, so it can pass it on.
5773 info->AddFromFollowing(node->info());
5778 void Analysis::VisitLoopChoice(LoopChoiceNode* that) {
5779 NodeInfo* info = that->info();
5780 for (int i = 0; i < that->alternatives()->length(); i++) {
5781 RegExpNode* node = that->alternatives()->at(i).node();
5782 if (node != that->loop_node()) {
5783 EnsureAnalyzed(node);
5784 if (has_failed()) return;
5785 info->AddFromFollowing(node->info());
5788 // Check the loop last since it may need the value of this node
5789 // to get a correct result.
5790 EnsureAnalyzed(that->loop_node());
5791 if (!has_failed()) {
5792 info->AddFromFollowing(that->loop_node()->info());
5797 void Analysis::VisitBackReference(BackReferenceNode* that) {
5798 EnsureAnalyzed(that->on_success());
5802 void Analysis::VisitAssertion(AssertionNode* that) {
5803 EnsureAnalyzed(that->on_success());
5807 void BackReferenceNode::FillInBMInfo(int offset,
5809 BoyerMooreLookahead* bm,
5810 bool not_at_start) {
5811 // Working out the set of characters that a backreference can match is too
5812 // hard, so we just say that any character can match.
5813 bm->SetRest(offset);
5814 SaveBMInfo(bm, not_at_start, offset);
5818 STATIC_ASSERT(BoyerMoorePositionInfo::kMapSize ==
5819 RegExpMacroAssembler::kTableSize);
5822 void ChoiceNode::FillInBMInfo(int offset,
5824 BoyerMooreLookahead* bm,
5825 bool not_at_start) {
5826 ZoneList<GuardedAlternative>* alts = alternatives();
5827 budget = (budget - 1) / alts->length();
5828 for (int i = 0; i < alts->length(); i++) {
5829 GuardedAlternative& alt = alts->at(i);
5830 if (alt.guards() != NULL && alt.guards()->length() != 0) {
5831 bm->SetRest(offset); // Give up trying to fill in info.
5832 SaveBMInfo(bm, not_at_start, offset);
5835 alt.node()->FillInBMInfo(offset, budget, bm, not_at_start);
5837 SaveBMInfo(bm, not_at_start, offset);
5841 void TextNode::FillInBMInfo(int initial_offset,
5843 BoyerMooreLookahead* bm,
5844 bool not_at_start) {
5845 if (initial_offset >= bm->length()) return;
5846 int offset = initial_offset;
5847 int max_char = bm->max_char();
5848 for (int i = 0; i < elements()->length(); i++) {
5849 if (offset >= bm->length()) {
5850 if (initial_offset == 0) set_bm_info(not_at_start, bm);
5853 TextElement text = elements()->at(i);
5854 if (text.text_type() == TextElement::ATOM) {
5855 RegExpAtom* atom = text.atom();
5856 for (int j = 0; j < atom->length(); j++, offset++) {
5857 if (offset >= bm->length()) {
5858 if (initial_offset == 0) set_bm_info(not_at_start, bm);
5861 uc16 character = atom->data()[j];
5862 if (bm->compiler()->ignore_case()) {
5863 unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
5864 int length = GetCaseIndependentLetters(
5867 bm->max_char() == String::kMaxOneByteCharCode,
5869 for (int j = 0; j < length; j++) {
5870 bm->Set(offset, chars[j]);
5873 if (character <= max_char) bm->Set(offset, character);
5877 DCHECK_EQ(TextElement::CHAR_CLASS, text.text_type());
5878 RegExpCharacterClass* char_class = text.char_class();
5879 ZoneList<CharacterRange>* ranges = char_class->ranges(zone());
5880 if (char_class->is_negated()) {
5883 for (int k = 0; k < ranges->length(); k++) {
5884 CharacterRange& range = ranges->at(k);
5885 if (range.from() > max_char) continue;
5886 int to = Min(max_char, static_cast<int>(range.to()));
5887 bm->SetInterval(offset, Interval(range.from(), to));
5893 if (offset >= bm->length()) {
5894 if (initial_offset == 0) set_bm_info(not_at_start, bm);
5897 on_success()->FillInBMInfo(offset,
5900 true); // Not at start after a text node.
5901 if (initial_offset == 0) set_bm_info(not_at_start, bm);
5905 // -------------------------------------------------------------------
5906 // Dispatch table construction
5909 void DispatchTableConstructor::VisitEnd(EndNode* that) {
5910 AddRange(CharacterRange::Everything());
5914 void DispatchTableConstructor::BuildTable(ChoiceNode* node) {
5915 node->set_being_calculated(true);
5916 ZoneList<GuardedAlternative>* alternatives = node->alternatives();
5917 for (int i = 0; i < alternatives->length(); i++) {
5918 set_choice_index(i);
5919 alternatives->at(i).node()->Accept(this);
5921 node->set_being_calculated(false);
5925 class AddDispatchRange {
5927 explicit AddDispatchRange(DispatchTableConstructor* constructor)
5928 : constructor_(constructor) { }
5929 void Call(uc32 from, DispatchTable::Entry entry);
5931 DispatchTableConstructor* constructor_;
5935 void AddDispatchRange::Call(uc32 from, DispatchTable::Entry entry) {
5936 CharacterRange range(from, entry.to());
5937 constructor_->AddRange(range);
5941 void DispatchTableConstructor::VisitChoice(ChoiceNode* node) {
5942 if (node->being_calculated())
5944 DispatchTable* table = node->GetTable(ignore_case_);
5945 AddDispatchRange adder(this);
5946 table->ForEach(&adder);
5950 void DispatchTableConstructor::VisitBackReference(BackReferenceNode* that) {
5951 // TODO(160): Find the node that we refer back to and propagate its start
5952 // set back to here. For now we just accept anything.
5953 AddRange(CharacterRange::Everything());
5957 void DispatchTableConstructor::VisitAssertion(AssertionNode* that) {
5958 RegExpNode* target = that->on_success();
5959 target->Accept(this);
5963 static int CompareRangeByFrom(const CharacterRange* a,
5964 const CharacterRange* b) {
5965 return Compare<uc16>(a->from(), b->from());
5969 void DispatchTableConstructor::AddInverse(ZoneList<CharacterRange>* ranges) {
5970 ranges->Sort(CompareRangeByFrom);
5972 for (int i = 0; i < ranges->length(); i++) {
5973 CharacterRange range = ranges->at(i);
5974 if (last < range.from())
5975 AddRange(CharacterRange(last, range.from() - 1));
5976 if (range.to() >= last) {
5977 if (range.to() == String::kMaxUtf16CodeUnit) {
5980 last = range.to() + 1;
5984 AddRange(CharacterRange(last, String::kMaxUtf16CodeUnit));
5988 void DispatchTableConstructor::VisitText(TextNode* that) {
5989 TextElement elm = that->elements()->at(0);
5990 switch (elm.text_type()) {
5991 case TextElement::ATOM: {
5992 uc16 c = elm.atom()->data()[0];
5993 AddRange(CharacterRange(c, c));
5996 case TextElement::CHAR_CLASS: {
5997 RegExpCharacterClass* tree = elm.char_class();
5998 ZoneList<CharacterRange>* ranges = tree->ranges(that->zone());
5999 if (tree->is_negated()) {
6002 for (int i = 0; i < ranges->length(); i++)
6003 AddRange(ranges->at(i));
6014 void DispatchTableConstructor::VisitAction(ActionNode* that) {
6015 RegExpNode* target = that->on_success();
6016 target->Accept(this);
6020 RegExpEngine::CompilationResult RegExpEngine::Compile(
6021 Isolate* isolate, Zone* zone, RegExpCompileData* data, bool ignore_case,
6022 bool is_global, bool is_multiline, bool is_sticky, Handle<String> pattern,
6023 Handle<String> sample_subject, bool is_one_byte) {
6024 if ((data->capture_count + 1) * 2 - 1 > RegExpMacroAssembler::kMaxRegister) {
6025 return IrregexpRegExpTooBig(isolate);
6027 RegExpCompiler compiler(isolate, zone, data->capture_count, ignore_case,
6030 compiler.set_optimize(!TooMuchRegExpCode(pattern));
6032 // Sample some characters from the middle of the string.
6033 static const int kSampleSize = 128;
6035 sample_subject = String::Flatten(sample_subject);
6036 int chars_sampled = 0;
6037 int half_way = (sample_subject->length() - kSampleSize) / 2;
6038 for (int i = Max(0, half_way);
6039 i < sample_subject->length() && chars_sampled < kSampleSize;
6040 i++, chars_sampled++) {
6041 compiler.frequency_collator()->CountCharacter(sample_subject->Get(i));
6044 // Wrap the body of the regexp in capture #0.
6045 RegExpNode* captured_body = RegExpCapture::ToNode(data->tree,
6049 RegExpNode* node = captured_body;
6050 bool is_end_anchored = data->tree->IsAnchoredAtEnd();
6051 bool is_start_anchored = data->tree->IsAnchoredAtStart();
6052 int max_length = data->tree->max_match();
6053 if (!is_start_anchored && !is_sticky) {
6054 // Add a .*? at the beginning, outside the body capture, unless
6055 // this expression is anchored at the beginning or sticky.
6056 RegExpNode* loop_node =
6057 RegExpQuantifier::ToNode(0,
6058 RegExpTree::kInfinity,
6060 new(zone) RegExpCharacterClass('*'),
6063 data->contains_anchor);
6065 if (data->contains_anchor) {
6066 // Unroll loop once, to take care of the case that might start
6067 // at the start of input.
6068 ChoiceNode* first_step_node = new(zone) ChoiceNode(2, zone);
6069 first_step_node->AddAlternative(GuardedAlternative(captured_body));
6070 first_step_node->AddAlternative(GuardedAlternative(
6071 new(zone) TextNode(new(zone) RegExpCharacterClass('*'), loop_node)));
6072 node = first_step_node;
6078 node = node->FilterOneByte(RegExpCompiler::kMaxRecursion, ignore_case);
6079 // Do it again to propagate the new nodes to places where they were not
6080 // put because they had not been calculated yet.
6082 node = node->FilterOneByte(RegExpCompiler::kMaxRecursion, ignore_case);
6086 if (node == NULL) node = new(zone) EndNode(EndNode::BACKTRACK, zone);
6088 Analysis analysis(isolate, ignore_case, is_one_byte);
6089 analysis.EnsureAnalyzed(node);
6090 if (analysis.has_failed()) {
6091 const char* error_message = analysis.error_message();
6092 return CompilationResult(isolate, error_message);
6095 // Create the correct assembler for the architecture.
6096 #ifndef V8_INTERPRETED_REGEXP
6097 // Native regexp implementation.
6099 NativeRegExpMacroAssembler::Mode mode =
6100 is_one_byte ? NativeRegExpMacroAssembler::LATIN1
6101 : NativeRegExpMacroAssembler::UC16;
6103 #if V8_TARGET_ARCH_IA32
6104 RegExpMacroAssemblerIA32 macro_assembler(isolate, zone, mode,
6105 (data->capture_count + 1) * 2);
6106 #elif V8_TARGET_ARCH_X64
6107 RegExpMacroAssemblerX64 macro_assembler(isolate, zone, mode,
6108 (data->capture_count + 1) * 2);
6109 #elif V8_TARGET_ARCH_ARM
6110 RegExpMacroAssemblerARM macro_assembler(isolate, zone, mode,
6111 (data->capture_count + 1) * 2);
6112 #elif V8_TARGET_ARCH_ARM64
6113 RegExpMacroAssemblerARM64 macro_assembler(isolate, zone, mode,
6114 (data->capture_count + 1) * 2);
6115 #elif V8_TARGET_ARCH_PPC
6116 RegExpMacroAssemblerPPC macro_assembler(isolate, zone, mode,
6117 (data->capture_count + 1) * 2);
6118 #elif V8_TARGET_ARCH_MIPS
6119 RegExpMacroAssemblerMIPS macro_assembler(isolate, zone, mode,
6120 (data->capture_count + 1) * 2);
6121 #elif V8_TARGET_ARCH_MIPS64
6122 RegExpMacroAssemblerMIPS macro_assembler(isolate, zone, mode,
6123 (data->capture_count + 1) * 2);
6124 #elif V8_TARGET_ARCH_X87
6125 RegExpMacroAssemblerX87 macro_assembler(isolate, zone, mode,
6126 (data->capture_count + 1) * 2);
6128 #error "Unsupported architecture"
6131 #else // V8_INTERPRETED_REGEXP
6132 // Interpreted regexp implementation.
6133 EmbeddedVector<byte, 1024> codes;
6134 RegExpMacroAssemblerIrregexp macro_assembler(isolate, codes, zone);
6135 #endif // V8_INTERPRETED_REGEXP
6137 macro_assembler.set_slow_safe(TooMuchRegExpCode(pattern));
6139 // Inserted here, instead of in Assembler, because it depends on information
6140 // in the AST that isn't replicated in the Node structure.
6141 static const int kMaxBacksearchLimit = 1024;
6142 if (is_end_anchored &&
6143 !is_start_anchored &&
6144 max_length < kMaxBacksearchLimit) {
6145 macro_assembler.SetCurrentPositionFromEnd(max_length);
6149 macro_assembler.set_global_mode(
6150 (data->tree->min_match() > 0)
6151 ? RegExpMacroAssembler::GLOBAL_NO_ZERO_LENGTH_CHECK
6152 : RegExpMacroAssembler::GLOBAL);
6155 return compiler.Assemble(¯o_assembler,
6157 data->capture_count,
6162 bool RegExpEngine::TooMuchRegExpCode(Handle<String> pattern) {
6163 Heap* heap = pattern->GetHeap();
6164 bool too_much = pattern->length() > RegExpImpl::kRegExpTooLargeToOptimize;
6165 if (heap->total_regexp_code_generated() > RegExpImpl::kRegExpCompiledLimit &&
6166 heap->isolate()->memory_allocator()->SizeExecutable() >
6167 RegExpImpl::kRegExpExecutableMemoryLimit) {
6172 }} // namespace v8::internal