bb7ad604140cd5a18a0057ea2ad86ae3518aa391
[platform/upstream/nodejs.git] / deps / v8 / src / jsregexp.cc
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.
4
5 #include "src/v8.h"
6
7 #include "src/ast.h"
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"
24
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
42 #else
43 #error Unsupported target architecture.
44 #endif
45 #endif
46
47 #include "src/interpreter-irregexp.h"
48
49
50 namespace v8 {
51 namespace internal {
52
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);
60 }
61
62
63 MUST_USE_RESULT
64 static inline MaybeHandle<Object> ThrowRegExpException(
65     Handle<JSRegExp> re,
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);
77 }
78
79
80 ContainedInLattice AddRange(ContainedInLattice containment,
81                             const int* ranges,
82                             int ranges_length,
83                             Interval new_range) {
84   DCHECK((ranges_length & 1) == 1);
85   DCHECK(ranges[ranges_length - 1] == String::kMaxUtf16CodeUnit + 1);
86   if (containment == kLatticeUnknown) return containment;
87   bool inside = false;
88   int last = 0;
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);
97     }
98     return kLatticeUnknown;
99   }
100   return containment;
101 }
102
103
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;
109
110
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];
118   int different = 0;
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;
124       different++;
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;
128     }
129   }
130   return true;
131 }
132
133
134 // Generic RegExp methods. Dispatches to implementation specific methods.
135
136
137 MaybeHandle<Object> RegExpImpl::Compile(Handle<JSRegExp> re,
138                                         Handle<String> pattern,
139                                         JSRegExp::Flags flags) {
140   Isolate* isolate = re->GetIsolate();
141   Zone zone;
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));
148
149   Handle<Object> result;
150   if (in_cache) {
151     re->set_data(*cached);
152     return re;
153   }
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(),
160                                  &parse_result)) {
161     // Throw an exception if we fail to parse the pattern.
162     return ThrowRegExpException(re,
163                                 pattern,
164                                 parse_result.error,
165                                 "malformed_regexp");
166   }
167
168   bool has_been_compiled = false;
169
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),
187         Object);
188     if (!HasFewDifferentCharacters(atom_string)) {
189       AtomCompile(re, pattern, flags, atom_string);
190       has_been_compiled = true;
191     }
192   }
193   if (!has_been_compiled) {
194     IrregexpInitialize(re, pattern, flags, parse_result.capture_count);
195   }
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);
201
202   return re;
203 }
204
205
206 MaybeHandle<Object> RegExpImpl::Exec(Handle<JSRegExp> regexp,
207                                      Handle<String> subject,
208                                      int index,
209                                      Handle<JSArray> last_match_info) {
210   switch (regexp->TypeTag()) {
211     case JSRegExp::ATOM:
212       return AtomExec(regexp, subject, index, last_match_info);
213     case JSRegExp::IRREGEXP: {
214       return IrregexpExec(regexp, subject, index, last_match_info);
215     }
216     default:
217       UNREACHABLE();
218       return MaybeHandle<Object>();
219   }
220 }
221
222
223 // RegExp Atom implementation: Simple string search using indexOf.
224
225
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,
231                                                  JSRegExp::ATOM,
232                                                  pattern,
233                                                  flags,
234                                                  match_pattern);
235 }
236
237
238 static void SetAtomLastCapture(FixedArray* array,
239                                String* subject,
240                                int from,
241                                int to) {
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);
248 }
249
250
251 int RegExpImpl::AtomExecRaw(Handle<JSRegExp> regexp,
252                             Handle<String> subject,
253                             int index,
254                             int32_t* output,
255                             int output_size) {
256   Isolate* isolate = regexp->GetIsolate();
257
258   DCHECK(0 <= index);
259   DCHECK(index <= subject->length());
260
261   subject = String::Flatten(subject);
262   DisallowHeapAllocation no_gc;  // ensure vectors stay valid
263
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);
268
269   if (index + needle_len > subject->length()) {
270     return RegExpImpl::RE_FAILURE;
271   }
272
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
279     index =
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)));
291     if (index == -1) {
292       return i / 2;  // Return number of matches.
293     } else {
294       output[i] = index;
295       output[i+1] = index + needle_len;
296       index += needle_len;
297     }
298   }
299   return output_size / 2;
300 }
301
302
303 Handle<Object> RegExpImpl::AtomExec(Handle<JSRegExp> re,
304                                     Handle<String> subject,
305                                     int index,
306                                     Handle<JSArray> last_match_info) {
307   Isolate* isolate = re->GetIsolate();
308
309   static const int kNumRegisters = 2;
310   STATIC_ASSERT(kNumRegisters <= Isolate::kJSRegexpStaticOffsetsVectorSize);
311   int32_t* output_registers = isolate->jsregexp_static_offsets_vector();
312
313   int res = AtomExecRaw(re, subject, index, output_registers, kNumRegisters);
314
315   if (res == RegExpImpl::RE_FAILURE) return isolate->factory()->null_value();
316
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;
322 }
323
324
325 // Irregexp implementation.
326
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
332 // returns false.
333 bool RegExpImpl::EnsureCompiledIrregexp(Handle<JSRegExp> re,
334                                         Handle<String> sample_subject,
335                                         bool is_one_byte) {
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;
341 #endif
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());
349     return true;
350   }
351   return CompileIrregexp(re, sample_subject, is_one_byte);
352 }
353
354
355 static void CreateRegExpErrorObjectAndThrow(Handle<JSRegExp> re,
356                                             Handle<String> error_message,
357                                             Isolate* isolate) {
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);
367 }
368
369
370 bool RegExpImpl::CompileIrregexp(Handle<JSRegExp> re,
371                                  Handle<String> sample_subject,
372                                  bool is_one_byte) {
373   // Compile the RegExp.
374   Isolate* isolate = re->GetIsolate();
375   Zone zone;
376   PostponeInterruptsScope postpone(isolate);
377   // If we had a compilation error the last time this is saved at the
378   // saved code index.
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
382   // been flushed.
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));
388
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);
397     return false;
398   }
399
400   JSRegExp::Flags flags = re->GetFlags();
401
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,
411                              pattern,
412                              compile_data.error,
413                              "malformed_regexp"));
414     return false;
415   }
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,
419       is_one_byte);
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);
425     return false;
426   }
427
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);
433   }
434
435   return true;
436 }
437
438
439 int RegExpImpl::IrregexpMaxRegisterCount(FixedArray* re) {
440   return Smi::cast(
441       re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value();
442 }
443
444
445 void RegExpImpl::SetIrregexpMaxRegisterCount(FixedArray* re, int value) {
446   re->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(value));
447 }
448
449
450 int RegExpImpl::IrregexpNumberOfCaptures(FixedArray* re) {
451   return Smi::cast(re->get(JSRegExp::kIrregexpCaptureCountIndex))->value();
452 }
453
454
455 int RegExpImpl::IrregexpNumberOfRegisters(FixedArray* re) {
456   return Smi::cast(re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value();
457 }
458
459
460 ByteArray* RegExpImpl::IrregexpByteCode(FixedArray* re, bool is_one_byte) {
461   return ByteArray::cast(re->get(JSRegExp::code_index(is_one_byte)));
462 }
463
464
465 Code* RegExpImpl::IrregexpNativeCode(FixedArray* re, bool is_one_byte) {
466   return Code::cast(re->get(JSRegExp::code_index(is_one_byte)));
467 }
468
469
470 void RegExpImpl::IrregexpInitialize(Handle<JSRegExp> re,
471                                     Handle<String> pattern,
472                                     JSRegExp::Flags flags,
473                                     int capture_count) {
474   // Initialize compiled code entries to null.
475   re->GetIsolate()->factory()->SetRegExpIrregexpData(re,
476                                                      JSRegExp::IRREGEXP,
477                                                      pattern,
478                                                      flags,
479                                                      capture_count);
480 }
481
482
483 int RegExpImpl::IrregexpPrepare(Handle<JSRegExp> regexp,
484                                 Handle<String> subject) {
485   subject = String::Flatten(subject);
486
487   // Check representation of the underlying storage.
488   bool is_one_byte = subject->IsOneByteRepresentationUnderneath();
489   if (!EnsureCompiledIrregexp(regexp, subject, is_one_byte)) return -1;
490
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
500   // internally.
501   return (IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())) + 1) * 2;
502 #endif  // V8_INTERPRETED_REGEXP
503 }
504
505
506 int RegExpImpl::IrregexpExecRaw(Handle<JSRegExp> regexp,
507                                 Handle<String> subject,
508                                 int index,
509                                 int32_t* output,
510                                 int output_size) {
511   Isolate* isolate = regexp->GetIsolate();
512
513   Handle<FixedArray> irregexp(FixedArray::cast(regexp->data()), isolate);
514
515   DCHECK(index >= 0);
516   DCHECK(index <= subject->length());
517   DCHECK(subject->IsFlat());
518
519   bool is_one_byte = subject->IsOneByteRepresentationUnderneath();
520
521 #ifndef V8_INTERPRETED_REGEXP
522   DCHECK(output_size >= (IrregexpNumberOfCaptures(*irregexp) + 1) * 2);
523   do {
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,
532                                           subject,
533                                           output,
534                                           output_size,
535                                           index,
536                                           isolate);
537     if (res != NativeRegExpMacroAssembler::RETRY) {
538       DCHECK(res != NativeRegExpMacroAssembler::EXCEPTION ||
539              isolate->has_pending_exception());
540       STATIC_ASSERT(
541           static_cast<int>(NativeRegExpMacroAssembler::SUCCESS) == RE_SUCCESS);
542       STATIC_ASSERT(
543           static_cast<int>(NativeRegExpMacroAssembler::FAILURE) == RE_FAILURE);
544       STATIC_ASSERT(static_cast<int>(NativeRegExpMacroAssembler::EXCEPTION)
545                     == RE_EXCEPTION);
546       return static_cast<IrregexpResult>(res);
547     }
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();
556   } while (true);
557   UNREACHABLE();
558   return RE_EXCEPTION;
559 #else  // V8_INTERPRETED_REGEXP
560
561   DCHECK(output_size >= IrregexpNumberOfRegisters(*irregexp));
562   // We must have done EnsureCompiledIrregexp, so we can get the number of
563   // registers.
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
569   // last match info.
570   for (int i = number_of_capture_registers - 1; i >= 0; i--) {
571     raw_output[i] = -1;
572   }
573   Handle<ByteArray> byte_codes(IrregexpByteCode(*irregexp, is_one_byte),
574                                isolate);
575
576   IrregexpResult result = IrregexpInterpreter::Match(isolate,
577                                                      byte_codes,
578                                                      subject,
579                                                      raw_output,
580                                                      index);
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));
584   }
585   if (result == RE_EXCEPTION) {
586     DCHECK(!isolate->has_pending_exception());
587     isolate->StackOverflow();
588   }
589   return result;
590 #endif  // V8_INTERPRETED_REGEXP
591 }
592
593
594 MaybeHandle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp,
595                                              Handle<String> subject,
596                                              int previous_index,
597                                              Handle<JSArray> last_match_info) {
598   Isolate* isolate = regexp->GetIsolate();
599   DCHECK_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP);
600
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());
607   }
608 #endif
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>();
614   }
615
616   int32_t* output_registers = NULL;
617   if (required_registers > Isolate::kJSRegexpStaticOffsetsVectorSize) {
618     output_registers = NewArray<int32_t>(required_registers);
619   }
620   SmartArrayPointer<int32_t> auto_release(output_registers);
621   if (output_registers == NULL) {
622     output_registers = isolate->jsregexp_static_offsets_vector();
623   }
624
625   int res = RegExpImpl::IrregexpExecRaw(
626       regexp, subject, previous_index, output_registers, required_registers);
627   if (res == RE_SUCCESS) {
628     int capture_count =
629         IrregexpNumberOfCaptures(FixedArray::cast(regexp->data()));
630     return SetLastMatchInfo(
631         last_match_info, subject, capture_count, output_registers);
632   }
633   if (res == RE_EXCEPTION) {
634     DCHECK(isolate->has_pending_exception());
635     return MaybeHandle<Object>();
636   }
637   DCHECK(res == RE_FAILURE);
638   return isolate->factory()->null_value();
639 }
640
641
642 Handle<JSArray> RegExpImpl::SetLastMatchInfo(Handle<JSArray> last_match_info,
643                                              Handle<String> subject,
644                                              int capture_count,
645                                              int32_t* match) {
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());
652   if (match != NULL) {
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]);
656     }
657   }
658   SetLastCaptureCount(array, capture_register_count);
659   SetLastSubject(array, *subject);
660   SetLastInput(array, *subject);
661   return last_match_info;
662 }
663
664
665 RegExpImpl::GlobalCache::GlobalCache(Handle<JSRegExp> regexp,
666                                      Handle<String> subject,
667                                      bool is_global,
668                                      Isolate* isolate)
669   : register_array_(NULL),
670     register_array_size_(0),
671     regexp_(regexp),
672     subject_(subject) {
673 #ifdef V8_INTERPRETED_REGEXP
674   bool interpreted = true;
675 #else
676   bool interpreted = false;
677 #endif  // V8_INTERPRETED_REGEXP
678
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.
683     interpreted = false;
684   } else {
685     registers_per_match_ = RegExpImpl::IrregexpPrepare(regexp_, subject_);
686     if (registers_per_match_ < 0) {
687       num_matches_ = -1;  // Signal exception.
688       return;
689     }
690   }
691
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_;
696   } else {
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_;
700     max_matches_ = 1;
701   }
702
703   if (register_array_size_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
704     register_array_ = NewArray<int32_t>(register_array_size_);
705   } else {
706     register_array_ = isolate->jsregexp_static_offsets_vector();
707   }
708
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       &register_array_[current_match_index_ * registers_per_match_];
717   last_match[0] = -1;
718   last_match[1] = 0;
719 }
720
721
722 // -------------------------------------------------------------------
723 // Implementation of the Irregexp regular expression engine.
724 //
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.
728
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
738 //      detail below.
739
740 // Code generation.
741 //
742 //   The nodes are divided into four main categories.
743 //   * Choice nodes
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 {}).
747 //   * Action nodes
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.
753 //   * Matching nodes
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.
757 //   * End nodes
758 //        These are used to implement the actions required on finding
759 //        a successful match or failing to find a match.
760 //
761 //   The code generated (whether as byte codes or native code) maintains
762 //   some state as it runs.  This consists of the following elements:
763 //
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.
769 //
770 // Conceptual regular expression execution model:
771 //
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:
775 //
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
783 //     }
784 //     <generate code to test for last choice>
785 //
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>
794 //
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>
799 //     else
800 //       <pop backtrack location from stack and go to it>
801 //
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.
805 //
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.
813 //
814 // Actual regular expression code generation.
815 //
816 //   Code generation is actually more complicated than the above.  In order
817 //   to improve the efficiency of the generated code some optimizations are
818 //   performed
819 //
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
823 //     implemented.
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
835 //     space.
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.
841 //
842 // Execution state virtualization.
843 //
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.
856 //
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.
870
871
872 void RegExpTree::AppendToText(RegExpText* text, Zone* zone) {
873   UNREACHABLE();
874 }
875
876
877 void RegExpAtom::AppendToText(RegExpText* text, Zone* zone) {
878   text->AddElement(TextElement::Atom(this), zone);
879 }
880
881
882 void RegExpCharacterClass::AppendToText(RegExpText* text, Zone* zone) {
883   text->AddElement(TextElement::CharClass(this), zone);
884 }
885
886
887 void RegExpText::AppendToText(RegExpText* text, Zone* zone) {
888   for (int i = 0; i < elements()->length(); i++)
889     text->AddElement(elements()->at(i), zone);
890 }
891
892
893 TextElement TextElement::Atom(RegExpAtom* atom) {
894   return TextElement(ATOM, atom);
895 }
896
897
898 TextElement TextElement::CharClass(RegExpCharacterClass* char_class) {
899   return TextElement(CHAR_CLASS, char_class);
900 }
901
902
903 int TextElement::length() const {
904   switch (text_type()) {
905     case ATOM:
906       return atom()->length();
907
908     case CHAR_CLASS:
909       return 1;
910   }
911   UNREACHABLE();
912   return 0;
913 }
914
915
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);
921   }
922   return table_;
923 }
924
925
926 class FrequencyCollator {
927  public:
928   FrequencyCollator() : total_samples_(0) {
929     for (int i = 0; i < RegExpMacroAssembler::kTableSize; i++) {
930       frequencies_[i] = CharacterFrequency(i);
931     }
932   }
933
934   void CountCharacter(int character) {
935     int index = (character & RegExpMacroAssembler::kTableMask);
936     frequencies_[index].Increment();
937     total_samples_++;
938   }
939
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.
945     int freq_in_per128 =
946         (frequencies_[in_character].counter() * 128) / total_samples_;
947     return freq_in_per128;
948   }
949
950  private:
951   class CharacterFrequency {
952    public:
953     CharacterFrequency() : counter_(0), character_(-1) { }
954     explicit CharacterFrequency(int character)
955         : counter_(0), character_(character) { }
956
957     void Increment() { counter_++; }
958     int counter() { return counter_; }
959     int character() { return character_; }
960
961    private:
962     int counter_;
963     int character_;
964   };
965
966
967  private:
968   CharacterFrequency frequencies_[RegExpMacroAssembler::kTableSize];
969   int total_samples_;
970 };
971
972
973 class RegExpCompiler {
974  public:
975   RegExpCompiler(Isolate* isolate, Zone* zone, int capture_count,
976                  bool ignore_case, bool is_one_byte);
977
978   int AllocateRegister() {
979     if (next_register_ >= RegExpMacroAssembler::kMaxRegister) {
980       reg_exp_too_big_ = true;
981       return next_register_;
982     }
983     return next_register_++;
984   }
985
986   RegExpEngine::CompilationResult Assemble(RegExpMacroAssembler* assembler,
987                                            RegExpNode* start,
988                                            int capture_count,
989                                            Handle<String> pattern);
990
991   inline void AddWork(RegExpNode* node) { work_list_->Add(node); }
992
993   static const int kImplementationOffset = 0;
994   static const int kNumberOfRegistersOffset = 0;
995   static const int kCodeOffset = 1;
996
997   RegExpMacroAssembler* macro_assembler() { return macro_assembler_; }
998   EndNode* accept() { return accept_; }
999
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_--; }
1004
1005   void SetRegExpTooBig() { reg_exp_too_big_ = true; }
1006
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_; }
1012
1013   int current_expansion_factor() { return current_expansion_factor_; }
1014   void set_current_expansion_factor(int value) {
1015     current_expansion_factor_ = value;
1016   }
1017
1018   Isolate* isolate() const { return isolate_; }
1019   Zone* zone() const { return zone_; }
1020
1021   static const int kNoRegister = -1;
1022
1023  private:
1024   EndNode* accept_;
1025   int next_register_;
1026   List<RegExpNode*>* work_list_;
1027   int recursion_depth_;
1028   RegExpMacroAssembler* macro_assembler_;
1029   bool ignore_case_;
1030   bool one_byte_;
1031   bool reg_exp_too_big_;
1032   bool optimize_;
1033   int current_expansion_factor_;
1034   FrequencyCollator frequency_collator_;
1035   Isolate* isolate_;
1036   Zone* zone_;
1037 };
1038
1039
1040 class RecursionCheck {
1041  public:
1042   explicit RecursionCheck(RegExpCompiler* compiler) : compiler_(compiler) {
1043     compiler->IncrementRecursionDepth();
1044   }
1045   ~RecursionCheck() { compiler_->DecrementRecursionDepth(); }
1046  private:
1047   RegExpCompiler* compiler_;
1048 };
1049
1050
1051 static RegExpEngine::CompilationResult IrregexpRegExpTooBig(Isolate* isolate) {
1052   return RegExpEngine::CompilationResult(isolate, "RegExp too big");
1053 }
1054
1055
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)),
1061       work_list_(NULL),
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_(),
1069       isolate_(isolate),
1070       zone_(zone) {
1071   accept_ = new(zone) EndNode(EndNode::ACCEPT, zone);
1072   DCHECK(next_register_ - 1 <= RegExpMacroAssembler::kMaxRegister);
1073 }
1074
1075
1076 RegExpEngine::CompilationResult RegExpCompiler::Assemble(
1077     RegExpMacroAssembler* macro_assembler,
1078     RegExpNode* start,
1079     int capture_count,
1080     Handle<String> pattern) {
1081   Heap* heap = pattern->GetHeap();
1082
1083 #ifdef DEBUG
1084   if (FLAG_trace_regexp_assembler)
1085     macro_assembler_ =
1086         new RegExpMacroAssemblerTracer(isolate(), macro_assembler);
1087   else
1088 #endif
1089     macro_assembler_ = macro_assembler;
1090
1091   List <RegExpNode*> work_list(0);
1092   work_list_ = &work_list;
1093   Label fail;
1094   macro_assembler_->PushBacktrack(&fail);
1095   Trace new_trace;
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);
1101   }
1102   if (reg_exp_too_big_) return IrregexpRegExpTooBig(isolate_);
1103
1104   Handle<HeapObject> code = macro_assembler_->GetCode(pattern);
1105   heap->IncreaseTotalRegexpCodeGenerated(code->Size());
1106   work_list_ = NULL;
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);
1112   }
1113 #endif
1114 #ifdef DEBUG
1115   if (FLAG_trace_regexp_assembler) {
1116     delete macro_assembler_;
1117   }
1118 #endif
1119   return RegExpEngine::CompilationResult(*code, next_register_);
1120 }
1121
1122
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);
1127   } else {
1128     return reg() == that;
1129   }
1130 }
1131
1132
1133 bool Trace::mentions_reg(int reg) {
1134   for (DeferredAction* action = actions_;
1135        action != NULL;
1136        action = action->next()) {
1137     if (action->Mentions(reg))
1138       return true;
1139   }
1140   return false;
1141 }
1142
1143
1144 bool Trace::GetStoredPosition(int reg, int* cp_offset) {
1145   DCHECK_EQ(0, *cp_offset);
1146   for (DeferredAction* action = actions_;
1147        action != NULL;
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();
1152         return true;
1153       } else {
1154         return false;
1155       }
1156     }
1157   }
1158   return false;
1159 }
1160
1161
1162 int Trace::FindAffectedRegisters(OutSet* affected_registers,
1163                                  Zone* zone) {
1164   int max_register = RegExpCompiler::kNoRegister;
1165   for (DeferredAction* action = actions_;
1166        action != NULL;
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();
1173     } else {
1174       affected_registers->Set(action->reg(), zone);
1175       if (action->reg() > max_register) max_register = action->reg();
1176     }
1177   }
1178   return max_register;
1179 }
1180
1181
1182 void Trace::RestoreAffectedRegisters(RegExpMacroAssembler* assembler,
1183                                      int max_register,
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)) {
1190       int clear_to = reg;
1191       while (reg > 0 && registers_to_clear.Get(reg - 1)) {
1192         reg--;
1193       }
1194       assembler->ClearRegisters(reg, clear_to);
1195     }
1196   }
1197 }
1198
1199
1200 void Trace::PerformDeferredActions(RegExpMacroAssembler* assembler,
1201                                    int max_register,
1202                                    const OutSet& affected_registers,
1203                                    OutSet* registers_to_pop,
1204                                    OutSet* registers_to_clear,
1205                                    Zone* zone) {
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;
1208
1209   // Count pushes performed to force a stack limit check occasionally.
1210   int pushes = 0;
1211
1212   for (int reg = 0; reg <= max_register; reg++) {
1213     if (!affected_registers.Get(reg)) {
1214       continue;
1215     }
1216
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;
1222
1223     int value = 0;
1224     bool absolute = false;
1225     bool clear = 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_;
1230          action != NULL;
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);
1237             if (!absolute) {
1238               value += psr->value();
1239               absolute = true;
1240             }
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
1245             // restore.
1246             undo_action = RESTORE;
1247             DCHECK_EQ(store_position, -1);
1248             DCHECK(!clear);
1249             break;
1250           }
1251           case ActionNode::INCREMENT_REGISTER:
1252             if (!absolute) {
1253               value++;
1254             }
1255             DCHECK_EQ(store_position, -1);
1256             DCHECK(!clear);
1257             undo_action = RESTORE;
1258             break;
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();
1264             }
1265
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.
1269             if (reg <= 1) {
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;
1275             } else {
1276               undo_action = pc->is_capture() ? CLEAR : RESTORE;
1277             }
1278             DCHECK(!absolute);
1279             DCHECK_EQ(value, 0);
1280             break;
1281           }
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) {
1287               clear = true;
1288             }
1289             undo_action = RESTORE;
1290             DCHECK(!absolute);
1291             DCHECK_EQ(value, 0);
1292             break;
1293           }
1294           default:
1295             UNREACHABLE();
1296             break;
1297         }
1298       }
1299     }
1300     // Prepare for the undo-action (e.g., push if it's going to be popped).
1301     if (undo_action == RESTORE) {
1302       pushes++;
1303       RegExpMacroAssembler::StackCheckFlag stack_check =
1304           RegExpMacroAssembler::kNoStackLimitCheck;
1305       if (pushes == push_limit) {
1306         stack_check = RegExpMacroAssembler::kCheckStackLimit;
1307         pushes = 0;
1308       }
1309
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);
1314     }
1315     // Perform the chronologically last action (or accumulated increment)
1316     // for the register.
1317     if (store_position != -1) {
1318       assembler->WriteCurrentPositionToRegister(reg, store_position);
1319     } else if (clear) {
1320       assembler->ClearRegisters(reg, reg);
1321     } else if (absolute) {
1322       assembler->SetRegister(reg, value);
1323     } else if (value != 0) {
1324       assembler->AdvanceRegister(reg, value);
1325     }
1326   }
1327 }
1328
1329
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();
1335
1336   DCHECK(!is_trivial());
1337
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.
1344     Trace new_state;
1345     successor->Emit(compiler, &new_state);
1346     return;
1347   }
1348
1349   // Generate deferred actions here along with code to undo them again.
1350   OutSet affected_registers;
1351
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();
1357   }
1358
1359   int max_register = FindAffectedRegisters(&affected_registers,
1360                                            compiler->zone());
1361   OutSet registers_to_pop;
1362   OutSet registers_to_clear;
1363   PerformDeferredActions(assembler,
1364                          max_register,
1365                          affected_registers,
1366                          &registers_to_pop,
1367                          &registers_to_clear,
1368                          compiler->zone());
1369   if (cp_offset_ != 0) {
1370     assembler->AdvanceCurrentPosition(cp_offset_);
1371   }
1372
1373   // Create a new trivial state and generate the node with that.
1374   Label undo;
1375   assembler->PushBacktrack(&undo);
1376   Trace new_state;
1377   successor->Emit(compiler, &new_state);
1378
1379   // On backtrack we need to restore state.
1380   assembler->Bind(&undo);
1381   RestoreAffectedRegisters(assembler,
1382                            max_register,
1383                            registers_to_pop,
1384                            registers_to_clear);
1385   if (backtrack() == NULL) {
1386     assembler->Backtrack();
1387   } else {
1388     assembler->PopCurrentPosition();
1389     assembler->GoTo(backtrack());
1390   }
1391 }
1392
1393
1394 void NegativeSubmatchSuccess::Emit(RegExpCompiler* compiler, Trace* trace) {
1395   RegExpMacroAssembler* assembler = compiler->macro_assembler();
1396
1397   // Omit flushing the trace. We discard the entire stack frame anyway.
1398
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());
1403   }
1404
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);
1414   }
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();
1418 }
1419
1420
1421 void EndNode::Emit(RegExpCompiler* compiler, Trace* trace) {
1422   if (!trace->is_trivial()) {
1423     trace->Flush(compiler, this);
1424     return;
1425   }
1426   RegExpMacroAssembler* assembler = compiler->macro_assembler();
1427   if (!label()->is_bound()) {
1428     assembler->Bind(label());
1429   }
1430   switch (action_) {
1431     case ACCEPT:
1432       assembler->Succeed();
1433       return;
1434     case BACKTRACK:
1435       assembler->GoTo(trace->backtrack());
1436       return;
1437     case NEGATIVE_SUBMATCH_SUCCESS:
1438       // This case is handled in a different virtual method.
1439       UNREACHABLE();
1440   }
1441   UNIMPLEMENTED();
1442 }
1443
1444
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);
1449 }
1450
1451
1452 ActionNode* ActionNode::SetRegister(int reg,
1453                                     int val,
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;
1459   return result;
1460 }
1461
1462
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;
1467   return result;
1468 }
1469
1470
1471 ActionNode* ActionNode::StorePosition(int reg,
1472                                       bool is_capture,
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;
1478   return result;
1479 }
1480
1481
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();
1488   return result;
1489 }
1490
1491
1492 ActionNode* ActionNode::BeginSubmatch(int stack_reg,
1493                                       int position_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;
1499   return result;
1500 }
1501
1502
1503 ActionNode* ActionNode::PositiveSubmatchSuccess(int stack_reg,
1504                                                 int position_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;
1514   return result;
1515 }
1516
1517
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;
1527   return result;
1528 }
1529
1530
1531 #define DEFINE_ACCEPT(Type)                                          \
1532   void Type##Node::Accept(NodeVisitor* visitor) {                    \
1533     visitor->Visit##Type(this);                                      \
1534   }
1535 FOR_EACH_NODE_TYPE(DEFINE_ACCEPT)
1536 #undef DEFINE_ACCEPT
1537
1538
1539 void LoopChoiceNode::Accept(NodeVisitor* visitor) {
1540   visitor->VisitLoopChoice(this);
1541 }
1542
1543
1544 // -------------------------------------------------------------------
1545 // Emit code.
1546
1547
1548 void ChoiceNode::GenerateGuard(RegExpMacroAssembler* macro_assembler,
1549                                Guard* guard,
1550                                Trace* trace) {
1551   switch (guard->op()) {
1552     case Guard::LT:
1553       DCHECK(!trace->mentions_reg(guard->reg()));
1554       macro_assembler->IfRegisterGE(guard->reg(),
1555                                     guard->value(),
1556                                     trace->backtrack());
1557       break;
1558     case Guard::GEQ:
1559       DCHECK(!trace->mentions_reg(guard->reg()));
1560       macro_assembler->IfRegisterLT(guard->reg(),
1561                                     guard->value(),
1562                                     trace->backtrack());
1563       break;
1564   }
1565 }
1566
1567
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) {
1573   int length =
1574       isolate->jsregexp_uncanonicalize()->get(character, '\0', letters);
1575   // Unibrow returns 0 or 1 for characters where case independence is
1576   // trivial.
1577   if (length == 0) {
1578     letters[0] = character;
1579     length = 1;
1580   }
1581   if (!one_byte_subject || character <= String::kMaxOneByteCharCode) {
1582     return length;
1583   }
1584
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.
1589   return 0;
1590 }
1591
1592
1593 static inline bool EmitSimpleCharacter(Isolate* isolate,
1594                                        RegExpCompiler* compiler,
1595                                        uc16 c,
1596                                        Label* on_failure,
1597                                        int cp_offset,
1598                                        bool check,
1599                                        bool preloaded) {
1600   RegExpMacroAssembler* assembler = compiler->macro_assembler();
1601   bool bound_checked = false;
1602   if (!preloaded) {
1603     assembler->LoadCurrentCharacter(
1604         cp_offset,
1605         on_failure,
1606         check);
1607     bound_checked = true;
1608   }
1609   assembler->CheckNotCharacter(c, on_failure);
1610   return bound_checked;
1611 }
1612
1613
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,
1618                                      uc16 c,
1619                                      Label* on_failure,
1620                                      int cp_offset,
1621                                      bool check,
1622                                      bool preloaded) {
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);
1627   if (length < 1) {
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.
1632   }
1633   bool checked = false;
1634   // We handle the length > 1 case in a later pass.
1635   if (length == 1) {
1636     if (one_byte && c > String::kMaxOneByteCharCodeU) {
1637       // Can't match - see above.
1638       return false;  // Bounds not checked.
1639     }
1640     if (!preloaded) {
1641       macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check);
1642       checked = check;
1643     }
1644     macro_assembler->CheckNotCharacter(c, on_failure);
1645   }
1646   return checked;
1647 }
1648
1649
1650 static bool ShortCutEmitCharacterPair(RegExpMacroAssembler* macro_assembler,
1651                                       bool one_byte, uc16 c1, uc16 c2,
1652                                       Label* on_failure) {
1653   uc16 char_mask;
1654   if (one_byte) {
1655     char_mask = String::kMaxOneByteCharCode;
1656   } else {
1657     char_mask = String::kMaxUtf16CodeUnit;
1658   }
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.
1664     DCHECK(c2 > c1);
1665     uc16 mask = char_mask ^ exor;
1666     macro_assembler->CheckNotCharacterAfterAnd(c1, mask, on_failure);
1667     return true;
1668   }
1669   DCHECK(c2 > c1);
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,
1678                                                     diff,
1679                                                     mask,
1680                                                     on_failure);
1681     return true;
1682   }
1683   return false;
1684 }
1685
1686
1687 typedef bool EmitCharacterFunction(Isolate* isolate,
1688                                    RegExpCompiler* compiler,
1689                                    uc16 c,
1690                                    Label* on_failure,
1691                                    int cp_offset,
1692                                    bool check,
1693                                    bool preloaded);
1694
1695 // Only emits letters (things that have case).  Only used for case independent
1696 // matches.
1697 static inline bool EmitAtomLetter(Isolate* isolate,
1698                                   RegExpCompiler* compiler,
1699                                   uc16 c,
1700                                   Label* on_failure,
1701                                   int cp_offset,
1702                                   bool check,
1703                                   bool preloaded) {
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.
1711   if (!preloaded) {
1712     macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check);
1713   }
1714   Label ok;
1715   DCHECK(unibrow::Ecma262UnCanonicalize::kMaxWidth == 4);
1716   switch (length) {
1717     case 2: {
1718       if (ShortCutEmitCharacterPair(macro_assembler, one_byte, chars[0],
1719                                     chars[1], on_failure)) {
1720       } else {
1721         macro_assembler->CheckCharacter(chars[0], &ok);
1722         macro_assembler->CheckNotCharacter(chars[1], on_failure);
1723         macro_assembler->Bind(&ok);
1724       }
1725       break;
1726     }
1727     case 4:
1728       macro_assembler->CheckCharacter(chars[3], &ok);
1729       // Fall through!
1730     case 3:
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);
1735       break;
1736     default:
1737       UNREACHABLE();
1738       break;
1739   }
1740   return true;
1741 }
1742
1743
1744 static void EmitBoundaryTest(RegExpMacroAssembler* masm,
1745                              int border,
1746                              Label* fall_through,
1747                              Label* above_or_equal,
1748                              Label* below) {
1749   if (below != fall_through) {
1750     masm->CheckCharacterLT(border, below);
1751     if (above_or_equal != fall_through) masm->GoTo(above_or_equal);
1752   } else {
1753     masm->CheckCharacterGT(border - 1, above_or_equal);
1754   }
1755 }
1756
1757
1758 static void EmitDoubleBoundaryTest(RegExpMacroAssembler* masm,
1759                                    int first,
1760                                    int last,
1761                                    Label* fall_through,
1762                                    Label* in_range,
1763                                    Label* out_of_range) {
1764   if (in_range == fall_through) {
1765     if (first == last) {
1766       masm->CheckNotCharacter(first, out_of_range);
1767     } else {
1768       masm->CheckCharacterNotInRange(first, last, out_of_range);
1769     }
1770   } else {
1771     if (first == last) {
1772       masm->CheckCharacter(first, in_range);
1773     } else {
1774       masm->CheckCharacterInRange(first, last, in_range);
1775     }
1776     if (out_of_range != fall_through) masm->GoTo(out_of_range);
1777   }
1778 }
1779
1780
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,
1786     int start_index,
1787     int end_index,
1788     int min_char,
1789     Label* fall_through,
1790     Label* even_label,
1791     Label* odd_label) {
1792   static const int kSize = RegExpMacroAssembler::kTableSize;
1793   static const int kMask = RegExpMacroAssembler::kTableMask;
1794
1795   int base = (min_char & ~kMask);
1796   USE(base);
1797
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);
1801   }
1802   DCHECK(start_index == 0 || (ranges->at(start_index - 1) & ~kMask) <= base);
1803
1804   char templ[kSize];
1805   Label* on_bit_set;
1806   Label* on_bit_clear;
1807   int bit;
1808   if (even_label == fall_through) {
1809     on_bit_set = odd_label;
1810     on_bit_clear = even_label;
1811     bit = 1;
1812   } else {
1813     on_bit_set = even_label;
1814     on_bit_clear = odd_label;
1815     bit = 0;
1816   }
1817   for (int i = 0; i < (ranges->at(start_index) & kMask) && i < kSize; i++) {
1818     templ[i] = bit;
1819   }
1820   int j = 0;
1821   bit ^= 1;
1822   for (int i = start_index; i < end_index; i++) {
1823     for (j = (ranges->at(i) & kMask); j < (ranges->at(i + 1) & kMask); j++) {
1824       templ[j] = bit;
1825     }
1826     bit ^= 1;
1827   }
1828   for (int i = j; i < kSize; i++) {
1829     templ[i] = bit;
1830   }
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]);
1836   }
1837   masm->CheckBitInTable(ba, on_bit_set);
1838   if (on_bit_clear != fall_through) masm->GoTo(on_bit_clear);
1839 }
1840
1841
1842 static void CutOutRange(RegExpMacroAssembler* masm,
1843                         ZoneList<int>* ranges,
1844                         int start_index,
1845                         int end_index,
1846                         int cut_index,
1847                         Label* even_label,
1848                         Label* odd_label) {
1849   bool odd = (((cut_index - start_index) & 1) == 1);
1850   Label* in_range_label = odd ? odd_label : even_label;
1851   Label dummy;
1852   EmitDoubleBoundaryTest(masm,
1853                          ranges->at(cut_index),
1854                          ranges->at(cut_index + 1) - 1,
1855                          &dummy,
1856                          in_range_label,
1857                          &dummy);
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);
1864   }
1865   for (int j = cut_index + 1; j < end_index; j++) {
1866     ranges->at(j) = ranges->at(j + 1);
1867   }
1868 }
1869
1870
1871 // Unicode case.  Split the search space into kSize spaces that are handled
1872 // with recursion.
1873 static void SplitSearchSpace(ZoneList<int>* ranges,
1874                              int start_index,
1875                              int end_index,
1876                              int* new_start_index,
1877                              int* new_end_index,
1878                              int* border) {
1879   static const int kSize = RegExpMacroAssembler::kTableSize;
1880   static const int kMask = RegExpMacroAssembler::kTableMask;
1881
1882   int first = ranges->at(start_index);
1883   int last = ranges->at(end_index) - 1;
1884
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)++;
1890   }
1891   // new_start_index is the index of the first edge that is beyond the
1892   // current kSize space.
1893
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
1906   // punctuation).
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;
1913
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;
1918         break;
1919       }
1920       scan_forward_for_section_border++;
1921     }
1922   }
1923
1924   DCHECK(*new_start_index > start_index);
1925   *new_end_index = *new_start_index - 1;
1926   if (ranges->at(*new_end_index) == *border) {
1927     (*new_end_index)--;
1928   }
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;
1933   }
1934 }
1935
1936
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,
1945                              int start_index,
1946                              int end_index,
1947                              uc16 min_char,
1948                              uc16 max_char,
1949                              Label* fall_through,
1950                              Label* even_label,
1951                              Label* odd_label) {
1952   int first = ranges->at(start_index);
1953   int last = ranges->at(end_index) - 1;
1954
1955   DCHECK_LT(min_char, first);
1956
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);
1961     return;
1962   }
1963
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);
1969     return;
1970   }
1971
1972   // It's not worth using table lookup if there are very few intervals in the
1973   // character class.
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) {
1981         cut = i;
1982         break;
1983       }
1984     }
1985     if (cut == kNoCutIndex) cut = start_index;
1986     CutOutRange(
1987         masm, ranges, start_index, end_index, cut, even_label, odd_label);
1988     DCHECK_GE(end_index - start_index, 2);
1989     GenerateBranches(masm,
1990                      ranges,
1991                      start_index + 1,
1992                      end_index - 1,
1993                      min_char,
1994                      max_char,
1995                      fall_through,
1996                      even_label,
1997                      odd_label);
1998     return;
1999   }
2000
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;
2004
2005   if ((max_char >> kBits) == (min_char >> kBits)) {
2006     EmitUseLookupTable(masm,
2007                        ranges,
2008                        start_index,
2009                        end_index,
2010                        min_char,
2011                        fall_through,
2012                        even_label,
2013                        odd_label);
2014     return;
2015   }
2016
2017   if ((min_char >> kBits) != (first >> kBits)) {
2018     masm->CheckCharacterLT(first, odd_label);
2019     GenerateBranches(masm,
2020                      ranges,
2021                      start_index + 1,
2022                      end_index,
2023                      first,
2024                      max_char,
2025                      fall_through,
2026                      odd_label,
2027                      even_label);
2028     return;
2029   }
2030
2031   int new_start_index = 0;
2032   int new_end_index = 0;
2033   int border = 0;
2034
2035   SplitSearchSpace(ranges,
2036                    start_index,
2037                    end_index,
2038                    &new_start_index,
2039                    &new_end_index,
2040                    &border);
2041
2042   Label handle_rest;
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);
2049   }
2050
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));
2067
2068   masm->CheckCharacterGT(border - 1, above);
2069   Label dummy;
2070   GenerateBranches(masm,
2071                    ranges,
2072                    start_index,
2073                    new_end_index,
2074                    min_char,
2075                    border - 1,
2076                    &dummy,
2077                    even_label,
2078                    odd_label);
2079   if (handle_rest.is_linked()) {
2080     masm->Bind(&handle_rest);
2081     bool flip = (new_start_index & 1) != (start_index & 1);
2082     GenerateBranches(masm,
2083                      ranges,
2084                      new_start_index,
2085                      end_index,
2086                      border,
2087                      max_char,
2088                      &dummy,
2089                      flip ? odd_label : even_label,
2090                      flip ? even_label : odd_label);
2091   }
2092 }
2093
2094
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);
2102   }
2103
2104   int max_char;
2105   if (one_byte) {
2106     max_char = String::kMaxOneByteCharCode;
2107   } else {
2108     max_char = String::kMaxUtf16CodeUnit;
2109   }
2110
2111   int range_count = ranges->length();
2112
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) {
2117       break;
2118     }
2119     last_valid_range--;
2120   }
2121
2122   if (last_valid_range < 0) {
2123     if (!cc->is_negated()) {
2124       macro_assembler->GoTo(on_failure);
2125     }
2126     if (check_offset) {
2127       macro_assembler->CheckPosition(cp_offset, on_failure);
2128     }
2129     return;
2130   }
2131
2132   if (last_valid_range == 0 &&
2133       ranges->at(0).IsEverything(max_char)) {
2134     if (cc->is_negated()) {
2135       macro_assembler->GoTo(on_failure);
2136     } else {
2137       // This is a common case hit by non-anchored expressions.
2138       if (check_offset) {
2139         macro_assembler->CheckPosition(cp_offset, on_failure);
2140       }
2141     }
2142     return;
2143   }
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.
2148     if (check_offset) {
2149       macro_assembler->CheckPosition(cp_offset, on_failure);
2150     }
2151     return;
2152   }
2153
2154   if (!preloaded) {
2155     macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check_offset);
2156   }
2157
2158   if (cc->is_standard(zone) &&
2159         macro_assembler->CheckSpecialCharacterClass(cc->standard_type(),
2160                                                     on_failure)) {
2161       return;
2162   }
2163
2164
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);
2173
2174   bool zeroth_entry_is_failure = !cc->is_negated();
2175
2176   for (int i = 0; i <= last_valid_range; i++) {
2177     CharacterRange& range = ranges->at(i);
2178     if (range.from() == 0) {
2179       DCHECK_EQ(i, 0);
2180       zeroth_entry_is_failure = !zeroth_entry_is_failure;
2181     } else {
2182       range_boundaries->Add(range.from(), zone);
2183     }
2184     range_boundaries->Add(range.to() + 1, zone);
2185   }
2186   int end_index = range_boundaries->length() - 1;
2187   if (range_boundaries->at(end_index) > max_char) {
2188     end_index--;
2189   }
2190
2191   Label fall_through;
2192   GenerateBranches(macro_assembler,
2193                    range_boundaries,
2194                    0,  // start_index.
2195                    end_index,
2196                    0,  // min_char.
2197                    max_char,
2198                    &fall_through,
2199                    zeroth_entry_is_failure ? &fall_through : on_failure,
2200                    zeroth_entry_is_failure ? on_failure : &fall_through);
2201   macro_assembler->Bind(&fall_through);
2202 }
2203
2204
2205 RegExpNode::~RegExpNode() {
2206 }
2207
2208
2209 RegExpNode::LimitResult RegExpNode::LimitVersions(RegExpCompiler* compiler,
2210                                                   Trace* trace) {
2211   // If we are generating a greedy loop then don't stop and don't reuse code.
2212   if (trace->stop_node() != NULL) {
2213     return CONTINUE;
2214   }
2215
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_);
2222       return DONE;
2223     }
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_);
2229       return DONE;
2230     }
2231     // Generate generic version of the node and bind the label for later use.
2232     macro_assembler->Bind(&label_);
2233     return CONTINUE;
2234   }
2235
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.
2238   trace_count_++;
2239   if (compiler->optimize() && trace_count_ < kMaxCopiesCodeGenerated &&
2240       compiler->recursion_depth() <= RegExpCompiler::kMaxRecursion) {
2241     return CONTINUE;
2242   }
2243
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);
2248   return DONE;
2249 }
2250
2251
2252 int ActionNode::EatsAtLeast(int still_to_find,
2253                             int budget,
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,
2258                                    budget - 1,
2259                                    not_at_start);
2260 }
2261
2262
2263 void ActionNode::FillInBMInfo(int offset,
2264                               int budget,
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);
2271   }
2272   SaveBMInfo(bm, not_at_start, offset);
2273 }
2274
2275
2276 int AssertionNode::EatsAtLeast(int still_to_find,
2277                                int budget,
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,
2287                                    budget - 1,
2288                                    not_at_start);
2289 }
2290
2291
2292 void AssertionNode::FillInBMInfo(int offset,
2293                                  int budget,
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);
2300 }
2301
2302
2303 int BackReferenceNode::EatsAtLeast(int still_to_find,
2304                                    int budget,
2305                                    bool not_at_start) {
2306   if (budget <= 0) return 0;
2307   return on_success()->EatsAtLeast(still_to_find,
2308                                    budget - 1,
2309                                    not_at_start);
2310 }
2311
2312
2313 int TextNode::EatsAtLeast(int still_to_find,
2314                           int budget,
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,
2321                                             budget - 1,
2322                                             true);
2323 }
2324
2325
2326 int NegativeLookaheadChoiceNode::EatsAtLeast(int still_to_find,
2327                                              int budget,
2328                                              bool not_at_start) {
2329   if (budget <= 0) return 0;
2330   // Alternative 0 is the negative lookahead, alternative 1 is what comes
2331   // afterwards.
2332   RegExpNode* node = alternatives_->at(1).node();
2333   return node->EatsAtLeast(still_to_find, budget - 1, not_at_start);
2334 }
2335
2336
2337 void NegativeLookaheadChoiceNode::GetQuickCheckDetails(
2338     QuickCheckDetails* details,
2339     RegExpCompiler* compiler,
2340     int filled_in,
2341     bool not_at_start) {
2342   // Alternative 0 is the negative lookahead, alternative 1 is what comes
2343   // afterwards.
2344   RegExpNode* node = alternatives_->at(1).node();
2345   return node->GetQuickCheckDetails(details, compiler, filled_in, not_at_start);
2346 }
2347
2348
2349 int ChoiceNode::EatsAtLeastHelper(int still_to_find,
2350                                   int budget,
2351                                   RegExpNode* ignore_this_node,
2352                                   bool not_at_start) {
2353   if (budget <= 0) return 0;
2354   int min = 100;
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;
2364   }
2365   return min;
2366 }
2367
2368
2369 int LoopChoiceNode::EatsAtLeast(int still_to_find,
2370                                 int budget,
2371                                 bool not_at_start) {
2372   return EatsAtLeastHelper(still_to_find,
2373                            budget - 1,
2374                            loop_node_,
2375                            not_at_start);
2376 }
2377
2378
2379 int ChoiceNode::EatsAtLeast(int still_to_find,
2380                             int budget,
2381                             bool not_at_start) {
2382   return EatsAtLeastHelper(still_to_find,
2383                            budget,
2384                            NULL,
2385                            not_at_start);
2386 }
2387
2388
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) {
2391   v |= v >> 1;
2392   v |= v >> 2;
2393   v |= v >> 4;
2394   v |= v >> 8;
2395   v |= v >> 16;
2396   return v;
2397 }
2398
2399
2400 bool QuickCheckDetails::Rationalize(bool asc) {
2401   bool found_useful_op = false;
2402   uint32_t char_mask;
2403   if (asc) {
2404     char_mask = String::kMaxOneByteCharCode;
2405   } else {
2406     char_mask = String::kMaxUtf16CodeUnit;
2407   }
2408   mask_ = 0;
2409   value_ = 0;
2410   int char_shift = 0;
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;
2415     }
2416     mask_ |= (pos->mask & char_mask) << char_shift;
2417     value_ |= (pos->value & char_mask) << char_shift;
2418     char_shift += asc ? 8 : 16;
2419   }
2420   return found_useful_op;
2421 }
2422
2423
2424 bool RegExpNode::EmitQuickCheck(RegExpCompiler* compiler,
2425                                 Trace* bounds_check_trace,
2426                                 Trace* 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();
2440
2441   RegExpMacroAssembler* assembler = compiler->macro_assembler();
2442
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());
2453   }
2454
2455
2456   bool need_mask = true;
2457
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.
2461     uint32_t char_mask;
2462     if (compiler->one_byte()) {
2463       char_mask = String::kMaxOneByteCharCode;
2464     } else {
2465       char_mask = String::kMaxUtf16CodeUnit;
2466     }
2467     if ((mask & char_mask) == char_mask) need_mask = false;
2468     mask &= char_mask;
2469   } else {
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;
2476     } else {
2477       if (mask == 0xffffffff) need_mask = false;
2478     }
2479   }
2480
2481   if (fall_through_on_failure) {
2482     if (need_mask) {
2483       assembler->CheckCharacterAfterAnd(value, mask, on_possible_success);
2484     } else {
2485       assembler->CheckCharacter(value, on_possible_success);
2486     }
2487   } else {
2488     if (need_mask) {
2489       assembler->CheckNotCharacterAfterAnd(value, mask, trace->backtrack());
2490     } else {
2491       assembler->CheckNotCharacter(value, trace->backtrack());
2492     }
2493   }
2494   return true;
2495 }
2496
2497
2498 // Here is the meat of GetQuickCheckDetails (see also the comment on the
2499 // super-class in the .h file).
2500 //
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();
2513   int char_mask;
2514   if (compiler->one_byte()) {
2515     char_mask = String::kMaxOneByteCharCode;
2516   } else {
2517     char_mask = String::kMaxUtf16CodeUnit;
2518   }
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);
2526         uc16 c = quarks[i];
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
2531           // vice versa.
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;
2536           return;
2537         }
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).
2543           if (length == 1) {
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;
2548             pos->value = c;
2549             pos->determines_perfectly = true;
2550           } else {
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;
2557             }
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;
2565             }
2566             pos->mask = common_bits;
2567             pos->value = bits;
2568           }
2569         } else {
2570           // Don't ignore case.  Nice simple case where the mask-compare will
2571           // determine definitely whether we have a match at this character
2572           // position.
2573           pos->mask = char_mask;
2574           pos->value = c;
2575           pos->determines_perfectly = true;
2576         }
2577         characters_filled_in++;
2578         DCHECK(characters_filled_in <= details->characters());
2579         if (characters_filled_in == details->characters()) {
2580           return;
2581         }
2582       }
2583     } else {
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
2592         // succeed.
2593         pos->mask = 0;
2594         pos->value = 0;
2595       } else {
2596         int first_range = 0;
2597         while (ranges->at(first_range).from() > char_mask) {
2598           first_range++;
2599           if (first_range == ranges->length()) {
2600             details->set_cannot_match();
2601             pos->determines_perfectly = false;
2602             return;
2603           }
2604         }
2605         CharacterRange range = ranges->at(first_range);
2606         uc16 from = range.from();
2607         uc16 to = range.to();
2608         if (to > char_mask) {
2609           to = char_mask;
2610         }
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;
2617         }
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;
2639         }
2640         pos->mask = common_bits;
2641         pos->value = bits;
2642       }
2643       characters_filled_in++;
2644       DCHECK(characters_filled_in <= details->characters());
2645       if (characters_filled_in == details->characters()) {
2646         return;
2647       }
2648     }
2649   }
2650   DCHECK(characters_filled_in != details->characters());
2651   if (!details->cannot_match()) {
2652     on_success()-> GetQuickCheckDetails(details,
2653                                         compiler,
2654                                         characters_filled_in,
2655                                         true);
2656   }
2657 }
2658
2659
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;
2665   }
2666   characters_ = 0;
2667 }
2668
2669
2670 void QuickCheckDetails::Advance(int by, bool one_byte) {
2671   DCHECK(by >= 0);
2672   if (by >= characters_) {
2673     Clear();
2674     return;
2675   }
2676   for (int i = 0; i < characters_ - by; i++) {
2677     positions_[i] = positions_[by + i];
2678   }
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;
2683   }
2684   characters_ -= by;
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.
2688 }
2689
2690
2691 void QuickCheckDetails::Merge(QuickCheckDetails* other, int from_index) {
2692   DCHECK(characters_ == other->characters_);
2693   if (other->cannot_match_) {
2694     return;
2695   }
2696   if (cannot_match_) {
2697     *this = *other;
2698     return;
2699   }
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;
2709     }
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;
2716   }
2717 }
2718
2719
2720 class VisitMarker {
2721  public:
2722   explicit VisitMarker(NodeInfo* info) : info_(info) {
2723     DCHECK(!info->visited);
2724     info->visited = true;
2725   }
2726   ~VisitMarker() {
2727     info_->visited = false;
2728   }
2729  private:
2730   NodeInfo* info_;
2731 };
2732
2733
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);
2740 }
2741
2742
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);
2746   on_success_ = next;
2747   return set_replacement(this);
2748 }
2749
2750
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);
2756 }
2757
2758
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;
2763   }
2764   return false;
2765 }
2766
2767
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;
2790       }
2791     } else {
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);
2797       }
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);
2807         }
2808       } else {
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);
2814         }
2815       }
2816     }
2817   }
2818   return FilterSuccessor(depth - 1, ignore_case);
2819 }
2820
2821
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;
2826   {
2827     VisitMarker marker(info());
2828
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
2832     // loop.
2833     if (continue_replacement == NULL) return set_replacement(NULL);
2834   }
2835
2836   return ChoiceNode::FilterOneByte(depth - 1, ignore_case);
2837 }
2838
2839
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();
2846
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);
2851       return this;
2852     }
2853   }
2854
2855   int surviving = 0;
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);
2864       surviving++;
2865       survivor = replacement;
2866     }
2867   }
2868   if (surviving < 2) return set_replacement(survivor);
2869
2870   set_replacement(this);
2871   if (surviving == choice_count) {
2872     return this;
2873   }
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());
2884     }
2885   }
2886   alternatives_ = new_alternatives;
2887   return this;
2888 }
2889
2890
2891 RegExpNode* NegativeLookaheadChoiceNode::FilterOneByte(int depth,
2892                                                        bool ignore_case) {
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
2898   // afterwards.
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);
2903
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);
2911 }
2912
2913
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,
2921                                           compiler,
2922                                           characters_filled_in,
2923                                           not_at_start);
2924 }
2925
2926
2927 void LoopChoiceNode::FillInBMInfo(int offset,
2928                                   int budget,
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);
2934     return;
2935   }
2936   ChoiceNode::FillInBMInfo(offset, budget - 1, bm, not_at_start);
2937   SaveBMInfo(bm, not_at_start, offset);
2938 }
2939
2940
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,
2949                                                     compiler,
2950                                                     characters_filled_in,
2951                                                     not_at_start);
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,
2957                                not_at_start);
2958     // Here we merge the quick match details of the two branches.
2959     details->Merge(&new_details, characters_filled_in);
2960   }
2961 }
2962
2963
2964 // Check for [0-9A-Z_a-z].
2965 static void EmitWordCheck(RegExpMacroAssembler* assembler,
2966                           Label* word,
2967                           Label* non_word,
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.
2973     return;
2974   }
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);
2983   } else {
2984     assembler->CheckCharacter('_', word);
2985   }
2986 }
2987
2988
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,
2993                     Trace* trace) {
2994   RegExpMacroAssembler* assembler = compiler->macro_assembler();
2995   // We will be loading the previous character into the current character
2996   // register.
2997   Trace new_trace(*trace);
2998   new_trace.InvalidateCurrentCharacter();
2999
3000   Label ok;
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);
3005   }
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(),
3010                                   false);
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);
3016     }
3017     assembler->CheckCharacter('\n', &ok);
3018     assembler->CheckNotCharacter('\r', new_trace.backtrack());
3019   }
3020   assembler->Bind(&ok);
3021   on_success->Emit(compiler, &new_trace);
3022 }
3023
3024
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) {
3032     int eats_at_least =
3033         Min(kMaxLookaheadForBoyerMoore, EatsAtLeast(kMaxLookaheadForBoyerMoore,
3034                                                     kRecursionBudget,
3035                                                     not_at_start));
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;
3043     }
3044   } else {
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;
3049   }
3050   bool at_boundary = (assertion_type_ == AssertionNode::AT_BOUNDARY);
3051   if (next_is_word_character == Trace::UNKNOWN) {
3052     Label before_non_word;
3053     Label before_word;
3054     if (trace->characters_preloaded() != 1) {
3055       assembler->LoadCurrentCharacter(trace->cp_offset(), &before_non_word);
3056     }
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);
3061     Label ok;
3062     BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord);
3063     assembler->GoTo(&ok);
3064
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);
3070   } else {
3071     DCHECK(next_is_word_character == Trace::FALSE_VALUE);
3072     BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord);
3073   }
3074 }
3075
3076
3077 void AssertionNode::BacktrackIfPrevious(
3078     RegExpCompiler* compiler,
3079     Trace* trace,
3080     AssertionNode::IfPrevious backtrack_if_previous) {
3081   RegExpMacroAssembler* assembler = compiler->macro_assembler();
3082   Trace new_trace(*trace);
3083   new_trace.InvalidateCurrentCharacter();
3084
3085   Label fall_through, dummy;
3086
3087   Label* non_word = backtrack_if_previous == kIsNonWord ?
3088                     new_trace.backtrack() :
3089                     &fall_through;
3090   Label* word = backtrack_if_previous == kIsNonWord ?
3091                 &fall_through :
3092                 new_trace.backtrack();
3093
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);
3098   }
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);
3103
3104   assembler->Bind(&fall_through);
3105   on_success()->Emit(compiler, &new_trace);
3106 }
3107
3108
3109 void AssertionNode::GetQuickCheckDetails(QuickCheckDetails* details,
3110                                          RegExpCompiler* compiler,
3111                                          int filled_in,
3112                                          bool not_at_start) {
3113   if (assertion_type_ == AT_START && not_at_start) {
3114     details->set_cannot_match();
3115     return;
3116   }
3117   return on_success()->GetQuickCheckDetails(details,
3118                                             compiler,
3119                                             filled_in,
3120                                             not_at_start);
3121 }
3122
3123
3124 void AssertionNode::Emit(RegExpCompiler* compiler, Trace* trace) {
3125   RegExpMacroAssembler* assembler = compiler->macro_assembler();
3126   switch (assertion_type_) {
3127     case AT_END: {
3128       Label ok;
3129       assembler->CheckPosition(trace->cp_offset(), &ok);
3130       assembler->GoTo(trace->backtrack());
3131       assembler->Bind(&ok);
3132       break;
3133     }
3134     case AT_START: {
3135       if (trace->at_start() == Trace::FALSE_VALUE) {
3136         assembler->GoTo(trace->backtrack());
3137         return;
3138       }
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);
3144         return;
3145       }
3146     }
3147     break;
3148     case AFTER_NEWLINE:
3149       EmitHat(compiler, on_success(), trace);
3150       return;
3151     case AT_BOUNDARY:
3152     case AT_NON_BOUNDARY: {
3153       EmitBoundaryCheck(compiler, trace);
3154       return;
3155     }
3156   }
3157   on_success()->Emit(compiler, trace);
3158 }
3159
3160
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;
3165 }
3166
3167
3168 static void UpdateBoundsCheck(int index, int* checked_up_to) {
3169   if (index > *checked_up_to) {
3170     *checked_up_to = index;
3171   }
3172 }
3173
3174
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.
3181 //
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'.
3187 //
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
3194 // checked again.
3195 //
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,
3206                             bool preloaded,
3207                             Trace* trace,
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;
3225         switch (pass) {
3226           case NON_LATIN1_MATCH:
3227             DCHECK(one_byte);
3228             if (quarks[j] > String::kMaxOneByteCharCode) {
3229               assembler->GoTo(backtrack);
3230               return;
3231             }
3232             break;
3233           case NON_LETTER_CHARACTER_MATCH:
3234             emit_function = &EmitAtomNonLetter;
3235             break;
3236           case SIMPLE_CHARACTER_MATCH:
3237             emit_function = &EmitSimpleCharacter;
3238             break;
3239           case CASE_CHARACTER_MATCH:
3240             emit_function = &EmitAtomLetter;
3241             break;
3242           default:
3243             break;
3244         }
3245         if (emit_function != NULL) {
3246           bool bound_checked = emit_function(isolate,
3247                                              compiler,
3248                                              quarks[j],
3249                                              backtrack,
3250                                              cp_offset + j,
3251                                              *checked_up_to < cp_offset + j,
3252                                              preloaded);
3253           if (bound_checked) UpdateBoundsCheck(cp_offset + j, checked_up_to);
3254         }
3255       }
3256     } else {
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);
3265       }
3266     }
3267   }
3268 }
3269
3270
3271 int TextNode::Length() {
3272   TextElement elm = elms_->last();
3273   DCHECK(elm.cp_offset() >= 0);
3274   return elm.cp_offset() + elm.length();
3275 }
3276
3277
3278 bool TextNode::SkipPass(int int_pass, bool ignore_case) {
3279   TextEmitPassType pass = static_cast<TextEmitPassType>(int_pass);
3280   if (ignore_case) {
3281     return pass == SIMPLE_CHARACTER_MATCH;
3282   } else {
3283     return pass == NON_LETTER_CHARACTER_MATCH || pass == CASE_CHARACTER_MATCH;
3284   }
3285 }
3286
3287
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);
3298
3299   if (trace->cp_offset() + Length() > RegExpMacroAssembler::kMaxCPOffset) {
3300     compiler->SetRegExpTooBig();
3301     return;
3302   }
3303
3304   if (compiler->one_byte()) {
3305     int dummy = 0;
3306     TextEmitPass(compiler, NON_LATIN1_MATCH, false, trace, false, &dummy);
3307   }
3308
3309   bool first_elt_done = false;
3310   int bound_checked_to = trace->cp_offset() - 1;
3311   bound_checked_to += trace->bound_checked_up_to();
3312
3313   // If a character is preloaded into the current character register then
3314   // check that now.
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),
3320                      true,
3321                      trace,
3322                      false,
3323                      &bound_checked_to);
3324       }
3325     }
3326     first_elt_done = true;
3327   }
3328
3329   for (int pass = kFirstRealPass; pass <= kLastPass; pass++) {
3330     if (!SkipPass(pass, compiler->ignore_case())) {
3331       TextEmitPass(compiler,
3332                    static_cast<TextEmitPassType>(pass),
3333                    false,
3334                    trace,
3335                    first_elt_done,
3336                    &bound_checked_to);
3337     }
3338   }
3339
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);
3345 }
3346
3347
3348 void Trace::InvalidateCurrentCharacter() {
3349   characters_preloaded_ = 0;
3350 }
3351
3352
3353 void Trace::AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler) {
3354   DCHECK(by > 0);
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());
3363   cp_offset_ += by;
3364   if (cp_offset_ > RegExpMacroAssembler::kMaxCPOffset) {
3365     compiler->SetRegExpTooBig();
3366     cp_offset_ = 0;
3367   }
3368   bound_checked_up_to_ = Max(0, bound_checked_up_to_ - by);
3369 }
3370
3371
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);
3385       }
3386     }
3387   }
3388 }
3389
3390
3391 int TextNode::GreedyLoopTextLength() {
3392   TextElement elm = elms_->at(elms_->length() - 1);
3393   return elm.cp_offset() + elm.length();
3394 }
3395
3396
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);
3406   }
3407   if (node->is_negated()) {
3408     return ranges->length() == 0 ? on_success() : NULL;
3409   }
3410   if (ranges->length() != 1) return NULL;
3411   uint32_t max_char;
3412   if (compiler->one_byte()) {
3413     max_char = String::kMaxOneByteCharCode;
3414   } else {
3415     max_char = String::kMaxUtf16CodeUnit;
3416   }
3417   return ranges->at(0).IsEverything(max_char) ? on_success() : NULL;
3418 }
3419
3420
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) {
3427   int length = 0;
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;
3435     }
3436     int node_length = node->GreedyLoopTextLength();
3437     if (node_length == kNodeIsTooComplexForGreedyLoops) {
3438       return kNodeIsTooComplexForGreedyLoops;
3439     }
3440     length += node_length;
3441     SeqRegExpNode* seq_node = static_cast<SeqRegExpNode*>(node);
3442     node = seq_node->on_success();
3443   }
3444   return length;
3445 }
3446
3447
3448 void LoopChoiceNode::AddLoopAlternative(GuardedAlternative alt) {
3449   DCHECK_NULL(loop_node_);
3450   AddAlternative(alt);
3451   loop_node_ = alt.node();
3452 }
3453
3454
3455 void LoopChoiceNode::AddContinueAlternative(GuardedAlternative alt) {
3456   DCHECK_NULL(continue_node_);
3457   AddAlternative(alt);
3458   continue_node_ = alt.node();
3459 }
3460
3461
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.
3466     int text_length =
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());
3474     return;
3475   }
3476   DCHECK_NULL(trace->stop_node());
3477   if (!trace->is_trivial()) {
3478     trace->Flush(compiler, this);
3479     return;
3480   }
3481   ChoiceNode::Emit(compiler, trace);
3482 }
3483
3484
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();
3490     if (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;
3496     } else {
3497       if (preload_characters > 2) preload_characters = 2;
3498     }
3499   } else {
3500     if (preload_characters > 1) preload_characters = 1;
3501   }
3502   return preload_characters;
3503 }
3504
3505
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 {
3509  public:
3510   AlternativeGeneration()
3511       : possible_success(),
3512         expects_preload(false),
3513         after(),
3514         quick_check_details() { }
3515   Label possible_success;
3516   bool expects_preload;
3517   Label after;
3518   QuickCheckDetails quick_check_details;
3519 };
3520
3521
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 {
3525  public:
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);
3530     }
3531     for (int i = kAFew; i < count; i++) {
3532       alt_gens_.Add(new AlternativeGeneration(), zone);
3533     }
3534   }
3535   ~AlternativeGenerationList() {
3536     for (int i = kAFew; i < alt_gens_.length(); i++) {
3537       delete alt_gens_[i];
3538       alt_gens_[i] = NULL;
3539     }
3540   }
3541
3542   AlternativeGeneration* at(int i) {
3543     return alt_gens_[i];
3544   }
3545
3546  private:
3547   static const int kAFew = 10;
3548   ZoneList<AlternativeGeneration*> alt_gens_;
3549   AlternativeGeneration a_few_alt_gens_[kAFew];
3550 };
3551
3552
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);
3561
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);
3572
3573
3574 void BoyerMoorePositionInfo::Set(int character) {
3575   SetInterval(Interval(character, character));
3576 }
3577
3578
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);
3583   surrogate_ =
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;
3589     }
3590     return;
3591   }
3592   for (int i = interval.from(); i <= interval.to(); i++) {
3593     int mod_character = (i & kMask);
3594     if (!map_->at(mod_character)) {
3595       map_count_++;
3596       map_->at(mod_character) = true;
3597     }
3598     if (map_count_ == kMapSize) return;
3599   }
3600 }
3601
3602
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;
3608   }
3609 }
3610
3611
3612 BoyerMooreLookahead::BoyerMooreLookahead(
3613     int length, RegExpCompiler* compiler, Zone* zone)
3614     : length_(length),
3615       compiler_(compiler) {
3616   if (compiler->one_byte()) {
3617     max_char_ = String::kMaxOneByteCharCode;
3618   } else {
3619     max_char_ = String::kMaxUtf16CodeUnit;
3620   }
3621   bitmaps_ = new(zone) ZoneList<BoyerMoorePositionInfo*>(length, zone);
3622   for (int i = 0; i < length; i++) {
3623     bitmaps_->Add(new(zone) BoyerMoorePositionInfo(zone), zone);
3624   }
3625 }
3626
3627
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) {
3639     biggest_points =
3640         FindBestInterval(max_number_of_chars, biggest_points, from, to);
3641   }
3642   if (biggest_points == 0) return false;
3643   return true;
3644 }
3645
3646
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);
3666       i++;
3667     }
3668     int frequency = 0;
3669     for (int j = 0; j < kSize; j++) {
3670       if (union_map[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;
3677       }
3678     }
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;
3693       *to = i - 1;
3694       biggest_points = points;
3695     }
3696   }
3697   return biggest_points;
3698 }
3699
3700
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,
3707                                       int max_lookahead,
3708                                       Handle<ByteArray> boolean_skip_table) {
3709   const int kSize = RegExpMacroAssembler::kTableSize;
3710
3711   const int kSkipArrayEntry = 0;
3712   const int kDontSkipArrayEntry = 1;
3713
3714   for (int i = 0; i < kSize; i++) {
3715     boolean_skip_table->set(i, kSkipArrayEntry);
3716   }
3717   int skip = max_lookahead + 1 - min_lookahead;
3718
3719   for (int i = max_lookahead; i >= min_lookahead; i--) {
3720     BoyerMoorePositionInfo* map = bitmaps_->at(i);
3721     for (int j = 0; j < kSize; j++) {
3722       if (map->at(j)) {
3723         boolean_skip_table->set(j, kDontSkipArrayEntry);
3724       }
3725     }
3726   }
3727
3728   return skip;
3729 }
3730
3731
3732 // See comment above on the implementation of GetSkipTable.
3733 void BoyerMooreLookahead::EmitSkipInstructions(RegExpMacroAssembler* masm) {
3734   const int kSize = RegExpMacroAssembler::kTableSize;
3735
3736   int min_lookahead = 0;
3737   int max_lookahead = 0;
3738
3739   if (!FindWorthwhileInterval(&min_lookahead, &max_lookahead)) return;
3740
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;
3748       break;
3749     }
3750     for (int j = 0; j < kSize; j++) {
3751       if (map->at(j)) {
3752         found_single_character = true;
3753         single_character = j;
3754         break;
3755       }
3756     }
3757   }
3758
3759   int lookahead_width = max_lookahead + 1 - min_lookahead;
3760
3761   if (found_single_character && lookahead_width == 1 && max_lookahead < 3) {
3762     // The mask-compare can probably handle this better.
3763     return;
3764   }
3765
3766   if (found_single_character) {
3767     Label cont, again;
3768     masm->Bind(&again);
3769     masm->LoadCurrentCharacter(max_lookahead, &cont, true);
3770     if (max_char_ > kSize) {
3771       masm->CheckCharacterAfterAnd(single_character,
3772                                    RegExpMacroAssembler::kTableMask,
3773                                    &cont);
3774     } else {
3775       masm->CheckCharacter(single_character, &cont);
3776     }
3777     masm->AdvanceCurrentPosition(lookahead_width);
3778     masm->GoTo(&again);
3779     masm->Bind(&cont);
3780     return;
3781   }
3782
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);
3788
3789   Label cont, again;
3790   masm->Bind(&again);
3791   masm->LoadCurrentCharacter(max_lookahead, &cont, true);
3792   masm->CheckBitInTable(boolean_skip_table, &cont);
3793   masm->AdvanceCurrentPosition(skip_distance);
3794   masm->GoTo(&again);
3795   masm->Bind(&cont);
3796 }
3797
3798
3799 /* Code generation for choice nodes.
3800  *
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.
3805  *
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.
3816  *
3817  * ----------> R
3818  *             |
3819  *             V
3820  *            Q1 -----> S1
3821  *             |   S   /
3822  *            F|      /
3823  *             |    F/
3824  *             |    /
3825  *             |   R
3826  *             |  /
3827  *             V L
3828  *            Q2 -----> S2
3829  *             |   S   /
3830  *            F|      /
3831  *             |    F/
3832  *             |    /
3833  *             |   R
3834  *             |  /
3835  *             V L
3836  *            S3
3837  *             |
3838  *            F|
3839  *             |
3840  *             R
3841  *             |
3842  * backtrack   V
3843  * <----------Q4
3844  *   \    F    |
3845  *    \        |S
3846  *     \   F   V
3847  *      \-----S4
3848  *
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).
3854  *
3855  *              _____
3856  *             /     \
3857  *             V     |
3858  * ----------> S1    |
3859  *            /|     |
3860  *           / |S    |
3861  *         F/  \_____/
3862  *         /
3863  *        |<-----
3864  *        |      \
3865  *        V       |S
3866  *        Q2 ---> U----->backtrack
3867  *        |  F   /
3868  *       S|     /
3869  *        V  F /
3870  *        S2--/
3871  */
3872
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);
3876 }
3877
3878
3879 void ChoiceNode::AssertGuardsMentionRegisters(Trace* trace) {
3880 #ifdef DEBUG
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()));
3888     }
3889   }
3890 #endif
3891 }
3892
3893
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);
3902     }
3903     state->preload_characters_ =
3904         CalculatePreloadCharacters(compiler, state->eats_at_least_);
3905
3906     state->preload_is_current_ =
3907         (current_trace->characters_preloaded() == state->preload_characters_);
3908     state->preload_has_checked_bounds_ = state->preload_is_current_;
3909 }
3910
3911
3912 void ChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) {
3913   int choice_count = alternatives_->length();
3914
3915   AssertGuardsMentionRegisters(trace);
3916
3917   LimitResult limit_result = LimitVersions(compiler, trace);
3918   if (limit_result == DONE) return;
3919   DCHECK(limit_result == CONTINUE);
3920
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);
3925     return;
3926   }
3927
3928   RecursionCheck rc(compiler);
3929
3930   PreloadState preload;
3931   preload.init();
3932   GreedyLoopState greedy_loop_state(not_at_start());
3933
3934   int text_length = GreedyLoopTextLengthForAlternative(&alternatives_->at(0));
3935   AlternativeGenerationList alt_gens(choice_count, zone());
3936
3937   if (choice_count > 1 && text_length != kNodeIsTooComplexForGreedyLoops) {
3938     trace = EmitGreedyLoop(compiler,
3939                            trace,
3940                            &alt_gens,
3941                            &preload,
3942                            &greedy_loop_state,
3943                            text_length);
3944   } else {
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);
3949
3950     preload.eats_at_least_ = EmitOptimizedUnanchoredSearch(compiler, trace);
3951
3952     EmitChoices(compiler,
3953                 &alt_gens,
3954                 0,
3955                 trace,
3956                 &preload);
3957   }
3958
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
3961   // label was bound.
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);
3971     }
3972     bool next_expects_preload =
3973         i == choice_count - 1 ? false : alt_gens.at(i + 1)->expects_preload;
3974     EmitOutOfLineContinuation(compiler,
3975                               &new_trace,
3976                               alternatives_->at(i),
3977                               alt_gen,
3978                               preload.preload_characters_,
3979                               next_expects_preload);
3980   }
3981 }
3982
3983
3984 Trace* ChoiceNode::EmitGreedyLoop(RegExpCompiler* compiler,
3985                                   Trace* trace,
3986                                   AlternativeGenerationList* alt_gens,
3987                                   PreloadState* preload,
3988                                   GreedyLoopState* greedy_loop_state,
3989                                   int text_length) {
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
3997   // space.
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);
4004   Label loop_label;
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);
4010
4011   Label second_choice;  // For use in greedy matches.
4012   macro_assembler->Bind(&second_choice);
4013
4014   Trace* new_trace = greedy_loop_state->counter_backtrack_trace();
4015
4016   EmitChoices(compiler,
4017               alt_gens,
4018               1,
4019               new_trace,
4020               preload);
4021
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);
4028   return new_trace;
4029 }
4030
4031 int ChoiceNode::EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler,
4032                                               Trace* trace) {
4033   int eats_at_least = PreloadState::kEatsAtLeastNotYetInitialized;
4034   if (alternatives_->length() != 2) return eats_at_least;
4035
4036   GuardedAlternative alt1 = alternatives_->at(1);
4037   if (alt1.guards() != NULL && alt1.guards()->length() != 0) {
4038     return eats_at_least;
4039   }
4040   RegExpNode* eats_anything_node = alt1.node();
4041   if (eats_anything_node->GetSuccessorOfOmnivorousTextNode(compiler) != this) {
4042     return eats_at_least;
4043   }
4044
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());
4052
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);
4062   if (bm == NULL) {
4063     eats_at_least = Min(kMaxLookaheadForBoyerMoore,
4064                         EatsAtLeast(kMaxLookaheadForBoyerMoore,
4065                                     kRecursionBudget,
4066                                     false));
4067     if (eats_at_least >= 1) {
4068       bm = new(zone()) BoyerMooreLookahead(eats_at_least,
4069                                            compiler,
4070                                            zone());
4071       GuardedAlternative alt0 = alternatives_->at(0);
4072       alt0.node()->FillInBMInfo(0, kRecursionBudget, bm, false);
4073     }
4074   }
4075   if (bm != NULL) {
4076     bm->EmitSkipInstructions(macro_assembler);
4077   }
4078   return eats_at_least;
4079 }
4080
4081
4082 void ChoiceNode::EmitChoices(RegExpCompiler* compiler,
4083                              AlternativeGenerationList* alt_gens,
4084                              int first_choice,
4085                              Trace* trace,
4086                              PreloadState* preload) {
4087   RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
4088   SetUpPreLoad(compiler, trace, preload);
4089
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();
4093
4094   int new_flush_budget = trace->flush_budget() / choice_count;
4095
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_ :
4107                                          0);
4108     if (preload->preload_has_checked_bounds_) {
4109       new_trace.set_bound_checked_up_to(preload->preload_characters_);
4110     }
4111     new_trace.quick_check_performed()->Clear();
4112     if (not_at_start_) new_trace.set_at_start(Trace::FALSE_VALUE);
4113     if (!is_last) {
4114       new_trace.set_backtrack(&alt_gen->after);
4115     }
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;
4135       }
4136     } else if (alt_gen->quick_check_details.cannot_match()) {
4137       if (!fall_through_on_failure) {
4138         macro_assembler->GoTo(trace->backtrack());
4139       }
4140       continue;
4141     } else {
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();
4150       }
4151       generate_full_check_inline = true;
4152     }
4153     if (generate_full_check_inline) {
4154       if (new_trace.actions() != NULL) {
4155         new_trace.set_flush_budget(new_flush_budget);
4156       }
4157       for (int j = 0; j < guard_count; j++) {
4158         GenerateGuard(macro_assembler, guards->at(j), &new_trace);
4159       }
4160       alternative.node()->Emit(compiler, &new_trace);
4161       preload->preload_is_current_ = false;
4162     }
4163     macro_assembler->Bind(&alt_gen->after);
4164   }
4165 }
4166
4167
4168 void ChoiceNode::EmitOutOfLineContinuation(RegExpCompiler* compiler,
4169                                            Trace* trace,
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;
4175
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);
4189     }
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(),
4196                                           NULL,
4197                                           false,
4198                                           preload_characters);
4199     macro_assembler->GoTo(&(alt_gen->after));
4200   } else {
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);
4204     }
4205     alternative.node()->Emit(compiler, &out_of_line_trace);
4206   }
4207 }
4208
4209
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);
4215
4216   RecursionCheck rc(compiler);
4217
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,
4223                       trace);
4224       Trace new_trace = *trace;
4225       new_trace.add_action(&new_capture);
4226       on_success()->Emit(compiler, &new_trace);
4227       break;
4228     }
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);
4235       break;
4236     }
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);
4243       break;
4244     }
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);
4252       break;
4253     }
4254     case BEGIN_SUBMATCH:
4255       if (!trace->is_trivial()) {
4256         trace->Flush(compiler, this);
4257       } else {
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);
4263       }
4264       break;
4265     case EMPTY_MATCH_CHECK: {
4266       int start_pos_reg = data_.u_empty_match_check.start_register;
4267       int stored_pos = 0;
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
4277         // immediately.
4278         on_success()->Emit(compiler, trace);
4279       } else if (!trace->is_trivial()) {
4280         trace->Flush(compiler, this);
4281       } else {
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.
4285         if (has_minimum) {
4286           int limit = data_.u_empty_match_check.repetition_limit;
4287           assembler->IfRegisterLT(rep_reg, limit, &skip_empty_check);
4288         }
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);
4295       }
4296       break;
4297     }
4298     case POSITIVE_SUBMATCH_SUCCESS: {
4299       if (!trace->is_trivial()) {
4300         trace->Flush(compiler, this);
4301         return;
4302       }
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);
4310         return;
4311       }
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);
4317
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);
4321
4322       DCHECK(trace->backtrack() == NULL);
4323       assembler->Backtrack();
4324       return;
4325     }
4326     default:
4327       UNREACHABLE();
4328   }
4329 }
4330
4331
4332 void BackReferenceNode::Emit(RegExpCompiler* compiler, Trace* trace) {
4333   RegExpMacroAssembler* assembler = compiler->macro_assembler();
4334   if (!trace->is_trivial()) {
4335     trace->Flush(compiler, this);
4336     return;
4337   }
4338
4339   LimitResult limit_result = LimitVersions(compiler, trace);
4340   if (limit_result == DONE) return;
4341   DCHECK(limit_result == CONTINUE);
4342
4343   RecursionCheck rc(compiler);
4344
4345   DCHECK_EQ(start_reg_ + 1, end_reg_);
4346   if (compiler->ignore_case()) {
4347     assembler->CheckNotBackReferenceIgnoreCase(start_reg_,
4348                                                trace->backtrack());
4349   } else {
4350     assembler->CheckNotBackReference(start_reg_, trace->backtrack());
4351   }
4352   on_success()->Emit(compiler, trace);
4353 }
4354
4355
4356 // -------------------------------------------------------------------
4357 // Dot/dotty output
4358
4359
4360 #ifdef DEBUG
4361
4362
4363 class DotPrinter: public NodeVisitor {
4364  public:
4365   DotPrinter(std::ostream& os, bool ignore_case)  // NOLINT
4366       : os_(os),
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
4376  private:
4377   std::ostream& os_;
4378   bool ignore_case_;
4379 };
4380
4381
4382 void DotPrinter::PrintNode(const char* label, RegExpNode* node) {
4383   os_ << "digraph G {\n  graph [label=\"";
4384   for (int i = 0; label[i]; i++) {
4385     switch (label[i]) {
4386       case '\\':
4387         os_ << "\\\\";
4388         break;
4389       case '"':
4390         os_ << "\"";
4391         break;
4392       default:
4393         os_ << label[i];
4394         break;
4395     }
4396   }
4397   os_ << "\"];\n";
4398   Visit(node);
4399   os_ << "}" << std::endl;
4400 }
4401
4402
4403 void DotPrinter::Visit(RegExpNode* node) {
4404   if (node->info()->visited) return;
4405   node->info()->visited = true;
4406   node->Accept(this);
4407 }
4408
4409
4410 void DotPrinter::PrintOnFailure(RegExpNode* from, RegExpNode* on_failure) {
4411   os_ << "  n" << from << " -> n" << on_failure << " [style=dotted];\n";
4412   Visit(on_failure);
4413 }
4414
4415
4416 class TableEntryBodyPrinter {
4417  public:
4418   TableEntryBodyPrinter(std::ostream& os, ChoiceNode* choice)  // NOLINT
4419       : os_(os),
4420         choice_(choice) {}
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";
4427       }
4428     }
4429   }
4430  private:
4431   ChoiceNode* choice() { return choice_; }
4432   std::ostream& os_;
4433   ChoiceNode* choice_;
4434 };
4435
4436
4437 class TableEntryHeaderPrinter {
4438  public:
4439   explicit TableEntryHeaderPrinter(std::ostream& os)  // NOLINT
4440       : first_(true),
4441         os_(os) {}
4442   void Call(uc16 from, DispatchTable::Entry entry) {
4443     if (first_) {
4444       first_ = false;
4445     } else {
4446       os_ << "|";
4447     }
4448     os_ << "{\\" << AsUC16(from) << "-\\" << AsUC16(entry.to()) << "|{";
4449     OutSet* out_set = entry.out_set();
4450     int priority = 0;
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;
4455         priority++;
4456       }
4457     }
4458     os_ << "}}";
4459   }
4460
4461  private:
4462   bool first_;
4463   std::ostream& os_;
4464 };
4465
4466
4467 class AttributePrinter {
4468  public:
4469   explicit AttributePrinter(std::ostream& os)  // NOLINT
4470       : os_(os),
4471         first_(true) {}
4472   void PrintSeparator() {
4473     if (first_) {
4474       first_ = false;
4475     } else {
4476       os_ << "|";
4477     }
4478   }
4479   void PrintBit(const char* name, bool value) {
4480     if (!value) return;
4481     PrintSeparator();
4482     os_ << "{" << name << "}";
4483   }
4484   void PrintPositive(const char* name, int value) {
4485     if (value < 0) return;
4486     PrintSeparator();
4487     os_ << "{" << name << "|" << value << "}";
4488   }
4489
4490  private:
4491   std::ostream& os_;
4492   bool first_;
4493 };
4494
4495
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());
4507   os_ << "}\"];\n"
4508       << "  a" << that << " -> n" << that
4509       << " [style=dashed, color=grey, arrowhead=none];\n";
4510 }
4511
4512
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);
4519     os_ << "\"]\n";
4520     PrintAttributes(that);
4521     TableEntryBodyPrinter body_printer(os_, that);
4522     that->GetTable(ignore_case_)->ForEach(&body_printer);
4523   } else {
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();
4528     }
4529   }
4530   for (int i = 0; i < that->alternatives()->length(); i++) {
4531     GuardedAlternative alt = that->alternatives()->at(i);
4532     alt.node()->Accept(this);
4533   }
4534 }
4535
4536
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]);
4548         }
4549         break;
4550       }
4551       case TextElement::CHAR_CLASS: {
4552         RegExpCharacterClass* node = elm.char_class();
4553         os_ << "[";
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());
4558         }
4559         os_ << "]";
4560         break;
4561       }
4562       default:
4563         UNREACHABLE();
4564     }
4565   }
4566   os_ << "\", shape=box, peripheries=2];\n";
4567   PrintAttributes(that);
4568   os_ << "  n" << that << " -> n" << that->on_success() << ";\n";
4569   Visit(that->on_success());
4570 }
4571
4572
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());
4579 }
4580
4581
4582 void DotPrinter::VisitEnd(EndNode* that) {
4583   os_ << "  n" << that << " [style=bold, shape=point];\n";
4584   PrintAttributes(that);
4585 }
4586
4587
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";
4593       break;
4594     case AssertionNode::AT_START:
4595       os_ << "label=\"^\", shape=septagon";
4596       break;
4597     case AssertionNode::AT_BOUNDARY:
4598       os_ << "label=\"\\b\", shape=septagon";
4599       break;
4600     case AssertionNode::AT_NON_BOUNDARY:
4601       os_ << "label=\"\\B\", shape=septagon";
4602       break;
4603     case AssertionNode::AFTER_NEWLINE:
4604       os_ << "label=\"(?<=\\n)\", shape=septagon";
4605       break;
4606   }
4607   os_ << "];\n";
4608   PrintAttributes(that);
4609   RegExpNode* successor = that->on_success();
4610   os_ << "  n" << that << " -> n" << successor << ";\n";
4611   Visit(successor);
4612 }
4613
4614
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";
4621       break;
4622     case ActionNode::INCREMENT_REGISTER:
4623       os_ << "label=\"$" << that->data_.u_increment_register.reg
4624           << "++\", shape=octagon";
4625       break;
4626     case ActionNode::STORE_POSITION:
4627       os_ << "label=\"$" << that->data_.u_position_register.reg
4628           << ":=$pos\", shape=octagon";
4629       break;
4630     case ActionNode::BEGIN_SUBMATCH:
4631       os_ << "label=\"$" << that->data_.u_submatch.current_position_register
4632           << ":=$pos,begin\", shape=septagon";
4633       break;
4634     case ActionNode::POSITIVE_SUBMATCH_SUCCESS:
4635       os_ << "label=\"escape\", shape=septagon";
4636       break;
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";
4642       break;
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";
4647       break;
4648     }
4649   }
4650   os_ << "];\n";
4651   PrintAttributes(that);
4652   RegExpNode* successor = that->on_success();
4653   os_ << "  n" << that << " -> n" << successor << ";\n";
4654   Visit(successor);
4655 }
4656
4657
4658 class DispatchTableDumper {
4659  public:
4660   explicit DispatchTableDumper(std::ostream& os) : os_(os) {}
4661   void Call(uc16 key, DispatchTable::Entry entry);
4662  private:
4663   std::ostream& os_;
4664 };
4665
4666
4667 void DispatchTableDumper::Call(uc16 key, DispatchTable::Entry entry) {
4668   os_ << "[" << AsUC16(key) << "-" << AsUC16(entry.to()) << "]: {";
4669   OutSet* set = entry.out_set();
4670   bool first = true;
4671   for (unsigned i = 0; i < OutSet::kFirstLimit; i++) {
4672     if (set->Get(i)) {
4673       if (first) {
4674         first = false;
4675       } else {
4676         os_ << ", ";
4677       }
4678       os_ << i;
4679     }
4680   }
4681   os_ << "}\n";
4682 }
4683
4684
4685 void DispatchTable::Dump() {
4686   OFStream os(stderr);
4687   DispatchTableDumper dumper(os);
4688   tree()->ForEach(&dumper);
4689 }
4690
4691
4692 void RegExpEngine::DotPrint(const char* label,
4693                             RegExpNode* node,
4694                             bool ignore_case) {
4695   OFStream os(stdout);
4696   DotPrinter printer(os, ignore_case);
4697   printer.PrintNode(label, node);
4698 }
4699
4700
4701 #endif  // DEBUG
4702
4703
4704 // -------------------------------------------------------------------
4705 // Tree to graph conversion
4706
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);
4713 }
4714
4715
4716 RegExpNode* RegExpText::ToNode(RegExpCompiler* compiler,
4717                                RegExpNode* on_success) {
4718   return new(compiler->zone()) TextNode(elements(), on_success);
4719 }
4720
4721
4722 static bool CompareInverseRanges(ZoneList<CharacterRange>* ranges,
4723                                  const int* special_class,
4724                                  int length) {
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) {
4731     return false;
4732   }
4733   CharacterRange range = ranges->at(0);
4734   if (range.from() != 0) {
4735     return false;
4736   }
4737   for (int i = 0; i < length; i += 2) {
4738     if (special_class[i] != (range.to() + 1)) {
4739       return false;
4740     }
4741     range = ranges->at((i >> 1) + 1);
4742     if (special_class[i+1] != range.from()) {
4743       return false;
4744     }
4745   }
4746   if (range.to() != 0xffff) {
4747     return false;
4748   }
4749   return true;
4750 }
4751
4752
4753 static bool CompareRanges(ZoneList<CharacterRange>* ranges,
4754                           const int* special_class,
4755                           int length) {
4756   length--;  // Remove final 0x10000.
4757   DCHECK(special_class[length] == 0x10000);
4758   if (ranges->length() * 2 != length) {
4759     return false;
4760   }
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) {
4765       return false;
4766     }
4767   }
4768   return true;
4769 }
4770
4771
4772 bool RegExpCharacterClass::is_standard(Zone* zone) {
4773   // TODO(lrn): Remove need for this function, by not throwing away information
4774   // along the way.
4775   if (is_negated_) {
4776     return false;
4777   }
4778   if (set_.is_standard()) {
4779     return true;
4780   }
4781   if (CompareRanges(set_.ranges(zone), kSpaceRanges, kSpaceRangeCount)) {
4782     set_.set_standard_set_type('s');
4783     return true;
4784   }
4785   if (CompareInverseRanges(set_.ranges(zone), kSpaceRanges, kSpaceRangeCount)) {
4786     set_.set_standard_set_type('S');
4787     return true;
4788   }
4789   if (CompareInverseRanges(set_.ranges(zone),
4790                            kLineTerminatorRanges,
4791                            kLineTerminatorRangeCount)) {
4792     set_.set_standard_set_type('.');
4793     return true;
4794   }
4795   if (CompareRanges(set_.ranges(zone),
4796                     kLineTerminatorRanges,
4797                     kLineTerminatorRangeCount)) {
4798     set_.set_standard_set_type('n');
4799     return true;
4800   }
4801   if (CompareRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) {
4802     set_.set_standard_set_type('w');
4803     return true;
4804   }
4805   if (CompareInverseRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) {
4806     set_.set_standard_set_type('W');
4807     return true;
4808   }
4809   return false;
4810 }
4811
4812
4813 RegExpNode* RegExpCharacterClass::ToNode(RegExpCompiler* compiler,
4814                                          RegExpNode* on_success) {
4815   return new(compiler->zone()) TextNode(this, on_success);
4816 }
4817
4818
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,
4827                                                                on_success));
4828     result->AddAlternative(alternative);
4829   }
4830   return result;
4831 }
4832
4833
4834 RegExpNode* RegExpQuantifier::ToNode(RegExpCompiler* compiler,
4835                                      RegExpNode* on_success) {
4836   return ToNode(min(),
4837                 max(),
4838                 is_greedy(),
4839                 body(),
4840                 compiler,
4841                 on_success);
4842 }
4843
4844
4845 // Scoped object to keep track of how much we unroll quantifier loops in the
4846 // regexp graph generator.
4847 class RegExpExpansionLimiter {
4848  public:
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) {
4854     DCHECK(factor > 0);
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);
4860       } else {
4861         int new_factor = saved_expansion_factor_ * factor;
4862         ok_to_expand_ = (new_factor <= kMaxExpansionFactor);
4863         compiler->set_current_expansion_factor(new_factor);
4864       }
4865     }
4866   }
4867
4868   ~RegExpExpansionLimiter() {
4869     compiler_->set_current_expansion_factor(saved_expansion_factor_);
4870   }
4871
4872   bool ok_to_expand() { return ok_to_expand_; }
4873
4874  private:
4875   RegExpCompiler* compiler_;
4876   int saved_expansion_factor_;
4877   bool ok_to_expand_;
4878
4879   DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpExpansionLimiter);
4880 };
4881
4882
4883 RegExpNode* RegExpQuantifier::ToNode(int min,
4884                                      int max,
4885                                      bool is_greedy,
4886                                      RegExpTree* body,
4887                                      RegExpCompiler* compiler,
4888                                      RegExpNode* on_success,
4889                                      bool not_at_start) {
4890   // x{f, t} becomes this:
4891   //
4892   //             (r++)<-.
4893   //               |     `
4894   //               |     (x)
4895   //               v     ^
4896   //      (r=0)-->(?)---/ [if r < t]
4897   //               |
4898   //   [if r >= f] \----> ...
4899   //
4900
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.
4905
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
4909   // this case.
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();
4918
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
4923     // empty.
4924     {
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
4930         // ones.
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);
4938         }
4939         return answer;
4940       }
4941     }
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);
4950           if (is_greedy) {
4951             alternation->AddAlternative(
4952                 GuardedAlternative(body->ToNode(compiler, answer)));
4953             alternation->AddAlternative(GuardedAlternative(on_success));
4954           } else {
4955             alternation->AddAlternative(GuardedAlternative(on_success));
4956             alternation->AddAlternative(
4957                 GuardedAlternative(body->ToNode(compiler, answer)));
4958           }
4959           answer = alternation;
4960           if (not_at_start) alternation->set_not_at_start();
4961         }
4962         return answer;
4963       }
4964     }
4965   }
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,
4973                                                     zone);
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
4980     // backtrack.
4981     loop_return = ActionNode::EmptyMatchCheck(body_start_reg,
4982                                               reg_ctr,
4983                                               min,
4984                                               loop_return);
4985   }
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);
4991   }
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);
4995   }
4996   GuardedAlternative body_alt(body_node);
4997   if (has_max) {
4998     Guard* body_guard =
4999         new(zone) Guard(reg_ctr, Guard::LT, max);
5000     body_alt.AddGuard(body_guard, zone);
5001   }
5002   GuardedAlternative rest_alt(on_success);
5003   if (has_min) {
5004     Guard* rest_guard = new(compiler->zone()) Guard(reg_ctr, Guard::GEQ, min);
5005     rest_alt.AddGuard(rest_guard, zone);
5006   }
5007   if (is_greedy) {
5008     center->AddLoopAlternative(body_alt);
5009     center->AddContinueAlternative(rest_alt);
5010   } else {
5011     center->AddContinueAlternative(rest_alt);
5012     center->AddLoopAlternative(body_alt);
5013   }
5014   if (needs_counter) {
5015     return ActionNode::SetRegister(reg_ctr, 0, center);
5016   } else {
5017     return center;
5018   }
5019 }
5020
5021
5022 RegExpNode* RegExpAssertion::ToNode(RegExpCompiler* compiler,
5023                                     RegExpNode* on_success) {
5024   NodeInfo info;
5025   Zone* zone = compiler->zone();
5026
5027   switch (assertion_type()) {
5028     case START_OF_LINE:
5029       return AssertionNode::AfterNewline(on_success);
5030     case START_OF_INPUT:
5031       return AssertionNode::AtStart(on_success);
5032     case BOUNDARY:
5033       return AssertionNode::AtBoundary(on_success);
5034     case NON_BOUNDARY:
5035       return AssertionNode::AtNonBoundary(on_success);
5036     case END_OF_INPUT:
5037       return AssertionNode::AtEnd(on_success);
5038     case END_OF_LINE: {
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(
5052          newline_atom,
5053          ActionNode::PositiveSubmatchSuccess(stack_pointer_register,
5054                                              position_register,
5055                                              0,  // No captures inside.
5056                                              -1,  // Ignored if no captures.
5057                                              on_success));
5058       // Create an end-of-input matcher.
5059       RegExpNode* end_of_line = ActionNode::BeginSubmatch(
5060           stack_pointer_register,
5061           position_register,
5062           newline_matcher);
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);
5068       return result;
5069     }
5070     default:
5071       UNREACHABLE();
5072   }
5073   return on_success;
5074 }
5075
5076
5077 RegExpNode* RegExpBackReference::ToNode(RegExpCompiler* compiler,
5078                                         RegExpNode* on_success) {
5079   return new(compiler->zone())
5080       BackReferenceNode(RegExpCapture::StartRegister(index()),
5081                         RegExpCapture::EndRegister(index()),
5082                         on_success);
5083 }
5084
5085
5086 RegExpNode* RegExpEmpty::ToNode(RegExpCompiler* compiler,
5087                                 RegExpNode* on_success) {
5088   return on_success;
5089 }
5090
5091
5092 RegExpNode* RegExpLookahead::ToNode(RegExpCompiler* compiler,
5093                                     RegExpNode* on_success) {
5094   int stack_pointer_register = compiler->AllocateRegister();
5095   int position_register = compiler->AllocateRegister();
5096
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;
5102
5103   RegExpNode* success;
5104   if (is_positive()) {
5105     RegExpNode* node = ActionNode::BeginSubmatch(
5106         stack_pointer_register,
5107         position_register,
5108         body()->ToNode(
5109             compiler,
5110             ActionNode::PositiveSubmatchSuccess(stack_pointer_register,
5111                                                 position_register,
5112                                                 register_count,
5113                                                 register_start,
5114                                                 on_success)));
5115     return node;
5116   } else {
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
5126     // checks.
5127     Zone* zone = compiler->zone();
5128
5129     GuardedAlternative body_alt(
5130         body()->ToNode(
5131             compiler,
5132             success = new(zone) NegativeSubmatchSuccess(stack_pointer_register,
5133                                                         position_register,
5134                                                         register_count,
5135                                                         register_start,
5136                                                         zone)));
5137     ChoiceNode* choice_node =
5138         new(zone) NegativeLookaheadChoiceNode(body_alt,
5139                                               GuardedAlternative(on_success),
5140                                               zone);
5141     return ActionNode::BeginSubmatch(stack_pointer_register,
5142                                      position_register,
5143                                      choice_node);
5144   }
5145 }
5146
5147
5148 RegExpNode* RegExpCapture::ToNode(RegExpCompiler* compiler,
5149                                   RegExpNode* on_success) {
5150   return ToNode(body(), index(), compiler, on_success);
5151 }
5152
5153
5154 RegExpNode* RegExpCapture::ToNode(RegExpTree* body,
5155                                   int index,
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);
5163 }
5164
5165
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);
5172   }
5173   return current;
5174 }
5175
5176
5177 static void AddClass(const int* elmv,
5178                      int elmc,
5179                      ZoneList<CharacterRange>* ranges,
5180                      Zone* zone) {
5181   elmc--;
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);
5186   }
5187 }
5188
5189
5190 static void AddClassNegated(const int *elmv,
5191                             int elmc,
5192                             ZoneList<CharacterRange>* ranges,
5193                             Zone* zone) {
5194   elmc--;
5195   DCHECK(elmv[elmc] == 0x10000);
5196   DCHECK(elmv[0] != 0x0000);
5197   DCHECK(elmv[elmc-1] != String::kMaxUtf16CodeUnit);
5198   uc16 last = 0x0000;
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);
5203     last = elmv[i + 1];
5204   }
5205   ranges->Add(CharacterRange(last, String::kMaxUtf16CodeUnit), zone);
5206 }
5207
5208
5209 void CharacterRange::AddClassEscape(uc16 type,
5210                                     ZoneList<CharacterRange>* ranges,
5211                                     Zone* zone) {
5212   switch (type) {
5213     case 's':
5214       AddClass(kSpaceRanges, kSpaceRangeCount, ranges, zone);
5215       break;
5216     case 'S':
5217       AddClassNegated(kSpaceRanges, kSpaceRangeCount, ranges, zone);
5218       break;
5219     case 'w':
5220       AddClass(kWordRanges, kWordRangeCount, ranges, zone);
5221       break;
5222     case 'W':
5223       AddClassNegated(kWordRanges, kWordRangeCount, ranges, zone);
5224       break;
5225     case 'd':
5226       AddClass(kDigitRanges, kDigitRangeCount, ranges, zone);
5227       break;
5228     case 'D':
5229       AddClassNegated(kDigitRanges, kDigitRangeCount, ranges, zone);
5230       break;
5231     case '.':
5232       AddClassNegated(kLineTerminatorRanges,
5233                       kLineTerminatorRangeCount,
5234                       ranges,
5235                       zone);
5236       break;
5237     // This is not a character range as defined by the spec but a
5238     // convenient shorthand for a character class that matches any
5239     // character.
5240     case '*':
5241       ranges->Add(CharacterRange::Everything(), zone);
5242       break;
5243     // This is the set of characters matched by the $ and ^ symbols
5244     // in multiline mode.
5245     case 'n':
5246       AddClass(kLineTerminatorRanges,
5247                kLineTerminatorRangeCount,
5248                ranges,
5249                zone);
5250       break;
5251     default:
5252       UNREACHABLE();
5253   }
5254 }
5255
5256
5257 Vector<const int> CharacterRange::GetWordBounds() {
5258   return Vector<const int>(kWordRanges, kWordRangeCount - 1);
5259 }
5260
5261
5262 class CharacterRangeSplitter {
5263  public:
5264   CharacterRangeSplitter(ZoneList<CharacterRange>** included,
5265                          ZoneList<CharacterRange>** excluded,
5266                          Zone* zone)
5267       : included_(included),
5268         excluded_(excluded),
5269         zone_(zone) { }
5270   void Call(uc16 from, DispatchTable::Entry entry);
5271
5272   static const int kInBase = 0;
5273   static const int kInOverlay = 1;
5274
5275  private:
5276   ZoneList<CharacterRange>** included_;
5277   ZoneList<CharacterRange>** excluded_;
5278   Zone* zone_;
5279 };
5280
5281
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)
5285     ? included_
5286     : excluded_;
5287   if (*target == NULL) *target = new(zone_) ZoneList<CharacterRange>(2, zone_);
5288   (*target)->Add(CharacterRange(entry.from(), entry.to()), zone_);
5289 }
5290
5291
5292 void CharacterRange::Split(ZoneList<CharacterRange>* base,
5293                            Vector<const int> overlay,
5294                            ZoneList<CharacterRange>** included,
5295                            ZoneList<CharacterRange>** excluded,
5296                            Zone* zone) {
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);
5305   }
5306   CharacterRangeSplitter callback(included, excluded, zone);
5307   table.ForEach(&callback);
5308 }
5309
5310
5311 void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone,
5312                                         ZoneList<CharacterRange>* ranges,
5313                                         bool is_one_byte) {
5314   uc16 bottom = from();
5315   uc16 top = to();
5316   if (is_one_byte && !RangeContainsLatin1Equivalents(*this)) {
5317     if (bottom > String::kMaxOneByteCharCode) return;
5318     if (top > String::kMaxOneByteCharCode) top = String::kMaxOneByteCharCode;
5319   }
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);
5328       }
5329     }
5330   } else {
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];
5350     int pos = bottom;
5351     while (pos <= top) {
5352       int length = isolate->jsregexp_canonrange()->get(pos, '\0', range);
5353       uc16 block_end;
5354       if (length == 0) {
5355         block_end = pos;
5356       } else {
5357         DCHECK_EQ(1, length);
5358         block_end = range[0];
5359       }
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++) {
5363         uc32 c = range[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);
5368         }
5369       }
5370       pos = end + 1;
5371     }
5372   }
5373 }
5374
5375
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();
5385   }
5386   return true;
5387 }
5388
5389
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);
5394   }
5395   return ranges_;
5396 }
5397
5398
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,
5402                        int from,
5403                        int to,
5404                        int count) {
5405   // Ranges are potentially overlapping.
5406   if (from < to) {
5407     for (int i = count - 1; i >= 0; i--) {
5408       list->at(to + i) = list->at(from + i);
5409     }
5410   } else {
5411     for (int i = 0; i < count; i++) {
5412       list->at(to + i) = list->at(from + i);
5413     }
5414   }
5415 }
5416
5417
5418 static int InsertRangeInCanonicalList(ZoneList<CharacterRange>* list,
5419                                       int count,
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();
5428   int start_pos = 0;
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) {
5433       end_pos = i;
5434     } else if (current.to() + 1 < from) {
5435       start_pos = i + 1;
5436       break;
5437     }
5438   }
5439
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.
5446
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);
5451     }
5452     list->at(start_pos) = insert;
5453     return count + 1;
5454   }
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);
5461     return count;
5462   }
5463   // Replace a number of existing ranges from start_pos to end_pos - 1.
5464   // Move the remaining ranges down.
5465
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);
5470   }
5471   list->at(start_pos) = CharacterRange(new_from, new_to);
5472   return count - (end_pos - start_pos) + 1;
5473 }
5474
5475
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_);
5481 }
5482
5483
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,
5487   // non-adjacent).
5488   int n = character_ranges->length();
5489   int max = character_ranges->at(0).to();
5490   int i = 1;
5491   while (i < n) {
5492     CharacterRange current = character_ranges->at(i);
5493     if (current.from() <= max + 1) {
5494       break;
5495     }
5496     max = current.to();
5497     i++;
5498   }
5499   // Canonical until the i'th range. If that's all of them, we are done.
5500   if (i == n) return;
5501
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
5504   // list, in order).
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.
5509   do {
5510     num_canonical = InsertRangeInCanonicalList(character_ranges,
5511                                                num_canonical,
5512                                                character_ranges->at(read));
5513     read++;
5514   } while (read < n);
5515   character_ranges->Rewind(num_canonical);
5516
5517   DCHECK(CharacterRange::IsCanonical(character_ranges));
5518 }
5519
5520
5521 void CharacterRange::Negate(ZoneList<CharacterRange>* ranges,
5522                             ZoneList<CharacterRange>* negated_ranges,
5523                             Zone* zone) {
5524   DCHECK(CharacterRange::IsCanonical(ranges));
5525   DCHECK_EQ(0, negated_ranges->length());
5526   int range_count = ranges->length();
5527   uc16 from = 0;
5528   int i = 0;
5529   if (range_count > 0 && ranges->at(0).from() == 0) {
5530     from = ranges->at(0).to();
5531     i = 1;
5532   }
5533   while (i < range_count) {
5534     CharacterRange range = ranges->at(i);
5535     negated_ranges->Add(CharacterRange(from + 1, range.from() - 1), zone);
5536     from = range.to();
5537     i++;
5538   }
5539   if (from < String::kMaxUtf16CodeUnit) {
5540     negated_ranges->Add(CharacterRange(from + 1, String::kMaxUtf16CodeUnit),
5541                         zone);
5542   }
5543 }
5544
5545
5546 // -------------------------------------------------------------------
5547 // Splay tree
5548
5549
5550 OutSet* OutSet::Extend(unsigned value, Zone* zone) {
5551   if (Get(value))
5552     return this;
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))
5557         return successor;
5558     }
5559   } else {
5560     successors_ = new(zone) ZoneList<OutSet*>(2, zone);
5561   }
5562   OutSet* result = new(zone) OutSet(first_, remaining_);
5563   result->Set(value, zone);
5564   successors(zone)->Add(result, zone);
5565   return result;
5566 }
5567
5568
5569 void OutSet::Set(unsigned value, Zone *zone) {
5570   if (value < kFirstLimit) {
5571     first_ |= (1 << value);
5572   } else {
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);
5577   }
5578 }
5579
5580
5581 bool OutSet::Get(unsigned value) const {
5582   if (value < kFirstLimit) {
5583     return (first_ & (1 << value)) != 0;
5584   } else if (remaining_ == NULL) {
5585     return false;
5586   } else {
5587     return remaining_->Contains(value);
5588   }
5589 }
5590
5591
5592 const uc16 DispatchTable::Config::kNoKey = unibrow::Utf8::kBadChar;
5593
5594
5595 void DispatchTable::AddRange(CharacterRange full_range, int value,
5596                              Zone* zone) {
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);
5602     DCHECK(inserted);
5603     USE(inserted);
5604     loc.set_value(Entry(current.from(), current.to(),
5605                         empty()->Extend(value, zone)));
5606     return;
5607   }
5608   // First see if there is a range to the left of this one that
5609   // overlaps.
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);
5630       DCHECK(inserted);
5631       USE(inserted);
5632       loc.set_value(Entry(right.from(),
5633                           right.to(),
5634                           entry->out_set()));
5635     }
5636   }
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);
5648         DCHECK(inserted);
5649         USE(inserted);
5650         ins.set_value(Entry(current.from(),
5651                             entry->from() - 1,
5652                             empty()->Extend(value, zone)));
5653         current.set_from(entry->from());
5654       }
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);
5661         DCHECK(inserted);
5662         USE(inserted);
5663         ins.set_value(Entry(current.to() + 1,
5664                             entry->to(),
5665                             entry->out_set()));
5666         entry->set_to(current.to());
5667       }
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)
5676         break;
5677       DCHECK(entry->to() + 1 > current.from());
5678       current.set_from(entry->to() + 1);
5679     } else {
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);
5683       DCHECK(inserted);
5684       USE(inserted);
5685       ins.set_value(Entry(current.from(),
5686                           current.to(),
5687                           empty()->Extend(value, zone)));
5688       break;
5689     }
5690   }
5691 }
5692
5693
5694 OutSet* DispatchTable::Get(uc16 value) {
5695   ZoneSplayTree<Config>::Locator loc;
5696   if (!tree()->FindGreatestLessThan(value, &loc))
5697     return empty();
5698   Entry* entry = &loc.value();
5699   if (value <= entry->to())
5700     return entry->out_set();
5701   else
5702     return empty();
5703 }
5704
5705
5706 // -------------------------------------------------------------------
5707 // Analysis
5708
5709
5710 void Analysis::EnsureAnalyzed(RegExpNode* that) {
5711   StackLimitCheck check(isolate());
5712   if (check.HasOverflowed()) {
5713     fail("Stack overflow");
5714     return;
5715   }
5716   if (that->info()->been_analyzed || that->info()->being_analyzed)
5717     return;
5718   that->info()->being_analyzed = true;
5719   that->Accept(this);
5720   that->info()->being_analyzed = false;
5721   that->info()->been_analyzed = true;
5722 }
5723
5724
5725 void Analysis::VisitEnd(EndNode* that) {
5726   // nothing to do
5727 }
5728
5729
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.
5734   int cp_offset = 0;
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();
5739   }
5740 }
5741
5742
5743 void Analysis::VisitText(TextNode* that) {
5744   if (ignore_case_) {
5745     that->MakeCaseIndependent(isolate(), is_one_byte_);
5746   }
5747   EnsureAnalyzed(that->on_success());
5748   if (!has_failed()) {
5749     that->CalculateOffsets();
5750   }
5751 }
5752
5753
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());
5761   }
5762 }
5763
5764
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());
5774   }
5775 }
5776
5777
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());
5786     }
5787   }
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());
5793   }
5794 }
5795
5796
5797 void Analysis::VisitBackReference(BackReferenceNode* that) {
5798   EnsureAnalyzed(that->on_success());
5799 }
5800
5801
5802 void Analysis::VisitAssertion(AssertionNode* that) {
5803   EnsureAnalyzed(that->on_success());
5804 }
5805
5806
5807 void BackReferenceNode::FillInBMInfo(int offset,
5808                                      int budget,
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);
5815 }
5816
5817
5818 STATIC_ASSERT(BoyerMoorePositionInfo::kMapSize ==
5819               RegExpMacroAssembler::kTableSize);
5820
5821
5822 void ChoiceNode::FillInBMInfo(int offset,
5823                               int budget,
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);
5833       return;
5834     }
5835     alt.node()->FillInBMInfo(offset, budget, bm, not_at_start);
5836   }
5837   SaveBMInfo(bm, not_at_start, offset);
5838 }
5839
5840
5841 void TextNode::FillInBMInfo(int initial_offset,
5842                             int budget,
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);
5851       return;
5852     }
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);
5859           return;
5860         }
5861         uc16 character = atom->data()[j];
5862         if (bm->compiler()->ignore_case()) {
5863           unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
5864           int length = GetCaseIndependentLetters(
5865               Isolate::Current(),
5866               character,
5867               bm->max_char() == String::kMaxOneByteCharCode,
5868               chars);
5869           for (int j = 0; j < length; j++) {
5870             bm->Set(offset, chars[j]);
5871           }
5872         } else {
5873           if (character <= max_char) bm->Set(offset, character);
5874         }
5875       }
5876     } else {
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()) {
5881         bm->SetAll(offset);
5882       } else {
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));
5888         }
5889       }
5890       offset++;
5891     }
5892   }
5893   if (offset >= bm->length()) {
5894     if (initial_offset == 0) set_bm_info(not_at_start, bm);
5895     return;
5896   }
5897   on_success()->FillInBMInfo(offset,
5898                              budget - 1,
5899                              bm,
5900                              true);  // Not at start after a text node.
5901   if (initial_offset == 0) set_bm_info(not_at_start, bm);
5902 }
5903
5904
5905 // -------------------------------------------------------------------
5906 // Dispatch table construction
5907
5908
5909 void DispatchTableConstructor::VisitEnd(EndNode* that) {
5910   AddRange(CharacterRange::Everything());
5911 }
5912
5913
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);
5920   }
5921   node->set_being_calculated(false);
5922 }
5923
5924
5925 class AddDispatchRange {
5926  public:
5927   explicit AddDispatchRange(DispatchTableConstructor* constructor)
5928     : constructor_(constructor) { }
5929   void Call(uc32 from, DispatchTable::Entry entry);
5930  private:
5931   DispatchTableConstructor* constructor_;
5932 };
5933
5934
5935 void AddDispatchRange::Call(uc32 from, DispatchTable::Entry entry) {
5936   CharacterRange range(from, entry.to());
5937   constructor_->AddRange(range);
5938 }
5939
5940
5941 void DispatchTableConstructor::VisitChoice(ChoiceNode* node) {
5942   if (node->being_calculated())
5943     return;
5944   DispatchTable* table = node->GetTable(ignore_case_);
5945   AddDispatchRange adder(this);
5946   table->ForEach(&adder);
5947 }
5948
5949
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());
5954 }
5955
5956
5957 void DispatchTableConstructor::VisitAssertion(AssertionNode* that) {
5958   RegExpNode* target = that->on_success();
5959   target->Accept(this);
5960 }
5961
5962
5963 static int CompareRangeByFrom(const CharacterRange* a,
5964                               const CharacterRange* b) {
5965   return Compare<uc16>(a->from(), b->from());
5966 }
5967
5968
5969 void DispatchTableConstructor::AddInverse(ZoneList<CharacterRange>* ranges) {
5970   ranges->Sort(CompareRangeByFrom);
5971   uc16 last = 0;
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) {
5978         return;
5979       } else {
5980         last = range.to() + 1;
5981       }
5982     }
5983   }
5984   AddRange(CharacterRange(last, String::kMaxUtf16CodeUnit));
5985 }
5986
5987
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));
5994       break;
5995     }
5996     case TextElement::CHAR_CLASS: {
5997       RegExpCharacterClass* tree = elm.char_class();
5998       ZoneList<CharacterRange>* ranges = tree->ranges(that->zone());
5999       if (tree->is_negated()) {
6000         AddInverse(ranges);
6001       } else {
6002         for (int i = 0; i < ranges->length(); i++)
6003           AddRange(ranges->at(i));
6004       }
6005       break;
6006     }
6007     default: {
6008       UNIMPLEMENTED();
6009     }
6010   }
6011 }
6012
6013
6014 void DispatchTableConstructor::VisitAction(ActionNode* that) {
6015   RegExpNode* target = that->on_success();
6016   target->Accept(this);
6017 }
6018
6019
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);
6026   }
6027   RegExpCompiler compiler(isolate, zone, data->capture_count, ignore_case,
6028                           is_one_byte);
6029
6030   compiler.set_optimize(!TooMuchRegExpCode(pattern));
6031
6032   // Sample some characters from the middle of the string.
6033   static const int kSampleSize = 128;
6034
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));
6042   }
6043
6044   // Wrap the body of the regexp in capture #0.
6045   RegExpNode* captured_body = RegExpCapture::ToNode(data->tree,
6046                                                     0,
6047                                                     &compiler,
6048                                                     compiler.accept());
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,
6059                                  false,
6060                                  new(zone) RegExpCharacterClass('*'),
6061                                  &compiler,
6062                                  captured_body,
6063                                  data->contains_anchor);
6064
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;
6073     } else {
6074       node = loop_node;
6075     }
6076   }
6077   if (is_one_byte) {
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.
6081     if (node != NULL) {
6082       node = node->FilterOneByte(RegExpCompiler::kMaxRecursion, ignore_case);
6083     }
6084   }
6085
6086   if (node == NULL) node = new(zone) EndNode(EndNode::BACKTRACK, zone);
6087   data->node = node;
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);
6093   }
6094
6095   // Create the correct assembler for the architecture.
6096 #ifndef V8_INTERPRETED_REGEXP
6097   // Native regexp implementation.
6098
6099   NativeRegExpMacroAssembler::Mode mode =
6100       is_one_byte ? NativeRegExpMacroAssembler::LATIN1
6101                   : NativeRegExpMacroAssembler::UC16;
6102
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);
6127 #else
6128 #error "Unsupported architecture"
6129 #endif
6130
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
6136
6137   macro_assembler.set_slow_safe(TooMuchRegExpCode(pattern));
6138
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);
6146   }
6147
6148   if (is_global) {
6149     macro_assembler.set_global_mode(
6150         (data->tree->min_match() > 0)
6151             ? RegExpMacroAssembler::GLOBAL_NO_ZERO_LENGTH_CHECK
6152             : RegExpMacroAssembler::GLOBAL);
6153   }
6154
6155   return compiler.Assemble(&macro_assembler,
6156                            node,
6157                            data->capture_count,
6158                            pattern);
6159 }
6160
6161
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) {
6168     too_much = true;
6169   }
6170   return too_much;
6171 }
6172 }}  // namespace v8::internal